TextureConversionShader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include <cstdio>
  6. #include "Common/MathUtil.h"
  7. #include "VideoCommon/BPMemory.h"
  8. #include "VideoCommon/RenderBase.h"
  9. #include "VideoCommon/TextureConversionShader.h"
  10. #include "VideoCommon/TextureDecoder.h"
  11. #include "VideoCommon/VideoConfig.h"
  12. #define WRITE p+=sprintf
  13. static char text[16384];
  14. static bool IntensityConstantAdded = false;
  15. namespace TextureConversionShader
  16. {
  17. u16 GetEncodedSampleCount(u32 format)
  18. {
  19. switch (format)
  20. {
  21. case GX_TF_I4: return 8;
  22. case GX_TF_I8: return 4;
  23. case GX_TF_IA4: return 4;
  24. case GX_TF_IA8: return 2;
  25. case GX_TF_RGB565: return 2;
  26. case GX_TF_RGB5A3: return 2;
  27. case GX_TF_RGBA8: return 1;
  28. case GX_CTF_R4: return 8;
  29. case GX_CTF_RA4: return 4;
  30. case GX_CTF_RA8: return 2;
  31. case GX_CTF_A8: return 4;
  32. case GX_CTF_R8: return 4;
  33. case GX_CTF_G8: return 4;
  34. case GX_CTF_B8: return 4;
  35. case GX_CTF_RG8: return 2;
  36. case GX_CTF_GB8: return 2;
  37. case GX_TF_Z8: return 4;
  38. case GX_TF_Z16: return 2;
  39. case GX_TF_Z24X8: return 1;
  40. case GX_CTF_Z4: return 8;
  41. case GX_CTF_Z8M: return 4;
  42. case GX_CTF_Z8L: return 4;
  43. case GX_CTF_Z16L: return 2;
  44. default: return 1;
  45. }
  46. }
  47. // block dimensions : widthStride, heightStride
  48. // texture dims : width, height, x offset, y offset
  49. static void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
  50. {
  51. // left, top, of source rectangle within source texture
  52. // width of the destination rectangle, scale_factor (1 or 2)
  53. WRITE(p, "uniform int4 position;\n");
  54. int blkW = TexDecoder_GetBlockWidthInTexels(format);
  55. int blkH = TexDecoder_GetBlockHeightInTexels(format);
  56. int samples = GetEncodedSampleCount(format);
  57. if (ApiType == API_OPENGL)
  58. {
  59. WRITE(p, "#define samp0 samp9\n");
  60. WRITE(p, "SAMPLER_BINDING(9) uniform sampler2DArray samp0;\n");
  61. WRITE(p, " out vec4 ocol0;\n");
  62. WRITE(p, "void main()\n");
  63. WRITE(p, "{\n"
  64. " int2 sampleUv;\n"
  65. " int2 uv1 = int2(gl_FragCoord.xy);\n"
  66. );
  67. }
  68. else // D3D
  69. {
  70. WRITE(p, "sampler samp0 : register(s0);\n");
  71. WRITE(p, "Texture2DArray Tex0 : register(t0);\n");
  72. WRITE(p, "void main(\n");
  73. WRITE(p, " out float4 ocol0 : SV_Target, in float4 rawpos : SV_Position)\n");
  74. WRITE(p, "{\n"
  75. " int2 sampleUv;\n"
  76. " int2 uv1 = int2(rawpos.xy);\n"
  77. );
  78. }
  79. WRITE(p, " int x_block_position = (uv1.x >> %d) << %d;\n", IntLog2(blkH * blkW / samples), IntLog2(blkW));
  80. WRITE(p, " int y_block_position = uv1.y << %d;\n", IntLog2(blkH));
  81. if (samples == 1)
  82. {
  83. // With samples == 1, we write out pairs of blocks; one A8R8, one G8B8.
  84. WRITE(p, " bool first = (uv1.x & %d) == 0;\n", blkH * blkW / 2);
  85. samples = 2;
  86. }
  87. WRITE(p, " int offset_in_block = uv1.x & %d;\n", (blkH * blkW / samples) - 1);
  88. WRITE(p, " int y_offset_in_block = offset_in_block >> %d;\n", IntLog2(blkW / samples));
  89. WRITE(p, " int x_offset_in_block = (offset_in_block & %d) << %d;\n", (blkW / samples) - 1, IntLog2(samples));
  90. WRITE(p, " sampleUv.x = x_block_position + x_offset_in_block;\n");
  91. WRITE(p, " sampleUv.y = y_block_position + y_offset_in_block;\n");
  92. WRITE(p, " float2 uv0 = float2(sampleUv);\n"); // sampleUv is the sample position in (int)gx_coords
  93. WRITE(p, " uv0 += float2(0.5, 0.5);\n"); // move to center of pixel
  94. WRITE(p, " uv0 *= float(position.w);\n"); // scale by two if needed (also move to pixel borders so that linear filtering will average adjacent pixel)
  95. WRITE(p, " uv0 += float2(position.xy);\n"); // move to copied rect
  96. WRITE(p, " uv0 /= float2(%d, %d);\n", EFB_WIDTH, EFB_HEIGHT); // normalize to [0:1]
  97. if (ApiType == API_OPENGL) // ogl has to flip up and down
  98. {
  99. WRITE(p, " uv0.y = 1.0-uv0.y;\n");
  100. }
  101. WRITE(p, " float sample_offset = float(position.w) / float(%d);\n", EFB_WIDTH);
  102. }
  103. static void WriteSampleColor(char*& p, const char* colorComp, const char* dest, int xoffset, API_TYPE ApiType)
  104. {
  105. if (ApiType == API_OPENGL)
  106. {
  107. WRITE(p, " %s = texture(samp0, float3(uv0 + float2(%d, 0) * sample_offset, 0.0)).%s;\n",
  108. dest, xoffset, colorComp
  109. );
  110. }
  111. else
  112. {
  113. WRITE(p, " %s = Tex0.Sample(samp0, float3(uv0 + float2(%d, 0) * sample_offset, 0.0)).%s;\n",
  114. dest, xoffset, colorComp
  115. );
  116. }
  117. }
  118. static void WriteColorToIntensity(char*& p, const char* src, const char* dest)
  119. {
  120. if (!IntensityConstantAdded)
  121. {
  122. WRITE(p, " float4 IntensityConst = float4(0.257f,0.504f,0.098f,0.0625f);\n");
  123. IntensityConstantAdded = true;
  124. }
  125. WRITE(p, " %s = dot(IntensityConst.rgb, %s.rgb);\n", dest, src);
  126. // don't add IntensityConst.a yet, because doing it later is faster and uses less instructions, due to vectorization
  127. }
  128. static void WriteToBitDepth(char*& p, u8 depth, const char* src, const char* dest)
  129. {
  130. WRITE(p, " %s = floor(%s * 255.0 / exp2(8.0 - %d.0));\n", dest, src, depth);
  131. }
  132. static void WriteEncoderEnd(char*& p, API_TYPE ApiType)
  133. {
  134. WRITE(p, "}\n");
  135. IntensityConstantAdded = false;
  136. }
  137. static void WriteI8Encoder(char*& p, API_TYPE ApiType)
  138. {
  139. WriteSwizzler(p, GX_TF_I8, ApiType);
  140. WRITE(p, " float3 texSample;\n");
  141. WriteSampleColor(p, "rgb", "texSample", 0, ApiType);
  142. WriteColorToIntensity(p, "texSample", "ocol0.b");
  143. WriteSampleColor(p, "rgb", "texSample", 1, ApiType);
  144. WriteColorToIntensity(p, "texSample", "ocol0.g");
  145. WriteSampleColor(p, "rgb", "texSample", 2, ApiType);
  146. WriteColorToIntensity(p, "texSample", "ocol0.r");
  147. WriteSampleColor(p, "rgb", "texSample", 3, ApiType);
  148. WriteColorToIntensity(p, "texSample", "ocol0.a");
  149. WRITE(p, " ocol0.rgba += IntensityConst.aaaa;\n"); // see WriteColorToIntensity
  150. WriteEncoderEnd(p, ApiType);
  151. }
  152. static void WriteI4Encoder(char*& p, API_TYPE ApiType)
  153. {
  154. WriteSwizzler(p, GX_TF_I4, ApiType);
  155. WRITE(p, " float3 texSample;\n");
  156. WRITE(p, " float4 color0;\n");
  157. WRITE(p, " float4 color1;\n");
  158. WriteSampleColor(p, "rgb", "texSample", 0, ApiType);
  159. WriteColorToIntensity(p, "texSample", "color0.b");
  160. WriteSampleColor(p, "rgb", "texSample", 1, ApiType);
  161. WriteColorToIntensity(p, "texSample", "color1.b");
  162. WriteSampleColor(p, "rgb", "texSample", 2, ApiType);
  163. WriteColorToIntensity(p, "texSample", "color0.g");
  164. WriteSampleColor(p, "rgb", "texSample", 3, ApiType);
  165. WriteColorToIntensity(p, "texSample", "color1.g");
  166. WriteSampleColor(p, "rgb", "texSample", 4, ApiType);
  167. WriteColorToIntensity(p, "texSample", "color0.r");
  168. WriteSampleColor(p, "rgb", "texSample", 5, ApiType);
  169. WriteColorToIntensity(p, "texSample", "color1.r");
  170. WriteSampleColor(p, "rgb", "texSample", 6, ApiType);
  171. WriteColorToIntensity(p, "texSample", "color0.a");
  172. WriteSampleColor(p, "rgb", "texSample", 7, ApiType);
  173. WriteColorToIntensity(p, "texSample", "color1.a");
  174. WRITE(p, " color0.rgba += IntensityConst.aaaa;\n");
  175. WRITE(p, " color1.rgba += IntensityConst.aaaa;\n");
  176. WriteToBitDepth(p, 4, "color0", "color0");
  177. WriteToBitDepth(p, 4, "color1", "color1");
  178. WRITE(p, " ocol0 = (color0 * 16.0 + color1) / 255.0;\n");
  179. WriteEncoderEnd(p, ApiType);
  180. }
  181. static void WriteIA8Encoder(char*& p,API_TYPE ApiType)
  182. {
  183. WriteSwizzler(p, GX_TF_IA8, ApiType);
  184. WRITE(p, " float4 texSample;\n");
  185. WriteSampleColor(p, "rgba", "texSample", 0, ApiType);
  186. WRITE(p, " ocol0.b = texSample.a;\n");
  187. WriteColorToIntensity(p, "texSample", "ocol0.g");
  188. WriteSampleColor(p, "rgba", "texSample", 1, ApiType);
  189. WRITE(p, " ocol0.r = texSample.a;\n");
  190. WriteColorToIntensity(p, "texSample", "ocol0.a");
  191. WRITE(p, " ocol0.ga += IntensityConst.aa;\n");
  192. WriteEncoderEnd(p, ApiType);
  193. }
  194. static void WriteIA4Encoder(char*& p,API_TYPE ApiType)
  195. {
  196. WriteSwizzler(p, GX_TF_IA4, ApiType);
  197. WRITE(p, " float4 texSample;\n");
  198. WRITE(p, " float4 color0;\n");
  199. WRITE(p, " float4 color1;\n");
  200. WriteSampleColor(p, "rgba", "texSample", 0, ApiType);
  201. WRITE(p, " color0.b = texSample.a;\n");
  202. WriteColorToIntensity(p, "texSample", "color1.b");
  203. WriteSampleColor(p, "rgba", "texSample", 1, ApiType);
  204. WRITE(p, " color0.g = texSample.a;\n");
  205. WriteColorToIntensity(p, "texSample", "color1.g");
  206. WriteSampleColor(p, "rgba", "texSample", 2, ApiType);
  207. WRITE(p, " color0.r = texSample.a;\n");
  208. WriteColorToIntensity(p, "texSample", "color1.r");
  209. WriteSampleColor(p, "rgba", "texSample", 3, ApiType);
  210. WRITE(p, " color0.a = texSample.a;\n");
  211. WriteColorToIntensity(p, "texSample", "color1.a");
  212. WRITE(p, " color1.rgba += IntensityConst.aaaa;\n");
  213. WriteToBitDepth(p, 4, "color0", "color0");
  214. WriteToBitDepth(p, 4, "color1", "color1");
  215. WRITE(p, " ocol0 = (color0 * 16.0 + color1) / 255.0;\n");
  216. WriteEncoderEnd(p, ApiType);
  217. }
  218. static void WriteRGB565Encoder(char*& p,API_TYPE ApiType)
  219. {
  220. WriteSwizzler(p, GX_TF_RGB565, ApiType);
  221. WriteSampleColor(p, "rgb", "float3 texSample0", 0, ApiType);
  222. WriteSampleColor(p, "rgb", "float3 texSample1", 1, ApiType);
  223. WRITE(p, " float2 texRs = float2(texSample0.r, texSample1.r);\n");
  224. WRITE(p, " float2 texGs = float2(texSample0.g, texSample1.g);\n");
  225. WRITE(p, " float2 texBs = float2(texSample0.b, texSample1.b);\n");
  226. WriteToBitDepth(p, 6, "texGs", "float2 gInt");
  227. WRITE(p, " float2 gUpper = floor(gInt / 8.0);\n");
  228. WRITE(p, " float2 gLower = gInt - gUpper * 8.0;\n");
  229. WriteToBitDepth(p, 5, "texRs", "ocol0.br");
  230. WRITE(p, " ocol0.br = ocol0.br * 8.0 + gUpper;\n");
  231. WriteToBitDepth(p, 5, "texBs", "ocol0.ga");
  232. WRITE(p, " ocol0.ga = ocol0.ga + gLower * 32.0;\n");
  233. WRITE(p, " ocol0 = ocol0 / 255.0;\n");
  234. WriteEncoderEnd(p, ApiType);
  235. }
  236. static void WriteRGB5A3Encoder(char*& p,API_TYPE ApiType)
  237. {
  238. WriteSwizzler(p, GX_TF_RGB5A3, ApiType);
  239. WRITE(p, " float4 texSample;\n");
  240. WRITE(p, " float color0;\n");
  241. WRITE(p, " float gUpper;\n");
  242. WRITE(p, " float gLower;\n");
  243. WriteSampleColor(p, "rgba", "texSample", 0, ApiType);
  244. // 0.8784 = 224 / 255 which is the maximum alpha value that can be represented in 3 bits
  245. WRITE(p, "if(texSample.a > 0.878f) {\n");
  246. WriteToBitDepth(p, 5, "texSample.g", "color0");
  247. WRITE(p, " gUpper = floor(color0 / 8.0);\n");
  248. WRITE(p, " gLower = color0 - gUpper * 8.0;\n");
  249. WriteToBitDepth(p, 5, "texSample.r", "ocol0.b");
  250. WRITE(p, " ocol0.b = ocol0.b * 4.0 + gUpper + 128.0;\n");
  251. WriteToBitDepth(p, 5, "texSample.b", "ocol0.g");
  252. WRITE(p, " ocol0.g = ocol0.g + gLower * 32.0;\n");
  253. WRITE(p, "} else {\n");
  254. WriteToBitDepth(p, 4, "texSample.r", "ocol0.b");
  255. WriteToBitDepth(p, 4, "texSample.b", "ocol0.g");
  256. WriteToBitDepth(p, 3, "texSample.a", "color0");
  257. WRITE(p, "ocol0.b = ocol0.b + color0 * 16.0;\n");
  258. WriteToBitDepth(p, 4, "texSample.g", "color0");
  259. WRITE(p, "ocol0.g = ocol0.g + color0 * 16.0;\n");
  260. WRITE(p, "}\n");
  261. WriteSampleColor(p, "rgba", "texSample", 1, ApiType);
  262. WRITE(p, "if(texSample.a > 0.878f) {\n");
  263. WriteToBitDepth(p, 5, "texSample.g", "color0");
  264. WRITE(p, " gUpper = floor(color0 / 8.0);\n");
  265. WRITE(p, " gLower = color0 - gUpper * 8.0;\n");
  266. WriteToBitDepth(p, 5, "texSample.r", "ocol0.r");
  267. WRITE(p, " ocol0.r = ocol0.r * 4.0 + gUpper + 128.0;\n");
  268. WriteToBitDepth(p, 5, "texSample.b", "ocol0.a");
  269. WRITE(p, " ocol0.a = ocol0.a + gLower * 32.0;\n");
  270. WRITE(p, "} else {\n");
  271. WriteToBitDepth(p, 4, "texSample.r", "ocol0.r");
  272. WriteToBitDepth(p, 4, "texSample.b", "ocol0.a");
  273. WriteToBitDepth(p, 3, "texSample.a", "color0");
  274. WRITE(p, "ocol0.r = ocol0.r + color0 * 16.0;\n");
  275. WriteToBitDepth(p, 4, "texSample.g", "color0");
  276. WRITE(p, "ocol0.a = ocol0.a + color0 * 16.0;\n");
  277. WRITE(p, "}\n");
  278. WRITE(p, " ocol0 = ocol0 / 255.0;\n");
  279. WriteEncoderEnd(p, ApiType);
  280. }
  281. static void WriteRGBA8Encoder(char*& p,API_TYPE ApiType)
  282. {
  283. WriteSwizzler(p, GX_TF_RGBA8, ApiType);
  284. WRITE(p, " float4 texSample;\n");
  285. WRITE(p, " float4 color0;\n");
  286. WRITE(p, " float4 color1;\n");
  287. WriteSampleColor(p, "rgba", "texSample", 0, ApiType);
  288. WRITE(p, " color0.b = texSample.a;\n");
  289. WRITE(p, " color0.g = texSample.r;\n");
  290. WRITE(p, " color1.b = texSample.g;\n");
  291. WRITE(p, " color1.g = texSample.b;\n");
  292. WriteSampleColor(p, "rgba", "texSample", 1, ApiType);
  293. WRITE(p, " color0.r = texSample.a;\n");
  294. WRITE(p, " color0.a = texSample.r;\n");
  295. WRITE(p, " color1.r = texSample.g;\n");
  296. WRITE(p, " color1.a = texSample.b;\n");
  297. WRITE(p, " ocol0 = first ? color0 : color1;\n");
  298. WriteEncoderEnd(p, ApiType);
  299. }
  300. static void WriteC4Encoder(char*& p, const char* comp,API_TYPE ApiType)
  301. {
  302. WriteSwizzler(p, GX_CTF_R4, ApiType);
  303. WRITE(p, " float4 color0;\n");
  304. WRITE(p, " float4 color1;\n");
  305. WriteSampleColor(p, comp, "color0.b", 0, ApiType);
  306. WriteSampleColor(p, comp, "color1.b", 1, ApiType);
  307. WriteSampleColor(p, comp, "color0.g", 2, ApiType);
  308. WriteSampleColor(p, comp, "color1.g", 3, ApiType);
  309. WriteSampleColor(p, comp, "color0.r", 4, ApiType);
  310. WriteSampleColor(p, comp, "color1.r", 5, ApiType);
  311. WriteSampleColor(p, comp, "color0.a", 6, ApiType);
  312. WriteSampleColor(p, comp, "color1.a", 7, ApiType);
  313. WriteToBitDepth(p, 4, "color0", "color0");
  314. WriteToBitDepth(p, 4, "color1", "color1");
  315. WRITE(p, " ocol0 = (color0 * 16.0 + color1) / 255.0;\n");
  316. WriteEncoderEnd(p, ApiType);
  317. }
  318. static void WriteC8Encoder(char*& p, const char* comp,API_TYPE ApiType)
  319. {
  320. WriteSwizzler(p, GX_CTF_R8, ApiType);
  321. WriteSampleColor(p, comp, "ocol0.b", 0, ApiType);
  322. WriteSampleColor(p, comp, "ocol0.g", 1, ApiType);
  323. WriteSampleColor(p, comp, "ocol0.r", 2, ApiType);
  324. WriteSampleColor(p, comp, "ocol0.a", 3, ApiType);
  325. WriteEncoderEnd(p, ApiType);
  326. }
  327. static void WriteCC4Encoder(char*& p, const char* comp,API_TYPE ApiType)
  328. {
  329. WriteSwizzler(p, GX_CTF_RA4, ApiType);
  330. WRITE(p, " float2 texSample;\n");
  331. WRITE(p, " float4 color0;\n");
  332. WRITE(p, " float4 color1;\n");
  333. WriteSampleColor(p, comp, "texSample", 0, ApiType);
  334. WRITE(p, " color0.b = texSample.x;\n");
  335. WRITE(p, " color1.b = texSample.y;\n");
  336. WriteSampleColor(p, comp, "texSample", 1, ApiType);
  337. WRITE(p, " color0.g = texSample.x;\n");
  338. WRITE(p, " color1.g = texSample.y;\n");
  339. WriteSampleColor(p, comp, "texSample", 2, ApiType);
  340. WRITE(p, " color0.r = texSample.x;\n");
  341. WRITE(p, " color1.r = texSample.y;\n");
  342. WriteSampleColor(p, comp, "texSample", 3, ApiType);
  343. WRITE(p, " color0.a = texSample.x;\n");
  344. WRITE(p, " color1.a = texSample.y;\n");
  345. WriteToBitDepth(p, 4, "color0", "color0");
  346. WriteToBitDepth(p, 4, "color1", "color1");
  347. WRITE(p, " ocol0 = (color0 * 16.0 + color1) / 255.0;\n");
  348. WriteEncoderEnd(p, ApiType);
  349. }
  350. static void WriteCC8Encoder(char*& p, const char* comp, API_TYPE ApiType)
  351. {
  352. WriteSwizzler(p, GX_CTF_RA8, ApiType);
  353. WriteSampleColor(p, comp, "ocol0.bg", 0, ApiType);
  354. WriteSampleColor(p, comp, "ocol0.ra", 1, ApiType);
  355. WriteEncoderEnd(p, ApiType);
  356. }
  357. static void WriteZ8Encoder(char*& p, const char* multiplier,API_TYPE ApiType)
  358. {
  359. WriteSwizzler(p, GX_CTF_Z8M, ApiType);
  360. WRITE(p, " float depth;\n");
  361. WriteSampleColor(p, "r", "depth", 0, ApiType);
  362. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  363. WRITE(p, "ocol0.b = frac(depth * %s);\n", multiplier);
  364. WriteSampleColor(p, "r", "depth", 1, ApiType);
  365. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  366. WRITE(p, "ocol0.g = frac(depth * %s);\n", multiplier);
  367. WriteSampleColor(p, "r", "depth", 2, ApiType);
  368. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  369. WRITE(p, "ocol0.r = frac(depth * %s);\n", multiplier);
  370. WriteSampleColor(p, "r", "depth", 3, ApiType);
  371. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  372. WRITE(p, "ocol0.a = frac(depth * %s);\n", multiplier);
  373. WriteEncoderEnd(p, ApiType);
  374. }
  375. static void WriteZ16Encoder(char*& p,API_TYPE ApiType)
  376. {
  377. WriteSwizzler(p, GX_TF_Z16, ApiType);
  378. WRITE(p, " float depth;\n");
  379. WRITE(p, " float3 expanded;\n");
  380. // byte order is reversed
  381. WriteSampleColor(p, "r", "depth", 0, ApiType);
  382. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  383. WRITE(p, " depth = clamp(depth * 16777216.0, 0.0, float(0xFFFFFF));\n");
  384. WRITE(p, " expanded.r = floor(depth / (256.0 * 256.0));\n");
  385. WRITE(p, " depth -= expanded.r * 256.0 * 256.0;\n");
  386. WRITE(p, " expanded.g = floor(depth / 256.0);\n");
  387. WRITE(p, " ocol0.b = expanded.g / 255.0;\n");
  388. WRITE(p, " ocol0.g = expanded.r / 255.0;\n");
  389. WriteSampleColor(p, "r", "depth", 1, ApiType);
  390. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  391. WRITE(p, " depth = clamp(depth * 16777216.0, 0.0, float(0xFFFFFF));\n");
  392. WRITE(p, " expanded.r = floor(depth / (256.0 * 256.0));\n");
  393. WRITE(p, " depth -= expanded.r * 256.0 * 256.0;\n");
  394. WRITE(p, " expanded.g = floor(depth / 256.0);\n");
  395. WRITE(p, " ocol0.r = expanded.g / 255.0;\n");
  396. WRITE(p, " ocol0.a = expanded.r / 255.0;\n");
  397. WriteEncoderEnd(p, ApiType);
  398. }
  399. static void WriteZ16LEncoder(char*& p,API_TYPE ApiType)
  400. {
  401. WriteSwizzler(p, GX_CTF_Z16L, ApiType);
  402. WRITE(p, " float depth;\n");
  403. WRITE(p, " float3 expanded;\n");
  404. // byte order is reversed
  405. WriteSampleColor(p, "r", "depth", 0, ApiType);
  406. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  407. WRITE(p, " depth = clamp(depth * 16777216.0, 0.0, float(0xFFFFFF));\n");
  408. WRITE(p, " expanded.r = floor(depth / (256.0 * 256.0));\n");
  409. WRITE(p, " depth -= expanded.r * 256.0 * 256.0;\n");
  410. WRITE(p, " expanded.g = floor(depth / 256.0);\n");
  411. WRITE(p, " depth -= expanded.g * 256.0;\n");
  412. WRITE(p, " expanded.b = depth;\n");
  413. WRITE(p, " ocol0.b = expanded.b / 255.0;\n");
  414. WRITE(p, " ocol0.g = expanded.g / 255.0;\n");
  415. WriteSampleColor(p, "r", "depth", 1, ApiType);
  416. if (ApiType == API_D3D) WRITE(p, "depth = 1.0f - depth;\n");
  417. WRITE(p, " depth = clamp(depth * 16777216.0, 0.0, float(0xFFFFFF));\n");
  418. WRITE(p, " expanded.r = floor(depth / (256.0 * 256.0));\n");
  419. WRITE(p, " depth -= expanded.r * 256.0 * 256.0;\n");
  420. WRITE(p, " expanded.g = floor(depth / 256.0);\n");
  421. WRITE(p, " depth -= expanded.g * 256.0;\n");
  422. WRITE(p, " expanded.b = depth;\n");
  423. WRITE(p, " ocol0.r = expanded.b / 255.0;\n");
  424. WRITE(p, " ocol0.a = expanded.g / 255.0;\n");
  425. WriteEncoderEnd(p, ApiType);
  426. }
  427. static void WriteZ24Encoder(char*& p, API_TYPE ApiType)
  428. {
  429. WriteSwizzler(p, GX_TF_Z24X8, ApiType);
  430. WRITE(p, " float depth0;\n");
  431. WRITE(p, " float depth1;\n");
  432. WRITE(p, " float3 expanded0;\n");
  433. WRITE(p, " float3 expanded1;\n");
  434. WriteSampleColor(p, "r", "depth0", 0, ApiType);
  435. if (ApiType == API_D3D) WRITE(p, "depth0 = 1.0f - depth0;\n");
  436. WriteSampleColor(p, "r", "depth1", 1, ApiType);
  437. if (ApiType == API_D3D) WRITE(p, "depth1 = 1.0f - depth1;\n");
  438. for (int i = 0; i < 2; i++)
  439. {
  440. WRITE(p, " depth%i = clamp(depth%i * 16777216.0, 0.0, float(0xFFFFFF));\n", i, i);
  441. WRITE(p, " expanded%i.r = floor(depth%i / (256.0 * 256.0));\n", i, i);
  442. WRITE(p, " depth%i -= expanded%i.r * 256.0 * 256.0;\n", i, i);
  443. WRITE(p, " expanded%i.g = floor(depth%i / 256.0);\n", i, i);
  444. WRITE(p, " depth%i -= expanded%i.g * 256.0;\n", i, i);
  445. WRITE(p, " expanded%i.b = depth%i;\n", i, i);
  446. }
  447. WRITE(p, " if (!first) {\n");
  448. // upper 16
  449. WRITE(p, " ocol0.b = expanded0.g / 255.0;\n");
  450. WRITE(p, " ocol0.g = expanded0.b / 255.0;\n");
  451. WRITE(p, " ocol0.r = expanded1.g / 255.0;\n");
  452. WRITE(p, " ocol0.a = expanded1.b / 255.0;\n");
  453. WRITE(p, " } else {\n");
  454. // lower 8
  455. WRITE(p, " ocol0.b = 1.0;\n");
  456. WRITE(p, " ocol0.g = expanded0.r / 255.0;\n");
  457. WRITE(p, " ocol0.r = 1.0;\n");
  458. WRITE(p, " ocol0.a = expanded1.r / 255.0;\n");
  459. WRITE(p, " }\n");
  460. WriteEncoderEnd(p, ApiType);
  461. }
  462. const char *GenerateEncodingShader(u32 format,API_TYPE ApiType)
  463. {
  464. text[sizeof(text) - 1] = 0x7C; // canary
  465. char *p = text;
  466. switch (format)
  467. {
  468. case GX_TF_I4:
  469. WriteI4Encoder(p, ApiType);
  470. break;
  471. case GX_TF_I8:
  472. WriteI8Encoder(p, ApiType);
  473. break;
  474. case GX_TF_IA4:
  475. WriteIA4Encoder(p, ApiType);
  476. break;
  477. case GX_TF_IA8:
  478. WriteIA8Encoder(p, ApiType);
  479. break;
  480. case GX_TF_RGB565:
  481. WriteRGB565Encoder(p, ApiType);
  482. break;
  483. case GX_TF_RGB5A3:
  484. WriteRGB5A3Encoder(p, ApiType);
  485. break;
  486. case GX_TF_RGBA8:
  487. WriteRGBA8Encoder(p, ApiType);
  488. break;
  489. case GX_CTF_R4:
  490. WriteC4Encoder(p, "r", ApiType);
  491. break;
  492. case GX_CTF_RA4:
  493. WriteCC4Encoder(p, "ar", ApiType);
  494. break;
  495. case GX_CTF_RA8:
  496. WriteCC8Encoder(p, "ar", ApiType);
  497. break;
  498. case GX_CTF_A8:
  499. WriteC8Encoder(p, "a", ApiType);
  500. break;
  501. case GX_CTF_R8:
  502. WriteC8Encoder(p, "r", ApiType);
  503. break;
  504. case GX_CTF_G8:
  505. WriteC8Encoder(p, "g", ApiType);
  506. break;
  507. case GX_CTF_B8:
  508. WriteC8Encoder(p, "b", ApiType);
  509. break;
  510. case GX_CTF_RG8:
  511. WriteCC8Encoder(p, "rg", ApiType);
  512. break;
  513. case GX_CTF_GB8:
  514. WriteCC8Encoder(p, "gb", ApiType);
  515. break;
  516. case GX_TF_Z8:
  517. WriteC8Encoder(p, "r", ApiType);
  518. break;
  519. case GX_TF_Z16:
  520. WriteZ16Encoder(p, ApiType);
  521. break;
  522. case GX_TF_Z24X8:
  523. WriteZ24Encoder(p, ApiType);
  524. break;
  525. case GX_CTF_Z4:
  526. WriteC4Encoder(p, "r", ApiType);
  527. break;
  528. case GX_CTF_Z8M:
  529. WriteZ8Encoder(p, "256.0", ApiType);
  530. break;
  531. case GX_CTF_Z8L:
  532. WriteZ8Encoder(p, "65536.0" , ApiType);
  533. break;
  534. case GX_CTF_Z16L:
  535. WriteZ16LEncoder(p, ApiType);
  536. break;
  537. default:
  538. PanicAlert("Unknown texture copy format: 0x%x\n", format);
  539. break;
  540. }
  541. if (text[sizeof(text) - 1] != 0x7C)
  542. PanicAlert("TextureConversionShader generator - buffer too small, canary has been eaten!");
  543. return text;
  544. }
  545. } // namespace