From 656adee630cb1de738ed4dc6cf1cbc35af40f64f Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sun, 25 Jul 2021 22:26:23 -0300 Subject: [PATCH] spirv: Implement rescaling patching --- .../backend/spirv/emit_context.cpp | 35 +++++++++++++++++++ .../backend/spirv/emit_context.h | 5 +++ .../backend/spirv/emit_spirv.h | 5 ++- .../spirv/emit_spirv_context_get_set.cpp | 6 ++-- .../backend/spirv/emit_spirv_image.cpp | 26 ++++++++++++-- .../frontend/maxwell/translate_program.cpp | 3 ++ src/shader_recompiler/runtime_info.h | 2 ++ src/shader_recompiler/shader_info.h | 9 +++++ 8 files changed, 86 insertions(+), 5 deletions(-) diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 3c84e64666..222baa177f 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include #include "common/common_types.h" @@ -496,6 +498,7 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf DefineImages(program.info, image_binding); DefineAttributeMemAccess(program.info); DefineGlobalMemoryFunctions(program.info); + DefineRescalingInput(program.info); } EmitContext::~EmitContext() = default; @@ -996,6 +999,38 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) { define(&StorageDefinitions::U32x4, storage_types.U32x4, U32[4], sizeof(u32[4])); } +void EmitContext::DefineRescalingInput(const Info& info) { + if (!info.uses_rescaling_uniform) { + return; + } + boost::container::static_vector members{F32[1]}; + u32 member_index{0}; + const u32 num_texture_words{Common::DivCeil(runtime_info.num_textures, 32u)}; + if (runtime_info.num_textures > 0) { + rescaling_textures_type = TypeArray(U32[1], Const(num_texture_words)); + Decorate(rescaling_textures_type, spv::Decoration::ArrayStride, 4u); + members.push_back(rescaling_textures_type); + rescaling_textures_member_index = ++member_index; + } + const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))}; + Decorate(push_constant_struct, spv::Decoration::Block); + Name(push_constant_struct, "ResolutionInfo"); + MemberDecorate(push_constant_struct, 0u, spv::Decoration::Offset, 0u); + MemberName(push_constant_struct, 0u, "down_factor"); + if (runtime_info.num_textures > 0) { + MemberDecorate(push_constant_struct, rescaling_textures_member_index, + spv::Decoration::Offset, 4u); + MemberName(push_constant_struct, rescaling_textures_member_index, "rescaling_textures"); + } + const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)}; + rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant); + Name(rescaling_push_constants, "rescaling_push_constants"); + + if (profile.supported_spirv >= 0x00010400) { + interfaces.push_back(rescaling_push_constants); + } +} + void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { if (info.constant_buffer_descriptors.empty()) { return; diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index 112c52382a..a7917ac517 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h @@ -238,6 +238,10 @@ public: Id indexed_load_func{}; Id indexed_store_func{}; + Id rescaling_push_constants{}; + Id rescaling_textures_type{}; + u32 rescaling_textures_member_index{}; + Id local_memory{}; Id shared_memory_u8{}; @@ -314,6 +318,7 @@ private: void DefineImages(const Info& info, u32& binding); void DefineAttributeMemAccess(const Info& info); void DefineGlobalMemoryFunctions(const Info& info); + void DefineRescalingInput(const Info& info); void DefineInputs(const IR::Program& program); void DefineOutputs(const IR::Program& program); diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.h b/src/shader_recompiler/backend/spirv/emit_spirv.h index db0c935fe1..7b0d8d9800 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv.h @@ -20,8 +20,11 @@ namespace Shader::Backend::SPIRV { IR::Program& program, Bindings& bindings); [[nodiscard]] inline std::vector EmitSPIRV(const Profile& profile, IR::Program& program) { + RuntimeInfo runtime_info{}; + runtime_info.num_textures = Shader::NumDescriptors(program.info.texture_descriptors); + Bindings binding; - return EmitSPIRV(profile, {}, program, binding); + return EmitSPIRV(profile, runtime_info, program, binding); } } // namespace Shader::Backend::SPIRV diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 43f440dfb1..6bb791b036 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -527,8 +527,10 @@ Id EmitYDirection(EmitContext& ctx) { } Id EmitResolutionDownFactor(EmitContext& ctx) { - UNIMPLEMENTED(); - return ctx.Const(1.0f); + const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[1])}; + const Id pointer{ + ctx.OpAccessChain(pointer_type, ctx.rescaling_push_constants, ctx.u32_zero_value)}; + return ctx.OpLoad(ctx.F32[1], pointer); } Id EmitLoadLocal(EmitContext& ctx, Id word_offset) { diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp index 2f925cc3e7..7d7c0627ec 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp @@ -470,8 +470,30 @@ void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id ctx.OpImageWrite(Image(ctx, index, info), coords, color); } -Id EmitIsTextureScaled([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] const IR::Value& index) { - return ctx.false_value; +Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) { + const Id push_constant_u32{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.U32[1])}; + const Id member_index{ctx.Const(ctx.rescaling_textures_member_index)}; + Id bit{}; + if (index.IsImmediate()) { + // Use BitwiseAnd instead of BitfieldExtract for better codegen on Nvidia OpenGL. + // LOP32I.NZ is used to set the predicate rather than BFE+ISETP. + const u32 index_value{index.U32()}; + const Id word_index{ctx.Const(index_value / 32)}; + const Id bit_index_mask{ctx.Const(1u << (index_value % 32))}; + const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants, + member_index, word_index)}; + const Id word{ctx.OpLoad(ctx.U32[1], pointer)}; + bit = ctx.OpBitwiseAnd(ctx.U32[1], word, bit_index_mask); + } else { + const Id index_value{ctx.Def(index)}; + const Id word_index{ctx.OpShiftRightArithmetic(ctx.U32[1], index_value, ctx.Const(5u))}; + const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants, + member_index, word_index)}; + const Id word{ctx.OpLoad(ctx.U32[1], pointer)}; + const Id bit_index{ctx.OpBitwiseAnd(ctx.U32[1], index_value, ctx.Const(31u))}; + bit = ctx.OpBitFieldUExtract(ctx.U32[1], index_value, bit_index, ctx.Const(1u)); + } + return ctx.OpINotEqual(ctx.U1, bit, ctx.u32_zero_value); } } // namespace Shader::Backend::SPIRV diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp index 2fc542f0eb..795f5cf08c 100644 --- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp +++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp @@ -178,6 +178,9 @@ IR::Program TranslateProgram(ObjectPool& inst_pool, ObjectPool generic_input_types{}; VaryingState previous_stage_stores; + u32 num_textures{}; + bool convert_depth_mode{}; bool force_early_z{}; diff --git a/src/shader_recompiler/shader_info.h b/src/shader_recompiler/shader_info.h index 7bac9e2cdc..9f375c30e5 100644 --- a/src/shader_recompiler/shader_info.h +++ b/src/shader_recompiler/shader_info.h @@ -191,4 +191,13 @@ struct Info { ImageDescriptors image_descriptors; }; +template +u32 NumDescriptors(const Descriptors& descriptors) { + u32 num{}; + for (const auto& desc : descriptors) { + num += desc.count; + } + return num; +} + } // namespace Shader