Guest User

Untitled

a guest
Dec 9th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. DECLARATIONS shadow_map_sampling
  2. {{
  3. #define SHADOW_EPSILON 14.f
  4. #define MIN_VARIANCE 4.f
  5.  
  6. SAMPLER_CMPDECL( shadow_map_0 );
  7. SAMPLER_CMPDECL( shadow_map_1 );
  8. SAMPLER_CMPDECL( shadow_map_2 );
  9. SAMPLER_CMPDECL( shadow_map_3 );
  10.  
  11. SamplerState genericSampler{
  12. Filter = MIN_MAG_MIP_LINEAR; //sets min/mag/mip filter to linear
  13. AddressU = Wrap;
  14. AddressV = Wrap;
  15. };
  16.  
  17. CBUFFER_BEGIN( cshadow_map_sampling )
  18. float4x4 light_matrices_0;
  19. float4x4 light_matrices_1;
  20. float4x4 light_matrices_2;
  21. float4x4 light_matrices_3;
  22.  
  23. float4 shadow_scale_0;
  24. float4 shadow_scale_1;
  25. float4 shadow_scale_2;
  26. float4 shadow_scale_3;
  27.  
  28. float4 shadow_samples;
  29. CBUFFER_END
  30.  
  31. float linstep( float min, float max, float v )
  32. {
  33. return saturate( ( v - min ) / (max - min ) );
  34. }
  35.  
  36. float ReduceLightBleeding( float p_max, float amount )
  37. {
  38. return linstep( amount, 1, p_max );
  39. }
  40.  
  41. float ChebyshevUpperBound( float2 Moments, float t )
  42. {
  43. //One tailed inequality valid if t > Moments.x
  44. float p = ( t <= Moments.x );
  45. float Variance = Moments.y - (Moments.x * Moments.x);
  46. Variance = max( Variance, MIN_VARIANCE );
  47.  
  48. //Compute probabilistic upper bound.
  49. float d = t - Moments.x;
  50. float p_max = Variance / ( Variance + d * d );
  51.  
  52. //return p;//_max;
  53. return max( p, p_max );
  54. }
  55.  
  56. //Used for Varience shadow mapping only
  57. float ShadowContribution( SAMPLER_ARGCMPDECL( shadow_map ), float2 LightTexCoord, float DistanceToLight )
  58. {
  59. #ifdef VARIANCE_SHADOW_MAPPING
  60. //Read the moments from the variance shadow map
  61. float2 Moments = SAMPLE_TEX2D( shadow_map, LightTexCoord ).xy;
  62.  
  63. // Compute the upper bound
  64. return ChebyshevUpperBound( Moments, DistanceToLight );
  65. #else
  66. return 1.0;
  67. #endif
  68. }
  69.  
  70. float ShadowMap( float4 position, float4x4 light_matrix, float4 shadow_scale, SAMPLER_ARGCMPDECL( shadow_map ), uniform bool directional )
  71. {
  72. #ifndef VARIANCE_SHADOW_MAPPING
  73.  
  74. #define SHADOW_MAP_SAMPLES 7
  75.  
  76. float4 light_space_pos = mul( position, light_matrix );
  77.  
  78. if(!directional)
  79. light_space_pos.z-=(light_space_pos.z*shadow_scale.z + 1.0f)/light_space_pos.w; // apply bias
  80.  
  81. float blur_range=(directional ? 1.0f/5000.0f : 0.14f);
  82. blur_range*=8.14f;
  83. const float2 offsets[SHADOW_MAP_SAMPLES]=
  84. {
  85. float2(0.000, 0.000)*blur_range,
  86. float2(1.000, 0.000)*blur_range,
  87. float2(0.500, 0.866)*blur_range,
  88. float2(-0.500, 0.866)*blur_range,
  89. float2(-1.000, -0.000)*blur_range,
  90. float2(-0.500, -0.866)*blur_range,
  91. float2(0.500, -0.866)*blur_range,
  92. };
  93. float light=0;
  94. for(int i=0; i< SHADOW_MAP_SAMPLES; i++)
  95. {
  96. float4 pos=light_space_pos;
  97. if(directional)pos.xy+=offsets[i]*shadow_scale.xy;
  98. else pos.xy+=offsets[i];
  99.  
  100. #if defined( DX11 ) || defined( GNMX )
  101. light += SAMPLE_CMPLEVELZERO( shadow_map, pos).x;
  102. #else
  103. light += SAMPLE_TEX2DPROJ(shadow_map, pos).x;
  104. #endif
  105. }
  106.  
  107. #if ENABLE_SHADOW_FADING
  108. #if TEXTURE_BORDER_BASED
  109. const float fade_range=0.9f; // 0 .. 1 parameter, affecting range of shadows fading (0=fading occurs on whole shadow range, 1=no fading occurs)
  110.  
  111. float2 shadow_map_uv=light_space_pos.xy/light_space_pos.w; // focus at 0 .. 1 range
  112. float2 delta=abs(shadow_map_uv-0.5f); // focus at 0 .. 0.5
  113. float fade=max(delta.x, delta.y); // focus at 0 .. 0.5
  114. fade=linstep(fade_range*0.5f, 0.5f, fade); // range 0 .. 1
  115. #else // pixel vs camera distance
  116. float fade=linstep(1200, 5000, length(position.xyz-camera_position.xyz));
  117. #endif
  118. return lerp(light*(1.0f/SHADOW_MAP_SAMPLES), 1, fade);
  119. #else
  120. return light*(1.0f/SHADOW_MAP_SAMPLES);
  121. #endif
  122.  
  123. #else
  124. //Variance shadow mapping
  125. float4 light_space_pos = mul( position, light_matrix );
  126. light_space_pos.xy /= light_space_pos.w;
  127.  
  128. const float distance = light_space_pos.z;
  129. return ShadowContribution( SAMPLER_ARG( shadow_map ), light_space_pos.xy, distance );
  130. #endif
  131. }
  132.  
  133. }}
  134.  
  135. DECLARATIONS shadow_map_generation
  136. {{
  137. CBUFFER_BEGIN( cshadow_map_generation )
  138. float4x4 shadow_map_view_projection;
  139. float4 shadow_map_view;
  140. CBUFFER_END
  141.  
  142. float2 ComputeMoments( float Depth )
  143. {
  144. //Depth += 4.0f; //What is this for?
  145. float2 Moments;
  146.  
  147. //First component is simple depth
  148. Moments.x = Depth;
  149.  
  150. //Compute the partial derivatives of depth.
  151. float dx = ddx( Depth );
  152. float dy = ddy( Depth );
  153.  
  154. Moments.y = Depth * Depth + 0.25*( dx * dx + dy * dy );
  155.  
  156. return Moments;
  157. }
  158. }}
  159.  
  160. FRAGMENT shadow_map_projection
  161. include shadow_map_generation
  162. in float4 iWorldPosition : world_position
  163. out float4 oProjectedPosition : POSITION
  164. out float4 oWorldPosition : TEXCOORD0
  165. {{
  166. oWorldPosition = iWorldPosition;
  167. oProjectedPosition = mul( iWorldPosition, shadow_map_view_projection );
  168. }}
  169.  
  170. FRAGMENT shadow_map_output
  171. include shadow_map_generation
  172. in float3 iProjectedPosition : world_pos
  173. out float4 colour : PIXEL_RETURN_SEMANTIC
  174. {{
  175. #ifndef VARIANCE_SHADOW_MAPPING
  176. colour = 0.0f; // we don't need color for hardware shadow maps, as we read it back from the depth buffer
  177. #else
  178. //const float distance = length( shadow_map_view.xyz - iProjectedPosition.xyz );
  179. const float distance = iProjectedPosition.z;
  180. colour = float4( ComputeMoments( distance ), 0.0f, 0.0f );// / 1000.0f;
  181. #endif
  182. }}
  183.  
  184. FRAGMENT kill_on_alpha_test
  185. include basic_materials
  186. in float2 iTextureUV : uv
  187. #ifdef COLOR_OUTPUT_ENABLED
  188. in float4 mod : COLOR0
  189. #endif
  190. {{
  191. #ifdef COLOR_OUTPUT_ENABLED
  192. // Used 0.49f instead of 0.5f to avoid issues when texture is fully opaque but blend mode is alpha test shadows
  193. clip( SAMPLE_TEX2D( basic_texture_map, iTextureUV ).a * mod.a - 0.49f );
  194. #else
  195. clip( SAMPLE_TEX2D( basic_texture_map, iTextureUV ).a - 0.5f );
  196. #endif
  197. }}
Advertisement
Add Comment
Please, Sign In to add comment