From b1d9e13d75ecab111dae12a328c5a7f8933277dc Mon Sep 17 00:00:00 2001 From: iwubcode Date: Tue, 12 Dec 2023 17:56:26 -0600 Subject: [PATCH] VideoCommon: prepare graphics mods for custom shader material data --- .../Runtime/GraphicsModActionData.h | 2 ++ Source/Core/VideoCommon/PixelShaderGen.cpp | 13 +++++++++++-- Source/Core/VideoCommon/PixelShaderGen.h | 3 ++- Source/Core/VideoCommon/ShaderGenCommon.h | 1 + Source/Core/VideoCommon/UberShaderPixel.cpp | 2 +- Source/Core/VideoCommon/VertexManagerBase.cpp | 3 ++- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Source/Core/VideoCommon/GraphicsModSystem/Runtime/GraphicsModActionData.h b/Source/Core/VideoCommon/GraphicsModSystem/Runtime/GraphicsModActionData.h index a9efb6a0dd..1b9115e6d5 100644 --- a/Source/Core/VideoCommon/GraphicsModSystem/Runtime/GraphicsModActionData.h +++ b/Source/Core/VideoCommon/GraphicsModSystem/Runtime/GraphicsModActionData.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -20,6 +21,7 @@ struct DrawStarted u32 texture_unit; bool* skip; std::optional* custom_pixel_shader; + std::span* material_uniform_buffer; }; struct EFB diff --git a/Source/Core/VideoCommon/PixelShaderGen.cpp b/Source/Core/VideoCommon/PixelShaderGen.cpp index 69fdc36efc..fff2374531 100644 --- a/Source/Core/VideoCommon/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/PixelShaderGen.cpp @@ -345,7 +345,8 @@ void ClearUnusedPixelShaderUidBits(APIType api_type, const ShaderHostConfig& hos } void WritePixelShaderCommonHeader(ShaderCode& out, APIType api_type, - const ShaderHostConfig& host_config, bool bounding_box) + const ShaderHostConfig& host_config, bool bounding_box, + const CustomPixelShaderContents& custom_details) { // dot product for integer vectors out.Write("int idot(int3 x, int3 y)\n" @@ -426,6 +427,14 @@ void WritePixelShaderCommonHeader(ShaderCode& out, APIType api_type, out.Write("}};\n"); } + if (!custom_details.shaders.empty() && + !custom_details.shaders.back().material_uniform_block.empty()) + { + out.Write("UBO_BINDING(std140, 3) uniform CustomShaderBlock {{\n"); + out.Write("{}", custom_details.shaders.back().material_uniform_block); + out.Write("}} custom_uniforms;\n"); + } + if (bounding_box) { out.Write("SSBO_BINDING(0) coherent buffer BBox {{\n" @@ -907,7 +916,7 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos // Stuff that is shared between ubershaders and pixelgen. WriteBitfieldExtractHeader(out, api_type, host_config); - WritePixelShaderCommonHeader(out, api_type, host_config, uid_data->bounding_box); + WritePixelShaderCommonHeader(out, api_type, host_config, uid_data->bounding_box, custom_details); // Custom shader details WriteCustomShaderStructDef(&out, uid_data->genMode_numtexgens); diff --git a/Source/Core/VideoCommon/PixelShaderGen.h b/Source/Core/VideoCommon/PixelShaderGen.h index 456c72c2cd..e5dd43d754 100644 --- a/Source/Core/VideoCommon/PixelShaderGen.h +++ b/Source/Core/VideoCommon/PixelShaderGen.h @@ -165,7 +165,8 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos const pixel_shader_uid_data* uid_data, const CustomPixelShaderContents& custom_details); void WritePixelShaderCommonHeader(ShaderCode& out, APIType api_type, - const ShaderHostConfig& host_config, bool bounding_box); + const ShaderHostConfig& host_config, bool bounding_box, + const CustomPixelShaderContents& custom_details); void ClearUnusedPixelShaderUidBits(APIType api_type, const ShaderHostConfig& host_config, PixelShaderUid* uid); PixelShaderUid GetPixelShaderUid(); diff --git a/Source/Core/VideoCommon/ShaderGenCommon.h b/Source/Core/VideoCommon/ShaderGenCommon.h index 60e4b3cef9..48f3d3d9bc 100644 --- a/Source/Core/VideoCommon/ShaderGenCommon.h +++ b/Source/Core/VideoCommon/ShaderGenCommon.h @@ -334,6 +334,7 @@ constexpr std::string_view CUSTOM_PIXELSHADER_COLOR_FUNC = "customShaderColor"; struct CustomPixelShader { std::string custom_shader; + std::string material_uniform_block; bool operator==(const CustomPixelShader& other) const = default; }; diff --git a/Source/Core/VideoCommon/UberShaderPixel.cpp b/Source/Core/VideoCommon/UberShaderPixel.cpp index 7e8c07cfb8..a4874fd388 100644 --- a/Source/Core/VideoCommon/UberShaderPixel.cpp +++ b/Source/Core/VideoCommon/UberShaderPixel.cpp @@ -330,7 +330,7 @@ ShaderCode GenPixelShader(APIType api_type, const ShaderHostConfig& host_config, out.Write("// {}\n", *uid_data); WriteBitfieldExtractHeader(out, api_type, host_config); - WritePixelShaderCommonHeader(out, api_type, host_config, bounding_box); + WritePixelShaderCommonHeader(out, api_type, host_config, bounding_box, custom_details); WriteCustomShaderStructDef(&out, numTexgen); for (std::size_t i = 0; i < custom_details.shaders.size(); i++) { diff --git a/Source/Core/VideoCommon/VertexManagerBase.cpp b/Source/Core/VideoCommon/VertexManagerBase.cpp index ac4446abba..b2f7dc87dd 100644 --- a/Source/Core/VideoCommon/VertexManagerBase.cpp +++ b/Source/Core/VideoCommon/VertexManagerBase.cpp @@ -603,7 +603,8 @@ void VertexManagerBase::Flush() const std::string& texture_name = texture_names[i]; const u32 texture_unit = texture_units[i]; bool skip = false; - GraphicsModActionData::DrawStarted draw_started{texture_unit, &skip, &custom_pixel_shader}; + GraphicsModActionData::DrawStarted draw_started{texture_unit, &skip, &custom_pixel_shader, + &custom_pixel_shader_uniforms}; for (const auto& action : g_graphics_mod_manager->GetDrawStartedActions(texture_name)) { action->OnDrawStarted(&draw_started);