From 80e01b60a04ba6fd938dfbbdb59e2de059e8c067 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Tue, 28 Jan 2025 09:41:53 +0100 Subject: [PATCH] again more code --- external/vcpkg | 2 +- src/gpu/GPURenderer.cpp | 120 ++++++++++++++++++++++++++++++++++------ 2 files changed, 105 insertions(+), 17 deletions(-) diff --git a/external/vcpkg b/external/vcpkg index 01be99f..ab42fb3 160000 --- a/external/vcpkg +++ b/external/vcpkg @@ -1 +1 @@ -Subproject commit 01be99fec01f777c4113ceea192a45115c69cdb7 +Subproject commit ab42fb3032acd29dd12cea7897f85025526a60e6 diff --git a/src/gpu/GPURenderer.cpp b/src/gpu/GPURenderer.cpp index 0b5b39b..047c346 100644 --- a/src/gpu/GPURenderer.cpp +++ b/src/gpu/GPURenderer.cpp @@ -130,10 +130,8 @@ using namespace slang; void GPURenderer::createShaders() { - /* Slang::ComPtr globalSession; - SlangGlobalSessionDesc desc = {}; - createGlobalSession(&desc, globalSession.writeRef()); + createGlobalSession(globalSession.writeRef()); SessionDesc sessionDesc; TargetDesc targetDesc; targetDesc.format = SLANG_SPIRV; @@ -143,29 +141,119 @@ void GPURenderer::createShaders() const char* searchPaths[] = {"res/shaders/"}; sessionDesc.searchPaths = searchPaths; sessionDesc.searchPathCount = 1; - /* ... fill in `sessionDesc` ... Slang::ComPtr session; globalSession->createSession(sessionDesc, session.writeRef()); Slang::ComPtr diagnostics; - IModule* module = session->loadModule("MyShaders", diagnostics.writeRef()); + IModule* raygenModule = session->loadModule("RayGen", diagnostics.writeRef()); if (diagnostics) { std::cout << (const char*)diagnostics->getBufferPointer() << std::endl; } - Slang::ComPtr computeEntryPoint; - module->findEntryPointByName("myComputeMain", computeEntryPoint.writeRef()); - IComponentType* components[] = {module, computeEntryPoint}; + Slang::ComPtr rayGenEntry; + raygenModule->findEntryPointByName("rayGen", rayGenEntry.writeRef()); + + IModule* closestHitModule = session->loadModule("ClosestHit", diagnostics.writeRef()); + if (diagnostics) + { + std::cout << (const char*)diagnostics->getBufferPointer() << std::endl; + } + Slang::ComPtr closestHitEntry; + closestHitModule->findEntryPointByName("closestHit", closestHitEntry.writeRef()); + + IComponentType* components[] = {raygenModule, rayGenEntry, closestHitModule, closestHitEntry}; Slang::ComPtr program; - session->createCompositeComponentType(components, 2, program.writeRef()); + session->createCompositeComponentType(components, 4, program.writeRef()); + Slang::ComPtr linkedProgram; - Slang::ComPtr diagnosticBlob; - program->link(linkedProgram.writeRef(), diagnosticBlob.writeRef()); - int entryPointIndex = 0; // only one entry point - int targetIndex = 0; // only one target - Slang::ComPtr kernelBlob; - linkedProgram->getEntryPointCode(entryPointIndex, targetIndex, kernelBlob.writeRef(), diagnostics.writeRef()); - */ + program->link(linkedProgram.writeRef(), diagnostics.writeRef()); + + Slang::ComPtr rayGenCode; + linkedProgram->getEntryPointCode(0, 0, rayGenCode.writeRef(), diagnostics.writeRef()); + + Slang::ComPtr closestHitCode; + linkedProgram->getEntryPointCode(1, 0, closestHitCode.writeRef(), diagnostics.writeRef()); + + rayGen = + ShaderModule(device, vk::ShaderModuleCreateInfo({}, rayGenCode->getBufferSize(), (const uint32_t*)rayGenCode->getBufferPointer())); + + closestHit = ShaderModule( + device, vk::ShaderModuleCreateInfo({}, closestHitCode->getBufferSize(), (const uint32_t*)closestHitCode->getBufferPointer())); + + std::vector shaderStages; + std::vector shaderGroups; + + { + shaderStages.push_back(VkPipelineShaderStageCreateInfo{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR, + .module = *rayGen, + .pName = "rayGen", + .pSpecializationInfo = nullptr, + }); + shaderGroups.push_back(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, + .pShaderGroupCaptureReplayHandle = nullptr, + }); + } + { + shaderStages.push_back(VkPipelineShaderStageCreateInfo{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, + .module = *closestHit, + .pName = "closestHit", + .pSpecializationInfo = nullptr, + }); + uint32_t hitIndex = static_cast(shaderStages.size() - 1); + uint32_t anyHitIndex = VK_SHADER_UNUSED_KHR; + uint32_t intersectionIndex = VK_SHADER_UNUSED_KHR; + // if (hitgroup.anyHitShader != nullptr) + //{ + // auto anyHit = hitgroup.anyHitShader.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 = anyHit->getModuleHandle(), + // .pName = anyHit->getEntryPointName(), + // .pSpecializationInfo = nullptr, + // }); + // } + // if (hitgroup.intersectionShader != nullptr) + //{ + // auto intersect = hitgroup.intersectionShader.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(), + // .pSpecializationInfo = nullptr, + // }); + // } + shaderGroups.push_back(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 = hitIndex, + .anyHitShader = anyHitIndex, + .intersectionShader = intersectionIndex, + .pShaderGroupCaptureReplayHandle = nullptr, + }); + } } void GPURenderer::render(Camera cam, RenderParameter param)