| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <!--
- 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); \
- vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l); \
- vec3 diffC = diffuseLambert(gbuffer.diffuse); \
- float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
- float lambert = max(0.0, dot(gbuffer.normal, l));
- 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;
- // 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];
- }
- // Decode GBuffer
- GbufferInfo gbuffer;
- readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
- gbuffer.subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
- // Ambient and emissive color
- out_color = gbuffer.diffuse * gbuffer.emission;
- // Indirect
- out_color += textureLod(u_indirectRt, in_uv, 0.0).rgb;
- return;
- // Skip decals
- uint count = u_lightIndices[idxOffset];
- idxOffset += count + 1u;
- // Point lights
- vec3 viewDir = normalize(u_cameraPos - worldPos);
- count = u_lightIndices[idxOffset++];
- uint idxOffsetEnd = idxOffset + count;
- ANKI_LOOP while(idxOffset < idxOffsetEnd)
- {
- PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
- LIGHTING_COMMON_BRDF();
- if(light.diffuseColorTileSize.w >= 0.0)
- {
- float shadow = computeShadowFactorOmni(frag2Light,
- light.radiusPad1.x,
- light.atlasTiles,
- light.diffuseColorTileSize.w,
- u_shadowTex);
- lambert *= shadow;
- }
- out_color += (diffC + specC) * light.diffuseColorTileSize.rgb * (att * max(gbuffer.subsurface, lambert));
- }
- // Spot lights
- count = u_lightIndices[idxOffset++];
- idxOffsetEnd = idxOffset + count;
- ANKI_LOOP while(idxOffset < idxOffsetEnd)
- {
- SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
- LIGHTING_COMMON_BRDF();
- float spot = computeSpotFactor(
- l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDirRadius.xyz);
- float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
- if(shadowmapLayerIdx >= 0.0)
- {
- float shadow = computeShadowFactorSpot(
- light.texProjectionMat, worldPos, light.lightDirRadius.w, u_shadowTex);
- lambert *= shadow;
- }
- out_color +=
- (diffC + specC) * light.diffuseColorShadowmapId.rgb * (att * spot * max(gbuffer.subsurface, lambert));
- }
- #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>
|