From e84fe4d94849b54137f96d468e37c132f8f43622 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 8 May 2021 02:11:02 +1000 Subject: [PATCH] D3D11HostDisplay: Fix alpha channel of lightgun crosshair --- src/frontend-common/d3d11_host_display.cpp | 8 +- src/frontend-common/d3d11_host_display.h | 1 + src/frontend-common/display_ps.hlsl | 4 + src/frontend-common/display_ps_alpha.hlsl.h | 131 ++++++++++++++++++ .../make_d3d_display_shader_bytecode.bat | 3 +- 5 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 src/frontend-common/display_ps_alpha.hlsl.h diff --git a/src/frontend-common/d3d11_host_display.cpp b/src/frontend-common/d3d11_host_display.cpp index 1bc5c2b7b..c7571fa02 100644 --- a/src/frontend-common/d3d11_host_display.cpp +++ b/src/frontend-common/d3d11_host_display.cpp @@ -9,6 +9,7 @@ #include "core/settings.h" #include "core/shader_cache_version.h" #include "display_ps.hlsl.h" +#include "display_ps_alpha.hlsl.h" #include "display_vs.hlsl.h" #include "imgui.h" #include "imgui_impl_dx11.h" @@ -603,7 +604,9 @@ bool D3D11HostDisplay::CreateResources() D3D11::ShaderCompiler::CreateVertexShader(m_device.Get(), s_display_vs_bytecode, sizeof(s_display_vs_bytecode)); m_display_pixel_shader = D3D11::ShaderCompiler::CreatePixelShader(m_device.Get(), s_display_ps_bytecode, sizeof(s_display_ps_bytecode)); - if (!m_display_vertex_shader || !m_display_pixel_shader) + m_display_alpha_pixel_shader = D3D11::ShaderCompiler::CreatePixelShader(m_device.Get(), s_display_ps_alpha_bytecode, + sizeof(s_display_ps_alpha_bytecode)); + if (!m_display_vertex_shader || !m_display_pixel_shader || !m_display_alpha_pixel_shader) return false; if (!m_display_uniform_buffer.Create(m_device.Get(), D3D11_BIND_CONSTANT_BUFFER, DISPLAY_UNIFORM_BUFFER_SIZE)) @@ -662,6 +665,7 @@ void D3D11HostDisplay::DestroyResources() m_display_uniform_buffer.Release(); m_linear_sampler.Reset(); m_point_sampler.Reset(); + m_display_alpha_pixel_shader.Reset(); m_display_pixel_shader.Reset(); m_display_vertex_shader.Reset(); m_display_blend_state.Reset(); @@ -840,7 +844,7 @@ void D3D11HostDisplay::RenderSoftwareCursor(s32 left, s32 top, s32 width, s32 he { m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); m_context->VSSetShader(m_display_vertex_shader.Get(), nullptr, 0); - m_context->PSSetShader(m_display_pixel_shader.Get(), nullptr, 0); + m_context->PSSetShader(m_display_alpha_pixel_shader.Get(), nullptr, 0); m_context->PSSetShaderResources(0, 1, static_cast(texture_handle)->GetD3DSRVArray()); m_context->PSSetSamplers(0, 1, m_linear_sampler.GetAddressOf()); diff --git a/src/frontend-common/d3d11_host_display.h b/src/frontend-common/d3d11_host_display.h index b90a89ea7..9d64361f4 100644 --- a/src/frontend-common/d3d11_host_display.h +++ b/src/frontend-common/d3d11_host_display.h @@ -124,6 +124,7 @@ protected: ComPtr m_software_cursor_blend_state; ComPtr m_display_vertex_shader; ComPtr m_display_pixel_shader; + ComPtr m_display_alpha_pixel_shader; ComPtr m_point_sampler; ComPtr m_linear_sampler; diff --git a/src/frontend-common/display_ps.hlsl b/src/frontend-common/display_ps.hlsl index 95f12b6c3..f1d84f095 100644 --- a/src/frontend-common/display_ps.hlsl +++ b/src/frontend-common/display_ps.hlsl @@ -4,5 +4,9 @@ SamplerState samp0_ss : register(s0); void main(in float2 v_tex0 : TEXCOORD0, out float4 o_col0 : SV_Target) { +#ifdef ALPHA + o_col0 = samp0.Sample(samp0_ss, v_tex0); +#else o_col0 = float4(samp0.Sample(samp0_ss, v_tex0).rgb, 1.0); +#endif } \ No newline at end of file diff --git a/src/frontend-common/display_ps_alpha.hlsl.h b/src/frontend-common/display_ps_alpha.hlsl.h new file mode 100644 index 000000000..f863af159 --- /dev/null +++ b/src/frontend-common/display_ps_alpha.hlsl.h @@ -0,0 +1,131 @@ +#if 0 +// +// Generated by Microsoft (R) HLSL Shader Compiler 10.1 +// +// +// Resource Bindings: +// +// Name Type Format Dim HLSL Bind Count +// ------------------------------ ---------- ------- ----------- -------------- ------ +// samp0_ss sampler NA NA s0 1 +// samp0 texture float4 2d t0 1 +// +// +// +// Input signature: +// +// Name Index Mask Register SysValue Format Used +// -------------------- ----- ------ -------- -------- ------- ------ +// TEXCOORD 0 xy 0 NONE float xy +// +// +// Output signature: +// +// Name Index Mask Register SysValue Format Used +// -------------------- ----- ------ -------- -------- ------- ------ +// SV_Target 0 xyzw 0 TARGET float xyzw +// +ps_4_0 +dcl_sampler s0, mode_default +dcl_resource_texture2d (float,float,float,float) t0 +dcl_input_ps linear v0.xy +dcl_output o0.xyzw +sample o0.xyzw, v0.xyxx, t0.xyzw, s0 +ret +// Approximately 2 instruction slots used +#endif + +const BYTE static s_display_ps_alpha_bytecode[] = +{ + 68, 88, 66, 67, 140, 134, + 46, 29, 68, 36, 193, 23, + 94, 171, 102, 123, 183, 66, + 19, 177, 1, 0, 0, 0, + 32, 2, 0, 0, 5, 0, + 0, 0, 52, 0, 0, 0, + 208, 0, 0, 0, 4, 1, + 0, 0, 56, 1, 0, 0, + 164, 1, 0, 0, 82, 68, + 69, 70, 148, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 2, 0, 0, 0, + 28, 0, 0, 0, 0, 4, + 255, 255, 0, 129, 0, 0, + 107, 0, 0, 0, 92, 0, + 0, 0, 3, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, + 101, 0, 0, 0, 2, 0, + 0, 0, 5, 0, 0, 0, + 4, 0, 0, 0, 255, 255, + 255, 255, 0, 0, 0, 0, + 1, 0, 0, 0, 13, 0, + 0, 0, 115, 97, 109, 112, + 48, 95, 115, 115, 0, 115, + 97, 109, 112, 48, 0, 77, + 105, 99, 114, 111, 115, 111, + 102, 116, 32, 40, 82, 41, + 32, 72, 76, 83, 76, 32, + 83, 104, 97, 100, 101, 114, + 32, 67, 111, 109, 112, 105, + 108, 101, 114, 32, 49, 48, + 46, 49, 0, 171, 73, 83, + 71, 78, 44, 0, 0, 0, + 1, 0, 0, 0, 8, 0, + 0, 0, 32, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 0, + 0, 0, 0, 0, 3, 3, + 0, 0, 84, 69, 88, 67, + 79, 79, 82, 68, 0, 171, + 171, 171, 79, 83, 71, 78, + 44, 0, 0, 0, 1, 0, + 0, 0, 8, 0, 0, 0, + 32, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 3, 0, 0, 0, 0, 0, + 0, 0, 15, 0, 0, 0, + 83, 86, 95, 84, 97, 114, + 103, 101, 116, 0, 171, 171, + 83, 72, 68, 82, 100, 0, + 0, 0, 64, 0, 0, 0, + 25, 0, 0, 0, 90, 0, + 0, 3, 0, 96, 16, 0, + 0, 0, 0, 0, 88, 24, + 0, 4, 0, 112, 16, 0, + 0, 0, 0, 0, 85, 85, + 0, 0, 98, 16, 0, 3, + 50, 16, 16, 0, 0, 0, + 0, 0, 101, 0, 0, 3, + 242, 32, 16, 0, 0, 0, + 0, 0, 69, 0, 0, 9, + 242, 32, 16, 0, 0, 0, + 0, 0, 70, 16, 16, 0, + 0, 0, 0, 0, 70, 126, + 16, 0, 0, 0, 0, 0, + 0, 96, 16, 0, 0, 0, + 0, 0, 62, 0, 0, 1, + 83, 84, 65, 84, 116, 0, + 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0 +}; diff --git a/src/frontend-common/make_d3d_display_shader_bytecode.bat b/src/frontend-common/make_d3d_display_shader_bytecode.bat index 6bf21cc38..a6903029b 100644 --- a/src/frontend-common/make_d3d_display_shader_bytecode.bat +++ b/src/frontend-common/make_d3d_display_shader_bytecode.bat @@ -1,2 +1,3 @@ fxc /T vs_4_0 /E main /O3 /Fh display_vs.hlsl.h /Vn "static s_display_vs_bytecode" display_vs.hlsl -fxc /T ps_4_0 /E main /O3 /Fh display_ps.hlsl.h /Vn "static s_display_ps_bytecode" display_ps.hlsl \ No newline at end of file +fxc /T ps_4_0 /E main /O3 /Fh display_ps.hlsl.h /Vn "static s_display_ps_bytecode" display_ps.hlsl +fxc /T ps_4_0 /E main /O3 /D ALPHA=1 /Fh display_ps_alpha.hlsl.h /Vn "static s_display_ps_alpha_bytecode" display_ps.hlsl \ No newline at end of file