| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <!--
- Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
- All rights reserved.
- Code licensed under the BSD License.
- http://www.anki3d.org/LICENSE
- -->
- <shaderProgram>
- <inputs>
- <input name="CLUSTER_COUNT_X" type="uint" const="1"/>
- <input name="CLUSTER_COUNT_Y" type="uint" const="1"/>
- <input name="CLUSTER_COUNT_Z" type="uint" const="1"/>
- <input name="CLUSTER_COUNT" type="uint" const="1"/>
- <input name="IR_MIPMAP_COUNT" type="uint" const="1"/>
- </inputs>
- <shaders>
- <shader type="vert">
- <source><![CDATA[
- #include "shaders/Common.glsl"
- layout(location = 0) out vec2 out_uv;
- layout(location = 1) out vec2 out_clusterIJ;
- out gl_PerVertex
- {
- vec4 gl_Position;
- };
- void main()
- {
- out_uv = vec2(gl_VertexID & 1, gl_VertexID >> 1) * 2.0;
- vec2 pos = out_uv * 2.0 - 1.0;
- gl_Position = vec4(pos, 0.0, 1.0);
- out_clusterIJ = vec2(CLUSTER_COUNT_X, CLUSTER_COUNT_Y) * out_uv;
- }
- ]]></source>
- </shader>
- <shader type="frag">
- <source><![CDATA[
- #include "shaders/Pack.glsl"
- #include "shaders/Clusterer.glsl"
- #include "shaders/Functions.glsl"
- #define LIGHT_SET 0
- #define LIGHT_SS_BINDING 0
- #define LIGHT_UBO_BINDING 0
- #define LIGHT_TEX_BINDING 5
- #define LIGHT_LIGHTS
- #define LIGHT_COMMON_UNIS
- #include "shaders/ClusterLightCommon.glsl"
- layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
- layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
- layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
- layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
- layout(ANKI_TEX_BINDING(0, 4)) uniform sampler2D u_indirectRt;
- layout(location = 0) in vec2 in_uv;
- layout(location = 1) in vec2 in_clusterIJ;
- layout(location = 0) out vec3 out_color;
- const float SUBSURFACE_MIN = 0.05;
- // Common code for lighting
- #define LIGHTING_COMMON_BRDF() \
- vec3 frag2Light = light.posRadius.xyz - worldPos; \
- vec3 l = normalize(frag2Light); \
- float nol = max(0.0, dot(normal, l)); \
- vec3 specC = computeSpecularColorBrdf(viewDir, l, normal, specCol, light.specularColorRadius.rgb, a2, nol); \
- vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb); \
- float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
- float lambert = nol;
- void main()
- {
- float depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
- vec2 ndc = UV_TO_NDC(in_uv);
- // Get world position
- vec4 worldPos4 = u_invViewProjMat * vec4(ndc, depth, 1.0);
- vec3 worldPos = worldPos4.xyz / worldPos4.w;
- // Decode GBuffer
- vec3 normal;
- vec3 diffCol;
- vec3 specCol;
- float roughness;
- float subsurface;
- float emission;
- float metallic;
- GbufferInfo gbuffer;
- readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
- diffCol = gbuffer.diffuse;
- specCol = gbuffer.specular;
- normal = gbuffer.normal;
- roughness = gbuffer.roughness;
- metallic = gbuffer.metallic;
- subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
- emission = gbuffer.emission;
- // Get first light index
- uint idxOffset;
- {
- uint k = computeClusterK(u_clustererMagic, worldPos);
- uint clusterIdx =
- k * (CLUSTER_COUNT_X * CLUSTER_COUNT_Y) + uint(in_clusterIJ.y) * CLUSTER_COUNT_X + uint(in_clusterIJ.x);
- idxOffset = u_clusters[clusterIdx];
- }
- // Skip decals
- uint count = u_lightIndices[idxOffset++];
- idxOffset += count;
- // Ambient and emissive color
- vec3 outC = diffCol * emission;
- // Don't allow zero a2 because we may end up with division with zero
- float a2 = computeRoughnesSquared(roughness);
- // Point lights
- vec3 viewDir = normalize(u_cameraPos - worldPos);
- count = u_lightIndices[idxOffset++];
- while(count-- != 0)
- {
- PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
- LIGHTING_COMMON_BRDF();
- if(light.diffuseColorShadowmapId.w >= 0.0)
- {
- float shadow = computeShadowFactorOmni(frag2Light,
- light.specularColorRadius.w,
- light.atlasTilesPad2.xy,
- light.diffuseColorShadowmapId.w,
- u_shadowTex);
- lambert *= shadow;
- }
- outC += (diffC + specC) * (att * max(subsurface, lambert));
- }
- // Spot lights
- count = u_lightIndices[idxOffset++];
- while(count-- != 0)
- {
- SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
- LIGHTING_COMMON_BRDF();
- float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
- float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
- if(shadowmapLayerIdx >= 0.0)
- {
- float shadow = computeShadowFactorSpot(
- light.texProjectionMat, worldPos, light.specularColorRadius.w, u_shadowTex);
- lambert *= shadow;
- }
- outC += (diffC + specC) * (att * spot * max(subsurface, lambert));
- }
- // Indirect
- outC += textureLod(u_indirectRt, in_uv, 0.0).rgb;
- out_color = outC;
- #if 0
- count = scount;
- if(count == 0)
- {
- out_color = vec3(0.0, 0.0, 0.0);
- }
- else if(count == 1)
- {
- out_color = vec3(1.0, 0.0, 0.0);
- }
- else if(count == 2)
- {
- out_color = vec3(0.0, 1.0, 0.0);
- }
- else if(count == 3)
- {
- out_color = vec3(0.0, 0.0, 1.0);
- }
- else
- {
- out_color = vec3(1.0, 1.0, 1.0);
- }
- #endif
- }
- ]]></source>
- </shader>
- </shaders>
- </shaderProgram>
|