Ray tracing texturing and alpha mapping sample
This commit is contained in:
parent
5645c2657f
commit
d0ad204606
11 changed files with 49 additions and 8 deletions
|
|
@ -1,4 +1,10 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
#version 460
|
#version 460
|
||||||
|
|
||||||
#extension GL_EXT_ray_tracing : require
|
#extension GL_EXT_ray_tracing : require
|
||||||
#extension GL_GOOGLE_include_directive : require
|
#extension GL_GOOGLE_include_directive : require
|
||||||
#extension GL_EXT_nonuniform_qualifier : require
|
#extension GL_EXT_nonuniform_qualifier : require
|
||||||
|
|
@ -10,13 +16,15 @@ hitAttributeEXT vec2 attribs;
|
||||||
|
|
||||||
layout(binding = 3, set = 0) uniform sampler2D image;
|
layout(binding = 3, set = 0) uniform sampler2D image;
|
||||||
|
|
||||||
#include "_bufferreferences.glsl"
|
#include "bufferreferences.glsl"
|
||||||
#include "_geometrytypes.glsl"
|
#include "geometrytypes.glsl"
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
Triangle tri = unpackTriangle(gl_PrimitiveID, 32);
|
Triangle tri = unpackTriangle(gl_PrimitiveID, 32);
|
||||||
vec4 color = texture(image, tri.uv);
|
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) {
|
if (color.a < 0.9) {
|
||||||
ignoreIntersectionEXT;
|
ignoreIntersectionEXT;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
data/shaders/glsl/raytracingtextures/anyhit.rahit.spv
Normal file
BIN
data/shaders/glsl/raytracingtextures/anyhit.rahit.spv
Normal file
Binary file not shown.
|
|
@ -1,3 +1,9 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
layout(push_constant) uniform BufferReferences {
|
layout(push_constant) uniform BufferReferences {
|
||||||
uint64_t vertices;
|
uint64_t vertices;
|
||||||
uint64_t indices;
|
uint64_t indices;
|
||||||
|
|
@ -1,4 +1,11 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#version 460
|
#version 460
|
||||||
|
|
||||||
#extension GL_EXT_ray_tracing : require
|
#extension GL_EXT_ray_tracing : require
|
||||||
#extension GL_GOOGLE_include_directive : require
|
#extension GL_GOOGLE_include_directive : require
|
||||||
#extension GL_EXT_nonuniform_qualifier : require
|
#extension GL_EXT_nonuniform_qualifier : require
|
||||||
|
|
@ -9,14 +16,16 @@
|
||||||
layout(location = 0) rayPayloadInEXT vec3 hitValue;
|
layout(location = 0) rayPayloadInEXT vec3 hitValue;
|
||||||
hitAttributeEXT vec2 attribs;
|
hitAttributeEXT vec2 attribs;
|
||||||
|
|
||||||
#include "_bufferreferences.glsl"
|
layout(binding = 3, set = 0) uniform sampler2D image;
|
||||||
#include "_geometrytypes.glsl"
|
|
||||||
|
#include "bufferreferences.glsl"
|
||||||
|
#include "geometrytypes.glsl"
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
Triangle tri = unpackTriangle(gl_PrimitiveID, 32);
|
Triangle tri = unpackTriangle(gl_PrimitiveID, 32);
|
||||||
hitValue = vec3(tri.uv, 0.0f);
|
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);
|
vec4 color = texture(image, tri.uv);
|
||||||
hitValue = color.rgb;
|
hitValue = color.rgb;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
data/shaders/glsl/raytracingtextures/closesthit.rchit.spv
Normal file
BIN
data/shaders/glsl/raytracingtextures/closesthit.rchit.spv
Normal file
Binary file not shown.
|
|
@ -1,3 +1,9 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
struct Vertex
|
struct Vertex
|
||||||
{
|
{
|
||||||
vec3 pos;
|
vec3 pos;
|
||||||
|
|
@ -9,6 +15,7 @@ struct Triangle {
|
||||||
vec2 uv;
|
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 unpackTriangle(uint index, int vertexSize) {
|
||||||
Triangle tri;
|
Triangle tri;
|
||||||
const uint triIndex = index * 3;
|
const uint triIndex = index * 3;
|
||||||
|
|
@ -1,3 +1,9 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#version 460
|
#version 460
|
||||||
#extension GL_EXT_ray_tracing : enable
|
#extension GL_EXT_ray_tracing : enable
|
||||||
|
|
||||||
|
|
|
||||||
BIN
data/shaders/glsl/raytracingtextures/miss.rmiss.spv
Normal file
BIN
data/shaders/glsl/raytracingtextures/miss.rmiss.spv
Normal file
Binary file not shown.
|
|
@ -1,3 +1,9 @@
|
||||||
|
/* Copyright (c) 2023, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#version 460
|
#version 460
|
||||||
#extension GL_EXT_ray_tracing : enable
|
#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);
|
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));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
data/shaders/glsl/raytracingtextures/raygen.rgen.spv
Normal file
BIN
data/shaders/glsl/raytracingtextures/raygen.rgen.spv
Normal file
Binary file not shown.
|
|
@ -487,7 +487,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Closest hit group for doing texture lookups
|
// 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));
|
shaderStages.push_back(loadShader(getShadersPath() + "raytracingbasic/closesthit.rchit.spv", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR));
|
||||||
VkRayTracingShaderGroupCreateInfoKHR shaderGroup{};
|
VkRayTracingShaderGroupCreateInfoKHR shaderGroup{};
|
||||||
|
|
@ -496,7 +495,7 @@ public:
|
||||||
shaderGroup.generalShader = VK_SHADER_UNUSED_KHR;
|
shaderGroup.generalShader = VK_SHADER_UNUSED_KHR;
|
||||||
shaderGroup.closestHitShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
shaderGroup.closestHitShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
||||||
shaderGroup.intersectionShader = VK_SHADER_UNUSED_KHR;
|
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));
|
shaderStages.push_back(loadShader(getShadersPath() + "raytracingbasic/anyhit.rahit.spv", VK_SHADER_STAGE_ANY_HIT_BIT_KHR));
|
||||||
shaderGroup.anyHitShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
shaderGroup.anyHitShader = static_cast<uint32_t>(shaderStages.size()) - 1;
|
||||||
shaderGroups.push_back(shaderGroup);
|
shaderGroups.push_back(shaderGroup);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue