Debian Codegraph @

Source/Core/VideoCommon/TextureConversionShader.cpp

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