Merge pull request #12185 from iwubcode/custom_shader_uniform_backend_support

VideoBackends / VideoCommon: add support for custom shader uniforms to D3D, Vulkan, OGL
This commit is contained in:
Admiral H. Curtiss
2023-10-15 00:21:56 +02:00
committed by GitHub
21 changed files with 237 additions and 86 deletions

View File

@ -19,7 +19,7 @@ namespace VideoCommon
// As pipelines encompass both shader UIDs and render states, changes to either of these should
// also increment the pipeline UID version. Incrementing the UID version will cause all UID
// caches to be invalidated.
constexpr u32 GX_PIPELINE_UID_VERSION = 7; // Last changed in PR 11859
constexpr u32 GX_PIPELINE_UID_VERSION = 8; // Last changed in PR 12185
struct GXPipelineUid
{

View File

@ -93,7 +93,7 @@ ShaderCode GenerateGeometryShaderCode(APIType api_type, const ShaderHostConfig&
// uniforms
if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
out.Write("UBO_BINDING(std140, 3) uniform GSBlock {{\n");
out.Write("UBO_BINDING(std140, 4) uniform GSBlock {{\n");
else
out.Write("cbuffer GSBlock {{\n");

View File

@ -3,6 +3,8 @@
#pragma once
#include <span>
#include "Common/CommonTypes.h"
#include "VideoCommon/ConstantManager.h"
@ -52,6 +54,10 @@ public:
PixelShaderConstants constants{};
bool dirty = false;
// Constants for custom shaders
std::span<u8> custom_constants;
bool custom_constants_dirty = false;
private:
bool m_fog_range_adjusted_changed = false;
bool m_viewport_changed = false;

View File

@ -52,7 +52,7 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config
if (vertex_loader)
{
out.Write("UBO_BINDING(std140, 3) uniform GSBlock {{\n");
out.Write("UBO_BINDING(std140, 4) uniform GSBlock {{\n");
out.Write("{}", s_geometry_shader_uniforms);
out.Write("}};\n");
}
@ -84,7 +84,7 @@ SSBO_BINDING(1) readonly restrict buffer Vertices {{
// D3D12 uses a root constant for this uniform, since it changes with every draw.
// D3D11 doesn't currently support dynamic vertex loader, and we'll have to figure something
// out for it if we want to support it in the future.
out.Write("UBO_BINDING(std140, 4) uniform DX_Constants {{\n"
out.Write("UBO_BINDING(std140, 5) uniform DX_Constants {{\n"
" uint base_vertex;\n"
"}};\n\n"
"uint GetVertexBaseOffset(uint vertex_id) {{\n"

View File

@ -595,6 +595,7 @@ void VertexManagerBase::Flush()
CustomPixelShaderContents custom_pixel_shader_contents;
std::optional<CustomPixelShader> custom_pixel_shader;
std::vector<std::string> custom_pixel_texture_names;
std::span<u8> custom_pixel_shader_uniforms;
for (int i = 0; i < texture_names.size(); i++)
{
const std::string& texture_name = texture_names[i];
@ -644,6 +645,12 @@ void VertexManagerBase::Flush()
// Now we can upload uniforms, as nothing else will override them.
geometry_shader_manager.SetConstants(m_current_primitive_type);
pixel_shader_manager.SetConstants();
if (!custom_pixel_shader_uniforms.empty() &&
pixel_shader_manager.custom_constants.data() != custom_pixel_shader_uniforms.data())
{
pixel_shader_manager.custom_constants_dirty = true;
}
pixel_shader_manager.custom_constants = custom_pixel_shader_uniforms;
UploadUniforms();
// Update the pipeline, or compile one if needed.
@ -1052,7 +1059,8 @@ void VertexManagerBase::OnEndFrame()
return;
// In order to reduce CPU readback latency, we want to kick a command buffer roughly halfway
// between the draw counters that invoked the readback, or every 250 draws, whichever is smaller.
// between the draw counters that invoked the readback, or every 250 draws, whichever is
// smaller.
if (g_ActiveConfig.iCommandBufferExecuteInterval > 0)
{
u32 last_draw_counter = 0;

View File

@ -96,14 +96,14 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho
if (uid_data->vs_expand != VSExpand::None)
{
out.Write("UBO_BINDING(std140, 3) uniform GSBlock {{\n");
out.Write("UBO_BINDING(std140, 4) uniform GSBlock {{\n");
out.Write("{}", s_geometry_shader_uniforms);
out.Write("}};\n");
if (api_type == APIType::D3D)
{
// D3D doesn't include the base vertex in SV_VertexID
out.Write("UBO_BINDING(std140, 4) uniform DX_Constants {{\n"
out.Write("UBO_BINDING(std140, 5) uniform DX_Constants {{\n"
" uint base_vertex;\n"
"}};\n\n");
}