From 6d32cce2f5701a897ddd95bf702e5ae29ab1c546 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 5 Sep 2017 23:24:32 +1000 Subject: [PATCH 1/2] ShaderGen: Output uint when logic op is enabled for D3D ubershaders --- Source/Core/VideoCommon/UberShaderPixel.cpp | 32 ++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Source/Core/VideoCommon/UberShaderPixel.cpp b/Source/Core/VideoCommon/UberShaderPixel.cpp index 4e760408c5..0ba299260f 100644 --- a/Source/Core/VideoCommon/UberShaderPixel.cpp +++ b/Source/Core/VideoCommon/UberShaderPixel.cpp @@ -654,13 +654,19 @@ ShaderCode GenPixelShader(APIType ApiType, const ShaderHostConfig& host_config, if (early_depth && host_config.backend_early_z) out.Write("[earlydepthstencil]\n"); - out.Write("void main(\n" - " out float4 ocol0 : SV_Target0,\n" - " out float4 ocol1 : SV_Target1,\n" - " %s\n", - per_pixel_depth ? "\n out float depth : SV_Depth," : ""); + out.Write("void main(\n"); + if (uid_data->uint_output) + { + out.Write(" out uint4 ocol0 : SV_Target,\n"); + } + else + { + out.Write(" out float4 ocol0 : SV_Target0,\n" + " out float4 ocol1 : SV_Target1,\n"); + } + if (per_pixel_depth) + out.Write(" out float depth : SV_Depth,\n"); out.Write(" in float4 rawpos : SV_Position,\n"); - out.Write(" in %s float4 colors_0 : COLOR0,\n", GetInterpolationQualifier(msaa, ssaa)); out.Write(" in %s float4 colors_1 : COLOR1", GetInterpolationQualifier(msaa, ssaa)); @@ -1186,14 +1192,14 @@ ShaderCode GenPixelShader(APIType ApiType, const ShaderHostConfig& host_config, out.Write(" else\n" " ocol0.a = float(TevResult.a >> 2) / 63.0;\n" " \n"); - } - if (use_dual_source) - { - out.Write(" // Dest alpha override (dual source blending)\n" - " // Colors will be blended against the alpha from ocol1 and\n" - " // the alpha from ocol0 will be written to the framebuffer.\n" - " ocol1 = float4(0.0, 0.0, 0.0, float(TevResult.a) / 255.0);\n"); + if (use_dual_source) + { + out.Write(" // Dest alpha override (dual source blending)\n" + " // Colors will be blended against the alpha from ocol1 and\n" + " // the alpha from ocol0 will be written to the framebuffer.\n" + " ocol1 = float4(0.0, 0.0, 0.0, float(TevResult.a) / 255.0);\n"); + } } if (bounding_box) From 84f8ebd95fc70bc3482a53f31d5ebcc5266bb4b7 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 5 Sep 2017 23:33:15 +1000 Subject: [PATCH 2/2] VideoBackends: Clear uid bits that are unused for the current backend Currently, this is only the logic op bit, but this will be extended to the framebuffer fetch/blend modes. In the future, when/if we move to VideoCommon pipelines, this state will be part of the pipeline UID anyway, and we can mask it out in the backend by using a two-level map, so the shaders/programs are shared. --- Source/Core/VideoBackends/D3D/PixelShaderCache.cpp | 2 ++ Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp | 2 ++ Source/Core/VideoBackends/Vulkan/StateTracker.cpp | 5 ++++- Source/Core/VideoCommon/PixelShaderGen.cpp | 10 ++++++++++ Source/Core/VideoCommon/PixelShaderGen.h | 2 +- Source/Core/VideoCommon/UberShaderPixel.cpp | 10 ++++++++++ Source/Core/VideoCommon/UberShaderPixel.h | 1 + 7 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp b/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp index 07270f0e2a..326f840db1 100644 --- a/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp +++ b/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp @@ -593,6 +593,7 @@ bool PixelShaderCache::SetShader() return SetUberShader(); PixelShaderUid uid = GetPixelShaderUid(); + ClearUnusedPixelShaderUidBits(APIType::D3D, &uid); if (last_entry && uid == last_uid) { if (last_entry->pending) @@ -656,6 +657,7 @@ bool PixelShaderCache::SetShader() bool PixelShaderCache::SetUberShader() { UberShader::PixelShaderUid uid = UberShader::GetPixelShaderUid(); + UberShader::ClearUnusedPixelShaderUidBits(APIType::D3D, &uid); if (last_uber_entry && last_uber_uid == uid) { diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp index 6e91717ed5..5617a928a9 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp @@ -231,6 +231,7 @@ SHADER* ProgramShaderCache::SetShader(u32 primitive_type, const GLVertexFormat* uid.puid = GetPixelShaderUid(); uid.vuid = GetVertexShaderUid(); uid.guid = GetGeometryShaderUid(primitive_type); + ClearUnusedPixelShaderUidBits(APIType::OpenGL, &uid.puid); // Check if the shader is already set if (last_entry && uid == last_uid) @@ -298,6 +299,7 @@ SHADER* ProgramShaderCache::SetUberShader(u32 primitive_type, const GLVertexForm uid.puid = UberShader::GetPixelShaderUid(); uid.vuid = UberShader::GetVertexShaderUid(); uid.guid = GetGeometryShaderUid(primitive_type); + UberShader::ClearUnusedPixelShaderUidBits(APIType::OpenGL, &uid.puid); // We need to use the ubershader vertex format with all attributes enabled. // Otherwise, the NV driver can generate variants for the vertex shaders. diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp index 33baa86c4c..ae583374e8 100644 --- a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp +++ b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp @@ -338,8 +338,9 @@ bool StateTracker::CheckForShaderChanges(u32 gx_primitive_type) { VertexShaderUid vs_uid = GetVertexShaderUid(); PixelShaderUid ps_uid = GetPixelShaderUid(); - bool changed = false; + ClearUnusedPixelShaderUidBits(APIType::Vulkan, &ps_uid); + bool changed = false; bool use_ubershaders = g_ActiveConfig.bDisableSpecializedShaders; if (g_ActiveConfig.CanBackgroundCompileShaders() && !g_ActiveConfig.bDisableSpecializedShaders) { @@ -405,6 +406,7 @@ bool StateTracker::CheckForShaderChanges(u32 gx_primitive_type) } UberShader::PixelShaderUid uber_ps_uid = UberShader::GetPixelShaderUid(); + UberShader::ClearUnusedPixelShaderUidBits(APIType::Vulkan, &uber_ps_uid); VkShaderModule ps = g_shader_cache->GetPixelUberShaderForUid(uber_ps_uid); if (ps != m_pipeline_state.ps) { @@ -983,6 +985,7 @@ VkPipeline StateTracker::GetPipelineAndCacheUID() PipelineInfo uber_info = m_pipeline_state; UberShader::VertexShaderUid uber_vuid = UberShader::GetVertexShaderUid(); UberShader::PixelShaderUid uber_puid = UberShader::GetPixelShaderUid(); + UberShader::ClearUnusedPixelShaderUidBits(APIType::Vulkan, &uber_puid); uber_info.vs = g_shader_cache->GetVertexUberShaderForUid(uber_vuid); uber_info.ps = g_shader_cache->GetPixelUberShaderForUid(uber_puid); diff --git a/Source/Core/VideoCommon/PixelShaderGen.cpp b/Source/Core/VideoCommon/PixelShaderGen.cpp index 5e561456ab..990f5f938f 100644 --- a/Source/Core/VideoCommon/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/PixelShaderGen.cpp @@ -322,6 +322,16 @@ PixelShaderUid GetPixelShaderUid() return out; } +void ClearUnusedPixelShaderUidBits(APIType ApiType, PixelShaderUid* uid) +{ + pixel_shader_uid_data* uid_data = uid->GetUidData(); + + // OpenGL and Vulkan convert implicitly normalized color outputs to their uint representation. + // Therefore, it is not necessary to use a uint output on these backends. + if (ApiType != APIType::D3D) + uid_data->uint_output = 0; +} + void WritePixelShaderCommonHeader(ShaderCode& out, APIType ApiType, u32 num_texgens, bool per_pixel_lighting, bool bounding_box) { diff --git a/Source/Core/VideoCommon/PixelShaderGen.h b/Source/Core/VideoCommon/PixelShaderGen.h index 253dc59e22..b480777c41 100644 --- a/Source/Core/VideoCommon/PixelShaderGen.h +++ b/Source/Core/VideoCommon/PixelShaderGen.h @@ -162,5 +162,5 @@ ShaderCode GeneratePixelShaderCode(APIType ApiType, const ShaderHostConfig& host const pixel_shader_uid_data* uid_data); void WritePixelShaderCommonHeader(ShaderCode& out, APIType ApiType, u32 num_texgens, bool per_pixel_lighting, bool bounding_box); -ShaderCode GeneratePixelShaderCode(APIType ApiType, const pixel_shader_uid_data* uid_data); +void ClearUnusedPixelShaderUidBits(APIType ApiType, PixelShaderUid* uid); PixelShaderUid GetPixelShaderUid(); diff --git a/Source/Core/VideoCommon/UberShaderPixel.cpp b/Source/Core/VideoCommon/UberShaderPixel.cpp index 0ba299260f..494adca95d 100644 --- a/Source/Core/VideoCommon/UberShaderPixel.cpp +++ b/Source/Core/VideoCommon/UberShaderPixel.cpp @@ -29,6 +29,16 @@ PixelShaderUid GetPixelShaderUid() return out; } +void ClearUnusedPixelShaderUidBits(APIType ApiType, PixelShaderUid* uid) +{ + pixel_ubershader_uid_data* uid_data = uid->GetUidData(); + + // OpenGL and Vulkan convert implicitly normalized color outputs to their uint representation. + // Therefore, it is not necessary to use a uint output on these backends. + if (ApiType != APIType::D3D) + uid_data->uint_output = 0; +} + ShaderCode GenPixelShader(APIType ApiType, const ShaderHostConfig& host_config, const pixel_ubershader_uid_data* uid_data) { diff --git a/Source/Core/VideoCommon/UberShaderPixel.h b/Source/Core/VideoCommon/UberShaderPixel.h index 3a5a8f8435..5bacf68a43 100644 --- a/Source/Core/VideoCommon/UberShaderPixel.h +++ b/Source/Core/VideoCommon/UberShaderPixel.h @@ -29,4 +29,5 @@ ShaderCode GenPixelShader(APIType ApiType, const ShaderHostConfig& host_config, const pixel_ubershader_uid_data* uid_data); void EnumeratePixelShaderUids(const std::function& callback); +void ClearUnusedPixelShaderUidBits(APIType ApiType, PixelShaderUid* uid); }