| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/FramebufferShaderGen.h" |
| 5 | |
| 6 | #include <string_view> |
| 7 | |
| 8 | #include "Common/Logging/Log.h" |
| 9 | |
| 10 | #include "VideoCommon/FramebufferManager.h" |
| 11 | #include "VideoCommon/ShaderGenCommon.h" |
| 12 | #include "VideoCommon/TextureDecoder.h" |
| 13 | #include "VideoCommon/VertexShaderGen.h" |
| 14 | #include "VideoCommon/VideoCommon.h" |
| 15 | #include "VideoCommon/VideoConfig.h" |
| 16 | |
| 17 | namespace FramebufferShaderGen |
| 18 | { |
| 19 | namespace |
| 20 | { |
| 21 | APIType GetAPIType() |
| 22 | { |
| 23 | return g_backend_info.api_type; |
| 24 | } |
| 25 | |
| 26 | void EmitUniformBufferDeclaration(ShaderCode& code) |
| 27 | { |
| 28 | code.Write("UBO_BINDING(std140, 1) uniform PSBlock\n"); |
| 29 | } |
| 30 | |
| 31 | void EmitSamplerDeclarations(ShaderCode& code, u32 start = 0, u32 end = 1, |
| 32 | bool multisampled = false) |
| 33 | { |
| 34 | switch (GetAPIType()) |
| 35 | { |
| 36 | case APIType::D3D: |
| 37 | case APIType::Metal: |
| 38 | case APIType::OpenGL: |
| 39 | case APIType::Vulkan: |
| 40 | { |
| 41 | const char* array_type = multisampled ? "sampler2DMSArray" : "sampler2DArray"; |
| 42 | |
| 43 | for (u32 i = start; i < end; i++) |
| 44 | { |
| 45 | code.Write("SAMPLER_BINDING({}) uniform {} samp{};\n", i, array_type, i); |
| 46 | } |
| 47 | } |
| 48 | break; |
| 49 | default: |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void EmitSampleTexture(ShaderCode& code, u32 n, std::string_view coords) |
| 55 | { |
| 56 | switch (GetAPIType()) |
| 57 | { |
| 58 | case APIType::D3D: |
| 59 | case APIType::Metal: |
| 60 | case APIType::OpenGL: |
| 61 | case APIType::Vulkan: |
| 62 | code.Write("texture(samp{}, {})", n, coords); |
| 63 | break; |
| 64 | |
| 65 | default: |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Emits a texel fetch/load instruction. Assumes that "coords" is a 4-element vector, with z |
| 71 | // containing the layer, and w containing the mipmap level. |
| 72 | void EmitTextureLoad(ShaderCode& code, u32 n, std::string_view coords) |
| 73 | { |
| 74 | switch (GetAPIType()) |
| 75 | { |
| 76 | case APIType::D3D: |
| 77 | case APIType::Metal: |
| 78 | case APIType::OpenGL: |
| 79 | case APIType::Vulkan: |
| 80 | code.Write("texelFetch(samp{}, ({}).xyz, ({}).w)", n, coords, coords); |
| 81 | break; |
| 82 | |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void EmitVertexMainDeclaration(ShaderCode& code, u32 num_tex_inputs, u32 num_color_inputs, |
| 89 | bool position_input, u32 num_tex_outputs, u32 num_color_outputs, |
| 90 | std::string_view extra_inputs = {}) |
| 91 | { |
| 92 | switch (GetAPIType()) |
| 93 | { |
| 94 | case APIType::D3D: |
| 95 | case APIType::Metal: |
| 96 | case APIType::OpenGL: |
| 97 | case APIType::Vulkan: |
| 98 | { |
| 99 | for (u32 i = 0; i < num_tex_inputs; i++) |
| 100 | { |
| 101 | const auto attribute = ShaderAttrib::TexCoord0 + i; |
| 102 | code.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawtex{};\n", attribute, i); |
| 103 | } |
| 104 | for (u32 i = 0; i < num_color_inputs; i++) |
| 105 | { |
| 106 | const auto attribute = ShaderAttrib::Color0 + i; |
| 107 | code.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawcolor{};\n", attribute, i); |
| 108 | } |
| 109 | if (position_input) |
| 110 | code.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawpos;\n", ShaderAttrib::Position); |
| 111 | |
| 112 | if (g_backend_info.bSupportsGeometryShaders) |
| 113 | { |
| 114 | code.Write("VARYING_LOCATION(0) out VertexData {{\n"); |
| 115 | for (u32 i = 0; i < num_tex_outputs; i++) |
| 116 | code.Write(" float3 v_tex{};\n", i); |
| 117 | for (u32 i = 0; i < num_color_outputs; i++) |
| 118 | code.Write(" float4 v_col{};\n", i); |
| 119 | code.Write("}};\n"); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | for (u32 i = 0; i < num_tex_outputs; i++) |
| 124 | code.Write("VARYING_LOCATION({}) out float3 v_tex{};\n", i, i); |
| 125 | for (u32 i = 0; i < num_color_outputs; i++) |
| 126 | code.Write("VARYING_LOCATION({}) out float4 v_col{};\n", num_tex_inputs + i, i); |
| 127 | } |
| 128 | code.Write("#define opos gl_Position\n"); |
| 129 | code.Write("{}\n", extra_inputs); |
| 130 | code.Write("void main()\n"); |
| 131 | } |
| 132 | break; |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void EmitPixelMainDeclaration(ShaderCode& code, u32 num_tex_inputs, u32 num_color_inputs, |
| 139 | std::string_view output_type = "float4", |
| 140 | std::string_view extra_vars = {}, bool emit_frag_coord = false) |
| 141 | { |
| 142 | switch (GetAPIType()) |
| 143 | { |
| 144 | case APIType::D3D: |
| 145 | case APIType::Metal: |
| 146 | case APIType::OpenGL: |
| 147 | case APIType::Vulkan: |
| 148 | { |
| 149 | if (g_backend_info.bSupportsGeometryShaders) |
| 150 | { |
| 151 | code.Write("VARYING_LOCATION(0) in VertexData {{\n"); |
| 152 | for (u32 i = 0; i < num_tex_inputs; i++) |
| 153 | code.Write(" float3 v_tex{};\n", i); |
| 154 | for (u32 i = 0; i < num_color_inputs; i++) |
| 155 | code.Write(" float4 v_col{};\n", i); |
| 156 | code.Write("}};\n"); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | for (u32 i = 0; i < num_tex_inputs; i++) |
| 161 | code.Write("VARYING_LOCATION({}) in float3 v_tex{};\n", i, i); |
| 162 | for (u32 i = 0; i < num_color_inputs; i++) |
| 163 | code.Write("VARYING_LOCATION({}) in float4 v_col{};\n", num_tex_inputs + i, i); |
| 164 | } |
| 165 | |
| 166 | code.Write("FRAGMENT_OUTPUT_LOCATION(0) out {} ocol0;\n", output_type); |
| 167 | code.Write("{}\n", extra_vars); |
| 168 | if (emit_frag_coord) |
| 169 | code.Write("#define frag_coord gl_FragCoord\n"); |
| 170 | code.Write("void main()\n"); |
| 171 | } |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | } // Anonymous namespace |
| 179 | |
| 180 | std::string GenerateScreenQuadVertexShader() |
| 181 | { |
| 182 | ShaderCode code; |
| 183 | EmitVertexMainDeclaration(code, 0, 0, false, 1, 0, |
| 184 | |
| 185 | "#define id gl_VertexID\n"); |
| 186 | code.Write( |
| 187 | "{{\n" |
| 188 | " v_tex0 = float3(float((id << 1) & 2), float(id & 2), 0.0f);\n" |
| 189 | " opos = float4(v_tex0.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);\n"); |
| 190 | |
| 191 | // NDC space is flipped in Vulkan. We also flip in GL so that (0,0) is in the lower-left. |
| 192 | if (GetAPIType() == APIType::Vulkan || GetAPIType() == APIType::OpenGL) |
| 193 | code.Write(" opos.y = -opos.y;\n"); |
| 194 | |
| 195 | code.Write("}}\n"); |
| 196 | |
| 197 | return code.GetBuffer(); |
| 198 | } |
| 199 | |
| 200 | std::string GeneratePassthroughGeometryShader(u32 num_tex, u32 num_colors) |
| 201 | { |
| 202 | ShaderCode code; |
| 203 | if (GetAPIType() == APIType::D3D) |
| 204 | { |
| 205 | code.Write("struct VS_OUTPUT\n" |
| 206 | "{{\n"); |
| 207 | for (u32 i = 0; i < num_tex; i++) |
| 208 | code.Write(" float3 tex{} : TEXCOORD{};\n", i, i); |
| 209 | for (u32 i = 0; i < num_colors; i++) |
| 210 | code.Write(" float4 color{} : TEXCOORD{};\n", i, i + num_tex); |
| 211 | code.Write(" float4 position : SV_Position;\n" |
| 212 | "}};\n"); |
| 213 | |
| 214 | code.Write("struct GS_OUTPUT\n" |
| 215 | "{{"); |
| 216 | for (u32 i = 0; i < num_tex; i++) |
| 217 | code.Write(" float3 tex{} : TEXCOORD{};\n", i, i); |
| 218 | for (u32 i = 0; i < num_colors; i++) |
| 219 | code.Write(" float4 color{} : TEXCOORD{};\n", i, i + num_tex); |
| 220 | code.Write(" float4 position : SV_Position;\n" |
| 221 | " uint slice : SV_RenderTargetArrayIndex;\n" |
| 222 | "}};\n\n"); |
| 223 | |
| 224 | code.Write("[maxvertexcount(6)]\n" |
| 225 | "void main(triangle VS_OUTPUT vso[3], inout TriangleStream<GS_OUTPUT> output)\n" |
| 226 | "{{\n" |
| 227 | " for (uint slice = 0; slice < 2u; slice++)\n" |
| 228 | " {{\n" |
| 229 | " for (int i = 0; i < 3; i++)\n" |
| 230 | " {{\n" |
| 231 | " GS_OUTPUT gso;\n" |
| 232 | " gso.position = vso[i].position;\n"); |
| 233 | for (u32 i = 0; i < num_tex; i++) |
| 234 | code.Write(" gso.tex{} = float3(vso[i].tex{}.xy, float(slice));\n", i, i); |
| 235 | for (u32 i = 0; i < num_colors; i++) |
| 236 | code.Write(" gso.color{} = vso[i].color{};\n", i, i); |
| 237 | code.Write(" gso.slice = slice;\n" |
| 238 | " output.Append(gso);\n" |
| 239 | " }}\n" |
| 240 | " output.RestartStrip();\n" |
| 241 | " }}\n" |
| 242 | "}}\n"); |
| 243 | } |
| 244 | else if (GetAPIType() == APIType::OpenGL || GetAPIType() == APIType::Vulkan) |
| 245 | { |
| 246 | code.Write("layout(triangles) in;\n" |
| 247 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 248 | |
| 249 | if (num_tex > 0 || num_colors > 0) |
| 250 | { |
| 251 | code.Write("VARYING_LOCATION(0) in VertexData {{\n"); |
| 252 | for (u32 i = 0; i < num_tex; i++) |
| 253 | code.Write(" float3 v_tex{};\n", i); |
| 254 | for (u32 i = 0; i < num_colors; i++) |
| 255 | code.Write(" float4 v_col{};\n", i); |
| 256 | code.Write("}} v_in[];\n"); |
| 257 | |
| 258 | code.Write("VARYING_LOCATION(0) out VertexData {{\n"); |
| 259 | for (u32 i = 0; i < num_tex; i++) |
| 260 | code.Write(" float3 v_tex{};\n", i); |
| 261 | for (u32 i = 0; i < num_colors; i++) |
| 262 | code.Write(" float4 v_col{};\n", i); |
| 263 | code.Write("}} v_out;\n"); |
| 264 | } |
| 265 | code.Write("\n" |
| 266 | "void main()\n" |
| 267 | "{{\n" |
| 268 | " for (int j = 0; j < 2; j++)\n" |
| 269 | " {{\n" |
| 270 | " gl_Layer = j;\n"); |
| 271 | |
| 272 | // We have to explicitly unroll this loop otherwise the GL compiler gets cranky. |
| 273 | for (u32 v = 0; v < 3; v++) |
| 274 | { |
| 275 | code.Write(" gl_Position = gl_in[{}].gl_Position;\n", v); |
| 276 | for (u32 i = 0; i < num_tex; i++) |
| 277 | { |
| 278 | code.Write(" v_out.v_tex{} = float3(v_in[{}].v_tex{}.xy, float(j));\n", i, v, i); |
| 279 | } |
| 280 | for (u32 i = 0; i < num_colors; i++) |
| 281 | code.Write(" v_out.v_col{} = v_in[{}].v_col{};\n", i, v, i); |
| 282 | code.Write(" EmitVertex();\n\n"); |
| 283 | } |
| 284 | code.Write(" EndPrimitive();\n" |
| 285 | " }}\n" |
| 286 | "}}\n"); |
| 287 | } |
| 288 | |
| 289 | return code.GetBuffer(); |
| 290 | } |
| 291 | |
| 292 | std::string GenerateTextureCopyVertexShader() |
| 293 | { |
| 294 | ShaderCode code; |
| 295 | EmitUniformBufferDeclaration(code); |
| 296 | code.Write("{{" |
| 297 | " float2 src_offset;\n" |
| 298 | " float2 src_size;\n" |
| 299 | "}};\n\n"); |
| 300 | |
| 301 | EmitVertexMainDeclaration(code, 0, 0, false, 1, 0, |
| 302 | |
| 303 | "#define id gl_VertexID"); |
| 304 | code.Write("{{\n" |
| 305 | " v_tex0 = float3(float((id << 1) & 2), float(id & 2), 0.0f);\n" |
| 306 | " opos = float4(v_tex0.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);\n" |
| 307 | " v_tex0 = float3(src_offset + (src_size * v_tex0.xy), 0.0f);\n"); |
| 308 | |
| 309 | // NDC space is flipped in Vulkan. We also flip in GL so that (0,0) is in the lower-left. |
| 310 | if (GetAPIType() == APIType::Vulkan || GetAPIType() == APIType::OpenGL) |
| 311 | code.Write(" opos.y = -opos.y;\n"); |
| 312 | |
| 313 | code.Write("}}\n"); |
| 314 | |
| 315 | return code.GetBuffer(); |
| 316 | } |
| 317 | |
| 318 | std::string GenerateTextureCopyPixelShader() |
| 319 | { |
| 320 | ShaderCode code; |
| 321 | EmitSamplerDeclarations(code, 0, 1, false); |
| 322 | EmitPixelMainDeclaration(code, 1, 0); |
| 323 | code.Write("{{\n" |
| 324 | " ocol0 = "); |
| 325 | EmitSampleTexture(code, 0, "v_tex0"); |
| 326 | code.Write(";\n" |
| 327 | "}}\n"); |
| 328 | return code.GetBuffer(); |
| 329 | } |
| 330 | |
| 331 | std::string GenerateColorPixelShader() |
| 332 | { |
| 333 | ShaderCode code; |
| 334 | EmitPixelMainDeclaration(code, 0, 1); |
| 335 | code.Write("{{\n" |
| 336 | " ocol0 = v_col0;\n" |
| 337 | "}}\n"); |
| 338 | return code.GetBuffer(); |
| 339 | } |
| 340 | |
| 341 | std::string GenerateResolveColorPixelShader(u32 samples) |
| 342 | { |
| 343 | ShaderCode code; |
| 344 | EmitSamplerDeclarations(code, 0, 1, true); |
| 345 | EmitPixelMainDeclaration(code, 1, 0); |
| 346 | code.Write("{{\n" |
| 347 | " int layer = int(v_tex0.z);\n" |
| 348 | " int3 coords = int3(int2(gl_FragCoord.xy), layer);\n" |
| 349 | " ocol0 = float4(0.0f);\n"); |
| 350 | code.Write(" for (int i = 0; i < {}; i++)\n", samples); |
| 351 | code.Write(" ocol0 += texelFetch(samp0, coords, i);\n"); |
| 352 | code.Write(" ocol0 /= {}.0f;\n", samples); |
| 353 | code.Write("}}\n"); |
| 354 | return code.GetBuffer(); |
| 355 | } |
| 356 | |
| 357 | std::string GenerateResolveDepthPixelShader(u32 samples) |
| 358 | { |
| 359 | ShaderCode code; |
| 360 | EmitSamplerDeclarations(code, 0, 1, true); |
| 361 | EmitPixelMainDeclaration(code, 1, 0, "float", ""); |
| 362 | code.Write("{{\n" |
| 363 | " int layer = int(v_tex0.z);\n"); |
| 364 | code.Write(" int3 coords = int3(int2(gl_FragCoord.xy), layer);\n"); |
| 365 | |
| 366 | // Take the minimum of all depth samples. |
| 367 | code.Write(" ocol0 = texelFetch(samp0, coords, 0).r;\n"); |
| 368 | code.Write(" for (int i = 1; i < {}; i++)\n", samples); |
| 369 | code.Write(" ocol0 = min(ocol0, texelFetch(samp0, coords, i).r);\n"); |
| 370 | |
| 371 | code.Write("}}\n"); |
| 372 | return code.GetBuffer(); |
| 373 | } |
| 374 | |
| 375 | std::string GenerateClearVertexShader() |
| 376 | { |
| 377 | ShaderCode code; |
| 378 | EmitUniformBufferDeclaration(code); |
| 379 | code.Write("{{\n" |
| 380 | " float4 clear_color;\n" |
| 381 | " float clear_depth;\n" |
| 382 | "}};\n"); |
| 383 | |
| 384 | EmitVertexMainDeclaration(code, 0, 0, false, 0, 1, |
| 385 | |
| 386 | "#define id gl_VertexID\n"); |
| 387 | code.Write( |
| 388 | "{{\n" |
| 389 | " float2 coord = float2(float((id << 1) & 2), float(id & 2));\n" |
| 390 | " opos = float4(coord * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), clear_depth, 1.0f);\n" |
| 391 | " v_col0 = clear_color;\n"); |
| 392 | |
| 393 | // NDC space is flipped in Vulkan |
| 394 | if (GetAPIType() == APIType::Vulkan) |
| 395 | code.Write(" opos.y = -opos.y;\n"); |
| 396 | |
| 397 | code.Write("}}\n"); |
| 398 | |
| 399 | return code.GetBuffer(); |
| 400 | } |
| 401 | |
| 402 | std::string GenerateEFBPokeVertexShader() |
| 403 | { |
| 404 | ShaderCode code; |
| 405 | EmitVertexMainDeclaration(code, 0, 1, true, 0, 1); |
| 406 | code.Write("{{\n" |
| 407 | " v_col0 = rawcolor0;\n" |
| 408 | " opos = float4(rawpos.xyz, 1.0f);\n"); |
| 409 | if (g_backend_info.bSupportsLargePoints) |
| 410 | code.Write(" gl_PointSize = rawpos.w;\n"); |
| 411 | |
| 412 | // NDC space is flipped in Vulkan. |
| 413 | if (GetAPIType() == APIType::Vulkan) |
| 414 | code.Write(" opos.y = -opos.y;\n"); |
| 415 | |
| 416 | code.Write("}}\n"); |
| 417 | return code.GetBuffer(); |
| 418 | } |
| 419 | |
| 420 | std::string GenerateFormatConversionShader(EFBReinterpretType convtype, u32 samples) |
| 421 | { |
| 422 | ShaderCode code; |
| 423 | EmitSamplerDeclarations(code, 0, 1, samples > 1); |
| 424 | EmitPixelMainDeclaration(code, 1, 0, "float4", |
| 425 | |
| 426 | ""); |
| 427 | code.Write("{{\n" |
| 428 | " int layer = int(v_tex0.z);\n"); |
| 429 | code.Write(" int3 coords = int3(int2(gl_FragCoord.xy), layer);\n"); |
| 430 | |
| 431 | if (samples == 1) |
| 432 | { |
| 433 | // No MSAA at all. |
| 434 | code.Write(" float4 val = texelFetch(samp0, coords, 0);\n"); |
| 435 | } |
| 436 | else if (g_ActiveConfig.bSSAA) |
| 437 | { |
| 438 | // Sample shading, shader runs once per sample |
| 439 | code.Write(" float4 val = texelFetch(samp0, coords, gl_SampleID);"); |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | // MSAA without sample shading, average out all samples. |
| 444 | code.Write(" float4 val = float4(0.0f, 0.0f, 0.0f, 0.0f);\n"); |
| 445 | code.Write(" for (int i = 0; i < {}; i++)\n", samples); |
| 446 | code.Write(" val += texelFetch(samp0, coords, i);\n"); |
| 447 | code.Write(" val /= float({});\n", samples); |
| 448 | } |
| 449 | |
| 450 | switch (convtype) |
| 451 | { |
| 452 | case EFBReinterpretType::RGB8ToRGBA6: |
| 453 | code.Write(" int4 src8 = int4(round(val * 255.f));\n" |
| 454 | " int4 dst6;\n" |
| 455 | " dst6.r = src8.r >> 2;\n" |
| 456 | " dst6.g = ((src8.r & 0x3) << 4) | (src8.g >> 4);\n" |
| 457 | " dst6.b = ((src8.g & 0xF) << 2) | (src8.b >> 6);\n" |
| 458 | " dst6.a = src8.b & 0x3F;\n" |
| 459 | " ocol0 = float4(dst6) / 63.f;\n"); |
| 460 | break; |
| 461 | |
| 462 | case EFBReinterpretType::RGB8ToRGB565: |
| 463 | code.Write(" ocol0 = val;\n"); |
| 464 | break; |
| 465 | |
| 466 | case EFBReinterpretType::RGBA6ToRGB8: |
| 467 | code.Write(" int4 src6 = int4(round(val * 63.f));\n" |
| 468 | " int4 dst8;\n" |
| 469 | " dst8.r = (src6.r << 2) | (src6.g >> 4);\n" |
| 470 | " dst8.g = ((src6.g & 0xF) << 4) | (src6.b >> 2);\n" |
| 471 | " dst8.b = ((src6.b & 0x3) << 6) | src6.a;\n" |
| 472 | " dst8.a = 255;\n" |
| 473 | " ocol0 = float4(dst8) / 255.f;\n"); |
| 474 | break; |
| 475 | |
| 476 | case EFBReinterpretType::RGBA6ToRGB565: |
| 477 | code.Write(" ocol0 = val;\n"); |
| 478 | break; |
| 479 | |
| 480 | case EFBReinterpretType::RGB565ToRGB8: |
| 481 | code.Write(" ocol0 = val;\n"); |
| 482 | break; |
| 483 | |
| 484 | case EFBReinterpretType::RGB565ToRGBA6: |
| 485 | // |
| 486 | code.Write(" ocol0 = val;\n"); |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | code.Write("}}\n"); |
| 491 | return code.GetBuffer(); |
| 492 | } |
| 493 | |
| 494 | std::string GenerateTextureReinterpretShader(TextureFormat from_format, TextureFormat to_format) |
| 495 | { |
| 496 | ShaderCode code; |
| 497 | EmitSamplerDeclarations(code, 0, 1, false); |
| 498 | EmitPixelMainDeclaration(code, 1, 0, "float4", "", true); |
| 499 | code.Write("{{\n" |
| 500 | " int layer = int(v_tex0.z);\n" |
| 501 | " int4 coords = int4(int2(frag_coord.xy), layer, 0);\n"); |
| 502 | |
| 503 | // Convert to a 32-bit value encompassing all channels, filling the most significant bits with |
| 504 | // zeroes. |
| 505 | code.Write(" uint raw_value;\n"); |
| 506 | switch (from_format) |
| 507 | { |
| 508 | case TextureFormat::I8: |
| 509 | case TextureFormat::C8: |
| 510 | { |
| 511 | code.Write(" float4 temp_value = "); |
| 512 | EmitTextureLoad(code, 0, "coords"); |
| 513 | code.Write(";\n" |
| 514 | " raw_value = uint(temp_value.r * 255.0);\n"); |
| 515 | } |
| 516 | break; |
| 517 | |
| 518 | case TextureFormat::IA8: |
| 519 | { |
| 520 | code.Write(" float4 temp_value = "); |
| 521 | EmitTextureLoad(code, 0, "coords"); |
| 522 | code.Write(";\n" |
| 523 | " raw_value = uint(temp_value.r * 255.0) | (uint(temp_value.a * 255.0) << 8);\n"); |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case TextureFormat::I4: |
| 528 | { |
| 529 | code.Write(" float4 temp_value = "); |
| 530 | EmitTextureLoad(code, 0, "coords"); |
| 531 | code.Write(";\n" |
| 532 | " raw_value = uint(temp_value.r * 15.0);\n"); |
| 533 | } |
| 534 | break; |
| 535 | |
| 536 | case TextureFormat::IA4: |
| 537 | { |
| 538 | code.Write(" float4 temp_value = "); |
| 539 | EmitTextureLoad(code, 0, "coords"); |
| 540 | code.Write(";\n" |
| 541 | " raw_value = uint(temp_value.r * 15.0) | (uint(temp_value.a * 15.0) << 4);\n"); |
| 542 | } |
| 543 | break; |
| 544 | |
| 545 | case TextureFormat::RGB565: |
| 546 | { |
| 547 | code.Write(" float4 temp_value = "); |
| 548 | EmitTextureLoad(code, 0, "coords"); |
| 549 | code.Write(";\n" |
| 550 | " raw_value = uint(temp_value.b * 31.0) | (uint(temp_value.g * 63.0) << 5) |\n" |
| 551 | " (uint(temp_value.r * 31.0) << 11);\n"); |
| 552 | } |
| 553 | break; |
| 554 | |
| 555 | case TextureFormat::RGB5A3: |
| 556 | { |
| 557 | code.Write(" float4 temp_value = "); |
| 558 | EmitTextureLoad(code, 0, "coords"); |
| 559 | code.Write(";\n"); |
| 560 | |
| 561 | // 0.8784 = 224 / 255 which is the maximum alpha value that can be represented in 3 bits |
| 562 | code.Write( |
| 563 | " if (temp_value.a > 0.878f) {{\n" |
| 564 | " raw_value = (uint(temp_value.b * 31.0)) | (uint(temp_value.g * 31.0) << 5) |\n" |
| 565 | " (uint(temp_value.r * 31.0) << 10) | 0x8000u;\n" |
| 566 | " }} else {{\n" |
| 567 | " raw_value = (uint(temp_value.b * 15.0)) | (uint(temp_value.g * 15.0) << 4) |\n" |
| 568 | " (uint(temp_value.r * 15.0) << 8) | (uint(temp_value.a * 7.0) << 12);\n" |
| 569 | " }}\n"); |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | default: |
| 574 | WARN_LOG_FMT(VIDEO, "From format {} is not supported", from_format); |
| 575 | return "{}\n"; |
| 576 | } |
| 577 | |
| 578 | // Now convert it to its new representation. |
| 579 | switch (to_format) |
| 580 | { |
| 581 | case TextureFormat::I8: |
| 582 | case TextureFormat::C8: |
| 583 | { |
| 584 | code.Write(" float orgba = float(raw_value & 0xFFu) / 255.0;\n" |
| 585 | " ocol0 = float4(orgba, orgba, orgba, orgba);\n"); |
| 586 | } |
| 587 | break; |
| 588 | |
| 589 | case TextureFormat::IA8: |
| 590 | { |
| 591 | code.Write(" float orgb = float(raw_value & 0xFFu) / 255.0;\n" |
| 592 | " ocol0 = float4(orgb, orgb, orgb, float((raw_value >> 8) & 0xFFu) / 255.0);\n"); |
| 593 | } |
| 594 | break; |
| 595 | |
| 596 | case TextureFormat::IA4: |
| 597 | { |
| 598 | code.Write(" float orgb = float(raw_value & 0xFu) / 15.0;\n" |
| 599 | " ocol0 = float4(orgb, orgb, orgb, float((raw_value >> 4) & 0xFu) / 15.0);\n"); |
| 600 | } |
| 601 | break; |
| 602 | |
| 603 | case TextureFormat::RGB565: |
| 604 | { |
| 605 | code.Write(" ocol0 = float4(float((raw_value >> 10) & 0x1Fu) / 31.0,\n" |
| 606 | " float((raw_value >> 5) & 0x1Fu) / 31.0,\n" |
| 607 | " float(raw_value & 0x1Fu) / 31.0, 1.0);\n"); |
| 608 | } |
| 609 | break; |
| 610 | |
| 611 | case TextureFormat::RGB5A3: |
| 612 | { |
| 613 | code.Write(" if ((raw_value & 0x8000u) != 0u) {{\n" |
| 614 | " ocol0 = float4(float((raw_value >> 10) & 0x1Fu) / 31.0,\n" |
| 615 | " float((raw_value >> 5) & 0x1Fu) / 31.0,\n" |
| 616 | " float(raw_value & 0x1Fu) / 31.0, 1.0);\n" |
| 617 | " }} else {{\n" |
| 618 | " ocol0 = float4(float((raw_value >> 8) & 0x0Fu) / 15.0,\n" |
| 619 | " float((raw_value >> 4) & 0x0Fu) / 15.0,\n" |
| 620 | " float(raw_value & 0x0Fu) / 15.0,\n" |
| 621 | " float((raw_value >> 12) & 0x07u) / 7.0);\n" |
| 622 | " }}\n"); |
| 623 | } |
| 624 | break; |
| 625 | default: |
| 626 | WARN_LOG_FMT(VIDEO, "To format {} is not supported", to_format); |
| 627 | return "{}\n"; |
| 628 | } |
| 629 | |
| 630 | code.Write("}}\n"); |
| 631 | return code.GetBuffer(); |
| 632 | } |
| 633 | |
| 634 | std::string GenerateEFBRestorePixelShader() |
| 635 | { |
| 636 | ShaderCode code; |
| 637 | EmitSamplerDeclarations(code, 0, 2, false); |
| 638 | EmitPixelMainDeclaration(code, 1, 0, "float4", ""); |
| 639 | code.Write("{{\n" |
| 640 | " ocol0 = "); |
| 641 | EmitSampleTexture(code, 0, "v_tex0"); |
| 642 | code.Write(";\n"); |
| 643 | code.Write(" gl_FragDepth = "); |
| 644 | EmitSampleTexture(code, 1, "v_tex0"); |
| 645 | code.Write(".r;\n" |
| 646 | "}}\n"); |
| 647 | return code.GetBuffer(); |
| 648 | } |
| 649 | |
| 650 | std::string GenerateImGuiVertexShader() |
| 651 | { |
| 652 | ShaderCode code; |
| 653 | |
| 654 | // Uniform buffer contains the viewport size, and we transform in the vertex shader. |
| 655 | EmitUniformBufferDeclaration(code); |
| 656 | code.Write("{{\n" |
| 657 | "float2 u_rcp_viewport_size_mul2;\n" |
| 658 | "}};\n\n"); |
| 659 | |
| 660 | EmitVertexMainDeclaration(code, 1, 1, true, 1, 1); |
| 661 | code.Write("{{\n" |
| 662 | " v_tex0 = float3(rawtex0.xy, 0.0);\n" |
| 663 | " v_col0 = rawcolor0;\n" |
| 664 | " opos = float4(rawpos.x * u_rcp_viewport_size_mul2.x - 1.0," |
| 665 | " 1.0 - rawpos.y * u_rcp_viewport_size_mul2.y, 0.0, 1.0);\n"); |
| 666 | |
| 667 | // NDC space is flipped in Vulkan. |
| 668 | if (GetAPIType() == APIType::Vulkan) |
| 669 | code.Write(" opos.y = -opos.y;\n"); |
| 670 | |
| 671 | code.Write("}}\n"); |
| 672 | return code.GetBuffer(); |
| 673 | } |
| 674 | |
| 675 | std::string GenerateImGuiPixelShader(bool linear_space_output) |
| 676 | { |
| 677 | ShaderCode code; |
| 678 | EmitSamplerDeclarations(code, 0, 1, false); |
| 679 | EmitPixelMainDeclaration(code, 1, 1); |
| 680 | code.Write("{{\n" |
| 681 | " ocol0 = "); |
| 682 | EmitSampleTexture(code, 0, "float3(v_tex0.xy, 0.0)"); |
| 683 | // We approximate to gamma 2.2 instead of sRGB as it barely matters for this case. |
| 684 | // Note that if HDR is enabled, ideally we should multiply by |
| 685 | // the paper white brightness for readability. |
| 686 | if (linear_space_output) |
| 687 | code.Write(" * pow(v_col0, float4(2.2f, 2.2f, 2.2f, 1.0f));\n}}\n"); |
| 688 | else |
| 689 | code.Write(" * v_col0;\n}}\n"); |
| 690 | |
| 691 | return code.GetBuffer(); |
| 692 | } |
| 693 | |
| 694 | } // namespace FramebufferShaderGen |