From c3fee7ba6cdefd285255b9e1198bc1fdf585b1ac Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Sun, 15 Sep 2019 19:58:38 +1000 Subject: Add shader based background mode --- servers/visual/rasterizer_rd/shaders/sky.glsl | 42 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'servers/visual/rasterizer_rd/shaders') diff --git a/servers/visual/rasterizer_rd/shaders/sky.glsl b/servers/visual/rasterizer_rd/shaders/sky.glsl index 28fd2883c3..d536b9dbd5 100644 --- a/servers/visual/rasterizer_rd/shaders/sky.glsl +++ b/servers/visual/rasterizer_rd/shaders/sky.glsl @@ -49,6 +49,22 @@ layout(push_constant, binding = 1, std430) uniform Params { } params; +#ifdef USE_MATERIAL_UNIFORMS +layout(set = 3, binding = 0, std140) uniform MaterialUniforms{ + /* clang-format off */ + +MATERIAL_UNIFORMS + + /* clang-format on */ +} material; +#endif + +/* clang-format off */ + +FRAGMENT_SHADER_GLOBALS + +/* clang-format on */ + vec4 texturePanorama(sampler2D pano, vec3 normal) { vec2 st = vec2( @@ -68,12 +84,34 @@ layout(location = 0) out vec4 frag_color; void main() { vec3 cube_normal; - cube_normal.z = -1000000.0; + cube_normal.z = -1.0; cube_normal.x = (cube_normal.z * (-uv_interp.x - params.proj.x)) / params.proj.y; cube_normal.y = -(cube_normal.z * (-uv_interp.y - params.proj.z)) / params.proj.w; cube_normal = mat3(params.orientation) * cube_normal; cube_normal.z = -cube_normal.z; - frag_color.rgb = texturePanorama(source_panorama, normalize(cube_normal.xyz)).rgb; + vec3 color = vec3(0.0, 0.0, 0.0); + + // unused, just here to make our compiler happy, make sure we don't execute any light code the user adds in.. +#ifndef REALLYINCLUDETHIS + { + /* clang-format off */ + +LIGHT_SHADER_CODE + + /* clang-format on */ + } +#endif + + // color = texturePanorama(source_panorama, normalize(cube_normal.xyz)).rgb; + { + /* clang-format off */ + +FRAGMENT_SHADER_CODE + + /* clang-format on */ + } + + frag_color.rgb = color; frag_color.a = params.alpha; } -- cgit v1.2.3 From 61a74739ca2d201e7e057d85aa99ae68f0500c33 Mon Sep 17 00:00:00 2001 From: clayjohn Date: Thu, 19 Mar 2020 17:32:19 -0700 Subject: Working sky shader implementation --- .../rasterizer_rd/shaders/cubemap_downsampler.glsl | 80 +++++++--------------- .../rasterizer_rd/shaders/cubemap_filter.glsl | 38 ++++++++++ .../rasterizer_rd/shaders/cubemap_roughness.glsl | 37 ---------- servers/visual/rasterizer_rd/shaders/sky.glsl | 70 ++++++++++--------- 4 files changed, 101 insertions(+), 124 deletions(-) (limited to 'servers/visual/rasterizer_rd/shaders') diff --git a/servers/visual/rasterizer_rd/shaders/cubemap_downsampler.glsl b/servers/visual/rasterizer_rd/shaders/cubemap_downsampler.glsl index b042dc8868..08abe3f2c9 100644 --- a/servers/visual/rasterizer_rd/shaders/cubemap_downsampler.glsl +++ b/servers/visual/rasterizer_rd/shaders/cubemap_downsampler.glsl @@ -30,13 +30,7 @@ VERSION_DEFINES layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in; /* clang-format on */ -#ifdef MODE_SOURCE_PANORAMA -layout(set = 0, binding = 0) uniform sampler2D source_panorama; -#endif - -#ifdef MODE_SOURCE_CUBEMAP layout(set = 0, binding = 0) uniform samplerCube source_cubemap; -#endif layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly imageCube dest_cubemap; @@ -83,32 +77,6 @@ float calcWeight(float u, float v) { return val * sqrt(val); } -#ifdef MODE_SOURCE_PANORAMA - -vec4 texturePanorama(vec3 normal, sampler2D pano) { - - vec2 st = vec2( - atan(normal.x, -normal.z), - acos(normal.y)); - - if (st.x < 0.0) - st.x += M_PI * 2.0; - - st /= vec2(M_PI * 2.0, M_PI); - - return textureLod(pano, st, 0.0); -} - -#endif - -vec4 get_texture(vec3 p_dir) { -#ifdef MODE_SOURCE_PANORAMA - return texturePanorama(normalize(p_dir), source_panorama); -#else - return textureLod(source_cubemap, normalize(p_dir), 0.0); -#endif -} - void main() { uvec3 id = gl_GlobalInvocationID; uint face_size = params.face_size; @@ -138,81 +106,81 @@ void main() { switch (id.z) { case 0: get_dir_0(dir, u0, v0); - color = get_texture(dir) * weights[0]; + color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0]; get_dir_0(dir, u1, v0); - color += get_texture(dir) * weights[1]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1]; get_dir_0(dir, u0, v1); - color += get_texture(dir) * weights[2]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2]; get_dir_0(dir, u1, v1); - color += get_texture(dir) * weights[3]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3]; break; case 1: get_dir_1(dir, u0, v0); - color = get_texture(dir) * weights[0]; + color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0]; get_dir_1(dir, u1, v0); - color += get_texture(dir) * weights[1]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1]; get_dir_1(dir, u0, v1); - color += get_texture(dir) * weights[2]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2]; get_dir_1(dir, u1, v1); - color += get_texture(dir) * weights[3]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3]; break; case 2: get_dir_2(dir, u0, v0); - color = get_texture(dir) * weights[0]; + color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0]; get_dir_2(dir, u1, v0); - color += get_texture(dir) * weights[1]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1]; get_dir_2(dir, u0, v1); - color += get_texture(dir) * weights[2]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2]; get_dir_2(dir, u1, v1); - color += get_texture(dir) * weights[3]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3]; break; case 3: get_dir_3(dir, u0, v0); - color = get_texture(dir) * weights[0]; + color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0]; get_dir_3(dir, u1, v0); - color += get_texture(dir) * weights[1]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1]; get_dir_3(dir, u0, v1); - color += get_texture(dir) * weights[2]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2]; get_dir_3(dir, u1, v1); - color += get_texture(dir) * weights[3]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3]; break; case 4: get_dir_4(dir, u0, v0); - color = get_texture(dir) * weights[0]; + color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0]; get_dir_4(dir, u1, v0); - color += get_texture(dir) * weights[1]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1]; get_dir_4(dir, u0, v1); - color += get_texture(dir) * weights[2]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2]; get_dir_4(dir, u1, v1); - color += get_texture(dir) * weights[3]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3]; break; default: get_dir_5(dir, u0, v0); - color = get_texture(dir) * weights[0]; + color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0]; get_dir_5(dir, u1, v0); - color += get_texture(dir) * weights[1]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1]; get_dir_5(dir, u0, v1); - color += get_texture(dir) * weights[2]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2]; get_dir_5(dir, u1, v1); - color += get_texture(dir) * weights[3]; + color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3]; break; } imageStore(dest_cubemap, ivec3(id), color); diff --git a/servers/visual/rasterizer_rd/shaders/cubemap_filter.glsl b/servers/visual/rasterizer_rd/shaders/cubemap_filter.glsl index a7e51c1489..b0e97ca803 100644 --- a/servers/visual/rasterizer_rd/shaders/cubemap_filter.glsl +++ b/servers/visual/rasterizer_rd/shaders/cubemap_filter.glsl @@ -262,28 +262,66 @@ void main() { // write color color.xyz = max(vec3(0.0), color.xyz); color.w = 1.0; +#ifdef USE_TEXTURE_ARRAY + id.xy *= uvec2(2, 2); +#endif switch (level) { case 0: imageStore(dest_cubemap0, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap0, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap0, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap0, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; case 1: imageStore(dest_cubemap1, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap1, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap1, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap1, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; case 2: imageStore(dest_cubemap2, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap2, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap2, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap2, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; case 3: imageStore(dest_cubemap3, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap3, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap3, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap3, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; case 4: imageStore(dest_cubemap4, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap4, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap4, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap4, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; case 5: imageStore(dest_cubemap5, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap5, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap5, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap5, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; default: imageStore(dest_cubemap6, ivec3(id), color); +#ifdef USE_TEXTURE_ARRAY + imageStore(dest_cubemap6, ivec3(id) + ivec3(1.0, 0.0, 0.0), color); + imageStore(dest_cubemap6, ivec3(id) + ivec3(0.0, 1.0, 0.0), color); + imageStore(dest_cubemap6, ivec3(id) + ivec3(1.0, 1.0, 0.0), color); +#endif break; } } \ No newline at end of file diff --git a/servers/visual/rasterizer_rd/shaders/cubemap_roughness.glsl b/servers/visual/rasterizer_rd/shaders/cubemap_roughness.glsl index 3dba143e56..e85996fa1a 100644 --- a/servers/visual/rasterizer_rd/shaders/cubemap_roughness.glsl +++ b/servers/visual/rasterizer_rd/shaders/cubemap_roughness.glsl @@ -10,13 +10,7 @@ VERSION_DEFINES layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE, local_size_z = 1) in; /* clang-format on */ -#ifdef MODE_SOURCE_PANORAMA -layout(set = 0, binding = 0) uniform sampler2D source_panorama; -#endif - -#ifdef MODE_SOURCE_CUBEMAP layout(set = 0, binding = 0) uniform samplerCube source_cube; -#endif layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly imageCube dest_cubemap; @@ -115,24 +109,6 @@ vec2 Hammersley(uint i, uint N) { return vec2(float(i) / float(N), radicalInverse_VdC(i)); } -#ifdef MODE_SOURCE_PANORAMA - -vec4 texturePanorama(vec3 normal, sampler2D pano) { - - vec2 st = vec2( - atan(normal.x, -normal.z), - acos(normal.y)); - - if (st.x < 0.0) - st.x += M_PI * 2.0; - - st /= vec2(M_PI * 2.0, M_PI); - - return textureLod(pano, st, 0.0); -} - -#endif - void main() { uvec3 id = gl_GlobalInvocationID; id.z += params.face_id; @@ -144,15 +120,7 @@ void main() { if (params.use_direct_write) { -#ifdef MODE_SOURCE_PANORAMA - imageStore(dest_cubemap, ivec3(id), vec4(texturePanorama(N, source_panorama).rgb, 1.0)); -#endif - -#ifdef MODE_SOURCE_CUBEMAP imageStore(dest_cubemap, ivec3(id), vec4(texture(source_cube, N).rgb, 1.0)); - -#endif - } else { vec4 sum = vec4(0.0, 0.0, 0.0, 0.0); @@ -167,13 +135,8 @@ void main() { float ndotl = clamp(dot(N, L), 0.0, 1.0); if (ndotl > 0.0) { -#ifdef MODE_SOURCE_PANORAMA - sum.rgb += texturePanorama(L, source_panorama).rgb * ndotl; -#endif -#ifdef MODE_SOURCE_CUBEMAP sum.rgb += textureLod(source_cube, L, 0.0).rgb * ndotl; -#endif sum.a += ndotl; } } diff --git a/servers/visual/rasterizer_rd/shaders/sky.glsl b/servers/visual/rasterizer_rd/shaders/sky.glsl index d536b9dbd5..b73f9345e7 100644 --- a/servers/visual/rasterizer_rd/shaders/sky.glsl +++ b/servers/visual/rasterizer_rd/shaders/sky.glsl @@ -11,10 +11,8 @@ layout(location = 0) out vec2 uv_interp; layout(push_constant, binding = 1, std430) uniform Params { mat3 orientation; vec4 proj; - float multiplier; - float alpha; - float depth; - float pad; + vec4 position_multiplier; + float time; } params; @@ -22,7 +20,7 @@ void main() { vec2 base_arr[4] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 1.0), vec2(1.0, 1.0), vec2(1.0, -1.0)); uv_interp = base_arr[gl_VertexIndex]; - gl_Position = vec4(uv_interp, params.depth, 1.0); + gl_Position = vec4(uv_interp, 1.0, 1.0); } /* clang-format off */ @@ -37,20 +35,18 @@ VERSION_DEFINES layout(location = 0) in vec2 uv_interp; /* clang-format on */ -layout(set = 0, binding = 0) uniform sampler2D source_panorama; - layout(push_constant, binding = 1, std430) uniform Params { mat3 orientation; vec4 proj; - float multiplier; - float alpha; - float depth; - float pad; + vec4 position_multiplier; + float time; //TODO consider adding vec2 screen res, and float radiance size } params; +layout(set = 0, binding = 0) uniform sampler material_samplers[12]; + #ifdef USE_MATERIAL_UNIFORMS -layout(set = 3, binding = 0, std140) uniform MaterialUniforms{ +layout(set = 1, binding = 0, std140) uniform MaterialUniforms{ /* clang-format off */ MATERIAL_UNIFORMS @@ -59,25 +55,27 @@ MATERIAL_UNIFORMS } material; #endif -/* clang-format off */ +layout(set = 2, binding = 0) uniform textureCube radiance; +layout(set = 2, binding = 1) uniform texture2D half_res; +layout(set = 2, binding = 2) uniform texture2D quarter_res; -FRAGMENT_SHADER_GLOBALS +struct DirectionalLightData { + vec3 direction; + float energy; + vec3 color; + bool enabled; +}; -/* clang-format on */ - -vec4 texturePanorama(sampler2D pano, vec3 normal) { - - vec2 st = vec2( - atan(normal.x, normal.z), - acos(normal.y)); +layout(set = 3, binding = 0, std140) uniform DirectionalLights { + DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; +} +directional_lights; - if (st.x < 0.0) - st.x += M_PI * 2.0; +/* clang-format off */ - st /= vec2(M_PI * 2.0, M_PI); +FRAGMENT_SHADER_GLOBALS - return texture(pano, st); -} +/* clang-format on */ layout(location = 0) out vec4 frag_color; @@ -89,10 +87,22 @@ void main() { cube_normal.y = -(cube_normal.z * (-uv_interp.y - params.proj.z)) / params.proj.w; cube_normal = mat3(params.orientation) * cube_normal; cube_normal.z = -cube_normal.z; + cube_normal = normalize(cube_normal); + + vec2 uv = uv_interp * 0.5 + 0.5; + + vec2 panorama_coords = vec2(atan(cube_normal.x, cube_normal.z), acos(cube_normal.y)); + + if (panorama_coords.x < 0.0) { + panorama_coords.x += M_PI * 2.0; + } + + panorama_coords /= vec2(M_PI * 2.0, M_PI); vec3 color = vec3(0.0, 0.0, 0.0); + float alpha = 1.0; // Only available to subpasses - // unused, just here to make our compiler happy, make sure we don't execute any light code the user adds in.. +// unused, just here to make our compiler happy, make sure we don't execute any light code the user adds in.. #ifndef REALLYINCLUDETHIS { /* clang-format off */ @@ -102,8 +112,6 @@ LIGHT_SHADER_CODE /* clang-format on */ } #endif - - // color = texturePanorama(source_panorama, normalize(cube_normal.xyz)).rgb; { /* clang-format off */ @@ -112,6 +120,6 @@ FRAGMENT_SHADER_CODE /* clang-format on */ } - frag_color.rgb = color; - frag_color.a = params.alpha; + frag_color.rgb = color * params.position_multiplier.w; + frag_color.a = alpha; } -- cgit v1.2.3