Started working on a ray tracing texture mapping Sample
Uses any hit for transparency, and BDA for accessing vertex and index buffers
This commit is contained in:
parent
aa3c00a784
commit
8f1d351489
7 changed files with 835 additions and 0 deletions
|
|
@ -0,0 +1,7 @@
|
|||
layout(push_constant) uniform BufferReferences {
|
||||
uint64_t vertices;
|
||||
uint64_t indices;
|
||||
} bufferReferences;
|
||||
|
||||
layout(buffer_reference, scalar) buffer Vertices {vec4 v[]; };
|
||||
layout(buffer_reference, scalar) buffer Indices {uint i[]; };
|
||||
32
data/shaders/glsl/raytracingtextures/_geometrytypes.glsl
Normal file
32
data/shaders/glsl/raytracingtextures/_geometrytypes.glsl
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
struct Vertex
|
||||
{
|
||||
vec3 pos;
|
||||
vec2 uv;
|
||||
};
|
||||
|
||||
struct Triangle {
|
||||
Vertex vertices[3];
|
||||
vec2 uv;
|
||||
};
|
||||
|
||||
Triangle unpackTriangle(uint index, int vertexSize) {
|
||||
Triangle tri;
|
||||
const uint triIndex = index * 3;
|
||||
|
||||
Indices indices = Indices(bufferReferences.indices);
|
||||
Vertices vertices = Vertices(bufferReferences.vertices);
|
||||
|
||||
// Unpack vertices
|
||||
// Data is packed as vec4 so we can map to the glTF vertex structure from the host side
|
||||
for (uint i = 0; i < 3; i++) {
|
||||
const uint offset = indices.i[triIndex + i] * (vertexSize / 16);
|
||||
vec4 d0 = vertices.v[offset + 0]; // pos.xyz, n.x
|
||||
vec4 d1 = vertices.v[offset + 1]; // n.yz, uv.xy
|
||||
tri.vertices[i].pos = d0.xyz;
|
||||
tri.vertices[i].uv = d1.zw;
|
||||
}
|
||||
// Calculate values at barycentric coordinates
|
||||
vec3 barycentricCoords = vec3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
|
||||
tri.uv = tri.vertices[0].uv * barycentricCoords.x + tri.vertices[1].uv * barycentricCoords.y + tri.vertices[2].uv * barycentricCoords.z;
|
||||
return tri;
|
||||
}
|
||||
23
data/shaders/glsl/raytracingtextures/anyhit.rahit
Normal file
23
data/shaders/glsl/raytracingtextures/anyhit.rahit
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
#extension GL_EXT_nonuniform_qualifier : require
|
||||
#extension GL_EXT_buffer_reference2 : require
|
||||
#extension GL_EXT_scalar_block_layout : require
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
hitAttributeEXT vec2 attribs;
|
||||
|
||||
layout(binding = 3, set = 0) uniform sampler2D image;
|
||||
|
||||
#include "_bufferreferences.glsl"
|
||||
#include "_geometrytypes.glsl"
|
||||
|
||||
void main()
|
||||
{
|
||||
Triangle tri = unpackTriangle(gl_PrimitiveID, 32);
|
||||
vec4 color = texture(image, tri.uv);
|
||||
if (color.a < 0.9) {
|
||||
ignoreIntersectionEXT;
|
||||
}
|
||||
}
|
||||
22
data/shaders/glsl/raytracingtextures/closesthit.rchit
Normal file
22
data/shaders/glsl/raytracingtextures/closesthit.rchit
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
#extension GL_EXT_nonuniform_qualifier : require
|
||||
#extension GL_EXT_buffer_reference2 : require
|
||||
#extension GL_EXT_scalar_block_layout : require
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
layout(location = 0) rayPayloadInEXT vec3 hitValue;
|
||||
hitAttributeEXT vec2 attribs;
|
||||
|
||||
#include "_bufferreferences.glsl"
|
||||
#include "_geometrytypes.glsl"
|
||||
|
||||
void main()
|
||||
{
|
||||
Triangle tri = unpackTriangle(gl_PrimitiveID, 32);
|
||||
hitValue = vec3(tri.uv, 0.0f);
|
||||
vec4 color = texture(image, tri.uv);
|
||||
hitValue = color.rgb;
|
||||
|
||||
}
|
||||
9
data/shaders/glsl/raytracingtextures/miss.rmiss
Normal file
9
data/shaders/glsl/raytracingtextures/miss.rmiss
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
|
||||
layout(location = 0) rayPayloadInEXT vec3 hitValue;
|
||||
|
||||
void main()
|
||||
{
|
||||
hitValue = vec3(0.0, 0.0, 0.2);
|
||||
}
|
||||
32
data/shaders/glsl/raytracingtextures/raygen.rgen
Normal file
32
data/shaders/glsl/raytracingtextures/raygen.rgen
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
|
||||
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), 0) ;
|
||||
|
||||
float tmin = 0.001;
|
||||
float tmax = 10000.0;
|
||||
|
||||
hitValue = vec3(0.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));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue