Skip to content

Commit 663f2f2

Browse files
author
kevyuu
committed
In progress
1 parent e6c50ac commit 663f2f2

File tree

1 file changed

+72
-58
lines changed

1 file changed

+72
-58
lines changed

71_RayTracingPipeline/main.cpp

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -289,85 +289,99 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public Bui
289289

290290
// ray trace pipeline and descriptor set layout setup
291291
{
292-
const IGPUDescriptorSetLayout::SBinding bindings[] = {
293-
{
294-
.binding = 0,
295-
.type = asset::IDescriptor::E_TYPE::ET_ACCELERATION_STRUCTURE,
296-
.createFlags = IGPUDescriptorSetLayout::SBinding::E_CREATE_FLAGS::ECF_NONE,
297-
.stageFlags = asset::IShader::E_SHADER_STAGE::ESS_RAYGEN,
298-
.count = 1,
292+
const auto bindings = std::array<const ICPUDescriptorSetLayout::SBinding, 2>{
293+
ICPUDescriptorSetLayout::SBinding{
294+
.binding = 0,
295+
.type = asset::IDescriptor::E_TYPE::ET_ACCELERATION_STRUCTURE,
296+
.createFlags = IGPUDescriptorSetLayout::SBinding::E_CREATE_FLAGS::ECF_NONE,
297+
.stageFlags = asset::IShader::E_SHADER_STAGE::ESS_RAYGEN,
298+
.count = 1,
299299
},
300300
{
301-
.binding = 1,
302-
.type = asset::IDescriptor::E_TYPE::ET_STORAGE_IMAGE,
303-
.createFlags = IGPUDescriptorSetLayout::SBinding::E_CREATE_FLAGS::ECF_NONE,
304-
.stageFlags = asset::IShader::E_SHADER_STAGE::ESS_RAYGEN,
305-
.count = 1,
301+
.binding = 1,
302+
.type = asset::IDescriptor::E_TYPE::ET_STORAGE_IMAGE,
303+
.createFlags = IGPUDescriptorSetLayout::SBinding::E_CREATE_FLAGS::ECF_NONE,
304+
.stageFlags = asset::IShader::E_SHADER_STAGE::ESS_RAYGEN,
305+
.count = 1,
306306
}
307307
};
308-
const auto descriptorSetLayout = m_device->createDescriptorSetLayout(bindings);
309-
310-
const std::array<IGPUDescriptorSetLayout*, ICPUPipelineLayout::DESCRIPTOR_SET_COUNT> dsLayoutPtrs = { descriptorSetLayout.get() };
311-
m_rayTracingDsPool = m_device->createDescriptorPoolForDSLayouts(IDescriptorPool::ECF_UPDATE_AFTER_BIND_BIT, std::span(dsLayoutPtrs.begin(), dsLayoutPtrs.end()));
312-
m_rayTracingDs = m_rayTracingDsPool->createDescriptorSet(descriptorSetLayout);
308+
auto descriptorSetLayout = core::make_smart_refctd_ptr<ICPUDescriptorSetLayout>(bindings);
313309

314310
const SPushConstantRange pcRange = {
315311
.stageFlags = IShader::E_SHADER_STAGE::ESS_ALL_RAY_TRACING,
316312
.offset = 0u,
317313
.size = sizeof(SPushConstants),
318314
};
319-
const auto pipelineLayout = m_device->createPipelineLayout({ &pcRange, 1 }, smart_refctd_ptr(descriptorSetLayout), nullptr, nullptr, nullptr);
320-
321-
IGPURayTracingPipeline::SCreationParams params = {};
322-
params.layout = pipelineLayout.get();
323-
using RayTracingFlags = IGPURayTracingPipeline::SCreationParams::FLAGS;
324-
params.flags = core::bitflag(RayTracingFlags::NO_NULL_MISS_SHADERS) |
325-
RayTracingFlags::NO_NULL_INTERSECTION_SHADERS |
326-
RayTracingFlags::NO_NULL_ANY_HIT_SHADERS;
315+
const auto pipelineLayout = core::make_smart_refctd_ptr<ICPUPipelineLayout>(std::span<const asset::SPushConstantRange>({ pcRange }), std::move(descriptorSetLayout), nullptr, nullptr, nullptr);
327316

328-
auto& shaderGroups = params.shaderGroups;
317+
const auto pipeline = ICPURayTracingPipeline::create(pipelineLayout.get());
318+
pipeline->getCachedCreationParams() = {
319+
.maxRecursionDepth = 1,
320+
.dynamicStackSize = true,
321+
};
329322

330-
shaderGroups.raygen = { .shader = raygenShader.get(), .entryPoint = "main" };
323+
pipeline->getSpecInfos(ESS_RAYGEN)[0] = {
324+
.shader = raygenShader,
325+
.entryPoint = "main",
326+
};
331327

332-
IGPUPipelineBase::SShaderSpecInfo missGroups[EMT_COUNT];
333-
missGroups[EMT_PRIMARY] = { .shader = missShader.get(), .entryPoint = "main" };
334-
missGroups[EMT_OCCLUSION] = { .shader = missShadowShader.get(), .entryPoint = "main" };
335-
shaderGroups.misses = missGroups;
328+
pipeline->getSpecInfoVector(ESS_MISS)->resize(EMT_COUNT);
329+
const auto missGroups = pipeline->getSpecInfos(ESS_MISS);
330+
missGroups[EMT_PRIMARY] = { .shader = missShader, .entryPoint = "main" };
331+
missGroups[EMT_OCCLUSION] = { .shader = missShadowShader, .entryPoint = "main" };
336332

337333
auto getHitGroupIndex = [](E_GEOM_TYPE geomType, E_RAY_TYPE rayType)
338334
{
339335
return geomType * ERT_COUNT + rayType;
340336
};
341-
IGPURayTracingPipeline::SHitGroup hitGroups[E_RAY_TYPE::ERT_COUNT * E_GEOM_TYPE::EGT_COUNT];
342-
hitGroups[getHitGroupIndex(EGT_TRIANGLES, ERT_PRIMARY)] = {
343-
.closestHit = { .shader = closestHitShader.get(), .entryPoint = "main" },
344-
.anyHit = { .shader = anyHitShaderColorPayload.get(), .entryPoint = "main" },
345-
};
346-
hitGroups[getHitGroupIndex(EGT_TRIANGLES, ERT_OCCLUSION)] = {
347-
.anyHit = { .shader = anyHitShaderShadowPayload.get(), .entryPoint = "main" },
348-
};
349-
hitGroups[getHitGroupIndex(EGT_PROCEDURAL, ERT_PRIMARY)] = {
350-
.closestHit = { .shader = proceduralClosestHitShader.get(), .entryPoint = "main" },
351-
.anyHit = { .shader = anyHitShaderColorPayload.get(), .entryPoint = "main" },
352-
.intersection = { .shader = intersectionHitShader.get(), .entryPoint = "main" },
353-
};
354-
hitGroups[getHitGroupIndex(EGT_PROCEDURAL, ERT_OCCLUSION)] = {
355-
.anyHit = { .shader = anyHitShaderShadowPayload.get(), .entryPoint = "main" },
356-
.intersection = { .shader = intersectionHitShader.get(), .entryPoint = "main" },
357-
};
358-
shaderGroups.hits = hitGroups;
359337

360-
IGPUPipelineBase::SShaderSpecInfo callableGroups[ELT_COUNT];
361-
callableGroups[ELT_DIRECTIONAL] = { .shader = directionalLightCallShader.get(), .entryPoint = "main" };
362-
callableGroups[ELT_POINT] = { .shader = pointLightCallShader.get(), .entryPoint = "main" };
363-
callableGroups[ELT_SPOT] = { .shader = spotLightCallShader.get(), .entryPoint = "main" };
364-
shaderGroups.callables = callableGroups;
338+
const auto hitGroupCount = ERT_COUNT * EGT_COUNT;
339+
pipeline->getSpecInfoVector(ESS_CLOSEST_HIT)->resize(hitGroupCount);
340+
pipeline->getSpecInfoVector(ESS_ANY_HIT)->resize(hitGroupCount);
341+
pipeline->getSpecInfoVector(ESS_INTERSECTION)->resize(hitGroupCount);
342+
343+
const auto closestHitSpecs = pipeline->getSpecInfos(ESS_CLOSEST_HIT);
344+
const auto anyHitSpecs = pipeline->getSpecInfos(ESS_ANY_HIT);
345+
const auto intersectionSpecs = pipeline->getSpecInfos(ESS_INTERSECTION);
346+
347+
closestHitSpecs[getHitGroupIndex(EGT_TRIANGLES, ERT_PRIMARY)] = { .shader = closestHitShader, .entryPoint = "main" };
348+
anyHitSpecs[getHitGroupIndex(EGT_TRIANGLES, ERT_PRIMARY)] = {.shader = anyHitShaderColorPayload, .entryPoint = "main"};
349+
350+
anyHitSpecs[getHitGroupIndex(EGT_TRIANGLES, ERT_OCCLUSION)] = { .shader = anyHitShaderShadowPayload, .entryPoint = "main" };
351+
closestHitSpecs[getHitGroupIndex(EGT_PROCEDURAL, ERT_PRIMARY)] = { .shader = proceduralClosestHitShader, .entryPoint = "main" };
352+
anyHitSpecs[getHitGroupIndex(EGT_PROCEDURAL, ERT_PRIMARY)] = { .shader = anyHitShaderColorPayload, .entryPoint = "main" };
353+
intersectionSpecs[getHitGroupIndex(EGT_PROCEDURAL, ERT_PRIMARY)] = { .shader = intersectionHitShader, .entryPoint = "main" };
365354

366-
params.cached.maxRecursionDepth = 1;
367-
params.cached.dynamicStackSize = true;
355+
anyHitSpecs[getHitGroupIndex(EGT_PROCEDURAL, ERT_OCCLUSION)] = {.shader = anyHitShaderShadowPayload, .entryPoint = "main" };
356+
intersectionSpecs[getHitGroupIndex(EGT_PROCEDURAL, ERT_OCCLUSION)] = { .shader = intersectionHitShader, .entryPoint = "main" };
368357

369-
if (!m_device->createRayTracingPipelines(nullptr, { &params, 1 }, &m_rayTracingPipeline))
370-
return logFail("Failed to create ray tracing pipeline");
358+
pipeline->getSpecInfoVector(ESS_CALLABLE)->resize(ELT_COUNT);
359+
const auto callableGroups = pipeline->getSpecInfos(ESS_CALLABLE);
360+
callableGroups[ELT_DIRECTIONAL] = { .shader = directionalLightCallShader, .entryPoint = "main" };
361+
callableGroups[ELT_POINT] = { .shader = pointLightCallShader, .entryPoint = "main" };
362+
callableGroups[ELT_SPOT] = { .shader = spotLightCallShader, .entryPoint = "main" };
363+
364+
smart_refctd_ptr<CAssetConverter> converter = CAssetConverter::create({ .device = m_device.get(), .optimizer = {} });
365+
CAssetConverter::SInputs inputs = {};
366+
inputs.logger = m_logger.get();
367+
368+
const std::array cpuPipelines = { pipeline.get() };
369+
std::get<CAssetConverter::SInputs::asset_span_t<ICPURayTracingPipeline>>(inputs.assets) = cpuPipelines;
370+
371+
CAssetConverter::SConvertParams params = {};
372+
params.utilities = m_utils.get();
373+
374+
auto reservation = converter->reserve(inputs);
375+
auto future = reservation.convert(params);
376+
if (future.copy() != IQueue::RESULT::SUCCESS)
377+
{
378+
m_logger->log("Failed to await submission feature!", ILogger::ELL_ERROR);
379+
return false;
380+
}
381+
382+
// assign gpu objects to output
383+
auto&& pipelines = reservation.getGPUObjects<ICPURayTracingPipeline>();
384+
m_rayTracingPipeline = pipelines[0].value;
371385

372386
calculateRayTracingStackSize(m_rayTracingPipeline);
373387

0 commit comments

Comments
 (0)