| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/PipelineUtils.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | #include "Common/Logging/Log.h" |
| 8 | |
| 9 | #include "VideoCommon/BPMemory.h" |
| 10 | #include "VideoCommon/ConstantManager.h" |
| 11 | #include "VideoCommon/DriverDetails.h" |
| 12 | #include "VideoCommon/GeometryShaderGen.h" |
| 13 | #include "VideoCommon/PixelShaderGen.h" |
| 14 | #include "VideoCommon/RenderState.h" |
| 15 | #include "VideoCommon/VertexShaderGen.h" |
| 16 | #include "VideoCommon/VideoConfig.h" |
| 17 | |
| 18 | namespace VideoCommon |
| 19 | { |
| 20 | /// Edits the UID based on driver bugs and other special configurations |
| 21 | GXPipelineUid ApplyDriverBugs(const GXPipelineUid& in) |
| 22 | { |
| 23 | GXPipelineUid out; |
| 24 | // TODO: static_assert(std::is_trivially_copyable_v<GXPipelineUid>); |
| 25 | // GXPipelineUid is not trivially copyable because RasterizationState and BlendingState aren't |
| 26 | // either, but we can pretend it is for now. This will be improved after PR #10848 is finished. |
| 27 | memcpy(static_cast<void*>(&out), static_cast<const void*>(&in), sizeof(out)); // copy padding |
| 28 | pixel_shader_uid_data* ps = out.ps_uid.GetUidData(); |
| 29 | BlendingState& blend = out.blending_state; |
| 30 | |
| 31 | if (ps->ztest == EmulatedZ::ForcedEarly && !out.depth_state.update_enable) |
| 32 | { |
| 33 | // No need to force early depth test if you're not writing z |
| 34 | ps->ztest = EmulatedZ::Early; |
| 35 | } |
| 36 | |
| 37 | // If framebuffer fetch is available, we can emulate logic ops in the fragment shader |
| 38 | // and don't need the below blend approximation |
| 39 | if (blend.logic_op_enable && !g_backend_info.bSupportsLogicOp && |
| 40 | !g_backend_info.bSupportsFramebufferFetch) |
| 41 | { |
| 42 | if (!blend.LogicOpApproximationIsExact()) |
| 43 | WARN_LOG_FMT(VIDEO, |
| 44 | "Approximating logic op with blending, this will produce incorrect rendering."); |
| 45 | if (blend.LogicOpApproximationWantsShaderHelp()) |
| 46 | { |
| 47 | ps->emulate_logic_op_with_blend = true; |
| 48 | ps->logic_op_mode = static_cast<u32>(blend.logic_mode.Value()); |
| 49 | } |
| 50 | blend.ApproximateLogicOpWithBlending(); |
| 51 | } |
| 52 | |
| 53 | const bool benefits_from_ps_dual_source_off = |
| 54 | (!g_backend_info.bSupportsDualSourceBlend && g_backend_info.bSupportsFramebufferFetch) || |
| 55 | DriverDetails::HasBug(DriverDetails::BUG_BROKEN_DUAL_SOURCE_BLENDING); |
| 56 | if (benefits_from_ps_dual_source_off && !blend.RequiresDualSrc()) |
| 57 | { |
| 58 | // Only use dual-source blending when required on drivers that don't support it very well. |
| 59 | ps->no_dual_src = true; |
| 60 | blend.use_dual_src = false; |
| 61 | } |
| 62 | |
| 63 | if (g_backend_info.bSupportsFramebufferFetch) |
| 64 | { |
| 65 | bool fbfetch_blend = false; |
| 66 | if ((DriverDetails::HasBug(DriverDetails::BUG_BROKEN_DISCARD_WITH_EARLY_Z) || |
| 67 | !g_backend_info.bSupportsEarlyZ) && |
| 68 | ps->ztest == EmulatedZ::ForcedEarly) |
| 69 | { |
| 70 | ps->ztest = EmulatedZ::EarlyWithFBFetch; |
| 71 | fbfetch_blend |= static_cast<bool>(out.blending_state.blend_enable); |
| 72 | ps->no_dual_src = true; |
| 73 | } |
| 74 | fbfetch_blend |= blend.logic_op_enable && !g_backend_info.bSupportsLogicOp; |
| 75 | fbfetch_blend |= blend.use_dual_src && !g_backend_info.bSupportsDualSourceBlend; |
| 76 | if (fbfetch_blend) |
| 77 | { |
| 78 | ps->no_dual_src = true; |
| 79 | if (blend.logic_op_enable) |
| 80 | { |
| 81 | ps->logic_op_enable = true; |
| 82 | ps->logic_op_mode = static_cast<u32>(blend.logic_mode.Value()); |
| 83 | blend.logic_op_enable = false; |
| 84 | } |
| 85 | if (blend.blend_enable) |
| 86 | { |
| 87 | ps->blend_enable = true; |
| 88 | ps->blend_src_factor = blend.src_factor; |
| 89 | ps->blend_src_factor_alpha = blend.src_factor_alpha; |
| 90 | ps->blend_dst_factor = blend.dst_factor; |
| 91 | ps->blend_dst_factor_alpha = blend.dst_factor_alpha; |
| 92 | ps->blend_subtract = blend.subtract; |
| 93 | ps->blend_subtract_alpha = blend.subtract_alpha; |
| 94 | blend.blend_enable = false; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // force dual src off if we can't support it |
| 100 | if (!g_backend_info.bSupportsDualSourceBlend) |
| 101 | { |
| 102 | ps->no_dual_src = true; |
| 103 | blend.use_dual_src = false; |
| 104 | } |
| 105 | |
| 106 | if (ps->ztest == EmulatedZ::ForcedEarly && !g_backend_info.bSupportsEarlyZ) |
| 107 | { |
| 108 | // These things should be false |
| 109 | ASSERT(!ps->zfreeze); |
| 110 | // ZCOMPLOC HACK: |
| 111 | // The only way to emulate alpha test + early-z is to force early-z in the shader. |
| 112 | // As this isn't available on all drivers and as we can't emulate this feature otherwise, |
| 113 | // we are only able to choose which one we want to respect more. |
| 114 | // Tests seem to have proven that writing depth even when the alpha test fails is more |
| 115 | // important that a reliable alpha test, so we just force the alpha test to always succeed. |
| 116 | // At least this seems to be less buggy. |
| 117 | ps->ztest = EmulatedZ::EarlyWithZComplocHack; |
| 118 | } |
| 119 | |
| 120 | if (g_ActiveConfig.UseVSForLinePointExpand() && |
| 121 | (out.rasterization_state.primitive == PrimitiveType::Points || |
| 122 | out.rasterization_state.primitive == PrimitiveType::Lines)) |
| 123 | { |
| 124 | // All primitives are expanded to triangles in the vertex shader |
| 125 | vertex_shader_uid_data* vs = out.vs_uid.GetUidData(); |
| 126 | const PortableVertexDeclaration& decl = out.vertex_format->GetVertexDeclaration(); |
| 127 | vs->position_has_3_elems = decl.position.components >= 3; |
| 128 | vs->texcoord_elem_count = 0; |
| 129 | for (int i = 0; i < 8; i++) |
| 130 | { |
| 131 | if (decl.texcoords[i].enable) |
| 132 | { |
| 133 | ASSERT(decl.texcoords[i].components <= 3); |
| 134 | vs->texcoord_elem_count |= decl.texcoords[i].components << (i * 2); |
| 135 | } |
| 136 | } |
| 137 | out.vertex_format = nullptr; |
| 138 | if (out.rasterization_state.primitive == PrimitiveType::Points) |
| 139 | vs->vs_expand = VSExpand::Point; |
| 140 | else |
| 141 | vs->vs_expand = VSExpand::Line; |
| 142 | PrimitiveType prim = g_backend_info.bSupportsPrimitiveRestart ? PrimitiveType::TriangleStrip : |
| 143 | PrimitiveType::Triangles; |
| 144 | out.rasterization_state.primitive = prim; |
| 145 | out.gs_uid.GetUidData()->primitive_type = static_cast<u32>(prim); |
| 146 | } |
| 147 | |
| 148 | return out; |
| 149 | } |
| 150 | |
| 151 | std::size_t PipelineToHash(const GXPipelineUid& in) |
| 152 | { |
| 153 | XXH3_state_t pipeline_hash_state; |
| 154 | XXH3_INITSTATE(&pipeline_hash_state); |
| 155 | XXH3_64bits_reset_withSeed(&pipeline_hash_state, static_cast<XXH64_hash_t>(1)); |
| 156 | UpdateHashWithPipeline(in, &pipeline_hash_state); |
| 157 | return XXH3_64bits_digest(&pipeline_hash_state); |
| 158 | } |
| 159 | |
| 160 | void UpdateHashWithPipeline(const GXPipelineUid& in, XXH3_state_t* hash_state) |
| 161 | { |
| 162 | XXH3_64bits_update(hash_state, &in.vertex_format->GetVertexDeclaration(), |
| 163 | sizeof(PortableVertexDeclaration)); |
| 164 | XXH3_64bits_update(hash_state, &in.blending_state, sizeof(BlendingState)); |
| 165 | XXH3_64bits_update(hash_state, &in.depth_state, sizeof(DepthState)); |
| 166 | XXH3_64bits_update(hash_state, &in.rasterization_state, sizeof(RasterizationState)); |
| 167 | XXH3_64bits_update(hash_state, &in.gs_uid, sizeof(GeometryShaderUid)); |
| 168 | XXH3_64bits_update(hash_state, &in.ps_uid, sizeof(PixelShaderUid)); |
| 169 | XXH3_64bits_update(hash_state, &in.vs_uid, sizeof(VertexShaderUid)); |
| 170 | } |
| 171 | } // namespace VideoCommon |