SDL_shaders_d3d.c 11 KB

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