TextureConversionShader.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoCommon/TextureConversionShader.h"
  4. #include <map>
  5. #include <sstream>
  6. #include <string_view>
  7. #include "Common/CommonTypes.h"
  8. #include "Common/MathUtil.h"
  9. #include "Common/MsgHandler.h"
  10. #include "VideoCommon/ShaderGenCommon.h"
  11. #include "VideoCommon/TextureCacheBase.h"
  12. #include "VideoCommon/VertexManagerBase.h"
  13. #include "VideoCommon/VideoCommon.h"
  14. #include "VideoCommon/VideoConfig.h"
  15. namespace TextureConversionShaderTiled
  16. {
  17. u16 GetEncodedSampleCount(EFBCopyFormat format)
  18. {
  19. switch (format)
  20. {
  21. case EFBCopyFormat::R4:
  22. return 8;
  23. case EFBCopyFormat::RA4:
  24. return 4;
  25. case EFBCopyFormat::RA8:
  26. return 2;
  27. case EFBCopyFormat::RGB565:
  28. return 2;
  29. case EFBCopyFormat::RGB5A3:
  30. return 2;
  31. case EFBCopyFormat::RGBA8:
  32. return 1;
  33. case EFBCopyFormat::A8:
  34. case EFBCopyFormat::R8_0x1:
  35. case EFBCopyFormat::R8:
  36. case EFBCopyFormat::G8:
  37. case EFBCopyFormat::B8:
  38. return 4;
  39. case EFBCopyFormat::RG8:
  40. case EFBCopyFormat::GB8:
  41. return 2;
  42. case EFBCopyFormat::XFB:
  43. return 2;
  44. default:
  45. PanicAlertFmt("Invalid EFB Copy Format {}! (GetEncodedSampleCount)", format);
  46. return 1;
  47. }
  48. }
  49. static void WriteHeader(ShaderCode& code, APIType api_type)
  50. {
  51. // left, top, of source rectangle within source texture
  52. // width of the destination rectangle, scale_factor (1 or 2)
  53. code.Write("UBO_BINDING(std140, 1) uniform PSBlock {{\n"
  54. " int4 position;\n"
  55. " float y_scale;\n"
  56. " float gamma_rcp;\n"
  57. " float2 clamp_tb;\n"
  58. " uint3 filter_coefficients;\n"
  59. "}};\n");
  60. if (g_ActiveConfig.backend_info.bSupportsGeometryShaders)
  61. {
  62. code.Write("VARYING_LOCATION(0) in VertexData {{\n"
  63. " float3 v_tex0;\n"
  64. "}};\n");
  65. }
  66. else
  67. {
  68. code.Write("VARYING_LOCATION(0) in float3 v_tex0;\n");
  69. }
  70. code.Write("SAMPLER_BINDING(0) uniform sampler2DArray samp0;\n"
  71. "FRAGMENT_OUTPUT_LOCATION(0) out float4 ocol0;\n");
  72. // Alpha channel in the copy is set to 1 the EFB format does not have an alpha channel.
  73. code.Write("float4 RGBA8ToRGB8(float4 src)\n"
  74. "{{\n"
  75. " return float4(src.xyz, 1.0);\n"
  76. "}}\n"
  77. "float4 RGBA8ToRGBA6(float4 src)\n"
  78. "{{\n"
  79. " int4 val = int4(roundEven(src * 255.0));\n"
  80. " val = (val & 0xfc) | (val >> 6);\n"
  81. " return float4(val) / 255.0;\n"
  82. "}}\n"
  83. "float4 RGBA8ToRGB565(float4 src)\n"
  84. "{{\n"
  85. " int4 val = int4(roundEven(src * 255.0));\n"
  86. " val.r = (val.r & 0xf8) | (val.r >> 5);\n"
  87. " val.g = (val.g & 0xfc) | (val.g >> 6);\n"
  88. " val.b = (val.b & 0xf8) | (val.b >> 5);\n"
  89. " val.a = 255;\n"
  90. " return float4(val) / 255.0;\n"
  91. "}}\n");
  92. }
  93. static void WriteSampleFunction(ShaderCode& code, const EFBCopyParams& params, APIType api_type)
  94. {
  95. code.Write("uint4 SampleEFB0(float2 uv, float2 pixel_size, float x_offset, float y_offset) {{\n"
  96. " float4 tex_sample = texture(samp0, float3(uv.x + x_offset * pixel_size.x, ");
  97. // Reverse the direction for OpenGL, since positive numbers are distance from the bottom row.
  98. // TODO: This isn't done on TextureConverterShaderGen - maybe it handles that via pixel_size?
  99. if (api_type == APIType::OpenGL)
  100. code.Write("clamp(uv.y - y_offset * pixel_size.y, clamp_tb.x, clamp_tb.y)");
  101. else
  102. code.Write("clamp(uv.y + y_offset * pixel_size.y, clamp_tb.x, clamp_tb.y)");
  103. code.Write(", 0.0));\n");
  104. // TODO: Is this really needed? Doesn't the EFB only store appropriate values? Or is this for
  105. // EFB2Ram having consistent output with force 32-bit color?
  106. if (params.efb_format == PixelFormat::RGB8_Z24)
  107. code.Write(" tex_sample = RGBA8ToRGB8(tex_sample);\n");
  108. else if (params.efb_format == PixelFormat::RGBA6_Z24)
  109. code.Write(" tex_sample = RGBA8ToRGBA6(tex_sample);\n");
  110. else if (params.efb_format == PixelFormat::RGB565_Z16)
  111. code.Write(" tex_sample = RGBA8ToRGB565(tex_sample);\n");
  112. if (params.depth)
  113. {
  114. if (!g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
  115. code.Write(" tex_sample.x = 1.0 - tex_sample.x;\n");
  116. code.Write(" uint depth = uint(tex_sample.x * 16777216.0);\n"
  117. " return uint4((depth >> 16) & 255u, (depth >> 8) & 255u, depth & 255u, 255u);\n"
  118. "}}\n");
  119. }
  120. else
  121. {
  122. code.Write(" return uint4(tex_sample * 255.0);\n"
  123. "}}\n");
  124. }
  125. // The copy filter applies to both color and depth copies. This has been verified on hardware.
  126. // The filter is only applied to the RGB channels, the alpha channel is left intact.
  127. code.Write("float4 SampleEFB(float2 uv, float2 pixel_size, int x_offset)\n"
  128. "{{\n");
  129. if (params.all_copy_filter_coefs_needed)
  130. {
  131. code.Write(" uint4 prev_row = SampleEFB0(uv, pixel_size, float(x_offset), -1.0f);\n"
  132. " uint4 current_row = SampleEFB0(uv, pixel_size, float(x_offset), 0.0f);\n"
  133. " uint4 next_row = SampleEFB0(uv, pixel_size, float(x_offset), 1.0f);\n"
  134. " uint3 combined_rows = prev_row.rgb * filter_coefficients[0] +\n"
  135. " current_row.rgb * filter_coefficients[1] +\n"
  136. " next_row.rgb * filter_coefficients[2];\n");
  137. }
  138. else
  139. {
  140. code.Write(" uint4 current_row = SampleEFB0(uv, pixel_size, float(x_offset), 0.0f);\n"
  141. " uint3 combined_rows = current_row.rgb * filter_coefficients[1];\n");
  142. }
  143. code.Write(" // Shift right by 6 to divide by 64, as filter coefficients\n"
  144. " // that sum to 64 result in no change in brightness\n"
  145. " uint4 texcol_raw = uint4(combined_rows.rgb >> 6, current_row.a);\n");
  146. if (params.copy_filter_can_overflow)
  147. code.Write(" texcol_raw &= 0x1ffu;\n");
  148. // Note that overflow occurs when the sum of values is >= 128, but this max situation can be hit
  149. // on >= 64, so we always include it.
  150. code.Write(" texcol_raw = min(texcol_raw, uint4(255, 255, 255, 255));\n");
  151. if (params.apply_gamma)
  152. {
  153. code.Write(" texcol_raw = uint4(round(pow(float4(texcol_raw) / 255.0,\n"
  154. " float4(gamma_rcp, gamma_rcp, gamma_rcp, 1.0)) * 255.0));\n");
  155. }
  156. if (params.yuv)
  157. {
  158. code.Write(" // Intensity/YUV format conversion constants determined by hardware testing\n"
  159. " const float4 y_const = float4( 66, 129, 25, 16);\n"
  160. " const float4 u_const = float4(-38, -74, 112, 128);\n"
  161. " const float4 v_const = float4(112, -94, -18, 128);\n"
  162. " // Intensity/YUV format conversion\n"
  163. " texcol_raw.rgb = uint3(dot(y_const, float4(texcol_raw.rgb, 256)),\n"
  164. " dot(u_const, float4(texcol_raw.rgb, 256)),\n"
  165. " dot(v_const, float4(texcol_raw.rgb, 256)));\n"
  166. " // Divide by 256 and round .5 and higher up\n"
  167. " texcol_raw.rgb = (texcol_raw.rgb >> 8) + ((texcol_raw.rgb >> 7) & 1u);\n");
  168. }
  169. code.Write(" return float4(texcol_raw) / 255.0;\n");
  170. code.Write("}}\n");
  171. }
  172. // Block dimensions : widthStride, heightStride
  173. // Texture dimensions : width, height, x offset, y offset
  174. static void WriteSwizzler(ShaderCode& code, const EFBCopyParams& params, APIType api_type)
  175. {
  176. code.Write("void main()\n"
  177. "{{\n"
  178. " int2 sampleUv;\n"
  179. " int2 uv1 = int2(gl_FragCoord.xy);\n");
  180. const int blkW = TexDecoder_GetEFBCopyBlockWidthInTexels(params.copy_format);
  181. const int blkH = TexDecoder_GetEFBCopyBlockHeightInTexels(params.copy_format);
  182. int samples = GetEncodedSampleCount(params.copy_format);
  183. code.Write(" int x_block_position = (uv1.x >> {}) << {};\n",
  184. MathUtil::IntLog2(blkH * blkW / samples), MathUtil::IntLog2(blkW));
  185. code.Write(" int y_block_position = uv1.y << {};\n", MathUtil::IntLog2(blkH));
  186. if (samples == 1)
  187. {
  188. // With samples == 1, we write out pairs of blocks; one A8R8, one G8B8.
  189. code.Write(" bool first = (uv1.x & {}) == 0;\n", blkH * blkW / 2);
  190. samples = 2;
  191. }
  192. code.Write(" int offset_in_block = uv1.x & {};\n", (blkH * blkW / samples) - 1);
  193. code.Write(" int y_offset_in_block = offset_in_block >> {};\n",
  194. MathUtil::IntLog2(blkW / samples));
  195. code.Write(" int x_offset_in_block = (offset_in_block & {}) << {};\n", (blkW / samples) - 1,
  196. MathUtil::IntLog2(samples));
  197. code.Write(" sampleUv.x = x_block_position + x_offset_in_block;\n"
  198. " sampleUv.y = y_block_position + y_offset_in_block;\n");
  199. // sampleUv is the sample position in (int)gx_coords
  200. code.Write(" float2 uv0 = float2(sampleUv);\n");
  201. // Move to center of pixel
  202. code.Write(" uv0 += float2(0.5, 0.5);\n");
  203. // Scale by two if needed (also move to pixel borders
  204. // so that linear filtering will average adjacent
  205. // pixel)
  206. code.Write(" uv0 *= float(position.w);\n");
  207. // Move to copied rect
  208. code.Write(" uv0 += float2(position.xy);\n");
  209. // Normalize to [0:1]
  210. code.Write(" uv0 /= float2({}, {});\n", EFB_WIDTH, EFB_HEIGHT);
  211. // Apply the y scaling
  212. code.Write(" uv0 /= float2(1, y_scale);\n");
  213. // OGL has to flip up and down
  214. if (api_type == APIType::OpenGL)
  215. {
  216. code.Write(" uv0.y = 1.0-uv0.y;\n");
  217. }
  218. code.Write(" float2 pixel_size = float2(position.w, position.w) / float2({}, {});\n", EFB_WIDTH,
  219. EFB_HEIGHT);
  220. }
  221. static void WriteSampleColor(ShaderCode& code, std::string_view color_comp, std::string_view dest,
  222. int x_offset, APIType api_type, const EFBCopyParams& params)
  223. {
  224. code.Write(" {} = SampleEFB(uv0, pixel_size, {}).{};\n", dest, x_offset, color_comp);
  225. }
  226. static void WriteToBitDepth(ShaderCode& code, u8 depth, std::string_view src, std::string_view dest)
  227. {
  228. code.Write(" {} = floor({} * 255.0 / exp2(8.0 - {}.0));\n", dest, src, depth);
  229. }
  230. static void WriteRGB565Encoder(ShaderCode& code, APIType api_type, const EFBCopyParams& params)
  231. {
  232. code.Write(" float3 texSample0;\n"
  233. " float3 texSample1;\n");
  234. WriteSampleColor(code, "rgb", "texSample0", 0, api_type, params);
  235. WriteSampleColor(code, "rgb", "texSample1", 1, api_type, params);
  236. code.Write(" float2 texRs = float2(texSample0.r, texSample1.r);\n"
  237. " float2 texGs = float2(texSample0.g, texSample1.g);\n"
  238. " float2 texBs = float2(texSample0.b, texSample1.b);\n");
  239. WriteToBitDepth(code, 6, "texGs", "float2 gInt");
  240. code.Write(" float2 gUpper = floor(gInt / 8.0);\n"
  241. " float2 gLower = gInt - gUpper * 8.0;\n");
  242. WriteToBitDepth(code, 5, "texRs", "ocol0.br");
  243. code.Write(" ocol0.br = ocol0.br * 8.0 + gUpper;\n");
  244. WriteToBitDepth(code, 5, "texBs", "ocol0.ga");
  245. code.Write(" ocol0.ga = ocol0.ga + gLower * 32.0;\n");
  246. code.Write(" ocol0 = ocol0 / 255.0;\n");
  247. }
  248. static void WriteRGB5A3Encoder(ShaderCode& code, APIType api_type, const EFBCopyParams& params)
  249. {
  250. code.Write(" float4 texSample;\n"
  251. " float color0;\n"
  252. " float gUpper;\n"
  253. " float gLower;\n");
  254. WriteSampleColor(code, "rgba", "texSample", 0, api_type, params);
  255. // 0.8784 = 224 / 255 which is the maximum alpha value that can be represented in 3 bits
  256. code.Write("if(texSample.a > 0.878f) {{\n");
  257. WriteToBitDepth(code, 5, "texSample.g", "color0");
  258. code.Write(" gUpper = floor(color0 / 8.0);\n"
  259. " gLower = color0 - gUpper * 8.0;\n");
  260. WriteToBitDepth(code, 5, "texSample.r", "ocol0.b");
  261. code.Write(" ocol0.b = ocol0.b * 4.0 + gUpper + 128.0;\n");
  262. WriteToBitDepth(code, 5, "texSample.b", "ocol0.g");
  263. code.Write(" ocol0.g = ocol0.g + gLower * 32.0;\n");
  264. code.Write("}} else {{\n");
  265. WriteToBitDepth(code, 4, "texSample.r", "ocol0.b");
  266. WriteToBitDepth(code, 4, "texSample.b", "ocol0.g");
  267. WriteToBitDepth(code, 3, "texSample.a", "color0");
  268. code.Write("ocol0.b = ocol0.b + color0 * 16.0;\n");
  269. WriteToBitDepth(code, 4, "texSample.g", "color0");
  270. code.Write("ocol0.g = ocol0.g + color0 * 16.0;\n");
  271. code.Write("}}\n");
  272. WriteSampleColor(code, "rgba", "texSample", 1, api_type, params);
  273. code.Write("if(texSample.a > 0.878f) {{\n");
  274. WriteToBitDepth(code, 5, "texSample.g", "color0");
  275. code.Write(" gUpper = floor(color0 / 8.0);\n"
  276. " gLower = color0 - gUpper * 8.0;\n");
  277. WriteToBitDepth(code, 5, "texSample.r", "ocol0.r");
  278. code.Write(" ocol0.r = ocol0.r * 4.0 + gUpper + 128.0;\n");
  279. WriteToBitDepth(code, 5, "texSample.b", "ocol0.a");
  280. code.Write(" ocol0.a = ocol0.a + gLower * 32.0;\n");
  281. code.Write("}} else {{\n");
  282. WriteToBitDepth(code, 4, "texSample.r", "ocol0.r");
  283. WriteToBitDepth(code, 4, "texSample.b", "ocol0.a");
  284. WriteToBitDepth(code, 3, "texSample.a", "color0");
  285. code.Write("ocol0.r = ocol0.r + color0 * 16.0;\n");
  286. WriteToBitDepth(code, 4, "texSample.g", "color0");
  287. code.Write("ocol0.a = ocol0.a + color0 * 16.0;\n");
  288. code.Write("}}\n");
  289. code.Write(" ocol0 = ocol0 / 255.0;\n");
  290. }
  291. static void WriteRGBA8Encoder(ShaderCode& code, APIType api_type, const EFBCopyParams& params)
  292. {
  293. code.Write(" float4 texSample;\n"
  294. " float4 color0;\n"
  295. " float4 color1;\n");
  296. WriteSampleColor(code, "rgba", "texSample", 0, api_type, params);
  297. code.Write(" color0.b = texSample.a;\n"
  298. " color0.g = texSample.r;\n"
  299. " color1.b = texSample.g;\n"
  300. " color1.g = texSample.b;\n");
  301. WriteSampleColor(code, "rgba", "texSample", 1, api_type, params);
  302. code.Write(" color0.r = texSample.a;\n"
  303. " color0.a = texSample.r;\n"
  304. " color1.r = texSample.g;\n"
  305. " color1.a = texSample.b;\n");
  306. code.Write(" ocol0 = first ? color0 : color1;\n");
  307. }
  308. static void WriteC4Encoder(ShaderCode& code, std::string_view comp, APIType api_type,
  309. const EFBCopyParams& params)
  310. {
  311. code.Write(" float4 color0;\n"
  312. " float4 color1;\n");
  313. WriteSampleColor(code, comp, "color0.b", 0, api_type, params);
  314. WriteSampleColor(code, comp, "color1.b", 1, api_type, params);
  315. WriteSampleColor(code, comp, "color0.g", 2, api_type, params);
  316. WriteSampleColor(code, comp, "color1.g", 3, api_type, params);
  317. WriteSampleColor(code, comp, "color0.r", 4, api_type, params);
  318. WriteSampleColor(code, comp, "color1.r", 5, api_type, params);
  319. WriteSampleColor(code, comp, "color0.a", 6, api_type, params);
  320. WriteSampleColor(code, comp, "color1.a", 7, api_type, params);
  321. WriteToBitDepth(code, 4, "color0", "color0");
  322. WriteToBitDepth(code, 4, "color1", "color1");
  323. code.Write(" ocol0 = (color0 * 16.0 + color1) / 255.0;\n");
  324. }
  325. static void WriteC8Encoder(ShaderCode& code, std::string_view comp, APIType api_type,
  326. const EFBCopyParams& params)
  327. {
  328. WriteSampleColor(code, comp, "ocol0.b", 0, api_type, params);
  329. WriteSampleColor(code, comp, "ocol0.g", 1, api_type, params);
  330. WriteSampleColor(code, comp, "ocol0.r", 2, api_type, params);
  331. WriteSampleColor(code, comp, "ocol0.a", 3, api_type, params);
  332. }
  333. static void WriteCC4Encoder(ShaderCode& code, std::string_view comp, APIType api_type,
  334. const EFBCopyParams& params)
  335. {
  336. code.Write(" float2 texSample;\n"
  337. " float4 color0;\n"
  338. " float4 color1;\n");
  339. WriteSampleColor(code, comp, "texSample", 0, api_type, params);
  340. code.Write(" color0.b = texSample.x;\n"
  341. " color1.b = texSample.y;\n");
  342. WriteSampleColor(code, comp, "texSample", 1, api_type, params);
  343. code.Write(" color0.g = texSample.x;\n"
  344. " color1.g = texSample.y;\n");
  345. WriteSampleColor(code, comp, "texSample", 2, api_type, params);
  346. code.Write(" color0.r = texSample.x;\n"
  347. " color1.r = texSample.y;\n");
  348. WriteSampleColor(code, comp, "texSample", 3, api_type, params);
  349. code.Write(" color0.a = texSample.x;\n"
  350. " color1.a = texSample.y;\n");
  351. WriteToBitDepth(code, 4, "color0", "color0");
  352. WriteToBitDepth(code, 4, "color1", "color1");
  353. code.Write(" ocol0 = (color0 * 16.0 + color1) / 255.0;\n");
  354. }
  355. static void WriteCC8Encoder(ShaderCode& code, std::string_view comp, APIType api_type,
  356. const EFBCopyParams& params)
  357. {
  358. WriteSampleColor(code, comp, "ocol0.bg", 0, api_type, params);
  359. WriteSampleColor(code, comp, "ocol0.ra", 1, api_type, params);
  360. }
  361. static void WriteXFBEncoder(ShaderCode& code, APIType api_type, const EFBCopyParams& params)
  362. {
  363. code.Write("float4 color0 = float4(0, 0, 0, 1), color1 = float4(0, 0, 0, 1);\n");
  364. WriteSampleColor(code, "rgb", "color0.rgb", 0, api_type, params);
  365. WriteSampleColor(code, "rgb", "color1.rgb", 1, api_type, params);
  366. // Convert to YUV.
  367. code.Write(" // Intensity/YUV format conversion constants determined by hardware testing\n"
  368. " const float4 y_const = float4( 66, 129, 25, 16);\n"
  369. " const float4 u_const = float4(-38, -74, 112, 128);\n"
  370. " const float4 v_const = float4(112, -94, -18, 128);\n"
  371. " float4 average = (color0 + color1) * 0.5;\n"
  372. " // TODO: check rounding\n"
  373. " ocol0.b = round(dot(color0, y_const)) / 256.0;\n"
  374. " ocol0.g = round(dot(average, u_const)) / 256.0;\n"
  375. " ocol0.r = round(dot(color1, y_const)) / 256.0;\n"
  376. " ocol0.a = round(dot(average, v_const)) / 256.0;\n");
  377. }
  378. std::string GenerateEncodingShader(const EFBCopyParams& params, APIType api_type)
  379. {
  380. ShaderCode code;
  381. WriteHeader(code, api_type);
  382. WriteSampleFunction(code, params, api_type);
  383. WriteSwizzler(code, params, api_type);
  384. switch (params.copy_format)
  385. {
  386. case EFBCopyFormat::R4:
  387. WriteC4Encoder(code, "r", api_type, params);
  388. break;
  389. case EFBCopyFormat::RA4:
  390. WriteCC4Encoder(code, "ar", api_type, params);
  391. break;
  392. case EFBCopyFormat::RA8:
  393. WriteCC8Encoder(code, "ar", api_type, params);
  394. break;
  395. case EFBCopyFormat::RGB565:
  396. WriteRGB565Encoder(code, api_type, params);
  397. break;
  398. case EFBCopyFormat::RGB5A3:
  399. WriteRGB5A3Encoder(code, api_type, params);
  400. break;
  401. case EFBCopyFormat::RGBA8:
  402. WriteRGBA8Encoder(code, api_type, params);
  403. break;
  404. case EFBCopyFormat::A8:
  405. WriteC8Encoder(code, "a", api_type, params);
  406. break;
  407. case EFBCopyFormat::R8_0x1:
  408. case EFBCopyFormat::R8:
  409. WriteC8Encoder(code, "r", api_type, params);
  410. break;
  411. case EFBCopyFormat::G8:
  412. WriteC8Encoder(code, "g", api_type, params);
  413. break;
  414. case EFBCopyFormat::B8:
  415. WriteC8Encoder(code, "b", api_type, params);
  416. break;
  417. case EFBCopyFormat::RG8:
  418. WriteCC8Encoder(code, "gr", api_type, params);
  419. break;
  420. case EFBCopyFormat::GB8:
  421. WriteCC8Encoder(code, "bg", api_type, params);
  422. break;
  423. case EFBCopyFormat::XFB:
  424. WriteXFBEncoder(code, api_type, params);
  425. break;
  426. default:
  427. PanicAlertFmt("Invalid EFB Copy Format {}! (GenerateEncodingShader)", params.copy_format);
  428. break;
  429. }
  430. code.Write("}}\n");
  431. return code.GetBuffer();
  432. }
  433. // NOTE: In these uniforms, a row refers to a row of blocks, not texels.
  434. static const char decoding_shader_header[] = R"(
  435. #if defined(PALETTE_FORMAT_IA8) || defined(PALETTE_FORMAT_RGB565) || defined(PALETTE_FORMAT_RGB5A3)
  436. #define HAS_PALETTE 1
  437. #endif
  438. UBO_BINDING(std140, 1) uniform UBO {
  439. uint2 u_dst_size;
  440. uint2 u_src_size;
  441. uint u_src_offset;
  442. uint u_src_row_stride;
  443. uint u_palette_offset;
  444. };
  445. #if defined(API_METAL)
  446. #if defined(TEXEL_BUFFER_FORMAT_R8)
  447. SSBO_BINDING(0) readonly buffer Input { uint8_t s_input_buffer[]; };
  448. #define FETCH(offset) uint(s_input_buffer[offset])
  449. #elif defined(TEXEL_BUFFER_FORMAT_R16)
  450. SSBO_BINDING(0) readonly buffer Input { uint16_t s_input_buffer[]; };
  451. #define FETCH(offset) uint(s_input_buffer[offset])
  452. #elif defined(TEXEL_BUFFER_FORMAT_RGBA8)
  453. SSBO_BINDING(0) readonly buffer Input { u8vec4 s_input_buffer[]; };
  454. #define FETCH(offset) uvec4(s_input_buffer[offset])
  455. #elif defined(TEXEL_BUFFER_FORMAT_R32G32)
  456. SSBO_BINDING(0) readonly buffer Input { uvec2 s_input_buffer[]; };
  457. #define FETCH(offset) s_input_buffer[offset]
  458. #else
  459. #error No texel buffer?
  460. #endif
  461. #ifdef HAS_PALETTE
  462. SSBO_BINDING(1) readonly buffer Palette { uint16_t s_palette_buffer[]; };
  463. #define FETCH_PALETTE(offset) uint(s_palette_buffer[offset])
  464. #endif
  465. #else
  466. TEXEL_BUFFER_BINDING(0) uniform usamplerBuffer s_input_buffer;
  467. #if defined(TEXEL_BUFFER_FORMAT_R8) || defined(TEXEL_BUFFER_FORMAT_R16)
  468. #define FETCH(offset) texelFetch(s_input_buffer, int((offset) + u_src_offset)).r
  469. #elif defined(TEXEL_BUFFER_FORMAT_RGBA8)
  470. #define FETCH(offset) texelFetch(s_input_buffer, int((offset) + u_src_offset))
  471. #elif defined(TEXEL_BUFFER_FORMAT_R32G32)
  472. #define FETCH(offset) texelFetch(s_input_buffer, int((offset) + u_src_offset)).rg
  473. #else
  474. #error No texel buffer?
  475. #endif
  476. #ifdef HAS_PALETTE
  477. TEXEL_BUFFER_BINDING(1) uniform usamplerBuffer s_palette_buffer;
  478. #define FETCH_PALETTE(offset) texelFetch(s_palette_buffer, int((offset) + u_palette_offset)).r
  479. #endif
  480. #endif // defined(API_METAL)
  481. IMAGE_BINDING(rgba8, 0) uniform writeonly image2DArray output_image;
  482. #define GROUP_MEMORY_BARRIER_WITH_SYNC memoryBarrierShared(); barrier();
  483. #define GROUP_SHARED shared
  484. #define DEFINE_MAIN(lx, ly) \
  485. layout(local_size_x = lx, local_size_y = ly) in; \
  486. void main()
  487. uint Swap16(uint v)
  488. {
  489. // Convert BE to LE.
  490. return ((v >> 8) | (v << 8)) & 0xFFFFu;
  491. }
  492. uint Convert3To8(uint v)
  493. {
  494. // Swizzle bits: 00000123 -> 12312312
  495. return (v << 5) | (v << 2) | (v >> 1);
  496. }
  497. uint Convert4To8(uint v)
  498. {
  499. // Swizzle bits: 00001234 -> 12341234
  500. return (v << 4) | v;
  501. }
  502. uint Convert5To8(uint v)
  503. {
  504. // Swizzle bits: 00012345 -> 12345123
  505. return (v << 3) | (v >> 2);
  506. }
  507. uint Convert6To8(uint v)
  508. {
  509. // Swizzle bits: 00123456 -> 12345612
  510. return (v << 2) | (v >> 4);
  511. }
  512. uint GetTiledTexelOffset(uint2 block_size, uint2 coords)
  513. {
  514. uint2 block = coords / block_size;
  515. uint2 offset = coords % block_size;
  516. uint buffer_pos = 0u;
  517. buffer_pos += block.y * u_src_row_stride;
  518. buffer_pos += block.x * (block_size.x * block_size.y);
  519. buffer_pos += offset.y * block_size.x;
  520. buffer_pos += offset.x;
  521. return buffer_pos;
  522. }
  523. #if defined(HAS_PALETTE)
  524. uint4 GetPaletteColor(uint index)
  525. {
  526. // Fetch and swap BE to LE.
  527. uint val = Swap16(FETCH_PALETTE(index));
  528. uint4 color;
  529. #if defined(PALETTE_FORMAT_IA8)
  530. uint a = bitfieldExtract(val, 8, 8);
  531. uint i = bitfieldExtract(val, 0, 8);
  532. color = uint4(i, i, i, a);
  533. #elif defined(PALETTE_FORMAT_RGB565)
  534. color.x = Convert5To8(bitfieldExtract(val, 11, 5));
  535. color.y = Convert6To8(bitfieldExtract(val, 5, 6));
  536. color.z = Convert5To8(bitfieldExtract(val, 0, 5));
  537. color.a = 255u;
  538. #elif defined(PALETTE_FORMAT_RGB5A3)
  539. if ((val & 0x8000u) != 0u)
  540. {
  541. color.x = Convert5To8(bitfieldExtract(val, 10, 5));
  542. color.y = Convert5To8(bitfieldExtract(val, 5, 5));
  543. color.z = Convert5To8(bitfieldExtract(val, 0, 5));
  544. color.a = 255u;
  545. }
  546. else
  547. {
  548. color.a = Convert3To8(bitfieldExtract(val, 12, 3));
  549. color.r = Convert4To8(bitfieldExtract(val, 8, 4));
  550. color.g = Convert4To8(bitfieldExtract(val, 4, 4));
  551. color.b = Convert4To8(bitfieldExtract(val, 0, 4));
  552. }
  553. #else
  554. // Not used.
  555. color = uint4(0, 0, 0, 0);
  556. #endif
  557. return color;
  558. }
  559. float4 GetPaletteColorNormalized(uint index)
  560. {
  561. uint4 color = GetPaletteColor(index);
  562. return float4(color) / 255.0;
  563. }
  564. #endif // defined(HAS_PALETTE)
  565. )";
  566. static const std::map<TextureFormat, DecodingShaderInfo> s_decoding_shader_info{
  567. {TextureFormat::I4,
  568. {TEXEL_BUFFER_FORMAT_R8_UINT, 0, 8, 8, false,
  569. R"(
  570. DEFINE_MAIN(8, 8)
  571. {
  572. uint2 coords = gl_GlobalInvocationID.xy;
  573. // Tiled in 8x8 blocks, 4 bits per pixel
  574. // We need to do the tiling manually here because the texel size is smaller than
  575. // the size of the buffer elements.
  576. uint2 block = coords.xy / 8u;
  577. uint2 offset = coords.xy % 8u;
  578. uint buffer_pos = 0u;
  579. buffer_pos += block.y * u_src_row_stride;
  580. buffer_pos += block.x * 32u;
  581. buffer_pos += offset.y * 4u;
  582. buffer_pos += offset.x / 2u;
  583. // Select high nibble for odd texels, low for even.
  584. uint val = FETCH(buffer_pos);
  585. uint i;
  586. if ((coords.x & 1u) == 0u)
  587. i = Convert4To8((val >> 4));
  588. else
  589. i = Convert4To8((val & 0x0Fu));
  590. uint4 color = uint4(i, i, i, i);
  591. float4 norm_color = float4(color) / 255.0;
  592. imageStore(output_image, int3(int2(coords), 0), norm_color);
  593. }
  594. )"}},
  595. {TextureFormat::IA4,
  596. {TEXEL_BUFFER_FORMAT_R8_UINT, 0, 8, 8, false,
  597. R"(
  598. DEFINE_MAIN(8, 8)
  599. {
  600. uint2 coords = gl_GlobalInvocationID.xy;
  601. // Tiled in 8x4 blocks, 8 bits per pixel
  602. uint buffer_pos = GetTiledTexelOffset(uint2(8u, 4u), coords);
  603. uint val = FETCH(buffer_pos);
  604. uint i = Convert4To8((val & 0x0Fu));
  605. uint a = Convert4To8((val >> 4));
  606. uint4 color = uint4(i, i, i, a);
  607. float4 norm_color = float4(color) / 255.0;
  608. imageStore(output_image, int3(int2(coords), 0), norm_color);
  609. }
  610. )"}},
  611. {TextureFormat::I8,
  612. {TEXEL_BUFFER_FORMAT_R8_UINT, 0, 8, 8, false,
  613. R"(
  614. DEFINE_MAIN(8, 8)
  615. {
  616. uint2 coords = gl_GlobalInvocationID.xy;
  617. // Tiled in 8x4 blocks, 8 bits per pixel
  618. uint buffer_pos = GetTiledTexelOffset(uint2(8u, 4u), coords);
  619. uint i = FETCH(buffer_pos);
  620. uint4 color = uint4(i, i, i, i);
  621. float4 norm_color = float4(color) / 255.0;
  622. imageStore(output_image, int3(int2(coords), 0), norm_color);
  623. }
  624. )"}},
  625. {TextureFormat::IA8,
  626. {TEXEL_BUFFER_FORMAT_R16_UINT, 0, 8, 8, false,
  627. R"(
  628. DEFINE_MAIN(8, 8)
  629. {
  630. uint2 coords = gl_GlobalInvocationID.xy;
  631. // Tiled in 4x4 blocks, 16 bits per pixel
  632. uint buffer_pos = GetTiledTexelOffset(uint2(4u, 4u), coords);
  633. uint val = FETCH(buffer_pos);
  634. uint a = (val & 0xFFu);
  635. uint i = (val >> 8);
  636. uint4 color = uint4(i, i, i, a);
  637. float4 norm_color = float4(color) / 255.0;
  638. imageStore(output_image, int3(int2(coords), 0), norm_color);
  639. }
  640. )"}},
  641. {TextureFormat::RGB565,
  642. {TEXEL_BUFFER_FORMAT_R16_UINT, 0, 8, 8, false,
  643. R"(
  644. DEFINE_MAIN(8, 8)
  645. {
  646. uint2 coords = gl_GlobalInvocationID.xy;
  647. // Tiled in 4x4 blocks
  648. uint buffer_pos = GetTiledTexelOffset(uint2(4u, 4u), coords);
  649. uint val = Swap16(FETCH(buffer_pos));
  650. uint4 color;
  651. color.x = Convert5To8(bitfieldExtract(val, 11, 5));
  652. color.y = Convert6To8(bitfieldExtract(val, 5, 6));
  653. color.z = Convert5To8(bitfieldExtract(val, 0, 5));
  654. color.a = 255u;
  655. float4 norm_color = float4(color) / 255.0;
  656. imageStore(output_image, int3(int2(coords), 0), norm_color);
  657. }
  658. )"}},
  659. {TextureFormat::RGB5A3,
  660. {TEXEL_BUFFER_FORMAT_R16_UINT, 0, 8, 8, false,
  661. R"(
  662. DEFINE_MAIN(8, 8)
  663. {
  664. uint2 coords = gl_GlobalInvocationID.xy;
  665. // Tiled in 4x4 blocks
  666. uint buffer_pos = GetTiledTexelOffset(uint2(4u, 4u), coords);
  667. uint val = Swap16(FETCH(buffer_pos));
  668. uint4 color;
  669. if ((val & 0x8000u) != 0u)
  670. {
  671. color.x = Convert5To8(bitfieldExtract(val, 10, 5));
  672. color.y = Convert5To8(bitfieldExtract(val, 5, 5));
  673. color.z = Convert5To8(bitfieldExtract(val, 0, 5));
  674. color.a = 255u;
  675. }
  676. else
  677. {
  678. color.a = Convert3To8(bitfieldExtract(val, 12, 3));
  679. color.r = Convert4To8(bitfieldExtract(val, 8, 4));
  680. color.g = Convert4To8(bitfieldExtract(val, 4, 4));
  681. color.b = Convert4To8(bitfieldExtract(val, 0, 4));
  682. }
  683. float4 norm_color = float4(color) / 255.0;
  684. imageStore(output_image, int3(int2(coords), 0), norm_color);
  685. }
  686. )"}},
  687. {TextureFormat::RGBA8,
  688. {TEXEL_BUFFER_FORMAT_R16_UINT, 0, 8, 8, false,
  689. R"(
  690. DEFINE_MAIN(8, 8)
  691. {
  692. uint2 coords = gl_GlobalInvocationID.xy;
  693. // Tiled in 4x4 blocks
  694. // We can't use the normal calculation function, as these are packed as the AR channels
  695. // for the entire block, then the GB channels afterwards.
  696. uint2 block = coords.xy / 4u;
  697. uint2 offset = coords.xy % 4u;
  698. uint buffer_pos = 0u;
  699. // Our buffer has 16-bit elements, so the offsets here are half what they would be in bytes.
  700. buffer_pos += block.y * u_src_row_stride;
  701. buffer_pos += block.x * 32u;
  702. buffer_pos += offset.y * 4u;
  703. buffer_pos += offset.x;
  704. // The two GB channels follow after the block's AR channels.
  705. uint val1 = FETCH(buffer_pos + 0u);
  706. uint val2 = FETCH(buffer_pos + 16u);
  707. uint4 color;
  708. color.a = (val1 & 0xFFu);
  709. color.r = (val1 >> 8);
  710. color.g = (val2 & 0xFFu);
  711. color.b = (val2 >> 8);
  712. float4 norm_color = float4(color) / 255.0;
  713. imageStore(output_image, int3(int2(coords), 0), norm_color);
  714. }
  715. )"}},
  716. {TextureFormat::CMPR,
  717. {TEXEL_BUFFER_FORMAT_R32G32_UINT, 0, 64, 1, true,
  718. R"(
  719. // In the compute version of this decoder, we flatten the blocks to a one-dimension array.
  720. // Each group is subdivided into 16, and the first thread in each group fetches the DXT data.
  721. // All threads then calculate the possible colors for the block and write to the output image.
  722. #define GROUP_SIZE 64u
  723. #define BLOCK_SIZE_X 4u
  724. #define BLOCK_SIZE_Y 4u
  725. #define BLOCK_SIZE (BLOCK_SIZE_X * BLOCK_SIZE_Y)
  726. #define BLOCKS_PER_GROUP (GROUP_SIZE / BLOCK_SIZE)
  727. uint DXTBlend(uint v1, uint v2)
  728. {
  729. // 3/8 blend, which is close to 1/3
  730. return ((v1 * 3u + v2 * 5u) >> 3);
  731. }
  732. GROUP_SHARED uint2 shared_temp[BLOCKS_PER_GROUP];
  733. DEFINE_MAIN(GROUP_SIZE, 1)
  734. {
  735. uint local_thread_id = gl_LocalInvocationID.x;
  736. uint block_in_group = local_thread_id / BLOCK_SIZE;
  737. uint thread_in_block = local_thread_id % BLOCK_SIZE;
  738. uint block_index = gl_WorkGroupID.x * BLOCKS_PER_GROUP + block_in_group;
  739. // Annoyingly, we can't precalculate this as a uniform because the DXT block size differs
  740. // from the block size of the overall texture (4 vs 8). We can however use a multiply and
  741. // subtraction to avoid the modulo for calculating the block's X coordinate.
  742. uint blocks_wide = u_src_size.x / BLOCK_SIZE_X;
  743. uint2 block_coords;
  744. block_coords.y = block_index / blocks_wide;
  745. block_coords.x = block_index - (block_coords.y * blocks_wide);
  746. // Only the first thread for each block reads from the texel buffer.
  747. if (thread_in_block == 0u)
  748. {
  749. // Calculate tiled block coordinates.
  750. uint2 tile_block_coords = block_coords / 2u;
  751. uint2 subtile_block_coords = block_coords % 2u;
  752. uint buffer_pos = 0u;
  753. buffer_pos += tile_block_coords.y * u_src_row_stride;
  754. buffer_pos += tile_block_coords.x * 4u;
  755. buffer_pos += subtile_block_coords.y * 2u;
  756. buffer_pos += subtile_block_coords.x;
  757. // Read the entire DXT block to shared memory.
  758. uint2 raw_data = FETCH(buffer_pos);
  759. shared_temp[block_in_group] = raw_data;
  760. }
  761. // Ensure store is completed before the remaining threads in the block continue.
  762. GROUP_MEMORY_BARRIER_WITH_SYNC;
  763. // Unpack colors and swap BE to LE.
  764. uint2 raw_data = shared_temp[block_in_group];
  765. uint swapped = ((raw_data.x & 0xFF00FF00u) >> 8) | ((raw_data.x & 0x00FF00FFu) << 8);
  766. uint c1 = swapped & 0xFFFFu;
  767. uint c2 = swapped >> 16;
  768. // Expand 5/6 bit channels to 8-bits per channel.
  769. uint blue1 = Convert5To8(bitfieldExtract(c1, 0, 5));
  770. uint blue2 = Convert5To8(bitfieldExtract(c2, 0, 5));
  771. uint green1 = Convert6To8(bitfieldExtract(c1, 5, 6));
  772. uint green2 = Convert6To8(bitfieldExtract(c2, 5, 6));
  773. uint red1 = Convert5To8(bitfieldExtract(c1, 11, 5));
  774. uint red2 = Convert5To8(bitfieldExtract(c2, 11, 5));
  775. // Determine the four colors the block can use.
  776. // It's quicker to just precalculate all four colors rather than branching on the index.
  777. // NOTE: These must be masked with 0xFF. This is done at the normalization stage below.
  778. uint4 color0, color1, color2, color3;
  779. color0 = uint4(red1, green1, blue1, 255u);
  780. color1 = uint4(red2, green2, blue2, 255u);
  781. if (c1 > c2)
  782. {
  783. color2 = uint4(DXTBlend(red2, red1), DXTBlend(green2, green1), DXTBlend(blue2, blue1), 255u);
  784. color3 = uint4(DXTBlend(red1, red2), DXTBlend(green1, green2), DXTBlend(blue1, blue2), 255u);
  785. }
  786. else
  787. {
  788. color2 = uint4((red1 + red2) / 2u, (green1 + green2) / 2u, (blue1 + blue2) / 2u, 255u);
  789. color3 = uint4((red1 + red2) / 2u, (green1 + green2) / 2u, (blue1 + blue2) / 2u, 0u);
  790. }
  791. // Calculate the texel coordinates that we will write to.
  792. // The divides/modulo here should be turned into a shift/binary AND.
  793. uint local_y = thread_in_block / BLOCK_SIZE_X;
  794. uint local_x = thread_in_block % BLOCK_SIZE_X;
  795. uint global_x = block_coords.x * BLOCK_SIZE_X + local_x;
  796. uint global_y = block_coords.y * BLOCK_SIZE_Y + local_y;
  797. // Use the coordinates within the block to shift the 32-bit value containing
  798. // all 16 indices to a single 2-bit index.
  799. uint index = bitfieldExtract(raw_data.y, int((local_y * 8u) + (6u - local_x * 2u)), 2);
  800. // Select the un-normalized color from the precalculated color array.
  801. // Using a switch statement here removes the need for dynamic indexing of an array.
  802. uint4 color;
  803. switch (index)
  804. {
  805. case 0u: color = color0; break;
  806. case 1u: color = color1; break;
  807. case 2u: color = color2; break;
  808. case 3u: color = color3; break;
  809. default: color = color0; break;
  810. }
  811. // Normalize and write to the output image.
  812. float4 norm_color = float4(color & 0xFFu) / 255.0;
  813. imageStore(output_image, int3(int2(uint2(global_x, global_y)), 0), norm_color);
  814. }
  815. )"}},
  816. {TextureFormat::C4,
  817. {TEXEL_BUFFER_FORMAT_R8_UINT, static_cast<u32>(TexDecoder_GetPaletteSize(TextureFormat::C4)),
  818. 8, 8, false,
  819. R"(
  820. DEFINE_MAIN(8, 8)
  821. {
  822. uint2 coords = gl_GlobalInvocationID.xy;
  823. // Tiled in 8x8 blocks, 4 bits per pixel
  824. // We need to do the tiling manually here because the texel size is smaller than
  825. // the size of the buffer elements.
  826. uint2 block = coords.xy / 8u;
  827. uint2 offset = coords.xy % 8u;
  828. uint buffer_pos = 0u;
  829. buffer_pos += block.y * u_src_row_stride;
  830. buffer_pos += block.x * 32u;
  831. buffer_pos += offset.y * 4u;
  832. buffer_pos += offset.x / 2u;
  833. // Select high nibble for odd texels, low for even.
  834. uint val = FETCH(buffer_pos);
  835. uint index = ((coords.x & 1u) == 0u) ? (val >> 4) : (val & 0x0Fu);
  836. float4 norm_color = GetPaletteColorNormalized(index);
  837. imageStore(output_image, int3(int2(coords), 0), norm_color);
  838. }
  839. )"}},
  840. {TextureFormat::C8,
  841. {TEXEL_BUFFER_FORMAT_R8_UINT, static_cast<u32>(TexDecoder_GetPaletteSize(TextureFormat::C8)),
  842. 8, 8, false,
  843. R"(
  844. DEFINE_MAIN(8, 8)
  845. {
  846. uint2 coords = gl_GlobalInvocationID.xy;
  847. // Tiled in 8x4 blocks, 8 bits per pixel
  848. uint buffer_pos = GetTiledTexelOffset(uint2(8u, 4u), coords);
  849. uint index = FETCH(buffer_pos);
  850. float4 norm_color = GetPaletteColorNormalized(index);
  851. imageStore(output_image, int3(int2(coords), 0), norm_color);
  852. }
  853. )"}},
  854. {TextureFormat::C14X2,
  855. {TEXEL_BUFFER_FORMAT_R16_UINT,
  856. static_cast<u32>(TexDecoder_GetPaletteSize(TextureFormat::C14X2)), 8, 8, false,
  857. R"(
  858. DEFINE_MAIN(8, 8)
  859. {
  860. uint2 coords = gl_GlobalInvocationID.xy;
  861. // Tiled in 4x4 blocks, 16 bits per pixel
  862. uint buffer_pos = GetTiledTexelOffset(uint2(4u, 4u), coords);
  863. uint index = Swap16(FETCH(buffer_pos)) & 0x3FFFu;
  864. float4 norm_color = GetPaletteColorNormalized(index);
  865. imageStore(output_image, int3(int2(coords), 0), norm_color);
  866. }
  867. )"}},
  868. // We do the inverse BT.601 conversion for YCbCr to RGB
  869. // http://www.equasys.de/colorconversion.html#YCbCr-RGBColorFormatConversion
  870. // TODO: Use more precise numbers for this conversion (although on real hardware, the XFB isn't
  871. // in a real texture format, so does this conversion actually ever happen?)
  872. {TextureFormat::XFB,
  873. {TEXEL_BUFFER_FORMAT_RGBA8_UINT, 0, 8, 8, false,
  874. R"(
  875. DEFINE_MAIN(8, 8)
  876. {
  877. uint2 uv = gl_GlobalInvocationID.xy;
  878. uint buffer_pos = (uv.y * u_src_row_stride) + (uv.x / 2u);
  879. float4 yuyv = float4(FETCH(buffer_pos));
  880. float y = (uv.x & 1u) != 0u ? yuyv.b : yuyv.r;
  881. float yComp = 1.164 * (y - 16.0);
  882. float uComp = yuyv.g - 128.0;
  883. float vComp = yuyv.a - 128.0;
  884. float4 rgb = float4(yComp + (1.596 * vComp),
  885. yComp - (0.813 * vComp) - (0.391 * uComp),
  886. yComp + (2.018 * uComp),
  887. 255.0);
  888. float4 rgba_norm = rgb / 255.0;
  889. imageStore(output_image, int3(int2(uv), 0), rgba_norm);
  890. }
  891. )"}}};
  892. const DecodingShaderInfo* GetDecodingShaderInfo(TextureFormat format)
  893. {
  894. auto iter = s_decoding_shader_info.find(format);
  895. return iter != s_decoding_shader_info.end() ? &iter->second : nullptr;
  896. }
  897. std::pair<u32, u32> GetDispatchCount(const DecodingShaderInfo* info, u32 width, u32 height)
  898. {
  899. // Flatten to a single dimension?
  900. if (info->group_flatten)
  901. return {(width * height + (info->group_size_x - 1)) / info->group_size_x, 1};
  902. return {(width + (info->group_size_x - 1)) / info->group_size_x,
  903. (height + (info->group_size_y - 1)) / info->group_size_y};
  904. }
  905. std::string GenerateDecodingShader(TextureFormat format, std::optional<TLUTFormat> palette_format,
  906. APIType api_type)
  907. {
  908. const DecodingShaderInfo* info = GetDecodingShaderInfo(format);
  909. if (!info)
  910. return "";
  911. std::ostringstream ss;
  912. if (palette_format.has_value())
  913. {
  914. switch (*palette_format)
  915. {
  916. case TLUTFormat::IA8:
  917. ss << "#define PALETTE_FORMAT_IA8 1\n";
  918. break;
  919. case TLUTFormat::RGB565:
  920. ss << "#define PALETTE_FORMAT_RGB565 1\n";
  921. break;
  922. case TLUTFormat::RGB5A3:
  923. ss << "#define PALETTE_FORMAT_RGB5A3 1\n";
  924. break;
  925. }
  926. }
  927. switch (info->buffer_format)
  928. {
  929. case TEXEL_BUFFER_FORMAT_R8_UINT:
  930. ss << "#define TEXEL_BUFFER_FORMAT_R8 1\n";
  931. break;
  932. case TEXEL_BUFFER_FORMAT_R16_UINT:
  933. ss << "#define TEXEL_BUFFER_FORMAT_R16 1\n";
  934. break;
  935. case TEXEL_BUFFER_FORMAT_RGBA8_UINT:
  936. ss << "#define TEXEL_BUFFER_FORMAT_RGBA8 1\n";
  937. break;
  938. case TEXEL_BUFFER_FORMAT_R32G32_UINT:
  939. ss << "#define TEXEL_BUFFER_FORMAT_R32G32 1\n";
  940. break;
  941. case NUM_TEXEL_BUFFER_FORMATS:
  942. ASSERT(false);
  943. break;
  944. }
  945. ss << decoding_shader_header;
  946. ss << info->shader_body;
  947. return ss.str();
  948. }
  949. std::string GeneratePaletteConversionShader(TLUTFormat palette_format, APIType api_type)
  950. {
  951. std::ostringstream ss;
  952. ss << R"(
  953. int Convert3To8(int v)
  954. {
  955. // Swizzle bits: 00000123 -> 12312312
  956. return (v << 5) | (v << 2) | (v >> 1);
  957. }
  958. int Convert4To8(int v)
  959. {
  960. // Swizzle bits: 00001234 -> 12341234
  961. return (v << 4) | v;
  962. }
  963. int Convert5To8(int v)
  964. {
  965. // Swizzle bits: 00012345 -> 12345123
  966. return (v << 3) | (v >> 2);
  967. }
  968. int Convert6To8(int v)
  969. {
  970. // Swizzle bits: 00123456 -> 12345612
  971. return (v << 2) | (v >> 4);
  972. })";
  973. switch (palette_format)
  974. {
  975. case TLUTFormat::IA8:
  976. ss << R"(
  977. float4 DecodePixel(int val)
  978. {
  979. int i = val & 0xFF;
  980. int a = val >> 8;
  981. return float4(i, i, i, a) / 255.0;
  982. })";
  983. break;
  984. case TLUTFormat::RGB565:
  985. ss << R"(
  986. float4 DecodePixel(int val)
  987. {
  988. int r, g, b, a;
  989. r = Convert5To8((val >> 11) & 0x1f);
  990. g = Convert6To8((val >> 5) & 0x3f);
  991. b = Convert5To8((val) & 0x1f);
  992. a = 0xFF;
  993. return float4(r, g, b, a) / 255.0;
  994. })";
  995. break;
  996. case TLUTFormat::RGB5A3:
  997. ss << R"(
  998. float4 DecodePixel(int val)
  999. {
  1000. int r,g,b,a;
  1001. if ((val&0x8000) > 0)
  1002. {
  1003. r=Convert5To8((val>>10) & 0x1f);
  1004. g=Convert5To8((val>>5 ) & 0x1f);
  1005. b=Convert5To8((val ) & 0x1f);
  1006. a=0xFF;
  1007. }
  1008. else
  1009. {
  1010. a=Convert3To8((val>>12) & 0x7);
  1011. r=Convert4To8((val>>8 ) & 0xf);
  1012. g=Convert4To8((val>>4 ) & 0xf);
  1013. b=Convert4To8((val ) & 0xf);
  1014. }
  1015. return float4(r, g, b, a) / 255.0;
  1016. })";
  1017. break;
  1018. default:
  1019. PanicAlertFmt("Unknown format");
  1020. break;
  1021. }
  1022. ss << "\n";
  1023. if (api_type == APIType::Metal)
  1024. ss << "SSBO_BINDING(0) readonly buffer Palette { uint16_t palette[]; };\n";
  1025. else
  1026. ss << "TEXEL_BUFFER_BINDING(0) uniform usamplerBuffer samp0;\n";
  1027. ss << "SAMPLER_BINDING(1) uniform sampler2DArray samp1;\n";
  1028. ss << "UBO_BINDING(std140, 1) uniform PSBlock {\n";
  1029. ss << " float multiplier;\n";
  1030. ss << " int texel_buffer_offset;\n";
  1031. ss << "};\n";
  1032. if (g_ActiveConfig.backend_info.bSupportsGeometryShaders)
  1033. {
  1034. ss << "VARYING_LOCATION(0) in VertexData {\n";
  1035. ss << " float3 v_tex0;\n";
  1036. ss << "};\n";
  1037. }
  1038. else
  1039. {
  1040. ss << "VARYING_LOCATION(0) in float3 v_tex0;\n";
  1041. }
  1042. ss << "FRAGMENT_OUTPUT_LOCATION(0) out float4 ocol0;\n";
  1043. ss << "void main() {\n";
  1044. ss << " float3 coords = v_tex0;\n";
  1045. ss << " int src = int(round(texture(samp1, coords).r * multiplier));\n";
  1046. if (api_type == APIType::Metal)
  1047. ss << " src = int(palette[uint(src)]);\n";
  1048. else
  1049. ss << " src = int(texelFetch(samp0, src + texel_buffer_offset).r);\n";
  1050. ss << " src = ((src << 8) | (src >> 8)) & 0xFFFF;\n";
  1051. ss << " ocol0 = DecodePixel(src);\n";
  1052. ss << "}\n";
  1053. return ss.str();
  1054. }
  1055. } // namespace TextureConversionShaderTiled