-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVulkanRayTracingPipelineBuilder.cpp
146 lines (112 loc) · 5.06 KB
/
VulkanRayTracingPipelineBuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/******************************************************************************
This file is part of the Newcastle Vulkan Tutorial Series
Author:Rich Davison
Contact:richgdavison@gmail.com
License: MIT (see LICENSE file at the top of the source tree)
*//////////////////////////////////////////////////////////////////////////////
#include "VulkanRayTracingPipelineBuilder.h"
#include "VulkanMesh.h"
#include "VulkanUtils.h"
using namespace NCL;
using namespace Rendering;
using namespace Vulkan;
VulkanRayTracingPipelineBuilder::VulkanRayTracingPipelineBuilder(vk::Device device) : PipelineBuilderBase(device){
}
VulkanRayTracingPipelineBuilder::~VulkanRayTracingPipelineBuilder() {
}
VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithRecursionDepth(uint32_t count) {
pipelineCreate.maxPipelineRayRecursionDepth = count;
return *this;
}
VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithRayGenGroup(uint32_t shaderIndex) {
vk::RayTracingShaderGroupCreateInfoKHR groupCreateInfo;
groupCreateInfo.type = vk::RayTracingShaderGroupTypeKHR::eGeneral;
groupCreateInfo.generalShader = shaderIndex;
genGroups.push_back(groupCreateInfo);
return *this;
}
VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithMissGroup(uint32_t shaderIndex) {
vk::RayTracingShaderGroupCreateInfoKHR groupCreateInfo;
groupCreateInfo.type = vk::RayTracingShaderGroupTypeKHR::eGeneral;
groupCreateInfo.generalShader = shaderIndex;
missGroups.push_back(groupCreateInfo);
return *this;
}
//VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithShaderGroup(const vk::RayTracingShaderGroupCreateInfoKHR& groupCreateInfo) {
// shaderGroups.push_back(groupCreateInfo);
// return *this;
//}
//
//VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithGeneralGroup(uint32_t index) {
// vk::RayTracingShaderGroupCreateInfoKHR groupCreateInfo;
//
// groupCreateInfo.type = vk::RayTracingShaderGroupTypeKHR::eGeneral;
// groupCreateInfo.intersectionShader = VK_SHADER_UNUSED_KHR;
// groupCreateInfo.generalShader = index;
// groupCreateInfo.closestHitShader = VK_SHADER_UNUSED_KHR;
// groupCreateInfo.anyHitShader = VK_SHADER_UNUSED_KHR;
//
// shaderGroups.push_back(groupCreateInfo);
//
// return *this;
//}
VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithTriangleHitGroup(uint32_t closestHit, uint32_t anyHit) {
vk::RayTracingShaderGroupCreateInfoKHR groupCreateInfo;
groupCreateInfo.type = vk::RayTracingShaderGroupTypeKHR::eTrianglesHitGroup;
groupCreateInfo.generalShader = VK_SHADER_UNUSED_KHR;
groupCreateInfo.intersectionShader = VK_SHADER_UNUSED_KHR;
groupCreateInfo.closestHitShader = closestHit;
groupCreateInfo.anyHitShader = anyHit;
hitGroups.push_back(groupCreateInfo);
return *this;
}
VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithProceduralHitGroup(uint32_t intersection, uint32_t closestHit, uint32_t anyHit){
vk::RayTracingShaderGroupCreateInfoKHR groupCreateInfo;
groupCreateInfo.type = vk::RayTracingShaderGroupTypeKHR::eProceduralHitGroup;
groupCreateInfo.generalShader = VK_SHADER_UNUSED_KHR;
groupCreateInfo.intersectionShader = intersection;
groupCreateInfo.closestHitShader = closestHit;
groupCreateInfo.anyHitShader = anyHit;
hitGroups.push_back(groupCreateInfo);
return *this;
}
VulkanRayTracingPipelineBuilder& VulkanRayTracingPipelineBuilder::WithShader(VulkanRTShader& shader, vk::ShaderStageFlagBits stage, const string& entry) {
ShaderEntry entryInfo;
entryInfo.entryPoint = entry;
entryInfo.shader = &shader;
entryInfo.stage = stage;
entries.push_back(entryInfo);
shader.FillDescriptorSetLayouts(reflectionLayouts);
shader.FillPushConstants(allPushConstants);
return *this;
}
VulkanPipeline VulkanRayTracingPipelineBuilder::Build(const std::string& debugName, vk::PipelineCache cache) {
for (const auto& i : entries) {
vk::PipelineShaderStageCreateInfo stageInfo;
stageInfo.pName = i.entryPoint.c_str();
stageInfo.stage = i.stage;
stageInfo.module = *i.shader->GetModule();
shaderStages.push_back(stageInfo);
}
FinaliseDescriptorLayouts();
allGroups.clear();
allGroups.insert(allGroups.end(), genGroups.begin() , genGroups.end());
allGroups.insert(allGroups.end(), missGroups.begin(), missGroups.end());
allGroups.insert(allGroups.end(), hitGroups.begin() , hitGroups.end());
pipelineCreate.groupCount = allGroups.size();
pipelineCreate.pGroups = allGroups.data();
pipelineCreate.stageCount = shaderStages.size();
pipelineCreate.pStages = shaderStages.data();
vk::PipelineLayoutCreateInfo pipeLayoutCreate = vk::PipelineLayoutCreateInfo()
.setSetLayouts(allLayouts)
.setPushConstantRanges(allPushConstants);
VulkanPipeline output;
output.layout = sourceDevice.createPipelineLayoutUnique(pipeLayoutCreate);
pipelineCreate.layout = *output.layout;
pipelineCreate.setPDynamicState(&dynamicCreate);
output.pipeline = sourceDevice.createRayTracingPipelineKHRUnique({}, cache, pipelineCreate).value;
if (!debugName.empty()) {
SetDebugName(sourceDevice, vk::ObjectType::ePipeline, GetVulkanHandle(*output.pipeline), debugName);
}
return output;
}