-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoitRender.cpp
424 lines (367 loc) · 16.5 KB
/
oitRender.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright (c) 2020-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) 2020-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
// This file contains implementations of the main OIT drawing functions from
// oit.h, excluding GUI and resolving from m_colorImage to the swapchain.
#include "oit.h"
void Sample::render(VkCommandBuffer& cmdBuffer)
{
// Clear auxiliary buffers before we even start a render pass - this
// reduces the number of render passes we need to use by 1.
switch(m_state.algorithm)
{
case OIT_SIMPLE:
clearTransparentSimple(cmdBuffer);
break;
case OIT_LINKEDLIST:
clearTransparentLinkedList(cmdBuffer);
break;
case OIT_LOOP:
clearTransparentLoop(cmdBuffer);
break;
case OIT_LOOP64:
clearTransparentLoop64(cmdBuffer);
break;
case OIT_INTERLOCK:
case OIT_SPINLOCK:
clearTransparentLock(cmdBuffer, (m_state.algorithm == OIT_INTERLOCK));
break;
case OIT_WEIGHTED:
// Its render pass clears OIT_WEIGHTED for us
break;
default:
assert(!"Algorithm case not called in switch statement!");
}
// We'll make the first m_state.percentTransparent percent of our spheres transparent;
// the rest, at the end, will be opaque. Since we only have one mesh, we can do this
// by drawing the last range of triangles using an opaque shader, and then drawing
// the first using our OIT methods.
const int numObjects = m_sceneTriangleIndices / m_objectTriangleIndices;
int numTransparent = (numObjects * m_state.percentTransparent) / 100;
if(numTransparent > numObjects)
{
numTransparent = numObjects;
}
const int numOpaque = numObjects - numTransparent;
// Start the main render pass
{
const nvvk::ProfilerVK::Section scopedTimer(m_profilerVK, "Main", cmdBuffer);
// Transition the color image to work as a color attachment, in case it
// was set to VK_IMAGE_LAYOUT_GENERAL.
m_colorImage.transitionTo(cmdBuffer, // Command buffer
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // New layout
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
// Set up the render pass
VkRenderPassBeginInfo renderPassInfo = {VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO};
renderPassInfo.renderPass = m_renderPassColorDepthClear;
renderPassInfo.framebuffer = m_mainColorDepthFramebuffer;
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent.width = m_colorImage.c_width;
renderPassInfo.renderArea.extent.height = m_colorImage.c_height;
std::array<VkClearValue, 2> clearValues = {};
clearValues[0].color = {0.2f, 0.2f, 0.2f, 0.2f}; // Background color, in linear space
clearValues[1].depthStencil = {1.0f, 0}; // Clear depth
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(cmdBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
// Draw all of the opaque objects
{
// Bind the descriptor set (constant buffers, images)
// Pipeline layout depends only on descriptor set layout.
VkDescriptorSet descriptorSet = m_descriptorInfo.getSet(m_swapChain.getActiveImageIndex());
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_descriptorInfo.getPipeLayout(), 0, 1,
&descriptorSet, 0, nullptr);
drawSceneObjects(cmdBuffer, numTransparent, numOpaque);
}
// Now, draw the transparent objects.
switch(m_state.algorithm)
{
case OIT_SIMPLE:
drawTransparentSimple(cmdBuffer, numTransparent);
break;
case OIT_LINKEDLIST:
drawTransparentLinkedList(cmdBuffer, numTransparent);
break;
case OIT_LOOP:
drawTransparentLoop(cmdBuffer, numTransparent);
break;
case OIT_LOOP64:
drawTransparentLoop64(cmdBuffer, numTransparent);
break;
case OIT_INTERLOCK:
case OIT_SPINLOCK:
drawTransparentLock(cmdBuffer, numTransparent, (m_state.algorithm == OIT_INTERLOCK));
break;
case OIT_WEIGHTED:
drawTransparentWeighted(cmdBuffer, numTransparent);
break;
default:
assert(!"Algorithm case not called in switch statement!");
}
vkCmdEndRenderPass(cmdBuffer);
}
}
void Sample::drawSceneObjects(VkCommandBuffer& cmdBuffer, int firstObject, int numObjects)
{
// Bind the vertex and index buffers
VkBuffer vertexBuffers[] = {m_vertexBuffer.buffer};
VkDeviceSize offsets[] = {0};
vkCmdBindVertexBuffers(cmdBuffer, 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(cmdBuffer, m_indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
// Bind the graphics pipeline state object (shaders, configuration)
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineOpaque);
// Draw!
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, firstObject * m_objectTriangleIndices, 0, 0);
}
void Sample::clearTransparentSimple(VkCommandBuffer& cmdBuffer)
{
// Clears all values in m_oitAux to 0.
const nvvk::ProfilerVK::Section scopedTimer(m_profilerVK, "ClearSimple", cmdBuffer);
// Clear the base mip and layer of m_oitAuxImage
VkClearColorValue auxClearColor;
auxClearColor.uint32[0] = 0; // Since m_oitAux is R32UINT
VkImageSubresourceRange auxClearRanges;
auxClearRanges.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
auxClearRanges.baseArrayLayer = 0;
auxClearRanges.baseMipLevel = 0;
auxClearRanges.layerCount = m_oitAuxImage.c_layers;
auxClearRanges.levelCount = 1;
vkCmdClearColorImage(cmdBuffer, // Command buffer
m_oitAuxImage.image.image, // The VkImage
VK_IMAGE_LAYOUT_GENERAL, // The current image layout
&auxClearColor, // The color to clear it with
1, // The number of VkImageSubresourceRanges below
&auxClearRanges // Range of mipmap levels, array layers, and aspects to be cleared
);
// Make sure this completes before using m_oitAuxImage again.
cmdTransferBarrierSimple(cmdBuffer);
}
void Sample::drawTransparentSimple(VkCommandBuffer& cmdBuffer, int numObjects)
{
// COLOR
// Stores the first OIT_LAYERS fragments per pixel or sample in the A-buffer,
// and tail-blends the rest.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineSimpleColor);
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Make sure the color pass completes before the composite pass
cmdFragmentBarrierSimple(cmdBuffer);
// COMPOSITE
// Sorts the stored fragments per pixel or sample and composites them onto the color image.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineSimpleComposite);
// Draw a full-screen triangle:
vkCmdDraw(cmdBuffer, 3, 1, 0, 0);
}
}
void Sample::clearTransparentLinkedList(VkCommandBuffer& cmdBuffer)
{
// Sets the atomic counter (really a 1x1 image) to 0, and set imgAux to 0.
const nvvk::ProfilerVK::Section scopedTimer(m_profilerVK, "ClearLinkedList", cmdBuffer);
VkClearColorValue auxClearColor;
auxClearColor.uint32[0] = 0; // Since m_oitAux is R32UINT
VkImageSubresourceRange auxClearRanges;
auxClearRanges.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
auxClearRanges.baseArrayLayer = 0;
auxClearRanges.baseMipLevel = 0;
auxClearRanges.layerCount = m_oitAuxImage.c_layers;
auxClearRanges.levelCount = 1;
vkCmdClearColorImage(cmdBuffer, m_oitAuxImage.image.image, m_oitAuxImage.currentLayout, &auxClearColor, 1, &auxClearRanges);
auxClearRanges.layerCount = 1;
vkCmdClearColorImage(cmdBuffer, m_oitCounterImage.image.image, m_oitCounterImage.currentLayout, &auxClearColor, 1, &auxClearRanges);
// Make sure this completes before using these images again.
cmdTransferBarrierSimple(cmdBuffer);
}
void Sample::drawTransparentLinkedList(VkCommandBuffer& cmdBuffer, int numObjects)
{
// COLOR
// Constructs the linked lists.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLinkedListColor);
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Make sure the color pass completes before the composite pass
cmdFragmentBarrierSimple(cmdBuffer);
// COMPOSITE
// Iterates through the linked lists and sorts and tail-blends fragments.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLinkedListComposite);
// Draw a full-screen triangle
vkCmdDraw(cmdBuffer, 3, 1, 0, 0);
}
}
void Sample::clearTransparentLoop(VkCommandBuffer& cmdBuffer)
{
// Set all depth values in m_oitABuffer to 0xFFFFFFFF.
const nvvk::ProfilerVK::Section scopedTimer(m_profilerVK, "ClearLoop", cmdBuffer);
// This makes sure to only overwrite the depth portion of the A-buffer, which
// should improve bandwidth. See the memory layout described in oitScene.frag.glsl
// for more information.
const size_t clearSize = m_sceneUbo.viewport.z * sizeof(uint32_t) * m_state.oitLayers;
for(size_t i = 0; i < (m_state.sampleShading ? m_state.msaa : 1); i++)
{
vkCmdFillBuffer(cmdBuffer, // Command buffer
m_oitABuffer.buffer.buffer, // Buffer
i * clearSize * 2, // Offset
clearSize, // Size
0xFFFFFFFFu); // Data
}
// Make sure this completes before using m_oitABuffer again.
cmdTransferBarrierSimple(cmdBuffer);
}
void Sample::drawTransparentLoop(VkCommandBuffer& cmdBuffer, int numObjects)
{
// DEPTH
// Sorts the frontmost OIT_LAYERS depths per sample.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLoopDepth);
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Make sure the depth pass completes before the composite pass
cmdFragmentBarrierSimple(cmdBuffer);
// COLOR
// Uses the sorted depth information to sort colors into layers
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLoopColor);
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Make sure the color pass completes before the composite pass
cmdFragmentBarrierSimple(cmdBuffer);
// COMPOSITE
// Blends the sorted colors together.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLoopComposite);
// Draw a full-screen triangle
vkCmdDraw(cmdBuffer, 3, 1, 0, 0);
}
}
void Sample::clearTransparentLoop64(VkCommandBuffer& cmdBuffer)
{
// Sets all values in m_oitABuffer to 0xFFFFFFFF (depth), 0xFFFFFFFF (color)
const nvvk::ProfilerVK::Section scopedTimer(m_profilerVK, "ClearLoop64", cmdBuffer);
vkCmdFillBuffer(cmdBuffer, m_oitABuffer.buffer.buffer, 0, VK_WHOLE_SIZE, 0xFFFFFFFFu);
// Make sure this completes before using m_oitABuffer again.
cmdTransferBarrierSimple(cmdBuffer);
}
void Sample::drawTransparentLoop64(VkCommandBuffer& cmdBuffer, int numObjects)
{
// (DEPTH +) COLOR
// Sorts the frontmost OIT_LAYERS (depth, color) pairs per sample.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLoop64Color);
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Make sure the depth + color pass completes before the composite pass
cmdFragmentBarrierSimple(cmdBuffer);
// COMPOSITE
// Blends the sorted colors together
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLoop64Composite);
// Draw a full-screen triangle
vkCmdDraw(cmdBuffer, 3, 1, 0, 0);
}
}
void Sample::clearTransparentLock(VkCommandBuffer& cmdBuffer, bool useInterlock)
{
// Sets the values in IMG_AUX to 0 and IMG_AUXDEPTH to 0xFFFFFFFF.
// If using spinlock, sets the values in IMG_AUXSPIN to 0 as well.
const nvvk::ProfilerVK::Section scopedTimer(m_profilerVK, "ClearLock", cmdBuffer);
VkClearColorValue auxClearColor0;
auxClearColor0.uint32[0] = 0; // Since m_oitAux is R32UINT
VkClearColorValue auxClearColorF;
auxClearColorF.uint32[0] = 0xFFFFFFFFu;
VkImageSubresourceRange auxClearRanges;
auxClearRanges.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
auxClearRanges.baseArrayLayer = 0;
auxClearRanges.baseMipLevel = 0;
auxClearRanges.layerCount = m_oitAuxDepthImage.c_layers;
auxClearRanges.levelCount = 1;
vkCmdClearColorImage(cmdBuffer, m_oitAuxDepthImage.image.image, m_oitAuxDepthImage.currentLayout, &auxClearColorF, 1, &auxClearRanges);
vkCmdClearColorImage(cmdBuffer, m_oitAuxImage.image.image, m_oitAuxImage.currentLayout, &auxClearColor0, 1, &auxClearRanges);
if(!useInterlock)
{
// Also clear m_oitAuxSpinImage
vkCmdClearColorImage(cmdBuffer, m_oitAuxSpinImage.image.image, m_oitAuxSpinImage.currentLayout, &auxClearColor0, 1, &auxClearRanges);
}
cmdTransferBarrierSimple(cmdBuffer);
}
void Sample::drawTransparentLock(VkCommandBuffer& cmdBuffer, int numObjects, bool useInterlock)
{
// COLOR
// Sorts the frontmost OIT_LAYERS (depth, color) pairs per pixel.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, (useInterlock ? m_pipelineInterlockColor : m_pipelineSpinlockColor));
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Make sure the color pass completes before the composite pass
cmdFragmentBarrierSimple(cmdBuffer);
// COMPOSITE
// Blends the sorted colors together
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
(useInterlock ? m_pipelineInterlockComposite : m_pipelineSpinlockComposite));
// Draw a full-screen triangle
vkCmdDraw(cmdBuffer, 3, 1, 0, 0);
}
}
void Sample::drawTransparentWeighted(VkCommandBuffer& cmdBuffer, int numObjects)
{
// Swap out the render pass for WBOIT's render pass
vkCmdEndRenderPass(cmdBuffer);
// Transition the color image to work as an attachment
m_colorImage.transitionTo(cmdBuffer, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
VkRenderPassBeginInfo renderPassInfo = {VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO};
renderPassInfo.renderPass = m_renderPassWeighted;
renderPassInfo.framebuffer = m_weightedFramebuffer;
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent.width = m_oitWeightedColorImage.c_width;
renderPassInfo.renderArea.extent.height = m_oitWeightedRevealImage.c_height;
std::array<VkClearValue, 2> clearValues;
clearValues[0].color.float32[0] = 0.0f;
clearValues[0].color.float32[1] = 0.0f;
clearValues[0].color.float32[2] = 0.0f;
clearValues[0].color.float32[3] = 0.0f;
clearValues[1].color.float32[0] = 1.0f; // Initially, all pixels show through all the way (reveal = 100%)
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(cmdBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
// COLOR PASS
// Computes the weighted sum and reveal factor.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineWeightedColor);
// Draw all objects
vkCmdDrawIndexed(cmdBuffer, numObjects * m_objectTriangleIndices, 1, 0, 0, 0);
}
// Move to the next subpass
vkCmdNextSubpass(cmdBuffer, VK_SUBPASS_CONTENTS_INLINE);
// COMPOSITE PASS
// Averages out the summed colors (in some sense) to get the final transparent color.
{
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineWeightedComposite);
// Draw a full-screen triangle
vkCmdDraw(cmdBuffer, 3, 1, 0, 0);
}
}