From 421245aeba76ad4e7f1b545794f485f6be4a8722 Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Fri, 13 Dec 2024 17:21:32 +0100 Subject: [PATCH] lightgun xhair: centralize logic in crosshairNeeded() --- core/hw/maple/maple_cfg.cpp | 6 ---- core/rend/dx11/dx11_overlay.cpp | 53 +++++++++++++++---------------- core/rend/dx11/dx11context.cpp | 10 +++--- core/rend/dx9/d3d_overlay.cpp | 43 ++++++++++++------------- core/rend/dx9/dxcontext.cpp | 5 +-- core/rend/gles/gldraw.cpp | 11 ++----- core/rend/osd.h | 19 ++++++++--- core/rend/vulkan/oit/oit_drawer.h | 1 - core/rend/vulkan/overlay.cpp | 7 ++-- 9 files changed, 68 insertions(+), 87 deletions(-) diff --git a/core/hw/maple/maple_cfg.cpp b/core/hw/maple/maple_cfg.cpp index 9ce4199de..4cc0f51ab 100644 --- a/core/hw/maple/maple_cfg.cpp +++ b/core/hw/maple/maple_cfg.cpp @@ -405,12 +405,6 @@ void mcfg_CreateDevices() die("Unknown system"); break; } - if (settings.platform.isArcade() && !settings.input.fourPlayerGames) - { - // No known 4-player lightgun/touchscreen game so far - config::CrosshairColor[2].override(0); - config::CrosshairColor[3].override(0); - } vmuDigest(); } diff --git a/core/rend/dx11/dx11_overlay.cpp b/core/rend/dx11/dx11_overlay.cpp index ff507c56e..2c0354741 100644 --- a/core/rend/dx11/dx11_overlay.cpp +++ b/core/rend/dx11/dx11_overlay.cpp @@ -142,39 +142,36 @@ void DX11Overlay::draw(u32 width, u32 height, bool vmu, bool crosshair) quad.draw(vmuTextureViews[i], samplers->getSampler(false)); } } - if (crosshair && crosshairsNeeded()) + if (crosshair) { - if (!xhairTexture) + for (u32 i = 0; i < config::CrosshairColor.size(); i++) { - const u32* texData = getCrosshairTextureData(); - D3D11_TEXTURE2D_DESC desc{}; - desc.Width = 16; - desc.Height = 16; - desc.ArraySize = 1; - desc.SampleDesc.Count = 1; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; - desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.MipLevels = 1; - - if (SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &xhairTexture.get()))) + if (!crosshairNeeded(i)) + continue; + if (!xhairTexture) { - D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc{}; - viewDesc.Format = desc.Format; - viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; - viewDesc.Texture2D.MipLevels = desc.MipLevels; - device->CreateShaderResourceView(xhairTexture, &viewDesc, &xhairTextureView.get()); + const u32* texData = getCrosshairTextureData(); + D3D11_TEXTURE2D_DESC desc{}; + desc.Width = 16; + desc.Height = 16; + desc.ArraySize = 1; + desc.SampleDesc.Count = 1; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.MipLevels = 1; + + if (SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &xhairTexture.get()))) + { + D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc{}; + viewDesc.Format = desc.Format; + viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + viewDesc.Texture2D.MipLevels = desc.MipLevels; + device->CreateShaderResourceView(xhairTexture, &viewDesc, &xhairTextureView.get()); - deviceContext->UpdateSubresource(xhairTexture, 0, nullptr, texData, 16 * 4, 16 * 4 * 16); + deviceContext->UpdateSubresource(xhairTexture, 0, nullptr, texData, 16 * 4, 16 * 4 * 16); + } } - } - for (u32 i = 0; i < config::CrosshairColor.size(); i++) - { - if (config::CrosshairColor[i] == 0) - continue; - if (settings.platform.isConsole() - && config::MapleMainDevices[i] != MDT_LightGun) - continue; auto [x, y] = getCrosshairPosition(i); #ifdef LIBRETRO diff --git a/core/rend/dx11/dx11context.cpp b/core/rend/dx11/dx11context.cpp index 0e0d5cf4d..808efac00 100644 --- a/core/rend/dx11/dx11context.cpp +++ b/core/rend/dx11/dx11context.cpp @@ -253,7 +253,10 @@ void DX11Context::EndImGuiFrame() { if (pDevice && pDeviceContext && renderTargetView) { - if (!overlayOnly) + if (overlayOnly) { + overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true); + } + else { pDeviceContext->OMSetRenderTargets(1, &renderTargetView.get(), nullptr); const FLOAT black[4] { 0.f, 0.f, 0.f, 1.f }; @@ -261,11 +264,6 @@ void DX11Context::EndImGuiFrame() if (renderer != nullptr) renderer->RenderLastFrame(); } - if (overlayOnly) - { - if (crosshairsNeeded() || config::FloatVMUs) - overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true); - } ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); } frameRendered = true; diff --git a/core/rend/dx9/d3d_overlay.cpp b/core/rend/dx9/d3d_overlay.cpp index b62c2ecb7..4dc527b7a 100644 --- a/core/rend/dx9/d3d_overlay.cpp +++ b/core/rend/dx9/d3d_overlay.cpp @@ -87,35 +87,32 @@ void D3DOverlay::draw(u32 width, u32 height, bool vmu, bool crosshair) drawQuad(rect, D3DCOLOR_ARGB(192, 255, 255, 255)); } } - if (crosshair && crosshairsNeeded()) + if (crosshair) { - if (!xhairTexture) + for (u32 i = 0; i < config::CrosshairColor.size(); i++) { - const u32* texData = getCrosshairTextureData(); - device->CreateTexture(16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &xhairTexture.get(), 0); - D3DLOCKED_RECT rect; - if (SUCCEEDED(xhairTexture->LockRect(0, &rect, nullptr, 0))) + if (!crosshairNeeded(i)) + continue; + + if (!xhairTexture) { - if (rect.Pitch == 16 * sizeof(u32)) - memcpy(rect.pBits, texData, 16 * 16 * sizeof(u32)); - else + const u32* texData = getCrosshairTextureData(); + device->CreateTexture(16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &xhairTexture.get(), 0); + D3DLOCKED_RECT rect; + if (SUCCEEDED(xhairTexture->LockRect(0, &rect, nullptr, 0))) { - u8 *dst = (u8 *) rect.pBits; - for (int y = 0; y < 16; y++) - memcpy(dst + y * rect.Pitch, texData + y * 16, 16 * sizeof(u32)); + if (rect.Pitch == 16 * sizeof(u32)) + memcpy(rect.pBits, texData, 16 * 16 * sizeof(u32)); + else + { + u8 *dst = (u8 *) rect.pBits; + for (int y = 0; y < 16; y++) + memcpy(dst + y * rect.Pitch, texData + y * 16, 16 * sizeof(u32)); + } + xhairTexture->UnlockRect(0); } - xhairTexture->UnlockRect(0); } - } - device->SetTexture(0, xhairTexture); - for (u32 i = 0; i < config::CrosshairColor.size(); i++) - { - if (config::CrosshairColor[i] == 0) - continue; - if (settings.platform.isConsole() - && config::MapleMainDevices[i] != MDT_LightGun) - continue; - + device->SetTexture(0, xhairTexture); auto [x, y] = getCrosshairPosition(i); float halfWidth = config::CrosshairSize * settings.display.uiScale / 2.f; RECT rect { (long) (x - halfWidth), (long) (y - halfWidth), (long) (x + halfWidth), (long) (y + halfWidth) }; diff --git a/core/rend/dx9/dxcontext.cpp b/core/rend/dx9/dxcontext.cpp index f57306725..2fb35aa07 100644 --- a/core/rend/dx9/dxcontext.cpp +++ b/core/rend/dx9/dxcontext.cpp @@ -196,10 +196,7 @@ void DXContext::EndImGuiFrame() if (SUCCEEDED(pDevice->BeginScene())) { if (overlayOnly) - { - if (crosshairsNeeded() || config::FloatVMUs) - overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true); - } + overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true); ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); pDevice->EndScene(); } diff --git a/core/rend/gles/gldraw.cpp b/core/rend/gles/gldraw.cpp index dcf0447be..01327cc26 100644 --- a/core/rend/gles/gldraw.cpp +++ b/core/rend/gles/gldraw.cpp @@ -1015,12 +1015,6 @@ static void updateLightGunTexture() static void drawGunCrosshair(u8 port, int width, int height) { - if (config::CrosshairColor[port] == 0) - return; - if (settings.platform.isConsole() - && config::MapleMainDevices[port] != MDT_LightGun) - return; - auto [x, y] = getCrosshairPosition(port); #ifdef LIBRETRO float halfWidth = lightgun_crosshair_size / 2.f / config::ScreenStretching * 100.f * config::RenderResolution / 480.f; @@ -1073,10 +1067,9 @@ void drawVmusAndCrosshairs(int width, int height) drawVmuTexture(i, width, height); } - if (crosshairsNeeded()) { - for (int i = 0 ; i < 4 ; i++) + for (int i = 0 ; i < 4 ; i++) + if (crosshairNeeded(i)) drawGunCrosshair(i, width, height); - } glCheck(); } diff --git a/core/rend/osd.h b/core/rend/osd.h index 60a202797..f88a13846 100644 --- a/core/rend/osd.h +++ b/core/rend/osd.h @@ -30,14 +30,23 @@ void push_vmu_screen(int bus_id, int bus_port, u8* buffer); const u32 *getCrosshairTextureData(); std::pair getCrosshairPosition(int playerNum); -static inline bool crosshairsNeeded() +static inline bool crosshairNeeded(int port) { - if (config::CrosshairColor[0] == 0 && config::CrosshairColor[1] == 0 - && config::CrosshairColor[2] == 0 && config::CrosshairColor[3] == 0) + if (port < 0 || port >= 4) return false; - if (settings.platform.isArcade() && !settings.input.lightgunGame) - // not a lightgun game + if (config::CrosshairColor[port] == 0) return false; + if (settings.platform.isArcade()) + { + // Arcade game: only for lightgun games and P1 or P2 (no known 4-player lightgun or touchscreen game for now) + if (!settings.input.lightgunGame || (port >= 2 && !settings.input.fourPlayerGames)) + return false; + } + else { + // Console game + if (config::MapleMainDevices[port] != MDT_LightGun) + return false; + } return true; } diff --git a/core/rend/vulkan/oit/oit_drawer.h b/core/rend/vulkan/oit/oit_drawer.h index 7075351bf..7ea1a5613 100644 --- a/core/rend/vulkan/oit/oit_drawer.h +++ b/core/rend/vulkan/oit/oit_drawer.h @@ -126,7 +126,6 @@ class OITDrawer : public BaseDrawer SamplerManager *samplerManager = nullptr; OITBuffers *oitBuffers = nullptr; bool needAttachmentTransition = false; - bool needDepthTransition = false; OITDescriptorSets descriptorSets; vk::Buffer curMainBuffer; bool dithering = false; diff --git a/core/rend/vulkan/overlay.cpp b/core/rend/vulkan/overlay.cpp index 78bb7fb5e..483f1e8d9 100644 --- a/core/rend/vulkan/overlay.cpp +++ b/core/rend/vulkan/overlay.cpp @@ -212,19 +212,16 @@ void VulkanOverlay::Draw(vk::CommandBuffer commandBuffer, vk::Extent2D viewport, drawers[i]->Draw(commandBuffer, vmuTextures[i]->GetImageView(), vtx, true, color); } } - if (crosshair && crosshairsNeeded()) + if (crosshair) { pipeline->BindPipeline(commandBuffer); bool imageViewBound = false; for (size_t i = 0; i < config::CrosshairColor.size(); i++) { - if (config::CrosshairColor[i] == 0) - continue; - if (settings.platform.isConsole() && config::MapleMainDevices[i] != MDT_LightGun) + if (!crosshairNeeded(i)) continue; auto [x, y] = getCrosshairPosition(i); - #ifdef LIBRETRO float w = lightgun_crosshair_size * scaling / config::ScreenStretching * 100.f; float h = lightgun_crosshair_size * scaling;