Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

GraniteShaderLibBase.hlsl 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. #ifndef GRA_HLSL_3
  2. #define GRA_HLSL_3 0
  3. #endif
  4. #ifndef GRA_HLSL_4
  5. #define GRA_HLSL_4 0
  6. #endif
  7. #ifndef GRA_HLSL_5
  8. #define GRA_HLSL_5 0
  9. #endif
  10. #ifndef GRA_GLSL_120
  11. #define GRA_GLSL_120 0
  12. #endif
  13. #ifndef GRA_GLSL_130
  14. #define GRA_GLSL_130 0
  15. #endif
  16. #ifndef GRA_GLSL_330
  17. #define GRA_GLSL_330 0
  18. #endif
  19. #ifndef GRA_VERTEX_SHADER
  20. #define GRA_VERTEX_SHADER 0
  21. #endif
  22. #ifndef GRA_PIXEL_SHADER
  23. #define GRA_PIXEL_SHADER 0
  24. #endif
  25. #ifndef GRA_HQ_CUBEMAPPING
  26. #define GRA_HQ_CUBEMAPPING 0
  27. #endif
  28. #ifndef GRA_DEBUG_TILES
  29. #define GRA_DEBUG_TILES 0
  30. #endif
  31. #ifndef GRA_BGRA
  32. #define GRA_BGRA 0
  33. #endif
  34. #ifndef GRA_ROW_MAJOR
  35. #define GRA_ROW_MAJOR 1
  36. #endif
  37. #ifndef GRA_DEBUG
  38. #define GRA_DEBUG 1
  39. #endif
  40. #ifndef GRA_64BIT_RESOLVER
  41. #define GRA_64BIT_RESOLVER 0
  42. #endif
  43. #ifndef GRA_RWTEXTURE2D_SCALE
  44. #define GRA_RWTEXTURE2D_SCALE 16
  45. #endif
  46. #ifndef GRA_DISABLE_TEX_LOAD
  47. #define GRA_DISABLE_TEX_LOAD 0
  48. #endif
  49. #ifndef GRA_PACK_RESOLVE_OUTPUT
  50. #define GRA_PACK_RESOLVE_OUTPUT 1
  51. #endif
  52. // Temp workaround for some platforms's lack of unorm.
  53. #ifdef GRA_NO_UNORM
  54. #define GRA_UNORM
  55. #else
  56. #define GRA_UNORM unorm
  57. #endif
  58. #ifndef GRA_TEXTURE_ARRAY_SUPPORT
  59. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1) || (GRA_GLSL_330 == 1)
  60. #define GRA_TEXTURE_ARRAY_SUPPORT 1
  61. #else
  62. #define GRA_TEXTURE_ARRAY_SUPPORT 0
  63. #endif
  64. #endif
  65. #define GRA_HLSL_FAMILY ((GRA_HLSL_3 == 1) || (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1))
  66. #define GRA_GLSL_FAMILY ((GRA_GLSL_120 == 1) || (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1))
  67. #if GRA_HLSL_FAMILY
  68. #define gra_Float2 float2
  69. #define gra_Float3 float3
  70. #define gra_Float4 float4
  71. #define gra_Int3 int3
  72. #define gra_Float4x4 float4x4
  73. #define gra_Unroll [unroll]
  74. #define gra_Branch [branch]
  75. #elif GRA_GLSL_FAMILY
  76. #if (GRA_VERTEX_SHADER == 0) && (GRA_PIXEL_SHADER ==0)
  77. #error GLSL requires knowledge of the shader stage! Neither GRA_VERTEX_SHADER or GRA_PIXEL_SHADER are defined!
  78. #else
  79. #define gra_Float2 vec2
  80. #define gra_Float3 vec3
  81. #define gra_Float4 vec4
  82. #define gra_Int3 ivec3
  83. #define gra_Float4x4 mat4
  84. #define gra_Unroll
  85. #define gra_Branch
  86. #if (GRA_VERTEX_SHADER == 1)
  87. #define ddx
  88. #define ddy
  89. #elif (GRA_PIXEL_SHADER == 1)
  90. #define ddx dFdx
  91. #define ddy dFdy
  92. #endif
  93. #define frac fract
  94. #define lerp mix
  95. /** This is not correct (http://stackoverflow.com/questions/7610631/glsl-mod-vs-hlsl-fmod) but it is for our case */
  96. #define fmod mod
  97. #endif
  98. #else
  99. #error unknown shader architecture
  100. #endif
  101. #if (GRA_DISABLE_TEX_LOAD!=1)
  102. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1) || (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1)
  103. #define GRA_LOAD_INSTR 1
  104. #else
  105. #define GRA_LOAD_INSTR 0
  106. #endif
  107. #else
  108. #define GRA_LOAD_INSTR 0
  109. #endif
  110. /**
  111. a cross API texture handle
  112. */
  113. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  114. struct GraniteTranslationTexture
  115. {
  116. SamplerState Sampler;
  117. Texture2D Texture;
  118. };
  119. struct GraniteCacheTexture
  120. {
  121. SamplerState Sampler;
  122. #if GRA_TEXTURE_ARRAY_SUPPORT
  123. Texture2DArray TextureArray;
  124. #else
  125. Texture2D Texture;
  126. #endif
  127. };
  128. #elif (GRA_HLSL_3 == 1) || (GRA_GLSL_120 == 1) || (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1)
  129. #define GraniteTranslationTexture sampler2D
  130. #if GRA_TEXTURE_ARRAY_SUPPORT
  131. #define GraniteCacheTexture sampler2DArray
  132. #else
  133. #define GraniteCacheTexture sampler2D
  134. #endif
  135. #else
  136. #error unknow shader archtecture
  137. #endif
  138. /**
  139. Struct defining the constant buffer for each streaming texture.
  140. Use IStreamingTexture::GetConstantBuffer to fill this struct.
  141. */
  142. struct GraniteStreamingTextureConstantBuffer
  143. {
  144. #define _grStreamingTextureCBSize 2
  145. gra_Float4 data[_grStreamingTextureCBSize];
  146. };
  147. /**
  148. Struct defining the constant buffer for each cube streaming texture.
  149. Use multiple calls to IStreamingTexture::GetConstantBuffer this struct (one call for each face).
  150. */
  151. struct GraniteStreamingTextureCubeConstantBuffer
  152. {
  153. #define _grStreamingTextureCubeCBSize 6
  154. GraniteStreamingTextureConstantBuffer data[_grStreamingTextureCubeCBSize];
  155. };
  156. /**
  157. Struct defining the constant buffer for each tileset.
  158. Use ITileSet::GetConstantBuffer to fill this struct.
  159. */
  160. struct GraniteTilesetConstantBuffer
  161. {
  162. #define _grTilesetCBSize 2
  163. gra_Float4x4 data[_grTilesetCBSize];
  164. };
  165. /**
  166. Utility struct used by the shaderlib to wrap up all required constant buffers needed to perform a VT lookup/sample.
  167. */
  168. struct GraniteConstantBuffers
  169. {
  170. GraniteTilesetConstantBuffer tilesetBuffer;
  171. GraniteStreamingTextureConstantBuffer streamingTextureBuffer;
  172. };
  173. /**
  174. Utility struct used by the shaderlib to wrap up all required constant buffers needed to perform a Cube VT lookup/sample.
  175. */
  176. struct GraniteCubeConstantBuffers
  177. {
  178. GraniteTilesetConstantBuffer tilesetBuffer;
  179. GraniteStreamingTextureCubeConstantBuffer streamingTextureCubeBuffer;
  180. };
  181. /**
  182. The Granite lookup data for the different sampling functions.
  183. */
  184. // Granite lookup data for automatic mip level selecting sampling
  185. struct GraniteLookupData
  186. {
  187. gra_Float4 translationTableData;
  188. gra_Float2 textureCoordinates;
  189. gra_Float2 dX;
  190. gra_Float2 dY;
  191. };
  192. // Granite lookup data for explicit level-of-detail sampling
  193. struct GraniteLODLookupData
  194. {
  195. gra_Float4 translationTableData;
  196. gra_Float2 textureCoordinates;
  197. float cacheLevel;
  198. };
  199. //@IGNORE_END
  200. // public interface
  201. /*
  202. END OF PUBLIC INTERFACE
  203. Everything below this point should be treated as private to GraniteShaderLib.h
  204. */
  205. //@INSERT_DEFINES
  206. #define gra_TilesetBuffer grCB.tilesetBuffer
  207. #define gra_TilesetBufferInternal tsCB.data[0]
  208. #define gra_TilesetCacheBuffer tsCB.data[1]
  209. #define gra_StreamingTextureCB grCB.streamingTextureBuffer
  210. #define gra_StreamingTextureCubeCB grCB.streamingTextureCubeBuffer
  211. #define gra_Transform grCB.streamingTextureBuffer.data[0]
  212. #define gra_CubeTransform grCB.streamingTextureCubeBuffer.data
  213. #define gra_StreamingTextureTransform grSTCB.data[0]
  214. #define gra_StreamingTextureInfo grSTCB.data[1]
  215. #define gra_NumLevels gra_StreamingTextureInfo.x
  216. #define gra_AssetWidthRcp gra_StreamingTextureInfo.y
  217. #define gra_AssetHeightRcp gra_StreamingTextureInfo.z
  218. #if GRA_ROW_MAJOR == 1
  219. #define gra_TranslationTableBias gra_TilesetBufferInternal[0][0]
  220. #define gra_MaxAnisotropyLog2 gra_TilesetBufferInternal[1][0]
  221. #define gra_CalcMiplevelDeltaScale gra_Float2(gra_TilesetBufferInternal[2][0], gra_TilesetBufferInternal[3][0])
  222. #define gra_CalcMiplevelDeltaScaleX gra_TilesetBufferInternal[2][0]
  223. #define gra_CalcMiplevelDeltaScaleY gra_TilesetBufferInternal[3][0]
  224. #define gra_LodBiasPow2 gra_TilesetBufferInternal[0][1]
  225. #define gra_TrilinearOffset gra_TilesetBufferInternal[0][2]
  226. #define gra_TileContentInTiles gra_Float2(gra_TilesetBufferInternal[0][2], gra_TilesetBufferInternal[1][2])
  227. #define gra_Level0NumTilesX gra_TilesetBufferInternal[0][3]
  228. #define gra_NumTilesYScale gra_TilesetBufferInternal[1][3]
  229. #define gra_TextureMagic gra_TilesetBufferInternal[2][3]
  230. #define gra_TextureId gra_TilesetBufferInternal[3][3]
  231. #define gra_RcpCacheInTiles(l) gra_Float2(gra_TilesetCacheBuffer[0][l], gra_TilesetCacheBuffer[1][l])
  232. #define gra_BorderPixelsRcpCache(l) gra_Float2(gra_TilesetCacheBuffer[2][l], gra_TilesetCacheBuffer[3][l])
  233. #else
  234. #define gra_TranslationTableBias gra_TilesetBufferInternal[0][0]
  235. #define gra_MaxAnisotropyLog2 gra_TilesetBufferInternal[0][1]
  236. #define gra_CalcMiplevelDeltaScale gra_Float2(gra_TilesetBufferInternal[0][2], gra_TilesetBufferInternal[0][3])
  237. #define gra_CalcMiplevelDeltaScaleX gra_TilesetBufferInternal[0][2]
  238. #define gra_CalcMiplevelDeltaScaleY gra_TilesetBufferInternal[0][3]
  239. #define gra_LodBiasPow2 gra_TilesetBufferInternal[1][0]
  240. #define gra_TrilinearOffset gra_TilesetBufferInternal[2][0]
  241. #define gra_TileContentInTiles gra_Float2(gra_TilesetBufferInternal[2][0], gra_TilesetBufferInternal[2][1])
  242. #define gra_Level0NumTilesX gra_TilesetBufferInternal[3][0]
  243. #define gra_NumTilesYScale gra_TilesetBufferInternal[3][1]
  244. #define gra_TextureMagic gra_TilesetBufferInternal[3][2]
  245. #define gra_TextureId gra_TilesetBufferInternal[3][3]
  246. #define gra_RcpCacheInTiles(l) gra_Float2(gra_TilesetCacheBuffer[l][0], gra_TilesetCacheBuffer[l][1])
  247. #define gra_BorderPixelsRcpCache(l) gra_Float2(gra_TilesetCacheBuffer[l][2], gra_TilesetCacheBuffer[l][3])
  248. #endif
  249. #if (GRA_GLSL_120==1)
  250. // Extension needed for texture2DLod
  251. //extension GL_ARB_shader_texture_lod : enable
  252. // Extensions needed fot texture2DGrad
  253. //extension GL_EXT_gpu_shader4 : enable
  254. // Extensions needed for bit manipulation
  255. //extension GL_ARB_shader_bit_encoding : enable
  256. #endif
  257. #if (GRA_TEXTURE_ARRAY_SUPPORT==1)
  258. gra_Float4 GranitePrivate_SampleArray(in GraniteCacheTexture tex, in gra_Float3 texCoord)
  259. {
  260. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  261. return tex.TextureArray.Sample(tex.Sampler, texCoord);
  262. #elif (GRA_GLSL_330 == 1)
  263. return texture(tex, texCoord);
  264. #else
  265. #error using unsupported function
  266. #endif
  267. }
  268. gra_Float4 GranitePrivate_SampleGradArray(in GraniteCacheTexture tex, in gra_Float3 texCoord, in gra_Float2 dX, in gra_Float2 dY)
  269. {
  270. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  271. return tex.TextureArray.SampleGrad(tex.Sampler,texCoord,dX,dY);
  272. #elif (GRA_GLSL_330 == 1)
  273. return textureGrad(tex, texCoord, dX, dY);
  274. #else
  275. #error using unsupported function
  276. #endif
  277. }
  278. gra_Float4 GranitePrivate_SampleLevelArray(in GraniteCacheTexture tex, in gra_Float3 texCoord, in float level)
  279. {
  280. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  281. return tex.TextureArray.SampleLevel(tex.Sampler, texCoord, level);
  282. #elif (GRA_GLSL_330 == 1)
  283. return textureLod(tex, texCoord, level);
  284. #else
  285. #error using unsupported function
  286. #endif
  287. }
  288. #else
  289. gra_Float4 GranitePrivate_Sample(in GraniteCacheTexture tex, in gra_Float2 texCoord)
  290. {
  291. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  292. return tex.Texture.Sample(tex.Sampler,texCoord);
  293. #elif (GRA_HLSL_3 == 1)
  294. return tex2D(tex,texCoord);
  295. #elif (GRA_GLSL_120 == 1) || (GRA_GLSL_130 == 1)
  296. return texture2D(tex, texCoord);
  297. #elif (GRA_GLSL_330 == 1)
  298. return texture(tex, texCoord);
  299. #endif
  300. }
  301. gra_Float4 GranitePrivate_SampleLevel(in GraniteCacheTexture tex, in gra_Float2 texCoord, in float level)
  302. {
  303. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  304. return tex.Texture.SampleLevel(tex.Sampler, texCoord, level);
  305. #elif (GRA_HLSL_3 == 1)
  306. return tex2Dlod(tex,gra_Float4(texCoord,0.0,level));
  307. #elif (GRA_GLSL_120 == 1)
  308. return texture2DLod(tex, texCoord, level);
  309. #elif (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1)
  310. return textureLod(tex, texCoord, level);
  311. #endif
  312. }
  313. gra_Float4 GranitePrivate_SampleGrad(in GraniteCacheTexture tex, in gra_Float2 texCoord, in gra_Float2 dX, in gra_Float2 dY)
  314. {
  315. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  316. return tex.Texture.SampleGrad(tex.Sampler,texCoord,dX,dY);
  317. #elif (GRA_HLSL_3 == 1)
  318. return tex2D(tex,texCoord,dX,dY);
  319. #elif (GRA_GLSL_120 == 1)
  320. return texture2DGrad(tex, texCoord, dX, dY);
  321. #elif (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1)
  322. return textureGrad(tex, texCoord, dX, dY);
  323. #endif
  324. }
  325. #endif //#if (GRA_TEXTURE_ARRAY_SUPPORT==1)
  326. #if (GRA_LOAD_INSTR==1)
  327. gra_Float4 GranitePrivate_Load(in GraniteTranslationTexture tex, in gra_Int3 location)
  328. {
  329. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  330. return tex.Texture.Load(location);
  331. #elif (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1)
  332. return texelFetch(tex, location.xy, location.z);
  333. #elif (GRA_HLSL_3 == 1) || (GRA_GLSL_120 == 1)
  334. #error using unsupported function
  335. #endif
  336. }
  337. #endif
  338. //work-around shader compiler bug
  339. //compiler gets confused with GranitePrivate_SampleLevel taking a GraniteCacheTexture as argument when array support is disabled
  340. //Without array support, GraniteCacheTexture and GraniteTranslationTexture are the same (but still different types!)
  341. //compiler is confused (ERR_AMBIGUOUS_FUNCTION_CALL). Looks like somebody is over enthusiastic optimizing...
  342. gra_Float4 GranitePrivate_SampleLevel_Translation(in GraniteTranslationTexture tex, in gra_Float2 texCoord, in float level)
  343. {
  344. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  345. return tex.Texture.SampleLevel(tex.Sampler, texCoord, level);
  346. #elif (GRA_HLSL_3 == 1)
  347. return tex2Dlod(tex,gra_Float4(texCoord,0.0,level));
  348. #elif (GRA_GLSL_120 == 1)
  349. return texture2DLod(tex, texCoord, level);
  350. #elif (GRA_GLSL_130 == 1) || (GRA_GLSL_330 == 1)
  351. return textureLod(tex, texCoord, level);
  352. #endif
  353. }
  354. float GranitePrivate_Saturate(in float value)
  355. {
  356. #if GRA_HLSL_FAMILY
  357. return saturate(value);
  358. #elif GRA_GLSL_FAMILY
  359. return clamp(value, 0.0f, 1.0f);
  360. #endif
  361. }
  362. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1) || (GRA_GLSL_330 == 1)
  363. uint GranitePrivate_FloatAsUint(float value)
  364. {
  365. #if (GRA_HLSL_5 == 1) || (GRA_HLSL_4 == 1)
  366. return asuint(value);
  367. #elif (GRA_GLSL_330 == 1)
  368. return floatBitsToUint(value);
  369. #endif
  370. }
  371. #endif
  372. float GranitePrivate_Pow2(uint exponent)
  373. {
  374. #if GRA_HLSL_FAMILY
  375. return pow(2.0, exponent);
  376. #else
  377. return pow(2.0, float(exponent));
  378. #endif
  379. }
  380. gra_Float2 GranitePrivate_RepeatUV(in gra_Float2 uv, in GraniteStreamingTextureConstantBuffer grSTCB)
  381. {
  382. return frac(uv);
  383. }
  384. gra_Float2 GranitePrivate_UdimUV(in gra_Float2 uv, in GraniteStreamingTextureConstantBuffer grSTCB)
  385. {
  386. return uv;
  387. }
  388. gra_Float2 GranitePrivate_ClampUV(in gra_Float2 uv, in GraniteStreamingTextureConstantBuffer grSTCB)
  389. {
  390. gra_Float2 epsilon2 = gra_Float2(gra_AssetWidthRcp, gra_AssetHeightRcp);
  391. return clamp(uv, epsilon2, gra_Float2(1,1) - epsilon2);
  392. }
  393. gra_Float2 GranitePrivate_MirrorUV(in gra_Float2 uv, in GraniteStreamingTextureConstantBuffer grSTCB)
  394. {
  395. gra_Float2 t = frac(uv*0.5)*2.0;
  396. gra_Float2 l = gra_Float2(1.0,1.0);
  397. return l-abs(t-l);
  398. }
  399. // function definitons for private functions
  400. gra_Float4 GranitePrivate_PackTileId(in gra_Float2 tileXY, in float level, in float textureID);
  401. gra_Float4 Granite_DebugPackedTileId64(in gra_Float4 PackedTile)
  402. {
  403. #if GRA_64BIT_RESOLVER
  404. gra_Float4 output;
  405. const float scale = 1.0f / 65535.0f;
  406. gra_Float4 temp = PackedTile / scale;
  407. output.x = fmod(temp.x, 256.0f);
  408. output.y = floor(temp.x / 256.0f) + fmod(temp.y, 16.0f) * 16.0f;
  409. output.z = floor(temp.y / 16.0f);
  410. output.w = temp.z + temp.a * 16.0f;
  411. return gra_Float4
  412. (
  413. (float)output.x / 255.0f,
  414. (float)output.y / 255.0f,
  415. (float)output.z / 255.0f,
  416. (float)output.w / 255.0f
  417. );
  418. #else
  419. return PackedTile;
  420. #endif
  421. }
  422. gra_Float3 Granite_UnpackNormal(in gra_Float4 PackedNormal, float scale)
  423. {
  424. gra_Float2 reconstructed = gra_Float2(PackedNormal.x * PackedNormal.a, PackedNormal.y) * 2.0f - 1.0f;
  425. reconstructed *= scale;
  426. float z = sqrt(1.0f - GranitePrivate_Saturate(dot(reconstructed, reconstructed)));
  427. return gra_Float3(reconstructed, z);
  428. }
  429. gra_Float3 Granite_UnpackNormal(in gra_Float4 PackedNormal)
  430. {
  431. return Granite_UnpackNormal(PackedNormal, 1.0);
  432. }
  433. #if GRA_HLSL_FAMILY
  434. GraniteTilesetConstantBuffer Granite_ApplyResolutionOffset(in GraniteTilesetConstantBuffer INtsCB, in float resolutionOffsetPow2)
  435. {
  436. GraniteTilesetConstantBuffer tsCB = INtsCB;
  437. gra_LodBiasPow2 *= resolutionOffsetPow2;
  438. //resolutionOffsetPow2 *= resolutionOffsetPow2; //Square it before multiplying it in below
  439. gra_CalcMiplevelDeltaScaleX *= resolutionOffsetPow2;
  440. gra_CalcMiplevelDeltaScaleY *= resolutionOffsetPow2;
  441. return tsCB;
  442. }
  443. GraniteTilesetConstantBuffer Granite_SetMaxAnisotropy(in GraniteTilesetConstantBuffer INtsCB, in float maxAnisotropyLog2)
  444. {
  445. GraniteTilesetConstantBuffer tsCB = INtsCB;
  446. gra_MaxAnisotropyLog2 = min(gra_MaxAnisotropyLog2, maxAnisotropyLog2);
  447. return tsCB;
  448. }
  449. #else
  450. void Granite_ApplyResolutionOffset(inout GraniteTilesetConstantBuffer tsCB, in float resolutionOffsetPow2)
  451. {
  452. gra_LodBiasPow2 *= resolutionOffsetPow2;
  453. //resolutionOffsetPow2 *= resolutionOffsetPow2; //Square it before multiplying it in below
  454. gra_CalcMiplevelDeltaScaleX *= resolutionOffsetPow2;
  455. gra_CalcMiplevelDeltaScaleY *= resolutionOffsetPow2;
  456. }
  457. void Granite_SetMaxAnisotropy(inout GraniteTilesetConstantBuffer tsCB, in float maxAnisotropyLog2)
  458. {
  459. gra_MaxAnisotropyLog2 = min(gra_MaxAnisotropyLog2, maxAnisotropyLog2);
  460. }
  461. #endif
  462. gra_Float2 Granite_Transform(in GraniteStreamingTextureConstantBuffer grSTCB, in gra_Float2 textureCoord)
  463. {
  464. return textureCoord * gra_StreamingTextureTransform.zw + gra_StreamingTextureTransform.xy;
  465. }
  466. gra_Float4 Granite_MergeResolveOutputs(in gra_Float4 resolve0, in gra_Float4 resolve1, in gra_Float2 pixelLocation)
  467. {
  468. gra_Float2 screenPos = frac(pixelLocation * 0.5f);
  469. bool dither = (screenPos.x != screenPos.y);
  470. return (dither) ? resolve0 : resolve1;
  471. }
  472. gra_Float4 Granite_PackTileId(in gra_Float4 unpackedTileID)
  473. {
  474. return GranitePrivate_PackTileId(unpackedTileID.xy, unpackedTileID.z, unpackedTileID.w);
  475. }
  476. #if (GRA_HLSL_5 == 1)
  477. void Granite_DitherResolveOutput(in gra_Float4 resolve, in RWTexture2D<GRA_UNORM gra_Float4> resolveTexture, in gra_Float2 screenPos, in float alpha)
  478. {
  479. const uint2 pixelPos = int2(screenPos);
  480. const uint2 pixelLocation = pixelPos % GRA_RWTEXTURE2D_SCALE;
  481. bool dither = (pixelLocation.x == 0) && (pixelLocation.y == 0);
  482. uint2 writePos = pixelPos / GRA_RWTEXTURE2D_SCALE;
  483. if ( alpha == 0 )
  484. {
  485. dither = false;
  486. }
  487. else if (alpha != 1.0)
  488. {
  489. // Do a 4x4 dither patern so alternating pixels resolve to the first or the second texture
  490. gra_Float2 pixelLocationAlpha = frac(screenPos * 0.25f); // We don't scale after the frac so this will give coords 0, 0.25, 0.5, 0.75
  491. int pixelId = (int)(pixelLocationAlpha.y * 16 + pixelLocationAlpha.x * 4); //faster as a dot2 ?
  492. // Clamp
  493. // This ensures that for example alpha=0.95 still resolves some tiles of the surfaces behind it
  494. // and alpha=0.05 still resolves some tiles of this surface
  495. alpha = min(max(alpha, 0.0625), 0.9375);
  496. // Modern hardware supports array indexing with per pixel varying indexes
  497. // on old hardware this will be expanded to a conditional tree by the compiler
  498. const float thresholdMaxtrix[16] = { 1.0f / 17.0f, 9.0f / 17.0f, 3.0f / 17.0f, 11.0f / 17.0f,
  499. 13.0f / 17.0f, 5.0f / 17.0f, 15.0f / 17.0f, 7.0f / 17.0f,
  500. 4.0f / 17.0f, 12.0f / 17.0f, 2.0f / 17.0f, 10.0f / 17.0f,
  501. 16.0f / 17.0f, 8.0f / 17.0f, 14.0f / 17.0f, 6.0f / 17.0f};
  502. float threshold = thresholdMaxtrix[pixelId];
  503. if (alpha < threshold)
  504. {
  505. dither = false;
  506. }
  507. }
  508. gra_Branch if (dither)
  509. {
  510. #if (GRA_PACK_RESOLVE_OUTPUT==0)
  511. resolveTexture[writePos] = Granite_PackTileId(resolve);
  512. #else
  513. resolveTexture[writePos] = resolve;
  514. #endif
  515. }
  516. }
  517. #endif
  518. float GranitePrivate_CalcMiplevelAnisotropic(in GraniteTilesetConstantBuffer tsCB, in GraniteStreamingTextureConstantBuffer grSTCB, in gra_Float2 ddxTc, in gra_Float2 ddyTc)
  519. {
  520. // Calculate the required mipmap level, this uses a similar
  521. // formula as the GL spec.
  522. // To reduce sqrt's and log2's we do some stuff in squared space here and further below in log space
  523. // i.e. we wait with the sqrt untill we can do it for 'free' later during the log2
  524. ddxTc *= gra_CalcMiplevelDeltaScale;
  525. ddyTc *= gra_CalcMiplevelDeltaScale;
  526. float lenDxSqr = dot(ddxTc, ddxTc);
  527. float lenDySqr = dot(ddyTc, ddyTc);
  528. float dMaxSqr = max(lenDxSqr, lenDySqr);
  529. float dMinSqr = min(lenDxSqr, lenDySqr);
  530. // Calculate mipmap levels directly from sqared distances. This uses log2(sqrt(x)) = 0.5 * log2(x) to save some sqrt's
  531. float maxLevel = 0.5 * log2( dMaxSqr );
  532. float minLevel = 0.5 * log2( dMinSqr );
  533. // Calculate the log2 of the anisotropy and clamp it by the max supported. This uses log2(a/b) = log2(a)-log2(b) and min(log(a),log(b)) = log(min(a,b))
  534. float anisoLog2 = maxLevel - minLevel;
  535. anisoLog2 = min( anisoLog2, gra_MaxAnisotropyLog2 );
  536. // Adjust for anisotropy & clamp to level 0
  537. float result = max(maxLevel - anisoLog2 - 0.5f, 0.0f); //Subtract 0.5 to compensate for trilinear mipmapping
  538. // Added clamping to avoid "hot pink" on small tilesets that try to sample past the 1x1 tile miplevel
  539. // This happens if you for example import a relatively small texture and zoom out
  540. return min(result, gra_NumLevels);
  541. }
  542. float GranitePrivate_CalcMiplevelLinear(in GraniteTilesetConstantBuffer tsCB, in GraniteStreamingTextureConstantBuffer grSTCB, in gra_Float2 ddxTc, in gra_Float2 ddyTc)
  543. {
  544. // Calculate the required mipmap level, this uses a similar
  545. // formula as the GL spec.
  546. // To reduce sqrt's and log2's we do some stuff in squared space here and further below in log space
  547. // i.e. we wait with the sqrt untill we can do it for 'free' later during the log2
  548. ddxTc *= gra_CalcMiplevelDeltaScale;
  549. ddyTc *= gra_CalcMiplevelDeltaScale;
  550. float lenDxSqr = dot(ddxTc, ddxTc);
  551. float lenDySqr = dot(ddyTc, ddyTc);
  552. float dMaxSqr = max(lenDxSqr, lenDySqr);
  553. // Calculate mipmap levels directly from squared distances. This uses log2(sqrt(x)) = 0.5 * log2(x) to save some sqrt's
  554. float maxLevel = 0.5 * log2(dMaxSqr) - 0.5f; //Subtract 0.5 to compensate for trilinear mipmapping
  555. return clamp(maxLevel, 0.0f, gra_NumLevels);
  556. }
  557. gra_Float4 GranitePrivate_PackTileId(in gra_Float2 tileXY, in float level, in float textureID)
  558. {
  559. #if GRA_64BIT_RESOLVER == 0
  560. gra_Float4 resultBits;
  561. resultBits.x = fmod(tileXY.x, 256.0f);
  562. resultBits.y = floor(tileXY.x / 256.0f) + fmod(tileXY.y, 32.0f) * 8.0f;
  563. resultBits.z = floor(tileXY.y / 32.0f) + fmod(level, 4.0f) * 64.0f;
  564. resultBits.w = floor(level / 4.0f) + textureID * 4.0f;
  565. const float scale = 1.0f / 255.0f;
  566. #if GRA_BGRA == 0
  567. return scale * gra_Float4
  568. (
  569. float(resultBits.x),
  570. float(resultBits.y),
  571. float(resultBits.z),
  572. float(resultBits.w)
  573. );
  574. #else
  575. return scale * gra_Float4
  576. (
  577. float(resultBits.z),
  578. float(resultBits.y),
  579. float(resultBits.x),
  580. float(resultBits.w)
  581. );
  582. #endif
  583. #else
  584. const float scale = 1.0f / 65535.0f;
  585. return gra_Float4(tileXY.x, tileXY.y, level, textureID) * scale;
  586. #endif
  587. }
  588. gra_Float4 GranitePrivate_UnpackTileId(in gra_Float4 packedTile)
  589. {
  590. gra_Float4 swiz;
  591. #if GRA_BGRA == 0
  592. swiz = packedTile;
  593. #else
  594. swiz = packedTile.zyxw;
  595. #endif
  596. swiz *= 255.0f;
  597. float tileX = swiz.x + fmod(swiz.y, 16.0f) * 256.0f;
  598. float tileY = floor(swiz.y / 16.0f) + swiz.z * 16.0f;
  599. float level = fmod(swiz.w, 16.0f);
  600. float tex = floor(swiz.w / 16.0f);
  601. return gra_Float4(tileX, tileY, level, tex);
  602. }
  603. gra_Float3 GranitePrivate_TranslateCoord(in GraniteTilesetConstantBuffer tsCB, in gra_Float2 inputTexCoord, in gra_Float4 translationData, in int layer, out gra_Float2 numPagesOnLevel)
  604. {
  605. // The translation table contains uint32_t values so we have to get to the individual bits of the float data
  606. uint data = GranitePrivate_FloatAsUint(translationData[layer]);
  607. // Slice Index: 7 bits, Cache X: 10 bits, Cache Y: 10 bits, Tile Level: 4 bits
  608. uint slice = (data >> 24u) & 0x7Fu;
  609. uint cacheX = (data >> 14u) & 0x3FFu;
  610. uint cacheY = (data >> 4u) & 0x3FFu;
  611. uint revLevel = data & 0xFu;
  612. gra_Float2 numTilesOnLevel;
  613. numTilesOnLevel.x = GranitePrivate_Pow2(revLevel);
  614. numTilesOnLevel.y = numTilesOnLevel.x * gra_NumTilesYScale;
  615. gra_Float2 tileTexCoord = frac(inputTexCoord * numTilesOnLevel);
  616. gra_Float2 tileTexCoordCache = tileTexCoord * gra_TileContentInTiles + gra_Float2(cacheX, cacheY);
  617. gra_Float3 final = gra_Float3(tileTexCoordCache * gra_RcpCacheInTiles(layer) + gra_BorderPixelsRcpCache(layer), slice);
  618. numPagesOnLevel = numTilesOnLevel * gra_TileContentInTiles * gra_RcpCacheInTiles(layer);
  619. return final;
  620. }
  621. gra_Float4 GranitePrivate_DrawDebugTiles(in gra_Float4 sourceColor, in gra_Float2 textureCoord, in gra_Float2 numPagesOnLevel)
  622. {
  623. // Calculate the border values
  624. gra_Float2 cacheOffs = frac(textureCoord * numPagesOnLevel);
  625. float borderTemp = max(cacheOffs.x, 1.0-cacheOffs.x);
  626. borderTemp = max(max(cacheOffs.y, 1.0-cacheOffs.y), borderTemp);
  627. float border = smoothstep(0.98, 0.99, borderTemp);
  628. // White
  629. gra_Float4 borderColor = gra_Float4(1,1,1,1);
  630. //Lerp it over the source color
  631. return lerp(sourceColor, borderColor, border);
  632. }
  633. gra_Float4 GranitePrivate_MakeResolveOutput(in GraniteTilesetConstantBuffer tsCB, in gra_Float2 tileXY, in float level)
  634. {
  635. #if GRA_PACK_RESOLVE_OUTPUT
  636. return GranitePrivate_PackTileId(tileXY, level, gra_TextureId);
  637. #else
  638. return gra_Float4(tileXY, level, gra_TextureId);
  639. #endif
  640. }
  641. gra_Float4 GranitePrivate_ResolverPixel(in GraniteTilesetConstantBuffer tsCB, in gra_Float2 inputTexCoord, in float LOD)
  642. {
  643. float level = floor(LOD + 0.5f);
  644. // Number of tiles on level zero
  645. gra_Float2 level0NumTiles;
  646. level0NumTiles.x = gra_Level0NumTilesX;
  647. level0NumTiles.y = gra_Level0NumTilesX * gra_NumTilesYScale;
  648. // Calculate xy of the tiles to load
  649. gra_Float2 virtualTilesUv = floor(inputTexCoord * level0NumTiles * pow(0.5, level));
  650. return GranitePrivate_MakeResolveOutput(tsCB, virtualTilesUv, level);
  651. }
  652. void GranitePrivate_CalculateCubemapCoordinates(in gra_Float3 inputTexCoord, in gra_Float3 dVx, in gra_Float3 dVy, in GraniteStreamingTextureCubeConstantBuffer transforms, out int faceIdx, out gra_Float2 texCoord, out gra_Float2 dX, out gra_Float2 dY)
  653. {
  654. gra_Float2 contTexCoord;
  655. gra_Float3 derivX;
  656. gra_Float3 derivY;
  657. float majorAxis;
  658. if (abs(inputTexCoord.z) >= abs(inputTexCoord.x) && abs(inputTexCoord.z) >= abs(inputTexCoord.y))
  659. {
  660. // Z major axis
  661. if(inputTexCoord.z < 0.0)
  662. {
  663. faceIdx = 5;
  664. texCoord.x = -inputTexCoord.x;
  665. }
  666. else
  667. {
  668. faceIdx = 4;
  669. texCoord.x = inputTexCoord.x;
  670. }
  671. texCoord.y = -inputTexCoord.y;
  672. majorAxis = inputTexCoord.z;
  673. contTexCoord = gra_Float2(inputTexCoord.x, inputTexCoord.y);
  674. derivX = gra_Float3(dVx.x, dVx.y, dVx.z);
  675. derivY = gra_Float3(dVy.x, dVy.y, dVy.z);
  676. }
  677. else if (abs(inputTexCoord.y) >= abs(inputTexCoord.x))
  678. {
  679. // Y major axis
  680. if(inputTexCoord.y < 0.0)
  681. {
  682. faceIdx = 3;
  683. texCoord.y = -inputTexCoord.z;
  684. }
  685. else
  686. {
  687. faceIdx = 2;
  688. texCoord.y = inputTexCoord.z;
  689. }
  690. texCoord.x = inputTexCoord.x;
  691. majorAxis = inputTexCoord.y;
  692. contTexCoord = gra_Float2(inputTexCoord.x, inputTexCoord.z);
  693. derivX = gra_Float3(dVx.x, dVx.z, dVx.y);
  694. derivY = gra_Float3(dVy.x, dVy.z, dVy.y);
  695. }
  696. else
  697. {
  698. // X major axis
  699. if(inputTexCoord.x < 0.0)
  700. {
  701. faceIdx = 1;
  702. texCoord.x = inputTexCoord.z;
  703. }
  704. else
  705. {
  706. faceIdx = 0;
  707. texCoord.x = -inputTexCoord.z;
  708. }
  709. texCoord.y = -inputTexCoord.y;
  710. majorAxis = inputTexCoord.x;
  711. contTexCoord = gra_Float2(inputTexCoord.z, inputTexCoord.y);
  712. derivX = gra_Float3(dVx.z, dVx.y, dVx.x);
  713. derivY = gra_Float3(dVy.z, dVy.y, dVy.x);
  714. }
  715. texCoord = (texCoord + majorAxis) / (2.0 * abs(majorAxis));
  716. #if GRA_HQ_CUBEMAPPING
  717. dX = /*contTexCoord **/ ((contTexCoord + derivX.xy) / ( 2.0 * (majorAxis + derivX.z)) - (contTexCoord / (2.0 * majorAxis)));
  718. dY = /*contTexCoord **/ ((contTexCoord + derivY.xy) / ( 2.0 * (majorAxis + derivY.z)) - (contTexCoord / (2.0 * majorAxis)));
  719. #else
  720. dX = ((/*contTexCoord **/ derivX.xy) / (2.0 * abs(majorAxis)));
  721. dY = ((/*contTexCoord **/ derivY.xy) / (2.0 * abs(majorAxis)));
  722. #endif
  723. // Now scale the derivatives with the texture transform scale
  724. dX *= transforms.data[faceIdx].data[0].zw;
  725. dY *= transforms.data[faceIdx].data[0].zw;
  726. }
  727. // Auto-level
  728. void GranitePrivate_CalculateCubemapCoordinates(in gra_Float3 inputTexCoord, in GraniteStreamingTextureCubeConstantBuffer transforms, out int faceIdx, out gra_Float2 texCoord, out gra_Float2 dX, out gra_Float2 dY)
  729. {
  730. gra_Float3 dVx = ddx(inputTexCoord);
  731. gra_Float3 dVy = ddy(inputTexCoord);
  732. GranitePrivate_CalculateCubemapCoordinates(inputTexCoord, dVx, dVy, transforms, faceIdx, texCoord, dX, dY);
  733. }
  734. gra_Float2 Granite_GetTextureDimensions(in GraniteStreamingTextureConstantBuffer grSTCB)
  735. {
  736. return gra_Float2(1.0 / gra_AssetWidthRcp, 1.0 / gra_AssetHeightRcp); //TODO(ddebaets) use HLSL rcp here
  737. }