Added samples for ray queries and callable ray tracing shaders
This commit is contained in:
parent
08be260685
commit
f79c9705b4
22 changed files with 1391 additions and 33 deletions
70
README.md
70
README.md
|
|
@ -5,7 +5,7 @@ A comprehensive collection of open source C++ examples for [Vulkan®](https://ww
|
|||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BHXPMV6ZKPH9E)
|
||||
|
||||
## Table of Contents
|
||||
+ [Official Khronos Vulkan Samples](#Khronossamples)
|
||||
+ [Official Khronos Vulkan Samples](#official-khronos-vulkan-samples)
|
||||
+ [Cloning](#Cloning)
|
||||
+ [Assets](#Assets)
|
||||
+ [Building](#Building)
|
||||
|
|
@ -15,20 +15,20 @@ A comprehensive collection of open source C++ examples for [Vulkan®](https://ww
|
|||
+ [glTF](#glTF)
|
||||
+ [Advanced](#Advanced)
|
||||
+ [Performance](#Performance)
|
||||
+ [Physically Based Rendering](#PBR)
|
||||
+ [Physically Based Rendering](#physically-based-rendering)
|
||||
+ [Deferred](#Deferred)
|
||||
+ [Compute Shader](#ComputeShader)
|
||||
+ [Geometry Shader](#GeometryShader)
|
||||
+ [Tessellation Shader](#TessellationShader)
|
||||
+ [Ray tracing](#Raytracing)
|
||||
+ [Compute Shader](#compute-shader)
|
||||
+ [Geometry Shader](#geometry-shader)
|
||||
+ [Tessellation Shader](#tessellation-shader)
|
||||
+ [Hardware accelerated ray tracing](#hardware-accelerated-ray-tracing)
|
||||
+ [Headless](#Headless)
|
||||
+ [User Interface](#UserInterface)
|
||||
+ [User Interface](#user-interface)
|
||||
+ [Effects](#Effects)
|
||||
+ [Extensions](#Extensions)
|
||||
+ [Misc](#Misc)
|
||||
+ [Credits and Attributions](#CreditsAttributions)
|
||||
+ [Credits and Attributions](#credits-and-attributions)
|
||||
|
||||
## <a name="Khronossamples"></a> Official Khronos Vulkan Samples
|
||||
## Official Khronos Vulkan Samples
|
||||
|
||||
Khronos recently made an official Vulkan Samples repository available to the public ([press release](https://www.khronos.org/blog/vulkan-releases-unified-samples-repository?utm_source=Khronos%20Blog&utm_medium=Twitter&utm_campaign=Vulkan%20Repository)).
|
||||
|
||||
|
|
@ -36,7 +36,7 @@ You can find this repository at https://github.com/KhronosGroup/Vulkan-Samples
|
|||
|
||||
As I've been involved with getting the official repository up and running, I'll be mostly contributing to that repository from now, but may still add samples that don't fit there in here and I'll of course continue to maintain these samples.
|
||||
|
||||
## <a name="Cloning"></a> Cloning
|
||||
## Cloning
|
||||
This repository contains submodules for external dependencies, so when doing a fresh clone you need to clone recursively:
|
||||
|
||||
```
|
||||
|
|
@ -50,26 +50,26 @@ git submodule init
|
|||
git submodule update
|
||||
```
|
||||
|
||||
## <a name="Assets"></a> Assets
|
||||
## Assets
|
||||
Many examples require assets from the asset pack that is not part of this repository due to file size. A python script is included to download the asset pack that. Run
|
||||
|
||||
python download_assets.py
|
||||
|
||||
from the root of the repository after cloning or see [this](data/README.md) for manual download.
|
||||
|
||||
## <a name="Building"></a> Building
|
||||
## Building
|
||||
|
||||
The repository contains everything required to compile and build the examples on <img src="./images/windowslogo.png" alt="" height="22px" valign="bottom"> Windows, <img src="./images/linuxlogo.png" alt="" height="24px" valign="bottom"> Linux, <img src="./images/androidlogo.png" alt="" height="24px" valign="bottom"> Android, <img src="./images/applelogo.png" alt="" valign="bottom" height="24px"> iOS and macOS (using MoltenVK) using a C++ compiler that supports C++11.
|
||||
|
||||
See [BUILD.md](BUILD.md) for details on how to build for the different platforms.
|
||||
|
||||
## <a name="Shaders"></a> Shaders
|
||||
## Shaders
|
||||
|
||||
Vulkan consumes shaders in an intermediate representation called SPIR-V. This makes it possible to use different shader languages by compiling them to that bytecode format. The primary shader language used here is [GLSL](data/shaders/glsl) but thanks to an external contribution you'll also find [HLSL](data/shaders/hlsl) shader sources.
|
||||
|
||||
## <a name="Examples"></a> Examples
|
||||
## Examples
|
||||
|
||||
### <a name="Basics"></a> Basics
|
||||
### Basics
|
||||
|
||||
#### [01 - Triangle](examples/triangle/)
|
||||
Basic and verbose example for getting a colored triangle rendered to the screen using Vulkan. This is meant as a starting point for learning Vulkan from the ground up. A huge part of the code is boilerplate that is abstracted away in later examples.
|
||||
|
|
@ -134,7 +134,7 @@ Implements a simple CPU based particle system. Particle data is stored in host m
|
|||
|
||||
Uses the stencil buffer and its compare functionality for rendering a 3D model with dynamic outlines.
|
||||
|
||||
### <a name="glTF"></a> glTF
|
||||
### glTF
|
||||
|
||||
These samples show how implement different features of the [glTF 2.0 3D format](https://www.khronos.org/gltf/) 3D transmission file format in detail.
|
||||
|
||||
|
|
@ -150,7 +150,7 @@ Demonstrates how to do GPU vertex skinning from animation data stored in a [glTF
|
|||
|
||||
Renders a complete scene loaded from an [glTF 2.0](https://github.com/KhronosGroup/glTF) file. The sample is based on the glTF model loading sample, and adds data structures, functions and shaders required to render a more complex scene using Crytek's Sponza model with per-material pipelines and normal mapping.
|
||||
|
||||
### <a name="Advanced"></a> Advanced
|
||||
### Advanced
|
||||
|
||||
#### [01 - Multi sampling](examples/multisampling/)
|
||||
|
||||
|
|
@ -184,7 +184,7 @@ Capturing and saving an image after a scene has been rendered using blits to cop
|
|||
|
||||
Implements order independent transparency based on linked lists. To achieve this, the sample uses storage buffers in combination with image load and store atomic operations in the fragment shader.
|
||||
|
||||
### <a name="Performance"></a> Performance
|
||||
### Performance
|
||||
|
||||
#### [01 - Multi threaded command buffer generation](examples/multithreading/)
|
||||
|
||||
|
|
@ -206,7 +206,7 @@ Using query pool objects to get number of passed samples for rendered primitives
|
|||
|
||||
Using query pool objects to gather statistics from different stages of the pipeline like vertex, fragment shader and tessellation evaluation shader invocations depending on payload.
|
||||
|
||||
### <a name="PBR"></a> Physically Based Rendering
|
||||
### Physically Based Rendering
|
||||
|
||||
Physical based rendering as a lighting technique that achieves a more realistic and dynamic look by applying approximations of bidirectional reflectance distribution functions based on measured real-world material parameters and environment lighting.
|
||||
|
||||
|
|
@ -222,7 +222,7 @@ Adds image based lighting from an hdr environment cubemap to the PBR equation, u
|
|||
|
||||
Renders a model specially crafted for a metallic-roughness PBR workflow with textures defining material parameters for the PRB equation (albedo, metallic, roughness, baked ambient occlusion, normal maps) in an image based lighting environment.
|
||||
|
||||
### <a name="Deferred"></a> Deferred
|
||||
### Deferred
|
||||
|
||||
These examples use a [deferred shading](https://en.wikipedia.org/wiki/Deferred_shading) setup.
|
||||
|
||||
|
|
@ -242,7 +242,7 @@ Adds shadows from multiple spotlights to a deferred renderer using a layered dep
|
|||
|
||||
Adds ambient occlusion in screen space to a 3D scene. Depth values from a previous deferred pass are used to generate an ambient occlusion texture that is blurred before being applied to the scene in a final composition path.
|
||||
|
||||
### <a name="ComputeShader"></a> Compute Shader
|
||||
### Compute Shader
|
||||
|
||||
#### [01 - Image processing](examples/computeshader/)
|
||||
|
||||
|
|
@ -268,7 +268,7 @@ Mass-spring based cloth system on the GPU using a compute shader to calculate an
|
|||
|
||||
Purely GPU based frustum visibility culling and level-of-detail system. A compute shader is used to modify draw commands stored in an indirect draw commands buffer to toggle model visibility and select its level-of-detail based on camera distance, no calculations have to be done on and synced with the CPU.
|
||||
|
||||
### <a name="GeometryShader"></a> Geometry Shader
|
||||
### Geometry Shader
|
||||
|
||||
#### [01 - Normal debugging](examples/geometryshader/)
|
||||
|
||||
|
|
@ -278,7 +278,7 @@ Visualizing per-vertex model normals (for debugging). First pass renders the pla
|
|||
|
||||
Renders a scene to multiple viewports in one pass using a geometry shader to apply different matrices per viewport to simulate stereoscopic rendering (left/right). Requires a device with support for ```multiViewport```.
|
||||
|
||||
### <a name="TessellationShader"></a> Tessellation Shader
|
||||
### Tessellation Shader
|
||||
|
||||
#### [01 - Displacement mapping](examples/tessellation/)
|
||||
|
||||
|
|
@ -292,11 +292,11 @@ Renders a terrain using tessellation shaders for height displacement (based on a
|
|||
|
||||
Uses curved PN-triangles ([paper](http://alex.vlachos.com/graphics/CurvedPNTriangles.pdf)) for adding details to a low-polygon model.
|
||||
|
||||
### <a name="Raytracing"></a> Ray Tracing (VK_KHR_ray_tracing)
|
||||
### Hardware accelerated ray tracing
|
||||
|
||||
#### [01 - Basic ray tracing](examples/raytracingbasic)
|
||||
|
||||
Basic example for doing hardware accelerated ray tracing using the ```VK_KHR_ray_tracing``` extension. Shows how to setup acceleration structures, ray tracing pipelines and the shaders needed to do the actual ray tracing.
|
||||
Basic example for doing hardware accelerated ray tracing using the ```VK_KHR_acceleration_structure``` and ```VK_KHR_ray_tracing_pipeline``` extensions. Shows how to setup acceleration structures, ray tracing pipelines and the shader binding table needed to do the actual ray tracing.
|
||||
|
||||
#### [02 - Ray traced shadows](examples/raytracingshadows)
|
||||
|
||||
|
|
@ -306,7 +306,15 @@ Adds ray traced shadows casting using the new ray tracing extensions to a more c
|
|||
|
||||
Renders a complex scene with reflective surfaces using the new ray tracing extensions. Shows how to do recursion inside of the ray tracing shaders for implementing real time reflections.
|
||||
|
||||
### <a name="Headless"></a> Headless
|
||||
#### [04 - Callable ray tracing shaders](examples/raytracingcallable)
|
||||
|
||||
Callable haders can be called from other ray tracing shaders to execute different shaders based on dynamic conditions. The example ray traces multiple geometries, with each calling a differnt callable shader from the closest hit shader.
|
||||
|
||||
#### [05 - Ray query](examples/rayquery)
|
||||
|
||||
```GLSL_EXT_ray_query``` adds ray queryies, which can be used to do ray interesctions in any shader stage. This example makes uses ray quers to add ray casted shadows to a rasteriser in the fragment shader.
|
||||
|
||||
### Headless
|
||||
|
||||
Examples that run one-time tasks and don't make use of visual output (no window system integration). These can be run in environments where no user interface is available ([blog entry](https://www.saschawillems.de/tutorials/vulkan/headless_examples)).
|
||||
|
||||
|
|
@ -318,7 +326,7 @@ Renders a basic scene to a (non-visible) frame buffer attachment, reads it back
|
|||
|
||||
Only uses compute shader capabilities for running calculations on an input data set (passed via SSBO). A fibonacci row is calculated based on input data via the compute shader, stored back and displayed via command line.
|
||||
|
||||
### <a name="UserInterface"></a> User Interface
|
||||
### User Interface
|
||||
|
||||
#### [01 - Text rendering](examples/textoverlay/)
|
||||
|
||||
|
|
@ -332,7 +340,7 @@ Uses a texture that stores signed distance field information per character along
|
|||
|
||||
Generates and renders a complex user interface with multiple windows, controls and user interaction on top of a 3D scene. The UI is generated using [Dear ImGUI](https://github.com/ocornut/imgui) and updated each frame.
|
||||
|
||||
### <a name="Effects"></a> Effects
|
||||
### Effects
|
||||
|
||||
#### [01 - Fullscreen radial blur](examples/radialblur/)
|
||||
|
||||
|
|
@ -350,7 +358,7 @@ Implements multiple texture mapping methods to simulate depth based on texture i
|
|||
|
||||
Uses a spherical material capture texture array defining environment lighting and reflection information to fake complex lighting.
|
||||
|
||||
### <a name="Extensions"></a> Extensions
|
||||
### Extensions
|
||||
|
||||
#### [01 - Conservative rasterization (VK_EXT_conservative_rasterization)](examples/conservativeraster/)
|
||||
|
||||
|
|
@ -387,7 +395,7 @@ Shows how to render a scene using a negative viewport height, making the Vulkan
|
|||
Uses a special image that contains variable shading rates to vary the number of fragment shader invocations across the framebuffer. This makes it possible to lower fragment shader invocations for less important/less noisy parts of the framebuffer.
|
||||
|
||||
|
||||
### <a name="Misc"></a> Misc
|
||||
### Misc
|
||||
|
||||
#### [01 - Vulkan Gears](examples/gears/)
|
||||
|
||||
|
|
@ -397,5 +405,5 @@ Vulkan interpretation of glxgears. Procedurally generates and animates multiple
|
|||
|
||||
Renders a Vulkan demo scene with logos and mascots. Not an actual example but more of a playground and showcase.
|
||||
|
||||
## <a name="CreditsAttributions"></a> Credits and Attributions
|
||||
## Credits and Attributions
|
||||
See [CREDITS.md](CREDITS.md) for additional credits and attributions.
|
||||
|
|
|
|||
37
data/shaders/glsl/rayquery/scene.frag
Normal file
37
data/shaders/glsl/rayquery/scene.frag
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_EXT_ray_query : enable
|
||||
|
||||
layout (binding = 2, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec3 inColor;
|
||||
layout (location = 2) in vec3 inViewVec;
|
||||
layout (location = 3) in vec3 inLightVec;
|
||||
layout (location = 4) in vec3 inWorldPos;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
#define ambient 0.1
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 V = normalize(inViewVec);
|
||||
vec3 R = normalize(-reflect(L, N));
|
||||
vec3 diffuse = max(dot(N, L), ambient) * inColor;
|
||||
|
||||
outFragColor = vec4(diffuse, 1.0);
|
||||
|
||||
rayQueryEXT rayQuery;
|
||||
rayQueryInitializeEXT(rayQuery, topLevelAS, gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, inWorldPos, 0.01, L, 1000.0);
|
||||
|
||||
// Start the ray traversal, rayQueryProceedEXT returns false if the traversal is complete
|
||||
while (rayQueryProceedEXT(rayQuery)) { }
|
||||
|
||||
// If the intersection has hit a triangle, the fragment is shadowed
|
||||
if (rayQueryGetIntersectionTypeEXT(rayQuery, true) == gl_RayQueryCommittedIntersectionTriangleEXT ) {
|
||||
outFragColor *= 0.1;
|
||||
}
|
||||
}
|
||||
BIN
data/shaders/glsl/rayquery/scene.frag.spv
Normal file
BIN
data/shaders/glsl/rayquery/scene.frag.spv
Normal file
Binary file not shown.
33
data/shaders/glsl/rayquery/scene.vert
Normal file
33
data/shaders/glsl/rayquery/scene.vert
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#version 450
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec2 inUV;
|
||||
layout (location = 2) in vec3 inColor;
|
||||
layout (location = 3) in vec3 inNormal;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 view;
|
||||
mat4 model;
|
||||
vec3 lightPos;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec3 outColor;
|
||||
layout (location = 2) out vec3 outViewVec;
|
||||
layout (location = 3) out vec3 outLightVec;
|
||||
layout (location = 4) out vec3 outWorldPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
outColor = inColor;
|
||||
outNormal = inNormal;
|
||||
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
vec4 pos = ubo.model * vec4(inPos, 1.0);
|
||||
outWorldPos = vec3(ubo.model * vec4(inPos, 1.0));
|
||||
outNormal = mat3(ubo.model) * inNormal;
|
||||
outLightVec = normalize(ubo.lightPos - inPos);
|
||||
outViewVec = -pos.xyz;
|
||||
}
|
||||
|
||||
BIN
data/shaders/glsl/rayquery/scene.vert.spv
Normal file
BIN
data/shaders/glsl/rayquery/scene.vert.spv
Normal file
Binary file not shown.
11
data/shaders/glsl/raytracingcallable/callable1.rcall
Normal file
11
data/shaders/glsl/raytracingcallable/callable1.rcall
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#version 460 core
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
|
||||
layout(location = 0) callableDataInEXT vec3 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Generate a checker board pattern
|
||||
vec2 pos = vec2(gl_LaunchIDEXT / 8);
|
||||
outColor = vec3(mod(pos.x + mod(pos.y, 2.0), 2.0));
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/callable1.rcall.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/callable1.rcall.spv
Normal file
Binary file not shown.
9
data/shaders/glsl/raytracingcallable/callable2.rcall
Normal file
9
data/shaders/glsl/raytracingcallable/callable2.rcall
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#version 460 core
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
|
||||
layout(location = 0) callableDataInEXT vec3 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
outColor = vec3(0.0, 1.0, 0.0);
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/callable2.rcall.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/callable2.rcall.spv
Normal file
Binary file not shown.
13
data/shaders/glsl/raytracingcallable/callable3.rcall
Normal file
13
data/shaders/glsl/raytracingcallable/callable3.rcall
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#version 460 core
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
|
||||
layout(location = 0) callableDataInEXT vec3 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Generate a line pattern
|
||||
vec2 pos = vec2(gl_LaunchIDEXT / 8);
|
||||
outColor = vec3(mod(pos.y, 2.0));
|
||||
|
||||
// outColor = vec3(0.0, 0.0, 1.0);
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/callable3.rcall.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/callable3.rcall.spv
Normal file
Binary file not shown.
18
data/shaders/glsl/raytracingcallable/closesthit.rchit
Normal file
18
data/shaders/glsl/raytracingcallable/closesthit.rchit
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_EXT_nonuniform_qualifier : enable
|
||||
|
||||
layout(location = 0) rayPayloadInEXT vec3 hitValue;
|
||||
layout(location = 0) callableDataEXT vec3 outColor;
|
||||
hitAttributeEXT vec3 attribs;
|
||||
|
||||
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Execute the callable shader indexed by the current geometry being hit
|
||||
// For our sample this means that the first callable shader in the SBT is invoked for the first triangle, the second callable shader for the second triangle, etc.
|
||||
executeCallableEXT(gl_GeometryIndexEXT, 0);
|
||||
|
||||
hitValue = outColor;
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/closesthit.rchit.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/closesthit.rchit.spv
Normal file
Binary file not shown.
9
data/shaders/glsl/raytracingcallable/miss.rmiss
Normal file
9
data/shaders/glsl/raytracingcallable/miss.rmiss
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
|
||||
layout(location = 0) rayPayloadInEXT vec3 hitValue;
|
||||
|
||||
void main()
|
||||
{
|
||||
hitValue = vec3(0.0, 0.0, 0.2);
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/miss.rmiss.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/miss.rmiss.spv
Normal file
Binary file not shown.
32
data/shaders/glsl/raytracingcallable/raygen.rgen
Normal file
32
data/shaders/glsl/raytracingcallable/raygen.rgen
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
|
||||
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(binding = 1, set = 0, rgba8) uniform image2D image;
|
||||
layout(binding = 2, set = 0) uniform CameraProperties
|
||||
{
|
||||
mat4 viewInverse;
|
||||
mat4 projInverse;
|
||||
} cam;
|
||||
|
||||
layout(location = 0) rayPayloadEXT vec3 hitValue;
|
||||
|
||||
void main()
|
||||
{
|
||||
const vec2 pixelCenter = vec2(gl_LaunchIDEXT.xy) + vec2(0.5);
|
||||
const vec2 inUV = pixelCenter/vec2(gl_LaunchSizeEXT.xy);
|
||||
vec2 d = inUV * 2.0 - 1.0;
|
||||
|
||||
vec4 origin = cam.viewInverse * vec4(0,0,0,1);
|
||||
vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1) ;
|
||||
vec4 direction = cam.viewInverse*vec4(normalize(target.xyz / target.w), 0) ;
|
||||
|
||||
uint rayFlags = gl_RayFlagsOpaqueEXT;
|
||||
uint cullMask = 0xff;
|
||||
float tmin = 0.001;
|
||||
float tmax = 10000.0;
|
||||
|
||||
traceRayEXT(topLevelAS, rayFlags, cullMask, 0, 0, 0, origin.xyz, tmin, direction.xyz, tmax, 0);
|
||||
|
||||
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hitValue, 0.0));
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/raygen.rgen.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/raygen.rgen.spv
Normal file
Binary file not shown.
9
data/shaders/glsl/raytracingcallable/shadow.rmiss
Normal file
9
data/shaders/glsl/raytracingcallable/shadow.rmiss
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
|
||||
layout(location = 2) rayPayloadInEXT bool shadowed;
|
||||
|
||||
void main()
|
||||
{
|
||||
shadowed = false;
|
||||
}
|
||||
BIN
data/shaders/glsl/raytracingcallable/shadow.rmiss.spv
Normal file
BIN
data/shaders/glsl/raytracingcallable/shadow.rmiss.spv
Normal file
Binary file not shown.
|
|
@ -107,7 +107,9 @@ set(EXAMPLES
|
|||
pushconstants
|
||||
pushdescriptors
|
||||
radialblur
|
||||
rayquery
|
||||
raytracingbasic
|
||||
raytracingcallable
|
||||
raytracingreflections
|
||||
raytracingshadows
|
||||
renderheadless
|
||||
|
|
|
|||
507
examples/rayquery/rayquery.cpp
Normal file
507
examples/rayquery/rayquery.cpp
Normal file
|
|
@ -0,0 +1,507 @@
|
|||
/*
|
||||
* Vulkan Example - Using ray queries for hardware accelerated ray tracing queries in a fragment shader
|
||||
*
|
||||
* Copyright (C) 2020 by Sascha Willems - www.saschawillems.de
|
||||
*
|
||||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#include "vulkanexamplebase.h"
|
||||
#include "VulkanglTFModel.h"
|
||||
#include "VulkanRaytracingSample.h"
|
||||
|
||||
#define ENABLE_VALIDATION false
|
||||
|
||||
class VulkanExample : public VulkanRaytracingSample
|
||||
{
|
||||
public:
|
||||
glm::vec3 lightPos = glm::vec3();
|
||||
|
||||
struct UniformData {
|
||||
glm::mat4 projection;
|
||||
glm::mat4 view;
|
||||
glm::mat4 model;
|
||||
glm::vec3 lightPos;
|
||||
} uniformData;
|
||||
vks::Buffer ubo;
|
||||
|
||||
vkglTF::Model scene;
|
||||
|
||||
VkPipeline pipeline;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
|
||||
VkDescriptorSet descriptorSet;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
|
||||
VulkanRaytracingSample::AccelerationStructure bottomLevelAS{};
|
||||
VulkanRaytracingSample::AccelerationStructure topLevelAS{};
|
||||
|
||||
VkPhysicalDeviceRayQueryFeaturesKHR enabledRayQueryFeatures{};
|
||||
|
||||
VulkanExample() : VulkanRaytracingSample()
|
||||
{
|
||||
title = "Ray query";
|
||||
camera.type = Camera::CameraType::lookat;
|
||||
camera.setPosition(glm::vec3(0.0f, -0.0f, -20.0f));
|
||||
camera.setRotation(glm::vec3(-15.0f, -390.0f, 0.0f));
|
||||
camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f);
|
||||
timerSpeed *= 0.5f;
|
||||
settings.overlay = true;
|
||||
enableExtensions();
|
||||
enabledDeviceExtensions.push_back(VK_KHR_RAY_QUERY_EXTENSION_NAME);
|
||||
}
|
||||
|
||||
~VulkanExample()
|
||||
{
|
||||
vkDestroyPipeline(device, pipeline, nullptr);
|
||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||
ubo.destroy();
|
||||
}
|
||||
|
||||
/*
|
||||
Create the bottom level acceleration structure contains the scene's actual geometry (vertices, triangles)
|
||||
*/
|
||||
void createBottomLevelAccelerationStructure()
|
||||
{
|
||||
VkDeviceOrHostAddressConstKHR vertexBufferDeviceAddress{};
|
||||
VkDeviceOrHostAddressConstKHR indexBufferDeviceAddress{};
|
||||
|
||||
vertexBufferDeviceAddress.deviceAddress = getBufferDeviceAddress(scene.vertices.buffer);
|
||||
indexBufferDeviceAddress.deviceAddress = getBufferDeviceAddress(scene.indices.buffer);
|
||||
|
||||
uint32_t numTriangles = static_cast<uint32_t>(scene.indices.count) / 3;
|
||||
uint32_t maxVertex = scene.vertices.count;
|
||||
|
||||
// Build
|
||||
VkAccelerationStructureGeometryKHR accelerationStructureGeometry = vks::initializers::accelerationStructureGeometryKHR();
|
||||
accelerationStructureGeometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
|
||||
accelerationStructureGeometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
|
||||
accelerationStructureGeometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
|
||||
accelerationStructureGeometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
accelerationStructureGeometry.geometry.triangles.vertexData = vertexBufferDeviceAddress;
|
||||
accelerationStructureGeometry.geometry.triangles.maxVertex = maxVertex;
|
||||
accelerationStructureGeometry.geometry.triangles.vertexStride = sizeof(vkglTF::Vertex);
|
||||
accelerationStructureGeometry.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
|
||||
accelerationStructureGeometry.geometry.triangles.indexData = indexBufferDeviceAddress;
|
||||
accelerationStructureGeometry.geometry.triangles.transformData.deviceAddress = 0;
|
||||
accelerationStructureGeometry.geometry.triangles.transformData.hostAddress = nullptr;
|
||||
|
||||
// Get size info
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationStructureBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationStructureBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
|
||||
accelerationStructureBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationStructureBuildGeometryInfo.geometryCount = 1;
|
||||
accelerationStructureBuildGeometryInfo.pGeometries = &accelerationStructureGeometry;
|
||||
|
||||
VkAccelerationStructureBuildSizesInfoKHR accelerationStructureBuildSizesInfo = vks::initializers::accelerationStructureBuildSizesInfoKHR();
|
||||
vkGetAccelerationStructureBuildSizesKHR(
|
||||
device,
|
||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||
&accelerationStructureBuildGeometryInfo,
|
||||
&numTriangles,
|
||||
&accelerationStructureBuildSizesInfo);
|
||||
|
||||
createAccelerationStructure(bottomLevelAS, VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR, accelerationStructureBuildSizesInfo);
|
||||
|
||||
// Create a small scratch buffer used during build of the bottom level acceleration structure
|
||||
ScratchBuffer scratchBuffer = createScratchBuffer(accelerationStructureBuildSizesInfo.buildScratchSize);
|
||||
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
|
||||
accelerationBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationBuildGeometryInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
|
||||
accelerationBuildGeometryInfo.dstAccelerationStructure = bottomLevelAS.handle;
|
||||
accelerationBuildGeometryInfo.geometryCount = 1;
|
||||
accelerationBuildGeometryInfo.pGeometries = &accelerationStructureGeometry;
|
||||
accelerationBuildGeometryInfo.scratchData.deviceAddress = scratchBuffer.deviceAddress;
|
||||
|
||||
VkAccelerationStructureBuildRangeInfoKHR accelerationStructureBuildRangeInfo{};
|
||||
accelerationStructureBuildRangeInfo.primitiveCount = numTriangles;
|
||||
accelerationStructureBuildRangeInfo.primitiveOffset = 0;
|
||||
accelerationStructureBuildRangeInfo.firstVertex = 0;
|
||||
accelerationStructureBuildRangeInfo.transformOffset = 0;
|
||||
std::vector<VkAccelerationStructureBuildRangeInfoKHR*> accelerationBuildStructureRangeInfos = { &accelerationStructureBuildRangeInfo };
|
||||
|
||||
if (accelerationStructureFeatures.accelerationStructureHostCommands)
|
||||
{
|
||||
// Implementation supports building acceleration structure building on host
|
||||
vkBuildAccelerationStructuresKHR(
|
||||
device,
|
||||
VK_NULL_HANDLE,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Acceleration structure needs to be build on the device
|
||||
VkCommandBuffer commandBuffer = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
vkCmdBuildAccelerationStructuresKHR(
|
||||
commandBuffer,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
vulkanDevice->flushCommandBuffer(commandBuffer, queue);
|
||||
}
|
||||
|
||||
deleteScratchBuffer(scratchBuffer);
|
||||
}
|
||||
|
||||
/*
|
||||
The top level acceleration structure contains the scene's object instances
|
||||
*/
|
||||
void createTopLevelAccelerationStructure()
|
||||
{
|
||||
VkTransformMatrixKHR transformMatrix = {
|
||||
1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f };
|
||||
|
||||
VkAccelerationStructureInstanceKHR instance{};
|
||||
instance.transform = transformMatrix;
|
||||
instance.instanceCustomIndex = 0;
|
||||
instance.mask = 0xFF;
|
||||
instance.instanceShaderBindingTableRecordOffset = 0;
|
||||
instance.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR;
|
||||
instance.accelerationStructureReference = bottomLevelAS.deviceAddress;
|
||||
|
||||
// Buffer for instance data
|
||||
vks::Buffer instancesBuffer;
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&instancesBuffer,
|
||||
sizeof(VkAccelerationStructureInstanceKHR),
|
||||
&instance));
|
||||
|
||||
VkDeviceOrHostAddressConstKHR instanceDataDeviceAddress{};
|
||||
instanceDataDeviceAddress.deviceAddress = getBufferDeviceAddress(instancesBuffer.buffer);
|
||||
|
||||
VkAccelerationStructureGeometryKHR accelerationStructureGeometry = vks::initializers::accelerationStructureGeometryKHR();
|
||||
accelerationStructureGeometry.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
|
||||
accelerationStructureGeometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
|
||||
accelerationStructureGeometry.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
|
||||
accelerationStructureGeometry.geometry.instances.arrayOfPointers = VK_FALSE;
|
||||
accelerationStructureGeometry.geometry.instances.data = instanceDataDeviceAddress;
|
||||
|
||||
// Get size info
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationStructureBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationStructureBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
|
||||
accelerationStructureBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationStructureBuildGeometryInfo.geometryCount = 1;
|
||||
accelerationStructureBuildGeometryInfo.pGeometries = &accelerationStructureGeometry;
|
||||
|
||||
uint32_t primitive_count = 1;
|
||||
|
||||
VkAccelerationStructureBuildSizesInfoKHR accelerationStructureBuildSizesInfo = vks::initializers::accelerationStructureBuildSizesInfoKHR();
|
||||
vkGetAccelerationStructureBuildSizesKHR(
|
||||
device,
|
||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||
&accelerationStructureBuildGeometryInfo,
|
||||
&primitive_count,
|
||||
&accelerationStructureBuildSizesInfo);
|
||||
|
||||
createAccelerationStructure(topLevelAS, VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR, accelerationStructureBuildSizesInfo);
|
||||
|
||||
// Create a small scratch buffer used during build of the top level acceleration structure
|
||||
ScratchBuffer scratchBuffer = createScratchBuffer(accelerationStructureBuildSizesInfo.buildScratchSize);
|
||||
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
|
||||
accelerationBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationBuildGeometryInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
|
||||
accelerationBuildGeometryInfo.dstAccelerationStructure = topLevelAS.handle;
|
||||
accelerationBuildGeometryInfo.geometryCount = 1;
|
||||
accelerationBuildGeometryInfo.pGeometries = &accelerationStructureGeometry;
|
||||
accelerationBuildGeometryInfo.scratchData.deviceAddress = scratchBuffer.deviceAddress;
|
||||
|
||||
VkAccelerationStructureBuildRangeInfoKHR accelerationStructureBuildRangeInfo{};
|
||||
accelerationStructureBuildRangeInfo.primitiveCount = 1;
|
||||
accelerationStructureBuildRangeInfo.primitiveOffset = 0;
|
||||
accelerationStructureBuildRangeInfo.firstVertex = 0;
|
||||
accelerationStructureBuildRangeInfo.transformOffset = 0;
|
||||
std::vector<VkAccelerationStructureBuildRangeInfoKHR*> accelerationBuildStructureRangeInfos = { &accelerationStructureBuildRangeInfo };
|
||||
|
||||
if (accelerationStructureFeatures.accelerationStructureHostCommands)
|
||||
{
|
||||
// Implementation supports building acceleration structure building on host
|
||||
vkBuildAccelerationStructuresKHR(
|
||||
device,
|
||||
VK_NULL_HANDLE,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Acceleration structure needs to be build on the device
|
||||
VkCommandBuffer commandBuffer = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
vkCmdBuildAccelerationStructuresKHR(
|
||||
commandBuffer,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
vulkanDevice->flushCommandBuffer(commandBuffer, queue);
|
||||
}
|
||||
|
||||
deleteScratchBuffer(scratchBuffer);
|
||||
instancesBuffer.destroy();
|
||||
}
|
||||
|
||||
void buildCommandBuffers()
|
||||
{
|
||||
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
||||
|
||||
VkClearValue clearValues[2];
|
||||
VkViewport viewport;
|
||||
VkRect2D scissor;
|
||||
|
||||
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
||||
{
|
||||
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
||||
|
||||
/*
|
||||
Note: Explicit synchronization is not required between the render pass, as this is done implicit via sub pass dependencies
|
||||
*/
|
||||
|
||||
/*
|
||||
Second pass: Scene rendering with applied shadow map
|
||||
*/
|
||||
|
||||
clearValues[0].color = defaultClearColor;
|
||||
clearValues[1].depthStencil = { 1.0f, 0 };
|
||||
|
||||
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
||||
renderPassBeginInfo.renderPass = renderPass;
|
||||
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
||||
renderPassBeginInfo.renderArea.extent.width = width;
|
||||
renderPassBeginInfo.renderArea.extent.height = height;
|
||||
renderPassBeginInfo.clearValueCount = 2;
|
||||
renderPassBeginInfo.pClearValues = clearValues;
|
||||
|
||||
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
||||
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
||||
|
||||
scissor = vks::initializers::rect2D(width, height, 0, 0);
|
||||
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
||||
|
||||
// 3D scene
|
||||
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
||||
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
scene.draw(drawCmdBuffers[i]);
|
||||
|
||||
drawUI(drawCmdBuffers[i]);
|
||||
|
||||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||||
|
||||
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void loadAssets()
|
||||
{
|
||||
vkglTF::memoryPropertyFlags = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
||||
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
||||
scene.loadFromFile(getAssetPath() + "models/vulkanscene_shadow.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
||||
//const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
||||
//scenes.resize(2);
|
||||
//scenes[0].loadFromFile(getAssetPath() + "models/vulkanscene_shadow.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
||||
//scenes[1].loadFromFile(getAssetPath() + "models/samplescene.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
||||
//sceneNames = {"Vulkan scene", "Teapots and pillars" };
|
||||
}
|
||||
|
||||
void setupDescriptorPool()
|
||||
{
|
||||
std::vector<VkDescriptorPoolSize> poolSizes = {
|
||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
|
||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3),
|
||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 3)
|
||||
};
|
||||
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 3);
|
||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||
}
|
||||
|
||||
void setupDescriptorSetLayout()
|
||||
{
|
||||
// Shared pipeline layout for all pipelines used in this sample
|
||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
||||
// Binding 0 : Vertex shader uniform buffer
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
||||
// Binding 1 : Fragment shader image sampler (shadow map)
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1),
|
||||
// Binding 2: Acceleration structure
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, VK_SHADER_STAGE_FRAGMENT_BIT, 2),
|
||||
};
|
||||
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||
}
|
||||
|
||||
void setupDescriptorSets()
|
||||
{
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
|
||||
|
||||
// Debug display
|
||||
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
||||
|
||||
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);
|
||||
|
||||
// Scene rendering with shadow map applied
|
||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
||||
writeDescriptorSets = {
|
||||
// Binding 0 : Vertex shader uniform buffer
|
||||
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &ubo.descriptor)
|
||||
};
|
||||
|
||||
VkWriteDescriptorSetAccelerationStructureKHR descriptorAccelerationStructureInfo = vks::initializers::writeDescriptorSetAccelerationStructureKHR();
|
||||
descriptorAccelerationStructureInfo.accelerationStructureCount = 1;
|
||||
descriptorAccelerationStructureInfo.pAccelerationStructures = &topLevelAS.handle;
|
||||
|
||||
VkWriteDescriptorSet accelerationStructureWrite{};
|
||||
accelerationStructureWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
// The specialized acceleration structure descriptor has to be chained
|
||||
accelerationStructureWrite.pNext = &descriptorAccelerationStructureInfo;
|
||||
accelerationStructureWrite.dstSet = descriptorSet;
|
||||
accelerationStructureWrite.dstBinding = 2;
|
||||
accelerationStructureWrite.descriptorCount = 1;
|
||||
accelerationStructureWrite.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
|
||||
|
||||
writeDescriptorSets.push_back(accelerationStructureWrite);
|
||||
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);
|
||||
}
|
||||
|
||||
void preparePipelines()
|
||||
{
|
||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
||||
VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendStateCI = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
|
||||
VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
||||
VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
||||
VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
||||
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
||||
VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size(), 0);
|
||||
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
||||
|
||||
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
|
||||
pipelineCI.pInputAssemblyState = &inputAssemblyStateCI;
|
||||
pipelineCI.pRasterizationState = &rasterizationStateCI;
|
||||
pipelineCI.pColorBlendState = &colorBlendStateCI;
|
||||
pipelineCI.pMultisampleState = &multisampleStateCI;
|
||||
pipelineCI.pViewportState = &viewportStateCI;
|
||||
pipelineCI.pDepthStencilState = &depthStencilStateCI;
|
||||
pipelineCI.pDynamicState = &dynamicStateCI;
|
||||
pipelineCI.stageCount = shaderStages.size();
|
||||
pipelineCI.pStages = shaderStages.data();
|
||||
|
||||
// Scene rendering with ray traced shadows applied
|
||||
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({ vkglTF::VertexComponent::Position, vkglTF::VertexComponent::UV, vkglTF::VertexComponent::Color, vkglTF::VertexComponent::Normal });
|
||||
rasterizationStateCI.cullMode = VK_CULL_MODE_BACK_BIT;
|
||||
shaderStages[0] = loadShader(getShadersPath() + "rayquery/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
||||
shaderStages[1] = loadShader(getShadersPath() + "rayquery/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline));
|
||||
}
|
||||
|
||||
|
||||
// Prepare and initialize uniform buffer containing shader uniforms
|
||||
void prepareUniformBuffers()
|
||||
{
|
||||
// Scene vertex shader uniform buffer block
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&ubo,
|
||||
sizeof(UniformData)));
|
||||
|
||||
// Map persistent
|
||||
VK_CHECK_RESULT(ubo.map());
|
||||
|
||||
updateLight();
|
||||
updateUniformBuffers();
|
||||
}
|
||||
|
||||
void updateLight()
|
||||
{
|
||||
// Animate the light source
|
||||
lightPos.x = cos(glm::radians(timer * 360.0f)) * 40.0f;
|
||||
lightPos.y = -50.0f + sin(glm::radians(timer * 360.0f)) * 20.0f;
|
||||
lightPos.z = 25.0f + sin(glm::radians(timer * 360.0f)) * 5.0f;
|
||||
}
|
||||
|
||||
void updateUniformBuffers()
|
||||
{
|
||||
uniformData.projection = camera.matrices.perspective;
|
||||
uniformData.view = camera.matrices.view;
|
||||
uniformData.model = glm::mat4(1.0f);
|
||||
uniformData.lightPos = lightPos;
|
||||
memcpy(ubo.mapped, &uniformData, sizeof(UniformData));
|
||||
}
|
||||
|
||||
void getEnabledFeatures()
|
||||
{
|
||||
// Enable features required for ray tracing using feature chaining via pNext
|
||||
enabledBufferDeviceAddresFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES;
|
||||
enabledBufferDeviceAddresFeatures.bufferDeviceAddress = VK_TRUE;
|
||||
|
||||
enabledRayTracingPipelineFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR;
|
||||
enabledRayTracingPipelineFeatures.rayTracingPipeline = VK_TRUE;
|
||||
enabledRayTracingPipelineFeatures.pNext = &enabledBufferDeviceAddresFeatures;
|
||||
|
||||
enabledAccelerationStructureFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR;
|
||||
enabledAccelerationStructureFeatures.accelerationStructure = VK_TRUE;
|
||||
enabledAccelerationStructureFeatures.pNext = &enabledRayTracingPipelineFeatures;
|
||||
|
||||
enabledRayQueryFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR;
|
||||
enabledRayQueryFeatures.rayQuery = VK_TRUE;
|
||||
enabledRayQueryFeatures.pNext = &enabledAccelerationStructureFeatures;
|
||||
|
||||
deviceCreatepNextChain = &enabledRayQueryFeatures;
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
VulkanExampleBase::prepareFrame();
|
||||
|
||||
// Command buffer to be submitted to the queue
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||||
|
||||
// Submit to queue
|
||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||
|
||||
VulkanExampleBase::submitFrame();
|
||||
}
|
||||
|
||||
void prepare()
|
||||
{
|
||||
VulkanRaytracingSample::prepare();
|
||||
loadAssets();
|
||||
prepareUniformBuffers();
|
||||
setupDescriptorSetLayout();
|
||||
preparePipelines();
|
||||
createBottomLevelAccelerationStructure();
|
||||
createTopLevelAccelerationStructure();
|
||||
setupDescriptorPool();
|
||||
setupDescriptorSets();
|
||||
buildCommandBuffers();
|
||||
prepared = true;
|
||||
}
|
||||
|
||||
virtual void render()
|
||||
{
|
||||
if (!prepared)
|
||||
return;
|
||||
draw();
|
||||
if (!paused || camera.updated)
|
||||
{
|
||||
updateLight();
|
||||
updateUniformBuffers();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
VULKAN_EXAMPLE_MAIN()
|
||||
670
examples/raytracingcallable/raytracingcallable.cpp
Normal file
670
examples/raytracingcallable/raytracingcallable.cpp
Normal file
|
|
@ -0,0 +1,670 @@
|
|||
/*
|
||||
* Vulkan Example - Hardware accelerated ray tracing callable shaders example
|
||||
*
|
||||
* Renders a complex scene using multiple hit and miss shaders for implementing shadows
|
||||
*
|
||||
* Copyright (C) by Sascha Willems - www.saschawillems.de
|
||||
*
|
||||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#include "VulkanRaytracingSample.h"
|
||||
|
||||
class VulkanExample : public VulkanRaytracingSample
|
||||
{
|
||||
public:
|
||||
AccelerationStructure bottomLevelAS;
|
||||
AccelerationStructure topLevelAS;
|
||||
|
||||
std::vector<VkRayTracingShaderGroupCreateInfoKHR> shaderGroups{};
|
||||
struct ShaderBindingTables {
|
||||
ShaderBindingTable raygen;
|
||||
ShaderBindingTable miss;
|
||||
ShaderBindingTable hit;
|
||||
ShaderBindingTable callable;
|
||||
} shaderBindingTables;
|
||||
|
||||
struct UniformData {
|
||||
glm::mat4 viewInverse;
|
||||
glm::mat4 projInverse;
|
||||
} uniformData;
|
||||
vks::Buffer ubo;
|
||||
|
||||
VkPipeline pipeline;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
VkDescriptorSet descriptorSet;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
|
||||
vks::Buffer vertexBuffer;
|
||||
vks::Buffer indexBuffer;
|
||||
vks::Buffer transformBuffer;
|
||||
|
||||
uint32_t objectCount = 3;
|
||||
|
||||
// This sample is derived from an extended base class that saves most of the ray tracing setup boiler plate
|
||||
VulkanExample() : VulkanRaytracingSample()
|
||||
{
|
||||
title = "Ray tracing callable shaders";
|
||||
settings.overlay = false;
|
||||
timerSpeed *= 0.25f;
|
||||
camera.type = Camera::CameraType::lookat;
|
||||
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 512.0f);
|
||||
camera.setRotation(glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
camera.setTranslation(glm::vec3(0.0f, 0.0f, -10.0f));
|
||||
enableExtensions();
|
||||
}
|
||||
|
||||
~VulkanExample()
|
||||
{
|
||||
vkDestroyPipeline(device, pipeline, nullptr);
|
||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||
deleteStorageImage();
|
||||
deleteAccelerationStructure(bottomLevelAS);
|
||||
deleteAccelerationStructure(topLevelAS);
|
||||
shaderBindingTables.raygen.destroy();
|
||||
shaderBindingTables.miss.destroy();
|
||||
shaderBindingTables.hit.destroy();
|
||||
shaderBindingTables.callable.destroy();
|
||||
ubo.destroy();
|
||||
}
|
||||
|
||||
/*
|
||||
Create the bottom level acceleration structure contains the scene's actual geometry (vertices, triangles)
|
||||
*/
|
||||
void createBottomLevelAccelerationStructure()
|
||||
{
|
||||
// Setup vertices for a single triangle
|
||||
struct Vertex {
|
||||
float pos[3];
|
||||
};
|
||||
std::vector<Vertex> vertices = {
|
||||
{ { 1.0f, 1.0f, 0.0f } },
|
||||
{ { -1.0f, 1.0f, 0.0f } },
|
||||
{ { 0.0f, -1.0f, 0.0f } }
|
||||
};
|
||||
|
||||
// Setup indices
|
||||
std::vector<uint32_t> indices = { 0, 1, 2 };
|
||||
uint32_t indexCount = static_cast<uint32_t>(indices.size());
|
||||
|
||||
// Setup transform matrices for the geometries in the bottom level AS
|
||||
std::vector<VkTransformMatrixKHR> transformMatrices(objectCount);
|
||||
for (uint32_t i = 0; i < objectCount; i++) {
|
||||
transformMatrices[i] = {
|
||||
1.0f, 0.0f, 0.0f, (float)i * 3.0f - 3.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
}
|
||||
// Transform buffer
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&transformBuffer,
|
||||
objectCount * sizeof(VkTransformMatrixKHR),
|
||||
transformMatrices.data()));
|
||||
|
||||
// Create buffers
|
||||
// For the sake of simplicity we won't stage the vertex data to the GPU memory
|
||||
// Vertex buffer
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&vertexBuffer,
|
||||
vertices.size() * sizeof(Vertex),
|
||||
vertices.data()));
|
||||
// Index buffer
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&indexBuffer,
|
||||
indices.size() * sizeof(uint32_t),
|
||||
indices.data()));
|
||||
|
||||
VkDeviceOrHostAddressConstKHR vertexBufferDeviceAddress{};
|
||||
VkDeviceOrHostAddressConstKHR indexBufferDeviceAddress{};
|
||||
VkDeviceOrHostAddressConstKHR transformBufferDeviceAddress{};
|
||||
|
||||
vertexBufferDeviceAddress.deviceAddress = getBufferDeviceAddress(vertexBuffer.buffer);
|
||||
indexBufferDeviceAddress.deviceAddress = getBufferDeviceAddress(indexBuffer.buffer);
|
||||
transformBufferDeviceAddress.deviceAddress = getBufferDeviceAddress(transformBuffer.buffer);
|
||||
|
||||
uint32_t numTriangles = 1;
|
||||
|
||||
// Our scene will consist of three different triangles, that'll be distinguished in the shader via gl_GeometryIndexEXT, so we add three geometries to the bottom level AS
|
||||
std::vector<uint32_t> geometryCounts;
|
||||
std::vector<VkAccelerationStructureGeometryKHR> accelerationStructureGeometries;
|
||||
for (uint32_t i = 0; i < objectCount; i++) {
|
||||
VkAccelerationStructureGeometryKHR accelerationStructureGeometry = vks::initializers::accelerationStructureGeometryKHR();
|
||||
accelerationStructureGeometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
|
||||
accelerationStructureGeometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
|
||||
accelerationStructureGeometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
|
||||
accelerationStructureGeometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
accelerationStructureGeometry.geometry.triangles.vertexData = vertexBufferDeviceAddress;
|
||||
accelerationStructureGeometry.geometry.triangles.maxVertex = 3;
|
||||
accelerationStructureGeometry.geometry.triangles.vertexStride = sizeof(Vertex);
|
||||
accelerationStructureGeometry.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
|
||||
accelerationStructureGeometry.geometry.triangles.indexData = indexBufferDeviceAddress;
|
||||
accelerationStructureGeometry.geometry.triangles.transformData = transformBufferDeviceAddress;
|
||||
accelerationStructureGeometries.push_back(accelerationStructureGeometry);
|
||||
geometryCounts.push_back(1);
|
||||
}
|
||||
|
||||
// Get size info
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationStructureBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationStructureBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
|
||||
accelerationStructureBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationStructureBuildGeometryInfo.geometryCount = static_cast<uint32_t>(accelerationStructureGeometries.size());
|
||||
accelerationStructureBuildGeometryInfo.pGeometries = accelerationStructureGeometries.data();
|
||||
|
||||
VkAccelerationStructureBuildSizesInfoKHR accelerationStructureBuildSizesInfo = vks::initializers::accelerationStructureBuildSizesInfoKHR();
|
||||
vkGetAccelerationStructureBuildSizesKHR(
|
||||
device,
|
||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||
&accelerationStructureBuildGeometryInfo,
|
||||
geometryCounts.data(),
|
||||
&accelerationStructureBuildSizesInfo);
|
||||
|
||||
createAccelerationStructure(bottomLevelAS, VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR, accelerationStructureBuildSizesInfo);
|
||||
|
||||
// Create a small scratch buffer used during build of the bottom level acceleration structure
|
||||
ScratchBuffer scratchBuffer = createScratchBuffer(accelerationStructureBuildSizesInfo.buildScratchSize);
|
||||
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
|
||||
accelerationBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationBuildGeometryInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
|
||||
accelerationBuildGeometryInfo.dstAccelerationStructure = bottomLevelAS.handle;
|
||||
accelerationBuildGeometryInfo.geometryCount = static_cast<uint32_t>(accelerationStructureGeometries.size());
|
||||
accelerationBuildGeometryInfo.pGeometries = accelerationStructureGeometries.data();
|
||||
accelerationBuildGeometryInfo.scratchData.deviceAddress = scratchBuffer.deviceAddress;
|
||||
|
||||
std::vector<VkAccelerationStructureBuildRangeInfoKHR> accelerationStructureBuildRangeInfos{};
|
||||
for (uint32_t i = 0; i < objectCount; i++) {
|
||||
VkAccelerationStructureBuildRangeInfoKHR accelerationStructureBuildRangeInfo{};
|
||||
accelerationStructureBuildRangeInfo.primitiveCount = numTriangles;
|
||||
accelerationStructureBuildRangeInfo.primitiveOffset = 0;
|
||||
accelerationStructureBuildRangeInfo.firstVertex = 0;
|
||||
accelerationStructureBuildRangeInfo.transformOffset = i * sizeof(VkTransformMatrixKHR);
|
||||
accelerationStructureBuildRangeInfos.push_back(accelerationStructureBuildRangeInfo);
|
||||
}
|
||||
std::vector<VkAccelerationStructureBuildRangeInfoKHR*> accelerationBuildStructureRangeInfos = { &accelerationStructureBuildRangeInfos[0], &accelerationStructureBuildRangeInfos[1], &accelerationStructureBuildRangeInfos[2] };
|
||||
|
||||
if (accelerationStructureFeatures.accelerationStructureHostCommands)
|
||||
{
|
||||
// Implementation supports building acceleration structure building on host
|
||||
vkBuildAccelerationStructuresKHR(
|
||||
device,
|
||||
VK_NULL_HANDLE,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Acceleration structure needs to be build on the device
|
||||
VkCommandBuffer commandBuffer = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
vkCmdBuildAccelerationStructuresKHR(
|
||||
commandBuffer,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
vulkanDevice->flushCommandBuffer(commandBuffer, queue);
|
||||
}
|
||||
|
||||
deleteScratchBuffer(scratchBuffer);
|
||||
}
|
||||
|
||||
/*
|
||||
The top level acceleration structure contains the scene's object instances
|
||||
*/
|
||||
void createTopLevelAccelerationStructure()
|
||||
{
|
||||
VkTransformMatrixKHR transformMatrix = {
|
||||
1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f };
|
||||
|
||||
VkAccelerationStructureInstanceKHR instance{};
|
||||
instance.transform = transformMatrix;
|
||||
instance.instanceCustomIndex = 0;
|
||||
instance.mask = 0xFF;
|
||||
instance.instanceShaderBindingTableRecordOffset = 0;
|
||||
instance.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR;
|
||||
instance.accelerationStructureReference = bottomLevelAS.deviceAddress;
|
||||
|
||||
// Buffer for instance data
|
||||
vks::Buffer instancesBuffer;
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&instancesBuffer,
|
||||
sizeof(VkAccelerationStructureInstanceKHR),
|
||||
&instance));
|
||||
|
||||
VkDeviceOrHostAddressConstKHR instanceDataDeviceAddress{};
|
||||
instanceDataDeviceAddress.deviceAddress = getBufferDeviceAddress(instancesBuffer.buffer);
|
||||
|
||||
VkAccelerationStructureGeometryKHR accelerationStructureGeometry = vks::initializers::accelerationStructureGeometryKHR();
|
||||
accelerationStructureGeometry.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
|
||||
accelerationStructureGeometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
|
||||
accelerationStructureGeometry.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
|
||||
accelerationStructureGeometry.geometry.instances.arrayOfPointers = VK_FALSE;
|
||||
accelerationStructureGeometry.geometry.instances.data = instanceDataDeviceAddress;
|
||||
|
||||
// Get size info
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationStructureBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationStructureBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
|
||||
accelerationStructureBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationStructureBuildGeometryInfo.geometryCount = 1;
|
||||
accelerationStructureBuildGeometryInfo.pGeometries = &accelerationStructureGeometry;
|
||||
|
||||
uint32_t primitive_count = 1;
|
||||
|
||||
VkAccelerationStructureBuildSizesInfoKHR accelerationStructureBuildSizesInfo = vks::initializers::accelerationStructureBuildSizesInfoKHR();
|
||||
vkGetAccelerationStructureBuildSizesKHR(
|
||||
device,
|
||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||
&accelerationStructureBuildGeometryInfo,
|
||||
&primitive_count,
|
||||
&accelerationStructureBuildSizesInfo);
|
||||
|
||||
createAccelerationStructure(topLevelAS, VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR, accelerationStructureBuildSizesInfo);
|
||||
|
||||
// Create a small scratch buffer used during build of the top level acceleration structure
|
||||
ScratchBuffer scratchBuffer = createScratchBuffer(accelerationStructureBuildSizesInfo.buildScratchSize);
|
||||
|
||||
VkAccelerationStructureBuildGeometryInfoKHR accelerationBuildGeometryInfo = vks::initializers::accelerationStructureBuildGeometryInfoKHR();
|
||||
accelerationBuildGeometryInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
|
||||
accelerationBuildGeometryInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
accelerationBuildGeometryInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
|
||||
accelerationBuildGeometryInfo.dstAccelerationStructure = topLevelAS.handle;
|
||||
accelerationBuildGeometryInfo.geometryCount = 1;
|
||||
accelerationBuildGeometryInfo.pGeometries = &accelerationStructureGeometry;
|
||||
accelerationBuildGeometryInfo.scratchData.deviceAddress = scratchBuffer.deviceAddress;
|
||||
|
||||
VkAccelerationStructureBuildRangeInfoKHR accelerationStructureBuildRangeInfo{};
|
||||
accelerationStructureBuildRangeInfo.primitiveCount = 1;
|
||||
accelerationStructureBuildRangeInfo.primitiveOffset = 0;
|
||||
accelerationStructureBuildRangeInfo.firstVertex = 0;
|
||||
accelerationStructureBuildRangeInfo.transformOffset = 0;
|
||||
std::vector<VkAccelerationStructureBuildRangeInfoKHR*> accelerationBuildStructureRangeInfos = { &accelerationStructureBuildRangeInfo };
|
||||
|
||||
if (accelerationStructureFeatures.accelerationStructureHostCommands)
|
||||
{
|
||||
// Implementation supports building acceleration structure building on host
|
||||
vkBuildAccelerationStructuresKHR(
|
||||
device,
|
||||
VK_NULL_HANDLE,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Acceleration structure needs to be build on the device
|
||||
VkCommandBuffer commandBuffer = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
vkCmdBuildAccelerationStructuresKHR(
|
||||
commandBuffer,
|
||||
1,
|
||||
&accelerationBuildGeometryInfo,
|
||||
accelerationBuildStructureRangeInfos.data());
|
||||
vulkanDevice->flushCommandBuffer(commandBuffer, queue);
|
||||
}
|
||||
|
||||
deleteScratchBuffer(scratchBuffer);
|
||||
instancesBuffer.destroy();
|
||||
}
|
||||
|
||||
/*
|
||||
Create the Shader Binding Tables that binds the programs and top-level acceleration structure
|
||||
|
||||
SBT Layout used in this sample:
|
||||
|
||||
/-----------\
|
||||
| raygen |
|
||||
|-----------|
|
||||
| miss |
|
||||
|-----------|
|
||||
| hit |
|
||||
|-----------|
|
||||
| callable0 |
|
||||
| callable1 |
|
||||
| callabel2 |
|
||||
\-----------/
|
||||
|
||||
*/
|
||||
void createShaderBindingTables() {
|
||||
const uint32_t handleSize = rayTracingPipelineProperties.shaderGroupHandleSize;
|
||||
const uint32_t handleAlignment = rayTracingPipelineProperties.shaderGroupHandleAlignment;
|
||||
const uint32_t groupCount = 3 + objectCount;
|
||||
const uint32_t sbtSize = handleSize * groupCount;
|
||||
|
||||
std::vector<uint8_t> shaderHandleStorage(sbtSize);
|
||||
VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesKHR(device, pipeline, 0, groupCount, sbtSize, shaderHandleStorage.data()));
|
||||
|
||||
createShaderBindingTable(shaderBindingTables.raygen, 1);
|
||||
createShaderBindingTable(shaderBindingTables.miss, 1);
|
||||
createShaderBindingTable(shaderBindingTables.hit, 1);
|
||||
// The callable shader binding table contains one shader handle per ray traced object
|
||||
createShaderBindingTable(shaderBindingTables.callable, objectCount);
|
||||
|
||||
// Copy handles
|
||||
memcpy(shaderBindingTables.raygen.mapped, shaderHandleStorage.data(), handleSize);
|
||||
memcpy(shaderBindingTables.miss.mapped, shaderHandleStorage.data() + handleAlignment, handleSize);
|
||||
memcpy(shaderBindingTables.hit.mapped, shaderHandleStorage.data() + handleAlignment * 2, handleSize);
|
||||
memcpy(shaderBindingTables.callable.mapped, shaderHandleStorage.data() + handleAlignment * 3, handleSize * 3);
|
||||
}
|
||||
|
||||
/*
|
||||
Create the descriptor sets used for the ray tracing dispatch
|
||||
*/
|
||||
void createDescriptorSets()
|
||||
{
|
||||
std::vector<VkDescriptorPoolSize> poolSizes = {
|
||||
{ VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 1 },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1 },
|
||||
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1 },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2 }
|
||||
};
|
||||
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 1);
|
||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolCreateInfo, nullptr, &descriptorPool));
|
||||
|
||||
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSet));
|
||||
|
||||
VkWriteDescriptorSetAccelerationStructureKHR descriptorAccelerationStructureInfo = vks::initializers::writeDescriptorSetAccelerationStructureKHR();
|
||||
descriptorAccelerationStructureInfo.accelerationStructureCount = 1;
|
||||
descriptorAccelerationStructureInfo.pAccelerationStructures = &topLevelAS.handle;
|
||||
|
||||
VkWriteDescriptorSet accelerationStructureWrite{};
|
||||
accelerationStructureWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
// The specialized acceleration structure descriptor has to be chained
|
||||
accelerationStructureWrite.pNext = &descriptorAccelerationStructureInfo;
|
||||
accelerationStructureWrite.dstSet = descriptorSet;
|
||||
accelerationStructureWrite.dstBinding = 0;
|
||||
accelerationStructureWrite.descriptorCount = 1;
|
||||
accelerationStructureWrite.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
|
||||
|
||||
VkDescriptorImageInfo storageImageDescriptor{ VK_NULL_HANDLE, storageImage.view, VK_IMAGE_LAYOUT_GENERAL };
|
||||
VkDescriptorBufferInfo vertexBufferDescriptor{ vertexBuffer.buffer, 0, VK_WHOLE_SIZE };
|
||||
VkDescriptorBufferInfo indexBufferDescriptor{ indexBuffer.buffer, 0, VK_WHOLE_SIZE };
|
||||
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0: Top level acceleration structure
|
||||
accelerationStructureWrite,
|
||||
// Binding 0: Top level acceleration structure
|
||||
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, &storageImageDescriptor),
|
||||
// Binding 1: Ray tracing result image
|
||||
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2, &ubo.descriptor),
|
||||
// Binding 1: Ray tracing result image
|
||||
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 3, &vertexBufferDescriptor),
|
||||
// Binding 1: Ray tracing result image
|
||||
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4, &indexBufferDescriptor),
|
||||
};
|
||||
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, VK_NULL_HANDLE);
|
||||
}
|
||||
|
||||
/*
|
||||
Create our ray tracing pipeline
|
||||
*/
|
||||
void createRayTracingPipeline()
|
||||
{
|
||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
||||
// Binding 0: Acceleration structure
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, 0),
|
||||
// Binding 1: Storage image
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_RAYGEN_BIT_KHR, 1),
|
||||
// Binding 2: Uniform buffer
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_MISS_BIT_KHR, 2),
|
||||
// Binding 3: Vertex buffer
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, 3),
|
||||
// Binding 4: Index buffer
|
||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, 4),
|
||||
};
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayout));
|
||||
|
||||
VkPipelineLayoutCreateInfo pPipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCI, nullptr, &pipelineLayout));
|
||||
|
||||
/*
|
||||
Setup ray tracing shader groups
|
||||
*/
|
||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
||||
|
||||
// Ray generation shader group
|
||||
{
|
||||
shaderStages.push_back(loadShader(getShadersPath() + "raytracingcallable/raygen.rgen.spv", VK_SHADER_STAGE_RAYGEN_BIT_KHR));
|
||||
VkRayTracingShaderGroupCreateInfoKHR shaderGroup = vks::initializers::rayTracingShaderGroupCreateInfoKHR();
|
||||
shaderGroup.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
|
||||
shaderGroup.generalShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
||||
shaderGroup.closestHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.anyHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.intersectionShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroups.push_back(shaderGroup);
|
||||
}
|
||||
|
||||
// Miss shader group
|
||||
{
|
||||
shaderStages.push_back(loadShader(getShadersPath() + "raytracingcallable/miss.rmiss.spv", VK_SHADER_STAGE_MISS_BIT_KHR));
|
||||
VkRayTracingShaderGroupCreateInfoKHR shaderGroup = vks::initializers::rayTracingShaderGroupCreateInfoKHR();
|
||||
shaderGroup.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
|
||||
shaderGroup.generalShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
||||
shaderGroup.closestHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.anyHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.intersectionShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroups.push_back(shaderGroup);
|
||||
}
|
||||
|
||||
// Closest hit shader group
|
||||
{
|
||||
shaderStages.push_back(loadShader(getShadersPath() + "raytracingcallable/closesthit.rchit.spv", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR));
|
||||
VkRayTracingShaderGroupCreateInfoKHR shaderGroup = vks::initializers::rayTracingShaderGroupCreateInfoKHR();
|
||||
shaderGroup.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR;
|
||||
shaderGroup.generalShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.closestHitShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
||||
shaderGroup.anyHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.intersectionShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroups.push_back(shaderGroup);
|
||||
}
|
||||
|
||||
// Callable shader group
|
||||
// This sample's hit shader will call different callable shaders depending on the geometry index, so as we render three different geometries, we'll also use three callable shaders
|
||||
for (uint32_t i = 0; i < objectCount; i++)
|
||||
{
|
||||
shaderStages.push_back(loadShader(getShadersPath() + "raytracingcallable/callable" + std::to_string(i+1) + ".rcall.spv", VK_SHADER_STAGE_CALLABLE_BIT_KHR));
|
||||
VkRayTracingShaderGroupCreateInfoKHR shaderGroup{};
|
||||
shaderGroup.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR;
|
||||
shaderGroup.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
|
||||
shaderGroup.generalShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
||||
shaderGroup.closestHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.anyHitShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroup.intersectionShader = VK_SHADER_UNUSED_KHR;
|
||||
shaderGroups.push_back(shaderGroup);
|
||||
}
|
||||
|
||||
VkRayTracingPipelineCreateInfoKHR rayTracingPipelineCI = vks::initializers::rayTracingPipelineCreateInfoKHR();
|
||||
rayTracingPipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
|
||||
rayTracingPipelineCI.pStages = shaderStages.data();
|
||||
rayTracingPipelineCI.groupCount = static_cast<uint32_t>(shaderGroups.size());
|
||||
rayTracingPipelineCI.pGroups = shaderGroups.data();
|
||||
rayTracingPipelineCI.maxPipelineRayRecursionDepth = 2;
|
||||
rayTracingPipelineCI.layout = pipelineLayout;
|
||||
VK_CHECK_RESULT(vkCreateRayTracingPipelinesKHR(device, VK_NULL_HANDLE, VK_NULL_HANDLE, 1, &rayTracingPipelineCI, nullptr, &pipeline));
|
||||
}
|
||||
|
||||
/*
|
||||
Create the uniform buffer used to pass matrices to the ray tracing ray generation shader
|
||||
*/
|
||||
void createUniformBuffer()
|
||||
{
|
||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&ubo,
|
||||
sizeof(uniformData),
|
||||
&uniformData));
|
||||
VK_CHECK_RESULT(ubo.map());
|
||||
|
||||
updateUniformBuffers();
|
||||
}
|
||||
|
||||
/*
|
||||
If the window has been resized, we need to recreate the storage image and it's descriptor
|
||||
*/
|
||||
void handleResize()
|
||||
{
|
||||
// Recreate image
|
||||
createStorageImage(swapChain.colorFormat, { width, height, 1 });
|
||||
// Update descriptor
|
||||
VkDescriptorImageInfo storageImageDescriptor{ VK_NULL_HANDLE, storageImage.view, VK_IMAGE_LAYOUT_GENERAL };
|
||||
VkWriteDescriptorSet resultImageWrite = vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, &storageImageDescriptor);
|
||||
vkUpdateDescriptorSets(device, 1, &resultImageWrite, 0, VK_NULL_HANDLE);
|
||||
}
|
||||
|
||||
/*
|
||||
Command buffer generation
|
||||
*/
|
||||
void buildCommandBuffers()
|
||||
{
|
||||
if (resized)
|
||||
{
|
||||
handleResize();
|
||||
}
|
||||
|
||||
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
||||
|
||||
VkImageSubresourceRange subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
||||
|
||||
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
||||
{
|
||||
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
||||
|
||||
/*
|
||||
Dispatch the ray tracing commands
|
||||
*/
|
||||
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline);
|
||||
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipelineLayout, 0, 1, &descriptorSet, 0, 0);
|
||||
|
||||
vkCmdTraceRaysKHR(
|
||||
drawCmdBuffers[i],
|
||||
&shaderBindingTables.raygen.stridedDeviceAddressRegion,
|
||||
&shaderBindingTables.miss.stridedDeviceAddressRegion,
|
||||
&shaderBindingTables.hit.stridedDeviceAddressRegion,
|
||||
&shaderBindingTables.callable.stridedDeviceAddressRegion,
|
||||
width,
|
||||
height,
|
||||
1);
|
||||
|
||||
/*
|
||||
Copy ray tracing output to swap chain image
|
||||
*/
|
||||
|
||||
// Prepare current swap chain image as transfer destination
|
||||
vks::tools::setImageLayout(
|
||||
drawCmdBuffers[i],
|
||||
swapChain.images[i],
|
||||
VK_IMAGE_LAYOUT_UNDEFINED,
|
||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||
subresourceRange);
|
||||
|
||||
// Prepare ray tracing output image as transfer source
|
||||
vks::tools::setImageLayout(
|
||||
drawCmdBuffers[i],
|
||||
storageImage.image,
|
||||
VK_IMAGE_LAYOUT_GENERAL,
|
||||
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
||||
subresourceRange);
|
||||
|
||||
VkImageCopy copyRegion{};
|
||||
copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
|
||||
copyRegion.srcOffset = { 0, 0, 0 };
|
||||
copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
|
||||
copyRegion.dstOffset = { 0, 0, 0 };
|
||||
copyRegion.extent = { width, height, 1 };
|
||||
vkCmdCopyImage(drawCmdBuffers[i], storageImage.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, swapChain.images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region);
|
||||
|
||||
// Transition swap chain image back for presentation
|
||||
vks::tools::setImageLayout(
|
||||
drawCmdBuffers[i],
|
||||
swapChain.images[i],
|
||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
||||
subresourceRange);
|
||||
|
||||
// Transition ray tracing output image back to general layout
|
||||
vks::tools::setImageLayout(
|
||||
drawCmdBuffers[i],
|
||||
storageImage.image,
|
||||
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
||||
VK_IMAGE_LAYOUT_GENERAL,
|
||||
subresourceRange);
|
||||
|
||||
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void updateUniformBuffers()
|
||||
{
|
||||
uniformData.projInverse = glm::inverse(camera.matrices.perspective);
|
||||
uniformData.viewInverse = glm::inverse(camera.matrices.view);
|
||||
memcpy(ubo.mapped, &uniformData, sizeof(uniformData));
|
||||
}
|
||||
|
||||
void getEnabledFeatures()
|
||||
{
|
||||
// Enable features required for ray tracing using feature chaining via pNext
|
||||
enabledBufferDeviceAddresFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES;
|
||||
enabledBufferDeviceAddresFeatures.bufferDeviceAddress = VK_TRUE;
|
||||
|
||||
enabledRayTracingPipelineFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR;
|
||||
enabledRayTracingPipelineFeatures.rayTracingPipeline = VK_TRUE;
|
||||
enabledRayTracingPipelineFeatures.pNext = &enabledBufferDeviceAddresFeatures;
|
||||
|
||||
enabledAccelerationStructureFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR;
|
||||
enabledAccelerationStructureFeatures.accelerationStructure = VK_TRUE;
|
||||
enabledAccelerationStructureFeatures.pNext = &enabledRayTracingPipelineFeatures;
|
||||
|
||||
deviceCreatepNextChain = &enabledAccelerationStructureFeatures;
|
||||
}
|
||||
|
||||
void prepare()
|
||||
{
|
||||
VulkanRaytracingSample::prepare();
|
||||
|
||||
// Create the acceleration structures used to render the ray traced scene
|
||||
createBottomLevelAccelerationStructure();
|
||||
createTopLevelAccelerationStructure();
|
||||
|
||||
createStorageImage(swapChain.colorFormat, { width, height, 1 });
|
||||
createUniformBuffer();
|
||||
createRayTracingPipeline();
|
||||
createShaderBindingTables();
|
||||
createDescriptorSets();
|
||||
buildCommandBuffers();
|
||||
prepared = true;
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
VulkanExampleBase::prepareFrame();
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||
VulkanExampleBase::submitFrame();
|
||||
}
|
||||
|
||||
virtual void render()
|
||||
{
|
||||
if (!prepared)
|
||||
return;
|
||||
draw();
|
||||
if (!paused || camera.updated)
|
||||
updateUniformBuffers();
|
||||
}
|
||||
};
|
||||
|
||||
VULKAN_EXAMPLE_MAIN()
|
||||
Loading…
Add table
Add a link
Reference in a new issue