SDL_shaders_d3d.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED
  20. #include "../../core/windows/SDL_windows.h"
  21. #include <d3d9.h>
  22. #include "SDL_shaders_d3d.h"
  23. /* The shaders here were compiled with:
  24. fxc /T ps_2_0 /Fo"<OUTPUT FILE>" "<INPUT FILE>"
  25. Shader object code was converted to a list of DWORDs via the following
  26. *nix style command (available separately from Windows + MSVC):
  27. hexdump -v -e '6/4 "0x%08.8x, " "\n"' <FILE>
  28. */
  29. /* --- D3D9_PixelShader_YUV_JPEG.hlsl ---
  30. Texture2D theTextureY : register(t0);
  31. Texture2D theTextureU : register(t1);
  32. Texture2D theTextureV : register(t2);
  33. SamplerState theSampler = sampler_state
  34. {
  35. addressU = Clamp;
  36. addressV = Clamp;
  37. mipfilter = NONE;
  38. minfilter = LINEAR;
  39. magfilter = LINEAR;
  40. };
  41. struct PixelShaderInput
  42. {
  43. float4 pos : SV_POSITION;
  44. float2 tex : TEXCOORD0;
  45. float4 color : COLOR0;
  46. };
  47. float4 main(PixelShaderInput input) : SV_TARGET
  48. {
  49. const float3 offset = {0.0, -0.501960814, -0.501960814};
  50. const float3 Rcoeff = {1.0000, 0.0000, 1.4020};
  51. const float3 Gcoeff = {1.0000, -0.3441, -0.7141};
  52. const float3 Bcoeff = {1.0000, 1.7720, 0.0000};
  53. float4 Output;
  54. float3 yuv;
  55. yuv.x = theTextureY.Sample(theSampler, input.tex).r;
  56. yuv.y = theTextureU.Sample(theSampler, input.tex).r;
  57. yuv.z = theTextureV.Sample(theSampler, input.tex).r;
  58. yuv += offset;
  59. Output.r = dot(yuv, Rcoeff);
  60. Output.g = dot(yuv, Gcoeff);
  61. Output.b = dot(yuv, Bcoeff);
  62. Output.a = 1.0f;
  63. return Output * input.color;
  64. }
  65. */
  66. static const DWORD D3D9_PixelShader_YUV_JPEG[] = {
  67. 0xffff0200, 0x0044fffe, 0x42415443, 0x0000001c, 0x000000d7, 0xffff0200,
  68. 0x00000003, 0x0000001c, 0x00000100, 0x000000d0, 0x00000058, 0x00010003,
  69. 0x00000001, 0x00000070, 0x00000000, 0x00000080, 0x00020003, 0x00000001,
  70. 0x00000098, 0x00000000, 0x000000a8, 0x00000003, 0x00000001, 0x000000c0,
  71. 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478,
  72. 0xab005565, 0x00070004, 0x00040001, 0x00000001, 0x00000000, 0x53656874,
  73. 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478, 0xab005665, 0x00070004,
  74. 0x00040001, 0x00000001, 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265,
  75. 0x65546568, 0x72757478, 0xab005965, 0x00070004, 0x00040001, 0x00000001,
  76. 0x00000000, 0x325f7370, 0x4d00305f, 0x6f726369, 0x74666f73, 0x29522820,
  77. 0x534c4820, 0x6853204c, 0x72656461, 0x6d6f4320, 0x656c6970, 0x2e362072,
  78. 0x36392e33, 0x312e3030, 0x34383336, 0xababab00, 0x05000051, 0xa00f0000,
  79. 0x00000000, 0xbf008081, 0xbf008081, 0x3f800000, 0x05000051, 0xa00f0001,
  80. 0x3f800000, 0x00000000, 0x3fb374bc, 0x00000000, 0x05000051, 0xa00f0002,
  81. 0x3f800000, 0xbeb02de0, 0xbf36cf42, 0x00000000, 0x05000051, 0xa00f0003,
  82. 0x3f800000, 0x3fe2d0e5, 0x00000000, 0x00000000, 0x0200001f, 0x80000000,
  83. 0xb0030000, 0x0200001f, 0x80000000, 0x900f0000, 0x0200001f, 0x90000000,
  84. 0xa00f0800, 0x0200001f, 0x90000000, 0xa00f0801, 0x0200001f, 0x90000000,
  85. 0xa00f0802, 0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000042,
  86. 0x800f0001, 0xb0e40000, 0xa0e40801, 0x03000042, 0x800f0002, 0xb0e40000,
  87. 0xa0e40802, 0x02000001, 0x80020000, 0x80000001, 0x02000001, 0x80040000,
  88. 0x80000002, 0x03000002, 0x80070000, 0x80e40000, 0xa0e40000, 0x03000008,
  89. 0x80010001, 0x80e40000, 0xa0e40001, 0x03000008, 0x80020001, 0x80e40000,
  90. 0xa0e40002, 0x0400005a, 0x80040001, 0x80e40000, 0xa0e40003, 0xa0aa0003,
  91. 0x02000001, 0x80080001, 0xa0ff0000, 0x03000005, 0x800f0000, 0x80e40001,
  92. 0x90e40000, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff
  93. };
  94. /* --- D3D9_PixelShader_YUV_BT601.hlsl ---
  95. Texture2D theTextureY : register(t0);
  96. Texture2D theTextureU : register(t1);
  97. Texture2D theTextureV : register(t2);
  98. SamplerState theSampler = sampler_state
  99. {
  100. addressU = Clamp;
  101. addressV = Clamp;
  102. mipfilter = NONE;
  103. minfilter = LINEAR;
  104. magfilter = LINEAR;
  105. };
  106. struct PixelShaderInput
  107. {
  108. float4 pos : SV_POSITION;
  109. float2 tex : TEXCOORD0;
  110. float4 color : COLOR0;
  111. };
  112. float4 main(PixelShaderInput input) : SV_TARGET
  113. {
  114. const float3 offset = {-0.0627451017, -0.501960814, -0.501960814};
  115. const float3 Rcoeff = {1.1644, 0.0000, 1.5960};
  116. const float3 Gcoeff = {1.1644, -0.3918, -0.8130};
  117. const float3 Bcoeff = {1.1644, 2.0172, 0.0000};
  118. float4 Output;
  119. float3 yuv;
  120. yuv.x = theTextureY.Sample(theSampler, input.tex).r;
  121. yuv.y = theTextureU.Sample(theSampler, input.tex).r;
  122. yuv.z = theTextureV.Sample(theSampler, input.tex).r;
  123. yuv += offset;
  124. Output.r = dot(yuv, Rcoeff);
  125. Output.g = dot(yuv, Gcoeff);
  126. Output.b = dot(yuv, Bcoeff);
  127. Output.a = 1.0f;
  128. return Output * input.color;
  129. }
  130. */
  131. static const DWORD D3D9_PixelShader_YUV_BT601[] = {
  132. 0xffff0200, 0x0044fffe, 0x42415443, 0x0000001c, 0x000000d7, 0xffff0200,
  133. 0x00000003, 0x0000001c, 0x00000100, 0x000000d0, 0x00000058, 0x00010003,
  134. 0x00000001, 0x00000070, 0x00000000, 0x00000080, 0x00020003, 0x00000001,
  135. 0x00000098, 0x00000000, 0x000000a8, 0x00000003, 0x00000001, 0x000000c0,
  136. 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478,
  137. 0xab005565, 0x00070004, 0x00040001, 0x00000001, 0x00000000, 0x53656874,
  138. 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478, 0xab005665, 0x00070004,
  139. 0x00040001, 0x00000001, 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265,
  140. 0x65546568, 0x72757478, 0xab005965, 0x00070004, 0x00040001, 0x00000001,
  141. 0x00000000, 0x325f7370, 0x4d00305f, 0x6f726369, 0x74666f73, 0x29522820,
  142. 0x534c4820, 0x6853204c, 0x72656461, 0x6d6f4320, 0x656c6970, 0x2e362072,
  143. 0x36392e33, 0x312e3030, 0x34383336, 0xababab00, 0x05000051, 0xa00f0000,
  144. 0xbd808081, 0xbf008081, 0xbf008081, 0x3f800000, 0x05000051, 0xa00f0001,
  145. 0x3f950b0f, 0x00000000, 0x3fcc49ba, 0x00000000, 0x05000051, 0xa00f0002,
  146. 0x3f950b0f, 0xbec89a02, 0xbf5020c5, 0x00000000, 0x05000051, 0xa00f0003,
  147. 0x3f950b0f, 0x400119ce, 0x00000000, 0x00000000, 0x0200001f, 0x80000000,
  148. 0xb0030000, 0x0200001f, 0x80000000, 0x900f0000, 0x0200001f, 0x90000000,
  149. 0xa00f0800, 0x0200001f, 0x90000000, 0xa00f0801, 0x0200001f, 0x90000000,
  150. 0xa00f0802, 0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000042,
  151. 0x800f0001, 0xb0e40000, 0xa0e40801, 0x03000042, 0x800f0002, 0xb0e40000,
  152. 0xa0e40802, 0x02000001, 0x80020000, 0x80000001, 0x02000001, 0x80040000,
  153. 0x80000002, 0x03000002, 0x80070000, 0x80e40000, 0xa0e40000, 0x03000008,
  154. 0x80010001, 0x80e40000, 0xa0e40001, 0x03000008, 0x80020001, 0x80e40000,
  155. 0xa0e40002, 0x0400005a, 0x80040001, 0x80e40000, 0xa0e40003, 0xa0aa0003,
  156. 0x02000001, 0x80080001, 0xa0ff0000, 0x03000005, 0x800f0000, 0x80e40001,
  157. 0x90e40000, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff
  158. };
  159. /* --- D3D9_PixelShader_YUV_BT709.hlsl ---
  160. Texture2D theTextureY : register(t0);
  161. Texture2D theTextureU : register(t1);
  162. Texture2D theTextureV : register(t2);
  163. SamplerState theSampler = sampler_state
  164. {
  165. addressU = Clamp;
  166. addressV = Clamp;
  167. mipfilter = NONE;
  168. minfilter = LINEAR;
  169. magfilter = LINEAR;
  170. };
  171. struct PixelShaderInput
  172. {
  173. float4 pos : SV_POSITION;
  174. float2 tex : TEXCOORD0;
  175. float4 color : COLOR0;
  176. };
  177. float4 main(PixelShaderInput input) : SV_TARGET
  178. {
  179. const float3 offset = {-0.0627451017, -0.501960814, -0.501960814};
  180. const float3 Rcoeff = {1.1644, 0.0000, 1.7927};
  181. const float3 Gcoeff = {1.1644, -0.2132, -0.5329};
  182. const float3 Bcoeff = {1.1644, 2.1124, 0.0000};
  183. float4 Output;
  184. float3 yuv;
  185. yuv.x = theTextureY.Sample(theSampler, input.tex).r;
  186. yuv.y = theTextureU.Sample(theSampler, input.tex).r;
  187. yuv.z = theTextureV.Sample(theSampler, input.tex).r;
  188. yuv += offset;
  189. Output.r = dot(yuv, Rcoeff);
  190. Output.g = dot(yuv, Gcoeff);
  191. Output.b = dot(yuv, Bcoeff);
  192. Output.a = 1.0f;
  193. return Output * input.color;
  194. }
  195. */
  196. static const DWORD D3D9_PixelShader_YUV_BT709[] = {
  197. 0xffff0200, 0x0044fffe, 0x42415443, 0x0000001c, 0x000000d7, 0xffff0200,
  198. 0x00000003, 0x0000001c, 0x00000100, 0x000000d0, 0x00000058, 0x00010003,
  199. 0x00000001, 0x00000070, 0x00000000, 0x00000080, 0x00020003, 0x00000001,
  200. 0x00000098, 0x00000000, 0x000000a8, 0x00000003, 0x00000001, 0x000000c0,
  201. 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478,
  202. 0xab005565, 0x00070004, 0x00040001, 0x00000001, 0x00000000, 0x53656874,
  203. 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478, 0xab005665, 0x00070004,
  204. 0x00040001, 0x00000001, 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265,
  205. 0x65546568, 0x72757478, 0xab005965, 0x00070004, 0x00040001, 0x00000001,
  206. 0x00000000, 0x325f7370, 0x4d00305f, 0x6f726369, 0x74666f73, 0x29522820,
  207. 0x534c4820, 0x6853204c, 0x72656461, 0x6d6f4320, 0x656c6970, 0x2e362072,
  208. 0x36392e33, 0x312e3030, 0x34383336, 0xababab00, 0x05000051, 0xa00f0000,
  209. 0xbd808081, 0xbf008081, 0xbf008081, 0x3f800000, 0x05000051, 0xa00f0001,
  210. 0x3f950b0f, 0x00000000, 0x3fe57732, 0x00000000, 0x05000051, 0xa00f0002,
  211. 0x3f950b0f, 0xbe5a511a, 0xbf086c22, 0x00000000, 0x05000051, 0xa00f0003,
  212. 0x3f950b0f, 0x40073190, 0x00000000, 0x00000000, 0x0200001f, 0x80000000,
  213. 0xb0030000, 0x0200001f, 0x80000000, 0x900f0000, 0x0200001f, 0x90000000,
  214. 0xa00f0800, 0x0200001f, 0x90000000, 0xa00f0801, 0x0200001f, 0x90000000,
  215. 0xa00f0802, 0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000042,
  216. 0x800f0001, 0xb0e40000, 0xa0e40801, 0x03000042, 0x800f0002, 0xb0e40000,
  217. 0xa0e40802, 0x02000001, 0x80020000, 0x80000001, 0x02000001, 0x80040000,
  218. 0x80000002, 0x03000002, 0x80070000, 0x80e40000, 0xa0e40000, 0x03000008,
  219. 0x80010001, 0x80e40000, 0xa0e40001, 0x03000008, 0x80020001, 0x80e40000,
  220. 0xa0e40002, 0x0400005a, 0x80040001, 0x80e40000, 0xa0e40003, 0xa0aa0003,
  221. 0x02000001, 0x80080001, 0xa0ff0000, 0x03000005, 0x800f0000, 0x80e40001,
  222. 0x90e40000, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff
  223. };
  224. static const DWORD *D3D9_shaders[] = {
  225. D3D9_PixelShader_YUV_JPEG,
  226. D3D9_PixelShader_YUV_BT601,
  227. D3D9_PixelShader_YUV_BT709,
  228. };
  229. HRESULT D3D9_CreatePixelShader(IDirect3DDevice9 *d3dDevice, D3D9_Shader shader, IDirect3DPixelShader9 **pixelShader)
  230. {
  231. return IDirect3DDevice9_CreatePixelShader(d3dDevice, D3D9_shaders[shader], pixelShader);
  232. }
  233. #endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */