| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/RenderState.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | |
| 9 | #include "VideoCommon/BPMemory.h" |
| 10 | #include "VideoCommon/TextureConfig.h" |
| 11 | |
| 12 | void RasterizationState::Generate(const BPMemory& bp, PrimitiveType primitive_type) |
| 13 | { |
| 14 | cull_mode = bp.genMode.cull_mode; |
| 15 | primitive = primitive_type; |
| 16 | |
| 17 | // Back-face culling should be disabled for points/lines. |
| 18 | if (primitive_type != PrimitiveType::Triangles && primitive_type != PrimitiveType::TriangleStrip) |
| 19 | cull_mode = CullMode::None; |
| 20 | } |
| 21 | |
| 22 | void DepthState::Generate(const BPMemory& bp) |
| 23 | { |
| 24 | test_enable = bp.zmode.test_enable.Value(); |
| 25 | update_enable = bp.zmode.update_enable.Value(); |
| 26 | func = bp.zmode.func.Value(); |
| 27 | } |
| 28 | |
| 29 | static bool IsDualSrc(SrcBlendFactor factor) |
| 30 | { |
| 31 | return factor == SrcBlendFactor::SrcAlpha || factor == SrcBlendFactor::InvSrcAlpha; |
| 32 | } |
| 33 | |
| 34 | static bool IsDualSrc(DstBlendFactor factor) |
| 35 | { |
| 36 | return factor == DstBlendFactor::SrcAlpha || factor == DstBlendFactor::InvSrcAlpha; |
| 37 | } |
| 38 | |
| 39 | bool BlendingState::RequiresDualSrc() const |
| 40 | { |
| 41 | bool requires_dual_src = false; |
| 42 | requires_dual_src |= IsDualSrc(src_factor) || IsDualSrc(src_factor_alpha); |
| 43 | requires_dual_src |= IsDualSrc(dst_factor) || IsDualSrc(dst_factor_alpha); |
| 44 | requires_dual_src &= blend_enable && use_dual_src; |
| 45 | return requires_dual_src; |
| 46 | } |
| 47 | |
| 48 | // If the framebuffer format has no alpha channel, it is assumed to |
| 49 | // ONE on blending. As the backends may emulate this framebuffer |
| 50 | // configuration with an alpha channel, we just drop all references |
| 51 | // to the destination alpha channel. |
| 52 | static SrcBlendFactor RemoveDstAlphaUsage(SrcBlendFactor factor) |
| 53 | { |
| 54 | switch (factor) |
| 55 | { |
| 56 | case SrcBlendFactor::DstAlpha: |
| 57 | return SrcBlendFactor::One; |
| 58 | case SrcBlendFactor::InvDstAlpha: |
| 59 | return SrcBlendFactor::Zero; |
| 60 | default: |
| 61 | return factor; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static DstBlendFactor RemoveDstAlphaUsage(DstBlendFactor factor) |
| 66 | { |
| 67 | switch (factor) |
| 68 | { |
| 69 | case DstBlendFactor::DstAlpha: |
| 70 | return DstBlendFactor::One; |
| 71 | case DstBlendFactor::InvDstAlpha: |
| 72 | return DstBlendFactor::Zero; |
| 73 | default: |
| 74 | return factor; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // We separate the blending parameter for rgb and alpha. For blending |
| 79 | // the alpha component, CLR and ALPHA are identical. So just always |
| 80 | // use ALPHA as this makes it easier for the backends to use the second |
| 81 | // alpha value of dual source blending. |
| 82 | static DstBlendFactor RemoveSrcColorUsage(DstBlendFactor factor) |
| 83 | { |
| 84 | switch (factor) |
| 85 | { |
| 86 | case DstBlendFactor::SrcClr: |
| 87 | return DstBlendFactor::SrcAlpha; |
| 88 | case DstBlendFactor::InvSrcClr: |
| 89 | return DstBlendFactor::InvSrcAlpha; |
| 90 | default: |
| 91 | return factor; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Same as RemoveSrcColorUsage, but because of the overlapping enum, |
| 96 | // this must be written as another function. |
| 97 | static SrcBlendFactor RemoveDstColorUsage(SrcBlendFactor factor) |
| 98 | { |
| 99 | switch (factor) |
| 100 | { |
| 101 | case SrcBlendFactor::DstClr: |
| 102 | return SrcBlendFactor::DstAlpha; |
| 103 | case SrcBlendFactor::InvDstClr: |
| 104 | return SrcBlendFactor::InvDstAlpha; |
| 105 | default: |
| 106 | return factor; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void BlendingState::Generate(const BPMemory& bp) |
| 111 | { |
| 112 | // Start with everything disabled. |
| 113 | hex = 0; |
| 114 | |
| 115 | const bool target_has_alpha = bp.zcontrol.pixel_format == PixelFormat::RGBA6_Z24; |
| 116 | const bool alpha_test_may_succeed = bp.alpha_test.TestResult() != AlphaTestResult::Fail; |
| 117 | |
| 118 | color_update = bp.blendmode.color_update && alpha_test_may_succeed; |
| 119 | alpha_update = bp.blendmode.alpha_update && target_has_alpha && alpha_test_may_succeed; |
| 120 | const bool dst_alpha = bp.dstalpha.enable && alpha_update; |
| 121 | use_dual_src = true; |
| 122 | |
| 123 | if (bp.blendmode.blend_enable) |
| 124 | { |
| 125 | if (bp.blendmode.subtract) |
| 126 | { |
| 127 | blend_enable = true; |
| 128 | subtract_alpha = subtract = true; |
| 129 | src_factor_alpha = src_factor = SrcBlendFactor::One; |
| 130 | dst_factor_alpha = dst_factor = DstBlendFactor::One; |
| 131 | |
| 132 | if (dst_alpha) |
| 133 | { |
| 134 | subtract_alpha = false; |
| 135 | src_factor_alpha = SrcBlendFactor::One; |
| 136 | dst_factor_alpha = DstBlendFactor::Zero; |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | blend_enable = true; |
| 142 | src_factor = bp.blendmode.src_factor; |
| 143 | dst_factor = bp.blendmode.dst_factor; |
| 144 | if (!target_has_alpha) |
| 145 | { |
| 146 | // uses ONE instead of DSTALPHA |
| 147 | src_factor = RemoveDstAlphaUsage(src_factor); |
| 148 | dst_factor = RemoveDstAlphaUsage(dst_factor); |
| 149 | } |
| 150 | // replaces SrcClr with SrcAlpha and DstClr with DstAlpha, it is important to |
| 151 | // use the dst function for the src factor and vice versa |
| 152 | src_factor_alpha = RemoveDstColorUsage(src_factor); |
| 153 | dst_factor_alpha = RemoveSrcColorUsage(dst_factor); |
| 154 | |
| 155 | if (dst_alpha) |
| 156 | { |
| 157 | src_factor_alpha = SrcBlendFactor::One; |
| 158 | dst_factor_alpha = DstBlendFactor::Zero; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | else if (bp.blendmode.logic_op_enable) |
| 163 | { |
| 164 | if (bp.blendmode.logic_mode == LogicOp::NoOp) |
| 165 | { |
| 166 | // Fast path for Kirby's Return to Dreamland, they use it with dst_alpha. |
| 167 | color_update = false; |
| 168 | alpha_update = alpha_update && dst_alpha; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | logic_op_enable = true; |
| 173 | logic_mode = bp.blendmode.logic_mode; |
| 174 | |
| 175 | if (dst_alpha) |
| 176 | { |
| 177 | // TODO: Not supported by backends. |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // If we aren't writing color or alpha, don't blend it. |
| 183 | // Intel GPUs on D3D12 seem to have issues with dual-source blend if the second source is used in |
| 184 | // the blend state but not actually written (i.e. the alpha src or dst factor is src alpha, but |
| 185 | // alpha update is disabled). So, change the blending configuration to not use a dual-source |
| 186 | // factor. Note that in theory, disabling writing should render these irrelevant. |
| 187 | if (!color_update) |
| 188 | { |
| 189 | src_factor = SrcBlendFactor::Zero; |
| 190 | dst_factor = DstBlendFactor::One; |
| 191 | } |
| 192 | if (!alpha_update) |
| 193 | { |
| 194 | src_factor_alpha = SrcBlendFactor::Zero; |
| 195 | dst_factor_alpha = DstBlendFactor::One; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void BlendingState::ApproximateLogicOpWithBlending() |
| 200 | { |
| 201 | struct LogicOpApproximation |
| 202 | { |
| 203 | bool blend_enable; |
| 204 | bool subtract; |
| 205 | SrcBlendFactor src_factor; |
| 206 | DstBlendFactor dst_factor; |
| 207 | }; |
| 208 | // TODO: This previously had a warning about SRC and DST being aliased and not to mix them, |
| 209 | // but INVSRCCLR and INVDSTCLR were also aliased and were mixed. |
| 210 | // Thus, NOR, EQUIV, INVERT, COPY_INVERTED, and OR_INVERTED duplicate(d) other values. |
| 211 | static constexpr std::array<LogicOpApproximation, 16> approximations = {{ |
| 212 | // clang-format off |
| 213 | {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // CLEAR (Shader outputs 0) |
| 214 | {true, false, SrcBlendFactor::DstClr, DstBlendFactor::Zero}, // AND |
| 215 | {true, true, SrcBlendFactor::One, DstBlendFactor::InvSrcClr}, // AND_REVERSE |
| 216 | {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // COPY |
| 217 | {true, true, SrcBlendFactor::DstClr, DstBlendFactor::One}, // AND_INVERTED |
| 218 | {true, false, SrcBlendFactor::Zero, DstBlendFactor::One}, // NOOP |
| 219 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::InvSrcClr}, // XOR |
| 220 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::One}, // OR |
| 221 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::InvSrcClr}, // NOR |
| 222 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::Zero}, // EQUIV |
| 223 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::Zero}, // INVERT (Shader outputs 255) |
| 224 | {true, false, SrcBlendFactor::One, DstBlendFactor::InvDstAlpha}, // OR_REVERSE |
| 225 | {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // COPY_INVERTED (Shader inverts) |
| 226 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::One}, // OR_INVERTED |
| 227 | {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::InvSrcClr}, // NAND |
| 228 | {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // SET (Shader outputs 255) |
| 229 | // clang-format on |
| 230 | }}; |
| 231 | |
| 232 | logic_op_enable = false; |
| 233 | use_dual_src = false; |
| 234 | const LogicOpApproximation& approximation = approximations[static_cast<u32>(logic_mode.Value())]; |
| 235 | if (approximation.blend_enable) |
| 236 | { |
| 237 | blend_enable = true; |
| 238 | subtract = approximation.subtract; |
| 239 | src_factor = approximation.src_factor; |
| 240 | src_factor_alpha = approximation.src_factor; |
| 241 | dst_factor = approximation.dst_factor; |
| 242 | dst_factor_alpha = approximation.dst_factor; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | bool BlendingState::LogicOpApproximationIsExact() |
| 247 | { |
| 248 | switch (logic_mode.Value()) |
| 249 | { |
| 250 | case LogicOp::Clear: |
| 251 | case LogicOp::Set: |
| 252 | case LogicOp::NoOp: |
| 253 | case LogicOp::Invert: |
| 254 | case LogicOp::CopyInverted: |
| 255 | case LogicOp::Copy: |
| 256 | return true; |
| 257 | default: |
| 258 | return false; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | bool BlendingState::LogicOpApproximationWantsShaderHelp() |
| 263 | { |
| 264 | switch (logic_mode.Value()) |
| 265 | { |
| 266 | case LogicOp::Clear: |
| 267 | case LogicOp::Set: |
| 268 | case LogicOp::NoOp: |
| 269 | case LogicOp::Invert: |
| 270 | case LogicOp::CopyInverted: |
| 271 | return true; |
| 272 | default: |
| 273 | return false; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void SamplerState::Generate(const BPMemory& bp, u32 index) |
| 278 | { |
| 279 | auto tex = bp.tex.GetUnit(index); |
| 280 | const TexMode0& bp_tm0 = tex.texMode0; |
| 281 | const TexMode1& bp_tm1 = tex.texMode1; |
| 282 | |
| 283 | // GX can configure the mip filter to none. However, D3D and Vulkan can't express this in their |
| 284 | // sampler states. Therefore, we set the min/max LOD to zero if this option is used. |
| 285 | tm0.min_filter = bp_tm0.min_filter; |
| 286 | tm0.mipmap_filter = |
| 287 | bp_tm0.mipmap_filter == MipMode::Linear ? FilterMode::Linear : FilterMode::Near; |
| 288 | tm0.mag_filter = bp_tm0.mag_filter; |
| 289 | |
| 290 | // If mipmaps are disabled, clamp min/max lod |
| 291 | if (bp_tm0.mipmap_filter == MipMode::None) |
| 292 | { |
| 293 | tm1.max_lod = 0; |
| 294 | tm1.min_lod = 0; |
| 295 | tm0.lod_bias = 0; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | // NOTE: When comparing, max is checked first, then min; if max is less than min, max wins |
| 300 | tm1.max_lod = bp_tm1.max_lod.Value(); |
| 301 | tm1.min_lod = std::min(tm1.max_lod.Value(), bp_tm1.min_lod.Value()); |
| 302 | tm0.lod_bias = bp_tm0.lod_bias * (256 / 32); |
| 303 | } |
| 304 | |
| 305 | // Wrap modes |
| 306 | // Hardware testing indicates that wrap_mode set to 3 behaves the same as clamp. |
| 307 | auto filter_invalid_wrap = [](WrapMode mode) { |
| 308 | return (mode <= WrapMode::Mirror) ? mode : WrapMode::Clamp; |
| 309 | }; |
| 310 | tm0.wrap_u = filter_invalid_wrap(bp_tm0.wrap_s); |
| 311 | tm0.wrap_v = filter_invalid_wrap(bp_tm0.wrap_t); |
| 312 | if (bp_tm0.max_aniso == MaxAniso::Two) |
| 313 | tm0.anisotropic_filtering = 1; |
| 314 | else if (bp_tm0.max_aniso == MaxAniso::Four) |
| 315 | tm0.anisotropic_filtering = 2; |
| 316 | else |
| 317 | tm0.anisotropic_filtering = 0; |
| 318 | |
| 319 | tm0.diag_lod = bp_tm0.diag_lod; |
| 320 | tm0.lod_clamp = bp_tm0.lod_clamp; // TODO: What does this do? |
| 321 | } |
| 322 | |
| 323 | namespace RenderState |
| 324 | { |
| 325 | RasterizationState GetInvalidRasterizationState() |
| 326 | { |
| 327 | RasterizationState state; |
| 328 | state.hex = UINT32_C(0xFFFFFFFF); |
| 329 | return state; |
| 330 | } |
| 331 | |
| 332 | RasterizationState GetNoCullRasterizationState(PrimitiveType primitive) |
| 333 | { |
| 334 | RasterizationState state = {}; |
| 335 | state.cull_mode = CullMode::None; |
| 336 | state.primitive = primitive; |
| 337 | return state; |
| 338 | } |
| 339 | |
| 340 | RasterizationState GetCullBackFaceRasterizationState(PrimitiveType primitive) |
| 341 | { |
| 342 | RasterizationState state = {}; |
| 343 | state.cull_mode = CullMode::Back; |
| 344 | state.primitive = primitive; |
| 345 | return state; |
| 346 | } |
| 347 | |
| 348 | DepthState GetInvalidDepthState() |
| 349 | { |
| 350 | DepthState state; |
| 351 | state.hex = UINT32_C(0xFFFFFFFF); |
| 352 | return state; |
| 353 | } |
| 354 | |
| 355 | DepthState GetNoDepthTestingDepthState() |
| 356 | { |
| 357 | DepthState state = {}; |
| 358 | state.test_enable = false; |
| 359 | state.update_enable = false; |
| 360 | state.func = CompareMode::Always; |
| 361 | return state; |
| 362 | } |
| 363 | |
| 364 | DepthState GetAlwaysWriteDepthState() |
| 365 | { |
| 366 | DepthState state = {}; |
| 367 | state.test_enable = true; |
| 368 | state.update_enable = true; |
| 369 | state.func = CompareMode::Always; |
| 370 | return state; |
| 371 | } |
| 372 | |
| 373 | BlendingState GetInvalidBlendingState() |
| 374 | { |
| 375 | BlendingState state; |
| 376 | state.hex = UINT32_C(0xFFFFFFFF); |
| 377 | return state; |
| 378 | } |
| 379 | |
| 380 | BlendingState GetNoBlendingBlendState() |
| 381 | { |
| 382 | BlendingState state = {}; |
| 383 | state.use_dual_src = false; |
| 384 | state.blend_enable = false; |
| 385 | state.src_factor = SrcBlendFactor::One; |
| 386 | state.src_factor_alpha = SrcBlendFactor::One; |
| 387 | state.dst_factor = DstBlendFactor::Zero; |
| 388 | state.dst_factor_alpha = DstBlendFactor::Zero; |
| 389 | state.logic_op_enable = false; |
| 390 | state.color_update = true; |
| 391 | state.alpha_update = true; |
| 392 | return state; |
| 393 | } |
| 394 | |
| 395 | BlendingState GetNoColorWriteBlendState() |
| 396 | { |
| 397 | BlendingState state = {}; |
| 398 | state.use_dual_src = false; |
| 399 | state.blend_enable = false; |
| 400 | state.src_factor = SrcBlendFactor::One; |
| 401 | state.src_factor_alpha = SrcBlendFactor::One; |
| 402 | state.dst_factor = DstBlendFactor::Zero; |
| 403 | state.dst_factor_alpha = DstBlendFactor::Zero; |
| 404 | state.logic_op_enable = false; |
| 405 | state.color_update = false; |
| 406 | state.alpha_update = false; |
| 407 | return state; |
| 408 | } |
| 409 | |
| 410 | SamplerState GetInvalidSamplerState() |
| 411 | { |
| 412 | SamplerState state; |
| 413 | state.tm0.hex = 0xFFFFFFFF; |
| 414 | state.tm1.hex = 0xFFFFFFFF; |
| 415 | return state; |
| 416 | } |
| 417 | |
| 418 | SamplerState GetPointSamplerState() |
| 419 | { |
| 420 | SamplerState state = {}; |
| 421 | state.tm0.min_filter = FilterMode::Near; |
| 422 | state.tm0.mag_filter = FilterMode::Near; |
| 423 | state.tm0.mipmap_filter = FilterMode::Near; |
| 424 | state.tm0.wrap_u = WrapMode::Clamp; |
| 425 | state.tm0.wrap_v = WrapMode::Clamp; |
| 426 | state.tm1.min_lod = 0; |
| 427 | state.tm1.max_lod = 255; |
| 428 | state.tm0.lod_bias = 0; |
| 429 | state.tm0.anisotropic_filtering = false; |
| 430 | state.tm0.diag_lod = LODType::Edge; |
| 431 | state.tm0.lod_clamp = false; |
| 432 | return state; |
| 433 | } |
| 434 | |
| 435 | SamplerState GetLinearSamplerState() |
| 436 | { |
| 437 | SamplerState state = {}; |
| 438 | state.tm0.min_filter = FilterMode::Linear; |
| 439 | state.tm0.mag_filter = FilterMode::Linear; |
| 440 | state.tm0.mipmap_filter = FilterMode::Linear; |
| 441 | state.tm0.wrap_u = WrapMode::Clamp; |
| 442 | state.tm0.wrap_v = WrapMode::Clamp; |
| 443 | state.tm1.min_lod = 0; |
| 444 | state.tm1.max_lod = 255; |
| 445 | state.tm0.lod_bias = 0; |
| 446 | state.tm0.anisotropic_filtering = false; |
| 447 | state.tm0.diag_lod = LODType::Edge; |
| 448 | state.tm0.lod_clamp = false; |
| 449 | return state; |
| 450 | } |
| 451 | |
| 452 | FramebufferState GetColorFramebufferState(AbstractTextureFormat format) |
| 453 | { |
| 454 | FramebufferState state = {}; |
| 455 | state.color_texture_format = format; |
| 456 | state.depth_texture_format = AbstractTextureFormat::Undefined; |
| 457 | state.per_sample_shading = false; |
| 458 | state.samples = 1; |
| 459 | state.additional_color_attachment_count = 0; |
| 460 | return state; |
| 461 | } |
| 462 | |
| 463 | FramebufferState GetRGBA8FramebufferState() |
| 464 | { |
| 465 | return GetColorFramebufferState(AbstractTextureFormat::RGBA8); |
| 466 | } |
| 467 | |
| 468 | } // namespace RenderState |