| 1 | // Copyright 2014 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D/D3DState.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | #include <bit> |
| 9 | |
| 10 | #include "Common/Assert.h" |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/Logging/Log.h" |
| 13 | #include "Common/MsgHandler.h" |
| 14 | |
| 15 | #include "VideoBackends/D3D/D3DBase.h" |
| 16 | #include "VideoBackends/D3D/DXTexture.h" |
| 17 | #include "VideoBackends/D3DCommon/D3DCommon.h" |
| 18 | #include "VideoCommon/VideoConfig.h" |
| 19 | |
| 20 | namespace DX11 |
| 21 | { |
| 22 | namespace D3D |
| 23 | { |
| 24 | std::unique_ptr<StateManager> stateman; |
| 25 | |
| 26 | StateManager::StateManager() = default; |
| 27 | StateManager::~StateManager() = default; |
| 28 | |
| 29 | void StateManager::Apply() |
| 30 | { |
| 31 | if (m_dirtyFlags.none()) |
| 32 | return; |
| 33 | |
| 34 | // Framebuffer changes must occur before texture changes, otherwise the D3D runtime messes with |
| 35 | // our bindings and sets them to null to prevent hazards. |
| 36 | if (m_dirtyFlags.test(DirtyFlag_Framebuffer)) |
| 37 | { |
| 38 | if (g_backend_info.bSupportsBBox) |
| 39 | { |
| 40 | D3D::context->OMSetRenderTargetsAndUnorderedAccessViews( |
| 41 | m_pending.framebuffer->GetNumRTVs(), |
| 42 | m_pending.use_integer_rtv ? m_pending.framebuffer->GetIntegerRTVArray() : |
| 43 | m_pending.framebuffer->GetRTVArray(), |
| 44 | m_pending.framebuffer->GetDSV(), m_pending.framebuffer->GetNumRTVs() + 1, 1, |
| 45 | &m_pending.uav, nullptr); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | D3D::context->OMSetRenderTargets(m_pending.framebuffer->GetNumRTVs(), |
| 50 | m_pending.use_integer_rtv ? |
| 51 | m_pending.framebuffer->GetIntegerRTVArray() : |
| 52 | m_pending.framebuffer->GetRTVArray(), |
| 53 | m_pending.framebuffer->GetDSV()); |
| 54 | } |
| 55 | m_current.framebuffer = m_pending.framebuffer; |
| 56 | m_current.uav = m_pending.uav; |
| 57 | m_current.use_integer_rtv = m_pending.use_integer_rtv; |
| 58 | } |
| 59 | |
| 60 | const bool dirtyConstants = m_dirtyFlags.test(DirtyFlag_PixelConstants) || |
| 61 | m_dirtyFlags.test(DirtyFlag_VertexConstants) || |
| 62 | m_dirtyFlags.test(DirtyFlag_CustomConstants) || |
| 63 | m_dirtyFlags.test(DirtyFlag_GeometryConstants); |
| 64 | const bool dirtyShaders = m_dirtyFlags.test(DirtyFlag_PixelShader) || |
| 65 | m_dirtyFlags.test(DirtyFlag_VertexShader) || |
| 66 | m_dirtyFlags.test(DirtyFlag_GeometryShader); |
| 67 | const bool dirtyBuffers = |
| 68 | m_dirtyFlags.test(DirtyFlag_VertexBuffer) || m_dirtyFlags.test(DirtyFlag_IndexBuffer); |
| 69 | |
| 70 | if (dirtyConstants) |
| 71 | { |
| 72 | if (m_current.pixelConstants[0] != m_pending.pixelConstants[0] || |
| 73 | m_current.pixelConstants[1] != m_pending.pixelConstants[1]) |
| 74 | { |
| 75 | D3D::context->PSSetConstantBuffers(0, 1, &m_pending.pixelConstants[0]); |
| 76 | D3D::context->PSSetConstantBuffers(1, 1, &m_pending.pixelConstants[1]); |
| 77 | m_current.pixelConstants[0] = m_pending.pixelConstants[0]; |
| 78 | m_current.pixelConstants[1] = m_pending.pixelConstants[1]; |
| 79 | } |
| 80 | |
| 81 | if (m_current.vertexConstants != m_pending.vertexConstants) |
| 82 | { |
| 83 | D3D::context->VSSetConstantBuffers(0, 1, &m_pending.vertexConstants); |
| 84 | D3D::context->VSSetConstantBuffers(1, 1, &m_pending.vertexConstants); |
| 85 | m_current.vertexConstants = m_pending.vertexConstants; |
| 86 | } |
| 87 | |
| 88 | if (m_current.customConstants != m_pending.customConstants) |
| 89 | { |
| 90 | D3D::context->PSSetConstantBuffers(2, 1, &m_pending.customConstants); |
| 91 | D3D::context->VSSetConstantBuffers(2, 1, &m_pending.customConstants); |
| 92 | m_current.customConstants = m_pending.customConstants; |
| 93 | } |
| 94 | |
| 95 | if (m_current.geometryConstants != m_pending.geometryConstants) |
| 96 | { |
| 97 | D3D::context->GSSetConstantBuffers(0, 1, &m_pending.geometryConstants); |
| 98 | m_current.geometryConstants = m_pending.geometryConstants; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (dirtyBuffers || (m_dirtyFlags.test(DirtyFlag_InputAssembler))) |
| 103 | { |
| 104 | if (m_current.vertexBuffer != m_pending.vertexBuffer || |
| 105 | m_current.vertexBufferStride != m_pending.vertexBufferStride || |
| 106 | m_current.vertexBufferOffset != m_pending.vertexBufferOffset) |
| 107 | { |
| 108 | D3D::context->IASetVertexBuffers(0, 1, &m_pending.vertexBuffer, &m_pending.vertexBufferStride, |
| 109 | &m_pending.vertexBufferOffset); |
| 110 | m_current.vertexBuffer = m_pending.vertexBuffer; |
| 111 | m_current.vertexBufferStride = m_pending.vertexBufferStride; |
| 112 | m_current.vertexBufferOffset = m_pending.vertexBufferOffset; |
| 113 | } |
| 114 | |
| 115 | if (m_current.indexBuffer != m_pending.indexBuffer) |
| 116 | { |
| 117 | D3D::context->IASetIndexBuffer(m_pending.indexBuffer, DXGI_FORMAT_R16_UINT, 0); |
| 118 | m_current.indexBuffer = m_pending.indexBuffer; |
| 119 | } |
| 120 | |
| 121 | if (m_current.topology != m_pending.topology) |
| 122 | { |
| 123 | D3D::context->IASetPrimitiveTopology(m_pending.topology); |
| 124 | m_current.topology = m_pending.topology; |
| 125 | } |
| 126 | |
| 127 | if (m_current.inputLayout != m_pending.inputLayout) |
| 128 | { |
| 129 | D3D::context->IASetInputLayout(m_pending.inputLayout); |
| 130 | m_current.inputLayout = m_pending.inputLayout; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (dirtyShaders) |
| 135 | { |
| 136 | if (m_current.pixelShader != m_pending.pixelShader) |
| 137 | { |
| 138 | D3D::context->PSSetShader(m_pending.pixelShader, nullptr, 0); |
| 139 | m_current.pixelShader = m_pending.pixelShader; |
| 140 | } |
| 141 | |
| 142 | if (m_current.vertexShader != m_pending.vertexShader) |
| 143 | { |
| 144 | D3D::context->VSSetShader(m_pending.vertexShader, nullptr, 0); |
| 145 | m_current.vertexShader = m_pending.vertexShader; |
| 146 | } |
| 147 | |
| 148 | if (m_current.geometryShader != m_pending.geometryShader) |
| 149 | { |
| 150 | D3D::context->GSSetShader(m_pending.geometryShader, nullptr, 0); |
| 151 | m_current.geometryShader = m_pending.geometryShader; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (m_dirtyFlags.test(DirtyFlag_BlendState)) |
| 156 | { |
| 157 | D3D::context->OMSetBlendState(m_pending.blendState, nullptr, 0xFFFFFFFF); |
| 158 | m_current.blendState = m_pending.blendState; |
| 159 | } |
| 160 | if (m_dirtyFlags.test(DirtyFlag_DepthState)) |
| 161 | { |
| 162 | D3D::context->OMSetDepthStencilState(m_pending.depthState, 0); |
| 163 | m_current.depthState = m_pending.depthState; |
| 164 | } |
| 165 | if (m_dirtyFlags.test(DirtyFlag_RasterizerState)) |
| 166 | { |
| 167 | D3D::context->RSSetState(m_pending.rasterizerState); |
| 168 | m_current.rasterizerState = m_pending.rasterizerState; |
| 169 | } |
| 170 | |
| 171 | ApplyTextures(); |
| 172 | |
| 173 | m_dirtyFlags = 0; |
| 174 | } |
| 175 | |
| 176 | void StateManager::ApplyTextures() |
| 177 | { |
| 178 | for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) |
| 179 | { |
| 180 | const u32 flag = i; |
| 181 | if (m_dirtyFlags.test(flag)) |
| 182 | { |
| 183 | if (m_current.textures[i] != m_pending.textures[i]) |
| 184 | { |
| 185 | D3D::context->PSSetShaderResources(i, 1, &m_pending.textures[i]); |
| 186 | m_current.textures[i] = m_pending.textures[i]; |
| 187 | } |
| 188 | m_dirtyFlags.reset(flag); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) |
| 193 | { |
| 194 | const u32 flag = i + VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; |
| 195 | if (m_dirtyFlags.test(flag)) |
| 196 | { |
| 197 | if (m_current.samplers[i] != m_pending.samplers[i]) |
| 198 | { |
| 199 | D3D::context->PSSetSamplers(i, 1, &m_pending.samplers[i]); |
| 200 | m_current.samplers[i] = m_pending.samplers[i]; |
| 201 | } |
| 202 | m_dirtyFlags.reset(flag); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | u32 StateManager::UnsetTexture(ID3D11ShaderResourceView* srv) |
| 208 | { |
| 209 | u32 mask = 0; |
| 210 | |
| 211 | for (u32 index = 0; index < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; ++index) |
| 212 | { |
| 213 | if (m_current.textures[index] == srv) |
| 214 | { |
| 215 | SetTexture(index, nullptr); |
| 216 | mask |= 1 << index; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return mask; |
| 221 | } |
| 222 | |
| 223 | void StateManager::SetTextureByMask(u32 textureSlotMask, ID3D11ShaderResourceView* srv) |
| 224 | { |
| 225 | while (textureSlotMask) |
| 226 | { |
| 227 | const int index = std::countr_zero(textureSlotMask); |
| 228 | SetTexture(index, srv); |
| 229 | textureSlotMask &= ~(1 << index); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void StateManager::SetComputeUAV(u32 index, ID3D11UnorderedAccessView* uav) |
| 234 | { |
| 235 | if (m_compute_images[index] == uav) |
| 236 | return; |
| 237 | |
| 238 | m_compute_images[index] = uav; |
| 239 | D3D::context->CSSetUnorderedAccessViews(0, static_cast<u32>(m_compute_images.size()), |
| 240 | m_compute_images.data(), nullptr); |
| 241 | } |
| 242 | |
| 243 | void StateManager::SetComputeShader(ID3D11ComputeShader* shader) |
| 244 | { |
| 245 | if (m_compute_shader == shader) |
| 246 | return; |
| 247 | |
| 248 | m_compute_shader = shader; |
| 249 | D3D::context->CSSetShader(shader, nullptr, 0); |
| 250 | } |
| 251 | |
| 252 | void StateManager::SyncComputeBindings() |
| 253 | { |
| 254 | if (m_compute_constants != m_pending.pixelConstants[0]) |
| 255 | { |
| 256 | m_compute_constants = m_pending.pixelConstants[0]; |
| 257 | D3D::context->CSSetConstantBuffers(0, 1, &m_compute_constants); |
| 258 | } |
| 259 | |
| 260 | for (u32 start = 0; start < static_cast<u32>(m_compute_textures.size());) |
| 261 | { |
| 262 | if (m_compute_textures[start] == m_pending.textures[start]) |
| 263 | { |
| 264 | start++; |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | m_compute_textures[start] = m_pending.textures[start]; |
| 269 | |
| 270 | u32 end = start + 1; |
| 271 | for (; end < static_cast<u32>(m_compute_textures.size()); end++) |
| 272 | { |
| 273 | if (m_compute_textures[end] == m_pending.textures[end]) |
| 274 | break; |
| 275 | |
| 276 | m_compute_textures[end] = m_pending.textures[end]; |
| 277 | } |
| 278 | |
| 279 | D3D::context->CSSetShaderResources(start, end - start, &m_compute_textures[start]); |
| 280 | start = end; |
| 281 | } |
| 282 | |
| 283 | for (u32 start = 0; start < static_cast<u32>(m_compute_samplers.size());) |
| 284 | { |
| 285 | if (m_compute_samplers[start] == m_pending.samplers[start]) |
| 286 | { |
| 287 | start++; |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | m_compute_samplers[start] = m_pending.samplers[start]; |
| 292 | |
| 293 | u32 end = start + 1; |
| 294 | for (; end < static_cast<u32>(m_compute_samplers.size()); end++) |
| 295 | { |
| 296 | if (m_compute_samplers[end] == m_pending.samplers[end]) |
| 297 | break; |
| 298 | |
| 299 | m_compute_samplers[end] = m_pending.samplers[end]; |
| 300 | } |
| 301 | |
| 302 | D3D::context->CSSetSamplers(start, end - start, &m_compute_samplers[start]); |
| 303 | start = end; |
| 304 | } |
| 305 | } |
| 306 | } // namespace D3D |
| 307 | |
| 308 | StateCache::~StateCache() = default; |
| 309 | |
| 310 | ID3D11SamplerState* StateCache::Get(SamplerState state) |
| 311 | { |
| 312 | std::lock_guard<std::mutex> guard(m_lock); |
| 313 | auto it = m_sampler.find(state); |
| 314 | if (it != m_sampler.end()) |
| 315 | return it->second.Get(); |
| 316 | |
| 317 | D3D11_SAMPLER_DESC sampdc = CD3D11_SAMPLER_DESC(CD3D11_DEFAULT()); |
| 318 | if (state.tm0.mipmap_filter == FilterMode::Linear) |
| 319 | { |
| 320 | if (state.tm0.min_filter == FilterMode::Linear) |
| 321 | sampdc.Filter = (state.tm0.mag_filter == FilterMode::Linear) ? |
| 322 | D3D11_FILTER_MIN_MAG_MIP_LINEAR : |
| 323 | D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR; |
| 324 | else |
| 325 | sampdc.Filter = (state.tm0.mag_filter == FilterMode::Linear) ? |
| 326 | D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR : |
| 327 | D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | if (state.tm0.min_filter == FilterMode::Linear) |
| 332 | sampdc.Filter = (state.tm0.mag_filter == FilterMode::Linear) ? |
| 333 | D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT : |
| 334 | D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT; |
| 335 | else |
| 336 | sampdc.Filter = (state.tm0.mag_filter == FilterMode::Linear) ? |
| 337 | D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT : |
| 338 | D3D11_FILTER_MIN_MAG_MIP_POINT; |
| 339 | } |
| 340 | |
| 341 | static constexpr std::array<D3D11_TEXTURE_ADDRESS_MODE, 3> address_modes = { |
| 342 | {D3D11_TEXTURE_ADDRESS_CLAMP, D3D11_TEXTURE_ADDRESS_WRAP, D3D11_TEXTURE_ADDRESS_MIRROR}}; |
| 343 | sampdc.AddressU = address_modes[static_cast<u32>(state.tm0.wrap_u.Value())]; |
| 344 | sampdc.AddressV = address_modes[static_cast<u32>(state.tm0.wrap_v.Value())]; |
| 345 | sampdc.MaxLOD = state.tm1.max_lod / 16.f; |
| 346 | sampdc.MinLOD = state.tm1.min_lod / 16.f; |
| 347 | sampdc.MipLODBias = state.tm0.lod_bias / 256.f; |
| 348 | |
| 349 | if (state.tm0.anisotropic_filtering != 0) |
| 350 | { |
| 351 | sampdc.Filter = D3D11_FILTER_ANISOTROPIC; |
| 352 | sampdc.MaxAnisotropy = 1u << state.tm0.anisotropic_filtering; |
| 353 | } |
| 354 | |
| 355 | ComPtr<ID3D11SamplerState> res; |
| 356 | HRESULT hr = D3D::device->CreateSamplerState(&sampdc, &res); |
| 357 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating D3D sampler state failed: {}", DX11HRWrap(hr)); |
| 358 | return m_sampler.emplace(state, std::move(res)).first->second.Get(); |
| 359 | } |
| 360 | |
| 361 | ID3D11BlendState* StateCache::Get(BlendingState state) |
| 362 | { |
| 363 | std::lock_guard<std::mutex> guard(m_lock); |
| 364 | auto it = m_blend.find(state.hex); |
| 365 | if (it != m_blend.end()) |
| 366 | return it->second.Get(); |
| 367 | |
| 368 | if (state.logic_op_enable && g_backend_info.bSupportsLogicOp) |
| 369 | { |
| 370 | D3D11_BLEND_DESC1 desc = {}; |
| 371 | D3D11_RENDER_TARGET_BLEND_DESC1& tdesc = desc.RenderTarget[0]; |
| 372 | if (state.color_update) |
| 373 | tdesc.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_RED | D3D11_COLOR_WRITE_ENABLE_GREEN | |
| 374 | D3D11_COLOR_WRITE_ENABLE_BLUE; |
| 375 | else |
| 376 | tdesc.RenderTargetWriteMask = 0; |
| 377 | if (state.alpha_update) |
| 378 | tdesc.RenderTargetWriteMask |= D3D11_COLOR_WRITE_ENABLE_ALPHA; |
| 379 | |
| 380 | static constexpr std::array<D3D11_LOGIC_OP, 16> logic_ops = { |
| 381 | {D3D11_LOGIC_OP_CLEAR, D3D11_LOGIC_OP_AND, D3D11_LOGIC_OP_AND_REVERSE, D3D11_LOGIC_OP_COPY, |
| 382 | D3D11_LOGIC_OP_AND_INVERTED, D3D11_LOGIC_OP_NOOP, D3D11_LOGIC_OP_XOR, D3D11_LOGIC_OP_OR, |
| 383 | D3D11_LOGIC_OP_NOR, D3D11_LOGIC_OP_EQUIV, D3D11_LOGIC_OP_INVERT, D3D11_LOGIC_OP_OR_REVERSE, |
| 384 | D3D11_LOGIC_OP_COPY_INVERTED, D3D11_LOGIC_OP_OR_INVERTED, D3D11_LOGIC_OP_NAND, |
| 385 | D3D11_LOGIC_OP_SET}}; |
| 386 | tdesc.LogicOpEnable = TRUE; |
| 387 | tdesc.LogicOp = logic_ops[u32(state.logic_mode.Value())]; |
| 388 | |
| 389 | ComPtr<ID3D11BlendState1> res; |
| 390 | HRESULT hr = D3D::device1->CreateBlendState1(&desc, &res); |
| 391 | if (SUCCEEDED(hr)) |
| 392 | { |
| 393 | return m_blend.emplace(state.hex, std::move(res)).first->second.Get(); |
| 394 | } |
| 395 | WARN_LOG_FMT(VIDEO, "Creating D3D blend state failed with an error: {}", DX11HRWrap(hr)); |
| 396 | } |
| 397 | |
| 398 | D3D11_BLEND_DESC desc = {}; |
| 399 | desc.AlphaToCoverageEnable = FALSE; |
| 400 | desc.IndependentBlendEnable = FALSE; |
| 401 | |
| 402 | D3D11_RENDER_TARGET_BLEND_DESC& tdesc = desc.RenderTarget[0]; |
| 403 | tdesc.BlendEnable = state.blend_enable; |
| 404 | |
| 405 | if (state.color_update) |
| 406 | tdesc.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_RED | D3D11_COLOR_WRITE_ENABLE_GREEN | |
| 407 | D3D11_COLOR_WRITE_ENABLE_BLUE; |
| 408 | else |
| 409 | tdesc.RenderTargetWriteMask = 0; |
| 410 | if (state.alpha_update) |
| 411 | tdesc.RenderTargetWriteMask |= D3D11_COLOR_WRITE_ENABLE_ALPHA; |
| 412 | |
| 413 | const bool use_dual_source = state.use_dual_src; |
| 414 | const std::array<D3D11_BLEND, 8> src_factors = { |
| 415 | {D3D11_BLEND_ZERO, D3D11_BLEND_ONE, D3D11_BLEND_DEST_COLOR, D3D11_BLEND_INV_DEST_COLOR, |
| 416 | use_dual_source ? D3D11_BLEND_SRC1_ALPHA : D3D11_BLEND_SRC_ALPHA, |
| 417 | use_dual_source ? D3D11_BLEND_INV_SRC1_ALPHA : D3D11_BLEND_INV_SRC_ALPHA, |
| 418 | D3D11_BLEND_DEST_ALPHA, D3D11_BLEND_INV_DEST_ALPHA}}; |
| 419 | const std::array<D3D11_BLEND, 8> dst_factors = { |
| 420 | {D3D11_BLEND_ZERO, D3D11_BLEND_ONE, D3D11_BLEND_SRC_COLOR, D3D11_BLEND_INV_SRC_COLOR, |
| 421 | use_dual_source ? D3D11_BLEND_SRC1_ALPHA : D3D11_BLEND_SRC_ALPHA, |
| 422 | use_dual_source ? D3D11_BLEND_INV_SRC1_ALPHA : D3D11_BLEND_INV_SRC_ALPHA, |
| 423 | D3D11_BLEND_DEST_ALPHA, D3D11_BLEND_INV_DEST_ALPHA}}; |
| 424 | |
| 425 | tdesc.SrcBlend = src_factors[u32(state.src_factor.Value())]; |
| 426 | tdesc.SrcBlendAlpha = src_factors[u32(state.src_factor_alpha.Value())]; |
| 427 | tdesc.DestBlend = dst_factors[u32(state.dst_factor.Value())]; |
| 428 | tdesc.DestBlendAlpha = dst_factors[u32(state.dst_factor_alpha.Value())]; |
| 429 | tdesc.BlendOp = state.subtract ? D3D11_BLEND_OP_REV_SUBTRACT : D3D11_BLEND_OP_ADD; |
| 430 | tdesc.BlendOpAlpha = state.subtract_alpha ? D3D11_BLEND_OP_REV_SUBTRACT : D3D11_BLEND_OP_ADD; |
| 431 | |
| 432 | ComPtr<ID3D11BlendState> res; |
| 433 | HRESULT hr = D3D::device->CreateBlendState(&desc, &res); |
| 434 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating D3D blend state failed: {}", DX11HRWrap(hr)); |
| 435 | return m_blend.emplace(state.hex, std::move(res)).first->second.Get(); |
| 436 | } |
| 437 | |
| 438 | ID3D11RasterizerState* StateCache::Get(RasterizationState state) |
| 439 | { |
| 440 | std::lock_guard<std::mutex> guard(m_lock); |
| 441 | auto it = m_raster.find(state.hex); |
| 442 | if (it != m_raster.end()) |
| 443 | return it->second.Get(); |
| 444 | |
| 445 | static constexpr std::array<D3D11_CULL_MODE, 4> cull_modes = { |
| 446 | {D3D11_CULL_NONE, D3D11_CULL_BACK, D3D11_CULL_FRONT, D3D11_CULL_BACK}}; |
| 447 | |
| 448 | D3D11_RASTERIZER_DESC desc = {}; |
| 449 | desc.FillMode = D3D11_FILL_SOLID; |
| 450 | desc.CullMode = cull_modes[u32(state.cull_mode.Value())]; |
| 451 | desc.ScissorEnable = TRUE; |
| 452 | |
| 453 | ComPtr<ID3D11RasterizerState> res; |
| 454 | HRESULT hr = D3D::device->CreateRasterizerState(&desc, &res); |
| 455 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating D3D rasterizer state failed: {}", DX11HRWrap(hr)); |
| 456 | return m_raster.emplace(state.hex, std::move(res)).first->second.Get(); |
| 457 | } |
| 458 | |
| 459 | ID3D11DepthStencilState* StateCache::Get(DepthState state) |
| 460 | { |
| 461 | std::lock_guard<std::mutex> guard(m_lock); |
| 462 | auto it = m_depth.find(state.hex); |
| 463 | if (it != m_depth.end()) |
| 464 | return it->second.Get(); |
| 465 | |
| 466 | D3D11_DEPTH_STENCIL_DESC depthdc = CD3D11_DEPTH_STENCIL_DESC(CD3D11_DEFAULT()); |
| 467 | |
| 468 | depthdc.DepthEnable = TRUE; |
| 469 | depthdc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; |
| 470 | depthdc.DepthFunc = D3D11_COMPARISON_GREATER; |
| 471 | depthdc.StencilEnable = FALSE; |
| 472 | depthdc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; |
| 473 | depthdc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; |
| 474 | |
| 475 | // Less/greater are swapped due to inverted depth. |
| 476 | const D3D11_COMPARISON_FUNC d3dCmpFuncs[8] = { |
| 477 | D3D11_COMPARISON_NEVER, D3D11_COMPARISON_GREATER, D3D11_COMPARISON_EQUAL, |
| 478 | D3D11_COMPARISON_GREATER_EQUAL, D3D11_COMPARISON_LESS, D3D11_COMPARISON_NOT_EQUAL, |
| 479 | D3D11_COMPARISON_LESS_EQUAL, D3D11_COMPARISON_ALWAYS}; |
| 480 | |
| 481 | if (state.test_enable) |
| 482 | { |
| 483 | depthdc.DepthEnable = TRUE; |
| 484 | depthdc.DepthWriteMask = |
| 485 | state.update_enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; |
| 486 | depthdc.DepthFunc = d3dCmpFuncs[u32(state.func.Value())]; |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | // if the test is disabled write is disabled too |
| 491 | depthdc.DepthEnable = FALSE; |
| 492 | depthdc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; |
| 493 | } |
| 494 | |
| 495 | ComPtr<ID3D11DepthStencilState> res; |
| 496 | HRESULT hr = D3D::device->CreateDepthStencilState(&depthdc, &res); |
| 497 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Creating D3D depth stencil state failed: {}", DX11HRWrap(hr)); |
| 498 | return m_depth.emplace(state.hex, std::move(res)).first->second.Get(); |
| 499 | } |
| 500 | |
| 501 | D3D11_PRIMITIVE_TOPOLOGY StateCache::GetPrimitiveTopology(PrimitiveType primitive) |
| 502 | { |
| 503 | static constexpr std::array<D3D11_PRIMITIVE_TOPOLOGY, 4> primitives = { |
| 504 | {D3D11_PRIMITIVE_TOPOLOGY_POINTLIST, D3D11_PRIMITIVE_TOPOLOGY_LINELIST, |
| 505 | D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP}}; |
| 506 | return primitives[static_cast<u32>(primitive)]; |
| 507 | } |
| 508 | |
| 509 | } // namespace DX11 |