Transparent.bsl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "$ENGINE$\BasePass.bslinc"
  2. #include "$ENGINE$\LightingCommon.bslinc"
  3. #include "$ENGINE$\LightGridCommon.bslinc"
  4. Parameters =
  5. {
  6. Sampler2D gAlbedoSamp : alias("gAlbedoTex");
  7. Sampler2D gNormalSamp : alias("gNormalTex");
  8. Sampler2D gRoughnessSamp : alias("gRoughnessTex");
  9. Sampler2D gMetalnessSamp : alias("gMetalnessTex");
  10. Texture2D gAlbedoTex;
  11. Texture2D gNormalTex = "normal";
  12. Texture2D gRoughnessTex = "white";
  13. Texture2D gMetalnessTex = "black";
  14. float gOpacity = 1.0f;
  15. };
  16. Blocks =
  17. {
  18. Block MaterialParams;
  19. };
  20. Transparent = true;
  21. Technique
  22. : inherits("LightingCommon")
  23. : inherits("LightGridCommon")
  24. : base("Surface") =
  25. {
  26. Language = "HLSL11";
  27. Pass =
  28. {
  29. DepthWrite = false;
  30. Target =
  31. {
  32. Blend = true;
  33. Color = { SRCA, SRCIA, ADD };
  34. };
  35. Fragment =
  36. {
  37. SamplerState gAlbedoSamp : register(s0);
  38. SamplerState gNormalSamp : register(s1);
  39. SamplerState gRoughnessSamp : register(s2);
  40. SamplerState gMetalnessSamp : register(s3);
  41. Texture2D gAlbedoTex : register(t0);
  42. Texture2D gNormalTex : register(t1);
  43. Texture2D gRoughnessTex : register(t2);
  44. Texture2D gMetalnessTex : register(t3);
  45. Buffer<uint3> gGridOffsetsAndSize : register(t4);
  46. Buffer<uint> gGridLightIndices : register(t5);
  47. StructuredBuffer<LightData> gLights : register(t6);
  48. cbuffer MaterialParams : register(b5)
  49. {
  50. float gOpacity;
  51. }
  52. float4 main(in VStoFS input) : SV_Target0
  53. {
  54. float3 normal = normalize(gNormalTex.Sample(gNormalSamp, input.uv0).xyz * 2.0f - float3(1, 1, 1));
  55. float3 worldNormal = calcWorldNormal(input, normal);
  56. SurfaceData surfaceData;
  57. surfaceData.albedo = gAlbedoTex.Sample(gAlbedoSamp, input.uv0);
  58. surfaceData.worldNormal.xyz = worldNormal;
  59. surfaceData.roughness = gRoughnessTex.Sample(gRoughnessSamp, input.uv0).x;
  60. surfaceData.metalness = gMetalnessTex.Sample(gMetalnessSamp, input.uv0).x;
  61. float3 lightAccumulator = 0;
  62. // Directional lights
  63. for(uint lightIdx = 0; lightIdx < gLightOffsets[0]; ++lightIdx)
  64. lightAccumulator += getDirLightContibution(surfaceData, gLights[lightIdx]);
  65. uint2 pixelPos = (uint2)input.position.xy;
  66. uint cellIdx = calcCellIdx(pixelPos, input.position.z);
  67. uint3 offsetAndSize = gGridOffsetsAndSize[cellIdx];
  68. // Radial lights
  69. uint i = offsetAndSize.x;
  70. uint end = offsetAndSize.x + offsetAndSize.y;
  71. for(; i < end; i++)
  72. {
  73. uint lightIndex = gGridLightIndices[i];
  74. LightData lightData = gLights[lightIndex];
  75. lightAccumulator += getPointLightContribution(input.worldPosition, surfaceData, lightData);
  76. }
  77. // Spot lights
  78. end += offsetAndSize.z;
  79. for(; i < end; i++)
  80. {
  81. uint lightIndex = gGridLightIndices[i];
  82. LightData lightData = gLights[lightIndex];
  83. lightAccumulator += getSpotLightContribution(input.worldPosition, surfaceData, lightData);
  84. }
  85. lightAccumulator += surfaceData.albedo.xyz * gAmbientFactor;
  86. float3 diffuse = surfaceData.albedo.xyz / PI;
  87. return float4(diffuse * lightAccumulator, gOpacity); // TODO - Add better lighting model later
  88. }
  89. };
  90. };
  91. };
  92. Technique
  93. : inherits("LightingCommon")
  94. : inherits("LightGridCommon")
  95. : base("Surface") =
  96. {
  97. Language = "GLSL";
  98. Pass =
  99. {
  100. DepthWrite = false;
  101. Target =
  102. {
  103. Blend = true;
  104. Color = { SRCA, SRCIA, ADD };
  105. };
  106. Fragment =
  107. {
  108. layout(location = 0) in vec2 uv0;
  109. layout(location = 1) in vec3 worldPosition;
  110. layout(location = 2) in vec3 tangentToWorldZ;
  111. layout(location = 3) in vec4 tangentToWorldX;
  112. layout(binding = 5) uniform sampler2D gAlbedoTex;
  113. layout(binding = 6) uniform sampler2D gNormalTex;
  114. layout(binding = 7) uniform sampler2D gRoughnessTex;
  115. layout(binding = 8) uniform sampler2D gMetalnessTex;
  116. layout(binding = 9) uniform usamplerBuffer gGridOffsetsAndSize;
  117. layout(binding = 10) uniform usamplerBuffer gGridLightIndices;
  118. layout(std430, binding = 11) readonly buffer gLights
  119. {
  120. LightData[] gLightsData;
  121. };
  122. layout(binding = 12, std140) uniform MaterialParams
  123. {
  124. float gOpacity;
  125. };
  126. layout(location = 0) out vec4 fragColor;
  127. void main()
  128. {
  129. vec3 normal = normalize(texture(gNormalTex, uv0).xyz * 2.0f - vec3(1, 1, 1));
  130. vec3 worldNormal = calcWorldNormal(tangentToWorldZ, tangentToWorldX, normal);
  131. SurfaceData surfaceData;
  132. surfaceData.albedo = texture(gAlbedoTex, uv0);
  133. surfaceData.worldNormal.xyz = worldNormal;
  134. surfaceData.roughness = texture(gRoughnessTex, uv0).x;
  135. surfaceData.metalness = texture(gMetalnessTex, uv0).x;
  136. // Directional lights
  137. vec3 lightAccumulator = vec3(0, 0, 0);
  138. for(uint i = 0; i < gLightOffsets[0]; ++i)
  139. {
  140. LightData lightData = gLightsData[i];
  141. lightAccumulator += getDirLightContibution(surfaceData, lightData);
  142. }
  143. uvec2 pixelPos = uvec2(gl_FragCoord.xy);
  144. int cellIdx = calcCellIdx(pixelPos, gl_FragCoord.z);
  145. uvec3 offsetAndSize = texelFetch(gGridOffsetsAndSize, cellIdx).xyz;
  146. // Radial lights
  147. int i = int(offsetAndSize.x);
  148. uint end = offsetAndSize.x + offsetAndSize.y;
  149. for(; i < end; i++)
  150. {
  151. uint lightIndex = texelFetch(gGridLightIndices, i).x;
  152. LightData lightData = gLightsData[lightIndex];
  153. lightAccumulator += getPointLightContribution(worldPosition, surfaceData, lightData);
  154. }
  155. // Spot lights
  156. end += offsetAndSize.z;
  157. for(; i < end; i++)
  158. {
  159. uint lightIndex = texelFetch(gGridLightIndices, i).x;
  160. LightData lightData = gLightsData[lightIndex];
  161. lightAccumulator += getSpotLightContribution(worldPosition, surfaceData, lightData);
  162. }
  163. lightAccumulator += surfaceData.albedo.xyz * gAmbientFactor;
  164. vec3 diffuse = surfaceData.albedo.xyz / PI; // TODO - Add better lighting model later
  165. fragColor = vec4(diffuse * lightAccumulator, gOpacity);
  166. }
  167. };
  168. };
  169. };
  170. #include "$ENGINE$\Surface.bslinc"