torque.hlsl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TORQUE_HLSL_
  23. #define _TORQUE_HLSL_
  24. #include "./shaderModel.hlsl"
  25. static float M_HALFPI_F = 1.57079632679489661923f;
  26. static float M_PI_F = 3.14159265358979323846f;
  27. static float M_2PI_F = 6.28318530717958647692f;
  28. /// Calculate fog based on a start and end positions in worldSpace.
  29. float computeSceneFog( float3 startPos,
  30. float3 endPos,
  31. float fogDensity,
  32. float fogDensityOffset,
  33. float fogHeightFalloff )
  34. {
  35. float f = length( startPos - endPos ) - fogDensityOffset;
  36. float h = 1.0 - ( endPos.z * fogHeightFalloff );
  37. return exp( -fogDensity * f * h );
  38. }
  39. /// Calculate fog based on a start and end position and a height.
  40. /// Positions do not need to be in worldSpace but height does.
  41. float computeSceneFog( float3 startPos,
  42. float3 endPos,
  43. float height,
  44. float fogDensity,
  45. float fogDensityOffset,
  46. float fogHeightFalloff )
  47. {
  48. float f = length( startPos - endPos ) - fogDensityOffset;
  49. float h = 1.0 - ( height * fogHeightFalloff );
  50. return exp( -fogDensity * f * h );
  51. }
  52. /// Calculate fog based on a distance, height is not used.
  53. float computeSceneFog( float dist, float fogDensity, float fogDensityOffset )
  54. {
  55. float f = dist - fogDensityOffset;
  56. return exp( -fogDensity * f );
  57. }
  58. /// Convert a float4 uv in viewport space to render target space.
  59. float2 viewportCoordToRenderTarget( float4 inCoord, float4 rtParams )
  60. {
  61. float2 outCoord = inCoord.xy / inCoord.w;
  62. outCoord = ( outCoord * rtParams.zw ) + rtParams.xy;
  63. return outCoord;
  64. }
  65. /// Convert a float2 uv in viewport space to render target space.
  66. float2 viewportCoordToRenderTarget( float2 inCoord, float4 rtParams )
  67. {
  68. float2 outCoord = ( inCoord * rtParams.zw ) + rtParams.xy;
  69. return outCoord;
  70. }
  71. /// Convert a float4 quaternion into a 3x3 matrix.
  72. float3x3 quatToMat( float4 quat )
  73. {
  74. float xs = quat.x * 2.0f;
  75. float ys = quat.y * 2.0f;
  76. float zs = quat.z * 2.0f;
  77. float wx = quat.w * xs;
  78. float wy = quat.w * ys;
  79. float wz = quat.w * zs;
  80. float xx = quat.x * xs;
  81. float xy = quat.x * ys;
  82. float xz = quat.x * zs;
  83. float yy = quat.y * ys;
  84. float yz = quat.y * zs;
  85. float zz = quat.z * zs;
  86. float3x3 mat;
  87. mat[0][0] = 1.0f - (yy + zz);
  88. mat[0][1] = xy - wz;
  89. mat[0][2] = xz + wy;
  90. mat[1][0] = xy + wz;
  91. mat[1][1] = 1.0f - (xx + zz);
  92. mat[1][2] = yz - wx;
  93. mat[2][0] = xz - wy;
  94. mat[2][1] = yz + wx;
  95. mat[2][2] = 1.0f - (xx + yy);
  96. return mat;
  97. }
  98. /// The number of additional substeps we take when refining
  99. /// the results of the offset parallax mapping function below.
  100. ///
  101. /// You should turn down the number of steps if your needing
  102. /// more performance out of your parallax surfaces. Increasing
  103. /// the number doesn't yeild much better results and is rarely
  104. /// worth the additional cost.
  105. ///
  106. #define PARALLAX_REFINE_STEPS 3
  107. /// Performs fast parallax offset mapping using
  108. /// multiple refinement steps.
  109. ///
  110. /// @param texMap The texture map whos alpha channel we sample the parallax depth.
  111. /// @param texCoord The incoming texture coordinate for sampling the parallax depth.
  112. /// @param negViewTS The negative view vector in tangent space.
  113. /// @param depthScale The parallax factor used to scale the depth result.
  114. ///
  115. float2 parallaxOffset(TORQUE_SAMPLER2D(texMap), float2 texCoord, float3 negViewTS, float depthScale)
  116. {
  117. float depth = TORQUE_TEX2D(texMap, texCoord).a/(PARALLAX_REFINE_STEPS*2);
  118. float2 offset = negViewTS.xy * (depth * depthScale)/(PARALLAX_REFINE_STEPS);
  119. for (int i = 0; i < PARALLAX_REFINE_STEPS; i++)
  120. {
  121. depth = (depth + TORQUE_TEX2D(texMap, texCoord + offset).a)/(PARALLAX_REFINE_STEPS*2);
  122. offset = negViewTS.xy * (depth * depthScale)/(PARALLAX_REFINE_STEPS);
  123. }
  124. return offset;
  125. }
  126. /// Same as parallaxOffset but for dxtnm where depth is stored in the red channel instead of the alpha
  127. float2 parallaxOffsetDxtnm(TORQUE_SAMPLER2D(texMap), float2 texCoord, float3 negViewTS, float depthScale)
  128. {
  129. float depth = TORQUE_TEX2D(texMap, texCoord).r/(PARALLAX_REFINE_STEPS*2);
  130. float2 offset = negViewTS.xy * (depth * depthScale)/(PARALLAX_REFINE_STEPS*2);
  131. for (int i = 0; i < PARALLAX_REFINE_STEPS; i++)
  132. {
  133. depth = (depth + TORQUE_TEX2D(texMap, texCoord + offset).r)/(PARALLAX_REFINE_STEPS*2);
  134. offset = negViewTS.xy * (depth * depthScale)/(PARALLAX_REFINE_STEPS*2);
  135. }
  136. return offset;
  137. }
  138. /// The maximum value for 16bit per component integer HDR encoding.
  139. static const float HDR_RGB16_MAX = 100.0;
  140. /// The maximum value for 10bit per component integer HDR encoding.
  141. static const float HDR_RGB10_MAX = 4.0;
  142. /// Encodes an HDR color for storage into a target.
  143. float3 hdrEncode( float3 sample )
  144. {
  145. #if defined( TORQUE_HDR_RGB16 )
  146. return sample / HDR_RGB16_MAX;
  147. #elif defined( TORQUE_HDR_RGB10 )
  148. return sample / HDR_RGB10_MAX;
  149. #else
  150. // No encoding.
  151. return sample;
  152. #endif
  153. }
  154. /// Encodes an HDR color for storage into a target.
  155. float4 hdrEncode( float4 sample )
  156. {
  157. return float4( hdrEncode( sample.rgb ), sample.a );
  158. }
  159. /// Decodes an HDR color from a target.
  160. float3 hdrDecode( float3 sample )
  161. {
  162. #if defined( TORQUE_HDR_RGB16 )
  163. return sample * HDR_RGB16_MAX;
  164. #elif defined( TORQUE_HDR_RGB10 )
  165. return sample * HDR_RGB10_MAX;
  166. #else
  167. // No encoding.
  168. return sample;
  169. #endif
  170. }
  171. /// Decodes an HDR color from a target.
  172. float4 hdrDecode( float4 sample )
  173. {
  174. return float4( hdrDecode( sample.rgb ), sample.a );
  175. }
  176. /// Returns the luminance for an HDR pixel.
  177. float hdrLuminance( float3 sample )
  178. {
  179. // There are quite a few different ways to
  180. // calculate luminance from an rgb value.
  181. //
  182. // If you want to use a different technique
  183. // then plug it in here.
  184. //
  185. ////////////////////////////////////////////////////////////////////////////
  186. //
  187. // Max component luminance.
  188. //
  189. //float lum = max( sample.r, max( sample.g, sample.b ) );
  190. ////////////////////////////////////////////////////////////////////////////
  191. // The perceptual relative luminance.
  192. //
  193. // See http://en.wikipedia.org/wiki/Luminance_(relative)
  194. //
  195. const float3 RELATIVE_LUMINANCE = float3( 0.2126, 0.7152, 0.0722 );
  196. float lum = dot( sample, RELATIVE_LUMINANCE );
  197. ////////////////////////////////////////////////////////////////////////////
  198. //
  199. // The average component luminance.
  200. //
  201. //const float3 AVERAGE_LUMINANCE = float3( 0.3333, 0.3333, 0.3333 );
  202. //float lum = dot( sample, AVERAGE_LUMINANCE );
  203. return lum;
  204. }
  205. /// Called from the visibility feature to do screen
  206. /// door transparency for fading of objects.
  207. void fizzle(float2 vpos, float visibility)
  208. {
  209. // NOTE: The magic values below are what give us
  210. // the nice even pattern during the fizzle.
  211. //
  212. // These values can be changed to get different
  213. // patterns... some better than others.
  214. //
  215. // Horizontal Blinds - { vpos.x, 0.916, vpos.y, 0 }
  216. // Vertical Lines - { vpos.x, 12.9898, vpos.y, 78.233 }
  217. //
  218. // I'm sure there are many more patterns here to
  219. // discover for different effects.
  220. float2x2 m = { vpos.x, 0.916, vpos.y, 0.350 };
  221. clip( visibility - frac( determinant( m ) ) );
  222. }
  223. // Deferred Shading: Material Info Flag Check
  224. bool getFlag(float flags, int num)
  225. {
  226. int process = round(flags * 255);
  227. int squareNum = pow(2, num);
  228. return (fmod(process, pow(2, squareNum)) >= squareNum);
  229. }
  230. // #define TORQUE_STOCK_GAMMA
  231. #ifdef TORQUE_STOCK_GAMMA
  232. // Sample in linear space. Decodes gamma.
  233. float4 toLinear(float4 tex)
  234. {
  235. return tex;
  236. }
  237. // Encodes gamma.
  238. float4 toGamma(float4 tex)
  239. {
  240. return tex;
  241. }
  242. float3 toLinear(float3 tex)
  243. {
  244. return tex;
  245. }
  246. // Encodes gamma.
  247. float3 toGamma(float3 tex)
  248. {
  249. return tex;
  250. }
  251. float3 toLinear(float3 tex)
  252. {
  253. return tex;
  254. }
  255. // Encodes gamma.
  256. float3 toLinear(float3 tex)
  257. {
  258. return tex;
  259. }
  260. #else
  261. // Sample in linear space. Decodes gamma.
  262. float4 toLinear(float4 tex)
  263. {
  264. return float4(pow(abs(tex.rgb), 2.2), tex.a);
  265. }
  266. // Encodes gamma.
  267. float4 toGamma(float4 tex)
  268. {
  269. return float4(pow(abs(tex.rgb), 1.0/2.2), tex.a);
  270. }
  271. // Sample in linear space. Decodes gamma.
  272. float3 toLinear(float3 tex)
  273. {
  274. return pow(abs(tex.rgb), 2.2);
  275. }
  276. // Encodes gamma.
  277. float3 toGamma(float3 tex)
  278. {
  279. return pow(abs(tex.rgb), 1.0/2.2);
  280. }
  281. #endif //
  282. #endif // _TORQUE_HLSL_