rtx: make soft shadows by random sampling dlight surface
it includes bad random function, bad sampling strategy, bad performance, etc but it works somewhat
This commit is contained in:
parent
44b93dd30b
commit
e235c97be9
|
@ -1,16 +1,18 @@
|
||||||
## 2021-03-10
|
## 2021-03-10
|
||||||
- [x] rtx: dlights
|
- [x] rtx: dlights
|
||||||
- [x] rtx: dlight shadows
|
- [x] rtx: dlight shadows
|
||||||
|
- [x] rtx: dlight soft shadows
|
||||||
|
|
||||||
# Next
|
# Next
|
||||||
- [ ] rtx: dlight soft shadows
|
- [ ] rtx: model matrices
|
||||||
|
- [ ] rtx: blend normals according to barycentrics
|
||||||
- [ ] rtx: path tracing
|
- [ ] rtx: path tracing
|
||||||
- [ ] rtx: textures
|
- [ ] rtx: textures
|
||||||
- [ ] rtx: blend normals according to barycentrics
|
|
||||||
- [ ] rtx: light entities
|
- [ ] rtx: light entities
|
||||||
- [ ] rtx: add fps
|
- [ ] rtx: add fps
|
||||||
- [ ] better AS structure (fewer blases, etc)
|
- [ ] better AS structure (fewer blases, etc)
|
||||||
- [ ] rasterize into G-buffer, and only then compute lighting with rtx
|
- [ ] rasterize into G-buffer, and only then compute lighting with rtx
|
||||||
|
- [ ] rtx: better random
|
||||||
|
|
||||||
# Planned
|
# Planned
|
||||||
- [ ] restore render debug labels
|
- [ ] restore render debug labels
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#extension GL_EXT_ray_query : require
|
#extension GL_EXT_ray_query : require
|
||||||
#extension GL_EXT_shader_16bit_storage : require
|
#extension GL_EXT_shader_16bit_storage : require
|
||||||
|
|
||||||
|
|
||||||
// FIXME shader specialization
|
// FIXME shader specialization
|
||||||
layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
|
layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
|
||||||
|
|
||||||
|
@ -38,7 +37,7 @@ struct Light {
|
||||||
vec4 color;
|
vec4 color;
|
||||||
};
|
};
|
||||||
// FIXME what should this be?
|
// FIXME what should this be?
|
||||||
const float dlight_attenuation_const = 5000.;
|
const float dlight_attenuation_const = 20000.;
|
||||||
// TODO specialize in vk_rtx.c
|
// TODO specialize in vk_rtx.c
|
||||||
layout (constant_id = 1) const uint max_dlights = 32;
|
layout (constant_id = 1) const uint max_dlights = 32;
|
||||||
layout(set=0,binding=6) uniform UBODLights {
|
layout(set=0,binding=6) uniform UBODLights {
|
||||||
|
@ -46,6 +45,16 @@ layout(set=0,binding=6) uniform UBODLights {
|
||||||
Light lights[max_dlights];
|
Light lights[max_dlights];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO find better random function
|
||||||
|
float rand01_state;
|
||||||
|
float rand01() {
|
||||||
|
return rand01_state = fract(sin(rand01_state)*54873.35729);
|
||||||
|
}
|
||||||
|
|
||||||
|
layout (push_constant) uniform PC {
|
||||||
|
float t;
|
||||||
|
} pc;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 res = imageSize(image);
|
vec2 res = imageSize(image);
|
||||||
vec2 uv = (gl_GlobalInvocationID.xy + .5) / res * 2. - 1.;
|
vec2 uv = (gl_GlobalInvocationID.xy + .5) / res * 2. - 1.;
|
||||||
|
@ -66,7 +75,6 @@ void main() {
|
||||||
C = vec3(1., 0., 1.);
|
C = vec3(1., 0., 1.);
|
||||||
} else {
|
} else {
|
||||||
vec3 pos = O+D*l;
|
vec3 pos = O+D*l;
|
||||||
//C = fract(pos/100.);
|
|
||||||
|
|
||||||
//const int instance_index = rayQueryGetIntersectionInstanceIdEXT(rayQuery, true);
|
//const int instance_index = rayQueryGetIntersectionInstanceIdEXT(rayQuery, true);
|
||||||
const int instance_index = rayQueryGetIntersectionInstanceCustomIndexEXT(rayQuery, true);
|
const int instance_index = rayQueryGetIntersectionInstanceCustomIndexEXT(rayQuery, true);
|
||||||
|
@ -94,10 +102,21 @@ void main() {
|
||||||
/* C.b = float((first_vertex_index >> 10) & 31) / 31.f; */
|
/* C.b = float((first_vertex_index >> 10) & 31) / 31.f; */
|
||||||
|
|
||||||
const vec3 baseColor = vec3(1.);
|
const vec3 baseColor = vec3(1.);
|
||||||
|
rand01_state = fract((pos.x + pos.y + pos.z)/100.) + uv.x + uv.y + pc.t;
|
||||||
for (uint i = 0; i < num_lights; ++i) {
|
for (uint i = 0; i < num_lights; ++i) {
|
||||||
const vec4 light_pos_r = lights[i].pos_r;
|
const vec4 light_pos_r = lights[i].pos_r;
|
||||||
const vec3 light_color = lights[i].color.rgb;
|
const vec3 light_color = lights[i].color.rgb;
|
||||||
const vec3 light_dir = light_pos_r.xyz - pos;
|
|
||||||
|
rand01_state += fract((light_pos_r.x + light_pos_r.y + light_pos_r.z)/100.);
|
||||||
|
|
||||||
|
// Find random point on a sphere
|
||||||
|
// TODO proper BRDF importance sampling and correct random point distribution
|
||||||
|
vec3 rnd = normalize(vec3(rand01()*2.-1., rand01()*2.-1., rand01()*2.-1.));
|
||||||
|
if (dot(rnd, pos - light_pos_r.xyz) < 0.) rnd = -rnd;
|
||||||
|
|
||||||
|
// TODO fudge this
|
||||||
|
const float light_r_scaler = 10.;
|
||||||
|
const vec3 light_dir = light_pos_r.xyz - pos + rnd * light_pos_r.w / light_r_scaler;
|
||||||
const vec3 light_dir_norm = normalize(light_dir);
|
const vec3 light_dir_norm = normalize(light_dir);
|
||||||
const float dot_ld_norm = dot(light_dir_norm, normal);
|
const float dot_ld_norm = dot(light_dir_norm, normal);
|
||||||
if (dot_ld_norm <= 0.)
|
if (dot_ld_norm <= 0.)
|
||||||
|
@ -116,6 +135,7 @@ void main() {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const float r2 = light_pos_r.w * light_pos_r.w;
|
const float r2 = light_pos_r.w * light_pos_r.w;
|
||||||
|
// TODO this is a bad approximation
|
||||||
const float attenuation = dlight_attenuation_const / (d2 + r2 * .5);
|
const float attenuation = dlight_attenuation_const / (d2 + r2 * .5);
|
||||||
C += baseColor.rgb * light_color * dot_ld_norm * attenuation;
|
C += baseColor.rgb * light_color * dot_ld_norm * attenuation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -422,7 +422,10 @@ void VK_RaySceneEnd(const vk_ray_scene_render_args_t* args)
|
||||||
|
|
||||||
// 4. dispatch compute
|
// 4. dispatch compute
|
||||||
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, g_rtx.pipeline);
|
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, g_rtx.pipeline);
|
||||||
//vkCmdPushConstants(cmdbuf, g_rtx.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(matrix4x4), mvp);
|
{
|
||||||
|
const float t = gpGlobals->realtime;
|
||||||
|
vkCmdPushConstants(cmdbuf, g_rtx.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(float), &t);
|
||||||
|
}
|
||||||
vkCmdBindDescriptorSets(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, g_rtx.pipeline_layout, 0, 1, &g_rtx.desc_set, 0, NULL);
|
vkCmdBindDescriptorSets(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, g_rtx.pipeline_layout, 0, 1, &g_rtx.desc_set, 0, NULL);
|
||||||
vkCmdDispatch(cmdbuf, (args->dst.width+WG_W-1)/WG_W, (args->dst.height+WG_H-1)/WG_H, 1);
|
vkCmdDispatch(cmdbuf, (args->dst.width+WG_W-1)/WG_W, (args->dst.height+WG_H-1)/WG_H, 1);
|
||||||
}
|
}
|
||||||
|
@ -470,18 +473,18 @@ static void createLayouts( void ) {
|
||||||
|
|
||||||
XVK_CHECK(vkCreateDescriptorSetLayout(vk_core.device, &dslci, NULL, &g_rtx.desc_layout));
|
XVK_CHECK(vkCreateDescriptorSetLayout(vk_core.device, &dslci, NULL, &g_rtx.desc_layout));
|
||||||
|
|
||||||
/* VkPushConstantRange push_const = {0}; */
|
VkPushConstantRange push_const = {0};
|
||||||
/* push_const.offset = 0; */
|
push_const.offset = 0;
|
||||||
/* push_const.size = sizeof(matrix4x4); */
|
push_const.size = sizeof(float);
|
||||||
/* push_const.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; */
|
push_const.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
|
||||||
|
|
||||||
{
|
{
|
||||||
VkPipelineLayoutCreateInfo plci = {0};
|
VkPipelineLayoutCreateInfo plci = {0};
|
||||||
plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||||
plci.setLayoutCount = 1;
|
plci.setLayoutCount = 1;
|
||||||
plci.pSetLayouts = &g_rtx.desc_layout;
|
plci.pSetLayouts = &g_rtx.desc_layout;
|
||||||
//plci.pushConstantRangeCount = 1;
|
plci.pushConstantRangeCount = 1;
|
||||||
//plci.pPushConstantRanges = &push_const;
|
plci.pPushConstantRanges = &push_const;
|
||||||
XVK_CHECK(vkCreatePipelineLayout(vk_core.device, &plci, NULL, &g_rtx.pipeline_layout));
|
XVK_CHECK(vkCreatePipelineLayout(vk_core.device, &plci, NULL, &g_rtx.pipeline_layout));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue