Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. #include "globals.h"
  2.  
  3. // GLSLES has limited number of vertex shader registers so we have to use less bones
  4. #ifdef GLSLES
  5. #define MAX_BONE_COUNT 32
  6. #else
  7. #define MAX_BONE_COUNT 72
  8. #endif
  9.  
  10. // PowerVR saturate() is compiled to min/max pair
  11. // These are cross-platform specialized saturates that are free on PC and only cost 1 cycle on PowerVR
  12. #ifdef GLSLES
  13. float saturate0(float v) { return max(v, 0); }
  14. float saturate1(float v) { return min(v, 1); }
  15. #define WANG_SUBSET_SCALE 2
  16. #else
  17. float saturate0(float v) { return saturate(v); }
  18. float saturate1(float v) { return saturate(v); }
  19. #define WANG_SUBSET_SCALE 1
  20. #endif
  21.  
  22. #define GBUFFER_MAX_DEPTH 500.0f
  23.  
  24.  
  25. #ifndef DX11
  26. #define TEX_DECLARE2D(name, reg) sampler2D name: register(s##reg)
  27. #define TEX_DECLARE3D(name, reg) sampler3D name: register(s##reg)
  28. #define TEX_DECLARECUBE(name, reg) samplerCUBE name: register(s##reg)
  29.  
  30. #define TEXTURE(name) name
  31. #define TEXTURE_IN_2D(name) sampler2D name
  32. #define TEXTURE_IN_3D(name) sampler3D name
  33. #define TEXTURE_IN_CUBE(name) samplerCUBE name
  34.  
  35. #define WORLD_MATRIX(name) uniform float4x4 name;
  36. #define WORLD_MATRIX_ARRAY(name, count) uniform float4 name [ count ];
  37.  
  38. #ifdef GLSL
  39. #define ATTR_INT4 float4
  40. #define ATTR_INT3 float3
  41. #define ATTR_INT2 float2
  42. #else
  43. #define ATTR_INT4 int4
  44. #define ATTR_INT3 int3
  45. #define ATTR_INT2 int2
  46. #endif
  47. #else
  48. #define TEX_DECLARE2D(name, reg) SamplerState name##Sampler: register(s##reg); Texture2D<float4> name##Texture: register(t##reg)
  49. #define TEX_DECLARE3D(name, reg) SamplerState name##Sampler: register(s##reg); Texture3D<float4> name##Texture: register(t##reg)
  50. #define TEX_DECLARECUBE(name, reg) SamplerState name##Sampler: register(s##reg); TextureCube<float4> name##Texture: register(t##reg)
  51.  
  52. #define tex2D(tex, uv) tex##Texture.Sample(tex##Sampler, uv)
  53. #define tex3D(tex, uv) tex##Texture.Sample(tex##Sampler, uv)
  54. #define texCUBE(tex, uv) tex##Texture.Sample(tex##Sampler, uv)
  55. #define tex2Dgrad(tex, uv, DDX, DDY) tex##Texture.SampleGrad(tex##Sampler, uv, DDX, DDY)
  56. #define tex2Dbias(tex, uv) tex##Texture.SampleBias(tex##Sampler, uv.xy, uv.w)
  57. #define texCUBEbias(tex, uv) tex##Texture.SampleBias(tex##Sampler, uv.xyz, uv.w)
  58.  
  59. #define TEXTURE(name) name##Sampler, name##Texture
  60. #define TEXTURE_IN_2D(name) SamplerState name##Sampler, Texture2D name##Texture
  61. #define TEXTURE_IN_3D(name) SamplerState name##Sampler, Texture3D name##Texture
  62. #define TEXTURE_IN_CUBE(name) SamplerState name##Sampler, TextureCube name##Texture
  63.  
  64. #define WORLD_MATRIX(name) cbuffer WorldMatrixCB : register( b1 ) { float4x4 name; }
  65. #define WORLD_MATRIX_ARRAY(name, count) cbuffer WorldMatrixCB : register( b1 ) { float4 name[ count ]; }
  66.  
  67. #define ATTR_INT4 int4
  68. #define ATTR_INT3 int3
  69. #define ATTR_INT2 int2
  70. #endif
  71.  
  72. float2 sampleLA8Texture(TEXTURE_IN_2D(tex), float2 uv)
  73. {
  74. #ifdef DX11
  75. return tex2D(tex, uv).rg;
  76. #else
  77. return tex2D(tex, uv).ba;
  78. #endif
  79. }
  80.  
  81. #if defined(GLSLES) || defined(PIN_WANG_FALLBACK)
  82. #define TEXTURE_WANG(name) 0
  83. void getWang(float unused, float2 uv, float tiling, out float2 wangUv, out float4 wangUVDerivatives)
  84. {
  85. wangUv = uv * WANG_SUBSET_SCALE;
  86. wangUVDerivatives = float4(0,0,0,0); // not used in this mode
  87. }
  88. float4 sampleWang(TEXTURE_IN_2D(s), float2 uv, float4 wangUVDerivatives)
  89. {
  90. return tex2D(s,uv);
  91. }
  92. #else
  93. #define TEXTURE_WANG(name) TEXTURE(name)
  94. void getWang(TEXTURE_IN_2D(s), float2 uv, float tiling, out float2 wangUv, out float4 wangUVDerivatives)
  95. {
  96. #ifndef WIN_MOBILE
  97. float idxTexSize = 128;
  98. #else
  99. float idxTexSize = 32;
  100. #endif
  101.  
  102. float2 wangBase = uv * tiling * 4;
  103.  
  104. #if defined(DX11) && !defined(WIN_MOBILE)
  105. // compensate the precision problem of Point Sampling on some cards. (We do it just at DX11 for performance reasons)
  106. float2 wangUV = (floor(wangBase) + 0.5) / idxTexSize;
  107. #else
  108. float2 wangUV = wangBase / idxTexSize;
  109. #endif
  110.  
  111. float2 wang = sampleLA8Texture(TEXTURE(s), wangUV);
  112.  
  113. wangUVDerivatives = float4(ddx(wangBase*0.25), ddy(wangBase*0.25));
  114.  
  115. wang *= 255.0/256.0;
  116. wangUv = wang + frac(wangBase)*0.25;
  117. }
  118. float4 sampleWang(TEXTURE_IN_2D(s), float2 uv, float4 derivates)
  119. {
  120. return tex2Dgrad(s, uv, derivates.xy, derivates.zw);
  121. }
  122. #endif
  123.  
  124. float4 gbufferPack(float depth, float3 diffuse, float3 specular, float fog)
  125. {
  126. depth = saturate(depth / GBUFFER_MAX_DEPTH);
  127.  
  128. const float3 bitSh = float3(255*255, 255, 1);
  129. const float3 lumVec = float3(0.299, 0.587, 0.114);
  130.  
  131. float2 comp;
  132. comp = depth*float2(255,255*256);
  133. comp = frac(comp);
  134. comp = float2(depth,comp.x*256/255) - float2(comp.x, comp.y)/255;
  135.  
  136. float4 result;
  137.  
  138. result.r = lerp(1, dot(specular, lumVec), saturate(3 * fog));
  139. result.g = lerp(0, dot(diffuse, lumVec), saturate(3 * fog));
  140. result.ba = comp.yx;
  141.  
  142. return result;
  143. }
  144.  
  145. float3 lgridOffset(float3 v, float3 n)
  146. {
  147. // cells are 4 studs in size
  148. // offset in normal direction to prevent self-occlusion
  149. // the offset has to be 1.5 cells in order to fully eliminate the influence of the source cell with trilinear filtering
  150. // (i.e. 1 cell is enough for point filtering, but is not enough for trilinear filtering)
  151. return v + n * (1.5f * 4.f);
  152. }
  153.  
  154. float3 lgridPrepareSample(float3 c)
  155. {
  156. // yxz swizzle is necessary for GLSLES sampling to work efficiently
  157. // (having .y as the first component allows to do the LUT lookup as a non-dependent texture fetch)
  158. return c.yxz * G(LightConfig0).xyz + G(LightConfig1).xyz;
  159. }
  160.  
  161. #ifdef GLSLES
  162. #define LGRID_SAMPLER(name, register) TEX_DECLARE2D(name, register)
  163.  
  164. float4 lgridSample(TEXTURE_IN_2D(t), TEXTURE_IN_2D(lut), float3 data)
  165. {
  166. float4 offsets = tex2D(lut, data.xy);
  167.  
  168. // texture is 64 pixels high
  169. // let's compute slice lerp coeff
  170. float slicef = frac(data.x * 64);
  171.  
  172. // texture has 64 slices with 8x8 atlas setup
  173. float2 base = saturate(data.yz) * 0.125;
  174.  
  175. float4 s0 = tex2D(t, base + offsets.xy);
  176. float4 s1 = tex2D(t, base + offsets.zw);
  177.  
  178. return lerp(s0, s1, slicef);
  179. }
  180. #else
  181. #define LGRID_SAMPLER(name, register) TEX_DECLARE3D(name, register)
  182.  
  183. float4 lgridSample(TEXTURE_IN_3D(t), TEXTURE_IN_2D(lut), float3 data)
  184. {
  185. float3 edge = step(G(LightConfig3).xyz, abs(data - G(LightConfig2).xyz));
  186. float edgef = saturate1(dot(edge, 1));
  187.  
  188. // replace data with 0 on edges to minimize texture cache misses
  189. float4 light = tex3D(t, data.yzx - data.yzx * edgef);
  190.  
  191. return lerp(light, G(LightBorder), edgef);
  192. }
  193. #endif
  194.  
  195. #ifdef GLSLES
  196. float3 nmapUnpack(float4 value)
  197. {
  198. return value.rgb * 2 - 1;
  199. }
  200. #else
  201. float3 nmapUnpack(float4 value)
  202. {
  203. float2 xy = value.ag * 2 - 1;
  204.  
  205. return float3(xy, sqrt(saturate(1 + dot(-xy, xy))));
  206. }
  207. #endif
  208.  
  209. float3 terrainNormal(float4 tnp0, float4 tnp1, float4 tnp2, float3 w, float3 normal, float3 tsel)
  210. {
  211. // Inspired by "Voxel-Based Terrain for Real-Time Virtual Simulations" [Lengyel2010] 5.5.2
  212. float3 tangentTop = float3(normal.y, -normal.x, 0);
  213. float3 tangentSide = float3(normal.z, 0, -normal.x);
  214.  
  215. float3 bitangentTop = float3(0, -normal.z, normal.y);
  216. float3 bitangentSide = float3(0, -1, 0);
  217.  
  218. // Blend pre-unpack to save cycles
  219. float3 tn = nmapUnpack(tnp0 * w.x + tnp1 * w.y + tnp2 * w.z);
  220.  
  221. // We blend all tangent frames together as a faster approximation to the correct world normal blend
  222. float tselw = dot(tsel, w);
  223.  
  224. float3 tangent = lerp(tangentSide, tangentTop, tselw);
  225. float3 bitangent = lerp(bitangentSide, bitangentTop, tselw);
  226.  
  227. return normalize(tangent * tn.x + bitangent * tn.y + normal * tn.z);
  228. }
  229.  
  230. float3 shadowPrepareSample(float3 p)
  231. {
  232. float4 c = float4(p, 1);
  233.  
  234. return float3(dot(G(ShadowMatrix0), c), dot(G(ShadowMatrix1), c), dot(G(ShadowMatrix2), c));
  235. }
  236.  
  237. float shadowDepth(float3 lpos)
  238. {
  239. return lpos.z;
  240. }
  241.  
  242. float shadowStep(float d, float z)
  243. {
  244. // saturate returns 1 for z in [0.1..0.9]; it fades to 0 as z approaches 0 or 1
  245. return step(d, z) * saturate(9 - 20 * abs(z - 0.5));
  246. }
  247.  
  248. float shadowSample(TEXTURE_IN_2D(map), float3 lpos, float lightShadow)
  249. {
  250. float2 smDepth = tex2D(map, lpos.xy).rg;
  251. float smShadow = shadowStep(smDepth.x, shadowDepth(lpos));
  252.  
  253. return (1 - smShadow * smDepth.y * G(OutlineBrightness_ShadowInfo).w) * lightShadow;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement