├── .clang-format ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── common ├── obj_loader.cpp └── obj_loader.h ├── docs ├── Images │ ├── AccelerationStructure.svg │ ├── ShaderPipeline.svg │ ├── VkInstanceNsight1.png │ ├── VkInstanceNsight2.png │ ├── VkInstances.png │ ├── animation1.gif │ ├── animation2.gif │ ├── antialiasing.png │ ├── anyhit.png │ ├── anyhit_0.png │ ├── anyhit_01.png │ ├── callable.png │ ├── manyhits.png │ ├── manyhits2.png │ ├── manyhits3.png │ ├── manyhits4.png │ ├── ray_tracing__advance.png │ ├── ray_tracing_intersection.png │ ├── reflections.png │ ├── resultRasterCube.png │ ├── resultRaytraceEmptyCube.png │ ├── resultRaytraceFlatCube.png │ ├── resultRaytraceLightGreyCube.png │ ├── resultRaytraceLightMatCube.png │ ├── resultRaytraceLightMatMedieval.png │ └── resultRaytraceShadowMedieval.png ├── README.md ├── files │ ├── shaders.zip │ └── shadowShaders.zip ├── index.html ├── setup.md.html ├── vkrt_tuto_animation.md.html ├── vkrt_tuto_anyhit.md.html ├── vkrt_tuto_callable.md.html ├── vkrt_tuto_further.md.html ├── vkrt_tuto_instances.md.html ├── vkrt_tuto_intersection.md.html ├── vkrt_tuto_jitter_cam.md.html ├── vkrt_tuto_manyhits.md.html ├── vkrt_tuto_reflection.md.html ├── vkrt_tutorial.css └── vkrt_tutorial.md.html ├── media ├── scenes │ ├── Medieval_building.mtl │ ├── Medieval_building.obj │ ├── cube.mtl │ ├── cube.obj │ ├── cube_multi.mtl │ ├── cube_multi.obj │ ├── plane.mtl │ ├── plane.obj │ ├── sphere.mtl │ ├── sphere.obj │ ├── wuson.mtl │ └── wuson.obj └── textures │ ├── RooftilesWood0005_2_S.jpg │ ├── RooftilesWood0005_2_Sspec.jpg │ ├── WoodPlanksBare0002_1_S.jpg │ ├── WoodPlanksBare0002_1_Sspec.jpg │ ├── WoodRough0106_2_S.jpg │ ├── WoodRough0106_2_Spec.jpg │ ├── black.jpg │ ├── compass.jpg │ ├── default_specular.jpg │ ├── out_0_6E339CC8.png │ ├── panneau_col.jpg │ ├── white.jpg │ └── yellow.jpg ├── ray_tracing__advance ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp ├── obj.hpp ├── offscreen.cpp ├── offscreen.hpp ├── raytrace.cpp ├── raytrace.hpp ├── shaders │ ├── frag_shader.frag │ ├── light_inf.rcall │ ├── light_point.rcall │ ├── light_spot.rcall │ ├── passthrough.vert │ ├── post.frag │ ├── random.glsl │ ├── raycommon.glsl │ ├── raytrace.rahit │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rint │ ├── raytrace.rmiss │ ├── raytrace2.rahit │ ├── raytrace2.rchit │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl └── vkalloc.hpp ├── ray_tracing__before ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing__simple ├── CMakeLists.txt ├── README.md ├── files │ ├── shaders.zip │ └── shadowShaders.zip ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_animation ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── anim.comp │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_anyhit ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── random.glsl │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytraceShadow.rmiss │ ├── raytrace_0.rahit │ ├── raytrace_1.rahit │ ├── raytrace_rahit.glsl │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_callable ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── light_inf.rcall │ ├── light_point.rcall │ ├── light_spot.rcall │ ├── passthrough.vert │ ├── post.frag │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_instances ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_intersection ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rint │ ├── raytrace.rmiss │ ├── raytrace2.rchit │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_jitter_cam ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── random.glsl │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl ├── ray_tracing_manyhits ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders │ ├── frag_shader.frag │ ├── passthrough.vert │ ├── post.frag │ ├── raycommon.glsl │ ├── raytrace.rchit │ ├── raytrace.rgen │ ├── raytrace.rmiss │ ├── raytrace2.rchit │ ├── raytraceShadow.rmiss │ ├── vert_shader.vert │ └── wavefront.glsl └── ray_tracing_reflections ├── CMakeLists.txt ├── README.md ├── hello_vulkan.cpp ├── hello_vulkan.h ├── main.cpp └── shaders ├── frag_shader.frag ├── passthrough.vert ├── post.frag ├── raycommon.glsl ├── raytrace.rchit ├── raytrace.rgen ├── raytrace.rmiss ├── raytraceShadow.rmiss ├── vert_shader.vert └── wavefront.glsl /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | AccessModifierOffset: '-2' 3 | AlignAfterOpenBracket: Align 4 | AlignConsecutiveAssignments: 'true' 5 | AlignConsecutiveDeclarations: 'true' 6 | AlignOperands: 'true' 7 | AlignTrailingComments: 'true' 8 | AllowAllParametersOfDeclarationOnNextLine: 'false' 9 | AllowShortBlocksOnASingleLine: 'false' 10 | AllowShortCaseLabelsOnASingleLine: 'false' 11 | AllowShortFunctionsOnASingleLine: Inline 12 | AllowShortIfStatementsOnASingleLine: 'false' 13 | AllowShortLoopsOnASingleLine: 'false' 14 | AlwaysBreakAfterReturnType: None 15 | AlwaysBreakBeforeMultilineStrings: 'true' 16 | AlwaysBreakTemplateDeclarations: 'true' 17 | BinPackArguments: 'true' 18 | BinPackParameters: 'false' 19 | ExperimentalAutoDetectBinPacking: 'false' 20 | BreakBeforeBinaryOperators: NonAssignment 21 | BreakBeforeBraces: Custom 22 | BreakBeforeTernaryOperators: 'false' 23 | BreakConstructorInitializersBeforeComma: 'true' 24 | ColumnLimit: '100' 25 | ConstructorInitializerAllOnOneLineOrOnePerLine: 'false' 26 | Cpp11BracedListStyle: 'true' 27 | IndentCaseLabels: 'true' 28 | IndentWidth: '2' 29 | KeepEmptyLinesAtTheStartOfBlocks: 'true' 30 | Language: Cpp 31 | MaxEmptyLinesToKeep: '2' 32 | NamespaceIndentation: None 33 | ObjCSpaceBeforeProtocolList: 'true' 34 | PointerAlignment: Left 35 | SpaceAfterCStyleCast: 'false' 36 | SpaceBeforeAssignmentOperators: 'true' 37 | SpaceBeforeParens: Never 38 | SpaceInEmptyParentheses: 'false' 39 | SpacesBeforeTrailingComments: '2' 40 | SpacesInAngles: 'false' 41 | SpacesInCStyleCastParentheses: 'false' 42 | SpacesInParentheses: 'false' 43 | SpacesInSquareBrackets: 'false' 44 | Standard: Cpp11 45 | TabWidth: '2' 46 | UseTab: Never 47 | SortIncludes: 'true' 48 | ReflowComments: 'false' 49 | BraceWrapping: { 50 | AfterClass: 'true' 51 | AfterControlStatement: 'true' 52 | AfterEnum: 'true' 53 | AfterFunction: 'true' 54 | AfterNamespace: 'false' 55 | AfterStruct: 'true' 56 | AfterUnion: 'true' 57 | BeforeCatch: 'true' 58 | BeforeElse: 'true' 59 | IndentBraces: 'false' 60 | } 61 | PenaltyExcessCharacter: 9999 62 | PenaltyBreakBeforeFirstCallParameter: 40 63 | PenaltyBreakFirstLessLess: 1 64 | PenaltyBreakComment: 30 65 | PenaltyBreakString: 30 66 | PenaltyReturnTypeOnItsOwnLine: 9999 67 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/shaders/*.spv 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(vk_raytracing_tutorial) 3 | 4 | 5 | ##################################################################################### 6 | # look for nvpro_core 1) as a sub-folder 2) at some other locations 7 | # this cannot be put anywhere else since we still didn't find setup.cmake yet 8 | # 9 | if(NOT BASE_DIRECTORY) 10 | 11 | find_path(BASE_DIRECTORY 12 | NAMES nvpro_core/cmake/setup.cmake 13 | PATHS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/../.. 14 | REQUIRED 15 | DOC "Directory containing nvpro_core" 16 | ) 17 | endif() 18 | if(EXISTS ${BASE_DIRECTORY}/nvpro_core/cmake/setup.cmake) 19 | include(${BASE_DIRECTORY}/nvpro_core/cmake/setup.cmake) 20 | else() 21 | message(FATAL_ERROR "could not find base directory, please set BASE_DIRECTORY to folder containing nvpro_core") 22 | endif() 23 | 24 | set(CMAKE_CXX_STANDARD 17) 25 | 26 | _add_package_VulkanSDK() 27 | _add_package_ImGUI() 28 | 29 | _add_nvpro_core_lib() 30 | 31 | add_subdirectory(ray_tracing__advance) 32 | add_subdirectory(ray_tracing__before) 33 | add_subdirectory(ray_tracing__simple) 34 | add_subdirectory(ray_tracing_animation) 35 | add_subdirectory(ray_tracing_anyhit) 36 | add_subdirectory(ray_tracing_callable) 37 | add_subdirectory(ray_tracing_jitter_cam) 38 | add_subdirectory(ray_tracing_manyhits) 39 | add_subdirectory(ray_tracing_reflections) 40 | add_subdirectory(ray_tracing_instances) 41 | add_subdirectory(ray_tracing_intersection) 42 | 43 | install(DIRECTORY "media" CONFIGURATIONS Release DESTINATION "bin_${ARCH}") 44 | install(DIRECTORY "media" CONFIGURATIONS Debug DESTINATION "bin_${ARCH}_debug") -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![logo](http://nvidianews.nvidia.com/_ir/219/20157/NV_Designworks_logo_horizontal_greenblack.png) 2 | 3 | # NVIDIA Vulkan Ray Tracing Tutorials 4 | 5 | ****************************************************************************************** 6 | **Newer Tutorial version based on KHR** 7 | 8 | There is a newer version of this tutorial, based on VK_KHR_ray_tracing extension. 9 | * GitHub: https://github.com/nvpro-samples/vk_raytracing_tutorial_KHR 10 | 11 | This repository about the NV extension has been archived, we recommend using the 12 | KHR extension and following the above repository for future updates. 13 | ****************************************************************************************** 14 | 15 | The focus of this project and the provided code is to showcase a basic integration of 16 | ray tracing within an existing Vulkan sample, using the 17 | [`VK_NV_ray_tracing`](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VK_NV_ray_tracing) extension. 18 | The following tutorials starts from a the end of the previous ray tracing tutorial and provides step-by-step instructions to modify and add methods and functions. 19 | The sections are organized by components, with subsections identifying the modified functions. 20 | 21 | This project contains multiple tutorials all around Vulkan ray tracing. 22 | 23 | Instead of having examples fully functional, those tutorial starts from a program and guide the user to add what is necessary. 24 | 25 | ## Ray Tracing Tutorial 26 | 27 | The first tutorial is starting from a Vulkan code example, which can load multiple OBJ and render them using the rasterizer, and adds step-by-step what is require to do ray tracing. 28 | 29 | ### [**Start Ray Tracing Tutorial**](https://nvpro-samples.github.io/vk_raytracing_tutorial/) 30 | 31 | ![resultRaytraceShadowMedieval](docs/Images/resultRaytraceShadowMedieval.png) 32 | 33 | # Going Further 34 | 35 | From this point on, you can continue creating your own ray types and shaders, and experiment with more advanced ray tracing based algorithms. 36 | 37 | ### [**All Extra Tutorials**](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_further.md.html) 38 | -------------------------------------------------------------------------------- /common/obj_loader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2014-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | #include "nvmath/nvmath.h" 22 | #include "tiny_obj_loader.h" 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | // Structure holding the material 29 | struct MaterialObj 30 | { 31 | nvmath::vec3f ambient = nvmath::vec3f(0.1f, 0.1f, 0.1f); 32 | nvmath::vec3f diffuse = nvmath::vec3f(0.7f, 0.7f, 0.7f); 33 | nvmath::vec3f specular = nvmath::vec3f(1.0f, 1.0f, 1.0f); 34 | nvmath::vec3f transmittance = nvmath::vec3f(0.0f, 0.0f, 0.0f); 35 | nvmath::vec3f emission = nvmath::vec3f(0.0f, 0.0f, 0.10); 36 | float shininess = 0.f; 37 | float ior = 1.0f; // index of refraction 38 | float dissolve = 1.f; // 1 == opaque; 0 == fully transparent 39 | // illumination model (see http://www.fileformat.info/format/material/) 40 | int illum = 0; 41 | int textureID = -1; 42 | }; 43 | // OBJ representation of a vertex 44 | struct VertexObj 45 | { 46 | nvmath::vec3f pos; 47 | nvmath::vec3f nrm; 48 | nvmath::vec3f color; 49 | nvmath::vec2f texCoord; 50 | }; 51 | 52 | 53 | struct shapeObj 54 | { 55 | uint32_t offset; 56 | uint32_t nbIndex; 57 | uint32_t matIndex; 58 | }; 59 | 60 | class ObjLoader 61 | { 62 | public: 63 | void loadModel(const std::string& filename); 64 | 65 | std::vector m_vertices; 66 | std::vector m_indices; 67 | std::vector m_materials; 68 | std::vector m_textures; 69 | std::vector m_matIndx; 70 | }; 71 | -------------------------------------------------------------------------------- /docs/Images/VkInstanceNsight1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/VkInstanceNsight1.png -------------------------------------------------------------------------------- /docs/Images/VkInstanceNsight2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/VkInstanceNsight2.png -------------------------------------------------------------------------------- /docs/Images/VkInstances.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/VkInstances.png -------------------------------------------------------------------------------- /docs/Images/animation1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/animation1.gif -------------------------------------------------------------------------------- /docs/Images/animation2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/animation2.gif -------------------------------------------------------------------------------- /docs/Images/antialiasing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/antialiasing.png -------------------------------------------------------------------------------- /docs/Images/anyhit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/anyhit.png -------------------------------------------------------------------------------- /docs/Images/anyhit_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/anyhit_0.png -------------------------------------------------------------------------------- /docs/Images/anyhit_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/anyhit_01.png -------------------------------------------------------------------------------- /docs/Images/callable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/callable.png -------------------------------------------------------------------------------- /docs/Images/manyhits.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/manyhits.png -------------------------------------------------------------------------------- /docs/Images/manyhits2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/manyhits2.png -------------------------------------------------------------------------------- /docs/Images/manyhits3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/manyhits3.png -------------------------------------------------------------------------------- /docs/Images/manyhits4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/manyhits4.png -------------------------------------------------------------------------------- /docs/Images/ray_tracing__advance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/ray_tracing__advance.png -------------------------------------------------------------------------------- /docs/Images/ray_tracing_intersection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/ray_tracing_intersection.png -------------------------------------------------------------------------------- /docs/Images/reflections.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/reflections.png -------------------------------------------------------------------------------- /docs/Images/resultRasterCube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRasterCube.png -------------------------------------------------------------------------------- /docs/Images/resultRaytraceEmptyCube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRaytraceEmptyCube.png -------------------------------------------------------------------------------- /docs/Images/resultRaytraceFlatCube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRaytraceFlatCube.png -------------------------------------------------------------------------------- /docs/Images/resultRaytraceLightGreyCube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRaytraceLightGreyCube.png -------------------------------------------------------------------------------- /docs/Images/resultRaytraceLightMatCube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRaytraceLightMatCube.png -------------------------------------------------------------------------------- /docs/Images/resultRaytraceLightMatMedieval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRaytraceLightMatMedieval.png -------------------------------------------------------------------------------- /docs/Images/resultRaytraceShadowMedieval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/Images/resultRaytraceShadowMedieval.png -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Start [Ray Tracing Tutorial](https://nvpro-samples.github.io/vk_raytracing_tutorial/) 3 | -------------------------------------------------------------------------------- /docs/files/shaders.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/files/shaders.zip -------------------------------------------------------------------------------- /docs/files/shadowShaders.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/docs/files/shadowShaders.zip -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | (insert vkrt_tutorial.md.html here) 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | -------------------------------------------------------------------------------- /docs/setup.md.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Environment Setup 4 | 5 | To get support for `VK_NV_ray_tracing`, please install an [NVIDIA driver](http://www.nvidia.com/Download/index.aspx?lang=en-us) 6 | with version 440.97 or later, and the [Vulkan SDK](http://vulkan.lunarg.com/sdk/home) version 1.1.126.0 or later. 7 | 8 | This tutorial is a modification of [`ray_tracing__simple`](https://github.com/nvpro-samples/vk_raytracing_tutorial/tree/master/ray_tracing__simple), which is the result of the ray tracing tutorial. 9 | All following instructions are based on the modification of this project. 10 | 11 | Beside the current repository, you will also need to clone or download the following repositories: 12 | 13 | * [nvpro_core](https://github.com/nvpro-samples/nvpro_core): The primary framework that all samples depend on. 14 | 15 | The directory structure should look like this: 16 | 17 | ********************************************************** 18 | * \ 19 | * | 20 | * +-- 📂 nvpro_core 21 | * | 22 | * +-- 📂 vk_raytracing_tutorial 23 | * | | 24 | * | +-- 📂 ray_tracing__simple (<-- Start here) 25 | * | | 26 | * | +-- 📂 ray_tracing_... 27 | * | ⋮ 28 | * | 29 | * ⋮ 30 | ********************************************************** 31 | 32 | !!! Warning 33 | **Run CMake** in vk_raytracing_tutorial. 34 | 35 | 36 | 37 | 38 | 39 | 42 | -------------------------------------------------------------------------------- /media/scenes/cube.mtl: -------------------------------------------------------------------------------- 1 | newmtl cube_instance_material 2 | illum 3 3 | d 1 4 | Ns 32 5 | Ni 0 6 | Ka 0 0 0 7 | Kd .1 .8 .3 8 | Ks 0.95 0.95 0.95 9 | 10 | -------------------------------------------------------------------------------- /media/scenes/cube.obj: -------------------------------------------------------------------------------- 1 | # NVIDIA Iray Viewer OBJ exporter 2 | 3 | #Error: Object scnCamera of type ELEMENT_TYPE_CAMERA cannot be exported 4 | 5 | mtllib cube.mtl 6 | 7 | o cube 8 | v -0.5 -0.5 -0.5 9 | v -0.5 -0.5 0.5 10 | v -0.5 0.5 -0.5 11 | v -0.5 0.5 0.5 12 | v 0.5 -0.5 -0.5 13 | v 0.5 -0.5 0.5 14 | v 0.5 0.5 -0.5 15 | v 0.5 0.5 0.5 16 | 17 | 18 | vn -1 0 0 19 | vn 0 0 1 20 | vn 1 0 0 21 | vn 0 0 -1 22 | vn 0 -1 0 23 | vn 0 1 0 24 | 25 | 26 | vt 0 0 0 27 | vt 1 0 0 28 | vt 1 1 0 29 | vt 0 1 0 30 | 31 | 32 | f 1/1/1 2/2/1 4/3/1 33 | f 1/1/1 4/3/1 3/4/1 34 | f 2/1/2 6/2/2 8/3/2 35 | f 2/1/2 8/3/2 4/4/2 36 | f 6/1/3 5/2/3 7/3/3 37 | f 6/1/3 7/3/3 8/4/3 38 | f 5/1/4 1/2/4 3/3/4 39 | f 5/1/4 3/3/4 7/4/4 40 | f 5/1/5 6/2/5 2/3/5 41 | f 5/1/5 2/3/5 1/4/5 42 | f 3/1/6 4/2/6 8/3/6 43 | f 3/1/6 8/3/6 7/4/6 44 | 45 | 46 | -------------------------------------------------------------------------------- /media/scenes/cube_multi.mtl: -------------------------------------------------------------------------------- 1 | newmtl grey 2 | illum 1 3 | d 1 4 | Ns 0.975 5 | Ni 0 6 | Ka 0 0 0 7 | Kd 0.7 0.7 0.7 8 | Ks 0 0 0 9 | 10 | newmtl yellow 11 | illum 1 12 | d 1 13 | Ns 0.975 14 | Ni 0 15 | Ka 0 0 0 16 | Kd 0.982062 0.857638 0.400811 17 | Ks 0 0 0 18 | 19 | newmtl red 20 | illum 1 21 | d 1 22 | Ns 0.975 23 | Ni 0 24 | Ka 0 0 0 25 | Kd 0.982062 0.1 0.1 26 | Ks 0 0 0 27 | 28 | newmtl blue 29 | illum 1 30 | d 1 31 | Ns 0.975 32 | Ni 0 33 | Ka 0 0 0 34 | Kd 0.1 0.9 0.1 35 | Ks 0 0 0 36 | 37 | newmtl green 38 | illum 1 39 | d 1 40 | Ns 0.975 41 | Ni 0 42 | Ka 0 0 0 43 | Kd 0.1 0.1 0.982062 44 | Ks 0 0 0 45 | 46 | newmtl magenta 47 | illum 1 48 | d 1 49 | Ns 0.975 50 | Ni 0 51 | Ka 0 0 0 52 | Kd 0.982062 0.1 0.982062 53 | Ks 0 0 0 54 | -------------------------------------------------------------------------------- /media/scenes/cube_multi.obj: -------------------------------------------------------------------------------- 1 | # NVIDIA Iray Viewer OBJ exporter 2 | 3 | #Error: Object scnCamera of type ELEMENT_TYPE_CAMERA cannot be exported 4 | 5 | mtllib cube_multi.mtl 6 | 7 | o cube 8 | v -0.5 -0.5 -0.5 9 | v -0.5 -0.5 0.5 10 | v -0.5 0.5 -0.5 11 | v -0.5 0.5 0.5 12 | v 0.5 -0.5 -0.5 13 | v 0.5 -0.5 0.5 14 | v 0.5 0.5 -0.5 15 | v 0.5 0.5 0.5 16 | 17 | 18 | vn -1 0 0 19 | vn 0 0 1 20 | vn 1 0 0 21 | vn 0 0 -1 22 | vn 0 -1 0 23 | vn 0 1 0 24 | 25 | 26 | vt 0 0 0 27 | vt 1 0 0 28 | vt 1 1 0 29 | vt 0 1 0 30 | 31 | 32 | f 1/1/1 2/2/1 4/3/1 33 | f 1/1/1 4/3/1 3/4/1 34 | usemtl yellow 35 | f 2/1/2 6/2/2 8/3/2 36 | f 2/1/2 8/3/2 4/4/2 37 | usemtl blue 38 | f 6/1/3 5/2/3 7/3/3 39 | f 6/1/3 7/3/3 8/4/3 40 | usemtl red 41 | f 5/1/4 1/2/4 3/3/4 42 | f 5/1/4 3/3/4 7/4/4 43 | usemtl green 44 | f 5/1/5 6/2/5 2/3/5 45 | f 5/1/5 2/3/5 1/4/5 46 | usemtl magenta 47 | f 3/1/6 4/2/6 8/3/6 48 | f 3/1/6 8/3/6 7/4/6 49 | 50 | 51 | -------------------------------------------------------------------------------- /media/scenes/plane.mtl: -------------------------------------------------------------------------------- 1 | newmtl cube_instance_material 2 | illum 2 3 | d 1 4 | Ns 8.0 5 | Ni 0 6 | Ka 0 0 0 7 | Kd 0.8 0.8 0.8 8 | Ks 1 1 1 9 | 10 | -------------------------------------------------------------------------------- /media/scenes/plane.obj: -------------------------------------------------------------------------------- 1 | mtllib plane.mtl 2 | 3 | o cube 4 | v -20.0 0.0 -20.0 5 | v -20.0 0.0 20.0 6 | v 20.0 0.0 -20.0 7 | v 20.0 0.0 20.0 8 | 9 | vn 0 1 0 10 | 11 | vt 0 0 0 12 | vt 1 0 0 13 | vt 1 1 0 14 | vt 0 1 0 15 | 16 | 17 | f 1/1/1 2/2/1 3/3/1 18 | f 2/2/1 4/4/1 3/3/1 19 | 20 | -------------------------------------------------------------------------------- /media/scenes/sphere.mtl: -------------------------------------------------------------------------------- 1 | newmtl default 2 | illum 4 3 | d 0.5 4 | Ns 16 5 | Ni 0 6 | Ka 0 0 0 7 | Kd 0.982062 0.857638 0.400811 8 | Ks 1 1 1 9 | -------------------------------------------------------------------------------- /media/scenes/wuson.mtl: -------------------------------------------------------------------------------- 1 | newmtl default 2 | illum 2 3 | d 1 4 | Ns 32.0 5 | Ni 0 6 | Ka 0 0 0 7 | Kd 0.56 0.49 0.25 8 | Ks 1 1 1 9 | -------------------------------------------------------------------------------- /media/textures/RooftilesWood0005_2_S.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/RooftilesWood0005_2_S.jpg -------------------------------------------------------------------------------- /media/textures/RooftilesWood0005_2_Sspec.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/RooftilesWood0005_2_Sspec.jpg -------------------------------------------------------------------------------- /media/textures/WoodPlanksBare0002_1_S.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/WoodPlanksBare0002_1_S.jpg -------------------------------------------------------------------------------- /media/textures/WoodPlanksBare0002_1_Sspec.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/WoodPlanksBare0002_1_Sspec.jpg -------------------------------------------------------------------------------- /media/textures/WoodRough0106_2_S.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/WoodRough0106_2_S.jpg -------------------------------------------------------------------------------- /media/textures/WoodRough0106_2_Spec.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/WoodRough0106_2_Spec.jpg -------------------------------------------------------------------------------- /media/textures/black.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/black.jpg -------------------------------------------------------------------------------- /media/textures/compass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/compass.jpg -------------------------------------------------------------------------------- /media/textures/default_specular.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/default_specular.jpg -------------------------------------------------------------------------------- /media/textures/out_0_6E339CC8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/out_0_6E339CC8.png -------------------------------------------------------------------------------- /media/textures/panneau_col.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/panneau_col.jpg -------------------------------------------------------------------------------- /media/textures/white.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/white.jpg -------------------------------------------------------------------------------- /media/textures/yellow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/media/textures/yellow.jpg -------------------------------------------------------------------------------- /ray_tracing__advance/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | This example is the combination of all tutorials. 4 | 5 | If you haven't done the tutorials, you can start [here](https://nvpro-samples.github.io/vk_raytracing_tutorial) 6 | 7 | ![](../docs/Images/ray_tracing__advance.png) 8 | -------------------------------------------------------------------------------- /ray_tracing__advance/obj.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2014-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | #include "obj_loader.h" 22 | 23 | // The OBJ model 24 | struct ObjModel 25 | { 26 | uint32_t nbIndices{0}; 27 | uint32_t nbVertices{0}; 28 | nvvk::Buffer vertexBuffer; // Device buffer of all 'Vertex' 29 | nvvk::Buffer indexBuffer; // Device buffer of the indices forming triangles 30 | nvvk::Buffer matColorBuffer; // Device buffer of array of 'Wavefront material' 31 | nvvk::Buffer matIndexBuffer; // Device buffer of array of 'Wavefront material' 32 | }; 33 | 34 | // Instance of the OBJ 35 | struct ObjInstance 36 | { 37 | uint32_t objIndex{0}; // Reference to the `m_objModel` 38 | uint32_t txtOffset{0}; // Offset in `m_textures` 39 | nvmath::mat4f transform{1}; // Position of the instance 40 | nvmath::mat4f transformIT{1}; // Inverse transpose 41 | }; 42 | 43 | // Information pushed at each draw call 44 | struct ObjPushConstants 45 | { 46 | nvmath::vec3f lightPosition{10.f, 15.f, 8.f}; 47 | float lightIntensity{100.f}; 48 | nvmath::vec3f lightDirection{-1, -1, -1}; 49 | float lightSpotCutoff{cos(deg2rad(12.5f))}; 50 | float lightSpotOuterCutoff{cos(deg2rad(17.5f))}; 51 | int instanceId{0}; // To retrieve the transformation matrix 52 | int lightType{0}; // 0: point, 1: infinite 53 | int frame{0}; 54 | }; 55 | 56 | enum EObjType 57 | { 58 | eSphere = 0, 59 | eCube 60 | }; 61 | 62 | // One single implicit object 63 | struct ObjImplicit 64 | { 65 | nvmath::vec3f minimum{0, 0, 0}; // Aabb 66 | nvmath::vec3f maximum{0, 0, 0}; // Aabb 67 | int objType{0}; // 0: Sphere, 1: Cube 68 | int matId{0}; 69 | }; 70 | 71 | // All implicit objects 72 | struct ImplInst 73 | { 74 | std::vector objImpl; // All objects 75 | std::vector implMat; // All materials used by implicit obj 76 | nvvk::Buffer implBuf; // Buffer of objects 77 | nvvk::Buffer implMatBuf; // Buffer of material 78 | int blasId; 79 | nvmath::mat4f transform{1}; 80 | }; 81 | -------------------------------------------------------------------------------- /ray_tracing__advance/offscreen.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2014-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | 21 | #include 22 | 23 | #include "nvvk/debug_util_vk.hpp" 24 | #include "nvvk/descriptorsets_vk.hpp" 25 | #include "vkalloc.hpp" 26 | 27 | //-------------------------------------------------------------------------------------------------- 28 | // Class to render in off-screen framebuffers. Instead of rendering directly to the 29 | // screen back buffer, this class create the output frame buffer 'createFramebuffer', 30 | // and use the pipeline from 'createPipeline' to render a quad 'draw' with the colorTexture 31 | // image 32 | 33 | class Offscreen 34 | { 35 | public: 36 | void setup(const vk::Device& device, nvvk::ResourceAllocator* allocator, uint32_t queueFamily); 37 | void destroy(); 38 | 39 | void createFramebuffer(VkExtent2D& size); 40 | void createPipeline(vk::RenderPass& renderPass); 41 | void createDescriptor(); 42 | void updateDescriptorSet(); 43 | void draw(vk::CommandBuffer cmdBuf, VkExtent2D& size); 44 | 45 | const vk::RenderPass& renderPass() { return m_renderPass; } 46 | const vk::Framebuffer& frameBuffer() { return m_framebuffer; } 47 | const nvvk::Texture& colorTexture() { return m_colorTexture; } 48 | 49 | private: 50 | nvvk::DescriptorSetBindings m_dsetLayoutBinding; 51 | vk::DescriptorPool m_descPool; 52 | vk::DescriptorSetLayout m_dsetLayout; 53 | vk::DescriptorSet m_dset; 54 | vk::Pipeline m_pipeline; 55 | vk::PipelineLayout m_pipelineLayout; 56 | vk::RenderPass m_renderPass; 57 | vk::Framebuffer m_framebuffer; 58 | 59 | nvvk::Texture m_colorTexture; 60 | vk::Format m_colorFormat{vk::Format::eR32G32B32A32Sfloat}; 61 | nvvk::Texture m_depthTexture; 62 | vk::Format m_depthFormat{vk::Format::eD32Sfloat}; 63 | 64 | nvvk::ResourceAllocator* m_alloc{nullptr}; // Allocator for buffer, images, acceleration structures 65 | vk::Device m_device; 66 | int m_graphicsQueueIndex{0}; 67 | nvvk::DebugUtil m_debug; // Utility to name objects 68 | }; 69 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/light_inf.rcall: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 core 21 | #extension GL_NV_ray_tracing : enable 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 3) callableDataInNV rayLight cLight; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | vec3 lightPosition; 31 | float lightIntensity; 32 | vec3 lightDirection; 33 | float lightSpotCutoff; 34 | float lightSpotOuterCutoff; 35 | int lightType; 36 | }; 37 | 38 | void main() 39 | { 40 | cLight.outLightDistance = 10000000; 41 | cLight.outIntensity = 1.0; 42 | cLight.outLightDir = normalize(-lightDirection); 43 | } 44 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/light_point.rcall: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 core 21 | #extension GL_NV_ray_tracing : enable 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 3) callableDataInNV rayLight cLight; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | vec3 lightPosition; 31 | float lightIntensity; 32 | vec3 lightDirection; 33 | float lightSpotCutoff; 34 | float lightSpotOuterCutoff; 35 | int lightType; 36 | }; 37 | 38 | void main() 39 | { 40 | vec3 lDir = lightPosition - cLight.inHitPosition; 41 | cLight.outLightDistance = length(lDir); 42 | cLight.outIntensity = lightIntensity / (cLight.outLightDistance * cLight.outLightDistance); 43 | cLight.outLightDir = normalize(lDir); 44 | } 45 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/light_spot.rcall: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 core 21 | #extension GL_NV_ray_tracing : enable 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 3) callableDataInNV rayLight cLight; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | vec3 lightPosition; 31 | float lightIntensity; 32 | vec3 lightDirection; 33 | float lightSpotCutoff; 34 | float lightSpotOuterCutoff; 35 | int lightType; 36 | }; 37 | 38 | void main() 39 | { 40 | vec3 lDir = lightPosition - cLight.inHitPosition; 41 | cLight.outLightDistance = length(lDir); 42 | cLight.outIntensity = lightIntensity / (cLight.outLightDistance * cLight.outLightDistance); 43 | cLight.outLightDir = normalize(lDir); 44 | float theta = dot(cLight.outLightDir, normalize(-lightDirection)); 45 | float epsilon = lightSpotCutoff - lightSpotOuterCutoff; 46 | float spotIntensity = clamp((theta - lightSpotOuterCutoff) / epsilon, 0.0, 1.0); 47 | cLight.outIntensity *= spotIntensity; 48 | } 49 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/random.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // Generate a random unsigned int from two unsigned int values, using 16 pairs 21 | // of rounds of the Tiny Encryption Algorithm. See Zafar, Olano, and Curtis, 22 | // "GPU Random Numbers via the Tiny Encryption Algorithm" 23 | uint tea(uint val0, uint val1) 24 | { 25 | uint v0 = val0; 26 | uint v1 = val1; 27 | uint s0 = 0; 28 | 29 | for(uint n = 0; n < 16; n++) 30 | { 31 | s0 += 0x9e3779b9; 32 | v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4); 33 | v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e); 34 | } 35 | 36 | return v0; 37 | } 38 | 39 | // Generate a random unsigned int in [0, 2^24) given the previous RNG state 40 | // using the Numerical Recipes linear congruential generator 41 | uint lcg(inout uint prev) 42 | { 43 | uint LCG_A = 1664525u; 44 | uint LCG_C = 1013904223u; 45 | prev = (LCG_A * prev + LCG_C); 46 | return prev & 0x00FFFFFF; 47 | } 48 | 49 | // Generate a random float in [0, 1) given the previous RNG state 50 | float rnd(inout uint prev) 51 | { 52 | return (float(lcg(prev)) / float(0x01000000)); 53 | } 54 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | uint seed; 24 | int depth; 25 | vec3 attenuation; 26 | int done; 27 | vec3 rayOrigin; 28 | vec3 rayDir; 29 | }; 30 | 31 | 32 | struct rayLight 33 | { 34 | vec3 inHitPosition; 35 | float outLightDistance; 36 | vec3 outLightDir; 37 | float outIntensity; 38 | }; 39 | 40 | struct Implicit 41 | { 42 | vec3 minimum; 43 | vec3 maximum; 44 | int objType; 45 | int matId; 46 | }; 47 | 48 | struct Sphere 49 | { 50 | vec3 center; 51 | float radius; 52 | }; 53 | 54 | struct Aabb 55 | { 56 | vec3 minimum; 57 | vec3 maximum; 58 | }; 59 | 60 | #define KIND_SPHERE 0 61 | #define KIND_CUBE 1 62 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/raytrace.rahit: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_EXT_scalar_block_layout : enable 24 | #extension GL_GOOGLE_include_directive : enable 25 | 26 | #include "random.glsl" 27 | #include "raycommon.glsl" 28 | #include "wavefront.glsl" 29 | 30 | // clang-format off 31 | layout(location = 0) rayPayloadInNV hitPayload prd; 32 | 33 | layout(binding = 2, set = 1, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 34 | layout(binding = 4, set = 1) buffer MatIndexColorBuffer { int i[]; } matIndex[]; 35 | layout(binding = 1, set = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 36 | // clang-format on 37 | 38 | void main() 39 | { 40 | // Object of this instance 41 | uint objId = scnDesc.i[gl_InstanceID].objId; 42 | 43 | // Material of the object 44 | int matIdx = matIndex[nonuniformEXT(objId)].i[gl_PrimitiveID]; 45 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIdx]; 46 | 47 | if(mat.illum != 4) 48 | return; 49 | 50 | if(mat.dissolve == 0.0) 51 | ignoreIntersectionNV(); 52 | else if(rnd(prd.seed) > mat.dissolve) 53 | ignoreIntersectionNV(); 54 | } 55 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/raytrace2.rahit: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_EXT_scalar_block_layout : enable 24 | #extension GL_GOOGLE_include_directive : enable 25 | 26 | #include "random.glsl" 27 | #include "raycommon.glsl" 28 | #include "wavefront.glsl" 29 | 30 | // clang-format off 31 | layout(location = 0) rayPayloadInNV hitPayload prd; 32 | 33 | layout(binding = 1, set = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 34 | layout(binding = 7, set = 1, scalar) buffer allImplicits_ {Implicit i[];} allImplicits; 35 | // clang-format on 36 | 37 | void main() 38 | { 39 | // Material of the object 40 | Implicit impl = allImplicits.i[gl_PrimitiveID]; 41 | WaveFrontMaterial mat = materials[nonuniformEXT(gl_InstanceCustomIndexNV)].m[impl.matId]; 42 | 43 | if(mat.illum != 4) 44 | return; 45 | 46 | uint seed = prd.seed; 47 | 48 | if(mat.dissolve == 0.0) 49 | ignoreIntersectionNV(); 50 | else if(rnd(seed) > mat.dissolve) 51 | ignoreIntersectionNV(); 52 | } 53 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | float lightIntensity; 43 | vec3 lightDirection; 44 | float lightSpotCutoff; 45 | float lightSpotOuterCutoff; 46 | uint instanceId; 47 | int lightType; 48 | } 49 | pushC; 50 | 51 | layout(location = 0) in vec3 inPosition; 52 | layout(location = 1) in vec3 inNormal; 53 | layout(location = 2) in vec3 inColor; 54 | layout(location = 3) in vec2 inTexCoord; 55 | 56 | 57 | //layout(location = 0) flat out int matIndex; 58 | layout(location = 1) out vec2 fragTexCoord; 59 | layout(location = 2) out vec3 fragNormal; 60 | layout(location = 3) out vec3 viewDir; 61 | layout(location = 4) out vec3 worldPos; 62 | 63 | out gl_PerVertex 64 | { 65 | vec4 gl_Position; 66 | }; 67 | 68 | 69 | void main() 70 | { 71 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 72 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 73 | 74 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 75 | 76 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 77 | viewDir = vec3(worldPos - origin); 78 | fragTexCoord = inTexCoord; 79 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 80 | // matIndex = inMatID; 81 | 82 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 83 | } 84 | -------------------------------------------------------------------------------- /ray_tracing__advance/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing__advance/vkalloc.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2014-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | // Choosing the allocator to use 23 | #define ALLOC_DMA 24 | //#define ALLOC_DEDICATED 25 | //#define ALLOC_VMA 26 | #include 27 | 28 | #if defined(ALLOC_DMA) 29 | #include 30 | typedef nvvk::ResourceAllocatorDma Allocator; 31 | #elif defined(ALLOC_VMA) 32 | #include 33 | typedef nvvk::ResourceAllocatorVma Allocator; 34 | #else 35 | typedef nvvk::ResourceAllocatorDedicated Allocator; 36 | #endif 37 | -------------------------------------------------------------------------------- /ray_tracing__before/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | This example is a simple OBJ viewer in Vulkan, without any ray tracing functionality. 4 | It is the starting point of the ray tracing tutorial, the source of the application in which ray tracing will be added. 5 | 6 | ## [**Start Ray Tracing Tutorial**](https://nvpro-samples.github.io/vk_raytracing_tutorial/) 7 | 8 | ![resultRaytraceShadowMedieval](../docs/Images/resultRasterCube.png) 9 | -------------------------------------------------------------------------------- /ray_tracing__before/shaders/frag_shader.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #extension GL_EXT_scalar_block_layout : enable 25 | 26 | #include "wavefront.glsl" 27 | 28 | 29 | layout(push_constant) uniform shaderInformation 30 | { 31 | vec3 lightPosition; 32 | uint instanceId; 33 | float lightIntensity; 34 | int lightType; 35 | } 36 | pushC; 37 | 38 | // clang-format off 39 | // Incoming 40 | //layout(location = 0) flat in int matIndex; 41 | layout(location = 1) in vec2 fragTexCoord; 42 | layout(location = 2) in vec3 fragNormal; 43 | layout(location = 3) in vec3 viewDir; 44 | layout(location = 4) in vec3 worldPos; 45 | // Outgoing 46 | layout(location = 0) out vec4 outColor; 47 | // Buffers 48 | layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 49 | layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 50 | layout(binding = 3) uniform sampler2D[] textureSamplers; 51 | layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[]; 52 | 53 | // clang-format on 54 | 55 | 56 | void main() 57 | { 58 | // Object of this instance 59 | int objId = scnDesc.i[pushC.instanceId].objId; 60 | 61 | // Material of the object 62 | int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID]; 63 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex]; 64 | 65 | vec3 N = normalize(fragNormal); 66 | 67 | // Vector toward light 68 | vec3 L; 69 | float lightIntensity = pushC.lightIntensity; 70 | if(pushC.lightType == 0) 71 | { 72 | vec3 lDir = pushC.lightPosition - worldPos; 73 | float d = length(lDir); 74 | lightIntensity = pushC.lightIntensity / (d * d); 75 | L = normalize(lDir); 76 | } 77 | else 78 | { 79 | L = normalize(pushC.lightPosition - vec3(0)); 80 | } 81 | 82 | 83 | // Diffuse 84 | vec3 diffuse = computeDiffuse(mat, L, N); 85 | if(mat.textureId >= 0) 86 | { 87 | int txtOffset = scnDesc.i[pushC.instanceId].txtOffset; 88 | uint txtId = txtOffset + mat.textureId; 89 | vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz; 90 | diffuse *= diffuseTxt; 91 | } 92 | 93 | // Specular 94 | vec3 specular = computeSpecular(mat, viewDir, L, N); 95 | 96 | // Result 97 | outColor = vec4(lightIntensity * (diffuse + specular), 1); 98 | } 99 | -------------------------------------------------------------------------------- /ray_tracing__before/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing__before/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing__before/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing__before/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing__simple/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | This example is the result of the ray tracing tutorial. 4 | The tutorial is adding ray tracing capability to an OBJ rasterizer in Vulkan 5 | 6 | If you haven't done it, [**Start Ray Tracing Tutorial**](https://nvpro-samples.github.io/vk_raytracing_tutorial/). 7 | 8 | ![resultRaytraceShadowMedieval](../docs/Images/resultRaytraceShadowMedieval.png) 9 | 10 | ## Going Further 11 | 12 | Once the tutorial completed and the basics of ray tracing are in place, other tuturials are going further from this code base. 13 | 14 | See all other [additional ray tracing tutorials](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_further.md.html) 15 | -------------------------------------------------------------------------------- /ray_tracing__simple/files/shaders.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/ray_tracing__simple/files/shaders.zip -------------------------------------------------------------------------------- /ray_tracing__simple/files/shadowShaders.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_raytracing_tutorial_NV/002af7cef69b050958db66ca81eb7ec2c0bb6b8b/ray_tracing__simple/files/shadowShaders.zip -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/frag_shader.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #extension GL_EXT_scalar_block_layout : enable 25 | 26 | #include "wavefront.glsl" 27 | 28 | 29 | layout(push_constant) uniform shaderInformation 30 | { 31 | vec3 lightPosition; 32 | uint instanceId; 33 | float lightIntensity; 34 | int lightType; 35 | } 36 | pushC; 37 | 38 | // clang-format off 39 | // Incoming 40 | //layout(location = 0) flat in int matIndex; 41 | layout(location = 1) in vec2 fragTexCoord; 42 | layout(location = 2) in vec3 fragNormal; 43 | layout(location = 3) in vec3 viewDir; 44 | layout(location = 4) in vec3 worldPos; 45 | // Outgoing 46 | layout(location = 0) out vec4 outColor; 47 | // Buffers 48 | layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 49 | layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 50 | layout(binding = 3) uniform sampler2D[] textureSamplers; 51 | layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[]; 52 | 53 | // clang-format on 54 | 55 | 56 | void main() 57 | { 58 | // Object of this instance 59 | int objId = scnDesc.i[pushC.instanceId].objId; 60 | 61 | // Material of the object 62 | int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID]; 63 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex]; 64 | 65 | vec3 N = normalize(fragNormal); 66 | 67 | // Vector toward light 68 | vec3 L; 69 | float lightIntensity = pushC.lightIntensity; 70 | if(pushC.lightType == 0) 71 | { 72 | vec3 lDir = pushC.lightPosition - worldPos; 73 | float d = length(lDir); 74 | lightIntensity = pushC.lightIntensity / (d * d); 75 | L = normalize(lDir); 76 | } 77 | else 78 | { 79 | L = normalize(pushC.lightPosition - vec3(0)); 80 | } 81 | 82 | 83 | // Diffuse 84 | vec3 diffuse = computeDiffuse(mat, L, N); 85 | if(mat.textureId >= 0) 86 | { 87 | int txtOffset = scnDesc.i[pushC.instanceId].txtOffset; 88 | uint txtId = txtOffset + mat.textureId; 89 | vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz; 90 | diffuse *= diffuseTxt; 91 | } 92 | 93 | // Specular 94 | vec3 specular = computeSpecular(mat, viewDir, L, N); 95 | 96 | // Result 97 | outColor = vec4(lightIntensity * (diffuse + specular), 1); 98 | } 99 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/raytrace.rgen: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS; 26 | layout(binding = 1, set = 0, rgba32f) uniform image2D image; 27 | 28 | layout(location = 0) rayPayloadNV hitPayload prd; 29 | 30 | layout(binding = 0, set = 1) uniform CameraProperties 31 | { 32 | mat4 view; 33 | mat4 proj; 34 | mat4 viewInverse; 35 | mat4 projInverse; 36 | } 37 | cam; 38 | 39 | void main() 40 | { 41 | const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5); 42 | const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeNV.xy); 43 | vec2 d = inUV * 2.0 - 1.0; 44 | 45 | vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1); 46 | vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1); 47 | vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0); 48 | 49 | uint rayFlags = gl_RayFlagsOpaqueNV; 50 | float tMin = 0.001; 51 | float tMax = 10000.0; 52 | 53 | traceNV(topLevelAS, // acceleration structure 54 | rayFlags, // rayFlags 55 | 0xFF, // cullMask 56 | 0, // sbtRecordOffset 57 | 0, // sbtRecordStride 58 | 0, // missIndex 59 | origin.xyz, // ray origin 60 | tMin, // ray min range 61 | direction.xyz, // ray direction 62 | tMax, // ray max range 63 | 0 // payload (location = 0) 64 | ); 65 | 66 | imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(prd.hitValue, 1.0)); 67 | } 68 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing__simple/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_animation/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_animation.md.html) 4 | 5 | ![](../docs/Images/animation2.gif) -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/anim.comp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #include "wavefront.glsl" 25 | 26 | layout(binding = 0, scalar) buffer Vertices 27 | { 28 | Vertex v[]; 29 | } 30 | vertices; 31 | 32 | layout(push_constant) uniform shaderInformation 33 | { 34 | float iTime; 35 | } 36 | pushc; 37 | 38 | void main() 39 | { 40 | Vertex v0 = vertices.v[gl_GlobalInvocationID.x]; 41 | 42 | // Compute vertex position 43 | const float PI = 3.14159265; 44 | const float signY = (v0.pos.y >= 0 ? 1 : -1); 45 | const float radius = length(v0.pos.xz); 46 | const float argument = pushc.iTime * 4 + radius * PI; 47 | const float s = sin(argument); 48 | v0.pos.y = signY * abs(s) * 0.5; 49 | 50 | // Compute normal 51 | if(radius == 0.0f) 52 | { 53 | v0.nrm = vec3(0.0f, signY, 0.0f); 54 | } 55 | else 56 | { 57 | const float c = cos(argument); 58 | const float xzFactor = -PI * s * c; 59 | const float yFactor = 2.0f * signY * radius * abs(s); 60 | v0.nrm = normalize(vec3(v0.pos.x * xzFactor, yFactor, v0.pos.z * xzFactor)); 61 | } 62 | 63 | vertices.v[gl_GlobalInvocationID.x] = v0; 64 | } -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/frag_shader.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #extension GL_EXT_scalar_block_layout : enable 25 | 26 | #include "wavefront.glsl" 27 | 28 | 29 | layout(push_constant) uniform shaderInformation 30 | { 31 | vec3 lightPosition; 32 | uint instanceId; 33 | float lightIntensity; 34 | int lightType; 35 | } 36 | pushC; 37 | 38 | // clang-format off 39 | // Incoming 40 | //layout(location = 0) flat in int matIndex; 41 | layout(location = 1) in vec2 fragTexCoord; 42 | layout(location = 2) in vec3 fragNormal; 43 | layout(location = 3) in vec3 viewDir; 44 | layout(location = 4) in vec3 worldPos; 45 | // Outgoing 46 | layout(location = 0) out vec4 outColor; 47 | // Buffers 48 | layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 49 | layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 50 | layout(binding = 3) uniform sampler2D[] textureSamplers; 51 | layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[]; 52 | 53 | // clang-format on 54 | 55 | 56 | void main() 57 | { 58 | // Object of this instance 59 | int objId = scnDesc.i[pushC.instanceId].objId; 60 | 61 | // Material of the object 62 | int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID]; 63 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex]; 64 | 65 | vec3 N = normalize(fragNormal); 66 | 67 | // Vector toward light 68 | vec3 L; 69 | float lightIntensity = pushC.lightIntensity; 70 | if(pushC.lightType == 0) 71 | { 72 | vec3 lDir = pushC.lightPosition - worldPos; 73 | float d = length(lDir); 74 | lightIntensity = pushC.lightIntensity / (d * d); 75 | L = normalize(lDir); 76 | } 77 | else 78 | { 79 | L = normalize(pushC.lightPosition - vec3(0)); 80 | } 81 | 82 | 83 | // Diffuse 84 | vec3 diffuse = computeDiffuse(mat, L, N); 85 | if(mat.textureId >= 0) 86 | { 87 | int txtOffset = scnDesc.i[pushC.instanceId].txtOffset; 88 | uint txtId = txtOffset + mat.textureId; 89 | vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz; 90 | diffuse *= diffuseTxt; 91 | } 92 | 93 | // Specular 94 | vec3 specular = computeSpecular(mat, viewDir, L, N); 95 | 96 | // Result 97 | outColor = vec4(lightIntensity * (diffuse + specular), 1); 98 | } 99 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/raytrace.rgen: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS; 26 | layout(binding = 1, set = 0, rgba32f) uniform image2D image; 27 | 28 | layout(location = 0) rayPayloadNV hitPayload prd; 29 | 30 | layout(binding = 0, set = 1) uniform CameraProperties 31 | { 32 | mat4 view; 33 | mat4 proj; 34 | mat4 viewInverse; 35 | mat4 projInverse; 36 | } 37 | cam; 38 | 39 | void main() 40 | { 41 | const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5); 42 | const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeNV.xy); 43 | vec2 d = inUV * 2.0 - 1.0; 44 | 45 | vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1); 46 | vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1); 47 | vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0); 48 | 49 | uint rayFlags = gl_RayFlagsOpaqueNV; 50 | float tMin = 0.001; 51 | float tMax = 10000.0; 52 | 53 | traceNV(topLevelAS, // acceleration structure 54 | rayFlags, // rayFlags 55 | 0xFF, // cullMask 56 | 0, // sbtRecordOffset 57 | 0, // sbtRecordStride 58 | 0, // missIndex 59 | origin.xyz, // ray origin 60 | tMin, // ray min range 61 | direction.xyz, // ray direction 62 | tMax, // ray max range 63 | 0 // payload (location = 0) 64 | ); 65 | 66 | imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(prd.hitValue, 1.0)); 67 | } 68 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_animation/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_anyhit.md.html) 4 | 5 | ![](../docs/Images/anyhit.png) -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/frag_shader.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #extension GL_EXT_scalar_block_layout : enable 25 | 26 | #include "wavefront.glsl" 27 | 28 | 29 | layout(push_constant) uniform shaderInformation 30 | { 31 | vec3 lightPosition; 32 | uint instanceId; 33 | float lightIntensity; 34 | int lightType; 35 | } 36 | pushC; 37 | 38 | // clang-format off 39 | // Incoming 40 | //layout(location = 0) flat in int matIndex; 41 | layout(location = 1) in vec2 fragTexCoord; 42 | layout(location = 2) in vec3 fragNormal; 43 | layout(location = 3) in vec3 viewDir; 44 | layout(location = 4) in vec3 worldPos; 45 | // Outgoing 46 | layout(location = 0) out vec4 outColor; 47 | // Buffers 48 | layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 49 | layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 50 | layout(binding = 3) uniform sampler2D[] textureSamplers; 51 | layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[]; 52 | 53 | // clang-format on 54 | 55 | 56 | void main() 57 | { 58 | // Object of this instance 59 | int objId = scnDesc.i[pushC.instanceId].objId; 60 | 61 | // Material of the object 62 | int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID]; 63 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex]; 64 | 65 | vec3 N = normalize(fragNormal); 66 | 67 | // Vector toward light 68 | vec3 L; 69 | float lightIntensity = pushC.lightIntensity; 70 | if(pushC.lightType == 0) 71 | { 72 | vec3 lDir = pushC.lightPosition - worldPos; 73 | float d = length(lDir); 74 | lightIntensity = pushC.lightIntensity / (d * d); 75 | L = normalize(lDir); 76 | } 77 | else 78 | { 79 | L = normalize(pushC.lightPosition - vec3(0)); 80 | } 81 | 82 | 83 | // Diffuse 84 | vec3 diffuse = computeDiffuse(mat, L, N); 85 | if(mat.textureId >= 0) 86 | { 87 | int txtOffset = scnDesc.i[pushC.instanceId].txtOffset; 88 | uint txtId = txtOffset + mat.textureId; 89 | vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz; 90 | diffuse *= diffuseTxt; 91 | } 92 | 93 | // Specular 94 | vec3 specular = computeSpecular(mat, viewDir, L, N); 95 | 96 | // Result 97 | outColor = vec4(lightIntensity * (diffuse + specular), 1); 98 | } 99 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/random.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // Generate a random unsigned int from two unsigned int values, using 16 pairs 21 | // of rounds of the Tiny Encryption Algorithm. See Zafar, Olano, and Curtis, 22 | // "GPU Random Numbers via the Tiny Encryption Algorithm" 23 | uint tea(uint val0, uint val1) 24 | { 25 | uint v0 = val0; 26 | uint v1 = val1; 27 | uint s0 = 0; 28 | 29 | for(uint n = 0; n < 16; n++) 30 | { 31 | s0 += 0x9e3779b9; 32 | v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4); 33 | v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e); 34 | } 35 | 36 | return v0; 37 | } 38 | 39 | // Generate a random unsigned int in [0, 2^24) given the previous RNG state 40 | // using the Numerical Recipes linear congruential generator 41 | uint lcg(inout uint prev) 42 | { 43 | uint LCG_A = 1664525u; 44 | uint LCG_C = 1013904223u; 45 | prev = (LCG_A * prev + LCG_C); 46 | return prev & 0x00FFFFFF; 47 | } 48 | 49 | // Generate a random float in [0, 1) given the previous RNG state 50 | float rnd(inout uint prev) 51 | { 52 | return (float(lcg(prev)) / float(0x01000000)); 53 | } 54 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | uint seed; 24 | }; 25 | 26 | struct shadowPayload 27 | { 28 | bool isHit; 29 | uint seed; 30 | }; 31 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | 24 | #include "raycommon.glsl" 25 | 26 | layout(location = 1) rayPayloadInNV shadowPayload prd; 27 | 28 | void main() 29 | { 30 | prd.isHit = false; 31 | } 32 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/raytrace_0.rahit: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | 23 | #define PAYLOAD_0 24 | #include "raytrace_rahit.glsl" 25 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/raytrace_1.rahit: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | 23 | #define PAYLOAD_1 24 | #include "raytrace_rahit.glsl" 25 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/raytrace_rahit.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | //#version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_EXT_scalar_block_layout : enable 24 | //#extension GL_GOOGLE_include_directive : enable 25 | 26 | #include "random.glsl" 27 | #include "raycommon.glsl" 28 | #include "wavefront.glsl" 29 | 30 | // clang-format off 31 | #ifdef PAYLOAD_0 32 | layout(location = 0) rayPayloadInNV hitPayload prd; 33 | #elif defined(PAYLOAD_1) 34 | layout(location = 1) rayPayloadInNV shadowPayload prd; 35 | #endif 36 | 37 | layout(binding = 2, set = 1, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 38 | layout(binding = 4, set = 1) buffer MatIndexColorBuffer { int i[]; } matIndex[]; 39 | layout(binding = 5, set = 1, scalar) buffer Vertices { Vertex v[]; } vertices[]; 40 | layout(binding = 6, set = 1) buffer Indices { uint i[]; } indices[]; 41 | layout(binding = 1, set = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 42 | // clang-format on 43 | 44 | void main() 45 | { 46 | // Object of this instance 47 | uint objId = scnDesc.i[gl_InstanceID].objId; 48 | // Indices of the triangle 49 | uint ind = indices[nonuniformEXT(objId)].i[3 * gl_PrimitiveID + 0]; 50 | // Vertex of the triangle 51 | Vertex v0 = vertices[nonuniformEXT(objId)].v[ind.x]; 52 | 53 | // Material of the object 54 | int matIdx = matIndex[nonuniformEXT(objId)].i[gl_PrimitiveID]; 55 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIdx]; 56 | 57 | if(mat.illum != 4) 58 | return; 59 | 60 | if(mat.dissolve == 0.0) 61 | ignoreIntersectionNV(); 62 | else if(rnd(prd.seed) > mat.dissolve) 63 | ignoreIntersectionNV(); 64 | } 65 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_anyhit/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_callable/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_callable.md.html) 4 | 5 | ![](../docs/Images/callable.png) 6 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/light_inf.rcall: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 core 21 | #extension GL_NV_ray_tracing : enable 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) callableDataInNV rayLight cLight; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | vec3 lightPosition; 31 | float lightIntensity; 32 | vec3 lightDirection; 33 | float lightSpotCutoff; 34 | float lightSpotOuterCutoff; 35 | int lightType; 36 | }; 37 | 38 | void main() 39 | { 40 | cLight.outLightDistance = 10000000; 41 | cLight.outIntensity = 1.0; 42 | cLight.outLightDir = normalize(-lightDirection); 43 | } 44 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/light_point.rcall: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 core 21 | #extension GL_NV_ray_tracing : enable 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) callableDataInNV rayLight cLight; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | vec3 lightPosition; 31 | float lightIntensity; 32 | vec3 lightDirection; 33 | float lightSpotCutoff; 34 | float lightSpotOuterCutoff; 35 | int lightType; 36 | }; 37 | 38 | void main() 39 | { 40 | vec3 lDir = lightPosition - cLight.inHitPosition; 41 | cLight.outLightDistance = length(lDir); 42 | cLight.outIntensity = lightIntensity / (cLight.outLightDistance * cLight.outLightDistance); 43 | cLight.outLightDir = normalize(lDir); 44 | } 45 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/light_spot.rcall: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 core 21 | #extension GL_NV_ray_tracing : enable 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) callableDataInNV rayLight cLight; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | vec3 lightPosition; 31 | float lightIntensity; 32 | vec3 lightDirection; 33 | float lightSpotCutoff; 34 | float lightSpotOuterCutoff; 35 | int lightType; 36 | }; 37 | 38 | void main() 39 | { 40 | vec3 lDir = lightPosition - cLight.inHitPosition; 41 | cLight.outLightDistance = length(lDir); 42 | cLight.outIntensity = lightIntensity / (cLight.outLightDistance * cLight.outLightDistance); 43 | cLight.outLightDir = normalize(lDir); 44 | float theta = dot(cLight.outLightDir, normalize(-lightDirection)); 45 | float epsilon = lightSpotCutoff - lightSpotOuterCutoff; 46 | float spotIntensity = clamp((theta - lightSpotOuterCutoff) / epsilon, 0.0, 1.0); 47 | cLight.outIntensity *= spotIntensity; 48 | } 49 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | 25 | struct rayLight 26 | { 27 | vec3 inHitPosition; 28 | float outLightDistance; 29 | vec3 outLightDir; 30 | float outIntensity; 31 | }; 32 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/raytrace.rgen: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS; 26 | layout(binding = 1, set = 0, rgba32f) uniform image2D image; 27 | 28 | layout(location = 0) rayPayloadNV hitPayload prd; 29 | 30 | layout(binding = 0, set = 1) uniform CameraProperties 31 | { 32 | mat4 view; 33 | mat4 proj; 34 | mat4 viewInverse; 35 | mat4 projInverse; 36 | } 37 | cam; 38 | 39 | void main() 40 | { 41 | const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5); 42 | const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeNV.xy); 43 | vec2 d = inUV * 2.0 - 1.0; 44 | 45 | vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1); 46 | vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1); 47 | vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0); 48 | 49 | uint rayFlags = gl_RayFlagsOpaqueNV; 50 | float tMin = 0.001; 51 | float tMax = 10000.0; 52 | 53 | traceNV(topLevelAS, // acceleration structure 54 | rayFlags, // rayFlags 55 | 0xFF, // cullMask 56 | 0, // sbtRecordOffset 57 | 0, // sbtRecordStride 58 | 0, // missIndex 59 | origin.xyz, // ray origin 60 | tMin, // ray min range 61 | direction.xyz, // ray direction 62 | tMax, // ray max range 63 | 0 // payload (location = 0) 64 | ); 65 | 66 | imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(prd.hitValue, 1.0)); 67 | } 68 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | float lightIntensity; 43 | vec3 lightDirection; 44 | float lightSpotCutoff; 45 | float lightSpotOuterCutoff; 46 | uint instanceId; 47 | int lightType; 48 | } 49 | pushC; 50 | 51 | layout(location = 0) in vec3 inPosition; 52 | layout(location = 1) in vec3 inNormal; 53 | layout(location = 2) in vec3 inColor; 54 | layout(location = 3) in vec2 inTexCoord; 55 | 56 | 57 | //layout(location = 0) flat out int matIndex; 58 | layout(location = 1) out vec2 fragTexCoord; 59 | layout(location = 2) out vec3 fragNormal; 60 | layout(location = 3) out vec3 viewDir; 61 | layout(location = 4) out vec3 worldPos; 62 | 63 | out gl_PerVertex 64 | { 65 | vec4 gl_Position; 66 | }; 67 | 68 | 69 | void main() 70 | { 71 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 72 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 73 | 74 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 75 | 76 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 77 | viewDir = vec3(worldPos - origin); 78 | fragTexCoord = inTexCoord; 79 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 80 | // matIndex = inMatID; 81 | 82 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 83 | } 84 | -------------------------------------------------------------------------------- /ray_tracing_callable/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_instances/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_instances.md.html) 4 | 5 | ![](../docs/Images/VkInstances.png) -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/frag_shader.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #extension GL_EXT_scalar_block_layout : enable 25 | 26 | #include "wavefront.glsl" 27 | 28 | 29 | layout(push_constant) uniform shaderInformation 30 | { 31 | vec3 lightPosition; 32 | uint instanceId; 33 | float lightIntensity; 34 | int lightType; 35 | } 36 | pushC; 37 | 38 | // clang-format off 39 | // Incoming 40 | //layout(location = 0) flat in int matIndex; 41 | layout(location = 1) in vec2 fragTexCoord; 42 | layout(location = 2) in vec3 fragNormal; 43 | layout(location = 3) in vec3 viewDir; 44 | layout(location = 4) in vec3 worldPos; 45 | // Outgoing 46 | layout(location = 0) out vec4 outColor; 47 | // Buffers 48 | layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 49 | layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 50 | layout(binding = 3) uniform sampler2D[] textureSamplers; 51 | layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[]; 52 | 53 | // clang-format on 54 | 55 | 56 | void main() 57 | { 58 | // Object of this instance 59 | int objId = scnDesc.i[pushC.instanceId].objId; 60 | 61 | // Material of the object 62 | int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID]; 63 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex]; 64 | 65 | vec3 N = normalize(fragNormal); 66 | 67 | // Vector toward light 68 | vec3 L; 69 | float lightIntensity = pushC.lightIntensity; 70 | if(pushC.lightType == 0) 71 | { 72 | vec3 lDir = pushC.lightPosition - worldPos; 73 | float d = length(lDir); 74 | lightIntensity = pushC.lightIntensity / (d * d); 75 | L = normalize(lDir); 76 | } 77 | else 78 | { 79 | L = normalize(pushC.lightPosition - vec3(0)); 80 | } 81 | 82 | 83 | // Diffuse 84 | vec3 diffuse = computeDiffuse(mat, L, N); 85 | if(mat.textureId >= 0) 86 | { 87 | int txtOffset = scnDesc.i[pushC.instanceId].txtOffset; 88 | uint txtId = txtOffset + mat.textureId; 89 | vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz; 90 | diffuse *= diffuseTxt; 91 | } 92 | 93 | // Specular 94 | vec3 specular = computeSpecular(mat, viewDir, L, N); 95 | 96 | // Result 97 | outColor = vec4(lightIntensity * (diffuse + specular), 1); 98 | } 99 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/raytrace.rgen: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS; 26 | layout(binding = 1, set = 0, rgba32f) uniform image2D image; 27 | 28 | layout(location = 0) rayPayloadNV hitPayload prd; 29 | 30 | layout(binding = 0, set = 1) uniform CameraProperties 31 | { 32 | mat4 view; 33 | mat4 proj; 34 | mat4 viewInverse; 35 | mat4 projInverse; 36 | } 37 | cam; 38 | 39 | void main() 40 | { 41 | const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5); 42 | const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeNV.xy); 43 | vec2 d = inUV * 2.0 - 1.0; 44 | 45 | vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1); 46 | vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1); 47 | vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0); 48 | 49 | uint rayFlags = gl_RayFlagsOpaqueNV; 50 | float tMin = 0.001; 51 | float tMax = 10000.0; 52 | 53 | traceNV(topLevelAS, // acceleration structure 54 | rayFlags, // rayFlags 55 | 0xFF, // cullMask 56 | 0, // sbtRecordOffset 57 | 0, // sbtRecordStride 58 | 0, // missIndex 59 | origin.xyz, // ray origin 60 | tMin, // ray min range 61 | direction.xyz, // ray direction 62 | tMax, // ray max range 63 | 0 // payload (location = 0) 64 | ); 65 | 66 | imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(prd.hitValue, 1.0)); 67 | } 68 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_instances/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_intersection/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_intersection.md.html) 4 | 5 | ![](../docs/Images/ray_tracing_intersection.png) -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/frag_shader.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_nonuniform_qualifier : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | #extension GL_EXT_scalar_block_layout : enable 25 | 26 | #include "wavefront.glsl" 27 | 28 | 29 | layout(push_constant) uniform shaderInformation 30 | { 31 | vec3 lightPosition; 32 | uint instanceId; 33 | float lightIntensity; 34 | int lightType; 35 | } 36 | pushC; 37 | 38 | // clang-format off 39 | // Incoming 40 | //layout(location = 0) flat in int matIndex; 41 | layout(location = 1) in vec2 fragTexCoord; 42 | layout(location = 2) in vec3 fragNormal; 43 | layout(location = 3) in vec3 viewDir; 44 | layout(location = 4) in vec3 worldPos; 45 | // Outgoing 46 | layout(location = 0) out vec4 outColor; 47 | // Buffers 48 | layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[]; 49 | layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 50 | layout(binding = 3) uniform sampler2D[] textureSamplers; 51 | layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[]; 52 | 53 | // clang-format on 54 | 55 | 56 | void main() 57 | { 58 | // Object of this instance 59 | int objId = scnDesc.i[pushC.instanceId].objId; 60 | 61 | // Material of the object 62 | int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID]; 63 | WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex]; 64 | 65 | vec3 N = normalize(fragNormal); 66 | 67 | // Vector toward light 68 | vec3 L; 69 | float lightIntensity = pushC.lightIntensity; 70 | if(pushC.lightType == 0) 71 | { 72 | vec3 lDir = pushC.lightPosition - worldPos; 73 | float d = length(lDir); 74 | lightIntensity = pushC.lightIntensity / (d * d); 75 | L = normalize(lDir); 76 | } 77 | else 78 | { 79 | L = normalize(pushC.lightPosition - vec3(0)); 80 | } 81 | 82 | 83 | // Diffuse 84 | vec3 diffuse = computeDiffuse(mat, L, N); 85 | if(mat.textureId >= 0) 86 | { 87 | int txtOffset = scnDesc.i[pushC.instanceId].txtOffset; 88 | uint txtId = txtOffset + mat.textureId; 89 | vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz; 90 | diffuse *= diffuseTxt; 91 | } 92 | 93 | // Specular 94 | vec3 specular = computeSpecular(mat, viewDir, L, N); 95 | 96 | // Result 97 | outColor = vec4(lightIntensity * (diffuse + specular), 1); 98 | } 99 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | 25 | struct Sphere 26 | { 27 | vec3 center; 28 | float radius; 29 | }; 30 | 31 | struct Aabb 32 | { 33 | vec3 minimum; 34 | vec3 maximum; 35 | }; 36 | 37 | #define KIND_SPHERE 0 38 | #define KIND_CUBE 1 39 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/raytrace.rgen: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS; 26 | layout(binding = 1, set = 0, rgba32f) uniform image2D image; 27 | 28 | layout(location = 0) rayPayloadNV hitPayload prd; 29 | 30 | layout(binding = 0, set = 1) uniform CameraProperties 31 | { 32 | mat4 view; 33 | mat4 proj; 34 | mat4 viewInverse; 35 | mat4 projInverse; 36 | } 37 | cam; 38 | 39 | void main() 40 | { 41 | const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5); 42 | const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeNV.xy); 43 | vec2 d = inUV * 2.0 - 1.0; 44 | 45 | vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1); 46 | vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1); 47 | vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0); 48 | 49 | uint rayFlags = gl_RayFlagsOpaqueNV; 50 | float tMin = 0.001; 51 | float tMax = 10000.0; 52 | 53 | traceNV(topLevelAS, // acceleration structure 54 | rayFlags, // rayFlags 55 | 0xFF, // cullMask 56 | 0, // sbtRecordOffset 57 | 0, // sbtRecordStride 58 | 0, // missIndex 59 | origin.xyz, // ray origin 60 | tMin, // ray min range 61 | direction.xyz, // ray direction 62 | tMax, // ray max range 63 | 0 // payload (location = 0) 64 | ); 65 | 66 | imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(prd.hitValue, 1.0)); 67 | } 68 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_intersection/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_jitter_cam.md.html) 4 | 5 | ![](../docs/Images/antialiasing.png) -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/random.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // Generate a random unsigned int from two unsigned int values, using 16 pairs 21 | // of rounds of the Tiny Encryption Algorithm. See Zafar, Olano, and Curtis, 22 | // "GPU Random Numbers via the Tiny Encryption Algorithm" 23 | uint tea(uint val0, uint val1) 24 | { 25 | uint v0 = val0; 26 | uint v1 = val1; 27 | uint s0 = 0; 28 | 29 | for(uint n = 0; n < 16; n++) 30 | { 31 | s0 += 0x9e3779b9; 32 | v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4); 33 | v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e); 34 | } 35 | 36 | return v0; 37 | } 38 | 39 | // Generate a random unsigned int in [0, 2^24) given the previous RNG state 40 | // using the Numerical Recipes linear congruential generator 41 | uint lcg(inout uint prev) 42 | { 43 | uint LCG_A = 1664525u; 44 | uint LCG_C = 1013904223u; 45 | prev = (LCG_A * prev + LCG_C); 46 | return prev & 0x00FFFFFF; 47 | } 48 | 49 | // Generate a random float in [0, 1) given the previous RNG state 50 | float rnd(inout uint prev) 51 | { 52 | return (float(lcg(prev)) / float(0x01000000)); 53 | } 54 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_jitter_cam/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_manyhits.md.html) 4 | 5 | ![](../docs/Images/manyhits.png) -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | }; 24 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/raytrace.rgen: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS; 26 | layout(binding = 1, set = 0, rgba32f) uniform image2D image; 27 | 28 | layout(location = 0) rayPayloadNV hitPayload prd; 29 | 30 | layout(binding = 0, set = 1) uniform CameraProperties 31 | { 32 | mat4 view; 33 | mat4 proj; 34 | mat4 viewInverse; 35 | mat4 projInverse; 36 | } 37 | cam; 38 | 39 | void main() 40 | { 41 | const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5); 42 | const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeNV.xy); 43 | vec2 d = inUV * 2.0 - 1.0; 44 | 45 | vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1); 46 | vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1); 47 | vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0); 48 | 49 | uint rayFlags = gl_RayFlagsOpaqueNV; 50 | float tMin = 0.001; 51 | float tMax = 10000.0; 52 | 53 | traceNV(topLevelAS, // acceleration structure 54 | rayFlags, // rayFlags 55 | 0xFF, // cullMask 56 | 0, // sbtRecordOffset 57 | 0, // sbtRecordStride 58 | 0, // missIndex 59 | origin.xyz, // ray origin 60 | tMin, // ray min range 61 | direction.xyz, // ray direction 62 | tMax, // ray max range 63 | 0 // payload (location = 0) 64 | ); 65 | 66 | imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(prd.hitValue, 1.0)); 67 | } 68 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/raytrace2.rchit: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | 24 | #include "raycommon.glsl" 25 | 26 | layout(location = 0) rayPayloadInNV hitPayload prd; 27 | layout(shaderRecordNV) buffer sr_ 28 | { 29 | vec4 c; 30 | } 31 | shaderRec; 32 | 33 | void main() 34 | { 35 | prd.hitValue = shaderRec.c.rgb; 36 | } 37 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_manyhits/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | int hitGroup; 49 | }; 50 | 51 | 52 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 53 | { 54 | // Lambertian 55 | float dotNL = max(dot(normal, lightDir), 0.0); 56 | vec3 c = mat.diffuse * dotNL; 57 | if(mat.illum >= 1) 58 | c += mat.ambient; 59 | return c; 60 | } 61 | 62 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 63 | { 64 | if(mat.illum < 2) 65 | return vec3(0); 66 | 67 | // Compute specular only if not in shadow 68 | const float kPi = 3.14159265; 69 | const float kShininess = max(mat.shininess, 4.0); 70 | 71 | // Specular 72 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 73 | vec3 V = normalize(-viewDir); 74 | vec3 R = reflect(-lightDir, normal); 75 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 76 | 77 | return vec3(mat.specular * specular); 78 | } 79 | -------------------------------------------------------------------------------- /ray_tracing_reflections/README.md: -------------------------------------------------------------------------------- 1 | # NVIDIA Vulkan Ray Tracing Tutorial 2 | 3 | [Start the tutorial of this project](https://nvpro-samples.github.io/vk_raytracing_tutorial/vkrt_tuto_reflection.md.html) 4 | 5 | ![](../docs/Images/reflections.png) -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/passthrough.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout (location = 0) out vec2 outUV; 22 | 23 | 24 | out gl_PerVertex 25 | { 26 | vec4 gl_Position; 27 | }; 28 | 29 | 30 | void main() 31 | { 32 | outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); 33 | gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f); 34 | } 35 | -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/post.frag: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | layout(location = 0) in vec2 outUV; 22 | layout(location = 0) out vec4 fragColor; 23 | 24 | layout(set = 0, binding = 0) uniform sampler2D noisyTxt; 25 | 26 | layout(push_constant) uniform shaderInformation 27 | { 28 | float aspectRatio; 29 | } 30 | pushc; 31 | 32 | void main() 33 | { 34 | vec2 uv = outUV; 35 | float gamma = 1. / 2.2; 36 | fragColor = pow(texture(noisyTxt, uv).rgba, vec4(gamma)); 37 | } 38 | -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/raycommon.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct hitPayload 21 | { 22 | vec3 hitValue; 23 | int depth; 24 | vec3 attenuation; 25 | int done; 26 | vec3 rayOrigin; 27 | vec3 rayDir; 28 | }; 29 | -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/raytrace.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | #extension GL_GOOGLE_include_directive : enable 23 | #include "raycommon.glsl" 24 | 25 | layout(location = 0) rayPayloadInNV hitPayload prd; 26 | 27 | layout(push_constant) uniform Constants 28 | { 29 | vec4 clearColor; 30 | }; 31 | 32 | void main() 33 | { 34 | prd.hitValue = clearColor.xyz * 0.8; 35 | } 36 | -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/raytraceShadow.rmiss: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_NV_ray_tracing : require 22 | 23 | layout(location = 1) rayPayloadInNV bool isShadowed; 24 | 25 | void main() 26 | { 27 | isShadowed = false; 28 | } 29 | -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/vert_shader.vert: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 450 21 | #extension GL_ARB_separate_shader_objects : enable 22 | #extension GL_EXT_scalar_block_layout : enable 23 | #extension GL_GOOGLE_include_directive : enable 24 | 25 | #include "wavefront.glsl" 26 | 27 | // clang-format off 28 | layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc; 29 | // clang-format on 30 | 31 | layout(binding = 0) uniform UniformBufferObject 32 | { 33 | mat4 view; 34 | mat4 proj; 35 | mat4 viewI; 36 | } 37 | ubo; 38 | 39 | layout(push_constant) uniform shaderInformation 40 | { 41 | vec3 lightPosition; 42 | uint instanceId; 43 | float lightIntensity; 44 | int lightType; 45 | } 46 | pushC; 47 | 48 | layout(location = 0) in vec3 inPosition; 49 | layout(location = 1) in vec3 inNormal; 50 | layout(location = 2) in vec3 inColor; 51 | layout(location = 3) in vec2 inTexCoord; 52 | 53 | 54 | //layout(location = 0) flat out int matIndex; 55 | layout(location = 1) out vec2 fragTexCoord; 56 | layout(location = 2) out vec3 fragNormal; 57 | layout(location = 3) out vec3 viewDir; 58 | layout(location = 4) out vec3 worldPos; 59 | 60 | out gl_PerVertex 61 | { 62 | vec4 gl_Position; 63 | }; 64 | 65 | 66 | void main() 67 | { 68 | mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo; 69 | mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT; 70 | 71 | vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1)); 72 | 73 | worldPos = vec3(objMatrix * vec4(inPosition, 1.0)); 74 | viewDir = vec3(worldPos - origin); 75 | fragTexCoord = inTexCoord; 76 | fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0)); 77 | // matIndex = inMatID; 78 | 79 | gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0); 80 | } 81 | -------------------------------------------------------------------------------- /ray_tracing_reflections/shaders/wavefront.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | struct Vertex 21 | { 22 | vec3 pos; 23 | vec3 nrm; 24 | vec3 color; 25 | vec2 texCoord; 26 | }; 27 | 28 | struct WaveFrontMaterial 29 | { 30 | vec3 ambient; 31 | vec3 diffuse; 32 | vec3 specular; 33 | vec3 transmittance; 34 | vec3 emission; 35 | float shininess; 36 | float ior; // index of refraction 37 | float dissolve; // 1 == opaque; 0 == fully transparent 38 | int illum; // illumination model (see http://www.fileformat.info/format/material/) 39 | int textureId; 40 | }; 41 | 42 | struct sceneDesc 43 | { 44 | int objId; 45 | int txtOffset; 46 | mat4 transfo; 47 | mat4 transfoIT; 48 | }; 49 | 50 | 51 | vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) 52 | { 53 | // Lambertian 54 | float dotNL = max(dot(normal, lightDir), 0.0); 55 | vec3 c = mat.diffuse * dotNL; 56 | if(mat.illum >= 1) 57 | c += mat.ambient; 58 | return c; 59 | } 60 | 61 | vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) 62 | { 63 | if(mat.illum < 2) 64 | return vec3(0); 65 | 66 | // Compute specular only if not in shadow 67 | const float kPi = 3.14159265; 68 | const float kShininess = max(mat.shininess, 4.0); 69 | 70 | // Specular 71 | const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi); 72 | vec3 V = normalize(-viewDir); 73 | vec3 R = reflect(-lightDir, normal); 74 | float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess); 75 | 76 | return vec3(mat.specular * specular); 77 | } 78 | --------------------------------------------------------------------------------