OpenGL: Max UBO checks

This commit is contained in:
Ameer J 2023-11-26 22:26:19 -05:00
parent f21340f7aa
commit 72aff2873d
10 changed files with 72 additions and 24 deletions

View File

@ -7,6 +7,7 @@
#include "shader_recompiler/backend/glasm/glasm_emit_context.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/profile.h"
#include "shader_recompiler/runtime_info.h"
#include "shader_recompiler/shader_info.h"
namespace Shader::Backend::GLASM {
@ -23,7 +24,14 @@ void GetCbuf(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU
}
if (binding.IsImmediate()) {
ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
const u32 binding_index{binding.U32()};
const u32 max_num_cbufs{ctx.runtime_info.max_num_cbufs};
if (binding_index >= max_num_cbufs) {
// cbuf index exceeds device limit
ctx.Add("MOV.S {},0;", ret);
return;
}
ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding_index, offset);
return;
}

View File

@ -37,6 +37,12 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
if (desc.count != 1) {
throw NotImplementedException("Constant buffer descriptor array");
}
if (cbuf_index >= runtime_info.max_num_cbufs) {
LOG_WARNING(Shader_GLASM, "Constant buffer binding index {} exceeds device limit of {}",
cbuf_index, runtime_info.max_num_cbufs);
++cbuf_index;
continue;
}
Add("CBUFFER c{}[]={{program.buffer[{}]}};", desc.index, cbuf_index);
++cbuf_index;
}

View File

@ -15,9 +15,10 @@ namespace Shader::Backend::GLSL {
[[nodiscard]] std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info,
IR::Program& program, Bindings& bindings);
[[nodiscard]] inline std::string EmitGLSL(const Profile& profile, IR::Program& program) {
[[nodiscard]] inline std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info,
IR::Program& program) {
Bindings binding;
return EmitGLSL(profile, {}, program, binding);
return EmitGLSL(profile, runtime_info, program, binding);
}
} // namespace Shader::Backend::GLSL

View File

@ -46,6 +46,15 @@ std::string ChooseCbuf(EmitContext& ctx, const IR::Value& binding, std::string_v
void GetCbuf(EmitContext& ctx, std::string_view ret, const IR::Value& binding,
const IR::Value& offset, u32 num_bits, std::string_view cast = {},
std::string_view bit_offset = {}) {
if (binding.IsImmediate()) {
const u32 binding_index{binding.U32()};
const u32 max_num_cbufs{ctx.runtime_info.max_num_cbufs};
if (binding_index >= max_num_cbufs) {
// cbuf index exceeds device limit
ctx.Add("{}=0u;", ret);
return;
}
}
const bool is_immediate{offset.IsImmediate()};
const bool component_indexing_bug{!is_immediate && ctx.profile.has_gl_component_indexing_bug};
if (is_immediate) {

View File

@ -430,6 +430,12 @@ void EmitContext::DefineConstantBuffers(Bindings& bindings) {
return;
}
for (const auto& desc : info.constant_buffer_descriptors) {
if (bindings.uniform_buffer >= runtime_info.max_num_cbufs) {
LOG_WARNING(Shader_GLSL, "Constant buffer binding index {} exceeds device limit of {}",
bindings.uniform_buffer, runtime_info.max_num_cbufs);
bindings.uniform_buffer += desc.count;
continue;
}
const auto cbuf_type{profile.has_gl_cbuf_ftou_bug ? "uvec4" : "vec4"};
header += fmt::format("layout(std140,binding={}) uniform {}_cbuf_{}{{{} {}_cbuf{}[{}];}};",
bindings.uniform_buffer, stage_name, desc.index, cbuf_type,

View File

@ -38,4 +38,10 @@ constexpr u32 RENDERAREA_LAYOUT_OFFSET = offsetof(RenderAreaLayout, render_area)
return EmitSPIRV(profile, {}, program, binding);
}
[[nodiscard]] inline std::vector<u32> EmitSPIRV(const Profile& profile,
const RuntimeInfo& runtime_info,
IR::Program& program) {
Bindings binding;
return EmitSPIRV(profile, runtime_info, program, binding);
}
} // namespace Shader::Backend::SPIRV

View File

@ -122,25 +122,24 @@ Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr,
if (!binding.IsImmediate()) {
return ctx.OpFunctionCall(result_type, indirect_func, ctx.Def(binding), buffer_offset);
}
const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr};
const bool is_float{UniformDefinitions::IsFloat(member_ptr)};
const Id zero_val{is_float ? ctx.Const(0.0f) : ctx.Const(0u)};
const u32 binding_index{binding.U32()};
const u32 max_num_cbufs{ctx.runtime_info.max_num_cbufs};
if (binding_index >= max_num_cbufs) {
// cbuf index exceeds device limit
return zero_val;
}
const Id cbuf{ctx.cbufs[binding_index].*member_ptr};
const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)};
const Id val = ctx.OpLoad(result_type, access_chain);
const Id val{ctx.OpLoad(result_type, access_chain)};
if (offset.IsImmediate() || !ctx.profile.has_broken_robust) {
return val;
}
const auto is_float = UniformDefinitions::IsFloat(member_ptr);
const auto num_elements = UniformDefinitions::NumElements(member_ptr);
const std::array zero_vec{
is_float ? ctx.Const(0.0f) : ctx.Const(0u),
is_float ? ctx.Const(0.0f) : ctx.Const(0u),
is_float ? ctx.Const(0.0f) : ctx.Const(0u),
is_float ? ctx.Const(0.0f) : ctx.Const(0u),
};
const Id cond = ctx.OpULessThanEqual(ctx.TypeBool(), buffer_offset, ctx.Const(0xFFFFu));
const Id zero = ctx.OpCompositeConstruct(result_type, std::span(zero_vec.data(), num_elements));
const auto num_elements{UniformDefinitions::NumElements(member_ptr)};
const std::array zero_vec{zero_val, zero_val, zero_val, zero_val};
const Id cond{ctx.OpULessThanEqual(ctx.TypeBool(), buffer_offset, ctx.Const(0xFFFFu))};
const Id zero{ctx.OpCompositeConstruct(result_type, std::span(zero_vec.data(), num_elements))};
return ctx.OpSelect(result_type, cond, val, zero);
}

View File

@ -278,6 +278,12 @@ void DefineConstBuffers(EmitContext& ctx, const Info& info, Id UniformDefinition
ctx.uniform_types.*member_type = uniform_type;
for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
if (desc.index + desc.count > ctx.runtime_info.max_num_cbufs) {
LOG_WARNING(Shader_SPIRV, "Constant buffer binding index {} exceeds device limit of {}",
desc.index, ctx.runtime_info.max_num_cbufs);
binding += desc.count;
continue;
}
const Id id{ctx.AddGlobalVariable(struct_pointer_type, spv::StorageClass::Uniform)};
ctx.Decorate(id, spv::Decoration::Binding, binding);
ctx.Decorate(id, spv::Decoration::DescriptorSet, 0U);

View File

@ -86,6 +86,9 @@ struct RuntimeInfo {
/// Transform feedback state for each varying
std::array<TransformFeedbackVarying, 256> xfb_varyings{};
u32 xfb_count{0};
/// Maximum number of UBO/CBUF bindings allowed by the host device
u32 max_num_cbufs{32};
};
} // namespace Shader

View File

@ -72,7 +72,8 @@ Shader::OutputTopology MaxwellToOutputTopology(Maxwell::PrimitiveTopology topolo
Shader::RuntimeInfo MakeRuntimeInfo(const GraphicsPipelineKey& key,
const Shader::IR::Program& program,
const Shader::IR::Program* previous_program,
bool glasm_use_storage_buffers, bool use_assembly_shaders) {
bool glasm_use_storage_buffers, bool use_assembly_shaders,
u32 max_num_cbufs) {
Shader::RuntimeInfo info;
if (previous_program) {
info.previous_stage_stores = previous_program->info.stores;
@ -152,6 +153,7 @@ Shader::RuntimeInfo MakeRuntimeInfo(const GraphicsPipelineKey& key,
break;
}
info.glasm_use_storage_buffers = glasm_use_storage_buffers;
info.max_num_cbufs = max_num_cbufs;
return info;
}
@ -520,8 +522,9 @@ std::unique_ptr<GraphicsPipeline> ShaderCache::CreateGraphicsPipeline(
const size_t stage_index{index - 1};
infos[stage_index] = &program.info;
const auto runtime_info{
MakeRuntimeInfo(key, program, previous_program, glasm_use_storage_buffers, use_glasm)};
const u32 max_num_cbufs{device.GetMaxUniformBuffers(program.stage)};
const auto runtime_info{MakeRuntimeInfo(
key, program, previous_program, glasm_use_storage_buffers, use_glasm, max_num_cbufs)};
switch (device.GetShaderBackend()) {
case Settings::ShaderBackend::Glsl:
ConvertLegacyToGeneric(program, runtime_info);
@ -578,20 +581,21 @@ std::unique_ptr<ComputePipeline> ShaderCache::CreateComputePipeline(
auto program{TranslateProgram(pools.inst, pools.block, env, cfg, host_info)};
const u32 num_storage_buffers{Shader::NumDescriptors(program.info.storage_buffers_descriptors)};
Shader::RuntimeInfo info;
Shader::RuntimeInfo info{};
info.glasm_use_storage_buffers = num_storage_buffers <= device.GetMaxGLASMStorageBufferBlocks();
info.max_num_cbufs = device.GetMaxUniformBuffers(program.stage);
std::string code{};
std::vector<u32> code_spirv;
switch (device.GetShaderBackend()) {
case Settings::ShaderBackend::Glsl:
code = EmitGLSL(profile, program);
code = EmitGLSL(profile, info, program);
break;
case Settings::ShaderBackend::Glasm:
code = EmitGLASM(profile, info, program);
break;
case Settings::ShaderBackend::SpirV:
code_spirv = EmitSPIRV(profile, program);
code_spirv = EmitSPIRV(profile, info, program);
break;
}