diff options
Diffstat (limited to 'src/shader_recompiler')
10 files changed, 195 insertions, 71 deletions
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.h b/src/shader_recompiler/backend/spirv/emit_spirv.h index b412957c7..2b360e073 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv.h | |||
| @@ -22,7 +22,7 @@ constexpr u32 NUM_TEXTURE_AND_IMAGE_SCALING_WORDS = | |||
| 22 | struct RescalingLayout { | 22 | struct RescalingLayout { |
| 23 | alignas(16) std::array<u32, NUM_TEXTURE_SCALING_WORDS> rescaling_textures; | 23 | alignas(16) std::array<u32, NUM_TEXTURE_SCALING_WORDS> rescaling_textures; |
| 24 | alignas(16) std::array<u32, NUM_IMAGE_SCALING_WORDS> rescaling_images; | 24 | alignas(16) std::array<u32, NUM_IMAGE_SCALING_WORDS> rescaling_images; |
| 25 | alignas(16) u32 down_factor; | 25 | u32 down_factor; |
| 26 | }; | 26 | }; |
| 27 | constexpr u32 RESCALING_LAYOUT_WORDS_OFFSET = offsetof(RescalingLayout, rescaling_textures); | 27 | constexpr u32 RESCALING_LAYOUT_WORDS_OFFSET = offsetof(RescalingLayout, rescaling_textures); |
| 28 | constexpr u32 RESCALING_LAYOUT_DOWN_FACTOR_OFFSET = offsetof(RescalingLayout, down_factor); | 28 | constexpr u32 RESCALING_LAYOUT_DOWN_FACTOR_OFFSET = offsetof(RescalingLayout, down_factor); |
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 8ea730c80..80b4bbd27 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 | |||
| @@ -123,34 +123,36 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { | |||
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr, u32 element_size, | 125 | Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr, u32 element_size, |
| 126 | const IR::Value& binding, const IR::Value& offset) { | 126 | const IR::Value& binding, const IR::Value& offset, const Id indirect_func) { |
| 127 | Id buffer_offset; | ||
| 128 | const Id uniform_type{ctx.uniform_types.*member_ptr}; | ||
| 129 | if (offset.IsImmediate()) { | ||
| 130 | // Hardware been proved to read the aligned offset (e.g. LDC.U32 at 6 will read offset 4) | ||
| 131 | const Id imm_offset{ctx.Const(offset.U32() / element_size)}; | ||
| 132 | buffer_offset = imm_offset; | ||
| 133 | } else if (element_size > 1) { | ||
| 134 | const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))}; | ||
| 135 | const Id shift{ctx.Const(log2_element_size)}; | ||
| 136 | buffer_offset = ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), shift); | ||
| 137 | } else { | ||
| 138 | buffer_offset = ctx.Def(offset); | ||
| 139 | } | ||
| 127 | if (!binding.IsImmediate()) { | 140 | if (!binding.IsImmediate()) { |
| 128 | throw NotImplementedException("Constant buffer indexing"); | 141 | return ctx.OpFunctionCall(result_type, indirect_func, ctx.Def(binding), buffer_offset); |
| 129 | } | 142 | } |
| 130 | const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr}; | 143 | const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr}; |
| 131 | const Id uniform_type{ctx.uniform_types.*member_ptr}; | 144 | const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)}; |
| 132 | if (!offset.IsImmediate()) { | ||
| 133 | Id index{ctx.Def(offset)}; | ||
| 134 | if (element_size > 1) { | ||
| 135 | const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))}; | ||
| 136 | const Id shift{ctx.Const(log2_element_size)}; | ||
| 137 | index = ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), shift); | ||
| 138 | } | ||
| 139 | const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, index)}; | ||
| 140 | return ctx.OpLoad(result_type, access_chain); | ||
| 141 | } | ||
| 142 | // Hardware been proved to read the aligned offset (e.g. LDC.U32 at 6 will read offset 4) | ||
| 143 | const Id imm_offset{ctx.Const(offset.U32() / element_size)}; | ||
| 144 | const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, imm_offset)}; | ||
| 145 | return ctx.OpLoad(result_type, access_chain); | 145 | return ctx.OpLoad(result_type, access_chain); |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 148 | Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 149 | return GetCbuf(ctx, ctx.U32[1], &UniformDefinitions::U32, sizeof(u32), binding, offset); | 149 | return GetCbuf(ctx, ctx.U32[1], &UniformDefinitions::U32, sizeof(u32), binding, offset, |
| 150 | ctx.load_const_func_u32); | ||
| 150 | } | 151 | } |
| 151 | 152 | ||
| 152 | Id GetCbufU32x4(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 153 | Id GetCbufU32x4(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 153 | return GetCbuf(ctx, ctx.U32[4], &UniformDefinitions::U32x4, sizeof(u32[4]), binding, offset); | 154 | return GetCbuf(ctx, ctx.U32[4], &UniformDefinitions::U32x4, sizeof(u32[4]), binding, offset, |
| 155 | ctx.load_const_func_u32x4); | ||
| 154 | } | 156 | } |
| 155 | 157 | ||
| 156 | Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 index_offset) { | 158 | Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 index_offset) { |
| @@ -201,7 +203,8 @@ void EmitGetIndirectBranchVariable(EmitContext&) { | |||
| 201 | 203 | ||
| 202 | Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 204 | Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 203 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { | 205 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { |
| 204 | const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset)}; | 206 | const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset, |
| 207 | ctx.load_const_func_u8)}; | ||
| 205 | return ctx.OpUConvert(ctx.U32[1], load); | 208 | return ctx.OpUConvert(ctx.U32[1], load); |
| 206 | } | 209 | } |
| 207 | Id element{}; | 210 | Id element{}; |
| @@ -217,7 +220,8 @@ Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& of | |||
| 217 | 220 | ||
| 218 | Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 221 | Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 219 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { | 222 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { |
| 220 | const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset)}; | 223 | const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset, |
| 224 | ctx.load_const_func_u8)}; | ||
| 221 | return ctx.OpSConvert(ctx.U32[1], load); | 225 | return ctx.OpSConvert(ctx.U32[1], load); |
| 222 | } | 226 | } |
| 223 | Id element{}; | 227 | Id element{}; |
| @@ -233,8 +237,8 @@ Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& of | |||
| 233 | 237 | ||
| 234 | Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 238 | Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 235 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) { | 239 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) { |
| 236 | const Id load{ | 240 | const Id load{GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset, |
| 237 | GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset)}; | 241 | ctx.load_const_func_u16)}; |
| 238 | return ctx.OpUConvert(ctx.U32[1], load); | 242 | return ctx.OpUConvert(ctx.U32[1], load); |
| 239 | } | 243 | } |
| 240 | Id element{}; | 244 | Id element{}; |
| @@ -250,8 +254,8 @@ Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& o | |||
| 250 | 254 | ||
| 251 | Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 255 | Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 252 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) { | 256 | if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) { |
| 253 | const Id load{ | 257 | const Id load{GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset, |
| 254 | GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset)}; | 258 | ctx.load_const_func_u16)}; |
| 255 | return ctx.OpSConvert(ctx.U32[1], load); | 259 | return ctx.OpSConvert(ctx.U32[1], load); |
| 256 | } | 260 | } |
| 257 | Id element{}; | 261 | Id element{}; |
| @@ -276,7 +280,8 @@ Id EmitGetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& o | |||
| 276 | 280 | ||
| 277 | Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 281 | Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 278 | if (ctx.profile.support_descriptor_aliasing) { | 282 | if (ctx.profile.support_descriptor_aliasing) { |
| 279 | return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset); | 283 | return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset, |
| 284 | ctx.load_const_func_f32); | ||
| 280 | } else { | 285 | } else { |
| 281 | const Id vector{GetCbufU32x4(ctx, binding, offset)}; | 286 | const Id vector{GetCbufU32x4(ctx, binding, offset)}; |
| 282 | return ctx.OpBitcast(ctx.F32[1], GetCbufElement(ctx, vector, offset, 0u)); | 287 | return ctx.OpBitcast(ctx.F32[1], GetCbufElement(ctx, vector, offset, 0u)); |
| @@ -285,8 +290,8 @@ Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& o | |||
| 285 | 290 | ||
| 286 | Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { | 291 | Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { |
| 287 | if (ctx.profile.support_descriptor_aliasing) { | 292 | if (ctx.profile.support_descriptor_aliasing) { |
| 288 | return GetCbuf(ctx, ctx.U32[2], &UniformDefinitions::U32x2, sizeof(u32[2]), binding, | 293 | return GetCbuf(ctx, ctx.U32[2], &UniformDefinitions::U32x2, sizeof(u32[2]), binding, offset, |
| 289 | offset); | 294 | ctx.load_const_func_u32x2); |
| 290 | } else { | 295 | } else { |
| 291 | const Id vector{GetCbufU32x4(ctx, binding, offset)}; | 296 | const Id vector{GetCbufU32x4(ctx, binding, offset)}; |
| 292 | return ctx.OpCompositeConstruct(ctx.U32[2], GetCbufElement(ctx, vector, offset, 0u), | 297 | return ctx.OpCompositeConstruct(ctx.U32[2], GetCbufElement(ctx, vector, offset, 0u), |
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp index cd90c084a..aa5b6c9b7 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp | |||
| @@ -464,6 +464,7 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf | |||
| 464 | DefineSharedMemory(program); | 464 | DefineSharedMemory(program); |
| 465 | DefineSharedMemoryFunctions(program); | 465 | DefineSharedMemoryFunctions(program); |
| 466 | DefineConstantBuffers(program.info, uniform_binding); | 466 | DefineConstantBuffers(program.info, uniform_binding); |
| 467 | DefineConstantBufferIndirectFunctions(program.info); | ||
| 467 | DefineStorageBuffers(program.info, storage_binding); | 468 | DefineStorageBuffers(program.info, storage_binding); |
| 468 | DefineTextureBuffers(program.info, texture_binding); | 469 | DefineTextureBuffers(program.info, texture_binding); |
| 469 | DefineImageBuffers(program.info, image_binding); | 470 | DefineImageBuffers(program.info, image_binding); |
| @@ -993,7 +994,7 @@ void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { | |||
| 993 | } | 994 | } |
| 994 | return; | 995 | return; |
| 995 | } | 996 | } |
| 996 | IR::Type types{info.used_constant_buffer_types}; | 997 | IR::Type types{info.used_constant_buffer_types | info.used_indirect_cbuf_types}; |
| 997 | if (True(types & IR::Type::U8)) { | 998 | if (True(types & IR::Type::U8)) { |
| 998 | if (profile.support_int8) { | 999 | if (profile.support_int8) { |
| 999 | DefineConstBuffers(*this, info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8)); | 1000 | DefineConstBuffers(*this, info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8)); |
| @@ -1027,6 +1028,62 @@ void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { | |||
| 1027 | binding += static_cast<u32>(info.constant_buffer_descriptors.size()); | 1028 | binding += static_cast<u32>(info.constant_buffer_descriptors.size()); |
| 1028 | } | 1029 | } |
| 1029 | 1030 | ||
| 1031 | void EmitContext::DefineConstantBufferIndirectFunctions(const Info& info) { | ||
| 1032 | if (!info.uses_cbuf_indirect) { | ||
| 1033 | return; | ||
| 1034 | } | ||
| 1035 | const auto make_accessor{[&](Id buffer_type, Id UniformDefinitions::*member_ptr) { | ||
| 1036 | const Id func_type{TypeFunction(buffer_type, U32[1], U32[1])}; | ||
| 1037 | const Id func{OpFunction(buffer_type, spv::FunctionControlMask::MaskNone, func_type)}; | ||
| 1038 | const Id binding{OpFunctionParameter(U32[1])}; | ||
| 1039 | const Id offset{OpFunctionParameter(U32[1])}; | ||
| 1040 | |||
| 1041 | AddLabel(); | ||
| 1042 | |||
| 1043 | const Id merge_label{OpLabel()}; | ||
| 1044 | const Id uniform_type{uniform_types.*member_ptr}; | ||
| 1045 | |||
| 1046 | std::array<Id, Info::MAX_CBUFS> buf_labels; | ||
| 1047 | std::array<Sirit::Literal, Info::MAX_CBUFS> buf_literals; | ||
| 1048 | for (u32 i = 0; i < Info::MAX_CBUFS; i++) { | ||
| 1049 | buf_labels[i] = OpLabel(); | ||
| 1050 | buf_literals[i] = Sirit::Literal{i}; | ||
| 1051 | } | ||
| 1052 | OpSelectionMerge(merge_label, spv::SelectionControlMask::MaskNone); | ||
| 1053 | OpSwitch(binding, buf_labels[0], buf_literals, buf_labels); | ||
| 1054 | for (u32 i = 0; i < Info::MAX_CBUFS; i++) { | ||
| 1055 | AddLabel(buf_labels[i]); | ||
| 1056 | const Id cbuf{cbufs[i].*member_ptr}; | ||
| 1057 | const Id access_chain{OpAccessChain(uniform_type, cbuf, u32_zero_value, offset)}; | ||
| 1058 | const Id result{OpLoad(buffer_type, access_chain)}; | ||
| 1059 | OpReturnValue(result); | ||
| 1060 | } | ||
| 1061 | AddLabel(merge_label); | ||
| 1062 | OpUnreachable(); | ||
| 1063 | OpFunctionEnd(); | ||
| 1064 | return func; | ||
| 1065 | }}; | ||
| 1066 | IR::Type types{info.used_indirect_cbuf_types}; | ||
| 1067 | if (True(types & IR::Type::U8)) { | ||
| 1068 | load_const_func_u8 = make_accessor(U8, &UniformDefinitions::U8); | ||
| 1069 | } | ||
| 1070 | if (True(types & IR::Type::U16)) { | ||
| 1071 | load_const_func_u16 = make_accessor(U16, &UniformDefinitions::U16); | ||
| 1072 | } | ||
| 1073 | if (True(types & IR::Type::F32)) { | ||
| 1074 | load_const_func_f32 = make_accessor(F32[1], &UniformDefinitions::F32); | ||
| 1075 | } | ||
| 1076 | if (True(types & IR::Type::U32)) { | ||
| 1077 | load_const_func_u32 = make_accessor(U32[1], &UniformDefinitions::U32); | ||
| 1078 | } | ||
| 1079 | if (True(types & IR::Type::U32x2)) { | ||
| 1080 | load_const_func_u32x2 = make_accessor(U32[2], &UniformDefinitions::U32x2); | ||
| 1081 | } | ||
| 1082 | if (True(types & IR::Type::U32x4)) { | ||
| 1083 | load_const_func_u32x4 = make_accessor(U32[4], &UniformDefinitions::U32x4); | ||
| 1084 | } | ||
| 1085 | } | ||
| 1086 | |||
| 1030 | void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) { | 1087 | void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) { |
| 1031 | if (info.storage_buffers_descriptors.empty()) { | 1088 | if (info.storage_buffers_descriptors.empty()) { |
| 1032 | return; | 1089 | return; |
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h index f87138f7e..906a1dc2c 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h | |||
| @@ -294,6 +294,13 @@ public: | |||
| 294 | 294 | ||
| 295 | std::vector<Id> interfaces; | 295 | std::vector<Id> interfaces; |
| 296 | 296 | ||
| 297 | Id load_const_func_u8{}; | ||
| 298 | Id load_const_func_u16{}; | ||
| 299 | Id load_const_func_u32{}; | ||
| 300 | Id load_const_func_f32{}; | ||
| 301 | Id load_const_func_u32x2{}; | ||
| 302 | Id load_const_func_u32x4{}; | ||
| 303 | |||
| 297 | private: | 304 | private: |
| 298 | void DefineCommonTypes(const Info& info); | 305 | void DefineCommonTypes(const Info& info); |
| 299 | void DefineCommonConstants(); | 306 | void DefineCommonConstants(); |
| @@ -302,6 +309,7 @@ private: | |||
| 302 | void DefineSharedMemory(const IR::Program& program); | 309 | void DefineSharedMemory(const IR::Program& program); |
| 303 | void DefineSharedMemoryFunctions(const IR::Program& program); | 310 | void DefineSharedMemoryFunctions(const IR::Program& program); |
| 304 | void DefineConstantBuffers(const Info& info, u32& binding); | 311 | void DefineConstantBuffers(const Info& info, u32& binding); |
| 312 | void DefineConstantBufferIndirectFunctions(const Info& info); | ||
| 305 | void DefineStorageBuffers(const Info& info, u32& binding); | 313 | void DefineStorageBuffers(const Info& info, u32& binding); |
| 306 | void DefineTextureBuffers(const Info& info, u32& binding); | 314 | void DefineTextureBuffers(const Info& info, u32& binding); |
| 307 | void DefineImageBuffers(const Info& info, u32& binding); | 315 | void DefineImageBuffers(const Info& info, u32& binding); |
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/load_constant.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/load_constant.cpp index 2300088e3..8007a4d46 100644 --- a/src/shader_recompiler/frontend/maxwell/translate/impl/load_constant.cpp +++ b/src/shader_recompiler/frontend/maxwell/translate/impl/load_constant.cpp | |||
| @@ -11,10 +11,20 @@ namespace Shader::Maxwell { | |||
| 11 | using namespace LDC; | 11 | using namespace LDC; |
| 12 | namespace { | 12 | namespace { |
| 13 | std::pair<IR::U32, IR::U32> Slot(IR::IREmitter& ir, Mode mode, const IR::U32& imm_index, | 13 | std::pair<IR::U32, IR::U32> Slot(IR::IREmitter& ir, Mode mode, const IR::U32& imm_index, |
| 14 | const IR::U32& reg, const IR::U32& imm) { | 14 | const IR::U32& reg, const IR::U32& imm_offset) { |
| 15 | switch (mode) { | 15 | switch (mode) { |
| 16 | case Mode::Default: | 16 | case Mode::Default: |
| 17 | return {imm_index, ir.IAdd(reg, imm)}; | 17 | return {imm_index, ir.IAdd(reg, imm_offset)}; |
| 18 | case Mode::IS: { | ||
| 19 | // Segmented addressing mode | ||
| 20 | // Ra+imm_offset points into a flat mapping of const buffer | ||
| 21 | // address space | ||
| 22 | const IR::U32 address{ir.IAdd(reg, imm_offset)}; | ||
| 23 | const IR::U32 index{ir.BitFieldExtract(address, ir.Imm32(16), ir.Imm32(16))}; | ||
| 24 | const IR::U32 offset{ir.BitFieldExtract(address, ir.Imm32(0), ir.Imm32(16))}; | ||
| 25 | |||
| 26 | return {ir.IAdd(index, imm_index), offset}; | ||
| 27 | } | ||
| 18 | default: | 28 | default: |
| 19 | break; | 29 | break; |
| 20 | } | 30 | } |
diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp index 248ad3ced..b22725584 100644 --- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp +++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp | |||
| @@ -212,11 +212,11 @@ IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Blo | |||
| 212 | } | 212 | } |
| 213 | Optimization::SsaRewritePass(program); | 213 | Optimization::SsaRewritePass(program); |
| 214 | 214 | ||
| 215 | Optimization::ConstantPropagationPass(program); | ||
| 216 | |||
| 215 | Optimization::GlobalMemoryToStorageBufferPass(program); | 217 | Optimization::GlobalMemoryToStorageBufferPass(program); |
| 216 | Optimization::TexturePass(env, program); | 218 | Optimization::TexturePass(env, program); |
| 217 | 219 | ||
| 218 | Optimization::ConstantPropagationPass(program); | ||
| 219 | |||
| 220 | if (Settings::values.resolution_info.active) { | 220 | if (Settings::values.resolution_info.active) { |
| 221 | Optimization::RescalingPass(program); | 221 | Optimization::RescalingPass(program); |
| 222 | } | 222 | } |
diff --git a/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp b/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp index bfd2ae650..0b2c60842 100644 --- a/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp +++ b/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp | |||
| @@ -29,6 +29,46 @@ void AddConstantBufferDescriptor(Info& info, u32 index, u32 count) { | |||
| 29 | }); | 29 | }); |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | void AddRegisterIndexedLdc(Info& info) { | ||
| 33 | info.uses_cbuf_indirect = true; | ||
| 34 | |||
| 35 | // The shader can use any possible constant buffer | ||
| 36 | info.constant_buffer_mask = (1 << Info::MAX_CBUFS) - 1; | ||
| 37 | |||
| 38 | auto& cbufs{info.constant_buffer_descriptors}; | ||
| 39 | cbufs.clear(); | ||
| 40 | for (u32 i = 0; i < Info::MAX_CBUFS; i++) { | ||
| 41 | cbufs.push_back(ConstantBufferDescriptor{.index = i, .count = 1}); | ||
| 42 | |||
| 43 | // The shader can use any possible access size | ||
| 44 | info.constant_buffer_used_sizes[i] = 0x10'000; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | u32 GetElementSize(IR::Type& used_type, Shader::IR::Opcode opcode) { | ||
| 49 | switch (opcode) { | ||
| 50 | case IR::Opcode::GetCbufU8: | ||
| 51 | case IR::Opcode::GetCbufS8: | ||
| 52 | used_type |= IR::Type::U8; | ||
| 53 | return 1; | ||
| 54 | case IR::Opcode::GetCbufU16: | ||
| 55 | case IR::Opcode::GetCbufS16: | ||
| 56 | used_type |= IR::Type::U16; | ||
| 57 | return 2; | ||
| 58 | case IR::Opcode::GetCbufU32: | ||
| 59 | used_type |= IR::Type::U32; | ||
| 60 | return 4; | ||
| 61 | case IR::Opcode::GetCbufF32: | ||
| 62 | used_type |= IR::Type::F32; | ||
| 63 | return 4; | ||
| 64 | case IR::Opcode::GetCbufU32x2: | ||
| 65 | used_type |= IR::Type::U32x2; | ||
| 66 | return 8; | ||
| 67 | default: | ||
| 68 | throw InvalidArgument("Invalid opcode {}", opcode); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 32 | void GetPatch(Info& info, IR::Patch patch) { | 72 | void GetPatch(Info& info, IR::Patch patch) { |
| 33 | if (!IR::IsGeneric(patch)) { | 73 | if (!IR::IsGeneric(patch)) { |
| 34 | throw NotImplementedException("Reading non-generic patch {}", patch); | 74 | throw NotImplementedException("Reading non-generic patch {}", patch); |
| @@ -463,42 +503,18 @@ void VisitUsages(Info& info, IR::Inst& inst) { | |||
| 463 | case IR::Opcode::GetCbufU32x2: { | 503 | case IR::Opcode::GetCbufU32x2: { |
| 464 | const IR::Value index{inst.Arg(0)}; | 504 | const IR::Value index{inst.Arg(0)}; |
| 465 | const IR::Value offset{inst.Arg(1)}; | 505 | const IR::Value offset{inst.Arg(1)}; |
| 466 | if (!index.IsImmediate()) { | 506 | if (index.IsImmediate()) { |
| 467 | throw NotImplementedException("Constant buffer with non-immediate index"); | 507 | AddConstantBufferDescriptor(info, index.U32(), 1); |
| 468 | } | 508 | u32 element_size = GetElementSize(info.used_constant_buffer_types, inst.GetOpcode()); |
| 469 | AddConstantBufferDescriptor(info, index.U32(), 1); | 509 | u32& size{info.constant_buffer_used_sizes[index.U32()]}; |
| 470 | u32 element_size{}; | 510 | if (offset.IsImmediate()) { |
| 471 | switch (inst.GetOpcode()) { | 511 | size = Common::AlignUp(std::max(size, offset.U32() + element_size), 16u); |
| 472 | case IR::Opcode::GetCbufU8: | 512 | } else { |
| 473 | case IR::Opcode::GetCbufS8: | 513 | size = 0x10'000; |
| 474 | info.used_constant_buffer_types |= IR::Type::U8; | 514 | } |
| 475 | element_size = 1; | ||
| 476 | break; | ||
| 477 | case IR::Opcode::GetCbufU16: | ||
| 478 | case IR::Opcode::GetCbufS16: | ||
| 479 | info.used_constant_buffer_types |= IR::Type::U16; | ||
| 480 | element_size = 2; | ||
| 481 | break; | ||
| 482 | case IR::Opcode::GetCbufU32: | ||
| 483 | info.used_constant_buffer_types |= IR::Type::U32; | ||
| 484 | element_size = 4; | ||
| 485 | break; | ||
| 486 | case IR::Opcode::GetCbufF32: | ||
| 487 | info.used_constant_buffer_types |= IR::Type::F32; | ||
| 488 | element_size = 4; | ||
| 489 | break; | ||
| 490 | case IR::Opcode::GetCbufU32x2: | ||
| 491 | info.used_constant_buffer_types |= IR::Type::U32x2; | ||
| 492 | element_size = 8; | ||
| 493 | break; | ||
| 494 | default: | ||
| 495 | break; | ||
| 496 | } | ||
| 497 | u32& size{info.constant_buffer_used_sizes[index.U32()]}; | ||
| 498 | if (offset.IsImmediate()) { | ||
| 499 | size = Common::AlignUp(std::max(size, offset.U32() + element_size), 16u); | ||
| 500 | } else { | 515 | } else { |
| 501 | size = 0x10'000; | 516 | AddRegisterIndexedLdc(info); |
| 517 | GetElementSize(info.used_indirect_cbuf_types, inst.GetOpcode()); | ||
| 502 | } | 518 | } |
| 503 | break; | 519 | break; |
| 504 | } | 520 | } |
diff --git a/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp b/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp index 38592afd0..ddf497e32 100644 --- a/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp +++ b/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp | |||
| @@ -334,7 +334,8 @@ std::optional<LowAddrInfo> TrackLowAddress(IR::Inst* inst) { | |||
| 334 | /// Tries to track the storage buffer address used by a global memory instruction | 334 | /// Tries to track the storage buffer address used by a global memory instruction |
| 335 | std::optional<StorageBufferAddr> Track(const IR::Value& value, const Bias* bias) { | 335 | std::optional<StorageBufferAddr> Track(const IR::Value& value, const Bias* bias) { |
| 336 | const auto pred{[bias](const IR::Inst* inst) -> std::optional<StorageBufferAddr> { | 336 | const auto pred{[bias](const IR::Inst* inst) -> std::optional<StorageBufferAddr> { |
| 337 | if (inst->GetOpcode() != IR::Opcode::GetCbufU32) { | 337 | if (inst->GetOpcode() != IR::Opcode::GetCbufU32 && |
| 338 | inst->GetOpcode() != IR::Opcode::GetCbufU32x2) { | ||
| 338 | return std::nullopt; | 339 | return std::nullopt; |
| 339 | } | 340 | } |
| 340 | const IR::Value index{inst->Arg(0)}; | 341 | const IR::Value index{inst->Arg(0)}; |
diff --git a/src/shader_recompiler/ir_opt/rescaling_pass.cpp b/src/shader_recompiler/ir_opt/rescaling_pass.cpp index c28500dd1..496d4667e 100644 --- a/src/shader_recompiler/ir_opt/rescaling_pass.cpp +++ b/src/shader_recompiler/ir_opt/rescaling_pass.cpp | |||
| @@ -183,6 +183,31 @@ void ScaleIntegerComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_s | |||
| 183 | } | 183 | } |
| 184 | } | 184 | } |
| 185 | 185 | ||
| 186 | void ScaleIntegerOffsetComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled, | ||
| 187 | size_t index) { | ||
| 188 | const IR::Value composite{inst.Arg(index)}; | ||
| 189 | if (composite.IsEmpty()) { | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 193 | const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 0)})}; | ||
| 194 | const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 1)})}; | ||
| 195 | switch (info.type) { | ||
| 196 | case TextureType::ColorArray2D: | ||
| 197 | case TextureType::Color2D: | ||
| 198 | inst.SetArg(index, ir.CompositeConstruct(x, y)); | ||
| 199 | break; | ||
| 200 | case TextureType::Color1D: | ||
| 201 | case TextureType::ColorArray1D: | ||
| 202 | case TextureType::Color3D: | ||
| 203 | case TextureType::ColorCube: | ||
| 204 | case TextureType::ColorArrayCube: | ||
| 205 | case TextureType::Buffer: | ||
| 206 | // Nothing to patch here | ||
| 207 | break; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 186 | void SubScaleCoord(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled) { | 211 | void SubScaleCoord(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled) { |
| 187 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | 212 | const auto info{inst.Flags<IR::TextureInstInfo>()}; |
| 188 | const IR::Value coord{inst.Arg(1)}; | 213 | const IR::Value coord{inst.Arg(1)}; |
| @@ -220,7 +245,7 @@ void SubScaleImageFetch(IR::Block& block, IR::Inst& inst) { | |||
| 220 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; | 245 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; |
| 221 | SubScaleCoord(ir, inst, is_scaled); | 246 | SubScaleCoord(ir, inst, is_scaled); |
| 222 | // Scale ImageFetch offset | 247 | // Scale ImageFetch offset |
| 223 | ScaleIntegerComposite(ir, inst, is_scaled, 2); | 248 | ScaleIntegerOffsetComposite(ir, inst, is_scaled, 2); |
| 224 | } | 249 | } |
| 225 | 250 | ||
| 226 | void SubScaleImageRead(IR::Block& block, IR::Inst& inst) { | 251 | void SubScaleImageRead(IR::Block& block, IR::Inst& inst) { |
| @@ -242,7 +267,7 @@ void PatchImageFetch(IR::Block& block, IR::Inst& inst) { | |||
| 242 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; | 267 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; |
| 243 | ScaleIntegerComposite(ir, inst, is_scaled, 1); | 268 | ScaleIntegerComposite(ir, inst, is_scaled, 1); |
| 244 | // Scale ImageFetch offset | 269 | // Scale ImageFetch offset |
| 245 | ScaleIntegerComposite(ir, inst, is_scaled, 2); | 270 | ScaleIntegerOffsetComposite(ir, inst, is_scaled, 2); |
| 246 | } | 271 | } |
| 247 | 272 | ||
| 248 | void PatchImageRead(IR::Block& block, IR::Inst& inst) { | 273 | void PatchImageRead(IR::Block& block, IR::Inst& inst) { |
diff --git a/src/shader_recompiler/shader_info.h b/src/shader_recompiler/shader_info.h index 9f375c30e..9d36bd9eb 100644 --- a/src/shader_recompiler/shader_info.h +++ b/src/shader_recompiler/shader_info.h | |||
| @@ -173,9 +173,11 @@ struct Info { | |||
| 173 | bool uses_atomic_image_u32{}; | 173 | bool uses_atomic_image_u32{}; |
| 174 | bool uses_shadow_lod{}; | 174 | bool uses_shadow_lod{}; |
| 175 | bool uses_rescaling_uniform{}; | 175 | bool uses_rescaling_uniform{}; |
| 176 | bool uses_cbuf_indirect{}; | ||
| 176 | 177 | ||
| 177 | IR::Type used_constant_buffer_types{}; | 178 | IR::Type used_constant_buffer_types{}; |
| 178 | IR::Type used_storage_buffer_types{}; | 179 | IR::Type used_storage_buffer_types{}; |
| 180 | IR::Type used_indirect_cbuf_types{}; | ||
| 179 | 181 | ||
| 180 | u32 constant_buffer_mask{}; | 182 | u32 constant_buffer_mask{}; |
| 181 | std::array<u32, MAX_CBUFS> constant_buffer_used_sizes{}; | 183 | std::array<u32, MAX_CBUFS> constant_buffer_used_sizes{}; |