diff --git a/data/shaders/glsl/raytracingtextures/anyhit.rahit b/data/shaders/glsl/raytracingtextures/anyhit.rahit index 22c67147..eb85aa3a 100644 --- a/data/shaders/glsl/raytracingtextures/anyhit.rahit +++ b/data/shaders/glsl/raytracingtextures/anyhit.rahit @@ -1,4 +1,10 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ #version 460 + #extension GL_EXT_ray_tracing : require #extension GL_GOOGLE_include_directive : require #extension GL_EXT_nonuniform_qualifier : require @@ -10,13 +16,15 @@ hitAttributeEXT vec2 attribs; layout(binding = 3, set = 0) uniform sampler2D image; -#include "_bufferreferences.glsl" -#include "_geometrytypes.glsl" +#include "bufferreferences.glsl" +#include "geometrytypes.glsl" void main() { Triangle tri = unpackTriangle(gl_PrimitiveID, 32); vec4 color = texture(image, tri.uv); + // If the alpha value of the texture at the current UV coordinates is below a given threshold, we'll ignore this intersection + // That way ray traversal will be stopped and the miss shader will be invoked if (color.a < 0.9) { ignoreIntersectionEXT; } diff --git a/data/shaders/glsl/raytracingtextures/anyhit.rahit.spv b/data/shaders/glsl/raytracingtextures/anyhit.rahit.spv new file mode 100644 index 00000000..ac4bcc59 Binary files /dev/null and b/data/shaders/glsl/raytracingtextures/anyhit.rahit.spv differ diff --git a/data/shaders/glsl/raytracingtextures/_bufferreferences.glsl b/data/shaders/glsl/raytracingtextures/bufferreferences.glsl similarity index 74% rename from data/shaders/glsl/raytracingtextures/_bufferreferences.glsl rename to data/shaders/glsl/raytracingtextures/bufferreferences.glsl index a2d98b01..6fa238ed 100644 --- a/data/shaders/glsl/raytracingtextures/_bufferreferences.glsl +++ b/data/shaders/glsl/raytracingtextures/bufferreferences.glsl @@ -1,3 +1,9 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + layout(push_constant) uniform BufferReferences { uint64_t vertices; uint64_t indices; diff --git a/data/shaders/glsl/raytracingtextures/closesthit.rchit b/data/shaders/glsl/raytracingtextures/closesthit.rchit index 3808018c..b852d968 100644 --- a/data/shaders/glsl/raytracingtextures/closesthit.rchit +++ b/data/shaders/glsl/raytracingtextures/closesthit.rchit @@ -1,4 +1,11 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + #version 460 + #extension GL_EXT_ray_tracing : require #extension GL_GOOGLE_include_directive : require #extension GL_EXT_nonuniform_qualifier : require @@ -9,14 +16,16 @@ layout(location = 0) rayPayloadInEXT vec3 hitValue; hitAttributeEXT vec2 attribs; -#include "_bufferreferences.glsl" -#include "_geometrytypes.glsl" +layout(binding = 3, set = 0) uniform sampler2D image; + +#include "bufferreferences.glsl" +#include "geometrytypes.glsl" void main() { Triangle tri = unpackTriangle(gl_PrimitiveID, 32); hitValue = vec3(tri.uv, 0.0f); + // Fetch the color for this ray hit from the texture at the current uv coordinates vec4 color = texture(image, tri.uv); hitValue = color.rgb; - } diff --git a/data/shaders/glsl/raytracingtextures/closesthit.rchit.spv b/data/shaders/glsl/raytracingtextures/closesthit.rchit.spv new file mode 100644 index 00000000..7c950d27 Binary files /dev/null and b/data/shaders/glsl/raytracingtextures/closesthit.rchit.spv differ diff --git a/data/shaders/glsl/raytracingtextures/_geometrytypes.glsl b/data/shaders/glsl/raytracingtextures/geometrytypes.glsl similarity index 84% rename from data/shaders/glsl/raytracingtextures/_geometrytypes.glsl rename to data/shaders/glsl/raytracingtextures/geometrytypes.glsl index fdd5b89d..408dcce1 100644 --- a/data/shaders/glsl/raytracingtextures/_geometrytypes.glsl +++ b/data/shaders/glsl/raytracingtextures/geometrytypes.glsl @@ -1,3 +1,9 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + struct Vertex { vec3 pos; @@ -9,6 +15,7 @@ struct Triangle { vec2 uv; }; +// This function will unpack our vertex buffer data into a single triangle and calculates uv coordinates Triangle unpackTriangle(uint index, int vertexSize) { Triangle tri; const uint triIndex = index * 3; diff --git a/data/shaders/glsl/raytracingtextures/miss.rmiss b/data/shaders/glsl/raytracingtextures/miss.rmiss index 25633fbe..5165170c 100644 --- a/data/shaders/glsl/raytracingtextures/miss.rmiss +++ b/data/shaders/glsl/raytracingtextures/miss.rmiss @@ -1,3 +1,9 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + #version 460 #extension GL_EXT_ray_tracing : enable diff --git a/data/shaders/glsl/raytracingtextures/miss.rmiss.spv b/data/shaders/glsl/raytracingtextures/miss.rmiss.spv new file mode 100644 index 00000000..88ae284c Binary files /dev/null and b/data/shaders/glsl/raytracingtextures/miss.rmiss.spv differ diff --git a/data/shaders/glsl/raytracingtextures/raygen.rgen b/data/shaders/glsl/raytracingtextures/raygen.rgen index b6cefb8a..a3e99dc6 100644 --- a/data/shaders/glsl/raytracingtextures/raygen.rgen +++ b/data/shaders/glsl/raytracingtextures/raygen.rgen @@ -1,3 +1,9 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + #version 460 #extension GL_EXT_ray_tracing : enable @@ -28,5 +34,5 @@ void main() traceRayEXT(topLevelAS, gl_RayFlagsNoneEXT, 0xff, 0, 0, 0, origin.xyz, tmin, direction.xyz, tmax, 0); - imagexStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hitValue, 0.0)); + imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hitValue, 0.0)); } diff --git a/data/shaders/glsl/raytracingtextures/raygen.rgen.spv b/data/shaders/glsl/raytracingtextures/raygen.rgen.spv new file mode 100644 index 00000000..e39633f5 Binary files /dev/null and b/data/shaders/glsl/raytracingtextures/raygen.rgen.spv differ diff --git a/examples/raytracingtextures/raytracingtextures.cpp b/examples/raytracingtextures/raytracingtextures.cpp index dbae1084..f83bc9f9 100644 --- a/examples/raytracingtextures/raytracingtextures.cpp +++ b/examples/raytracingtextures/raytracingtextures.cpp @@ -487,7 +487,6 @@ public: } // Closest hit group for doing texture lookups - // This group also uses an anyhit shader for doing transparency { shaderStages.push_back(loadShader(getShadersPath() + "raytracingbasic/closesthit.rchit.spv", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR)); VkRayTracingShaderGroupCreateInfoKHR shaderGroup{}; @@ -496,7 +495,7 @@ public: shaderGroup.generalShader = VK_SHADER_UNUSED_KHR; shaderGroup.closestHitShader = static_cast(shaderStages.size()) - 1; shaderGroup.intersectionShader = VK_SHADER_UNUSED_KHR; - // @todo: comment + // This group also uses an anyhit shader for doing transparency (see anyhit.rahit for details) shaderStages.push_back(loadShader(getShadersPath() + "raytracingbasic/anyhit.rahit.spv", VK_SHADER_STAGE_ANY_HIT_BIT_KHR)); shaderGroup.anyHitShader = static_cast(shaderStages.size()) - 1; shaderGroups.push_back(shaderGroup);