#include "PipelineCache.h" #include "Descriptor.h" #include "Enums.h" #include "Graphics.h" #include "RenderPass.h" #include "Shader.h" #include using namespace Seele; using namespace Seele::Vulkan; PipelineCache::PipelineCache(PGraphics graphics, const std::string& cacheFilePath) : graphics(graphics), cacheFile(cacheFilePath) { std::ifstream stream(cacheFilePath, std::ios::binary | std::ios::ate); VkPipelineCacheCreateInfo cacheCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, .pNext = nullptr, .flags = 0, .initialDataSize = 0, }; if (stream.good()) { Array cacheData; uint32 fileSize = static_cast(stream.tellg()); cacheData.resize(fileSize); stream.seekg(0); stream.read((char*)cacheData.data(), fileSize); cacheCreateInfo.initialDataSize = fileSize; cacheCreateInfo.pInitialData = cacheData.data(); std::cout << "Loaded " << fileSize << " bytes from pipeline cache" << std::endl; } VK_CHECK(vkCreatePipelineCache(graphics->getDevice(), &cacheCreateInfo, nullptr, &cache)); } PipelineCache::~PipelineCache() { size_t cacheSize; VK_CHECK(vkGetPipelineCacheData(graphics->getDevice(), cache, &cacheSize, nullptr)); Array cacheData(cacheSize); VK_CHECK(vkGetPipelineCacheData(graphics->getDevice(), cache, &cacheSize, cacheData.data())); std::ofstream stream(cacheFile, std::ios::binary); stream.write((char*)cacheData.data(), cacheSize); stream.flush(); stream.close(); vkDestroyPipelineCache(graphics->getDevice(), cache, nullptr); std::cout << "Written " << cacheSize << " bytes to cache" << std::endl; } PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo gfxInfo) { uint32 hash = CRC::Calculate(&gfxInfo, sizeof(Gfx::LegacyPipelineCreateInfo), CRC::CRC_32()); if (graphicsPipelines.contains(hash)) { return graphicsPipelines[hash]; } PPipelineLayout layout = Gfx::PPipelineLayout(gfxInfo.pipelineLayout).cast(); Array bindings; Array attributes; if (gfxInfo.vertexInput != nullptr) { const VertexInputStateCreateInfo& vertexInputDesc = gfxInfo.vertexInput->getInfo(); for (const auto& b : vertexInputDesc.bindings) { bindings.add() = { .binding = b.binding, .stride = b.stride, .inputRate = VkVertexInputRate(b.inputRate), }; } for (const auto& a : vertexInputDesc.attributes) { attributes.add() = { .location = a.location, .binding = a.binding, .format = cast(a.format), .offset = a.offset, }; } } VkPipelineVertexInputStateCreateInfo vertexInput = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .vertexBindingDescriptionCount = (uint32)bindings.size(), .pVertexBindingDescriptions = bindings.data(), .vertexAttributeDescriptionCount = (uint32)attributes.size(), .pVertexAttributeDescriptions = attributes.data(), }; uint32 stageCount = 0; VkPipelineShaderStageCreateInfo stageInfos[2]; std::memset(stageInfos, 0, sizeof(stageInfos)); PVertexShader vertexShader = gfxInfo.vertexShader.cast(); stageInfos[stageCount++] = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vertexShader->getModuleHandle(), .pName = vertexShader->getEntryPointName(), .pSpecializationInfo = nullptr, }; if (gfxInfo.fragmentShader != nullptr) { PFragmentShader fragment = gfxInfo.fragmentShader.cast(); stageInfos[stageCount++] = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fragment->getModuleHandle(), .pName = fragment->getEntryPointName(), .pSpecializationInfo = nullptr, }; } VkPipelineInputAssemblyStateCreateInfo assemblyInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .topology = cast(gfxInfo.topology), .primitiveRestartEnable = false, }; VkPipelineViewportStateCreateInfo viewportInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .viewportCount = 1, .pViewports = nullptr, .scissorCount = 1, .pScissors = nullptr, }; VkPipelineRasterizationStateCreateInfo rasterizationState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .depthClampEnable = gfxInfo.rasterizationState.depthClampEnable, .rasterizerDiscardEnable = gfxInfo.rasterizationState.rasterizerDiscardEnable, .polygonMode = cast(gfxInfo.rasterizationState.polygonMode), .cullMode = gfxInfo.rasterizationState.cullMode, .frontFace = (VkFrontFace)gfxInfo.rasterizationState.frontFace, .depthBiasEnable = gfxInfo.rasterizationState.depthBiasEnable, .depthBiasConstantFactor = gfxInfo.rasterizationState.depthBiasConstantFactor, .depthBiasClamp = gfxInfo.rasterizationState.depthBiasClamp, .depthBiasSlopeFactor = gfxInfo.rasterizationState.depthBiasSlopeFactor, .lineWidth = gfxInfo.rasterizationState.lineWidth, }; VkPipelineMultisampleStateCreateInfo multisampleState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .rasterizationSamples = (VkSampleCountFlagBits)gfxInfo.multisampleState.samples, .sampleShadingEnable = gfxInfo.multisampleState.sampleShadingEnable, .minSampleShading = gfxInfo.multisampleState.minSampleShading, .alphaToCoverageEnable = gfxInfo.multisampleState.alphaCoverageEnable, .alphaToOneEnable = gfxInfo.multisampleState.alphaToOneEnable, }; VkPipelineDepthStencilStateCreateInfo depthStencilState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .depthTestEnable = gfxInfo.depthStencilState.depthTestEnable, .depthWriteEnable = gfxInfo.depthStencilState.depthWriteEnable, .depthCompareOp = cast(gfxInfo.depthStencilState.depthCompareOp), .depthBoundsTestEnable = gfxInfo.depthStencilState.depthBoundsTestEnable, .front = {(VkStencilOp)gfxInfo.depthStencilState.front}, .back = {(VkStencilOp)gfxInfo.depthStencilState.back}, .minDepthBounds = gfxInfo.depthStencilState.minDepthBounds, .maxDepthBounds = gfxInfo.depthStencilState.maxDepthBounds, }; Array blendAttachments; for (uint32 i = 0; i < gfxInfo.colorBlend.attachmentCount; ++i) { const Gfx::ColorBlendState::BlendAttachment& attachment = gfxInfo.colorBlend.blendAttachments[i]; blendAttachments.add() = { .blendEnable = attachment.blendEnable, .srcColorBlendFactor = (VkBlendFactor)attachment.srcColorBlendFactor, .dstColorBlendFactor = (VkBlendFactor)attachment.dstColorBlendFactor, .colorBlendOp = (VkBlendOp)attachment.colorBlendOp, .srcAlphaBlendFactor = (VkBlendFactor)attachment.srcAlphaBlendFactor, .dstAlphaBlendFactor = (VkBlendFactor)attachment.dstAlphaBlendFactor, .alphaBlendOp = (VkBlendOp)attachment.alphaBlendOp, .colorWriteMask = attachment.colorWriteMask, }; } VkPipelineColorBlendStateCreateInfo blendState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .logicOpEnable = gfxInfo.colorBlend.logicOpEnable, .logicOp = (VkLogicOp)gfxInfo.colorBlend.logicOp, .attachmentCount = (uint32)blendAttachments.size(), .pAttachments = blendAttachments.data(), }; std::memcpy(blendState.blendConstants, gfxInfo.colorBlend.blendConstants.data(), sizeof(blendState.blendConstants)); uint32 numDynamicEnabled = 0; StaticArray dynamicEnabled; dynamicEnabled[numDynamicEnabled++] = VK_DYNAMIC_STATE_VIEWPORT; dynamicEnabled[numDynamicEnabled++] = VK_DYNAMIC_STATE_SCISSOR; VkPipelineDynamicStateCreateInfo dynamicState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .dynamicStateCount = (uint32)dynamicEnabled.size(), .pDynamicStates = dynamicEnabled.data(), }; VkPipeline pipelineHandle; VkGraphicsPipelineCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .pNext = 0, .flags = 0, .stageCount = stageCount, .pStages = stageInfos, .pVertexInputState = &vertexInput, .pInputAssemblyState = &assemblyInfo, .pViewportState = &viewportInfo, .pRasterizationState = &rasterizationState, .pMultisampleState = &multisampleState, .pDepthStencilState = &depthStencilState, .pColorBlendState = &blendState, .pDynamicState = &dynamicState, .layout = layout->getHandle(), .renderPass = gfxInfo.renderPass.cast()->getHandle(), .subpass = 0, }; auto beginTime = std::chrono::high_resolution_clock::now(); VK_CHECK(vkCreateGraphicsPipelines(graphics->getDevice(), cache, 1, &createInfo, nullptr, &pipelineHandle)); auto endTime = std::chrono::high_resolution_clock::now(); int64 delta = std::chrono::duration_cast(endTime - beginTime).count(); std::cout << "Gfx creation time: " << delta << std::endl; OGraphicsPipeline pipeline = new GraphicsPipeline(graphics, pipelineHandle, gfxInfo.pipelineLayout); PGraphicsPipeline result = pipeline; graphicsPipelines[hash] = std::move(pipeline); return result; } PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo gfxInfo) { uint32 hash = CRC::Calculate(&gfxInfo, sizeof(Gfx::MeshPipelineCreateInfo), CRC::CRC_32()); if (graphicsPipelines.contains(hash)) { return graphicsPipelines[hash]; } PPipelineLayout layout = Gfx::PPipelineLayout(gfxInfo.pipelineLayout).cast(); // uint32 hash = layout->getHash(); uint32 stageCount = 0; VkPipelineShaderStageCreateInfo stageInfos[3]; std::memset(stageInfos, 0, sizeof(stageInfos)); if (gfxInfo.taskShader != nullptr) { PTaskShader taskShader = gfxInfo.taskShader.cast(); stageInfos[stageCount++] = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_TASK_BIT_EXT, .module = taskShader->getModuleHandle(), .pName = taskShader->getEntryPointName(), .pSpecializationInfo = nullptr, }; } PMeshShader meshShader = gfxInfo.meshShader.cast(); stageInfos[stageCount++] = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_MESH_BIT_EXT, .module = meshShader->getModuleHandle(), .pName = meshShader->getEntryPointName(), .pSpecializationInfo = nullptr, }; if (gfxInfo.fragmentShader != nullptr) { PFragmentShader fragment = gfxInfo.fragmentShader.cast(); stageInfos[stageCount++] = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fragment->getModuleHandle(), .pName = fragment->getEntryPointName(), .pSpecializationInfo = nullptr, }; } // hash = CRC::Calculate(stageInfos, sizeof(stageInfos), CRC::CRC_32(), hash); VkPipelineViewportStateCreateInfo viewportInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .viewportCount = 1, .pViewports = nullptr, .scissorCount = 1, .pScissors = nullptr, }; // hash = CRC::Calculate(&viewportInfo, sizeof(VkPipelineViewportStateCreateInfo), CRC::CRC_32(), hash); VkPipelineRasterizationStateCreateInfo rasterizationState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .depthClampEnable = gfxInfo.rasterizationState.depthClampEnable, .rasterizerDiscardEnable = gfxInfo.rasterizationState.rasterizerDiscardEnable, .polygonMode = cast(gfxInfo.rasterizationState.polygonMode), .cullMode = gfxInfo.rasterizationState.cullMode, .frontFace = (VkFrontFace)gfxInfo.rasterizationState.frontFace, .depthBiasEnable = gfxInfo.rasterizationState.depthBiasEnable, .depthBiasConstantFactor = gfxInfo.rasterizationState.depthBiasConstantFactor, .depthBiasClamp = gfxInfo.rasterizationState.depthBiasClamp, .depthBiasSlopeFactor = gfxInfo.rasterizationState.depthBiasSlopeFactor, .lineWidth = 0, }; // hash = CRC::Calculate(&rasterizationState, sizeof(VkPipelineRasterizationStateCreateInfo), CRC::CRC_32(), hash); VkPipelineMultisampleStateCreateInfo multisampleState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .rasterizationSamples = (VkSampleCountFlagBits)gfxInfo.multisampleState.samples, .sampleShadingEnable = gfxInfo.multisampleState.sampleShadingEnable, .minSampleShading = gfxInfo.multisampleState.minSampleShading, .pSampleMask = nullptr, .alphaToCoverageEnable = gfxInfo.multisampleState.alphaCoverageEnable, .alphaToOneEnable = gfxInfo.multisampleState.alphaToOneEnable, }; // hash = CRC::Calculate(&multisampleState, sizeof(VkPipelineMultisampleStateCreateInfo), CRC::CRC_32(), hash); VkPipelineDepthStencilStateCreateInfo depthStencilState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .depthTestEnable = gfxInfo.depthStencilState.depthTestEnable, .depthWriteEnable = gfxInfo.depthStencilState.depthWriteEnable, .depthCompareOp = cast(gfxInfo.depthStencilState.depthCompareOp), .depthBoundsTestEnable = gfxInfo.depthStencilState.depthBoundsTestEnable, .stencilTestEnable = gfxInfo.depthStencilState.stencilTestEnable, .front = VkStencilOpState{ .failOp = VK_STENCIL_OP_ZERO, .passOp = (VkStencilOp)gfxInfo.depthStencilState.front, .depthFailOp = VK_STENCIL_OP_ZERO, .compareOp = VK_COMPARE_OP_ALWAYS, .compareMask = 0, .writeMask = 0, .reference = 0, }, .back = VkStencilOpState{ .failOp = VK_STENCIL_OP_ZERO, .passOp = (VkStencilOp)gfxInfo.depthStencilState.back, .depthFailOp = VK_STENCIL_OP_ZERO, .compareOp = VK_COMPARE_OP_ALWAYS, .compareMask = 0, .writeMask = 0, .reference = 0, }, .minDepthBounds = gfxInfo.depthStencilState.minDepthBounds, .maxDepthBounds = gfxInfo.depthStencilState.maxDepthBounds, }; // hash = CRC::Calculate(&depthStencilState, sizeof(VkPipelineDepthStencilStateCreateInfo), CRC::CRC_32(), hash); Array blendAttachments; for (uint32 i = 0; i < gfxInfo.colorBlend.attachmentCount; ++i) { const Gfx::ColorBlendState::BlendAttachment& attachment = gfxInfo.colorBlend.blendAttachments[i]; blendAttachments.add() = { .blendEnable = attachment.blendEnable, .srcColorBlendFactor = (VkBlendFactor)attachment.srcColorBlendFactor, .dstColorBlendFactor = (VkBlendFactor)attachment.dstColorBlendFactor, .colorBlendOp = (VkBlendOp)attachment.colorBlendOp, .srcAlphaBlendFactor = (VkBlendFactor)attachment.srcAlphaBlendFactor, .dstAlphaBlendFactor = (VkBlendFactor)attachment.dstAlphaBlendFactor, .alphaBlendOp = (VkBlendOp)attachment.alphaBlendOp, .colorWriteMask = attachment.colorWriteMask, }; } // hash = CRC::Calculate(blendAttachments.data(), blendAttachments.size() * sizeof(VkPipelineColorBlendAttachmentState), CRC::CRC_32(), // hash); VkPipelineColorBlendStateCreateInfo blendState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, .logicOpEnable = gfxInfo.colorBlend.logicOpEnable, .logicOp = (VkLogicOp)gfxInfo.colorBlend.logicOp, .attachmentCount = (uint32)blendAttachments.size(), .pAttachments = blendAttachments.data(), }; std::memcpy(blendState.blendConstants, gfxInfo.colorBlend.blendConstants.data(), sizeof(blendState.blendConstants)); uint32 numDynamicEnabled = 0; StaticArray dynamicEnabled; dynamicEnabled[numDynamicEnabled++] = VK_DYNAMIC_STATE_VIEWPORT; dynamicEnabled[numDynamicEnabled++] = VK_DYNAMIC_STATE_SCISSOR; // hash = CRC::Calculate(dynamicEnabled.data(), sizeof(dynamicEnabled), CRC::CRC_32(), hash); VkPipelineDynamicStateCreateInfo dynamicState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, .pNext = nullptr, .dynamicStateCount = (uint32)dynamicEnabled.size(), .pDynamicStates = dynamicEnabled.data(), }; VkPipeline pipelineHandle; VkGraphicsPipelineCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .pNext = 0, .flags = 0, .stageCount = stageCount, .pStages = stageInfos, .pVertexInputState = nullptr, .pInputAssemblyState = nullptr, .pViewportState = &viewportInfo, .pRasterizationState = &rasterizationState, .pMultisampleState = &multisampleState, .pDepthStencilState = &depthStencilState, .pColorBlendState = &blendState, .pDynamicState = &dynamicState, .layout = layout->getHandle(), .renderPass = gfxInfo.renderPass.cast()->getHandle(), .subpass = 0, }; auto beginTime = std::chrono::high_resolution_clock::now(); VK_CHECK(vkCreateGraphicsPipelines(graphics->getDevice(), cache, 1, &createInfo, nullptr, &pipelineHandle)); auto endTime = std::chrono::high_resolution_clock::now(); int64 delta = std::chrono::duration_cast(endTime - beginTime).count(); std::cout << "Gfx creation time: " << delta << std::endl; OGraphicsPipeline pipeline = new GraphicsPipeline(graphics, pipelineHandle, gfxInfo.pipelineLayout); PGraphicsPipeline result = pipeline; graphicsPipelines[hash] = std::move(pipeline); return result; } PComputePipeline PipelineCache::createPipeline(Gfx::ComputePipelineCreateInfo computeInfo) { PPipelineLayout layout = Gfx::PPipelineLayout(computeInfo.pipelineLayout).cast(); auto computeStage = computeInfo.computeShader.cast(); uint32 hash = layout->getHash(); VkComputePipelineCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, .pNext = 0, .flags = 0, .stage = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = computeStage->getModuleHandle(), .pName = computeStage->getEntryPointName(), }, .layout = layout->getHandle(), .basePipelineHandle = VK_NULL_HANDLE, .basePipelineIndex = 0, }; hash = CRC::Calculate(&createInfo, sizeof(createInfo), CRC::CRC_32(), hash); VkPipeline pipelineHandle; auto beginTime = std::chrono::high_resolution_clock::now(); VK_CHECK(vkCreateComputePipelines(graphics->getDevice(), cache, 1, &createInfo, nullptr, &pipelineHandle)); auto endTime = std::chrono::high_resolution_clock::now(); int64 delta = std::chrono::duration_cast(endTime - beginTime).count(); std::cout << "Compute creation time: " << delta << std::endl; OComputePipeline pipeline = new ComputePipeline(graphics, pipelineHandle, computeInfo.pipelineLayout); PComputePipeline result = pipeline; graphicsPipelines[hash] = std::move(pipeline); return result; } PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateInfo createInfo) { Array shaderStages; Array shaderGroups; { auto rayGen = createInfo.rayGenShader.cast(); shaderStages.add(VkPipelineShaderStageCreateInfo{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR, .module = rayGen->getModuleHandle(), .pName = rayGen->getEntryPointName(), }); shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .pNext = nullptr, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR, .generalShader = static_cast(shaderStages.size() - 1), .closestHitShader = VK_SHADER_UNUSED_KHR, .anyHitShader = VK_SHADER_UNUSED_KHR, .intersectionShader = VK_SHADER_UNUSED_KHR, }); } { for (auto gfxHit : createInfo.closestHitShaders) { auto hit = gfxHit.cast(); shaderStages.add(VkPipelineShaderStageCreateInfo{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, .module = hit->getModuleHandle(), .pName = hit->getEntryPointName(), }); shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .pNext = nullptr, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR, .generalShader = VK_SHADER_UNUSED_KHR, .closestHitShader = static_cast(shaderStages.size() - 1), .anyHitShader = VK_SHADER_UNUSED_KHR, .intersectionShader = VK_SHADER_UNUSED_KHR, }); } } { for (auto gfxHit : createInfo.anyHitShaders) { auto hit = gfxHit.cast(); shaderStages.add(VkPipelineShaderStageCreateInfo{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_ANY_HIT_BIT_KHR, .module = hit->getModuleHandle(), .pName = hit->getEntryPointName(), }); shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .pNext = nullptr, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR, .generalShader = VK_SHADER_UNUSED_KHR, .closestHitShader = VK_SHADER_UNUSED_KHR, .anyHitShader = static_cast(shaderStages.size() - 1), .intersectionShader = VK_SHADER_UNUSED_KHR, }); } } { for (auto gfxIntersect : createInfo.intersectionShaders) { auto intersect = gfxIntersect.cast(); shaderStages.add(VkPipelineShaderStageCreateInfo{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_INTERSECTION_BIT_KHR, .module = intersect->getModuleHandle(), .pName = intersect->getEntryPointName(), }); shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .pNext = nullptr, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR, .generalShader = VK_SHADER_UNUSED_KHR, .closestHitShader = VK_SHADER_UNUSED_KHR, .anyHitShader = VK_SHADER_UNUSED_KHR, .intersectionShader = static_cast(shaderStages.size() - 1), }); } } { for (auto gfxMiss : createInfo.missShaders) { auto miss = gfxMiss.cast(); shaderStages.add(VkPipelineShaderStageCreateInfo{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_MISS_BIT_KHR, .module = miss->getModuleHandle(), .pName = miss->getEntryPointName(), }); shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .pNext = nullptr, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR, .generalShader = static_cast(shaderStages.size() - 1), .closestHitShader = VK_SHADER_UNUSED_KHR, .anyHitShader = VK_SHADER_UNUSED_KHR, .intersectionShader = VK_SHADER_UNUSED_KHR, }); } } { for (auto gfxCallable : createInfo.callableShaders) { auto miss = gfxCallable.cast(); shaderStages.add(VkPipelineShaderStageCreateInfo{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = nullptr, .flags = 0, .stage = VK_SHADER_STAGE_CALLABLE_BIT_KHR, .module = miss->getModuleHandle(), .pName = miss->getEntryPointName(), }); shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .pNext = nullptr, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR, .generalShader = static_cast(shaderStages.size() - 1), .closestHitShader = VK_SHADER_UNUSED_KHR, .anyHitShader = VK_SHADER_UNUSED_KHR, .intersectionShader = VK_SHADER_UNUSED_KHR, }); } } uint32 hash = CRC::Calculate(shaderStages.data(), sizeof(VkPipelineShaderStageCreateInfo) * shaderStages.size(), CRC::CRC_32()); hash = CRC::Calculate(shaderGroups.data(), sizeof(VkRayTracingShaderGroupCreateInfoKHR) * shaderGroups.size(), CRC::CRC_32(), hash); VkRayTracingPipelineCreateInfoKHR pipelineInfo = { .sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR, .pNext = nullptr, .stageCount = static_cast(shaderStages.size()), .pStages = shaderStages.data(), .groupCount = static_cast(shaderGroups.size()), .pGroups = shaderGroups.data(), .maxPipelineRayRecursionDepth = 24, .layout = createInfo.pipelineLayout.cast()->getHandle(), }; VkPipeline pipelineHandle; VK_CHECK(vkCreateRayTracingPipelinesKHR(graphics->getDevice(), VK_NULL_HANDLE, cache, 1, &pipelineInfo, nullptr, &pipelineHandle)); ORayTracingPipeline pipeline = new RayTracingPipeline(graphics, pipelineHandle, createInfo.pipelineLayout); PRayTracingPipeline handle = pipeline; rayTracingPipelines[hash] = std::move(pipeline); return handle; }