| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/TextureConverterShaderGen.h" |
| 5 | |
| 6 | #include "Common/CommonTypes.h" |
| 7 | #include "VideoCommon/BPMemory.h" |
| 8 | #include "VideoCommon/TextureCacheBase.h" |
| 9 | #include "VideoCommon/VideoCommon.h" |
| 10 | #include "VideoCommon/VideoConfig.h" |
| 11 | |
| 12 | namespace TextureConversionShaderGen |
| 13 | { |
| 14 | TCShaderUid GetShaderUid(EFBCopyFormat dst_format, bool is_depth_copy, bool is_intensity, |
| 15 | bool scale_by_half, float gamma_rcp, |
| 16 | const std::array<u32, 3>& filter_coefficients) |
| 17 | { |
| 18 | TCShaderUid out; |
| 19 | |
| 20 | UidData* const uid_data = out.GetUidData(); |
| 21 | if (g_ActiveConfig.bForceTrueColor) |
| 22 | { |
| 23 | // Increase the precision of EFB copies where it's likely to be safe. |
| 24 | switch (dst_format) |
| 25 | { |
| 26 | case EFBCopyFormat::RGB565: |
| 27 | // HACK: XFB is RGB8. |
| 28 | // Don't blindly do this in other places though, |
| 29 | // the enum value is used to identify XFB copies. |
| 30 | // The important thing here is that we need alpha = 1. |
| 31 | dst_format = EFBCopyFormat::XFB; |
| 32 | break; |
| 33 | case EFBCopyFormat::RGB5A3: |
| 34 | dst_format = EFBCopyFormat::RGBA8; |
| 35 | break; |
| 36 | default: |
| 37 | // Let's not touch the other formats for now, seems risky. |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | uid_data->dst_format = dst_format; |
| 42 | uid_data->efb_has_alpha = bpmem.zcontrol.pixel_format == PixelFormat::RGBA6_Z24; |
| 43 | uid_data->is_depth_copy = is_depth_copy; |
| 44 | uid_data->is_intensity = is_intensity; |
| 45 | uid_data->scale_by_half = scale_by_half; |
| 46 | uid_data->all_copy_filter_coefs_needed = |
| 47 | TextureCacheBase::AllCopyFilterCoefsNeeded(filter_coefficients); |
| 48 | uid_data->copy_filter_can_overflow = TextureCacheBase::CopyFilterCanOverflow(filter_coefficients); |
| 49 | // If the gamma is needed, then include that too. |
| 50 | uid_data->apply_gamma = gamma_rcp != 1.0f; |
| 51 | |
| 52 | return out; |
| 53 | } |
| 54 | |
| 55 | static void WriteHeader(APIType api_type, ShaderCode& out) |
| 56 | { |
| 57 | out.Write("UBO_BINDING(std140, 1) uniform PSBlock {{\n" |
| 58 | " float2 src_offset, src_size;\n" |
| 59 | " uint3 filter_coefficients;\n" |
| 60 | " float gamma_rcp;\n" |
| 61 | " float2 clamp_tb;\n" |
| 62 | " float pixel_height;\n" |
| 63 | "}};\n"); |
| 64 | } |
| 65 | |
| 66 | ShaderCode GenerateVertexShader(APIType api_type) |
| 67 | { |
| 68 | ShaderCode out; |
| 69 | WriteHeader(api_type, out); |
| 70 | |
| 71 | if (g_backend_info.bSupportsGeometryShaders) |
| 72 | { |
| 73 | out.Write("VARYING_LOCATION(0) out VertexData {{\n" |
| 74 | " float3 v_tex0;\n" |
| 75 | "}};\n"); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | out.Write("VARYING_LOCATION(0) out float3 v_tex0;\n"); |
| 80 | } |
| 81 | out.Write("#define id gl_VertexID\n" |
| 82 | "#define opos gl_Position\n" |
| 83 | "void main() {{\n"); |
| 84 | out.Write(" v_tex0 = float3(float((id << 1) & 2), float(id & 2), 0.0f);\n"); |
| 85 | out.Write( |
| 86 | " opos = float4(v_tex0.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);\n"); |
| 87 | out.Write(" v_tex0 = float3(src_offset + (src_size * v_tex0.xy), 0.0f);\n"); |
| 88 | |
| 89 | // NDC space is flipped in Vulkan |
| 90 | if (api_type == APIType::Vulkan) |
| 91 | out.Write(" opos.y = -opos.y;\n"); |
| 92 | |
| 93 | out.Write("}}\n"); |
| 94 | |
| 95 | return out; |
| 96 | } |
| 97 | |
| 98 | ShaderCode GeneratePixelShader(APIType api_type, const UidData* uid_data) |
| 99 | { |
| 100 | const bool mono_depth = uid_data->is_depth_copy && g_ActiveConfig.bStereoEFBMonoDepth; |
| 101 | |
| 102 | ShaderCode out; |
| 103 | WriteHeader(api_type, out); |
| 104 | |
| 105 | out.Write("SAMPLER_BINDING(0) uniform sampler2DArray samp0;\n"); |
| 106 | out.Write("uint4 SampleEFB(float3 uv, float y_offset) {{\n" |
| 107 | " float4 tex_sample = texture(samp0, float3(uv.x, clamp(uv.y + (y_offset * " |
| 108 | "pixel_height), clamp_tb.x, clamp_tb.y), {}));\n", |
| 109 | mono_depth ? "0.0" : "uv.z"); |
| 110 | if (uid_data->is_depth_copy) |
| 111 | { |
| 112 | if (!g_backend_info.bSupportsReversedDepthRange) |
| 113 | out.Write(" tex_sample.x = 1.0 - tex_sample.x;\n"); |
| 114 | |
| 115 | out.Write(" uint depth = uint(tex_sample.x * 16777216.0);\n" |
| 116 | " return uint4((depth >> 16) & 255u, (depth >> 8) & 255u, depth & 255u, 255u);\n" |
| 117 | "}}\n"); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | out.Write(" return uint4(tex_sample * 255.0);\n" |
| 122 | "}}\n"); |
| 123 | } |
| 124 | |
| 125 | if (g_backend_info.bSupportsGeometryShaders) |
| 126 | { |
| 127 | out.Write("VARYING_LOCATION(0) in VertexData {{\n" |
| 128 | " float3 v_tex0;\n" |
| 129 | "}};\n"); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | out.Write("VARYING_LOCATION(0) in vec3 v_tex0;\n"); |
| 134 | } |
| 135 | |
| 136 | out.Write("FRAGMENT_OUTPUT_LOCATION(0) out vec4 ocol0;\n" |
| 137 | "void main()\n{{\n"); |
| 138 | |
| 139 | // The copy filter applies to both color and depth copies. This has been verified on hardware. |
| 140 | // The filter is only applied to the RGB channels, the alpha channel is left intact. |
| 141 | if (uid_data->all_copy_filter_coefs_needed) |
| 142 | { |
| 143 | out.Write(" uint4 prev_row = SampleEFB(v_tex0, -1.0f);\n" |
| 144 | " uint4 current_row = SampleEFB(v_tex0, 0.0f);\n" |
| 145 | " uint4 next_row = SampleEFB(v_tex0, 1.0f);\n" |
| 146 | " uint3 combined_rows = prev_row.rgb * filter_coefficients[0] +\n" |
| 147 | " current_row.rgb * filter_coefficients[1] +\n" |
| 148 | " next_row.rgb * filter_coefficients[2];\n"); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | out.Write(" uint4 current_row = SampleEFB(v_tex0, 0.0f);\n" |
| 153 | " uint3 combined_rows = current_row.rgb * filter_coefficients[1];\n"); |
| 154 | } |
| 155 | out.Write(" // Shift right by 6 to divide by 64, as filter coefficients\n" |
| 156 | " // that sum to 64 result in no change in brightness\n" |
| 157 | " uint4 texcol_raw = uint4(combined_rows.rgb >> 6, {});\n", |
| 158 | uid_data->efb_has_alpha ? "current_row.a" : "255"); |
| 159 | |
| 160 | if (uid_data->copy_filter_can_overflow) |
| 161 | out.Write(" texcol_raw &= 0x1ffu;\n"); |
| 162 | // Note that overflow occurs when the sum of values is >= 128, but this max situation can be hit |
| 163 | // on >= 64, so we always include it. |
| 164 | out.Write(" texcol_raw = min(texcol_raw, uint4(255, 255, 255, 255));\n"); |
| 165 | |
| 166 | if (uid_data->apply_gamma) |
| 167 | { |
| 168 | out.Write(" texcol_raw = uint4(round(pow(abs(float4(texcol_raw) / 255.0),\n" |
| 169 | " float4(gamma_rcp, gamma_rcp, gamma_rcp, 1.0)) * 255.0));\n"); |
| 170 | } |
| 171 | |
| 172 | if (uid_data->is_intensity) |
| 173 | { |
| 174 | out.Write(" // Intensity/YUV format conversion constants determined by hardware testing\n" |
| 175 | " const float4 y_const = float4( 66, 129, 25, 16);\n" |
| 176 | " const float4 u_const = float4(-38, -74, 112, 128);\n" |
| 177 | " const float4 v_const = float4(112, -94, -18, 128);\n" |
| 178 | " // Intensity/YUV format conversion\n" |
| 179 | " texcol_raw.rgb = uint3(dot(y_const, float4(texcol_raw.rgb, 256)),\n" |
| 180 | " dot(u_const, float4(texcol_raw.rgb, 256)),\n" |
| 181 | " dot(v_const, float4(texcol_raw.rgb, 256)));\n" |
| 182 | " // Divide by 256 and round .5 and higher up\n" |
| 183 | " texcol_raw.rgb = (texcol_raw.rgb >> 8) + ((texcol_raw.rgb >> 7) & 1u);\n"); |
| 184 | } |
| 185 | |
| 186 | switch (uid_data->dst_format) |
| 187 | { |
| 188 | case EFBCopyFormat::R4: // R4 |
| 189 | out.Write(" float red = float(texcol_raw.r & 0xF0u) / 240.0;\n" |
| 190 | " ocol0 = float4(red, red, red, red);\n"); |
| 191 | break; |
| 192 | |
| 193 | case EFBCopyFormat::R8_0x1: // R8 |
| 194 | case EFBCopyFormat::R8: // R8 |
| 195 | out.Write(" ocol0 = float4(texcol_raw).rrrr / 255.0;\n"); |
| 196 | break; |
| 197 | |
| 198 | case EFBCopyFormat::RA4: // RA4 |
| 199 | out.Write(" float2 red_alpha = float2(texcol_raw.ra & 0xF0u) / 240.0;\n" |
| 200 | " ocol0 = red_alpha.rrrg;\n"); |
| 201 | break; |
| 202 | |
| 203 | case EFBCopyFormat::RA8: // RA8 |
| 204 | out.Write(" ocol0 = float4(texcol_raw).rrra / 255.0;\n"); |
| 205 | break; |
| 206 | |
| 207 | case EFBCopyFormat::A8: // A8 |
| 208 | out.Write(" ocol0 = float4(texcol_raw).aaaa / 255.0;\n"); |
| 209 | break; |
| 210 | |
| 211 | case EFBCopyFormat::G8: // G8 |
| 212 | out.Write(" ocol0 = float4(texcol_raw).gggg / 255.0;\n"); |
| 213 | break; |
| 214 | |
| 215 | case EFBCopyFormat::B8: // B8 |
| 216 | out.Write(" ocol0 = float4(texcol_raw).bbbb / 255.0;\n"); |
| 217 | break; |
| 218 | |
| 219 | case EFBCopyFormat::RG8: // RG8 |
| 220 | out.Write(" ocol0 = float4(texcol_raw).rrrg / 255.0;\n"); |
| 221 | break; |
| 222 | |
| 223 | case EFBCopyFormat::GB8: // GB8 |
| 224 | out.Write(" ocol0 = float4(texcol_raw).gggb / 255.0;\n"); |
| 225 | break; |
| 226 | |
| 227 | case EFBCopyFormat::RGB565: // RGB565 |
| 228 | out.Write(" float2 red_blue = float2(texcol_raw.rb & 0xF8u) / 248.0;\n" |
| 229 | " float green = float(texcol_raw.g & 0xFCu) / 252.0;\n" |
| 230 | " ocol0 = float4(red_blue.r, green, red_blue.g, 1.0);\n"); |
| 231 | break; |
| 232 | |
| 233 | case EFBCopyFormat::RGB5A3: // RGB5A3 |
| 234 | // TODO: The MSB controls whether we have RGB5 or RGB4A3, this selection |
| 235 | // will need to be implemented once we move away from floats. |
| 236 | out.Write(" float3 color = float3(texcol_raw.rgb & 0xF8u) / 248.0;\n" |
| 237 | " float alpha = float(texcol_raw.a & 0xE0u) / 224.0;\n" |
| 238 | " ocol0 = float4(color, alpha);\n"); |
| 239 | break; |
| 240 | |
| 241 | case EFBCopyFormat::RGBA8: // RGBA8 |
| 242 | out.Write(" ocol0 = float4(texcol_raw.rgba) / 255.0;\n"); |
| 243 | break; |
| 244 | |
| 245 | case EFBCopyFormat::XFB: |
| 246 | out.Write(" ocol0 = float4(float3(texcol_raw.rgb) / 255.0, 1.0);\n"); |
| 247 | break; |
| 248 | |
| 249 | default: |
| 250 | ERROR_LOG_FMT(VIDEO, "Unknown copy/intensity color format: {} {}", uid_data->dst_format, |
| 251 | uid_data->is_intensity); |
| 252 | out.Write(" ocol0 = float4(texcol_raw.rgba) / 255.0;\n"); |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | out.Write("}}\n"); |
| 257 | |
| 258 | return out; |
| 259 | } |
| 260 | |
| 261 | } // namespace TextureConversionShaderGen |