-
Notifications
You must be signed in to change notification settings - Fork 7
/
rasterizer.cpp
356 lines (293 loc) · 14.3 KB
/
rasterizer.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
//--------------------------------------------------------------------------------------------------
// This example is loading a glTF scene and renders it with a very simple material
//
#include <iostream>
#include <vulkan/vulkan.hpp>
#include "nvh/fileoperations.hpp"
#include "nvvk/images_vk.hpp"
#include "nvvk/pipeline_vk.hpp"
#include "nvvk/renderpasses_vk.hpp"
#include "nvvk/shaders_vk.hpp"
#include "rasterizer.hpp"
extern std::vector<std::string> defaultSearchPaths;
void Rasterizer::setup(const vk::Device& device, const vk::PhysicalDevice& physicalDevice, uint32_t queueIndex, nvvkpp::ResourceAllocator* allocator)
{
m_device = device;
m_queueIndex = queueIndex;
m_debug.setup(device);
m_alloc = allocator;
}
//--------------------------------------------------------------------------------------------------
// Overridden function called on shutdown
//
void Rasterizer::destroy()
{
m_device.waitIdle();
m_alloc->destroy(m_depthImage);
for(auto& t : m_rasterizerOutput)
m_alloc->destroy(t);
m_device.destroy(m_renderPass);
m_device.destroy(m_framebuffer);
m_device.destroy(m_drawPipeline);
m_device.destroy(m_pipelineLayout);
m_device.destroy(m_depthImageView);
m_framebuffer = vk::Framebuffer();
m_depthImageView = vk::ImageView();
}
//--------------------------------------------------------------------------------
// Called at each frame, as fast as possible
//
void Rasterizer::run(const vk::CommandBuffer& cmdBuf, const vk::DescriptorSet& dsetScene, int frame /*= 0*/)
{
auto dbgLabel = m_debug.scopeLabel(cmdBuf, "Start rendering");
vk::ClearValue clearValues[3];
clearValues[0].setColor(makeClearColor(m_clearColor)); // Color buffer
clearValues[1].setColor(std::array<float, 4>({0.0f, 0.0f, -1.0f, 0.f})); // Data buffer
clearValues[2].setDepthStencil({1.0f, 0});
// Pre-recorded scene
{
auto dbgLabel = m_debug.scopeLabel(cmdBuf, "Recorded Scene");
vk::RenderPassBeginInfo renderPassBeginInfo{m_renderPass, m_framebuffer, {{}, m_outputSize}, 3, clearValues};
// Recorded
//cmdBuf.beginRenderPass(renderPassBeginInfo, vk::SubpassContents::eSecondaryCommandBuffers);
//cmdBuf.executeCommands(m_recordedCmdBuffer);
// Immediate
cmdBuf.beginRenderPass(renderPassBeginInfo, vk::SubpassContents::eInline);
setViewport(cmdBuf);
render(cmdBuf, dsetScene);
cmdBuf.endRenderPass();
}
}
//--------------------------------------------------------------------------------------------------
// When the pipeline is set for using dynamic, this becomes useful
//
void Rasterizer::setViewport(const vk::CommandBuffer& cmdBuf)
{
cmdBuf.setViewport(0, {vk::Viewport(0.0f, 0.0f, static_cast<float>(m_outputSize.width),
static_cast<float>(m_outputSize.height), 0.0f, 1.0f)});
cmdBuf.setScissor(0, {{{0, 0}, {m_outputSize.width, m_outputSize.height}}});
}
//--------------------------------------------------------------------------------------------------
// Building the command buffer, is in fact, recording all the calls needed to draw the frame in a
// command buffer.This need to be call only if the number of objects in the scene is changing or
// if the viewport is changing
//
void Rasterizer::recordCommandBuffer(const vk::CommandPool& cmdPool, const vk::DescriptorSet& dsetScene)
{
m_device.freeCommandBuffers(cmdPool, {m_recordedCmdBuffer});
m_recordedCmdBuffer = m_device.allocateCommandBuffers({cmdPool, vk::CommandBufferLevel::eSecondary, 1})[0];
vk::CommandBufferInheritanceInfo inheritance_info{m_renderPass};
vk::CommandBufferBeginInfo begin_info{vkCB::eSimultaneousUse | vkCB::eRenderPassContinue, &inheritance_info};
m_recordedCmdBuffer.begin(begin_info);
{
setViewport(m_recordedCmdBuffer);
render(m_recordedCmdBuffer, dsetScene);
}
m_recordedCmdBuffer.end();
}
//--------------------------------------------------------------------------------------------------
// The pipeline is how things are rendered, which shaders, type of primitives, depth test and more
//
void Rasterizer::createPipeline(const vk::DescriptorSetLayout& sceneDescSetLayout)
{
vk::PushConstantRange pushConstantRanges = {vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0,
sizeof(PushC)};
// Creating the pipeline layout
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
pipelineLayoutCreateInfo.setSetLayoutCount(1);
pipelineLayoutCreateInfo.setPSetLayouts(&sceneDescSetLayout);
pipelineLayoutCreateInfo.setPushConstantRangeCount(1);
pipelineLayoutCreateInfo.setPPushConstantRanges(&pushConstantRanges);
m_pipelineLayout = m_device.createPipelineLayout(pipelineLayoutCreateInfo);
// Pipeline
std::vector<std::string> paths = defaultSearchPaths;
nvvkpp::GraphicsPipelineGeneratorCombined gpb(m_device, m_pipelineLayout, m_renderPass);
gpb.depthStencilState.depthTestEnable = true;
gpb.addBlendAttachmentState(nvvk::GraphicsPipelineState::makePipelineColorBlendAttachmentState());
gpb.addShader(nvh::loadFile("spv/rasterizer.vert.spv", true, paths), vk::ShaderStageFlagBits::eVertex);
gpb.addShader(nvh::loadFile("spv/rasterizer.frag.spv", true, paths), vk::ShaderStageFlagBits::eFragment);
gpb.addBindingDescriptions({{0, sizeof(glm::vec3)}, {1, sizeof(glm::vec3)}, {2, sizeof(glm::vec2)}});
gpb.addAttributeDescriptions({
{0, 0, vk::Format::eR32G32B32Sfloat, 0}, // Position
{1, 1, vk::Format::eR32G32B32Sfloat, 0}, // Normal
{2, 2, vk::Format::eR32G32Sfloat, 0}, // Texcoord0
});
gpb.rasterizationState.setCullMode(vk::CullModeFlagBits::eNone);
m_drawPipeline = gpb.createPipeline();
m_debug.setObjectName(m_drawPipeline, "ShadingPipeline");
m_debug.setObjectName(gpb.getShaderModule(0), "VertexShader");
m_debug.setObjectName(gpb.getShaderModule(1), "FragmentShader");
}
//--------------------------------------------------------------------------------------------------
// Rendering all glTF nodes
//
void Rasterizer::render(const vk::CommandBuffer& cmdBuff, const vk::DescriptorSet& dsetScene)
{
if(!m_drawPipeline)
{
return;
}
m_debug.setObjectName(cmdBuff, "Recored");
auto dgbLabel = m_debug.scopeLabel(cmdBuff, "Recording Scene");
// Pipeline to use for rendering the current scene
cmdBuff.bindPipeline(vk::PipelineBindPoint::eGraphics, m_drawPipeline);
// Offsets for the descriptor set and vertex buffer
std::vector<vk::DeviceSize> offsets = {0, 0, 0};
// Keeping track of the last material to avoid binding them again
uint32_t lastMaterial = -1;
std::vector<vk::Buffer> vertexBuffers = {m_vertexBuffer->buffer, m_normalBuffer->buffer, m_uvBuffer->buffer};
cmdBuff.bindVertexBuffers(0, static_cast<uint32_t>(vertexBuffers.size()), vertexBuffers.data(), offsets.data());
cmdBuff.bindIndexBuffer(m_indexBuffer->buffer, 0, vk::IndexType::eUint32);
std::vector<vk::DescriptorSet> descriptorSets = {dsetScene};
// The pipeline uses four descriptor set, one for the scene information, one for the matrix of the instance, one for the textures and for the environment
cmdBuff.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, m_pipelineLayout, 0, descriptorSets, {});
uint32_t idxNode = 0;
for(auto& node : m_gltfScene->m_nodes)
{
auto dgbLabel = m_debug.scopeLabel(cmdBuff, std::string("Draw Mesh: " + std::to_string(node.primMesh)));
auto& primitive = m_gltfScene->m_primMeshes[node.primMesh];
m_pushC.instID = idxNode++;
m_pushC.matID = primitive.materialIndex;
cmdBuff.pushConstants<PushC>(m_pipelineLayout, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0, m_pushC);
cmdBuff.drawIndexed(primitive.indexCount, 1, primitive.firstIndex, primitive.vertexOffset, 0);
}
}
void Rasterizer::setToonSteps(int nbStep)
{
m_pushC.nbSteps = nbStep;
}
void Rasterizer::setToonLightDir(glm::vec3 lightDir)
{
m_pushC.lightDir = lightDir;
}
// Return all outputs
const std::vector<nvvk::Texture>& Rasterizer::outputImages() const
{
return m_rasterizerOutput;
}
//--------------------------------------------------------------------------------------------------
// The display will render the recorded command buffer, then in a sub-pass, render the UI
//
void Rasterizer::createRenderPass()
{
m_renderPass = nvvkpp::createRenderPass(m_device, {vk::Format::eR32G32B32A32Sfloat, vk::Format::eR32G32B32A32Sfloat}, // color attachment
m_depthFormat, // depth attachment
1, // Nb sub-passes
true, // clearColor
true, // clearDepth
vk::ImageLayout::eUndefined, // initialLayout
vk::ImageLayout::eGeneral); // finalLayout
m_debug.setObjectName(m_renderPass, "General Render Pass");
}
//--------------------------------------------------------------------------------------------------
// Making the two output images: color, data(normal, depth, ID)
//
void Rasterizer::createOutputImages(vk::Extent2D size)
{
for(auto& t : m_rasterizerOutput)
m_alloc->destroy(t);
m_rasterizerOutput.clear();
m_outputSize = size;
auto usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eStorage
| vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eColorAttachment;
vk::DeviceSize imgSize = size.width * size.height * 4 * sizeof(float);
vk::Format format = vk::Format::eR32G32B32A32Sfloat;
// Create two output image, the color and the data
for(int i = 0; i < 2; i++)
{
nvvkpp::ScopeCommandBuffer cmdBuf(m_device, m_queueIndex);
vk::SamplerCreateInfo samplerCreateInfo; // default values
vk::ImageCreateInfo imageCreateInfo = nvvkpp::makeImage2DCreateInfo(size, format, usage);
nvvk::Image image = m_alloc->createImage(cmdBuf, imgSize, nullptr, imageCreateInfo, vk::ImageLayout::eGeneral);
vk::ImageViewCreateInfo ivInfo = nvvk::makeImageViewCreateInfo(image.image, imageCreateInfo);
nvvk::Texture txt = m_alloc->createTexture(image, ivInfo, samplerCreateInfo);
txt.descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
m_rasterizerOutput.push_back(txt);
}
{
nvvkpp::ScopeCommandBuffer cmdBuf(m_device, m_queueIndex);
createDepthBuffer(cmdBuf, size);
}
createFrameBuffer();
}
//--------------------------------------------------------------------------------------------------
// Create the framebuffers in which the images will be rendered
// - Swapchain need to be created before calling this
//
void Rasterizer::createFrameBuffer()
{
// Recreate the frame buffers
m_device.destroy(m_framebuffer);
// Array of attachment (color, depth)
std::array<vk::ImageView, 3> attachments;
// Create frame buffers for every swap chain image
vk::FramebufferCreateInfo framebufferCreateInfo;
framebufferCreateInfo.renderPass = m_renderPass;
framebufferCreateInfo.attachmentCount = 3;
framebufferCreateInfo.width = m_outputSize.width;
framebufferCreateInfo.height = m_outputSize.height;
framebufferCreateInfo.layers = 1;
framebufferCreateInfo.pAttachments = attachments.data();
// Create frame buffers for every swap chain image
attachments[0] = m_rasterizerOutput[0].descriptor.imageView; // Color
attachments[1] = m_rasterizerOutput[1].descriptor.imageView; // Data
attachments[2] = m_depthImageView; // Depth
m_framebuffer = m_device.createFramebuffer(framebufferCreateInfo);
std::string name = std::string("Rasterizer_Framebuffer");
#if DEBUG
m_device.setDebugUtilsObjectNameEXT(
{vk::ObjectType::eFramebuffer, reinterpret_cast<const uint64_t&>(m_framebuffer), name.c_str()});
#endif
}
//--------------------------------------------------------------------------------------------------
// Creating an image to be used as depth buffer
//
void Rasterizer::createDepthBuffer(vk::CommandBuffer commandBuffer, vk::Extent2D imageSize)
{
m_alloc->destroy(m_depthImage);
m_device.destroy(m_depthImageView);
vk::ImageCreateInfo imageInfo =
nvvkpp::makeImage2DCreateInfo(imageSize, m_depthFormat, vk::ImageUsageFlagBits::eDepthStencilAttachment);
m_depthImage = m_alloc->createImage(imageInfo);
m_debug.setObjectName(m_depthImage.image, "m_depthImage");
vk::ImageViewCreateInfo viewInfo =
nvvkpp::makeImage2DViewCreateInfo(m_depthImage.image, m_depthFormat, vk::ImageAspectFlagBits::eDepth);
m_depthImageView = m_device.createImageView(viewInfo);
m_debug.setObjectName(m_depthImageView, "m_depthImageView");
// Set layout to VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
nvvkpp::cmdBarrierImageLayout(commandBuffer, // Command buffer
m_depthImage.image, // Image
vk::ImageLayout::eUndefined, // Old layout
vk::ImageLayout::eDepthStencilAttachmentOptimal, // New layout
vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil);
}
void Rasterizer::setObjectPointers(nvh::GltfScene* gltfScene,
nvvk::Buffer* vertexBuffer,
nvvk::Buffer* normalBuffer,
nvvk::Buffer* uvBuffer,
nvvk::Buffer* indexBuffer)
{
m_gltfScene = gltfScene;
m_vertexBuffer = vertexBuffer;
m_normalBuffer = normalBuffer;
m_uvBuffer = uvBuffer;
m_indexBuffer = indexBuffer;
}