Crafter.Graphics/examples/VulkanTriangle/raygen.glsl

50 lines
1.3 KiB
Text
Raw Normal View History

2026-01-28 23:37:12 +01:00
#version 460
#extension GL_EXT_ray_tracing : enable
#extension GL_EXT_shader_image_load_formatted : enable
2026-04-05 22:53:59 +02:00
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable
#extension GL_EXT_descriptor_heap : enable
#extension GL_EXT_nonuniform_qualifier : enable
2026-01-28 23:37:12 +01:00
2026-04-05 22:53:59 +02:00
layout(constant_id = 0) const uint16_t bufferStart = 0us;
layout(descriptor_heap) uniform accelerationStructureEXT topLevelAS[];
layout(descriptor_heap) uniform writeonly image2D image[];
2026-01-28 23:37:12 +01:00
layout(location = 0) rayPayloadEXT vec3 hitValue;
2026-04-05 22:53:59 +02:00
void main() {
2026-01-29 19:18:47 +01:00
// Pixel coordinates
uvec2 pixel = gl_LaunchIDEXT.xy;
uvec2 resolution = gl_LaunchSizeEXT.xy;
// Normalized coordinates in range [-1, 1]
vec2 uv = (vec2(pixel) + 0.5) / vec2(resolution);
vec2 ndc = uv * 2.0 - 1.0;
// Camera parameters
vec3 origin = vec3(0.0, 0.0, -300.0);
float aspect = float(resolution.x) / float(resolution.y);
float fov = radians(60.0);
float tanHalfFov = tan(fov * 0.5);
// Simple pinhole camera facing +Z
vec3 direction = normalize(vec3(
ndc.x * aspect * tanHalfFov,
-ndc.y * tanHalfFov,
1.0
));
traceRayEXT(
2026-04-05 22:53:59 +02:00
topLevelAS[bufferStart],
2026-01-29 19:18:47 +01:00
gl_RayFlagsNoneEXT,
0xff,
0, 0, 0,
origin,
0.001,
direction,
10000.0,
0
);
2026-04-05 22:53:59 +02:00
imageStore(image[0], ivec2(pixel), vec4(hitValue, 1));
2026-01-29 19:18:47 +01:00
}