// MIT License // // Copyright (C) 2018-2023, Tellusim Technologies Inc. https://tellusim.com/ // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #version 430 core /* */ #if !SHADOW_TARGET && (VERTEX_SHADER || FRAGMENT_SHADER) /* */ layout(row_major, binding = 0) uniform CommonParameters { mat4 projection; mat4 modelview; mat4 transform; mat4 texcoord; vec4 camera; vec4 light; float znear_alpha; float shadow_power; }; #endif /* */ #if SHADOW_TARGET #if VERTEX_SHADER layout(location = 0) in vec4 in_position; layout(row_major, binding = 0) uniform CommonParameters { mat4 projection; mat4 modelview; mat4 transform; }; /* */ void main() { gl_Position = projection * (modelview * (transform * in_position)); } #elif FRAGMENT_SHADER /* */ void main() { } #endif /* */ #elif COMPUTE_SHADER #if HORIZONTAL_FILTER layout(local_size_x = GROUP_SIZE) in; #else layout(local_size_y = GROUP_SIZE) in; #endif layout(std140, binding = 0) uniform ComputeParameters { int shadow_size; float znear_alpha; float filter_isigma; }; layout(binding = 0, set = 1) uniform texture2D in_texture; layout(binding = 1, set = 1, r32f) uniform writeonly image2D out_surface; shared float distances[GROUP_SIZE + FILTER_SIZE * 2]; /* */ void main() { int local_id = gl_LocalInvocationIndex; ivec2 global_id = gl_GlobalInvocationID.xy; #if HORIZONTAL_FILTER float shadow = texelFetch(in_texture, global_id, 0).x; float distance = znear_alpha / max(shadow, 1e-12f); distances[FILTER_SIZE + local_id] = exp(distance); [[branch]] if(local_id < FILTER_SIZE) { ivec2 texcoord = ivec2(max(global_id.x - FILTER_SIZE, 0), global_id.y); float shadow = texelFetch(in_texture, texcoord, 0).x; float distance = znear_alpha / max(shadow, 1e-12f); distances[local_id] = exp(distance); } [[branch]] if(local_id < FILTER_SIZE) { ivec2 texcoord = ivec2(min(global_id.x + GROUP_SIZE, shadow_size - 1), global_id.y); float shadow = texelFetch(in_texture, texcoord, 0).x; float distance = znear_alpha / max(shadow, 1e-12f); distances[GROUP_SIZE + FILTER_SIZE + local_id] = exp(distance); } #elif VERTICAL_FILTER float distance = texelFetch(in_texture, global_id, 0).x; distances[FILTER_SIZE + local_id] = distance; [[branch]] if(local_id < FILTER_SIZE) { ivec2 texcoord = ivec2(global_id.x, max(global_id.y - FILTER_SIZE, 0)); float distance = texelFetch(in_texture, texcoord, 0).x; distances[local_id] = distance; } [[branch]] if(local_id < FILTER_SIZE) { ivec2 texcoord = ivec2(global_id.x, min(global_id.y + GROUP_SIZE, shadow_size - 1)); float distance = texelFetch(in_texture, texcoord, 0).x; distances[GROUP_SIZE + FILTER_SIZE + local_id] = distance; } #endif memoryBarrierShared(); barrier(); float value = 0.0f; float weight = 0.0f; for(int i = -FILTER_SIZE; i <= FILTER_SIZE; i++) { float w = exp(float(i * i) * filter_isigma); value += distances[FILTER_SIZE + i + local_id] * w; weight += w; } value /= weight; imageStore(out_surface, global_id, vec4(value)); } /* */ #elif VERTEX_SHADER layout(location = 0) in vec4 in_position; layout(location = 1) in vec3 in_normal; layout(location = 0) out vec3 s_normal; layout(location = 1) out vec4 s_texcoord; layout(location = 2) out vec3 s_position; /* */ void main() { vec4 position = transform * in_position; gl_Position = projection * (modelview * position); s_normal = (transform * vec4(in_normal, 0.0f)).xyz; s_texcoord = texcoord * position; s_position = position.xyz; } #elif FRAGMENT_SHADER layout(binding = 0, set = 1) uniform texture2D in_texture; layout(binding = 0, set = 2) uniform sampler in_point_sampler; layout(binding = 1, set = 2) uniform sampler in_linear_sampler; layout(location = 0) in vec3 s_normal; layout(location = 1) in vec4 s_texcoord; layout(location = 2) in vec3 s_position; layout(location = 0) out vec4 out_color; #if CLAY_WG #pragma texture(0, point) #pragma sampler(0, point) #endif /* */ void main() { vec3 normal = normalize(s_normal); vec3 view_vector = normalize(camera.xyz - s_position); vec3 light_vector = normalize(light.xyz - s_position); float diffuse = clamp(dot(light_vector, normal), 0.0f, 1.0f) * 0.75f; float specular = pow(clamp(dot(reflect(-light_vector, normal), view_vector), 0.0f, 1.0f), 16.0f) * 0.75f; vec3 texcoord = s_texcoord.xyz / s_texcoord.w; #if CLAY_WG vec2 w = texcoord.xy * textureSize(in_texture).xy - 0.5f; w -= floor(w); vec4 shadow4 = textureGather(sampler2D(in_texture, in_point_sampler), texcoord.xy); vec2 shadow2 = mix(shadow4.wz, shadow4.xy, w.y); float shadow = mix(shadow2.x, shadow2.y, w.x); #else float shadow = textureLod(sampler2D(in_texture, in_linear_sampler), texcoord.xy, 0.0f).x; #endif shadow = pow(shadow * exp(-znear_alpha / texcoord.z), shadow_power); if(any(greaterThan(abs(texcoord.xy - 0.5f), 0.5f))) shadow = 1.0f; float color = (diffuse + specular) * clamp(shadow, 0.05f, 1.0f); out_color = vec4(vec3(pow(color, 1.0f / 2.2f)), 1.0f); } #endif