rtx: add shadows

This commit is contained in:
Ivan Avdeev 2021-07-12 10:59:52 -07:00
parent 7d6ee9623b
commit caa284f835
3 changed files with 47 additions and 11 deletions

View File

@ -85,6 +85,17 @@ float rand01() {
} }
layout(location = 0) rayPayloadEXT RayPayload payload; layout(location = 0) rayPayloadEXT RayPayload payload;
layout(location = 1) rayPayloadEXT bool shadow;
bool shadowed(vec3 pos, vec3 dir, float dist) {
shadow = true;
traceRayEXT(tlas,
gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsOpaqueEXT | gl_RayFlagsSkipClosestHitShaderEXT,
0xff,
0, 0, 1 /* miss index */,
pos, 0., dir, dist, 1 /* payload location */);
return shadow;
}
void main() { void main() {
vec2 uv = (gl_LaunchIDEXT.xy + .5) / gl_LaunchSizeEXT.xy * 2. - 1.; vec2 uv = (gl_LaunchIDEXT.xy + .5) / gl_LaunchSizeEXT.xy * 2. - 1.;
@ -108,6 +119,7 @@ void main() {
if (payload.hit_pos_t.w > 0.) { if (payload.hit_pos_t.w > 0.) {
const int bounce = 0; const int bounce = 0;
const vec3 kc = vec3(1.); const vec3 kc = vec3(1.);
//rand01_state = uint(mod(pc.t, 100.) * 1000.) + gl_GlobalInvocationID.x * 1823 + 31337 * gl_GlobalInvocationID.y;
const ivec3 light_cell = ivec3(floor(payload.hit_pos_t.xyz / LIGHT_GRID_CELL_SIZE)) - light_grid.grid_min; const ivec3 light_cell = ivec3(floor(payload.hit_pos_t.xyz / LIGHT_GRID_CELL_SIZE)) - light_grid.grid_min;
const uint cluster_index = uint(dot(light_cell, ivec3(1, light_grid.grid_size.x, light_grid.grid_size.x * light_grid.grid_size.y))); const uint cluster_index = uint(dot(light_cell, ivec3(1, light_grid.grid_size.x, light_grid.grid_size.x * light_grid.grid_size.y)));
@ -211,10 +223,11 @@ void main() {
if (light_dir_normal_dot <= 0.) if (light_dir_normal_dot <= 0.)
continue; continue;
// if (shadowed(pos, light_dir, light_dist - shadow_offset_fudge)) { const float shadow_offset_fudge = .5;
// //C = vec3(0., 1., 0.); if (shadowed(payload.hit_pos_t.xyz, light_dir, light_dist - shadow_offset_fudge)) {
// continue; //C = vec3(0., 1., 0.);
// } continue;
}
// TODO // TODO
const float brightness_fudge = 50.; const float brightness_fudge = 50.;

View File

@ -0,0 +1,8 @@
#version 460 core
#extension GL_EXT_ray_tracing: require
layout(location = 1) rayPayloadInEXT bool shadow;
void main() {
shadow = false;
}

View File

@ -20,7 +20,7 @@
#define MAX_LIGHT_LEAVES 8192 #define MAX_LIGHT_LEAVES 8192
#define SBT_SIZE 3 #define SBT_SIZE 4
// TODO settings/realtime modifiable/adaptive // TODO settings/realtime modifiable/adaptive
#define FRAME_WIDTH 1280 #define FRAME_WIDTH 1280
@ -364,6 +364,13 @@ static void createPipeline( void )
//.pSpecializationInfo = &spec, //.pSpecializationInfo = &spec,
.pName = "main", .pName = "main",
}, },
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_MISS_BIT_KHR,
.module = loadShader("shadow.rmiss.spv"),
//.pSpecializationInfo = &spec,
.pName = "main",
},
{ {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, .stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR,
@ -390,11 +397,19 @@ static void createPipeline( void )
.generalShader = 1, // miss stage index; FIXME enum .generalShader = 1, // miss stage index; FIXME enum
.intersectionShader = VK_SHADER_UNUSED_KHR, .intersectionShader = VK_SHADER_UNUSED_KHR,
}, },
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.generalShader = 2, // shadow miss stage index; FIXME enum
.intersectionShader = VK_SHADER_UNUSED_KHR,
},
{ {
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR, .type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR, .anyHitShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = 2, // FIXME enum index .closestHitShader = 3, // FIXME enum index
.generalShader = VK_SHADER_UNUSED_KHR, .generalShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR, .intersectionShader = VK_SHADER_UNUSED_KHR,
}, },
@ -647,14 +662,14 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
{ {
const uint32_t sbt_record_size = g_rtx.sbt_record_size; const uint32_t sbt_record_size = g_rtx.sbt_record_size;
//const uint32_t sbt_record_size = vk_core.physical_device.properties_ray_tracing_pipeline.shaderGroupHandleSize; //const uint32_t sbt_record_size = vk_core.physical_device.properties_ray_tracing_pipeline.shaderGroupHandleSize;
#define SBT_INDEX(index) { \ #define SBT_INDEX(index, count) { \
.deviceAddress = getBufferDeviceAddress(g_rtx.sbt_buffer.buffer) + g_rtx.sbt_record_size * index, \ .deviceAddress = getBufferDeviceAddress(g_rtx.sbt_buffer.buffer) + g_rtx.sbt_record_size * index, \
.size = sbt_record_size, \ .size = sbt_record_size * count, \
.stride = sbt_record_size, \ .stride = sbt_record_size, \
} }
const VkStridedDeviceAddressRegionKHR sbt_raygen = SBT_INDEX(0); const VkStridedDeviceAddressRegionKHR sbt_raygen = SBT_INDEX(0, 1);
const VkStridedDeviceAddressRegionKHR sbt_miss = SBT_INDEX(1); const VkStridedDeviceAddressRegionKHR sbt_miss = SBT_INDEX(1, 2);
const VkStridedDeviceAddressRegionKHR sbt_hit = SBT_INDEX(2); const VkStridedDeviceAddressRegionKHR sbt_hit = SBT_INDEX(3, 1);
const VkStridedDeviceAddressRegionKHR sbt_callable = { 0 }; const VkStridedDeviceAddressRegionKHR sbt_callable = { 0 };
vkCmdTraceRaysKHR(cmdbuf, &sbt_raygen, &sbt_miss, &sbt_hit, &sbt_callable, FRAME_WIDTH, FRAME_HEIGHT, 1 ); vkCmdTraceRaysKHR(cmdbuf, &sbt_raygen, &sbt_miss, &sbt_hit, &sbt_callable, FRAME_WIDTH, FRAME_HEIGHT, 1 );