18 .offset = offsetof(
Data, localSizeX),
19 .size =
sizeof(uint32_t),
28 .offset = offsetof(
Data, localSizeX),
29 .size =
sizeof(uint32_t),
33 .offset = offsetof(
Data, nextStageThreadCount),
34 .size =
sizeof(uint32_t),
43 .offset = offsetof(
Data, screenWidth),
44 .size =
sizeof(float),
48 .offset = offsetof(
Data, screenHeight),
49 .size =
sizeof(float),
54#if !defined(HEADLESS) && !defined(COMPUTE_DEBUG)
57 VkPipelineLayout pipelineLayout,
58 VkRenderPass renderPass,
59 const std::string& meshShaderFilename,
60 const std::string& fragmentShaderFilename,
61 const std::vector<VkVertexInputBindingDescription>& vertexInputBindingDescriptions,
62 const std::vector<VkVertexInputAttributeDescription>& vertexInputAttributeDescriptions,
64 std::optional<const MeshShaderSpecializationData*> meshShaderSpecialization,
78 VkSpecializationInfo meshSpecInfo{};
79 std::vector<VkSpecializationMapEntry> meshSpecEntries;
80 if (meshShaderSpecialization.has_value()) {
81 meshSpecEntries = meshShaderSpecialization.value()->getMapEntries();
83 .mapEntryCount =
static_cast<uint32_t
>(meshSpecEntries.size()),
84 .pMapEntries = meshSpecEntries.data(),
85 .dataSize = meshShaderSpecialization.value()->getDataSize(),
86 .pData = meshShaderSpecialization.value()->getData()
90 VkPipelineShaderStageCreateInfo pipelineShaderStageCreateInfoMesh{
91 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
93 pipelineShaderStageCreateInfoMesh.module = shader.
getMeshShader();
94 pipelineShaderStageCreateInfoMesh.stage = VK_SHADER_STAGE_MESH_BIT_EXT;
95 pipelineShaderStageCreateInfoMesh.pName =
"main";
96 pipelineShaderStageCreateInfoMesh.pSpecializationInfo = meshShaderSpecialization.has_value() ? &meshSpecInfo :
nullptr;
98 VkPipelineShaderStageCreateInfo pipelineShaderStageCreateInfoFragment{
99 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
102 pipelineShaderStageCreateInfoFragment.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
103 pipelineShaderStageCreateInfoFragment.pName =
"main";
105 const std::array shaderStages = { pipelineShaderStageCreateInfoMesh, pipelineShaderStageCreateInfoFragment };
107 VkPipelineViewportStateCreateInfo pipelineViewportStateCreateInfo{
108 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
110 pipelineViewportStateCreateInfo.viewportCount = 1u;
111 pipelineViewportStateCreateInfo.scissorCount = 1u;
113 VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo{
114 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
116 pipelineRasterizationStateCreateInfo.polygonMode = VK_POLYGON_MODE_FILL;
117 pipelineRasterizationStateCreateInfo.lineWidth = 1.0f;
118 pipelineRasterizationStateCreateInfo.cullMode =
pipelineData.cullMode;
120 VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo{
121 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
123 pipelineMultisampleStateCreateInfo.rasterizationSamples =
multisampling;
125 VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo{
126 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
128 VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState{};
129 pipelineColorBlendAttachmentState.colorWriteMask =
130 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
131 pipelineColorBlendAttachmentState.blendEnable = VK_TRUE;
132 pipelineColorBlendAttachmentState.srcColorBlendFactor =
pipelineData.srcColorBlendFactor;
133 pipelineColorBlendAttachmentState.dstColorBlendFactor =
pipelineData.dstColorBlendFactor;
134 pipelineColorBlendAttachmentState.colorBlendOp =
pipelineData.colorBlendOp;
135 pipelineColorBlendAttachmentState.srcAlphaBlendFactor =
pipelineData.srcAlphaBlendFactor;
136 pipelineColorBlendAttachmentState.dstAlphaBlendFactor =
pipelineData.dstAlphaBlendFactor;
137 pipelineColorBlendAttachmentState.alphaBlendOp =
pipelineData.alphaBlendOp;
141 pipelineColorBlendStateCreateInfo.attachmentCount = 0;
142 pipelineColorBlendStateCreateInfo.pAttachments =
nullptr;
144 pipelineColorBlendStateCreateInfo.attachmentCount = 1;
145 pipelineColorBlendStateCreateInfo.pAttachments = &pipelineColorBlendAttachmentState;
148 VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo{
149 VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
151 constexpr std::array dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
152 pipelineDynamicStateCreateInfo.dynamicStateCount =
static_cast<uint32_t
>(dynamicStates.size());
153 pipelineDynamicStateCreateInfo.pDynamicStates = dynamicStates.data();
155 VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo{
156 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO
158 pipelineDepthStencilStateCreateInfo.depthTestEnable = VK_TRUE;
159 pipelineDepthStencilStateCreateInfo.depthWriteEnable = VK_TRUE;
160 pipelineDepthStencilStateCreateInfo.depthCompareOp = VK_COMPARE_OP_GREATER;
163 constexpr VkFormat colorFormat = VK_FORMAT_R8G8B8A8_SRGB;
164 constexpr VkFormat depthFormat = VK_FORMAT_D32_SFLOAT;
165 VkPipelineRenderingCreateInfo pipelineRenderingCreateInfo{
166 .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
168 .colorAttachmentCount = depthOnly ? 0u : 1u,
169 .pColorAttachmentFormats = depthOnly ? nullptr : &colorFormat,
170 .depthAttachmentFormat = depthFormat,
171 .stencilAttachmentFormat = VK_FORMAT_UNDEFINED
174 VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{ VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
175 graphicsPipelineCreateInfo.pNext = (renderPass == VK_NULL_HANDLE) ? &pipelineRenderingCreateInfo :
nullptr;
176 graphicsPipelineCreateInfo.layout = pipelineLayout;
177 graphicsPipelineCreateInfo.stageCount =
static_cast<uint32_t
>(shaderStages.size());
178 graphicsPipelineCreateInfo.pStages = shaderStages.data();
179 graphicsPipelineCreateInfo.pVertexInputState =
nullptr;
180 graphicsPipelineCreateInfo.pInputAssemblyState =
nullptr;
181 graphicsPipelineCreateInfo.pViewportState = &pipelineViewportStateCreateInfo;
182 graphicsPipelineCreateInfo.pRasterizationState = &pipelineRasterizationStateCreateInfo;
183 graphicsPipelineCreateInfo.pMultisampleState = &pipelineMultisampleStateCreateInfo;
184 graphicsPipelineCreateInfo.pColorBlendState = &pipelineColorBlendStateCreateInfo;
185 graphicsPipelineCreateInfo.pDynamicState = &pipelineDynamicStateCreateInfo;
186 graphicsPipelineCreateInfo.pDepthStencilState = &pipelineDepthStencilStateCreateInfo;
187 graphicsPipelineCreateInfo.renderPass = renderPass;
189 VkResult pipelineCreationResult = vkCreateGraphicsPipelines(
device,
nullptr, 1, &graphicsPipelineCreateInfo,
nullptr, &
pipeline);
190 if (pipelineCreationResult != VK_SUCCESS)
192 throw std::runtime_error(
"Failed to create vk graphics pipeline");
202 VkSampleCountFlagBits multisampling,
203 VkPipelineLayout pipelineLayout,
204 VkRenderPass renderPass,
205 const std::string & vertexShaderName,
206 const std::string & fragmentShaderName
207 ) : device(device), multisampling(multisampling)
211 VkPipelineShaderStageCreateInfo vertexStageInfo{
212 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
213 .stage = VK_SHADER_STAGE_VERTEX_BIT,
214 .module = shader.getVertexShader(),
218 VkPipelineShaderStageCreateInfo fragmentStageInfo{
219 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
220 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
221 .module = shader.getFragmentShader(),
225 const std::array shaderStages = { vertexStageInfo, fragmentStageInfo };
227 VkPipelineVertexInputStateCreateInfo vertexInputState{
228 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
229 .vertexBindingDescriptionCount = 0,
230 .pVertexBindingDescriptions =
nullptr,
231 .vertexAttributeDescriptionCount = 0,
232 .pVertexAttributeDescriptions =
nullptr
235 VkPipelineInputAssemblyStateCreateInfo inputAssemblyState{
236 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
237 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
238 .primitiveRestartEnable = VK_FALSE
241 VkPipelineViewportStateCreateInfo viewportState{
242 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
244 .pViewports =
nullptr,
249 VkPipelineRasterizationStateCreateInfo rasterizationState{
250 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
251 .polygonMode = VK_POLYGON_MODE_FILL,
252 .cullMode = VK_CULL_MODE_NONE,
253 .frontFace = VK_FRONT_FACE_CLOCKWISE,
257 VkPipelineMultisampleStateCreateInfo multisampleState{
258 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
262 VkPipelineDepthStencilStateCreateInfo depthStencilState{
263 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
264 .depthTestEnable = VK_FALSE,
265 .depthWriteEnable = VK_FALSE,
266 .depthCompareOp = VK_COMPARE_OP_GREATER
269 VkPipelineColorBlendAttachmentState colorBlendAttachment{
270 .blendEnable = VK_FALSE,
271 .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
274 VkPipelineColorBlendStateCreateInfo colorBlendState{
275 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
276 .attachmentCount = 1,
277 .pAttachments = &colorBlendAttachment
280 constexpr std::array dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
281 VkPipelineDynamicStateCreateInfo dynamicState{
282 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
283 .dynamicStateCount =
static_cast<uint32_t
>(dynamicStates.size()),
284 .pDynamicStates = dynamicStates.data()
289 const VkFormat colorFormat = VK_FORMAT_R8G8B8A8_SRGB;
291 VkPipelineRenderingCreateInfo pipelineRenderingCreateInfo{
292 .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
295 .colorAttachmentCount = 1,
296 .pColorAttachmentFormats = &colorFormat,
297 .depthAttachmentFormat = VK_FORMAT_UNDEFINED,
298 .stencilAttachmentFormat = VK_FORMAT_UNDEFINED
301 VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{
302 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
303 .pNext = (renderPass == VK_NULL_HANDLE) ? &pipelineRenderingCreateInfo : nullptr,
305 .stageCount = static_cast<uint32_t>(shaderStages.size()),
306 .pStages = shaderStages.data(),
307 .pVertexInputState = &vertexInputState,
308 .pInputAssemblyState = &inputAssemblyState,
309 .pTessellationState = nullptr,
310 .pViewportState = &viewportState,
311 .pRasterizationState = &rasterizationState,
312 .pMultisampleState = &multisampleState,
313 .pDepthStencilState = &depthStencilState,
314 .pColorBlendState = &colorBlendState,
315 .pDynamicState = &dynamicState,
316 .layout = pipelineLayout,
317 .renderPass = renderPass,
319 .basePipelineHandle = VK_NULL_HANDLE,
320 .basePipelineIndex = -1
323 VkResult pipelineCreationResult = vkCreateGraphicsPipelines(
device,
nullptr, 1, &graphicsPipelineCreateInfo,
nullptr, &
pipeline);
324 if (pipelineCreationResult != VK_SUCCESS)
326 throw std::runtime_error(
"Failed to create vk graphics pipeline");
329 shader.destroyShaders(
device );
345 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline);
380 VkPipelineShaderStageCreateInfo computeShaderStageInfo{
381 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
382 .stage = VK_SHADER_STAGE_COMPUTE_BIT,
385 .pSpecializationInfo =
nullptr
388 VkSpecializationInfo specializationInfo{};
389 std::vector<VkSpecializationMapEntry> specializationData;
390 if (pSpecializationData.has_value()) {
391 specializationData = pSpecializationData.value()->getMapEntries();
392 specializationInfo = {
393 .mapEntryCount =
static_cast<uint32_t
>(specializationData.size()),
394 .pMapEntries = specializationData.data(),
395 .dataSize = pSpecializationData.value()->getDataSize(),
396 .pData = pSpecializationData.value()->getData()
398 computeShaderStageInfo.pSpecializationInfo = &specializationInfo;
402 specializationData = std::vector<VkSpecializationMapEntry>();
405 VkComputePipelineCreateInfo computePipelineCreateInfo{
406 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
409 .stage = computeShaderStageInfo,
413 PLOG_FN_VK(vkCreateComputePipelines(
device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo,
nullptr, &
pipeline));
440 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE,
pipeline);
#define TRACY_ZONE_SCOPED_NAMED(name)
std::vector< VkSpecializationMapEntry > getMapEntries() const override
Gets the specialization data which will be uploaded to a shader.
void bind(VkCommandBuffer commandBuffer)
Binds this pipeline to an arbitrary command buffer.
void cleanup()
Cleans up all the resource handles of this object.
ComputePipeline(VkDevice device, VkPipelineLayout layout, std::string shaderFilename, std::optional< const PipelineSpecializationData * > pSpecializationData)
ComputeShader * computeShader
std::vector< VkSpecializationMapEntry > getMapEntries() const override
Creates the list of specialization map entries which are applied to a shader.
VkSampleCountFlagBits multisampling
std::string fragShaderName
bool operator==(const GraphicsPipeline &other) const
void bind(VkCommandBuffer commandBuffer) const
PipelineMaterialPayload pipelineData
const PipelineMaterialPayload & getPipelineMaterialData() const
GraphicsPipeline(VkDevice device, VkSampleCountFlagBits multisampling, VkPipelineLayout pipelineLayout, VkRenderPass renderPass, const std::string &meshShaderFilename, const std::string &fragmentShaderFilename, const std::vector< VkVertexInputBindingDescription > &vertexInputBindingDescriptions, const std::vector< VkVertexInputAttributeDescription > &vertexInputAttributeDescriptions, PipelineMaterialPayload pipelineData, std::optional< const MeshShaderSpecializationData * > meshShaderSpecialization=std::nullopt, bool depthOnly=false)
const std::string getFragShaderName() const
std::string meshShaderName
const std::string getMeshShaderName() const
bool isValid() const
Checks if the object is fully constructed.
std::vector< VkSpecializationMapEntry > getMapEntries() const override
Creates the list of specialization map entries which are applied to a shader.
void destroyShaders(VkDevice device) override
VkShaderModule getFragmentShader() const
VkShaderModule getMeshShader() const
static void setObjectName(VkDevice device, VulkanObjectType objectHandle, const std::string &name)
Log category system implementation.