init
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
# Copyright (c) 2021-2025, 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.
|
||||
#
|
||||
|
||||
get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
|
||||
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} PATH)
|
||||
get_filename_component(CATEGORY_NAME ${PARENT_DIR} NAME)
|
||||
|
||||
file(GLOB GLSL_FILES "${CMAKE_SOURCE_DIR}/shaders/${FOLDER_NAME}/*.*")
|
||||
|
||||
add_sample_with_tags(
|
||||
ID ${FOLDER_NAME}
|
||||
FILES ${GLSL_FILES}
|
||||
CATEGORY ${CATEGORY_NAME}
|
||||
AUTHOR "Martin-Karl Lefrançois"
|
||||
NAME "Ray tracing reflection"
|
||||
DESCRIPTION "Example for hardware accelerated ray tracing"
|
||||
SHADER_FILES_GLSL
|
||||
"ray_tracing_reflection/raygen.rgen"
|
||||
"ray_tracing_reflection/miss.rmiss"
|
||||
"ray_tracing_reflection/missShadow.rmiss"
|
||||
"ray_tracing_reflection/closesthit.rchit"
|
||||
GLSLC_ADDITIONAL_ARGUMENTS
|
||||
"--target-spv=spv1.5"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
////
|
||||
* Copyright (c) 2014-2025, 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) 2014-2025 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
////
|
||||
= Ray tracing - reflection
|
||||
|
||||
ifdef::site-gen-antora[]
|
||||
TIP: The source for this sample can be found in the https://github.com/KhronosGroup/Vulkan-Samples/tree/main/samples/extensions/ray_tracing_reflection[Khronos Vulkan samples github repository].
|
||||
endif::[]
|
||||
|
||||
|
||||
image::./img1.png[]
|
||||
|
||||
== Overview
|
||||
|
||||
This sample is a extended version of the link:../ray_tracing_basic[ray tracing basic] with the addition of multiple geometries, instances and materials.
|
||||
|
||||
In addition, this sample is showing how to cast shadow rays and reflections.
|
||||
|
||||
== Geometries, bottom, and top-level acceleration structures
|
||||
|
||||
The structures of the geometry are like those described in the https://en.wikipedia.org/wiki/Wavefront_.obj_file[OBJ format].
|
||||
For our geometry there is a list of vertices (position and normal) and a triplet of indices for each triangle.
|
||||
|
||||
Each triangle also has an index for a material.
|
||||
In this example, each object has its own list of materials, but we could have made it so that the materials are shared by all objects in the scene.
|
||||
|
||||
The example scene has two geometries: a plane and a cube.
|
||||
|
||||
You can see the creation of the scene under `RaytracingReflection::create_scene()`.
|
||||
|
||||
=== `create_model()`
|
||||
|
||||
This function allocates and upload the geometry to the GPU.
|
||||
There are four buffers per geometry.
|
||||
|
||||
* Vertices: the position and normal
|
||||
* Indices: index of vertex to form a triangle
|
||||
* Material index: material id per triangle
|
||||
* Materials: a list of materials (albedo, specular, reflection)
|
||||
|
||||
=== `create_buffer_references()`
|
||||
|
||||
In this example, buffer references are used.
|
||||
Instead of having a descriptor set with multiple arrays to access the buffers, we create a buffer that contains the addresses of the scene models.
|
||||
With this method, we can easily access the data of the model we hit in the shader.
|
||||
|
||||
In the shader, `VkDeviceAddress` are `uint64_t`, therefore we will access a buffer of an array of structure `ObjBuffers`.
|
||||
|
||||
[,cpp]
|
||||
----
|
||||
struct ObjBuffers
|
||||
{
|
||||
uint64_t vertices;
|
||||
uint64_t indices;
|
||||
uint64_t materials;
|
||||
uint64_t materialIndices;
|
||||
};
|
||||
layout(set = 0, binding = 3) buffer _scene_desc { ObjBuffers i[]; } scene_desc;
|
||||
----
|
||||
|
||||
The addresses correspond to buffers, so we will declare them like this:
|
||||
|
||||
[,cpp]
|
||||
----
|
||||
layout(buffer_reference, scalar) buffer Vertices {Vertex v[]; }; // Positions of an object
|
||||
layout(buffer_reference, scalar) buffer Indices {uvec3 i[]; }; // Triangle indices
|
||||
layout(buffer_reference, scalar) buffer Materials {WaveFrontMaterial m[]; }; // Array of all materials on an object
|
||||
layout(buffer_reference, scalar) buffer MatIndices {int i[]; }; // Material ID for each triangle
|
||||
----
|
||||
|
||||
The in the shader, to access the data of an object, we will do the following:
|
||||
|
||||
[,cpp]
|
||||
----
|
||||
ObjBuffers objResource = scene_desc.i[gl_InstanceCustomIndexEXT];
|
||||
MatIndices matIndices = MatIndices(objResource.materialIndices);
|
||||
Materials materials = Materials(objResource.materials);
|
||||
Indices indices = Indices(objResource.indices);
|
||||
Vertices vertices = Vertices(objResource.vertices);
|
||||
----
|
||||
|
||||
Note that `gl_InstanceCustomIndexEXT` was set with one of the three scene objects.
|
||||
See `RaytracingReflection::create_blas_instance`.
|
||||
|
||||
=== `create_bottom_level_acceleration_structure()`
|
||||
|
||||
We build a lower-level acceleration structure (BLAS) for each geometry: a cube with one material on each face (0), a plane (1), and a mirror cube (2).
|
||||
|
||||
These BLAS are instantiated by the top-level acceleration structure (TLAS) with a transformation matrix.
|
||||
|
||||
In this example w are calling separately the construction of all BLAS, allocating scratch buffer each time.
|
||||
A https://nvpro-samples.github.io/vk_raytracing_tutorial_KHR/#accelerationstructure/bottom-levelaccelerationstructure/helperdetails:raytracingbuilder::buildblas()[better way] would be to build them, knowing the size the biggest scratch buffer and doing all at once.
|
||||
Also provide in https://nvpro-samples.github.io/vk_raytracing_tutorial_KHR/#accelerationstructure/bottom-levelaccelerationstructure/helperdetails:raytracingbuilder::buildblas()[the helper] , is the ability to compact the memory used to store the BLAS when using the `VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR` flag.
|
||||
|
||||
=== `create_top_level_acceleration_structure()`
|
||||
|
||||
The top-level acceleration structure (TLAS) embeds multiple BLAS.
|
||||
It is possible to reuse the same BLAS and give it a different transformation matrix to place it in a different position in the scene.
|
||||
We see this in `create_scene()`, the same BLAS id is reused with different matrices.
|
||||
|
||||
Note, the BLAS id will be identified by the `gl_InstanceCustomIndexEXT` in the shader.
|
||||
|
||||
image::./img2.png[]
|
||||
|
||||
== Ray tracing pipeline
|
||||
|
||||
The difference with link:../raytracing_basic[ray tracing basic], is the addition of the second miss-shade module.
|
||||
This is called from the closest-hit shader to detect if there is an object between the hit point and the light.
|
||||
|
||||
More on ray tracing pipeline https://nvpro-samples.github.io/vk_raytracing_tutorial_KHR/#raytracingpipeline[here].
|
||||
|
||||
=== Shadows
|
||||
|
||||
In the closest-hit shader, we trace a ray to determine if there is an object between the hit point and the light.
|
||||
For this trace ray, we use the shadow-miss shader (index 1) and a different payload (index 1) that contains only one boolean value, "[.code]``isShadowed``".
|
||||
We assume that an object is blocking the light, so we initialize the value to `true`.
|
||||
Then, if the ray hits nothing, we link:missShadow.rmiss[set this value to false].
|
||||
|
||||
The origin of the ray is the hit position and the direction of the ray is toward the light.
|
||||
Note, we are using an hardcoded infinite light *L*.
|
||||
|
||||
This method for shooting shadow rays is fast because we set the trace flag to skip execution of the closest-hit and terminate on the first hit, then only execute the shadow-miss-shader and set a small payload.
|
||||
|
||||
[,cpp]
|
||||
----
|
||||
uint flags = gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsOpaqueEXT | gl_RayFlagsSkipClosestHitShaderEXT;
|
||||
----
|
||||
|
||||
=== Reflection
|
||||
|
||||
For reflection, we do not change `maxPipelineRayRecursionDepth` and leave the value at 2.
|
||||
Instead of recursively looping from the closest-hit shader, we store the information of the next ray in the payload and send new rays from the ray generation shader.
|
||||
|
||||
When we call `traceRayEXT` from the closest-hit shader, it must store the state of all variables needed after execution.
|
||||
Recursively calling `traceRayEXT` requires storing a lot of data per ray call, and that is typically slow.
|
||||
|
||||
Instead, we store in the payload the ray origin and the ray direction that the ray generation shader will use.
|
||||
This method also removes the pipeline ray recursion depth constraint.
|
||||
|
||||
Here is how the payload is defined:
|
||||
|
||||
[,cpp]
|
||||
----
|
||||
struct hitPayload
|
||||
{
|
||||
vec3 radiance;
|
||||
vec3 attenuation;
|
||||
int done;
|
||||
vec3 rayOrigin;
|
||||
vec3 rayDir;
|
||||
};
|
||||
----
|
||||
|
||||
The radiance is the value at the point of impact multiplied by the attenuation.
|
||||
The first time the attenuation is vec3(1) (no attenuation), but the shininess of the material reduces the attenuation in the following hits.
|
||||
After a few passes, the radiance will be close to vec3(0).
|
||||
The `done` is an indication that the ray did not hit anything.
|
||||
The miss shader sets it to true and the loop can be terminated.
|
||||
The origin of the ray starts at the camera, and is replaced by the position of the target.
|
||||
The direction starts at the camera direction, and then is reflected purely at the surface of the object.
|
||||
|
||||
The recursion limit is set in the ray-generation shader.
|
||||
Currently it is set to *64*, changing its value will change the number of times the ray bounces off.
|
||||
|
||||
NOTE: we could add a test on the attenuation and exist the loop if the value is below a certain threshold.
|
||||
|
||||
== Diagram of the ray pipeline
|
||||
|
||||
image::./img3.png[]
|
||||
|
||||
== Other Tutorial
|
||||
|
||||
The https://github.com/nvpro-samples/vk_raytracing_tutorial_KHR/tree/master/ray_tracing_reflections[following tutorial] is showing the limitation of hitting the recursion limits.
|
||||
Note, the spec does not guarantee a recursion check at runtime.
|
||||
If you exceed either the recursion depth you reported in the raytrace pipeline create info, or the physical device recursion limit, undefined behavior results.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* More complex example for hardware accelerated ray tracing using VK_KHR_ray_tracing_pipeline and VK_KHR_acceleration_structure
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "api_vulkan_sample.h"
|
||||
|
||||
struct ObjMaterial
|
||||
{
|
||||
glm::vec3 diffuse{0.7f, 0.7f, 0.7f};
|
||||
glm::vec3 specular{0.7f, 0.7f, 0.7f};
|
||||
float shininess{0.f};
|
||||
};
|
||||
|
||||
struct ObjVertex
|
||||
{
|
||||
glm::vec3 pos;
|
||||
glm::vec3 nrm;
|
||||
};
|
||||
|
||||
struct ObjModelCpu
|
||||
{
|
||||
std::vector<ObjVertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
std::vector<int32_t> mat_index;
|
||||
};
|
||||
|
||||
struct ObjModelGpu
|
||||
{
|
||||
uint32_t nb_indices{0};
|
||||
uint32_t nb_vertices{0};
|
||||
std::unique_ptr<vkb::core::BufferC> vertex_buffer; // Device buffer of all 'Vertex'
|
||||
std::unique_ptr<vkb::core::BufferC> index_buffer; // Device buffer of the indices forming triangles
|
||||
std::unique_ptr<vkb::core::BufferC> mat_color_buffer; // Device buffer of array of 'Wavefront material'
|
||||
std::unique_ptr<vkb::core::BufferC> mat_index_buffer; // Device buffer of array of 'Wavefront material'
|
||||
};
|
||||
|
||||
class RaytracingReflection : public ApiVulkanSample
|
||||
{
|
||||
struct AccelerationStructure
|
||||
{
|
||||
VkAccelerationStructureKHR handle;
|
||||
std::unique_ptr<vkb::core::BufferC> buffer;
|
||||
};
|
||||
|
||||
public:
|
||||
VkPhysicalDeviceRayTracingPipelinePropertiesKHR ray_tracing_pipeline_properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR};
|
||||
VkPhysicalDeviceAccelerationStructureFeaturesKHR acceleration_structure_features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR};
|
||||
|
||||
std::vector<AccelerationStructure> bottom_level_acceleration_structure;
|
||||
AccelerationStructure top_level_acceleration_structure;
|
||||
|
||||
std::vector<ObjModelGpu> obj_models; // Array of objects and instances in the scene
|
||||
std::vector<VkRayTracingShaderGroupCreateInfoKHR> shader_groups; // Shader groups
|
||||
|
||||
// Shading Binding Table
|
||||
std::unique_ptr<vkb::core::BufferC> raygen_shader_binding_table;
|
||||
std::unique_ptr<vkb::core::BufferC> miss_shader_binding_table;
|
||||
std::unique_ptr<vkb::core::BufferC> hit_shader_binding_table;
|
||||
|
||||
struct StorageImage
|
||||
{
|
||||
VkDeviceMemory memory{VK_NULL_HANDLE};
|
||||
VkImage image{VK_NULL_HANDLE};
|
||||
VkImageView view{VK_NULL_HANDLE};
|
||||
VkFormat format{VK_FORMAT_UNDEFINED};
|
||||
uint32_t width{0};
|
||||
uint32_t height{0};
|
||||
} storage_image;
|
||||
|
||||
struct UniformData
|
||||
{
|
||||
glm::mat4 view_inverse;
|
||||
glm::mat4 proj_inverse;
|
||||
} uniform_data;
|
||||
std::unique_ptr<vkb::core::BufferC> ubo;
|
||||
|
||||
struct ObjBuffers
|
||||
{
|
||||
VkDeviceAddress vertices;
|
||||
VkDeviceAddress indices;
|
||||
VkDeviceAddress materials;
|
||||
VkDeviceAddress materialIndices;
|
||||
} obj_buffers;
|
||||
std::unique_ptr<vkb::core::BufferC> scene_desc;
|
||||
|
||||
VkPipeline pipeline{VK_NULL_HANDLE};
|
||||
VkPipelineLayout pipeline_layout{VK_NULL_HANDLE};
|
||||
VkDescriptorSet descriptor_set{VK_NULL_HANDLE};
|
||||
VkDescriptorSetLayout descriptor_set_layout{VK_NULL_HANDLE};
|
||||
|
||||
RaytracingReflection();
|
||||
~RaytracingReflection();
|
||||
|
||||
void create_storage_image();
|
||||
void create_bottom_level_acceleration_structure(ObjModelGpu &obj_model);
|
||||
void create_top_level_acceleration_structure(std::vector<VkAccelerationStructureInstanceKHR> &blas_instances);
|
||||
void create_model(ObjModelCpu &obj, const std::vector<ObjMaterial> &materials);
|
||||
auto create_blas_instance(uint32_t blas_id, glm::mat4 &mat);
|
||||
void delete_acceleration_structure(AccelerationStructure &acceleration_structure);
|
||||
void create_scene();
|
||||
|
||||
void create_buffer_references();
|
||||
|
||||
void create_shader_binding_tables();
|
||||
void create_descriptor_sets();
|
||||
void create_ray_tracing_pipeline();
|
||||
void create_uniform_buffer();
|
||||
void update_uniform_buffers();
|
||||
void draw();
|
||||
|
||||
void build_command_buffers() override;
|
||||
void request_gpu_features(vkb::PhysicalDevice &gpu) override;
|
||||
bool prepare(const vkb::ApplicationOptions &options) override;
|
||||
void render(float delta_time) override;
|
||||
};
|
||||
|
||||
std::unique_ptr<vkb::VulkanSampleC> create_ray_tracing_reflection();
|
||||
Reference in New Issue
Block a user