From 1dabdc67d6e2780f5033ffd572934d786b4936d0 Mon Sep 17 00:00:00 2001 From: Ivan Avdeev Date: Fri, 3 Dec 2021 09:57:24 -0800 Subject: [PATCH] sample emissive lights textures --- ref_vk/shaders/ray.rgen | 50 ++++++++++++++++++++++++++++++++--------- ref_vk/vk_rtx.c | 2 +- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/ref_vk/shaders/ray.rgen b/ref_vk/shaders/ray.rgen index b6694aa5..4770afd3 100644 --- a/ref_vk/shaders/ray.rgen +++ b/ref_vk/shaders/ray.rgen @@ -1,4 +1,5 @@ #version 460 core +#extension GL_EXT_nonuniform_qualifier : enable #extension GL_GOOGLE_include_directive : require #include "ray_common.glsl" #include "ray_kusochki.glsl" @@ -16,6 +17,7 @@ const float throughput_threshold = 1e-3; layout (constant_id = 4) const float LIGHT_GRID_CELL_SIZE = 256.; layout (constant_id = 5) const uint MAX_LIGHT_CLUSTERS = 32768; +layout (constant_id = 6) const uint MAX_TEXTURES = 4096; layout (constant_id = 7) const uint SBT_RECORD_SIZE = 64; //const uint LIGHT_CLUSTER_SIZE = 2 + MAX_VISIBLE_POINT_LIGHTS + MAX_VISIBLE_SURFACE_LIGHTS; @@ -25,6 +27,7 @@ layout (constant_id = 7) const uint SBT_RECORD_SIZE = 64; //const uint LIGHT_CLUSTER_EMISSIVE_SURFACES_DATA_OFFSET = 3 + MAX_VISIBLE_DLIGHTS; layout(set = 0, binding = 0, rgba8) uniform image2D out_image_base_color; +layout(set = 0, binding = 6) uniform sampler2D textures[MAX_TEXTURES]; layout(set = 0, binding = 9, rgba16f) uniform image2D out_image_diffuse_gi; layout(set = 0, binding = 10, rgba16f) uniform image2D out_image_specular; layout(set = 0, binding = 11, rgba16f) uniform image2D out_image_additive; @@ -103,10 +106,19 @@ void evalSplitBRDF(vec3 N, vec3 L, vec3 V, MaterialProperties material, out vec3 #endif } +vec3 baryMix(vec3 v1, vec3 v2, vec3 v3, vec2 bary) { + return v1 * (1. - bary.x - bary.y) + v2 * bary.x + v3 * bary.y; +} + +vec2 baryMix(vec2 v1, vec2 v2, vec2 v3, vec2 bary) { + return v1 * (1. - bary.x - bary.y) + v2 * bary.x + v3 * bary.y; +} + void sampleSurfaceTriangle( vec3 color, vec3 view_dir, MaterialProperties material /* TODO BrdfData instead is supposedly more efficient */, mat4x3 emissive_transform, mat3 emissive_transform_normal, uint triangle_index, uint index_offset, uint vertex_offset, + uint kusok_index, out vec3 diffuse, out vec3 specular) { diffuse = specular = vec3(0.); @@ -122,7 +134,9 @@ void sampleSurfaceTriangle( const vec3 v3 = (emissive_transform * vec4(vertices[vi3].pos, 1.)).xyz; // TODO projected uniform sampling - const vec3 sample_pos = mix(mix(v1, v2, rand01()), v3, rand01()); + vec2 bary = vec2(rand01(), rand01()); + bary.y *= (1. - bary.x); + const vec3 sample_pos = baryMix(v1, v2, v3, bary); vec3 light_dir = sample_pos - payload_opaque.hit_pos_t.xyz; const float light_dir_normal_dot = dot(light_dir, payload_opaque.normal); @@ -158,6 +172,20 @@ void sampleSurfaceTriangle( color /= pdf; +#if 0 + { + const uint tex_index = kusochki[kusok_index].tex_base_color; + if ((KUSOK_MATERIAL_FLAG_SKYBOX & tex_index) == 0) { + const vec2 uv1 = vertices[vi1].gl_tc; + const vec2 uv2 = vertices[vi2].gl_tc; + const vec2 uv3 = vertices[vi3].gl_tc; + const vec2 uv = baryMix(uv1, uv2, uv3, bary); + + color *= texture(textures[nonuniformEXT(tex_index)], uv).rgb; + } + } +#endif + if (dot(color,color) < color_culling_threshold) #ifdef DEBUG_LIGHT_CULLING return vec3(0., 1., 0.) * color_factor; @@ -322,7 +350,7 @@ void computeLighting(vec3 throughput, vec3 view_dir, MaterialProperties material const uint triangle_index = rand_range(ekusok.triangles); vec3 ldiffuse, lspecular; - sampleSurfaceTriangle(throughput * ek.emissive, view_dir, material, emissive_transform, emissive_transform_normal, triangle_index, ekusok.index_offset, ekusok.vertex_offset, ldiffuse, lspecular); + sampleSurfaceTriangle(throughput * ek.emissive, view_dir, material, emissive_transform, emissive_transform_normal, triangle_index, ekusok.index_offset, ekusok.vertex_offset, emissive_kusok_index, ldiffuse, lspecular); diffuse += ldiffuse * sampling_light_scale; specular += lspecular * sampling_light_scale; } // for all emissive kusochki @@ -397,13 +425,13 @@ void main() { break; } -#ifdef DEBUG_LIGHT_CULLING +#if 0 //def DEBUG_LIGHT_CULLING // light clusters debugging { const ivec3 light_cell = ivec3(floor(payload_opaque.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))); if (any(greaterThanEqual(light_cell, light_grid.grid_size)) || cluster_index >= MAX_LIGHT_CLUSTERS) { - C = vec3(1., 0., 0.) * color_factor; + out_additive = vec3(1., 0., 0.) * color_factor; break; } @@ -416,15 +444,15 @@ void main() { continue; } - C = vec3(0., 0., 1.) * color_factor; + out_additive = vec3(0., 0., 1.) * color_factor; } - /* const uvec3 cellrand = pcg3d(uvec3(light_cell)); */ - /* C = .02 * color_factor * vec3( */ - /* uintToFloat01(cellrand.r), */ - /* uintToFloat01(cellrand.g), */ - /* uintToFloat01(cellrand.b)); */ - //break; + const uvec3 cellrand = pcg3d(uvec3(light_cell)); + out_additive = .2 * color_factor * vec3( + uintToFloat01(cellrand.r), + uintToFloat01(cellrand.g), + uintToFloat01(cellrand.b)); + break; } #endif diff --git a/ref_vk/vk_rtx.c b/ref_vk/vk_rtx.c index b2b2a174..335f0110 100644 --- a/ref_vk/vk_rtx.c +++ b/ref_vk/vk_rtx.c @@ -1164,7 +1164,7 @@ static void createLayouts( void ) { .binding = RayDescBinding_Textures, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .descriptorCount = MAX_TEXTURES, - .stageFlags = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_MISS_BIT_KHR, + .stageFlags = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_MISS_BIT_KHR | VK_SHADER_STAGE_RAYGEN_BIT_KHR, // FIXME on AMD using immutable samplers leads to nearest filtering ???! .pImmutableSamplers = NULL, //samplers, };