FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vk_mini_samples/samples/ray_query/ray_query.cpp at main · nvpro-samples/vk_mini_samples · GitHub
nvpro-samples
/
vk_mini_samples
Public
Notifications
You must be signed in to change notification settings
Fork
23
Star
369
Code
Issues
1
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vk_mini_samples
/
samples
/
ray_query
/
ray_query.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
784 lines (646 loc) · 30.2 KB
Breadcrumbs
vk_mini_samples
/
samples
/
ray_query
/
ray_query.cpp
Copy path
File metadata and controls
784 lines (646 loc) · 30.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
* Copyright (c) 2023-2026, 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) 2023-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
//
////////////////////////////////////////////////////////////////////////
/*
This shows the use of Ray Query, or casting rays in a compute shader
*/
//
////////////////////////////////////////////////////////////////////////
#
define
USE_SLANG
true
#
define
SHADER_LANGUAGE_STR
(
USE_SLANG
?
"
Slang
"
:
"
GLSL
"
)
#
define
VMA_IMPLEMENTATION
#
define
VMA_LEAK_LOG_FORMAT
(format, ...) \
{ \
printf
((format), __VA_ARGS__); \
printf
(
"
\n
"
); \
}
#
include
<
array
>
#
include
<
glm/glm.hpp
>
#
include
<
vulkan/vulkan_core.h
>
#
include
"
shaders/dh_bindings.h
"
#
include
"
shaders/shaderio.h
"
//
Shared between host and device
#
include
"
_autogen/ray_query.comp.glsl.h
"
#
include
"
_autogen/ray_query.slang.h
"
#
include
"
_autogen/tonemapper.slang.h
"
#
include
<
nvapp/application.hpp
>
#
include
<
nvapp/elem_camera.hpp
>
#
include
<
nvapp/elem_default_menu.hpp
>
#
include
<
nvapp/elem_default_title.hpp
>
#
include
<
nvgui/camera.hpp
>
#
include
<
nvgui/property_editor.hpp
>
#
include
<
nvgui/tonemapper.hpp
>
#
include
<
nvshaders_host/tonemapper.hpp
>
#
include
<
nvutils/camera_manipulator.hpp
>
#
include
<
nvutils/file_operations.hpp
>
#
include
<
nvutils/logger.hpp
>
#
include
<
nvutils/parameter_parser.hpp
>
#
include
<
nvutils/primitives.hpp
>
#
include
<
nvvk/acceleration_structures.hpp
>
#
include
<
nvvk/check_error.hpp
>
#
include
<
nvvk/context.hpp
>
#
include
<
nvvk/debug_util.hpp
>
#
include
<
nvvk/descriptors.hpp
>
#
include
<
nvapp/imgui_texture.hpp
>
#
include
<
nvvk/render_target.hpp
>
#
include
<
nvvk/resource_allocator.hpp
>
#
include
<
nvvk/sampler_pool.hpp
>
#
include
<
nvvk/sbt_generator.hpp
>
#
include
<
nvvk/specialization.hpp
>
#
include
<
nvvk/staging.hpp
>
#
include
"
common/utils.hpp
"
std::shared_ptr<nvutils::CameraManipulator> g_cameraManip{};
//
/ </summary> Ray trace multiple primitives using Ray Query
class
RayQuery
:
public
nvapp
::IAppElement
{
enum
{
eImgTonemapped,
eImgRendered
};
public:
RayQuery
() =
default
;
~RayQuery
()
override
=
default
;
void
onAttach
(nvapp::Application* app)
override
{
m_app = app;
m_device = m_app->
getDevice
();
//
Create the Vulkan allocator (VMA)
m_allocator.
init
({
.
flags
=
VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT
,
.
physicalDevice
= app->
getPhysicalDevice
(),
.
device
= app->
getDevice
(),
.
instance
= app->
getInstance
(),
.
vulkanApiVersion
=
VK_API_VERSION_1_4
,
});
//
Allocator
//
The texture sampler to use (needed to sample the rendered image in the tonemapper)
m_samplerPool.
init
(m_device);
NVVK_CHECK
(m_samplerPool.
acquireSampler
(m_linearSampler));
NVVK_DBG_NAME
(m_linearSampler);
//
Offscreen render target for the ray tracing and tonemapping
NVVK_CHECK
(m_renderTarget.
init
(
{.
alloc
= &m_allocator, .
colorFormats
= {
VK_FORMAT_R8G8B8A8_UNORM
,
VK_FORMAT_R32G32B32A32_SFLOAT
}, .
debugName
=
"
RayQuery
"
}));
//
Tonemapper
{
auto
code = std::span<
const
uint32_t
>(tonemapper_slang);
m_tonemapper.
init
(&m_allocator, code);
}
//
Requesting ray tracing properties
VkPhysicalDeviceProperties2 prop2{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
};
m_rtProperties.
pNext
= &m_asProperties;
prop2.
pNext
= &m_rtProperties;
vkGetPhysicalDeviceProperties2
(m_app->
getPhysicalDevice
(), &prop2);
//
Create resources
createScene
();
createVkBuffers
();
createBottomLevelAS
();
createTopLevelAS
();
createCompPipelines
();
}
void
onDetach
()
override
{
vkDeviceWaitIdle
(m_device);
destroyResources
();
}
void
onResize
(VkCommandBuffer cmd,
const
VkExtent2D& size)
override
{
NVVK_CHECK
(m_renderTarget.
update
(cmd, size));
m_viewportImage.
update
(m_renderTarget.
getUiImageView
(eImgTonemapped));
resetFrame
();
//
Reset frame to restart the rendering
}
void
onUIRender
()
override
{
{
//
Setting menu
ImGui::Begin
(
"
Settings
"
);
nvgui::CameraWidget
(g_cameraManip);
namespace
PE
=
nvgui::PropertyEditor;
bool
changed{
false
};
if
(
ImGui::CollapsingHeader
(
"
Settings
"
, ImGuiTreeNodeFlags_DefaultOpen))
{
PE::begin
();
if
(
PE::treeNode
(
"
Light
"
))
{
changed |=
PE::DragFloat3
(
"
Position
"
, &m_light.
position
.
x
);
changed |=
PE::SliderFloat
(
"
Intensity
"
, &m_light.
intensity
,
0
.
0F
,
1000
.
0F
,
"
%.3f
"
, ImGuiSliderFlags_Logarithmic);
changed |=
PE::SliderFloat
(
"
Radius
"
, &m_light.
radius
,
0
.
0F
,
1
.
0F
);
PE::treePop
();
}
if
(
PE::treeNode
(
"
Ray Tracer
"
))
{
changed |=
PE::SliderInt
(
"
Depth
"
, &m_pushConst.
maxDepth
,
0
,
20
);
changed |=
PE::SliderInt
(
"
Samples
"
, &m_pushConst.
maxSamples
,
1
,
10
);
PE::treePop
();
}
PE::end
();
}
if
(
ImGui::CollapsingHeader
(
"
Tonemapper
"
))
{
nvgui::tonemapperWidget
(m_tonemapperData);
}
ImGui::End
();
if
(changed)
resetFrame
();
}
{
//
Rendering Viewport
ImGui::PushStyleVar
(ImGuiStyleVar_WindowPadding,
ImVec2
(
0
.
0F
,
0
.
0F
));
ImGui::Begin
(
"
Viewport
"
);
//
Display the G-Buffer tonemapped image
ImGui::Image
(m_viewportImage,
ImGui::GetContentRegionAvail
());
ImGui::End
();
ImGui::PopStyleVar
();
}
}
void
onRender
(VkCommandBuffer cmd)
override
{
NVVK_DBG_SCOPE
(cmd);
if
(!
updateFrame
())
{
return
;
}
//
Update Camera uniform buffer
shaderio::CameraInfo cameraInfo{.
projInv
=
glm::inverse
(g_cameraManip->
getPerspectiveMatrix
()),
.
viewInv
=
glm::inverse
(g_cameraManip->
getViewMatrix
())};
vkCmdUpdateBuffer
(cmd, m_bCameraInfo.
buffer
,
0
,
sizeof
(shaderio::CameraInfo), &cameraInfo);
m_pushConst.
frame
= m_frame;
m_pushConst.
light
= m_light;
//
Make sure buffer is ready to be used
nvvk::cmdMemoryBarrier
(cmd,
VK_PIPELINE_STAGE_2_TRANSFER_BIT
,
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT
);
//
Ray trace
const
VkExtent2D& size = m_app->
getViewportSize
();
vkCmdBindPipeline
(cmd,
VK_PIPELINE_BIND_POINT_COMPUTE
, m_pipeline);
pushDescriptorSet
(cmd);
vkCmdPushConstants
(cmd, m_pipelineLayout,
VK_SHADER_STAGE_ALL
,
0
,
sizeof
(shaderio::PushConstant), &m_pushConst);
vkCmdDispatch
(cmd, (size.
width
+ (
WORKGROUP_SIZE
-
1
)) /
WORKGROUP_SIZE
,
(size.
height
+ (
WORKGROUP_SIZE
-
1
)) /
WORKGROUP_SIZE
,
1
);
//
Making sure the rendered image is ready to be used by tonemapper
nvvk::cmdMemoryBarrier
(cmd,
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT
,
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT
);
//
Tonemap
m_tonemapper.
runCompute
(cmd, m_renderTarget.
getSize
(), m_tonemapperData,
m_renderTarget.
getColorSampleDescriptorImageInfo
(eImgRendered, m_linearSampler),
m_renderTarget.
getColorSampleDescriptorImageInfo
(eImgTonemapped));
}
private:
void
createScene
()
{
m_materials.
push_back
({{
0
.
985f
,
0
.
862f
,
0
.
405f
},
0
.
5f
,
0
.
0f
});
m_materials.
push_back
({{
0
.
9622f
,
0
.
928f
,
0
.
9728f
},
0
.
0f
,
0
.
09f
,
1
.
0f
});
m_materials.
push_back
({{.
7F
, .
7F
, .
7F
},
0
.
3f
,
0
.
0f
});
m_meshes.
emplace_back
(
nvutils::createCube
(
1
,
1
,
1
));
m_meshes.
emplace_back
(
nvutils::createSphereUv
(
0
.
5f
,
100
,
100
));
m_meshes.
emplace_back
(
nvutils::createPlane
(
10
,
100
,
100
));
//
Instance Cube
{
auto
& node = m_nodes.
emplace_back
();
node.
mesh
=
0
;
node.
material
=
0
;
node.
translation
= {
0
.
0f
,
0
.
5f
,
0
.
0F
};
}
//
Instance Sphere
{
auto
& node = m_nodes.
emplace_back
();
node.
mesh
=
1
;
node.
material
=
1
;
node.
translation
= {
1
.
0f
,
1
.
5f
,
1
.
0F
};
}
//
Adding a plane & material
{
auto
& node = m_nodes.
emplace_back
();
node.
mesh
=
2
;
node.
material
=
2
;
node.
translation
= {
0
.
0f
,
0
.
0f
,
0
.
0f
};
}
//
Adding a light
m_light.
intensity
=
100
.
0f
;
m_light.
position
= {
2
.
0f
,
7
.
0f
,
2
.
0f
};
m_light.
radius
=
0
.
2f
;
//
Setting camera to see the scene
g_cameraManip->
setClipPlanes
({
0
.
1F
,
100
.
0F
});
g_cameraManip->
setLookat
({-
2
.
0F
,
2
.
5F
,
3
.
0f
}, {
0
.
4F
,
0
.
3F
,
0
.
2F
}, {
0
.
0F
,
1
.
0F
,
0
.
0F
});
//
Default parameters for overall material
m_pushConst.
maxDepth
=
5
;
m_pushConst.
frame
=
0
;
m_pushConst.
fireflyClampThreshold
=
10
;
m_pushConst.
maxSamples
=
2
;
m_pushConst.
light
= m_light;
}
//
Create all Vulkan buffer data
void
createVkBuffers
()
{
//
NVTX3_FUNC_RANGE();
VkCommandBuffer cmd = m_app->
createTempCmdBuffer
();
nvvk::StagingUploader uploader;
uploader.
init
(&m_allocator,
true
);
m_bMeshes.
resize
(m_meshes.
size
());
VkBufferUsageFlags rtUsageFlag =
VK_BUFFER_USAGE_2_STORAGE_BUFFER_BIT
|
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
|
VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR
;
//
Create a buffer of Vertex and Index per mesh
std::vector<shaderio::PrimMeshInfo> primInfo;
for
(
size_t
i =
0
; i < m_meshes.
size
(); i++)
{
auto
& m = m_bMeshes[i];
NVVK_CHECK
(m_allocator.
createBuffer
(m.
vertices
,
std::span
(m_meshes[i].
vertices
).
size_bytes
(), rtUsageFlag));
NVVK_CHECK
(m_allocator.
createBuffer
(m.
indices
,
std::span
(m_meshes[i].
triangles
).
size_bytes
(), rtUsageFlag));
NVVK_CHECK
(uploader.
appendBuffer
(m.
vertices
,
0
,
std::span
(m_meshes[i].
vertices
)));
NVVK_CHECK
(uploader.
appendBuffer
(m.
indices
,
0
,
std::span
(m_meshes[i].
triangles
)));
NVVK_DBG_NAME
(m.
vertices
.
buffer
);
NVVK_DBG_NAME
(m.
indices
.
buffer
);
//
To find the buffers of the mesh (buffer reference)
shaderio::PrimMeshInfo info{
.
vertexAddress
= m.
vertices
.
address
,
.
indexAddress
= m.
indices
.
address
,
};
primInfo.
emplace_back
(info);
}
//
Creating the buffer of all primitive/mesh information
NVVK_CHECK
(m_allocator.
createBuffer
(m_bPrimInfo,
std::span
(primInfo).
size_bytes
(), rtUsageFlag));
NVVK_CHECK
(uploader.
appendBuffer
(m_bPrimInfo,
0
,
std::span
(primInfo)));
NVVK_DBG_NAME
(m_bPrimInfo.
buffer
);
//
Create the buffer of the current camera transformation, changing at each frame
NVVK_CHECK
(m_allocator.
createBuffer
(m_bCameraInfo,
sizeof
(shaderio::CameraInfo),
VK_BUFFER_USAGE_2_UNIFORM_BUFFER_BIT
,
VMA_MEMORY_USAGE_AUTO_PREFER_HOST
));
NVVK_DBG_NAME
(m_bCameraInfo.
buffer
);
//
Primitive instance information
std::vector<shaderio::InstanceInfo> instInfo;
for
(
auto
& node : m_nodes)
{
shaderio::InstanceInfo info{
.
transform
= node.
localMatrix
(),
.
materialID
= node.
material
,
};
instInfo.
emplace_back
(info);
}
NVVK_CHECK
(m_allocator.
createBuffer
(m_bInstInfoBuffer,
std::span
(instInfo).
size_bytes
(),
VK_BUFFER_USAGE_2_STORAGE_BUFFER_BIT
|
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
));
NVVK_CHECK
(uploader.
appendBuffer
(m_bInstInfoBuffer,
0
,
std::span
(instInfo)));
NVVK_DBG_NAME
(m_bInstInfoBuffer.
buffer
);
NVVK_CHECK
(m_allocator.
createBuffer
(m_bMaterials,
std::span
(m_materials).
size_bytes
(),
VK_BUFFER_USAGE_2_STORAGE_BUFFER_BIT
|
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
));
NVVK_CHECK
(uploader.
appendBuffer
(m_bMaterials,
0
,
std::span
(m_materials)));
NVVK_DBG_NAME
(m_bMaterials.
buffer
);
//
Buffer references of all scene elements
shaderio::SceneInfo sceneDesc{
.
materialAddress
= m_bMaterials.
address
,
.
instInfoAddress
= m_bInstInfoBuffer.
address
,
.
primInfoAddress
= m_bPrimInfo.
address
,
.
light
= m_light,
};
NVVK_CHECK
(m_allocator.
createBuffer
(m_bSceneDesc,
sizeof
(shaderio::SceneInfo),
VK_BUFFER_USAGE_2_UNIFORM_BUFFER_BIT
|
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
));
NVVK_CHECK
(uploader.
appendBuffer
(m_bSceneDesc,
0
,
std::span
(&sceneDesc,
1
)));
NVVK_DBG_NAME
(m_bSceneDesc.
buffer
);
uploader.
cmdUploadAppended
(cmd);
m_app->
submitAndWaitTempCmdBuffer
(cmd);
uploader.
deinit
();
}
//
--------------------------------------------------------------------------------------------------
//
Converting a PrimitiveMesh as input for BLAS
//
nvvk::AccelerationStructureGeometryInfo
primitiveToGeometry
(
const
nvutils::PrimitiveMesh& prim,
VkDeviceAddress vertexAddress,
VkDeviceAddress indexAddress)
{
nvvk::AccelerationStructureGeometryInfo result;
uint32_t
maxPrimitiveCount =
static_cast
<
uint32_t
>(prim.
triangles
.
size
());
//
Describe buffer as array of VertexObj.
VkAccelerationStructureGeometryTrianglesDataKHR triangles{
.
sType
=
VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR
,
.
vertexFormat
=
VK_FORMAT_R32G32B32_SFLOAT
,
//
vec3 vertex position data
.
vertexData
= {.
deviceAddress
= vertexAddress},
.
vertexStride
=
sizeof
(nvutils::PrimitiveVertex),
.
maxVertex
=
static_cast
<
uint32_t
>(prim.
vertices
.
size
()) -
1
,
.
indexType
=
VK_INDEX_TYPE_UINT32
,
.
indexData
= {.
deviceAddress
= indexAddress},
};
//
Identify the above data as containing opaque triangles.
result.
geometry
= VkAccelerationStructureGeometryKHR{
.
sType
=
VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR
,
.
geometryType
=
VK_GEOMETRY_TYPE_TRIANGLES_KHR
,
.
geometry
= {.
triangles
= triangles},
.
flags
=
VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR
,
//
| VK_GEOMETRY_OPAQUE_BIT_KHR,
};
result.
rangeInfo
= VkAccelerationStructureBuildRangeInfoKHR{.
primitiveCount
= maxPrimitiveCount};
return
result;
}
//
--------------------------------------------------------------------------------------------------
//
Create all bottom level acceleration structures (BLAS)
//
void
createBottomLevelAS
()
{
//
NVTX3_FUNC_RANGE();
std::vector<nvvk::AccelerationStructureBuildData> blasData;
blasData.
resize
(m_meshes.
size
());
//
Build Information for each BLAS
m_bottomAs.
resize
(m_meshes.
size
());
//
The actual BLAS
//
Convert all primitives to acceleration structures geometry
VkDeviceSize maxScratchSize{
0
};
for
(
uint32_t
p_idx =
0
; p_idx < m_meshes.
size
(); p_idx++)
{
auto
vertexAddress = m_bMeshes[p_idx].
vertices
.
address
;
auto
indexAddress = m_bMeshes[p_idx].
indices
.
address
;
blasData[p_idx].
asType
=
VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR
;
blasData[p_idx].
addGeometry
(
primitiveToGeometry
(m_meshes[p_idx], vertexAddress, indexAddress));
VkAccelerationStructureBuildSizesInfoKHR sizeInfo =
blasData[p_idx].
finalizeGeometry
(m_device,
VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR
);
maxScratchSize =
std::max
(maxScratchSize, sizeInfo.
buildScratchSize
);
}
//
Scratch buffer for all BLAS
nvvk::Buffer scratchBuffer;
NVVK_CHECK
(m_allocator.
createBuffer
(scratchBuffer, maxScratchSize,
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
|
VK_BUFFER_USAGE_2_STORAGE_BUFFER_BIT
,
VMA_MEMORY_USAGE_AUTO
, {}, m_asProperties.
minAccelerationStructureScratchOffsetAlignment
));
NVVK_DBG_NAME
(scratchBuffer.
buffer
);
//
Create and build all BLAS
VkCommandBuffer cmd = m_app->
createTempCmdBuffer
();
for
(
size_t
i =
0
; i < m_bottomAs.
size
(); i++)
{
VkAccelerationStructureCreateInfoKHR createInfo = blasData[i].
makeCreateInfo
();
NVVK_CHECK
(m_allocator.
createAcceleration
(m_bottomAs[i], createInfo));
NVVK_DBG_NAME
(m_bottomAs[i].
accel
);
//
cmdBuildAccelerationStructure inserts a barrier for scratch reuse between builds
blasData[i].
cmdBuildAccelerationStructure
(cmd, m_bottomAs[i].
accel
, scratchBuffer.
address
);
}
m_app->
submitAndWaitTempCmdBuffer
(cmd);
m_allocator.
destroyBuffer
(scratchBuffer);
}
//
--------------------------------------------------------------------------------------------------
//
Create the top level acceleration structures, referencing all BLAS
//
void
createTopLevelAS
()
{
//
NVTX3_FUNC_RANGE();
nvvk::AccelerationStructureBuildData tlasData;
tlasData.
asType
=
VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR
;
std::vector<VkAccelerationStructureInstanceKHR> instances;
instances.
reserve
(m_nodes.
size
());
for
(
auto
& node : m_nodes)
{
VkAccelerationStructureInstanceKHR rayInst{.
transform
=
nvvk::toTransformMatrixKHR
(node.
localMatrix
()),
//
Position of the instance
.
instanceCustomIndex
=
static_cast
<
uint32_t
>(node.
mesh
),
//
gl_InstanceCustomIndexEX
.
mask
=
0xFF
,
.
flags
=
VK_GEOMETRY_INSTANCE_TRIANGLE_CULL_DISABLE_BIT_NV
,
.
accelerationStructureReference
= m_bottomAs[node.
mesh
].
address
};
instances.
emplace_back
(rayInst);
}
VkCommandBuffer cmd = m_app->
createTempCmdBuffer
();
nvvk::StagingUploader uploader;
uploader.
init
(&m_allocator);
//
Create a buffer holding the actual instance data (matrices++) for use by the AS builder
nvvk::Buffer instancesBuffer;
NVVK_CHECK
(m_allocator.
createBuffer
(instancesBuffer,
std::span
(instances).
size_bytes
(),
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
|
VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR
));
NVVK_CHECK
(uploader.
appendBuffer
(instancesBuffer,
0
,
std::span
(instances)));
NVVK_DBG_NAME
(instancesBuffer.
buffer
);
uploader.
cmdUploadAppended
(cmd);
//
AS build reads instance data (SHADER_READ) and writes the TLAS.
nvvk::accelerationStructureBarrier
(cmd,
VK_ACCESS_TRANSFER_WRITE_BIT
,
VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR
|
VK_ACCESS_2_SHADER_READ_BIT
);
auto
instGeo = tlasData.
makeInstanceGeometry
(instances.
size
(), instancesBuffer.
address
);
tlasData.
addGeometry
(instGeo);
//
Create the TLAS
auto
sizeInfo = tlasData.
finalizeGeometry
(m_device,
VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR
);
nvvk::Buffer scratchBuffer;
NVVK_CHECK
(m_allocator.
createBuffer
(scratchBuffer, sizeInfo.
buildScratchSize
,
VK_BUFFER_USAGE_2_STORAGE_BUFFER_BIT
|
VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT
,
VMA_MEMORY_USAGE_AUTO
, {}, m_asProperties.
minAccelerationStructureScratchOffsetAlignment
));
NVVK_DBG_NAME
(scratchBuffer.
buffer
);
VkAccelerationStructureCreateInfoKHR createInfo = tlasData.
makeCreateInfo
();
NVVK_CHECK
(m_allocator.
createAcceleration
(m_topAs, createInfo));
NVVK_DBG_NAME
(m_topAs.
accel
);
tlasData.
cmdBuildAccelerationStructure
(cmd, m_topAs.
accel
, scratchBuffer.
address
);
uploader.
cmdUploadAppended
(cmd);
nvvk::accelerationStructureBarrier
(cmd,
VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR
,
VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR
);
m_app->
submitAndWaitTempCmdBuffer
(cmd);
uploader.
deinit
();
m_allocator.
destroyBuffer
(instancesBuffer);
m_allocator.
destroyBuffer
(scratchBuffer);
}
//
--------------------------------------------------------------------------------------------------
//
Creating the pipeline: shader ...
//
void
createCompPipelines
()
{
//
This descriptor set, holds the top level acceleration structure and the output image
m_descriptorBinding.
addBinding
(B_tlas,
VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR
,
1
,
VK_SHADER_STAGE_ALL
);
m_descriptorBinding.
addBinding
(B_outImage,
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
,
1
,
VK_SHADER_STAGE_ALL
);
m_descriptorBinding.
addBinding
(B_cameraInfo,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
,
1
,
VK_SHADER_STAGE_ALL
);
m_descriptorBinding.
addBinding
(B_sceneDesc,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
,
1
,
VK_SHADER_STAGE_ALL
);
NVVK_CHECK
(m_descriptorBinding.
createDescriptorSetLayout
(m_device,
VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
,
&m_descriptorSetLayout));
NVVK_DBG_NAME
(m_descriptorSetLayout);
//
pushing time
const
VkPushConstantRange pushConstant{.
stageFlags
=
VK_SHADER_STAGE_ALL
, .
offset
=
0
, .
size
=
sizeof
(shaderio::PushConstant)};
NVVK_CHECK
(
nvvk::createPipelineLayout
(m_device, &m_pipelineLayout, {m_descriptorSetLayout}, {pushConstant}));
#
if
USE_SLANG
VkShaderModuleCreateInfo moduleInfo =
nvsamples::getShaderModuleCreateInfo
(ray_query_slang);
#
else
VkShaderModuleCreateInfo moduleInfo =
nvsamples::getShaderModuleCreateInfo
(ray_query_comp_glsl);
#
endif
VkPipelineShaderStageCreateInfo shaderStage{
.
sType
=
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
.
pNext
= &moduleInfo,
.
stage
=
VK_SHADER_STAGE_COMPUTE_BIT
,
.
pName
=
"
main
"
,
};
VkComputePipelineCreateInfo cpCreateInfo{
.
sType
=
VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
,
.
stage
= shaderStage,
.
layout
= m_pipelineLayout,
};
NVVK_CHECK
(
vkCreateComputePipelines
(m_device, {},
1
, &cpCreateInfo,
nullptr
, &m_pipeline));
NVVK_DBG_NAME
(m_pipeline);
}
void
pushDescriptorSet
(VkCommandBuffer cmd)
{
//
Write to descriptors
nvvk::WriteSetContainer writes{};
writes.
append
(m_descriptorBinding.
getWriteSet
(B_tlas), m_topAs);
writes.
append
(m_descriptorBinding.
getWriteSet
(B_outImage), m_renderTarget.
getColorAttachmentView
(eImgRendered),
VK_IMAGE_LAYOUT_GENERAL
);
writes.
append
(m_descriptorBinding.
getWriteSet
(B_cameraInfo), m_bCameraInfo);
writes.
append
(m_descriptorBinding.
getWriteSet
(B_sceneDesc), m_bSceneDesc);
vkCmdPushDescriptorSetKHR
(cmd,
VK_PIPELINE_BIND_POINT_COMPUTE
, m_pipelineLayout,
0
,
static_cast
<
uint32_t
>(writes.
size
()), writes.
data
());
}
//
--------------------------------------------------------------------------------------------------
//
To be call when renderer need to re-start
//
void
resetFrame
() { m_frame = -
1
; }
//
--------------------------------------------------------------------------------------------------
//
If the camera matrix has changed, resets the frame.
//
otherwise, increments frame.
//
bool
updateFrame
()
{
static
double
ref_fov{
0
};
static
glm::dmat4 ref_cam_matrix;
const
auto
& m = g_cameraManip->
getViewMatrix
();
const
auto
fov = g_cameraManip->
getFov
();
if
(ref_cam_matrix != m || ref_fov != fov)
{
resetFrame
();
ref_cam_matrix = m;
ref_fov = fov;
}
if
(m_frame >= m_maxFrames)
{
return
false
;
}
m_frame++;
return
true
;
}
void
destroyResources
()
{
for
(
auto
& m : m_bMeshes)
{
m_allocator.
destroyBuffer
(m.
vertices
);
m_allocator.
destroyBuffer
(m.
indices
);
}
m_allocator.
destroyBuffer
(m_bCameraInfo);
m_allocator.
destroyBuffer
(m_bPrimInfo);
m_allocator.
destroyBuffer
(m_bSceneDesc);
m_allocator.
destroyBuffer
(m_bInstInfoBuffer);
m_allocator.
destroyBuffer
(m_bMaterials);
m_viewportImage.
deinit
();
m_renderTarget.
deinit
();
for
(
auto
& b : m_bottomAs)
{
m_allocator.
destroyAcceleration
(b);
}
m_allocator.
destroyAcceleration
(m_topAs);
vkDestroyPipelineLayout
(m_device, m_pipelineLayout,
nullptr
);
vkDestroyPipeline
(m_device, m_pipeline,
nullptr
);
vkDestroyDescriptorSetLayout
(m_device, m_descriptorSetLayout,
nullptr
);
m_samplerPool.
deinit
();
m_tonemapper.
deinit
();
m_allocator.
deinit
();
}
void
onLastHeadlessFrame
()
override
{
m_app->
saveImageToFile
(m_renderTarget.
getColorImage
(eImgTonemapped), m_renderTarget.
getSize
(),
nvutils::getExecutablePath
().
replace_extension
(
"
.jpg
"
).
string
());
}
//
--------------------------------------------------------------------------------------------------
//
//
nvapp::Application* m_app{};
nvvk::ResourceAllocator m_allocator;
nvshaders::Tonemapper m_tonemapper{};
shaderio::TonemapperData m_tonemapperData;
nvvk::RenderTarget m_renderTarget;
//
Offscreen target: tonemapped + rendered color images
nvapp::ImTexture m_viewportImage;
//
ImGui texture displaying the tonemapped image
VkSampler m_linearSampler{};
//
Sampler used to feed the rendered image to the tonemapper
nvvk::SamplerPool m_samplerPool{};
//
Sampler pool (sampler for the tonemapper input)
VkDevice m_device{};
//
Vulkan device
//
Resources
struct
PrimitiveMeshVk
{
nvvk::Buffer vertices;
//
Buffer of the vertices
nvvk::Buffer indices;
//
Buffer of the indices
};
std::vector<PrimitiveMeshVk> m_bMeshes;
nvvk::Buffer m_bCameraInfo;
//
Camera information
nvvk::Buffer m_bPrimInfo;
//
Buffer of all PrimitiveMeshVk
nvvk::Buffer m_bSceneDesc;
//
Scene description with pointers to the buffers
nvvk::Buffer m_bInstInfoBuffer;
//
Transformation and material per instance
nvvk::Buffer m_bMaterials;
//
All materials
//
Data and setting
std::vector<nvutils::PrimitiveMesh> m_meshes;
std::vector<nvutils::Node> m_nodes;
std::vector<shaderio::Material> m_materials;
shaderio::Light m_light = {};
//
Pipeline
shaderio::PushConstant m_pushConst{};
//
Information sent to the shader
int
m_frame{
0
};
int
m_maxFrames{
10000
};
//
Pipeline
nvvk::DescriptorBindings m_descriptorBinding{};
VkDescriptorSetLayout m_descriptorSetLayout{};
VkPipeline m_pipeline{};
VkPipelineLayout m_pipelineLayout{};
VkPhysicalDeviceRayTracingPipelinePropertiesKHR m_rtProperties{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR
};
VkPhysicalDeviceAccelerationStructurePropertiesKHR m_asProperties{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR
};
//
Acceleration Structures
std::vector<nvvk::AccelerationStructure> m_bottomAs;
nvvk::AccelerationStructure m_topAs;
};
//
////////////////////////////////////////////////////////////////////////
//
/
//
/
//
/
auto
main
(
int
argc,
char
** argv) -> int
{
nvapp::ApplicationCreateInfo appInfo;
nvutils::ParameterParser
cli
(
nvutils::getExecutablePath
().
stem
().
string
());
nvutils::ParameterRegistry reg;
bool
verbose =
false
;
reg.
add
({
"
verbose
"
,
"
Verbose output of the Vulkan context
"
}, &verbose);
reg.
add
({
"
headless
"
}, &appInfo.
headless
,
true
);
reg.
add
({
"
frames
"
,
"
Number of frames to run in headless mode
"
}, &appInfo.
headlessFrameCount
);
cli.
add
(reg);
cli.
parse
(argc, argv);
//
#VKRay: Activate the ray tracing extension
//
Extension feature needed.
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelFeature{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR
};
VkPhysicalDeviceRayTracingPipelineFeaturesKHR rtPipelineFeature{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR
};
VkPhysicalDeviceRayQueryFeaturesKHR rayqueryFeature{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR
};
VkPhysicalDeviceShaderObjectFeaturesEXT shaderObjFeature{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_OBJECT_FEATURES_EXT
};
nvvk::ContextInitInfo vkSetup{
.
instanceExtensions
= {
VK_EXT_DEBUG_UTILS_EXTENSION_NAME
},
.
deviceExtensions
= {{
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME
},
{
VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME
},
{
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME
},
{
VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME
, &accelFeature},
{
VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME
, &rtPipelineFeature},
{
VK_EXT_SHADER_OBJECT_EXTENSION_NAME
, &shaderObjFeature},
{
VK_KHR_RAY_QUERY_EXTENSION_NAME
, &rayqueryFeature}},
};
if
(!appInfo.
headless
)
{
nvvk::addSurfaceExtensions
(vkSetup.
instanceExtensions
);
vkSetup.
deviceExtensions
.
emplace_back
(
VK_KHR_SWAPCHAIN_EXTENSION_NAME
);
}
//
Create the Vulkan context
nvvk::Context vkContext;
vkSetup.
verbose
|= verbose;
if
(vkContext.
init
(vkSetup) !=
VK_SUCCESS
)
{
LOGE
(
"
Error in Vulkan context creation
\n
"
);
return
1
;
}
//
Application settings
appInfo.
name
=
fmt::format
(
"
{} ({})
"
,
nvutils::getExecutablePath
().
stem
().
string
(),
SHADER_LANGUAGE_STR
);
appInfo.
vSync
=
false
;
appInfo.
instance
= vkContext.
getInstance
();
appInfo.
device
= vkContext.
getDevice
();
appInfo.
physicalDevice
= vkContext.
getPhysicalDevice
();
appInfo.
queues
= vkContext.
getQueueInfos
();
//
Create the application
nvapp::Application app;
app.
init
(appInfo);
//
Add all application elements
auto
elemCamera = std::make_shared<nvapp::ElementCamera>();
g_cameraManip = std::make_shared<nvutils::CameraManipulator>();
elemCamera->
setCameraManipulator
(g_cameraManip);
app.
addElement
(elemCamera);
app.
addElement
(std::make_shared<nvapp::ElementDefaultMenu>());
//
Menu / Quit
app.
addElement
(std::make_shared<nvapp::ElementDefaultWindowTitle>(
"
"
, appInfo.
name
));
//
Window title info
app.
addElement
(std::make_shared<RayQuery>());
app.
run
();
app.
deinit
();
vkContext.
deinit
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL