Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

800 lines
33 KiB

  1. //=============================================================================
  2. // D3D11 HLSL Routines for Manual Pack/Unpack of 32-bit DXGI_FORMAT_*
  3. //=============================================================================
  4. //
  5. // This file contains format conversion routines for use in the
  6. // Compute Shader or Pixel Shader on D3D11 Hardware.
  7. //
  8. // Skip to the end of this comment to see a summary of the routines
  9. // provided. The rest of the text below explains why they are needed
  10. // and how to use them.
  11. //
  12. // The scenario where these can be useful is if your application
  13. // needs to simultaneously both read and write texture - i.e. in-place
  14. // image editing.
  15. //
  16. // D3D11's Unordered Access View (UAV) of a Texture1D/2D/3D resource
  17. // allows random access reads and writes to memory from a Compute Shader
  18. // or Pixel Shader. However, the only texture format that supports this
  19. // is DXGI_FORMAT_R32_UINT. e.g. Other more interesting formats like
  20. // DXGI_FORMAT_R8G8B8A8_UNORM do not support simultaneous read and
  21. // write. You can use such formats for random access writing only
  22. // using a UAV, or reading only using a Shader Resource View (SRV).
  23. // But for simultaneous read+write, the format conversion hardware is
  24. // not available.
  25. //
  26. // There is a workaround to this limitation, involving casting the texture
  27. // to R32_UINT when creating a UAV, as long as the original format of the
  28. // resource supports it (most 32 bit per element formats). This allows
  29. // simultaneous read+write as long as the shader does manual format
  30. // unpacking on read and packing on write.
  31. //
  32. // The benefit is that later on, other views such as RenderTarget Views
  33. // or ShaderResource Views on the same texture can be used with the
  34. // proper format (e.g. DXGI_FORMAT_R16G16_FLOAT) so the hardware can
  35. // do the usual automatic format unpack/pack and do texture filtering etc.
  36. // where there are no hardware limitations.
  37. //
  38. // The sequence of actions for an application is the following:
  39. //
  40. // Suppose you want to make a texture than you can use a Pixel Shader
  41. // or Compute Shader to perform in-place editing, and that the format
  42. // you want the data to be stored in happens to be a descendent
  43. // of of one of these formats:
  44. //
  45. // DXGI_FORMAT_R10G10B10A2_TYPELESS
  46. // DXGI_FORMAT_R8G8B8A8_TYPELESS
  47. // DXGI_FORMAT_B8G8R8A8_TYPELESS
  48. // DXGI_FORMAT_B8G8R8X8_TYPELESS
  49. // DXGI_FORMAT_R16G16_TYPELESS
  50. //
  51. // e.g. DXGI_FORMAT_R10G10B10A2_UNORM is a descendent of
  52. // DXGI_FORMAT_R10G10B10A2_TYPELESS, so it supports the
  53. // usage pattern described here.
  54. //
  55. // (Formats descending from DXGI_FORMAT_R32_TYPELESS, such as
  56. // DXGI_FORMAT_R32_FLOAT, are trivially supported without
  57. // needing any of the format conversion help provided here.)
  58. //
  59. // Steps:
  60. //
  61. // (1) Create a texture with the appropriate _TYPELESS format above
  62. // along with the needed bind flags, such as
  63. // D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE.
  64. //
  65. // (2) For in-place image editing, create a UAV with the format
  66. // DXGI_FORMAT_R32_UINT. D3D normally doesn't allow casting
  67. // between different format "families", but the API makes
  68. // an exception here.
  69. //
  70. // (3) In the Compute Shader or Pixel Shader, use the appropriate
  71. // format pack/unpack routines provided in this file.
  72. // For example if the DXGI_FORMAT_R32_UINT UAV really holds
  73. // DXGI_FORMAT_R10G10B10A2_UNORM data, then, after reading a
  74. // uint from the UAV into the shader, unpack by calling:
  75. //
  76. // XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput)
  77. //
  78. // Then to write to the UAV in the same shader, call the following
  79. // to pack shader data into a uint that can be written out:
  80. //
  81. // UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  82. //
  83. // (4) Other views, such as SRVs, can be created with the desired format;
  84. // e.g. DXGI_FORMAT_R10G10B10A2_UNORM if the resource was created as
  85. // DXGI_FORMAT_R10G10B10A2_TYPELESS. When that view is accessed by a
  86. // shader, the hardware can do automatic type conversion as usual.
  87. //
  88. // Note, again, that if the shader only needs to write to a UAV, or read
  89. // as an SRV, then none of this is needed - fully typed UAV or SRVs can
  90. // be used. Only if simultaneous reading and writing to a UAV of a texture
  91. // is needed are the format conversion routines provided here potentially
  92. // useful.
  93. //
  94. // The following is the list of format conversion routines included in this
  95. // file, categorized by the DXGI_FORMAT they unpack/pack. Each of the
  96. // formats supported descends from one of the TYPELESS formats listed
  97. // above, and supports casting to DXGI_FORMAT_R32_UINT as a UAV.
  98. //
  99. // DXGI_FORMAT_R10G10B10A2_UNORM:
  100. //
  101. // XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput)
  102. // UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  103. //
  104. // DXGI_FORMAT_R10G10B10A2_UINT:
  105. //
  106. // XMUINT4 D3DX_R10G10B10A2_UINT_to_UINT4(UINT packedInput)
  107. // UINT D3DX_UINT4_to_R10G10B10A2_UINT(XMUINT4 unpackedInput)
  108. //
  109. // DXGI_FORMAT_R8G8B8A8_UNORM:
  110. //
  111. // XMFLOAT4 D3DX_R8G8B8A8_UNORM_to_FLOAT4(UINT packedInput)
  112. // UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  113. //
  114. // DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
  115. //
  116. // XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput) *
  117. // XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput)
  118. // UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput)
  119. //
  120. // * The "_inexact" function above uses shader instructions that don't
  121. // have high enough precision to give the exact answer, albeit close.
  122. // The alternative function uses a lookup table stored in the shader
  123. // to give an exact SRGB->float conversion.
  124. //
  125. // DXGI_FORMAT_R8G8B8A8_UINT:
  126. //
  127. // XMUINT4 D3DX_R8G8B8A8_UINT_to_UINT4(UINT packedInput)
  128. // XMUINT D3DX_UINT4_to_R8G8B8A8_UINT(XMUINT4 unpackedInput)
  129. //
  130. // DXGI_FORMAT_R8G8B8A8_SNORM:
  131. //
  132. // XMFLOAT4 D3DX_R8G8B8A8_SNORM_to_FLOAT4(UINT packedInput)
  133. // UINT D3DX_FLOAT4_to_R8G8B8A8_SNORM(hlsl_precise XMFLOAT4 unpackedInput)
  134. //
  135. // DXGI_FORMAT_R8G8B8A8_SINT:
  136. //
  137. // XMINT4 D3DX_R8G8B8A8_SINT_to_INT4(UINT packedInput)
  138. // UINT D3DX_INT4_to_R8G8B8A8_SINT(XMINT4 unpackedInput)
  139. //
  140. // DXGI_FORMAT_B8G8R8A8_UNORM:
  141. //
  142. // XMFLOAT4 D3DX_B8G8R8A8_UNORM_to_FLOAT4(UINT packedInput)
  143. // UINT D3DX_FLOAT4_to_B8G8R8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  144. //
  145. // DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
  146. //
  147. // XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput) *
  148. // XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput)
  149. // UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput)
  150. //
  151. // * The "_inexact" function above uses shader instructions that don't
  152. // have high enough precision to give the exact answer, albeit close.
  153. // The alternative function uses a lookup table stored in the shader
  154. // to give an exact SRGB->float conversion.
  155. //
  156. // DXGI_FORMAT_B8G8R8X8_UNORM:
  157. //
  158. // XMFLOAT3 D3DX_B8G8R8X8_UNORM_to_FLOAT3(UINT packedInput)
  159. // UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM(hlsl_precise XMFLOAT3 unpackedInput)
  160. //
  161. // DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
  162. //
  163. // XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3_inexact(UINT packedInput) *
  164. // XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3(UINT packedInput)
  165. // UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM_SRGB(hlsl_precise XMFLOAT3 unpackedInput)
  166. //
  167. // * The "_inexact" function above uses shader instructions that don't
  168. // have high enough precision to give the exact answer, albeit close.
  169. // The alternative function uses a lookup table stored in the shader
  170. // to give an exact SRGB->float conversion.
  171. //
  172. // DXGI_FORMAT_R16G16_FLOAT:
  173. //
  174. // XMFLOAT2 D3DX_R16G16_FLOAT_to_FLOAT2(UINT packedInput)
  175. // UINT D3DX_FLOAT2_to_R16G16_FLOAT(hlsl_precise XMFLOAT2 unpackedInput)
  176. //
  177. // DXGI_FORMAT_R16G16_UNORM:
  178. //
  179. // XMFLOAT2 D3DX_R16G16_UNORM_to_FLOAT2(UINT packedInput)
  180. // UINT D3DX_FLOAT2_to_R16G16_UNORM(hlsl_precise FLOAT2 unpackedInput)
  181. //
  182. // DXGI_FORMAT_R16G16_UINT:
  183. //
  184. // XMUINT2 D3DX_R16G16_UINT_to_UINT2(UINT packedInput)
  185. // UINT D3DX_UINT2_to_R16G16_UINT(XMUINT2 unpackedInput)
  186. //
  187. // DXGI_FORMAT_R16G16_SNORM:
  188. //
  189. // XMFLOAT2 D3DX_R16G16_SNORM_to_FLOAT2(UINT packedInput)
  190. // UINT D3DX_FLOAT2_to_R16G16_SNORM(hlsl_precise XMFLOAT2 unpackedInput)
  191. //
  192. // DXGI_FORMAT_R16G16_SINT:
  193. //
  194. // XMINT2 D3DX_R16G16_SINT_to_INT2(UINT packedInput)
  195. // UINT D3DX_INT2_to_R16G16_SINT(XMINT2 unpackedInput)
  196. //
  197. //=============================================================================
  198. #ifndef __D3DX_DXGI_FORMAT_CONVERT_INL___
  199. #define __D3DX_DXGI_FORMAT_CONVERT_INL___
  200. #if HLSL_VERSION > 0
  201. #define D3DX11INLINE
  202. typedef int INT;
  203. typedef uint UINT;
  204. typedef float2 XMFLOAT2;
  205. typedef float3 XMFLOAT3;
  206. typedef float4 XMFLOAT4;
  207. typedef int2 XMINT2;
  208. typedef int4 XMINT4;
  209. typedef uint2 XMUINT2;
  210. typedef uint4 XMUINT4;
  211. #define hlsl_precise precise
  212. #define D3DX_Saturate_FLOAT(_V) saturate(_V)
  213. #define D3DX_IsNan(_V) isnan(_V)
  214. #define D3DX_Truncate_FLOAT(_V) trunc(_V)
  215. #else // HLSL_VERSION > 0
  216. #ifndef __cplusplus
  217. #error C++ compilation required
  218. #endif
  219. #include <float.h>
  220. #include <xnamath.h>
  221. #define hlsl_precise
  222. D3DX11INLINE FLOAT D3DX_Saturate_FLOAT(FLOAT _V)
  223. {
  224. return min(max(_V, 0), 1);
  225. }
  226. D3DX11INLINE bool D3DX_IsNan(FLOAT _V)
  227. {
  228. return _V != _V;
  229. }
  230. D3DX11INLINE FLOAT D3DX_Truncate_FLOAT(FLOAT _V)
  231. {
  232. return _V >= 0 ? floor(_V) : ceil(_V);
  233. }
  234. // 2D Vector; 32 bit signed integer components
  235. typedef struct _XMINT2
  236. {
  237. INT x;
  238. INT y;
  239. } XMINT2;
  240. // 2D Vector; 32 bit unsigned integer components
  241. typedef struct _XMUINT2
  242. {
  243. UINT x;
  244. UINT y;
  245. } XMUINT2;
  246. // 4D Vector; 32 bit signed integer components
  247. typedef struct _XMINT4
  248. {
  249. INT x;
  250. INT y;
  251. INT z;
  252. INT w;
  253. } XMINT4;
  254. // 4D Vector; 32 bit unsigned integer components
  255. typedef struct _XMUINT4
  256. {
  257. UINT x;
  258. UINT y;
  259. UINT z;
  260. UINT w;
  261. } XMUINT4;
  262. #endif // HLSL_VERSION > 0
  263. //=============================================================================
  264. // SRGB Helper Functions Called By Conversions Further Below.
  265. //=============================================================================
  266. // SRGB_to_FLOAT_inexact is imprecise due to precision of pow implementations.
  267. // If exact SRGB->float conversion is needed, a table lookup is provided
  268. // further below.
  269. D3DX11INLINE FLOAT D3DX_SRGB_to_FLOAT_inexact(hlsl_precise FLOAT val)
  270. {
  271. if( val < 0.04045f )
  272. val /= 12.92f;
  273. else
  274. val = pow((val + 0.055f)/1.055f,2.4f);
  275. return val;
  276. }
  277. static const UINT D3DX_SRGBTable[] =
  278. {
  279. 0x00000000,0x399f22b4,0x3a1f22b4,0x3a6eb40e,0x3a9f22b4,0x3ac6eb61,0x3aeeb40e,0x3b0b3e5d,
  280. 0x3b1f22b4,0x3b33070b,0x3b46eb61,0x3b5b518d,0x3b70f18d,0x3b83e1c6,0x3b8fe616,0x3b9c87fd,
  281. 0x3ba9c9b7,0x3bb7ad6f,0x3bc63549,0x3bd56361,0x3be539c1,0x3bf5ba70,0x3c0373b5,0x3c0c6152,
  282. 0x3c15a703,0x3c1f45be,0x3c293e6b,0x3c3391f7,0x3c3e4149,0x3c494d43,0x3c54b6c7,0x3c607eb1,
  283. 0x3c6ca5df,0x3c792d22,0x3c830aa8,0x3c89af9f,0x3c9085db,0x3c978dc5,0x3c9ec7c2,0x3ca63433,
  284. 0x3cadd37d,0x3cb5a601,0x3cbdac20,0x3cc5e639,0x3cce54ab,0x3cd6f7d5,0x3cdfd010,0x3ce8ddb9,
  285. 0x3cf2212c,0x3cfb9ac1,0x3d02a569,0x3d0798dc,0x3d0ca7e6,0x3d11d2af,0x3d171963,0x3d1c7c2e,
  286. 0x3d21fb3c,0x3d2796b2,0x3d2d4ebb,0x3d332380,0x3d39152b,0x3d3f23e3,0x3d454fd1,0x3d4b991c,
  287. 0x3d51ffef,0x3d58846a,0x3d5f26b7,0x3d65e6fe,0x3d6cc564,0x3d73c20f,0x3d7add29,0x3d810b67,
  288. 0x3d84b795,0x3d887330,0x3d8c3e4a,0x3d9018f6,0x3d940345,0x3d97fd4a,0x3d9c0716,0x3da020bb,
  289. 0x3da44a4b,0x3da883d7,0x3daccd70,0x3db12728,0x3db59112,0x3dba0b3b,0x3dbe95b5,0x3dc33092,
  290. 0x3dc7dbe2,0x3dcc97b6,0x3dd1641f,0x3dd6412c,0x3ddb2eef,0x3de02d77,0x3de53cd5,0x3dea5d19,
  291. 0x3def8e52,0x3df4d091,0x3dfa23e8,0x3dff8861,0x3e027f07,0x3e054280,0x3e080ea3,0x3e0ae378,
  292. 0x3e0dc105,0x3e10a754,0x3e13966b,0x3e168e52,0x3e198f10,0x3e1c98ad,0x3e1fab30,0x3e22c6a3,
  293. 0x3e25eb09,0x3e29186c,0x3e2c4ed0,0x3e2f8e41,0x3e32d6c4,0x3e362861,0x3e39831e,0x3e3ce703,
  294. 0x3e405416,0x3e43ca5f,0x3e4749e4,0x3e4ad2ae,0x3e4e64c2,0x3e520027,0x3e55a4e6,0x3e595303,
  295. 0x3e5d0a8b,0x3e60cb7c,0x3e6495e0,0x3e6869bf,0x3e6c4720,0x3e702e0c,0x3e741e84,0x3e781890,
  296. 0x3e7c1c38,0x3e8014c2,0x3e82203c,0x3e84308d,0x3e8645ba,0x3e885fc5,0x3e8a7eb2,0x3e8ca283,
  297. 0x3e8ecb3d,0x3e90f8e1,0x3e932b74,0x3e9562f8,0x3e979f71,0x3e99e0e2,0x3e9c274e,0x3e9e72b7,
  298. 0x3ea0c322,0x3ea31892,0x3ea57308,0x3ea7d289,0x3eaa3718,0x3eaca0b7,0x3eaf0f69,0x3eb18333,
  299. 0x3eb3fc18,0x3eb67a18,0x3eb8fd37,0x3ebb8579,0x3ebe12e1,0x3ec0a571,0x3ec33d2d,0x3ec5da17,
  300. 0x3ec87c33,0x3ecb2383,0x3ecdd00b,0x3ed081cd,0x3ed338cc,0x3ed5f50b,0x3ed8b68d,0x3edb7d54,
  301. 0x3ede4965,0x3ee11ac1,0x3ee3f16b,0x3ee6cd67,0x3ee9aeb6,0x3eec955d,0x3eef815d,0x3ef272ba,
  302. 0x3ef56976,0x3ef86594,0x3efb6717,0x3efe6e02,0x3f00bd2d,0x3f02460e,0x3f03d1a7,0x3f055ff9,
  303. 0x3f06f106,0x3f0884cf,0x3f0a1b56,0x3f0bb49b,0x3f0d50a0,0x3f0eef67,0x3f1090f1,0x3f12353e,
  304. 0x3f13dc51,0x3f15862b,0x3f1732cd,0x3f18e239,0x3f1a946f,0x3f1c4971,0x3f1e0141,0x3f1fbbdf,
  305. 0x3f21794e,0x3f23398e,0x3f24fca0,0x3f26c286,0x3f288b41,0x3f2a56d3,0x3f2c253d,0x3f2df680,
  306. 0x3f2fca9e,0x3f31a197,0x3f337b6c,0x3f355820,0x3f3737b3,0x3f391a26,0x3f3aff7c,0x3f3ce7b5,
  307. 0x3f3ed2d2,0x3f40c0d4,0x3f42b1be,0x3f44a590,0x3f469c4b,0x3f4895f1,0x3f4a9282,0x3f4c9201,
  308. 0x3f4e946e,0x3f5099cb,0x3f52a218,0x3f54ad57,0x3f56bb8a,0x3f58ccb0,0x3f5ae0cd,0x3f5cf7e0,
  309. 0x3f5f11ec,0x3f612eee,0x3f634eef,0x3f6571e9,0x3f6797e3,0x3f69c0d6,0x3f6beccd,0x3f6e1bbf,
  310. 0x3f704db8,0x3f7282af,0x3f74baae,0x3f76f5ae,0x3f7933b9,0x3f7b74c6,0x3f7db8e0,0x3f800000
  311. };
  312. D3DX11INLINE FLOAT D3DX_SRGB_to_FLOAT(UINT val)
  313. {
  314. #if HLSL_VERSION > 0
  315. return asfloat(D3DX_SRGBTable[val]);
  316. #else
  317. return *(FLOAT*)&D3DX_SRGBTable[val];
  318. #endif
  319. }
  320. D3DX11INLINE FLOAT D3DX_FLOAT_to_SRGB(hlsl_precise FLOAT val)
  321. {
  322. if( val < 0.0031308f )
  323. val *= 12.92f;
  324. else
  325. val = 1.055f * pow(val,1.0f/2.4f) - 0.055f;
  326. return val;
  327. }
  328. D3DX11INLINE FLOAT D3DX_SaturateSigned_FLOAT(FLOAT _V)
  329. {
  330. if (D3DX_IsNan(_V))
  331. {
  332. return 0;
  333. }
  334. return min(max(_V, -1), 1);
  335. }
  336. D3DX11INLINE UINT D3DX_FLOAT_to_UINT(FLOAT _V,
  337. FLOAT _Scale)
  338. {
  339. return (UINT)floor(_V * _Scale + 0.5f);
  340. }
  341. D3DX11INLINE FLOAT D3DX_INT_to_FLOAT(INT _V,
  342. FLOAT _Scale)
  343. {
  344. FLOAT Scaled = (FLOAT)_V / _Scale;
  345. // The integer is a two's-complement signed
  346. // number so the negative range is slightly
  347. // larger than the positive range, meaning
  348. // the scaled value can be slight less than -1.
  349. // Clamp to keep the float range [-1, 1].
  350. return max(Scaled, -1.0f);
  351. }
  352. D3DX11INLINE INT D3DX_FLOAT_to_INT(FLOAT _V,
  353. FLOAT _Scale)
  354. {
  355. return (INT)D3DX_Truncate_FLOAT(_V * _Scale + (_V >= 0 ? 0.5f : -0.5f));
  356. }
  357. //=============================================================================
  358. // Conversion routines
  359. //=============================================================================
  360. //-----------------------------------------------------------------------------
  361. // R10B10G10A2_UNORM <-> FLOAT4
  362. //-----------------------------------------------------------------------------
  363. D3DX11INLINE XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput)
  364. {
  365. hlsl_precise XMFLOAT4 unpackedOutput;
  366. unpackedOutput.x = (FLOAT) (packedInput & 0x000003ff) / 1023;
  367. unpackedOutput.y = (FLOAT)(((packedInput>>10) & 0x000003ff)) / 1023;
  368. unpackedOutput.z = (FLOAT)(((packedInput>>20) & 0x000003ff)) / 1023;
  369. unpackedOutput.w = (FLOAT)(((packedInput>>30) & 0x00000003)) / 3;
  370. return unpackedOutput;
  371. }
  372. D3DX11INLINE UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  373. {
  374. UINT packedOutput;
  375. packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 1023)) |
  376. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 1023)<<10) |
  377. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 1023)<<20) |
  378. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.w), 3)<<30) );
  379. return packedOutput;
  380. }
  381. //-----------------------------------------------------------------------------
  382. // R10B10G10A2_UINT <-> UINT4
  383. //-----------------------------------------------------------------------------
  384. D3DX11INLINE XMUINT4 D3DX_R10G10B10A2_UINT_to_UINT4(UINT packedInput)
  385. {
  386. XMUINT4 unpackedOutput;
  387. unpackedOutput.x = packedInput & 0x000003ff;
  388. unpackedOutput.y = (packedInput>>10) & 0x000003ff;
  389. unpackedOutput.z = (packedInput>>20) & 0x000003ff;
  390. unpackedOutput.w = (packedInput>>30) & 0x00000003;
  391. return unpackedOutput;
  392. }
  393. D3DX11INLINE UINT D3DX_UINT4_to_R10G10B10A2_UINT(XMUINT4 unpackedInput)
  394. {
  395. UINT packedOutput;
  396. unpackedInput.x = min(unpackedInput.x, 0x000003ff);
  397. unpackedInput.y = min(unpackedInput.y, 0x000003ff);
  398. unpackedInput.z = min(unpackedInput.z, 0x000003ff);
  399. unpackedInput.w = min(unpackedInput.w, 0x00000003);
  400. packedOutput = ( (unpackedInput.x) |
  401. ((unpackedInput.y)<<10) |
  402. ((unpackedInput.z)<<20) |
  403. ((unpackedInput.w)<<30) );
  404. return packedOutput;
  405. }
  406. //-----------------------------------------------------------------------------
  407. // R8G8B8A8_UNORM <-> FLOAT4
  408. //-----------------------------------------------------------------------------
  409. D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_UNORM_to_FLOAT4(UINT packedInput)
  410. {
  411. hlsl_precise XMFLOAT4 unpackedOutput;
  412. unpackedOutput.x = (FLOAT) (packedInput & 0x000000ff) / 255;
  413. unpackedOutput.y = (FLOAT)(((packedInput>> 8) & 0x000000ff)) / 255;
  414. unpackedOutput.z = (FLOAT)(((packedInput>>16) & 0x000000ff)) / 255;
  415. unpackedOutput.w = (FLOAT) (packedInput>>24) / 255;
  416. return unpackedOutput;
  417. }
  418. D3DX11INLINE UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  419. {
  420. UINT packedOutput;
  421. packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 255)) |
  422. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 255)<< 8) |
  423. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 255)<<16) |
  424. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.w), 255)<<24) );
  425. return packedOutput;
  426. }
  427. //-----------------------------------------------------------------------------
  428. // R8G8B8A8_UNORM_SRGB <-> FLOAT4
  429. //-----------------------------------------------------------------------------
  430. D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput)
  431. {
  432. hlsl_precise XMFLOAT4 unpackedOutput;
  433. unpackedOutput.x = D3DX_SRGB_to_FLOAT_inexact(((FLOAT) (packedInput & 0x000000ff) )/255);
  434. unpackedOutput.y = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>> 8) & 0x000000ff)))/255);
  435. unpackedOutput.z = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>>16) & 0x000000ff)))/255);
  436. unpackedOutput.w = (FLOAT)(packedInput>>24) / 255;
  437. return unpackedOutput;
  438. }
  439. D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput)
  440. {
  441. hlsl_precise XMFLOAT4 unpackedOutput;
  442. unpackedOutput.x = D3DX_SRGB_to_FLOAT( (packedInput & 0x000000ff) );
  443. unpackedOutput.y = D3DX_SRGB_to_FLOAT((((packedInput>> 8) & 0x000000ff)));
  444. unpackedOutput.z = D3DX_SRGB_to_FLOAT((((packedInput>>16) & 0x000000ff)));
  445. unpackedOutput.w = (FLOAT)(packedInput>>24) / 255;
  446. return unpackedOutput;
  447. }
  448. D3DX11INLINE UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput)
  449. {
  450. UINT packedOutput;
  451. unpackedInput.x = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.x));
  452. unpackedInput.y = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.y));
  453. unpackedInput.z = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.z));
  454. unpackedInput.w = D3DX_Saturate_FLOAT(unpackedInput.w);
  455. packedOutput = ( (D3DX_FLOAT_to_UINT(unpackedInput.x, 255)) |
  456. (D3DX_FLOAT_to_UINT(unpackedInput.y, 255)<< 8) |
  457. (D3DX_FLOAT_to_UINT(unpackedInput.z, 255)<<16) |
  458. (D3DX_FLOAT_to_UINT(unpackedInput.w, 255)<<24) );
  459. return packedOutput;
  460. }
  461. //-----------------------------------------------------------------------------
  462. // R8G8B8A8_UINT <-> UINT4
  463. //-----------------------------------------------------------------------------
  464. D3DX11INLINE XMUINT4 D3DX_R8G8B8A8_UINT_to_UINT4(UINT packedInput)
  465. {
  466. XMUINT4 unpackedOutput;
  467. unpackedOutput.x = packedInput & 0x000000ff;
  468. unpackedOutput.y = (packedInput>> 8) & 0x000000ff;
  469. unpackedOutput.z = (packedInput>>16) & 0x000000ff;
  470. unpackedOutput.w = packedInput>>24;
  471. return unpackedOutput;
  472. }
  473. D3DX11INLINE UINT D3DX_UINT4_to_R8G8B8A8_UINT(XMUINT4 unpackedInput)
  474. {
  475. UINT packedOutput;
  476. unpackedInput.x = min(unpackedInput.x, 0x000000ff);
  477. unpackedInput.y = min(unpackedInput.y, 0x000000ff);
  478. unpackedInput.z = min(unpackedInput.z, 0x000000ff);
  479. unpackedInput.w = min(unpackedInput.w, 0x000000ff);
  480. packedOutput = ( unpackedInput.x |
  481. (unpackedInput.y<< 8) |
  482. (unpackedInput.z<<16) |
  483. (unpackedInput.w<<24) );
  484. return packedOutput;
  485. }
  486. //-----------------------------------------------------------------------------
  487. // R8G8B8A8_SNORM <-> FLOAT4
  488. //-----------------------------------------------------------------------------
  489. D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_SNORM_to_FLOAT4(UINT packedInput)
  490. {
  491. hlsl_precise XMFLOAT4 unpackedOutput;
  492. XMINT4 signExtendedBits;
  493. signExtendedBits.x = (INT)(packedInput << 24) >> 24;
  494. signExtendedBits.y = (INT)((packedInput << 16) & 0xff000000) >> 24;
  495. signExtendedBits.z = (INT)((packedInput << 8) & 0xff000000) >> 24;
  496. signExtendedBits.w = (INT)(packedInput & 0xff000000) >> 24;
  497. unpackedOutput.x = D3DX_INT_to_FLOAT(signExtendedBits.x, 127);
  498. unpackedOutput.y = D3DX_INT_to_FLOAT(signExtendedBits.y, 127);
  499. unpackedOutput.z = D3DX_INT_to_FLOAT(signExtendedBits.z, 127);
  500. unpackedOutput.w = D3DX_INT_to_FLOAT(signExtendedBits.w, 127);
  501. return unpackedOutput;
  502. }
  503. D3DX11INLINE UINT D3DX_FLOAT4_to_R8G8B8A8_SNORM(hlsl_precise XMFLOAT4 unpackedInput)
  504. {
  505. UINT packedOutput;
  506. packedOutput = ( (D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.x), 127) & 0x000000ff) |
  507. ((D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.y), 127) & 0x000000ff)<< 8) |
  508. ((D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.z), 127) & 0x000000ff)<<16) |
  509. ((D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.w), 127)) <<24) );
  510. return packedOutput;
  511. }
  512. //-----------------------------------------------------------------------------
  513. // R8G8B8A8_SINT <-> INT4
  514. //-----------------------------------------------------------------------------
  515. D3DX11INLINE XMINT4 D3DX_R8G8B8A8_SINT_to_INT4(UINT packedInput)
  516. {
  517. XMINT4 unpackedOutput;
  518. unpackedOutput.x = (INT)(packedInput << 24) >> 24;
  519. unpackedOutput.y = (INT)((packedInput << 16) & 0xff000000) >> 24;
  520. unpackedOutput.z = (INT)((packedInput << 8) & 0xff000000) >> 24;
  521. unpackedOutput.w = (INT)(packedInput & 0xff000000) >> 24;
  522. return unpackedOutput;
  523. }
  524. D3DX11INLINE UINT D3DX_INT4_to_R8G8B8A8_SINT(XMINT4 unpackedInput)
  525. {
  526. UINT packedOutput;
  527. unpackedInput.x = max(min(unpackedInput.x,127),-128);
  528. unpackedInput.y = max(min(unpackedInput.y,127),-128);
  529. unpackedInput.z = max(min(unpackedInput.z,127),-128);
  530. unpackedInput.w = max(min(unpackedInput.w,127),-128);
  531. packedOutput = ( (unpackedInput.x & 0x000000ff) |
  532. ((unpackedInput.y & 0x000000ff)<< 8) |
  533. ((unpackedInput.z & 0x000000ff)<<16) |
  534. (unpackedInput.w <<24) );
  535. return packedOutput;
  536. }
  537. //-----------------------------------------------------------------------------
  538. // B8G8R8A8_UNORM <-> FLOAT4
  539. //-----------------------------------------------------------------------------
  540. D3DX11INLINE XMFLOAT4 D3DX_B8G8R8A8_UNORM_to_FLOAT4(UINT packedInput)
  541. {
  542. hlsl_precise XMFLOAT4 unpackedOutput;
  543. unpackedOutput.z = (FLOAT) (packedInput & 0x000000ff) / 255;
  544. unpackedOutput.y = (FLOAT)(((packedInput>> 8) & 0x000000ff)) / 255;
  545. unpackedOutput.x = (FLOAT)(((packedInput>>16) & 0x000000ff)) / 255;
  546. unpackedOutput.w = (FLOAT) (packedInput>>24) / 255;
  547. return unpackedOutput;
  548. }
  549. D3DX11INLINE UINT D3DX_FLOAT4_to_B8G8R8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput)
  550. {
  551. UINT packedOutput;
  552. packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 255)) |
  553. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 255)<< 8) |
  554. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 255)<<16) |
  555. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.w), 255)<<24) );
  556. return packedOutput;
  557. }
  558. //-----------------------------------------------------------------------------
  559. // B8G8R8A8_UNORM_SRGB <-> FLOAT4
  560. //-----------------------------------------------------------------------------
  561. D3DX11INLINE XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput)
  562. {
  563. hlsl_precise XMFLOAT4 unpackedOutput;
  564. unpackedOutput.z = D3DX_SRGB_to_FLOAT_inexact(((FLOAT) (packedInput & 0x000000ff) )/255);
  565. unpackedOutput.y = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>> 8) & 0x000000ff)))/255);
  566. unpackedOutput.x = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>>16) & 0x000000ff)))/255);
  567. unpackedOutput.w = (FLOAT)(packedInput>>24) / 255;
  568. return unpackedOutput;
  569. }
  570. D3DX11INLINE XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput)
  571. {
  572. hlsl_precise XMFLOAT4 unpackedOutput;
  573. unpackedOutput.z = D3DX_SRGB_to_FLOAT( (packedInput & 0x000000ff) );
  574. unpackedOutput.y = D3DX_SRGB_to_FLOAT((((packedInput>> 8) & 0x000000ff)));
  575. unpackedOutput.x = D3DX_SRGB_to_FLOAT((((packedInput>>16) & 0x000000ff)));
  576. unpackedOutput.w = (FLOAT)(packedInput>>24) / 255;
  577. return unpackedOutput;
  578. }
  579. D3DX11INLINE UINT D3DX_FLOAT4_to_B8G8R8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput)
  580. {
  581. UINT packedOutput;
  582. unpackedInput.z = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.z));
  583. unpackedInput.y = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.y));
  584. unpackedInput.x = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.x));
  585. unpackedInput.w = D3DX_Saturate_FLOAT(unpackedInput.w);
  586. packedOutput = ( (D3DX_FLOAT_to_UINT(unpackedInput.z, 255)) |
  587. (D3DX_FLOAT_to_UINT(unpackedInput.y, 255)<< 8) |
  588. (D3DX_FLOAT_to_UINT(unpackedInput.x, 255)<<16) |
  589. (D3DX_FLOAT_to_UINT(unpackedInput.w, 255)<<24) );
  590. return packedOutput;
  591. }
  592. //-----------------------------------------------------------------------------
  593. // B8G8R8X8_UNORM <-> FLOAT3
  594. //-----------------------------------------------------------------------------
  595. D3DX11INLINE XMFLOAT3 D3DX_B8G8R8X8_UNORM_to_FLOAT3(UINT packedInput)
  596. {
  597. hlsl_precise XMFLOAT3 unpackedOutput;
  598. unpackedOutput.z = (FLOAT) (packedInput & 0x000000ff) / 255;
  599. unpackedOutput.y = (FLOAT)(((packedInput>> 8) & 0x000000ff)) / 255;
  600. unpackedOutput.x = (FLOAT)(((packedInput>>16) & 0x000000ff)) / 255;
  601. return unpackedOutput;
  602. }
  603. D3DX11INLINE UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM(hlsl_precise XMFLOAT3 unpackedInput)
  604. {
  605. UINT packedOutput;
  606. packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 255)) |
  607. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 255)<< 8) |
  608. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 255)<<16) );
  609. return packedOutput;
  610. }
  611. //-----------------------------------------------------------------------------
  612. // B8G8R8X8_UNORM_SRGB <-> FLOAT3
  613. //-----------------------------------------------------------------------------
  614. D3DX11INLINE XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3_inexact(UINT packedInput)
  615. {
  616. hlsl_precise XMFLOAT3 unpackedOutput;
  617. unpackedOutput.z = D3DX_SRGB_to_FLOAT_inexact(((FLOAT) (packedInput & 0x000000ff) )/255);
  618. unpackedOutput.y = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>> 8) & 0x000000ff)))/255);
  619. unpackedOutput.x = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>>16) & 0x000000ff)))/255);
  620. return unpackedOutput;
  621. }
  622. D3DX11INLINE XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3(UINT packedInput)
  623. {
  624. hlsl_precise XMFLOAT3 unpackedOutput;
  625. unpackedOutput.z = D3DX_SRGB_to_FLOAT( (packedInput & 0x000000ff) );
  626. unpackedOutput.y = D3DX_SRGB_to_FLOAT((((packedInput>> 8) & 0x000000ff)));
  627. unpackedOutput.x = D3DX_SRGB_to_FLOAT((((packedInput>>16) & 0x000000ff)));
  628. return unpackedOutput;
  629. }
  630. D3DX11INLINE UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM_SRGB(hlsl_precise XMFLOAT3 unpackedInput)
  631. {
  632. UINT packedOutput;
  633. unpackedInput.z = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.z));
  634. unpackedInput.y = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.y));
  635. unpackedInput.x = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.x));
  636. packedOutput = ( (D3DX_FLOAT_to_UINT(unpackedInput.z, 255)) |
  637. (D3DX_FLOAT_to_UINT(unpackedInput.y, 255)<< 8) |
  638. (D3DX_FLOAT_to_UINT(unpackedInput.x, 255)<<16) );
  639. return packedOutput;
  640. }
  641. //-----------------------------------------------------------------------------
  642. // R16G16_FLOAT <-> FLOAT2
  643. //-----------------------------------------------------------------------------
  644. #if HLSL_VERSION > 0
  645. D3DX11INLINE XMFLOAT2 D3DX_R16G16_FLOAT_to_FLOAT2(UINT packedInput)
  646. {
  647. hlsl_precise XMFLOAT2 unpackedOutput;
  648. unpackedOutput.x = f16tof32(packedInput&0x0000ffff);
  649. unpackedOutput.y = f16tof32(packedInput>>16);
  650. return unpackedOutput;
  651. }
  652. D3DX11INLINE UINT D3DX_FLOAT2_to_R16G16_FLOAT(hlsl_precise XMFLOAT2 unpackedInput)
  653. {
  654. UINT packedOutput;
  655. packedOutput = asuint(f32tof16(unpackedInput.x)) |
  656. (asuint(f32tof16(unpackedInput.y)) << 16);
  657. return packedOutput;
  658. }
  659. #endif // HLSL_VERSION > 0
  660. //-----------------------------------------------------------------------------
  661. // R16G16_UNORM <-> FLOAT2
  662. //-----------------------------------------------------------------------------
  663. D3DX11INLINE XMFLOAT2 D3DX_R16G16_UNORM_to_FLOAT2(UINT packedInput)
  664. {
  665. hlsl_precise XMFLOAT2 unpackedOutput;
  666. unpackedOutput.x = (FLOAT) (packedInput & 0x0000ffff) / 65535;
  667. unpackedOutput.y = (FLOAT) (packedInput>>16) / 65535;
  668. return unpackedOutput;
  669. }
  670. D3DX11INLINE UINT D3DX_FLOAT2_to_R16G16_UNORM(hlsl_precise XMFLOAT2 unpackedInput)
  671. {
  672. UINT packedOutput;
  673. packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 65535)) |
  674. (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 65535)<< 16) );
  675. return packedOutput;
  676. }
  677. //-----------------------------------------------------------------------------
  678. // R16G16_UINT <-> UINT2
  679. //-----------------------------------------------------------------------------
  680. D3DX11INLINE XMUINT2 D3DX_R16G16_UINT_to_UINT2(UINT packedInput)
  681. {
  682. XMUINT2 unpackedOutput;
  683. unpackedOutput.x = packedInput & 0x0000ffff;
  684. unpackedOutput.y = packedInput>>16;
  685. return unpackedOutput;
  686. }
  687. D3DX11INLINE UINT D3DX_UINT2_to_R16G16_UINT(XMUINT2 unpackedInput)
  688. {
  689. UINT packedOutput;
  690. unpackedInput.x = min(unpackedInput.x,0x0000ffff);
  691. unpackedInput.y = min(unpackedInput.y,0x0000ffff);
  692. packedOutput = ( unpackedInput.x |
  693. (unpackedInput.y<<16) );
  694. return packedOutput;
  695. }
  696. //-----------------------------------------------------------------------------
  697. // R16G16_SNORM <-> FLOAT2
  698. //-----------------------------------------------------------------------------
  699. D3DX11INLINE XMFLOAT2 D3DX_R16G16_SNORM_to_FLOAT2(UINT packedInput)
  700. {
  701. hlsl_precise XMFLOAT2 unpackedOutput;
  702. XMINT2 signExtendedBits;
  703. signExtendedBits.x = (INT)(packedInput << 16) >> 16;
  704. signExtendedBits.y = (INT)(packedInput & 0xffff0000) >> 16;
  705. unpackedOutput.x = D3DX_INT_to_FLOAT(signExtendedBits.x, 32767);
  706. unpackedOutput.y = D3DX_INT_to_FLOAT(signExtendedBits.y, 32767);
  707. return unpackedOutput;
  708. }
  709. D3DX11INLINE UINT D3DX_FLOAT2_to_R16G16_SNORM(hlsl_precise XMFLOAT2 unpackedInput)
  710. {
  711. UINT packedOutput;
  712. packedOutput = ( (D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.x), 32767) & 0x0000ffff) |
  713. (D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.y), 32767) <<16) );
  714. return packedOutput;
  715. }
  716. //-----------------------------------------------------------------------------
  717. // R16G16_SINT <-> INT2
  718. //-----------------------------------------------------------------------------
  719. D3DX11INLINE XMINT2 D3DX_R16G16_SINT_to_INT2(UINT packedInput)
  720. {
  721. XMINT2 unpackedOutput;
  722. unpackedOutput.x = (INT)(packedInput << 16) >> 16;
  723. unpackedOutput.y = (INT)(packedInput & 0xffff0000) >> 16;
  724. return unpackedOutput;
  725. }
  726. D3DX11INLINE UINT D3DX_INT2_to_R16G16_SINT(XMINT2 unpackedInput)
  727. {
  728. UINT packedOutput;
  729. unpackedInput.x = max(min(unpackedInput.x,32767),-32768);
  730. unpackedInput.y = max(min(unpackedInput.y,32767),-32768);
  731. packedOutput = ( (unpackedInput.x & 0x0000ffff) |
  732. (unpackedInput.y <<16) );
  733. return packedOutput;
  734. }
  735. #endif // __D3DX_DXGI_FORMAT_CONVERT_INL___