PipelineStateVK.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Renderer/VK/PipelineStateVK.h>
  6. #include <Renderer/VK/RendererVK.h>
  7. #include <Renderer/VK/FatalErrorIfFailedVK.h>
  8. PipelineStateVK::PipelineStateVK(RendererVK *inRenderer, const VertexShaderVK *inVertexShader, const EInputDescription *inInputDescription, uint inInputDescriptionCount, const PixelShaderVK *inPixelShader, EDrawPass inDrawPass, EFillMode inFillMode, ETopology inTopology, EDepthTest inDepthTest, EBlendMode inBlendMode, ECullMode inCullMode) :
  9. mRenderer(inRenderer),
  10. mVertexShader(inVertexShader),
  11. mPixelShader(inPixelShader)
  12. {
  13. VkPipelineShaderStageCreateInfo shader_stages[] = { inVertexShader->mStageInfo, inPixelShader->mStageInfo };
  14. // TODO: This doesn't follow the SPIR-V alignment rules
  15. Array<VkVertexInputAttributeDescription> attribute_descriptions;
  16. VkVertexInputAttributeDescription temp_vtx = { }, temp_instance = { };
  17. temp_instance.binding = 1;
  18. uint instance_alignment = 1;
  19. for (uint i = 0; i < inInputDescriptionCount; ++i)
  20. switch (inInputDescription[i])
  21. {
  22. case EInputDescription::Position:
  23. case EInputDescription::Normal:
  24. temp_vtx.format = VK_FORMAT_R32G32B32_SFLOAT;
  25. attribute_descriptions.push_back(temp_vtx);
  26. temp_vtx.offset += 3 * sizeof(float);
  27. break;
  28. case EInputDescription::Color:
  29. temp_vtx.format = VK_FORMAT_R8G8B8A8_UNORM;
  30. attribute_descriptions.push_back(temp_vtx);
  31. temp_vtx.offset += 4 * sizeof(uint8);
  32. break;
  33. case EInputDescription::TexCoord:
  34. temp_vtx.format = VK_FORMAT_R32G32_SFLOAT;
  35. attribute_descriptions.push_back(temp_vtx);
  36. temp_vtx.offset += 2 * sizeof(float);
  37. break;
  38. case EInputDescription::InstanceColor:
  39. instance_alignment = max(instance_alignment, 4u);
  40. temp_instance.format = VK_FORMAT_R8G8B8A8_UNORM;
  41. attribute_descriptions.push_back(temp_instance);
  42. temp_instance.offset += 4 * sizeof(uint8);
  43. break;
  44. case EInputDescription::InstanceTransform:
  45. case EInputDescription::InstanceInvTransform:
  46. instance_alignment = max(instance_alignment, 16u);
  47. temp_instance.format = VK_FORMAT_R32G32B32A32_SFLOAT;
  48. for (int j = 0; j < 4; ++j)
  49. {
  50. attribute_descriptions.push_back(temp_instance);
  51. temp_instance.offset += 4 * sizeof(float);
  52. }
  53. break;
  54. }
  55. for (uint32 i = 0; i < uint32(attribute_descriptions.size()); ++i)
  56. attribute_descriptions[i].location = i;
  57. VkVertexInputBindingDescription binding_description[2];
  58. binding_description[0].binding = 0;
  59. binding_description[0].stride = temp_vtx.offset;
  60. binding_description[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  61. binding_description[1].binding = 1;
  62. binding_description[1].stride = AlignUp(temp_instance.offset, instance_alignment);
  63. binding_description[1].inputRate = VK_VERTEX_INPUT_RATE_INSTANCE;
  64. VkPipelineVertexInputStateCreateInfo vertex_input_info = {};
  65. vertex_input_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  66. vertex_input_info.vertexBindingDescriptionCount = temp_instance.offset > 0? 2 : 1;
  67. vertex_input_info.pVertexBindingDescriptions = binding_description;
  68. vertex_input_info.vertexAttributeDescriptionCount = uint32(attribute_descriptions.size());
  69. vertex_input_info.pVertexAttributeDescriptions = attribute_descriptions.data();
  70. VkPipelineInputAssemblyStateCreateInfo input_assembly = {};
  71. input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  72. input_assembly.topology = inTopology == ETopology::Triangle? VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST : VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
  73. input_assembly.primitiveRestartEnable = VK_FALSE;
  74. VkPipelineViewportStateCreateInfo viewport_state = {};
  75. viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  76. viewport_state.viewportCount = 1;
  77. viewport_state.scissorCount = 1;
  78. VkPipelineRasterizationStateCreateInfo rasterizer = {};
  79. rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  80. rasterizer.depthClampEnable = VK_FALSE;
  81. rasterizer.rasterizerDiscardEnable = VK_FALSE;
  82. rasterizer.polygonMode = inFillMode == EFillMode::Solid? VK_POLYGON_MODE_FILL : VK_POLYGON_MODE_LINE;
  83. rasterizer.lineWidth = 1.0f;
  84. rasterizer.cullMode = inCullMode == ECullMode::Backface? VK_CULL_MODE_BACK_BIT : VK_CULL_MODE_FRONT_BIT;
  85. rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  86. rasterizer.depthBiasEnable = VK_FALSE;
  87. VkPipelineMultisampleStateCreateInfo multisampling = {};
  88. multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  89. multisampling.sampleShadingEnable = VK_FALSE;
  90. multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  91. VkPipelineDepthStencilStateCreateInfo depth_stencil = {};
  92. depth_stencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  93. depth_stencil.depthTestEnable = inDepthTest == EDepthTest::On? VK_TRUE : VK_FALSE;
  94. depth_stencil.depthWriteEnable = inDepthTest == EDepthTest::On? VK_TRUE : VK_FALSE;
  95. depth_stencil.depthCompareOp = VK_COMPARE_OP_GREATER; // Reverse-Z, greater is closer
  96. VkPipelineColorBlendAttachmentState color_blend_attachment = {};
  97. color_blend_attachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  98. switch (inBlendMode)
  99. {
  100. case EBlendMode::Write:
  101. color_blend_attachment.blendEnable = VK_FALSE;
  102. break;
  103. case EBlendMode::AlphaBlend:
  104. color_blend_attachment.blendEnable = VK_TRUE;
  105. color_blend_attachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  106. color_blend_attachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  107. color_blend_attachment.colorBlendOp = VK_BLEND_OP_ADD;
  108. color_blend_attachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  109. color_blend_attachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  110. color_blend_attachment.alphaBlendOp = VK_BLEND_OP_ADD;
  111. break;
  112. }
  113. VkPipelineColorBlendStateCreateInfo color_blending = {};
  114. color_blending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  115. color_blending.logicOpEnable = VK_FALSE;
  116. color_blending.logicOp = VK_LOGIC_OP_COPY;
  117. color_blending.attachmentCount = 1;
  118. color_blending.pAttachments = &color_blend_attachment;
  119. VkDynamicState dynamic_states[] = {
  120. VK_DYNAMIC_STATE_VIEWPORT,
  121. VK_DYNAMIC_STATE_SCISSOR
  122. };
  123. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  124. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  125. dynamic_state.dynamicStateCount = std::size(dynamic_states);
  126. dynamic_state.pDynamicStates = dynamic_states;
  127. VkGraphicsPipelineCreateInfo pipeline_info = {};
  128. pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  129. pipeline_info.stageCount = std::size(shader_stages);
  130. pipeline_info.pStages = shader_stages;
  131. pipeline_info.pVertexInputState = &vertex_input_info;
  132. pipeline_info.pInputAssemblyState = &input_assembly;
  133. pipeline_info.pViewportState = &viewport_state;
  134. pipeline_info.pRasterizationState = &rasterizer;
  135. pipeline_info.pMultisampleState = &multisampling;
  136. pipeline_info.pDepthStencilState = &depth_stencil;
  137. pipeline_info.pColorBlendState = &color_blending;
  138. pipeline_info.pDynamicState = &dynamic_state;
  139. pipeline_info.layout = mRenderer->GetPipelineLayout();
  140. pipeline_info.renderPass = inDrawPass == EDrawPass::Normal? mRenderer->GetRenderPass() : mRenderer->GetRenderPassShadow();
  141. FatalErrorIfFailed(vkCreateGraphicsPipelines(mRenderer->GetDevice(), VK_NULL_HANDLE, 1, &pipeline_info, nullptr, &mGraphicsPipeline));
  142. }
  143. PipelineStateVK::~PipelineStateVK()
  144. {
  145. vkDeviceWaitIdle(mRenderer->GetDevice());
  146. vkDestroyPipeline(mRenderer->GetDevice(), mGraphicsPipeline, nullptr);
  147. }
  148. void PipelineStateVK::Activate()
  149. {
  150. vkCmdBindPipeline(mRenderer->GetCommandBuffer(), VK_PIPELINE_BIND_POINT_GRAPHICS, mGraphicsPipeline);
  151. }