diff --git a/libs/video/renderer/Makemodule.am b/libs/video/renderer/Makemodule.am index fa1a3e067..b811b8c63 100644 --- a/libs/video/renderer/Makemodule.am +++ b/libs/video/renderer/Makemodule.am @@ -322,8 +322,10 @@ bsp_turbf_src = $(vkshaderpath)/bsp_turb.frag bsp_turbf_c = $(vkshaderpath)/bsp_turb.frag.spvc light_flat_src = $(vkshaderpath)/light_flat.vert light_flat_c = $(vkshaderpath)/light_flat.vert.spvc -light_splat_src = $(vkshaderpath)/light_splat.vert -light_splat_c = $(vkshaderpath)/light_splat.vert.spvc +light_splatv_src = $(vkshaderpath)/light_splat.vert +light_splatv_c = $(vkshaderpath)/light_splat.vert.spvc +light_splatf_src = $(vkshaderpath)/light_splat.frag +light_splatf_c = $(vkshaderpath)/light_splat.frag.spvc light_debug_src = $(vkshaderpath)/light_debug.frag light_debug_c = $(vkshaderpath)/light_debug.frag.spvc lightingf_src = $(vkshaderpath)/lighting.frag @@ -410,7 +412,9 @@ $(bsp_turbf_c): $(bsp_turbf_src) $(oit_store) $(oit_h) $(light_flat_c): $(light_flat_src) $(lighting_h) -$(light_splat_c): $(light_splat_src) $(lighting_h) +$(light_splatv_c): $(light_splatv_src) $(lighting_h) + +$(light_splatf_c): $(light_splatf_src) $(lighting_h) $(light_debug_c): $(light_debug_src) $(lighting_h) @@ -472,7 +476,8 @@ vkshader_c = \ $(bsp_skyf_c) \ $(bsp_turbf_c) \ $(light_flat_c) \ - $(light_splat_c) \ + $(light_splatv_c) \ + $(light_splatf_c) \ $(light_debug_c) \ $(lightingf_c) \ $(composef_c) \ diff --git a/libs/video/renderer/vulkan/rp_main_def.plist b/libs/video/renderer/vulkan/rp_main_def.plist index 4ee5fe962..17d9dcc2f 100644 --- a/libs/video/renderer/vulkan/rp_main_def.plist +++ b/libs/video/renderer/vulkan/rp_main_def.plist @@ -555,10 +555,10 @@ properties = { name = main; module = $builtin/light_flat.vert; }; - fragment = { + fragment_splat = { stage = fragment; name = main; - module = $builtin/lighting.frag; + module = $builtin/light_splat.frag; }; debug_fragment = { stage = fragment; @@ -1457,7 +1457,7 @@ renderpasses = { stages = ( $lighting.shader.vertex_splat, - $lighting.shader.fragment, + $lighting.shader.fragment_splat, ); vertexInput = $lighting.vertexInput_splat; inputAssembly = $lighting.inputAssembly; @@ -1475,7 +1475,7 @@ renderpasses = { stages = ( $lighting.shader.vertex_flat, - $lighting.shader.fragment, + $lighting.shader.fragment_splat, ); vertexInput = $lighting.vertexInput_flat; layout = $lighting.layout; diff --git a/libs/video/renderer/vulkan/shader.c b/libs/video/renderer/vulkan/shader.c index 9b7b3a6de..ed2ad7762 100644 --- a/libs/video/renderer/vulkan/shader.c +++ b/libs/video/renderer/vulkan/shader.c @@ -91,6 +91,8 @@ static static #include "libs/video/renderer/vulkan/shader/light_splat.vert.spvc" static +#include "libs/video/renderer/vulkan/shader/light_splat.frag.spvc" +static #include "libs/video/renderer/vulkan/shader/light_debug.frag.spvc" static #include "libs/video/renderer/vulkan/shader/lighting.frag.spvc" @@ -158,6 +160,7 @@ static shaderdata_t builtin_shaders[] = { { "bsp_turb.frag", bsp_turb_frag, sizeof (bsp_turb_frag) }, { "light_flat.vert", light_flat_vert, sizeof (light_flat_vert) }, { "light_splat.vert", light_splat_vert, sizeof (light_splat_vert) }, + { "light_splat.frag", light_splat_frag, sizeof (light_splat_frag) }, { "light_debug.frag", light_debug_frag, sizeof (light_debug_frag) }, { "lighting.frag", lighting_frag, sizeof (lighting_frag) }, { "compose.frag", compose_frag, sizeof (compose_frag) }, diff --git a/libs/video/renderer/vulkan/shader/light_splat.frag b/libs/video/renderer/vulkan/shader/light_splat.frag new file mode 100644 index 000000000..98cc64c56 --- /dev/null +++ b/libs/video/renderer/vulkan/shader/light_splat.frag @@ -0,0 +1,78 @@ +#version 450 +#extension GL_GOOGLE_include_directive : enable + +#include "lighting.h" + +layout (input_attachment_index = 0, set = 2, binding = 0) uniform subpassInput depth; +layout (input_attachment_index = 1, set = 2, binding = 1) uniform subpassInput color; +layout (input_attachment_index = 2, set = 2, binding = 2) uniform subpassInput emission; +layout (input_attachment_index = 3, set = 2, binding = 3) uniform subpassInput normal; +layout (input_attachment_index = 4, set = 2, binding = 4) uniform subpassInput position; + +layout (set = 3, binding = 0) uniform sampler2DArrayShadow shadowCascade[MaxLights]; +layout (set = 3, binding = 0) uniform sampler2DShadow shadowPlane[MaxLights]; +layout (set = 3, binding = 0) uniform samplerCubeShadow shadowCube[MaxLights]; + +layout (location = 0) flat in uint light_index; +layout (location = 0) out vec4 frag_color; + +float +spot_cone (vec4 light_direction, vec3 incoming) +{ + vec3 dir = light_direction.xyz; + float cone = light_direction.w; + float spotdot = dot (incoming, dir); + return 1 - smoothstep (cone, .995 * cone + 0.005, spotdot); +} + +float +diffuse (vec3 incoming, vec3 normal) +{ + float lightdot = dot (incoming, normal); + return clamp (lightdot, 0, 1); +} + +float +shadow_cascade (sampler2DArrayShadow map) +{ + return 1; +} + +float +shadow_plane (sampler2DShadow map) +{ + return 1; +} + +float +shadow_cube (samplerCubeShadow map) +{ + return 1; +} + +void +main (void) +{ + //float d = subpassLoad (depth).r; + //vec3 c = subpassLoad (color).rgb; + //vec3 e = subpassLoad (emission).rgb; + vec3 n = subpassLoad (normal).rgb; + vec3 p = subpassLoad (position).rgb; + vec3 light = vec3 (0); + + LightData l = lights[light_index]; + vec3 dir = l.position.xyz - l.position.w * p; + float r2 = dot (dir, dir); + vec4 a = l.attenuation; + + vec4 r = vec4 (r2, sqrt(r2), 1, 0); + vec3 incoming = dir / r.y; + float I = (1 - a.w * r.y) / dot (a, r); + + float namb = dot(l.direction.xyz, l.direction.xyz); + I *= spot_cone (l.direction, incoming) * diffuse (incoming, n); + I = clamp (mix (1, I, namb), 0, 1); + light += I * l.color.w * l.color.xyz; + + frag_color = vec4 (light, 1); +} diff --git a/libs/video/renderer/vulkan/shader/lighting.frag b/libs/video/renderer/vulkan/shader/lighting.frag index 98cc64c56..3e100a55c 100644 --- a/libs/video/renderer/vulkan/shader/lighting.frag +++ b/libs/video/renderer/vulkan/shader/lighting.frag @@ -1,26 +1,54 @@ #version 450 -#extension GL_GOOGLE_include_directive : enable -#include "lighting.h" +layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput depth; +layout (input_attachment_index = 1, set = 0, binding = 1) uniform subpassInput color; +layout (input_attachment_index = 2, set = 0, binding = 2) uniform subpassInput emission; +layout (input_attachment_index = 3, set = 0, binding = 3) uniform subpassInput normal; +layout (input_attachment_index = 4, set = 0, binding = 4) uniform subpassInput position; -layout (input_attachment_index = 0, set = 2, binding = 0) uniform subpassInput depth; -layout (input_attachment_index = 1, set = 2, binding = 1) uniform subpassInput color; -layout (input_attachment_index = 2, set = 2, binding = 2) uniform subpassInput emission; -layout (input_attachment_index = 3, set = 2, binding = 3) uniform subpassInput normal; -layout (input_attachment_index = 4, set = 2, binding = 4) uniform subpassInput position; +struct LightData { + vec4 color; // .a is intensity + vec4 position; // .w = 0 -> directional, .w = 1 -> point/cone + vec4 direction; // .w = -cos(cone_angle/2) (1 for omni/dir) + vec4 attenuation; +}; -layout (set = 3, binding = 0) uniform sampler2DArrayShadow shadowCascade[MaxLights]; -layout (set = 3, binding = 0) uniform sampler2DShadow shadowPlane[MaxLights]; -layout (set = 3, binding = 0) uniform samplerCubeShadow shadowCube[MaxLights]; +#define StyleMask 0x07f +#define ModelMask 0x380 +#define ShadowMask 0xc00 + +#define LM_LINEAR (0 << 7) // light - dist (or radius + dist if -ve) +#define LM_INVERSE (1 << 7) // distFactor1 * light / dist +#define LM_INVERSE2 (2 << 7) // distFactor2 * light / (dist * dist) +#define LM_INFINITE (3 << 7) // light +#define LM_AMBIENT (4 << 7) // light +#define LM_INVERSE3 (5 << 7) // distFactor2 * light / (dist + distFactor2)**2 + +#define ST_NONE (0 << 10) // no shadows +#define ST_PLANE (1 << 10) // single plane shadow map (small spotlight) +#define ST_CASCADE (2 << 10) // cascaded shadow maps +#define ST_CUBE (3 << 10) // cubemap (omni, large spotlight) + +layout (constant_id = 0) const int MaxLights = 768; + +layout (set = 2, binding = 0) uniform sampler2DArrayShadow shadowCascade[MaxLights]; +layout (set = 2, binding = 0) uniform sampler2DShadow shadowPlane[MaxLights]; +layout (set = 2, binding = 0) uniform samplerCubeShadow shadowCube[MaxLights]; + +layout (set = 1, binding = 0) uniform Lights { + LightData lights[MaxLights]; + int lightCount; + //mat4 shadowMat[MaxLights]; + //vec4 shadowCascale[MaxLights]; +}; -layout (location = 0) flat in uint light_index; layout (location = 0) out vec4 frag_color; float -spot_cone (vec4 light_direction, vec3 incoming) +spot_cone (LightData light, vec3 incoming) { - vec3 dir = light_direction.xyz; - float cone = light_direction.w; + vec3 dir = light.direction.xyz; + float cone = light.direction.w; float spotdot = dot (incoming, dir); return 1 - smoothstep (cone, .995 * cone + 0.005, spotdot); } @@ -54,25 +82,41 @@ void main (void) { //float d = subpassLoad (depth).r; - //vec3 c = subpassLoad (color).rgb; - //vec3 e = subpassLoad (emission).rgb; + vec3 c = subpassLoad (color).rgb; + vec3 e = subpassLoad (emission).rgb; vec3 n = subpassLoad (normal).rgb; vec3 p = subpassLoad (position).rgb; vec3 light = vec3 (0); - LightData l = lights[light_index]; - vec3 dir = l.position.xyz - l.position.w * p; - float r2 = dot (dir, dir); - vec4 a = l.attenuation; + //vec3 minLight = vec3 (0); + for (int i = 0; i < lightCount; i++) { + LightData l = lights[i]; + vec3 dir = l.position.xyz - l.position.w * p; + float r2 = dot (dir, dir); + vec4 a = l.attenuation; - vec4 r = vec4 (r2, sqrt(r2), 1, 0); - vec3 incoming = dir / r.y; - float I = (1 - a.w * r.y) / dot (a, r); + if (l.position.w * a.w * a.w * r2 >= 1) { + continue; + } + vec4 r = vec4 (r2, sqrt(r2), 1, 0); + vec3 incoming = dir / r.y; + float I = (1 - a.w * r.y) / dot (a, r); - float namb = dot(l.direction.xyz, l.direction.xyz); - I *= spot_cone (l.direction, incoming) * diffuse (incoming, n); - I = clamp (mix (1, I, namb), 0, 1); - light += I * l.color.w * l.color.xyz; + /*int shadow = lights[i].data & ShadowMask; + if (shadow == ST_CASCADE) { + I *= shadow_cascade (shadowCascade[i]); + } else if (shadow == ST_PLANE) { + I *= shadow_plane (shadowPlane[i]); + } else if (shadow == ST_CUBE) { + I *= shadow_cube (shadowCube[i]); + }*/ - frag_color = vec4 (light, 1); + float namb = dot(l.direction.xyz, l.direction.xyz); + I *= spot_cone (l, incoming) * diffuse (incoming, n); + I = mix (1, I, namb); + light += I * l.color.w * l.color.xyz; + } + //light = max (light, minLight); + + frag_color = vec4 (c * light + e, 1); }