diff options
Diffstat (limited to 'src/shader_recompiler')
33 files changed, 665 insertions, 10 deletions
diff --git a/src/shader_recompiler/CMakeLists.txt b/src/shader_recompiler/CMakeLists.txt index b5b7e5e83..bc3df80c8 100644 --- a/src/shader_recompiler/CMakeLists.txt +++ b/src/shader_recompiler/CMakeLists.txt | |||
| @@ -221,6 +221,7 @@ add_library(shader_recompiler STATIC | |||
| 221 | ir_opt/lower_fp16_to_fp32.cpp | 221 | ir_opt/lower_fp16_to_fp32.cpp |
| 222 | ir_opt/lower_int64_to_int32.cpp | 222 | ir_opt/lower_int64_to_int32.cpp |
| 223 | ir_opt/passes.h | 223 | ir_opt/passes.h |
| 224 | ir_opt/rescaling_pass.cpp | ||
| 224 | ir_opt/ssa_rewrite_pass.cpp | 225 | ir_opt/ssa_rewrite_pass.cpp |
| 225 | ir_opt/texture_pass.cpp | 226 | ir_opt/texture_pass.cpp |
| 226 | ir_opt/verification_pass.cpp | 227 | ir_opt/verification_pass.cpp |
diff --git a/src/shader_recompiler/backend/bindings.h b/src/shader_recompiler/backend/bindings.h index 35503000c..669702553 100644 --- a/src/shader_recompiler/backend/bindings.h +++ b/src/shader_recompiler/backend/bindings.h | |||
| @@ -14,6 +14,8 @@ struct Bindings { | |||
| 14 | u32 storage_buffer{}; | 14 | u32 storage_buffer{}; |
| 15 | u32 texture{}; | 15 | u32 texture{}; |
| 16 | u32 image{}; | 16 | u32 image{}; |
| 17 | u32 texture_scaling_index{}; | ||
| 18 | u32 image_scaling_index{}; | ||
| 17 | }; | 19 | }; |
| 18 | 20 | ||
| 19 | } // namespace Shader::Backend | 21 | } // namespace Shader::Backend |
diff --git a/src/shader_recompiler/backend/glasm/emit_context.cpp b/src/shader_recompiler/backend/glasm/emit_context.cpp index 069c019ad..8fd459dfe 100644 --- a/src/shader_recompiler/backend/glasm/emit_context.cpp +++ b/src/shader_recompiler/backend/glasm/emit_context.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include "shader_recompiler/backend/bindings.h" | 7 | #include "shader_recompiler/backend/bindings.h" |
| 8 | #include "shader_recompiler/backend/glasm/emit_context.h" | 8 | #include "shader_recompiler/backend/glasm/emit_context.h" |
| 9 | #include "shader_recompiler/backend/glasm/emit_glasm.h" | ||
| 9 | #include "shader_recompiler/frontend/ir/program.h" | 10 | #include "shader_recompiler/frontend/ir/program.h" |
| 10 | #include "shader_recompiler/profile.h" | 11 | #include "shader_recompiler/profile.h" |
| 11 | #include "shader_recompiler/runtime_info.h" | 12 | #include "shader_recompiler/runtime_info.h" |
| @@ -55,7 +56,8 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile | |||
| 55 | } | 56 | } |
| 56 | if (!runtime_info.glasm_use_storage_buffers) { | 57 | if (!runtime_info.glasm_use_storage_buffers) { |
| 57 | if (const size_t num = info.storage_buffers_descriptors.size(); num > 0) { | 58 | if (const size_t num = info.storage_buffers_descriptors.size(); num > 0) { |
| 58 | Add("PARAM c[{}]={{program.local[0..{}]}};", num, num - 1); | 59 | const size_t index{num + PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE}; |
| 60 | Add("PARAM c[{}]={{program.local[0..{}]}};", index, index - 1); | ||
| 59 | } | 61 | } |
| 60 | } | 62 | } |
| 61 | stage = program.stage; | 63 | stage = program.stage; |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm.cpp b/src/shader_recompiler/backend/glasm/emit_glasm.cpp index 4ce1c4f54..004658546 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm.cpp | |||
| @@ -448,6 +448,9 @@ std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, I | |||
| 448 | header += fmt::format("SHARED_MEMORY {};", program.shared_memory_size); | 448 | header += fmt::format("SHARED_MEMORY {};", program.shared_memory_size); |
| 449 | header += fmt::format("SHARED shared_mem[]={{program.sharedmem}};"); | 449 | header += fmt::format("SHARED shared_mem[]={{program.sharedmem}};"); |
| 450 | } | 450 | } |
| 451 | if (program.info.uses_rescaling_uniform) { | ||
| 452 | header += "PARAM scaling[1]={program.local[0..0]};"; | ||
| 453 | } | ||
| 451 | header += "TEMP "; | 454 | header += "TEMP "; |
| 452 | for (size_t index = 0; index < ctx.reg_alloc.NumUsedRegisters(); ++index) { | 455 | for (size_t index = 0; index < ctx.reg_alloc.NumUsedRegisters(); ++index) { |
| 453 | header += fmt::format("R{},", index); | 456 | header += fmt::format("R{},", index); |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm.h b/src/shader_recompiler/backend/glasm/emit_glasm.h index bcb55f062..292655acb 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm.h +++ b/src/shader_recompiler/backend/glasm/emit_glasm.h | |||
| @@ -13,6 +13,8 @@ | |||
| 13 | 13 | ||
| 14 | namespace Shader::Backend::GLASM { | 14 | namespace Shader::Backend::GLASM { |
| 15 | 15 | ||
| 16 | constexpr u32 PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE = 1; | ||
| 17 | |||
| 16 | [[nodiscard]] std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, | 18 | [[nodiscard]] std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, |
| 17 | IR::Program& program, Bindings& bindings); | 19 | IR::Program& program, Bindings& bindings); |
| 18 | 20 | ||
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_image.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_image.cpp index 09e3a9b82..d325d31c7 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_image.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm_image.cpp | |||
| @@ -608,6 +608,24 @@ void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Re | |||
| 608 | ctx.Add("STOREIM.{} {},{},{},{};", format, image, color, coord, type); | 608 | ctx.Add("STOREIM.{} {},{},{},{};", format, image, color, coord, type); |
| 609 | } | 609 | } |
| 610 | 610 | ||
| 611 | void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) { | ||
| 612 | if (!index.IsImmediate()) { | ||
| 613 | throw NotImplementedException("Non-constant texture rescaling"); | ||
| 614 | } | ||
| 615 | ctx.Add("AND.U RC.x,scaling[0].x,{};" | ||
| 616 | "SNE.S {},RC.x,0;", | ||
| 617 | 1u << index.U32(), ctx.reg_alloc.Define(inst)); | ||
| 618 | } | ||
| 619 | |||
| 620 | void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) { | ||
| 621 | if (!index.IsImmediate()) { | ||
| 622 | throw NotImplementedException("Non-constant texture rescaling"); | ||
| 623 | } | ||
| 624 | ctx.Add("AND.U RC.x,scaling[0].y,{};" | ||
| 625 | "SNE.S {},RC.x,0;", | ||
| 626 | 1u << index.U32(), ctx.reg_alloc.Define(inst)); | ||
| 627 | } | ||
| 628 | |||
| 611 | void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord, | 629 | void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord, |
| 612 | ScalarU32 value) { | 630 | ScalarU32 value) { |
| 613 | ImageAtomic(ctx, inst, index, coord, value, "ADD.U32"); | 631 | ImageAtomic(ctx, inst, index, coord, value, "ADD.U32"); |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h index 12afda43b..1f343bff5 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h +++ b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h | |||
| @@ -72,6 +72,7 @@ void EmitInvocationId(EmitContext& ctx, IR::Inst& inst); | |||
| 72 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); | 72 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); |
| 73 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); | 73 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); |
| 74 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); | 74 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); |
| 75 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst); | ||
| 75 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset); | 76 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset); |
| 76 | void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value); | 77 | void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value); |
| 77 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); | 78 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); |
| @@ -303,6 +304,8 @@ void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, Register a, Register b); | |||
| 303 | void EmitISub32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b); | 304 | void EmitISub32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b); |
| 304 | void EmitISub64(EmitContext& ctx, IR::Inst& inst, Register a, Register b); | 305 | void EmitISub64(EmitContext& ctx, IR::Inst& inst, Register a, Register b); |
| 305 | void EmitIMul32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b); | 306 | void EmitIMul32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b); |
| 307 | void EmitSDiv32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b); | ||
| 308 | void EmitUDiv32(EmitContext& ctx, IR::Inst& inst, ScalarU32 a, ScalarU32 b); | ||
| 306 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value); | 309 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value); |
| 307 | void EmitINeg64(EmitContext& ctx, IR::Inst& inst, Register value); | 310 | void EmitINeg64(EmitContext& ctx, IR::Inst& inst, Register value); |
| 308 | void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value); | 311 | void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value); |
| @@ -553,6 +556,8 @@ void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, | |||
| 553 | void EmitImageRead(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord); | 556 | void EmitImageRead(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord); |
| 554 | void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord, | 557 | void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord, |
| 555 | Register color); | 558 | Register color); |
| 559 | void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index); | ||
| 560 | void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index); | ||
| 556 | void EmitBindlessImageAtomicIAdd32(EmitContext&); | 561 | void EmitBindlessImageAtomicIAdd32(EmitContext&); |
| 557 | void EmitBindlessImageAtomicSMin32(EmitContext&); | 562 | void EmitBindlessImageAtomicSMin32(EmitContext&); |
| 558 | void EmitBindlessImageAtomicUMin32(EmitContext&); | 563 | void EmitBindlessImageAtomicUMin32(EmitContext&); |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_integer.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_integer.cpp index f55c26b76..8aa494a4d 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_integer.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm_integer.cpp | |||
| @@ -90,6 +90,14 @@ void EmitIMul32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) { | |||
| 90 | ctx.Add("MUL.S {}.x,{},{};", inst, a, b); | 90 | ctx.Add("MUL.S {}.x,{},{};", inst, a, b); |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | void EmitSDiv32(EmitContext& ctx, IR::Inst& inst, ScalarS32 a, ScalarS32 b) { | ||
| 94 | ctx.Add("DIV.S {}.x,{},{};", inst, a, b); | ||
| 95 | } | ||
| 96 | |||
| 97 | void EmitUDiv32(EmitContext& ctx, IR::Inst& inst, ScalarU32 a, ScalarU32 b) { | ||
| 98 | ctx.Add("DIV.U {}.x,{},{};", inst, a, b); | ||
| 99 | } | ||
| 100 | |||
| 93 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) { | 101 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, ScalarS32 value) { |
| 94 | if (value.type != Type::Register && static_cast<s32>(value.imm_u32) < 0) { | 102 | if (value.type != Type::Register && static_cast<s32>(value.imm_u32) < 0) { |
| 95 | ctx.Add("MOV.S {},{};", inst, -static_cast<s32>(value.imm_u32)); | 103 | ctx.Add("MOV.S {},{};", inst, -static_cast<s32>(value.imm_u32)); |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp index e537f6073..681aeda8d 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp | |||
| @@ -210,6 +210,10 @@ void EmitYDirection(EmitContext& ctx, IR::Inst& inst) { | |||
| 210 | ctx.Add("MOV.F {}.x,y_direction[0].w;", inst); | 210 | ctx.Add("MOV.F {}.x,y_direction[0].w;", inst); |
| 211 | } | 211 | } |
| 212 | 212 | ||
| 213 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) { | ||
| 214 | ctx.Add("MOV.F {}.x,scaling[0].z;", inst); | ||
| 215 | } | ||
| 216 | |||
| 213 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst) { | 217 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst) { |
| 214 | ctx.Add("MOV.S {}.x,0;", inst); | 218 | ctx.Add("MOV.S {}.x,0;", inst); |
| 215 | } | 219 | } |
diff --git a/src/shader_recompiler/backend/glsl/emit_context.cpp b/src/shader_recompiler/backend/glsl/emit_context.cpp index 4e6f2c0fe..97bd59302 100644 --- a/src/shader_recompiler/backend/glsl/emit_context.cpp +++ b/src/shader_recompiler/backend/glsl/emit_context.cpp | |||
| @@ -393,6 +393,9 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile | |||
| 393 | DefineGenericOutput(index, program.invocations); | 393 | DefineGenericOutput(index, program.invocations); |
| 394 | } | 394 | } |
| 395 | } | 395 | } |
| 396 | if (info.uses_rescaling_uniform) { | ||
| 397 | header += "layout(location=0) uniform vec4 scaling;"; | ||
| 398 | } | ||
| 396 | DefineConstantBuffers(bindings); | 399 | DefineConstantBuffers(bindings); |
| 397 | DefineStorageBuffers(bindings); | 400 | DefineStorageBuffers(bindings); |
| 398 | SetupImages(bindings); | 401 | SetupImages(bindings); |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp index 170db269a..4c26f3829 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp | |||
| @@ -445,6 +445,10 @@ void EmitYDirection(EmitContext& ctx, IR::Inst& inst) { | |||
| 445 | ctx.AddF32("{}=gl_FrontMaterial.ambient.a;", inst); | 445 | ctx.AddF32("{}=gl_FrontMaterial.ambient.a;", inst); |
| 446 | } | 446 | } |
| 447 | 447 | ||
| 448 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) { | ||
| 449 | ctx.AddF32("{}=scaling.z;", inst); | ||
| 450 | } | ||
| 451 | |||
| 448 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) { | 452 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) { |
| 449 | ctx.AddU32("{}=lmem[{}];", inst, word_offset); | 453 | ctx.AddU32("{}=lmem[{}];", inst, word_offset); |
| 450 | } | 454 | } |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp index 447eb8e0a..2f78d0267 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl_image.cpp | |||
| @@ -612,6 +612,22 @@ void EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, const IR::Value | |||
| 612 | value); | 612 | value); |
| 613 | } | 613 | } |
| 614 | 614 | ||
| 615 | void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) { | ||
| 616 | if (!index.IsImmediate()) { | ||
| 617 | throw NotImplementedException("Non-constant texture rescaling"); | ||
| 618 | } | ||
| 619 | const u32 image_index{index.U32()}; | ||
| 620 | ctx.AddU1("{}=(ftou(scaling.x)&{})!=0;", inst, 1u << image_index); | ||
| 621 | } | ||
| 622 | |||
| 623 | void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) { | ||
| 624 | if (!index.IsImmediate()) { | ||
| 625 | throw NotImplementedException("Non-constant texture rescaling"); | ||
| 626 | } | ||
| 627 | const u32 image_index{index.U32()}; | ||
| 628 | ctx.AddU1("{}=(ftou(scaling.y)&{})!=0;", inst, 1u << image_index); | ||
| 629 | } | ||
| 630 | |||
| 615 | void EmitBindlessImageSampleImplicitLod(EmitContext&) { | 631 | void EmitBindlessImageSampleImplicitLod(EmitContext&) { |
| 616 | NotImplemented(); | 632 | NotImplemented(); |
| 617 | } | 633 | } |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h index 5936d086f..f86502e4c 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h +++ b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h | |||
| @@ -85,6 +85,7 @@ void EmitInvocationId(EmitContext& ctx, IR::Inst& inst); | |||
| 85 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); | 85 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); |
| 86 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); | 86 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); |
| 87 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); | 87 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); |
| 88 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst); | ||
| 88 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset); | 89 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset); |
| 89 | void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value); | 90 | void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value); |
| 90 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); | 91 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); |
| @@ -362,6 +363,8 @@ void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::strin | |||
| 362 | void EmitISub32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); | 363 | void EmitISub32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); |
| 363 | void EmitISub64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); | 364 | void EmitISub64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); |
| 364 | void EmitIMul32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); | 365 | void EmitIMul32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); |
| 366 | void EmitSDiv32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); | ||
| 367 | void EmitUDiv32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b); | ||
| 365 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 368 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
| 366 | void EmitINeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 369 | void EmitINeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
| 367 | void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 370 | void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
| @@ -627,6 +630,8 @@ void EmitImageRead(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, | |||
| 627 | std::string_view coords); | 630 | std::string_view coords); |
| 628 | void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, | 631 | void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, |
| 629 | std::string_view coords, std::string_view color); | 632 | std::string_view coords, std::string_view color); |
| 633 | void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index); | ||
| 634 | void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index); | ||
| 630 | void EmitBindlessImageAtomicIAdd32(EmitContext&); | 635 | void EmitBindlessImageAtomicIAdd32(EmitContext&); |
| 631 | void EmitBindlessImageAtomicSMin32(EmitContext&); | 636 | void EmitBindlessImageAtomicSMin32(EmitContext&); |
| 632 | void EmitBindlessImageAtomicUMin32(EmitContext&); | 637 | void EmitBindlessImageAtomicUMin32(EmitContext&); |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp index 38419f88f..88c1d4c5e 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp | |||
| @@ -78,6 +78,14 @@ void EmitIMul32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::strin | |||
| 78 | ctx.AddU32("{}=uint({}*{});", inst, a, b); | 78 | ctx.AddU32("{}=uint({}*{});", inst, a, b); |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | void EmitSDiv32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) { | ||
| 82 | ctx.AddU32("{}=uint(int({})/int({}));", inst, a, b); | ||
| 83 | } | ||
| 84 | |||
| 85 | void EmitUDiv32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) { | ||
| 86 | ctx.AddU32("{}={}/{};", inst, a, b); | ||
| 87 | } | ||
| 88 | |||
| 81 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { | 89 | void EmitINeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { |
| 82 | ctx.AddU32("{}=uint(-({}));", inst, value); | 90 | ctx.AddU32("{}=uint(-({}));", inst, value); |
| 83 | } | 91 | } |
diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp index 3c84e6466..723455462 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp | |||
| @@ -7,11 +7,14 @@ | |||
| 7 | #include <climits> | 7 | #include <climits> |
| 8 | #include <string_view> | 8 | #include <string_view> |
| 9 | 9 | ||
| 10 | #include <boost/container/static_vector.hpp> | ||
| 11 | |||
| 10 | #include <fmt/format.h> | 12 | #include <fmt/format.h> |
| 11 | 13 | ||
| 12 | #include "common/common_types.h" | 14 | #include "common/common_types.h" |
| 13 | #include "common/div_ceil.h" | 15 | #include "common/div_ceil.h" |
| 14 | #include "shader_recompiler/backend/spirv/emit_context.h" | 16 | #include "shader_recompiler/backend/spirv/emit_context.h" |
| 17 | #include "shader_recompiler/backend/spirv/emit_spirv.h" | ||
| 15 | 18 | ||
| 16 | namespace Shader::Backend::SPIRV { | 19 | namespace Shader::Backend::SPIRV { |
| 17 | namespace { | 20 | namespace { |
| @@ -474,8 +477,9 @@ void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_vie | |||
| 474 | 477 | ||
| 475 | EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_info_, | 478 | EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_info_, |
| 476 | IR::Program& program, Bindings& bindings) | 479 | IR::Program& program, Bindings& bindings) |
| 477 | : Sirit::Module(profile_.supported_spirv), profile{profile_}, | 480 | : Sirit::Module(profile_.supported_spirv), profile{profile_}, runtime_info{runtime_info_}, |
| 478 | runtime_info{runtime_info_}, stage{program.stage} { | 481 | stage{program.stage}, texture_rescaling_index{bindings.texture_scaling_index}, |
| 482 | image_rescaling_index{bindings.image_scaling_index} { | ||
| 479 | const bool is_unified{profile.unified_descriptor_binding}; | 483 | const bool is_unified{profile.unified_descriptor_binding}; |
| 480 | u32& uniform_binding{is_unified ? bindings.unified : bindings.uniform_buffer}; | 484 | u32& uniform_binding{is_unified ? bindings.unified : bindings.uniform_buffer}; |
| 481 | u32& storage_binding{is_unified ? bindings.unified : bindings.storage_buffer}; | 485 | u32& storage_binding{is_unified ? bindings.unified : bindings.storage_buffer}; |
| @@ -492,10 +496,11 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf | |||
| 492 | DefineStorageBuffers(program.info, storage_binding); | 496 | DefineStorageBuffers(program.info, storage_binding); |
| 493 | DefineTextureBuffers(program.info, texture_binding); | 497 | DefineTextureBuffers(program.info, texture_binding); |
| 494 | DefineImageBuffers(program.info, image_binding); | 498 | DefineImageBuffers(program.info, image_binding); |
| 495 | DefineTextures(program.info, texture_binding); | 499 | DefineTextures(program.info, texture_binding, bindings.texture_scaling_index); |
| 496 | DefineImages(program.info, image_binding); | 500 | DefineImages(program.info, image_binding, bindings.image_scaling_index); |
| 497 | DefineAttributeMemAccess(program.info); | 501 | DefineAttributeMemAccess(program.info); |
| 498 | DefineGlobalMemoryFunctions(program.info); | 502 | DefineGlobalMemoryFunctions(program.info); |
| 503 | DefineRescalingInput(program.info); | ||
| 499 | } | 504 | } |
| 500 | 505 | ||
| 501 | EmitContext::~EmitContext() = default; | 506 | EmitContext::~EmitContext() = default; |
| @@ -996,6 +1001,73 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) { | |||
| 996 | define(&StorageDefinitions::U32x4, storage_types.U32x4, U32[4], sizeof(u32[4])); | 1001 | define(&StorageDefinitions::U32x4, storage_types.U32x4, U32[4], sizeof(u32[4])); |
| 997 | } | 1002 | } |
| 998 | 1003 | ||
| 1004 | void EmitContext::DefineRescalingInput(const Info& info) { | ||
| 1005 | if (!info.uses_rescaling_uniform) { | ||
| 1006 | return; | ||
| 1007 | } | ||
| 1008 | if (profile.unified_descriptor_binding) { | ||
| 1009 | DefineRescalingInputPushConstant(); | ||
| 1010 | } else { | ||
| 1011 | DefineRescalingInputUniformConstant(); | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | void EmitContext::DefineRescalingInputPushConstant() { | ||
| 1016 | boost::container::static_vector<Id, 3> members{}; | ||
| 1017 | u32 member_index{0}; | ||
| 1018 | |||
| 1019 | rescaling_textures_type = TypeArray(U32[1], Const(4u)); | ||
| 1020 | Decorate(rescaling_textures_type, spv::Decoration::ArrayStride, 4u); | ||
| 1021 | members.push_back(rescaling_textures_type); | ||
| 1022 | rescaling_textures_member_index = member_index++; | ||
| 1023 | |||
| 1024 | rescaling_images_type = TypeArray(U32[1], Const(NUM_IMAGE_SCALING_WORDS)); | ||
| 1025 | Decorate(rescaling_images_type, spv::Decoration::ArrayStride, 4u); | ||
| 1026 | members.push_back(rescaling_images_type); | ||
| 1027 | rescaling_images_member_index = member_index++; | ||
| 1028 | |||
| 1029 | if (stage != Stage::Compute) { | ||
| 1030 | members.push_back(F32[1]); | ||
| 1031 | rescaling_downfactor_member_index = member_index++; | ||
| 1032 | } | ||
| 1033 | const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))}; | ||
| 1034 | Decorate(push_constant_struct, spv::Decoration::Block); | ||
| 1035 | Name(push_constant_struct, "ResolutionInfo"); | ||
| 1036 | |||
| 1037 | MemberDecorate(push_constant_struct, rescaling_textures_member_index, spv::Decoration::Offset, | ||
| 1038 | static_cast<u32>(offsetof(RescalingLayout, rescaling_textures))); | ||
| 1039 | MemberName(push_constant_struct, rescaling_textures_member_index, "rescaling_textures"); | ||
| 1040 | |||
| 1041 | MemberDecorate(push_constant_struct, rescaling_images_member_index, spv::Decoration::Offset, | ||
| 1042 | static_cast<u32>(offsetof(RescalingLayout, rescaling_images))); | ||
| 1043 | MemberName(push_constant_struct, rescaling_images_member_index, "rescaling_images"); | ||
| 1044 | |||
| 1045 | if (stage != Stage::Compute) { | ||
| 1046 | MemberDecorate(push_constant_struct, rescaling_downfactor_member_index, | ||
| 1047 | spv::Decoration::Offset, | ||
| 1048 | static_cast<u32>(offsetof(RescalingLayout, down_factor))); | ||
| 1049 | MemberName(push_constant_struct, rescaling_downfactor_member_index, "down_factor"); | ||
| 1050 | } | ||
| 1051 | const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)}; | ||
| 1052 | rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant); | ||
| 1053 | Name(rescaling_push_constants, "rescaling_push_constants"); | ||
| 1054 | |||
| 1055 | if (profile.supported_spirv >= 0x00010400) { | ||
| 1056 | interfaces.push_back(rescaling_push_constants); | ||
| 1057 | } | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | void EmitContext::DefineRescalingInputUniformConstant() { | ||
| 1061 | const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, F32[4])}; | ||
| 1062 | rescaling_uniform_constant = | ||
| 1063 | AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant); | ||
| 1064 | Decorate(rescaling_uniform_constant, spv::Decoration::Location, 0u); | ||
| 1065 | |||
| 1066 | if (profile.supported_spirv >= 0x00010400) { | ||
| 1067 | interfaces.push_back(rescaling_uniform_constant); | ||
| 1068 | } | ||
| 1069 | } | ||
| 1070 | |||
| 999 | void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { | 1071 | void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { |
| 1000 | if (info.constant_buffer_descriptors.empty()) { | 1072 | if (info.constant_buffer_descriptors.empty()) { |
| 1001 | return; | 1073 | return; |
| @@ -1184,7 +1256,7 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) { | |||
| 1184 | } | 1256 | } |
| 1185 | } | 1257 | } |
| 1186 | 1258 | ||
| 1187 | void EmitContext::DefineTextures(const Info& info, u32& binding) { | 1259 | void EmitContext::DefineTextures(const Info& info, u32& binding, u32& scaling_index) { |
| 1188 | textures.reserve(info.texture_descriptors.size()); | 1260 | textures.reserve(info.texture_descriptors.size()); |
| 1189 | for (const TextureDescriptor& desc : info.texture_descriptors) { | 1261 | for (const TextureDescriptor& desc : info.texture_descriptors) { |
| 1190 | const Id image_type{ImageType(*this, desc)}; | 1262 | const Id image_type{ImageType(*this, desc)}; |
| @@ -1206,13 +1278,14 @@ void EmitContext::DefineTextures(const Info& info, u32& binding) { | |||
| 1206 | interfaces.push_back(id); | 1278 | interfaces.push_back(id); |
| 1207 | } | 1279 | } |
| 1208 | ++binding; | 1280 | ++binding; |
| 1281 | ++scaling_index; | ||
| 1209 | } | 1282 | } |
| 1210 | if (info.uses_atomic_image_u32) { | 1283 | if (info.uses_atomic_image_u32) { |
| 1211 | image_u32 = TypePointer(spv::StorageClass::Image, U32[1]); | 1284 | image_u32 = TypePointer(spv::StorageClass::Image, U32[1]); |
| 1212 | } | 1285 | } |
| 1213 | } | 1286 | } |
| 1214 | 1287 | ||
| 1215 | void EmitContext::DefineImages(const Info& info, u32& binding) { | 1288 | void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_index) { |
| 1216 | images.reserve(info.image_descriptors.size()); | 1289 | images.reserve(info.image_descriptors.size()); |
| 1217 | for (const ImageDescriptor& desc : info.image_descriptors) { | 1290 | for (const ImageDescriptor& desc : info.image_descriptors) { |
| 1218 | if (desc.count != 1) { | 1291 | if (desc.count != 1) { |
| @@ -1233,6 +1306,7 @@ void EmitContext::DefineImages(const Info& info, u32& binding) { | |||
| 1233 | interfaces.push_back(id); | 1306 | interfaces.push_back(id); |
| 1234 | } | 1307 | } |
| 1235 | ++binding; | 1308 | ++binding; |
| 1309 | ++scaling_index; | ||
| 1236 | } | 1310 | } |
| 1237 | } | 1311 | } |
| 1238 | 1312 | ||
diff --git a/src/shader_recompiler/backend/spirv/emit_context.h b/src/shader_recompiler/backend/spirv/emit_context.h index 112c52382..63f8185d9 100644 --- a/src/shader_recompiler/backend/spirv/emit_context.h +++ b/src/shader_recompiler/backend/spirv/emit_context.h | |||
| @@ -238,6 +238,16 @@ public: | |||
| 238 | Id indexed_load_func{}; | 238 | Id indexed_load_func{}; |
| 239 | Id indexed_store_func{}; | 239 | Id indexed_store_func{}; |
| 240 | 240 | ||
| 241 | Id rescaling_uniform_constant{}; | ||
| 242 | Id rescaling_push_constants{}; | ||
| 243 | Id rescaling_textures_type{}; | ||
| 244 | Id rescaling_images_type{}; | ||
| 245 | u32 rescaling_textures_member_index{}; | ||
| 246 | u32 rescaling_images_member_index{}; | ||
| 247 | u32 rescaling_downfactor_member_index{}; | ||
| 248 | u32 texture_rescaling_index{}; | ||
| 249 | u32 image_rescaling_index{}; | ||
| 250 | |||
| 241 | Id local_memory{}; | 251 | Id local_memory{}; |
| 242 | 252 | ||
| 243 | Id shared_memory_u8{}; | 253 | Id shared_memory_u8{}; |
| @@ -310,10 +320,13 @@ private: | |||
| 310 | void DefineStorageBuffers(const Info& info, u32& binding); | 320 | void DefineStorageBuffers(const Info& info, u32& binding); |
| 311 | void DefineTextureBuffers(const Info& info, u32& binding); | 321 | void DefineTextureBuffers(const Info& info, u32& binding); |
| 312 | void DefineImageBuffers(const Info& info, u32& binding); | 322 | void DefineImageBuffers(const Info& info, u32& binding); |
| 313 | void DefineTextures(const Info& info, u32& binding); | 323 | void DefineTextures(const Info& info, u32& binding, u32& scaling_index); |
| 314 | void DefineImages(const Info& info, u32& binding); | 324 | void DefineImages(const Info& info, u32& binding, u32& scaling_index); |
| 315 | void DefineAttributeMemAccess(const Info& info); | 325 | void DefineAttributeMemAccess(const Info& info); |
| 316 | void DefineGlobalMemoryFunctions(const Info& info); | 326 | void DefineGlobalMemoryFunctions(const Info& info); |
| 327 | void DefineRescalingInput(const Info& info); | ||
| 328 | void DefineRescalingInputPushConstant(); | ||
| 329 | void DefineRescalingInputUniformConstant(); | ||
| 317 | 330 | ||
| 318 | void DefineInputs(const IR::Program& program); | 331 | void DefineInputs(const IR::Program& program); |
| 319 | void DefineOutputs(const IR::Program& program); | 332 | 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 db0c935fe..4b25534ce 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv.h | |||
| @@ -16,6 +16,19 @@ | |||
| 16 | 16 | ||
| 17 | namespace Shader::Backend::SPIRV { | 17 | namespace Shader::Backend::SPIRV { |
| 18 | 18 | ||
| 19 | constexpr u32 NUM_TEXTURE_SCALING_WORDS = 4; | ||
| 20 | constexpr u32 NUM_IMAGE_SCALING_WORDS = 2; | ||
| 21 | constexpr u32 NUM_TEXTURE_AND_IMAGE_SCALING_WORDS = | ||
| 22 | NUM_TEXTURE_SCALING_WORDS + NUM_IMAGE_SCALING_WORDS; | ||
| 23 | |||
| 24 | struct RescalingLayout { | ||
| 25 | alignas(16) std::array<u32, NUM_TEXTURE_SCALING_WORDS> rescaling_textures; | ||
| 26 | alignas(16) std::array<u32, NUM_IMAGE_SCALING_WORDS> rescaling_images; | ||
| 27 | alignas(16) u32 down_factor; | ||
| 28 | }; | ||
| 29 | constexpr u32 RESCALING_LAYOUT_WORDS_OFFSET = offsetof(RescalingLayout, rescaling_textures); | ||
| 30 | constexpr u32 RESCALING_LAYOUT_DOWN_FACTOR_OFFSET = offsetof(RescalingLayout, down_factor); | ||
| 31 | |||
| 19 | [[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, | 32 | [[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, |
| 20 | IR::Program& program, Bindings& bindings); | 33 | IR::Program& program, Bindings& bindings); |
| 21 | 34 | ||
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 d3a93d5f4..bac683ae1 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 | |||
| @@ -526,6 +526,18 @@ Id EmitYDirection(EmitContext& ctx) { | |||
| 526 | return ctx.Const(ctx.runtime_info.y_negate ? -1.0f : 1.0f); | 526 | return ctx.Const(ctx.runtime_info.y_negate ? -1.0f : 1.0f); |
| 527 | } | 527 | } |
| 528 | 528 | ||
| 529 | Id EmitResolutionDownFactor(EmitContext& ctx) { | ||
| 530 | if (ctx.profile.unified_descriptor_binding) { | ||
| 531 | const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[1])}; | ||
| 532 | const Id index{ctx.Const(ctx.rescaling_downfactor_member_index)}; | ||
| 533 | const Id pointer{ctx.OpAccessChain(pointer_type, ctx.rescaling_push_constants, index)}; | ||
| 534 | return ctx.OpLoad(ctx.F32[1], pointer); | ||
| 535 | } else { | ||
| 536 | const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)}; | ||
| 537 | return ctx.OpCompositeExtract(ctx.F32[1], composite, 2u); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 529 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset) { | 541 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset) { |
| 530 | const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)}; | 542 | const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)}; |
| 531 | return ctx.OpLoad(ctx.U32[1], pointer); | 543 | return ctx.OpLoad(ctx.U32[1], pointer); |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp index 1d5364309..4d168a96d 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp | |||
| @@ -224,6 +224,36 @@ Id Emit(MethodPtrType sparse_ptr, MethodPtrType non_sparse_ptr, EmitContext& ctx | |||
| 224 | Decorate(ctx, inst, sample); | 224 | Decorate(ctx, inst, sample); |
| 225 | return ctx.OpCompositeExtract(result_type, sample, 1U); | 225 | return ctx.OpCompositeExtract(result_type, sample, 1U); |
| 226 | } | 226 | } |
| 227 | |||
| 228 | Id IsScaled(EmitContext& ctx, const IR::Value& index, Id member_index, u32 base_index) { | ||
| 229 | const Id push_constant_u32{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.U32[1])}; | ||
| 230 | Id bit{}; | ||
| 231 | if (index.IsImmediate()) { | ||
| 232 | // Use BitwiseAnd instead of BitfieldExtract for better codegen on Nvidia OpenGL. | ||
| 233 | // LOP32I.NZ is used to set the predicate rather than BFE+ISETP. | ||
| 234 | const u32 index_value{index.U32() + base_index}; | ||
| 235 | const Id word_index{ctx.Const(index_value / 32)}; | ||
| 236 | const Id bit_index_mask{ctx.Const(1u << (index_value % 32))}; | ||
| 237 | const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants, | ||
| 238 | member_index, word_index)}; | ||
| 239 | const Id word{ctx.OpLoad(ctx.U32[1], pointer)}; | ||
| 240 | bit = ctx.OpBitwiseAnd(ctx.U32[1], word, bit_index_mask); | ||
| 241 | } else { | ||
| 242 | Id index_value{ctx.Def(index)}; | ||
| 243 | if (base_index != 0) { | ||
| 244 | index_value = ctx.OpIAdd(ctx.U32[1], index_value, ctx.Const(base_index)); | ||
| 245 | } | ||
| 246 | const Id bit_index{ctx.OpBitwiseAnd(ctx.U32[1], index_value, ctx.Const(31u))}; | ||
| 247 | bit = ctx.OpBitFieldUExtract(ctx.U32[1], index_value, bit_index, ctx.Const(1u)); | ||
| 248 | } | ||
| 249 | return ctx.OpINotEqual(ctx.U1, bit, ctx.u32_zero_value); | ||
| 250 | } | ||
| 251 | |||
| 252 | Id BitTest(EmitContext& ctx, Id mask, Id bit) { | ||
| 253 | const Id shifted{ctx.OpShiftRightLogical(ctx.U32[1], mask, bit)}; | ||
| 254 | const Id bit_value{ctx.OpBitwiseAnd(ctx.U32[1], shifted, ctx.Const(1u))}; | ||
| 255 | return ctx.OpINotEqual(ctx.U1, bit_value, ctx.u32_zero_value); | ||
| 256 | } | ||
| 227 | } // Anonymous namespace | 257 | } // Anonymous namespace |
| 228 | 258 | ||
| 229 | Id EmitBindlessImageSampleImplicitLod(EmitContext&) { | 259 | Id EmitBindlessImageSampleImplicitLod(EmitContext&) { |
| @@ -470,4 +500,28 @@ void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id | |||
| 470 | ctx.OpImageWrite(Image(ctx, index, info), coords, color); | 500 | ctx.OpImageWrite(Image(ctx, index, info), coords, color); |
| 471 | } | 501 | } |
| 472 | 502 | ||
| 503 | Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) { | ||
| 504 | if (ctx.profile.unified_descriptor_binding) { | ||
| 505 | const Id member_index{ctx.Const(ctx.rescaling_textures_member_index)}; | ||
| 506 | return IsScaled(ctx, index, member_index, ctx.texture_rescaling_index); | ||
| 507 | } else { | ||
| 508 | const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)}; | ||
| 509 | const Id mask_f32{ctx.OpCompositeExtract(ctx.F32[1], composite, 0u)}; | ||
| 510 | const Id mask{ctx.OpBitcast(ctx.U32[1], mask_f32)}; | ||
| 511 | return BitTest(ctx, mask, ctx.Def(index)); | ||
| 512 | } | ||
| 513 | } | ||
| 514 | |||
| 515 | Id EmitIsImageScaled(EmitContext& ctx, const IR::Value& index) { | ||
| 516 | if (ctx.profile.unified_descriptor_binding) { | ||
| 517 | const Id member_index{ctx.Const(ctx.rescaling_images_member_index)}; | ||
| 518 | return IsScaled(ctx, index, member_index, ctx.image_rescaling_index); | ||
| 519 | } else { | ||
| 520 | const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)}; | ||
| 521 | const Id mask_f32{ctx.OpCompositeExtract(ctx.F32[1], composite, 1u)}; | ||
| 522 | const Id mask{ctx.OpBitcast(ctx.U32[1], mask_f32)}; | ||
| 523 | return BitTest(ctx, mask, ctx.Def(index)); | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 473 | } // namespace Shader::Backend::SPIRV | 527 | } // namespace Shader::Backend::SPIRV |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h index c9db1c164..6cd22dd3e 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h | |||
| @@ -75,6 +75,7 @@ Id EmitInvocationId(EmitContext& ctx); | |||
| 75 | Id EmitSampleId(EmitContext& ctx); | 75 | Id EmitSampleId(EmitContext& ctx); |
| 76 | Id EmitIsHelperInvocation(EmitContext& ctx); | 76 | Id EmitIsHelperInvocation(EmitContext& ctx); |
| 77 | Id EmitYDirection(EmitContext& ctx); | 77 | Id EmitYDirection(EmitContext& ctx); |
| 78 | Id EmitResolutionDownFactor(EmitContext& ctx); | ||
| 78 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset); | 79 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset); |
| 79 | void EmitWriteLocal(EmitContext& ctx, Id word_offset, Id value); | 80 | void EmitWriteLocal(EmitContext& ctx, Id word_offset, Id value); |
| 80 | Id EmitUndefU1(EmitContext& ctx); | 81 | Id EmitUndefU1(EmitContext& ctx); |
| @@ -283,6 +284,8 @@ Id EmitIAdd64(EmitContext& ctx, Id a, Id b); | |||
| 283 | Id EmitISub32(EmitContext& ctx, Id a, Id b); | 284 | Id EmitISub32(EmitContext& ctx, Id a, Id b); |
| 284 | Id EmitISub64(EmitContext& ctx, Id a, Id b); | 285 | Id EmitISub64(EmitContext& ctx, Id a, Id b); |
| 285 | Id EmitIMul32(EmitContext& ctx, Id a, Id b); | 286 | Id EmitIMul32(EmitContext& ctx, Id a, Id b); |
| 287 | Id EmitSDiv32(EmitContext& ctx, Id a, Id b); | ||
| 288 | Id EmitUDiv32(EmitContext& ctx, Id a, Id b); | ||
| 286 | Id EmitINeg32(EmitContext& ctx, Id value); | 289 | Id EmitINeg32(EmitContext& ctx, Id value); |
| 287 | Id EmitINeg64(EmitContext& ctx, Id value); | 290 | Id EmitINeg64(EmitContext& ctx, Id value); |
| 288 | Id EmitIAbs32(EmitContext& ctx, Id value); | 291 | Id EmitIAbs32(EmitContext& ctx, Id value); |
| @@ -510,6 +513,8 @@ Id EmitImageGradient(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, I | |||
| 510 | Id derivates, Id offset, Id lod_clamp); | 513 | Id derivates, Id offset, Id lod_clamp); |
| 511 | Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords); | 514 | Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords); |
| 512 | void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color); | 515 | void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color); |
| 516 | Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index); | ||
| 517 | Id EmitIsImageScaled(EmitContext& ctx, const IR::Value& index); | ||
| 513 | Id EmitBindlessImageAtomicIAdd32(EmitContext&); | 518 | Id EmitBindlessImageAtomicIAdd32(EmitContext&); |
| 514 | Id EmitBindlessImageAtomicSMin32(EmitContext&); | 519 | Id EmitBindlessImageAtomicSMin32(EmitContext&); |
| 515 | Id EmitBindlessImageAtomicUMin32(EmitContext&); | 520 | Id EmitBindlessImageAtomicUMin32(EmitContext&); |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_integer.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_integer.cpp index 3501d7495..50277eec3 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_integer.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_integer.cpp | |||
| @@ -72,6 +72,14 @@ Id EmitIMul32(EmitContext& ctx, Id a, Id b) { | |||
| 72 | return ctx.OpIMul(ctx.U32[1], a, b); | 72 | return ctx.OpIMul(ctx.U32[1], a, b); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | Id EmitSDiv32(EmitContext& ctx, Id a, Id b) { | ||
| 76 | return ctx.OpSDiv(ctx.U32[1], a, b); | ||
| 77 | } | ||
| 78 | |||
| 79 | Id EmitUDiv32(EmitContext& ctx, Id a, Id b) { | ||
| 80 | return ctx.OpUDiv(ctx.U32[1], a, b); | ||
| 81 | } | ||
| 82 | |||
| 75 | Id EmitINeg32(EmitContext& ctx, Id value) { | 83 | Id EmitINeg32(EmitContext& ctx, Id value) { |
| 76 | return ctx.OpSNegate(ctx.U32[1], value); | 84 | return ctx.OpSNegate(ctx.U32[1], value); |
| 77 | } | 85 | } |
diff --git a/src/shader_recompiler/frontend/ir/basic_block.cpp b/src/shader_recompiler/frontend/ir/basic_block.cpp index 7c08b25ce..974efa4a0 100644 --- a/src/shader_recompiler/frontend/ir/basic_block.cpp +++ b/src/shader_recompiler/frontend/ir/basic_block.cpp | |||
| @@ -22,6 +22,11 @@ void Block::AppendNewInst(Opcode op, std::initializer_list<Value> args) { | |||
| 22 | PrependNewInst(end(), op, args); | 22 | PrependNewInst(end(), op, args); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | Block::iterator Block::PrependNewInst(iterator insertion_point, const Inst& base_inst) { | ||
| 26 | Inst* const inst{inst_pool->Create(base_inst)}; | ||
| 27 | return instructions.insert(insertion_point, *inst); | ||
| 28 | } | ||
| 29 | |||
| 25 | Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode op, | 30 | Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode op, |
| 26 | std::initializer_list<Value> args, u32 flags) { | 31 | std::initializer_list<Value> args, u32 flags) { |
| 27 | Inst* const inst{inst_pool->Create(op, flags)}; | 32 | Inst* const inst{inst_pool->Create(op, flags)}; |
diff --git a/src/shader_recompiler/frontend/ir/basic_block.h b/src/shader_recompiler/frontend/ir/basic_block.h index 9ce1ed07e..fbfe98266 100644 --- a/src/shader_recompiler/frontend/ir/basic_block.h +++ b/src/shader_recompiler/frontend/ir/basic_block.h | |||
| @@ -40,6 +40,9 @@ public: | |||
| 40 | /// Appends a new instruction to the end of this basic block. | 40 | /// Appends a new instruction to the end of this basic block. |
| 41 | void AppendNewInst(Opcode op, std::initializer_list<Value> args); | 41 | void AppendNewInst(Opcode op, std::initializer_list<Value> args); |
| 42 | 42 | ||
| 43 | /// Prepends a copy of an instruction to this basic block before the insertion point. | ||
| 44 | iterator PrependNewInst(iterator insertion_point, const Inst& base_inst); | ||
| 45 | |||
| 43 | /// Prepends a new instruction to this basic block before the insertion point. | 46 | /// Prepends a new instruction to this basic block before the insertion point. |
| 44 | iterator PrependNewInst(iterator insertion_point, Opcode op, | 47 | iterator PrependNewInst(iterator insertion_point, Opcode op, |
| 45 | std::initializer_list<Value> args = {}, u32 flags = 0); | 48 | std::initializer_list<Value> args = {}, u32 flags = 0); |
diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.cpp b/src/shader_recompiler/frontend/ir/ir_emitter.cpp index 13159a68d..356f889ac 100644 --- a/src/shader_recompiler/frontend/ir/ir_emitter.cpp +++ b/src/shader_recompiler/frontend/ir/ir_emitter.cpp | |||
| @@ -375,6 +375,10 @@ F32 IREmitter::YDirection() { | |||
| 375 | return Inst<F32>(Opcode::YDirection); | 375 | return Inst<F32>(Opcode::YDirection); |
| 376 | } | 376 | } |
| 377 | 377 | ||
| 378 | F32 IREmitter::ResolutionDownFactor() { | ||
| 379 | return Inst<F32>(Opcode::ResolutionDownFactor); | ||
| 380 | } | ||
| 381 | |||
| 378 | U32 IREmitter::LaneId() { | 382 | U32 IREmitter::LaneId() { |
| 379 | return Inst<U32>(Opcode::LaneId); | 383 | return Inst<U32>(Opcode::LaneId); |
| 380 | } | 384 | } |
| @@ -1141,6 +1145,10 @@ U32 IREmitter::IMul(const U32& a, const U32& b) { | |||
| 1141 | return Inst<U32>(Opcode::IMul32, a, b); | 1145 | return Inst<U32>(Opcode::IMul32, a, b); |
| 1142 | } | 1146 | } |
| 1143 | 1147 | ||
| 1148 | U32 IREmitter::IDiv(const U32& a, const U32& b, bool is_signed) { | ||
| 1149 | return Inst<U32>(is_signed ? Opcode::SDiv32 : Opcode::UDiv32, a, b); | ||
| 1150 | } | ||
| 1151 | |||
| 1144 | U32U64 IREmitter::INeg(const U32U64& value) { | 1152 | U32U64 IREmitter::INeg(const U32U64& value) { |
| 1145 | switch (value.Type()) { | 1153 | switch (value.Type()) { |
| 1146 | case Type::U32: | 1154 | case Type::U32: |
| @@ -1938,6 +1946,14 @@ Value IREmitter::ImageAtomicExchange(const Value& handle, const Value& coords, c | |||
| 1938 | return Inst(op, Flags{info}, handle, coords, value); | 1946 | return Inst(op, Flags{info}, handle, coords, value); |
| 1939 | } | 1947 | } |
| 1940 | 1948 | ||
| 1949 | U1 IREmitter::IsTextureScaled(const U32& index) { | ||
| 1950 | return Inst<U1>(Opcode::IsTextureScaled, index); | ||
| 1951 | } | ||
| 1952 | |||
| 1953 | U1 IREmitter::IsImageScaled(const U32& index) { | ||
| 1954 | return Inst<U1>(Opcode::IsImageScaled, index); | ||
| 1955 | } | ||
| 1956 | |||
| 1941 | U1 IREmitter::VoteAll(const U1& value) { | 1957 | U1 IREmitter::VoteAll(const U1& value) { |
| 1942 | return Inst<U1>(Opcode::VoteAll, value); | 1958 | return Inst<U1>(Opcode::VoteAll, value); |
| 1943 | } | 1959 | } |
diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.h b/src/shader_recompiler/frontend/ir/ir_emitter.h index 1b89ca5a0..13eefa88b 100644 --- a/src/shader_recompiler/frontend/ir/ir_emitter.h +++ b/src/shader_recompiler/frontend/ir/ir_emitter.h | |||
| @@ -102,6 +102,8 @@ public: | |||
| 102 | [[nodiscard]] U1 IsHelperInvocation(); | 102 | [[nodiscard]] U1 IsHelperInvocation(); |
| 103 | [[nodiscard]] F32 YDirection(); | 103 | [[nodiscard]] F32 YDirection(); |
| 104 | 104 | ||
| 105 | [[nodiscard]] F32 ResolutionDownFactor(); | ||
| 106 | |||
| 105 | [[nodiscard]] U32 LaneId(); | 107 | [[nodiscard]] U32 LaneId(); |
| 106 | 108 | ||
| 107 | [[nodiscard]] U32 LoadGlobalU8(const U64& address); | 109 | [[nodiscard]] U32 LoadGlobalU8(const U64& address); |
| @@ -207,6 +209,7 @@ public: | |||
| 207 | [[nodiscard]] U32U64 IAdd(const U32U64& a, const U32U64& b); | 209 | [[nodiscard]] U32U64 IAdd(const U32U64& a, const U32U64& b); |
| 208 | [[nodiscard]] U32U64 ISub(const U32U64& a, const U32U64& b); | 210 | [[nodiscard]] U32U64 ISub(const U32U64& a, const U32U64& b); |
| 209 | [[nodiscard]] U32 IMul(const U32& a, const U32& b); | 211 | [[nodiscard]] U32 IMul(const U32& a, const U32& b); |
| 212 | [[nodiscard]] U32 IDiv(const U32& a, const U32& b, bool is_signed = false); | ||
| 210 | [[nodiscard]] U32U64 INeg(const U32U64& value); | 213 | [[nodiscard]] U32U64 INeg(const U32U64& value); |
| 211 | [[nodiscard]] U32 IAbs(const U32& value); | 214 | [[nodiscard]] U32 IAbs(const U32& value); |
| 212 | [[nodiscard]] U32U64 ShiftLeftLogical(const U32U64& base, const U32& shift); | 215 | [[nodiscard]] U32U64 ShiftLeftLogical(const U32U64& base, const U32& shift); |
| @@ -356,6 +359,10 @@ public: | |||
| 356 | TextureInstInfo info); | 359 | TextureInstInfo info); |
| 357 | [[nodiscard]] Value ImageAtomicExchange(const Value& handle, const Value& coords, | 360 | [[nodiscard]] Value ImageAtomicExchange(const Value& handle, const Value& coords, |
| 358 | const Value& value, TextureInstInfo info); | 361 | const Value& value, TextureInstInfo info); |
| 362 | |||
| 363 | [[nodiscard]] U1 IsTextureScaled(const U32& index); | ||
| 364 | [[nodiscard]] U1 IsImageScaled(const U32& index); | ||
| 365 | |||
| 359 | [[nodiscard]] U1 VoteAll(const U1& value); | 366 | [[nodiscard]] U1 VoteAll(const U1& value); |
| 360 | [[nodiscard]] U1 VoteAny(const U1& value); | 367 | [[nodiscard]] U1 VoteAny(const U1& value); |
| 361 | [[nodiscard]] U1 VoteEqual(const U1& value); | 368 | [[nodiscard]] U1 VoteEqual(const U1& value); |
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.cpp b/src/shader_recompiler/frontend/ir/microinstruction.cpp index 30b470bdd..97e2bf6af 100644 --- a/src/shader_recompiler/frontend/ir/microinstruction.cpp +++ b/src/shader_recompiler/frontend/ir/microinstruction.cpp | |||
| @@ -47,6 +47,17 @@ Inst::Inst(IR::Opcode op_, u32 flags_) noexcept : op{op_}, flags{flags_} { | |||
| 47 | } | 47 | } |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | Inst::Inst(const Inst& base) : op{base.op}, flags{base.flags} { | ||
| 51 | if (base.op == Opcode::Phi) { | ||
| 52 | throw NotImplementedException("Copying phi node"); | ||
| 53 | } | ||
| 54 | std::construct_at(&args); | ||
| 55 | const size_t num_args{base.NumArgs()}; | ||
| 56 | for (size_t index = 0; index < num_args; ++index) { | ||
| 57 | SetArg(index, base.Arg(index)); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 50 | Inst::~Inst() { | 61 | Inst::~Inst() { |
| 51 | if (op == Opcode::Phi) { | 62 | if (op == Opcode::Phi) { |
| 52 | std::destroy_at(&phi_args); | 63 | std::destroy_at(&phi_args); |
diff --git a/src/shader_recompiler/frontend/ir/opcodes.inc b/src/shader_recompiler/frontend/ir/opcodes.inc index d91098c80..6929919df 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.inc +++ b/src/shader_recompiler/frontend/ir/opcodes.inc | |||
| @@ -62,6 +62,7 @@ OPCODE(InvocationId, U32, | |||
| 62 | OPCODE(SampleId, U32, ) | 62 | OPCODE(SampleId, U32, ) |
| 63 | OPCODE(IsHelperInvocation, U1, ) | 63 | OPCODE(IsHelperInvocation, U1, ) |
| 64 | OPCODE(YDirection, F32, ) | 64 | OPCODE(YDirection, F32, ) |
| 65 | OPCODE(ResolutionDownFactor, F32, ) | ||
| 65 | 66 | ||
| 66 | // Undefined | 67 | // Undefined |
| 67 | OPCODE(UndefU1, U1, ) | 68 | OPCODE(UndefU1, U1, ) |
| @@ -286,6 +287,8 @@ OPCODE(IAdd64, U64, U64, | |||
| 286 | OPCODE(ISub32, U32, U32, U32, ) | 287 | OPCODE(ISub32, U32, U32, U32, ) |
| 287 | OPCODE(ISub64, U64, U64, U64, ) | 288 | OPCODE(ISub64, U64, U64, U64, ) |
| 288 | OPCODE(IMul32, U32, U32, U32, ) | 289 | OPCODE(IMul32, U32, U32, U32, ) |
| 290 | OPCODE(SDiv32, U32, U32, U32, ) | ||
| 291 | OPCODE(UDiv32, U32, U32, U32, ) | ||
| 289 | OPCODE(INeg32, U32, U32, ) | 292 | OPCODE(INeg32, U32, U32, ) |
| 290 | OPCODE(INeg64, U64, U64, ) | 293 | OPCODE(INeg64, U64, U64, ) |
| 291 | OPCODE(IAbs32, U32, U32, ) | 294 | OPCODE(IAbs32, U32, U32, ) |
| @@ -490,6 +493,9 @@ OPCODE(ImageGradient, F32x4, Opaq | |||
| 490 | OPCODE(ImageRead, U32x4, Opaque, Opaque, ) | 493 | OPCODE(ImageRead, U32x4, Opaque, Opaque, ) |
| 491 | OPCODE(ImageWrite, Void, Opaque, Opaque, U32x4, ) | 494 | OPCODE(ImageWrite, Void, Opaque, Opaque, U32x4, ) |
| 492 | 495 | ||
| 496 | OPCODE(IsTextureScaled, U1, U32, ) | ||
| 497 | OPCODE(IsImageScaled, U1, U32, ) | ||
| 498 | |||
| 493 | // Atomic Image operations | 499 | // Atomic Image operations |
| 494 | 500 | ||
| 495 | OPCODE(BindlessImageAtomicIAdd32, U32, U32, Opaque, U32, ) | 501 | OPCODE(BindlessImageAtomicIAdd32, U32, U32, Opaque, U32, ) |
diff --git a/src/shader_recompiler/frontend/ir/value.h b/src/shader_recompiler/frontend/ir/value.h index 6c9ef6bdd..947579852 100644 --- a/src/shader_recompiler/frontend/ir/value.h +++ b/src/shader_recompiler/frontend/ir/value.h | |||
| @@ -116,10 +116,10 @@ public: | |||
| 116 | class Inst : public boost::intrusive::list_base_hook<> { | 116 | class Inst : public boost::intrusive::list_base_hook<> { |
| 117 | public: | 117 | public: |
| 118 | explicit Inst(IR::Opcode op_, u32 flags_) noexcept; | 118 | explicit Inst(IR::Opcode op_, u32 flags_) noexcept; |
| 119 | explicit Inst(const Inst& base); | ||
| 119 | ~Inst(); | 120 | ~Inst(); |
| 120 | 121 | ||
| 121 | Inst& operator=(const Inst&) = delete; | 122 | Inst& operator=(const Inst&) = delete; |
| 122 | Inst(const Inst&) = delete; | ||
| 123 | 123 | ||
| 124 | Inst& operator=(Inst&&) = delete; | 124 | Inst& operator=(Inst&&) = delete; |
| 125 | Inst(Inst&&) = delete; | 125 | Inst(Inst&&) = delete; |
diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp index 2fc542f0e..267ebe4af 100644 --- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp +++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp | |||
| @@ -179,6 +179,10 @@ IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Blo | |||
| 179 | Optimization::TexturePass(env, program); | 179 | Optimization::TexturePass(env, program); |
| 180 | 180 | ||
| 181 | Optimization::ConstantPropagationPass(program); | 181 | Optimization::ConstantPropagationPass(program); |
| 182 | |||
| 183 | if (Settings::values.resolution_info.active) { | ||
| 184 | Optimization::RescalingPass(program); | ||
| 185 | } | ||
| 182 | Optimization::DeadCodeEliminationPass(program); | 186 | Optimization::DeadCodeEliminationPass(program); |
| 183 | if (Settings::values.renderer_debug) { | 187 | if (Settings::values.renderer_debug) { |
| 184 | Optimization::VerificationPass(program); | 188 | Optimization::VerificationPass(program); |
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 f69e1c9cc..1e476d83d 100644 --- a/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp +++ b/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp | |||
| @@ -430,6 +430,11 @@ void VisitUsages(Info& info, IR::Inst& inst) { | |||
| 430 | case IR::Opcode::IsHelperInvocation: | 430 | case IR::Opcode::IsHelperInvocation: |
| 431 | info.uses_is_helper_invocation = true; | 431 | info.uses_is_helper_invocation = true; |
| 432 | break; | 432 | break; |
| 433 | case IR::Opcode::ResolutionDownFactor: | ||
| 434 | case IR::Opcode::IsTextureScaled: | ||
| 435 | case IR::Opcode::IsImageScaled: | ||
| 436 | info.uses_rescaling_uniform = true; | ||
| 437 | break; | ||
| 433 | case IR::Opcode::LaneId: | 438 | case IR::Opcode::LaneId: |
| 434 | info.uses_subgroup_invocation_id = true; | 439 | info.uses_subgroup_invocation_id = true; |
| 435 | break; | 440 | break; |
diff --git a/src/shader_recompiler/ir_opt/passes.h b/src/shader_recompiler/ir_opt/passes.h index 2f89b1ea0..f877c7ba0 100644 --- a/src/shader_recompiler/ir_opt/passes.h +++ b/src/shader_recompiler/ir_opt/passes.h | |||
| @@ -19,6 +19,7 @@ void GlobalMemoryToStorageBufferPass(IR::Program& program); | |||
| 19 | void IdentityRemovalPass(IR::Program& program); | 19 | void IdentityRemovalPass(IR::Program& program); |
| 20 | void LowerFp16ToFp32(IR::Program& program); | 20 | void LowerFp16ToFp32(IR::Program& program); |
| 21 | void LowerInt64ToInt32(IR::Program& program); | 21 | void LowerInt64ToInt32(IR::Program& program); |
| 22 | void RescalingPass(IR::Program& program); | ||
| 22 | void SsaRewritePass(IR::Program& program); | 23 | void SsaRewritePass(IR::Program& program); |
| 23 | void TexturePass(Environment& env, IR::Program& program); | 24 | void TexturePass(Environment& env, IR::Program& program); |
| 24 | void VerificationPass(const IR::Program& program); | 25 | void VerificationPass(const IR::Program& program); |
diff --git a/src/shader_recompiler/ir_opt/rescaling_pass.cpp b/src/shader_recompiler/ir_opt/rescaling_pass.cpp new file mode 100644 index 000000000..c28500dd1 --- /dev/null +++ b/src/shader_recompiler/ir_opt/rescaling_pass.cpp | |||
| @@ -0,0 +1,327 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/alignment.h" | ||
| 6 | #include "common/settings.h" | ||
| 7 | #include "shader_recompiler/environment.h" | ||
| 8 | #include "shader_recompiler/frontend/ir/ir_emitter.h" | ||
| 9 | #include "shader_recompiler/frontend/ir/modifiers.h" | ||
| 10 | #include "shader_recompiler/frontend/ir/program.h" | ||
| 11 | #include "shader_recompiler/frontend/ir/value.h" | ||
| 12 | #include "shader_recompiler/ir_opt/passes.h" | ||
| 13 | #include "shader_recompiler/shader_info.h" | ||
| 14 | |||
| 15 | namespace Shader::Optimization { | ||
| 16 | namespace { | ||
| 17 | [[nodiscard]] bool IsTextureTypeRescalable(TextureType type) { | ||
| 18 | switch (type) { | ||
| 19 | case TextureType::Color2D: | ||
| 20 | case TextureType::ColorArray2D: | ||
| 21 | return true; | ||
| 22 | case TextureType::Color1D: | ||
| 23 | case TextureType::ColorArray1D: | ||
| 24 | case TextureType::Color3D: | ||
| 25 | case TextureType::ColorCube: | ||
| 26 | case TextureType::ColorArrayCube: | ||
| 27 | case TextureType::Buffer: | ||
| 28 | break; | ||
| 29 | } | ||
| 30 | return false; | ||
| 31 | } | ||
| 32 | |||
| 33 | void VisitMark(IR::Block& block, IR::Inst& inst) { | ||
| 34 | switch (inst.GetOpcode()) { | ||
| 35 | case IR::Opcode::ShuffleIndex: | ||
| 36 | case IR::Opcode::ShuffleUp: | ||
| 37 | case IR::Opcode::ShuffleDown: | ||
| 38 | case IR::Opcode::ShuffleButterfly: { | ||
| 39 | const IR::Value shfl_arg{inst.Arg(0)}; | ||
| 40 | if (shfl_arg.IsImmediate()) { | ||
| 41 | break; | ||
| 42 | } | ||
| 43 | const IR::Inst* const arg_inst{shfl_arg.InstRecursive()}; | ||
| 44 | if (arg_inst->GetOpcode() != IR::Opcode::BitCastU32F32) { | ||
| 45 | break; | ||
| 46 | } | ||
| 47 | const IR::Value bitcast_arg{arg_inst->Arg(0)}; | ||
| 48 | if (bitcast_arg.IsImmediate()) { | ||
| 49 | break; | ||
| 50 | } | ||
| 51 | IR::Inst* const bitcast_inst{bitcast_arg.InstRecursive()}; | ||
| 52 | bool must_patch_outside = false; | ||
| 53 | if (bitcast_inst->GetOpcode() == IR::Opcode::GetAttribute) { | ||
| 54 | const IR::Attribute attr{bitcast_inst->Arg(0).Attribute()}; | ||
| 55 | switch (attr) { | ||
| 56 | case IR::Attribute::PositionX: | ||
| 57 | case IR::Attribute::PositionY: | ||
| 58 | bitcast_inst->SetFlags<u32>(0xDEADBEEF); | ||
| 59 | must_patch_outside = true; | ||
| 60 | break; | ||
| 61 | default: | ||
| 62 | break; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | if (must_patch_outside) { | ||
| 66 | const auto it{IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 67 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 68 | const IR::F32 new_inst{&*block.PrependNewInst(it, inst)}; | ||
| 69 | const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())}; | ||
| 70 | const IR::Value converted{ir.FPMul(new_inst, up_factor)}; | ||
| 71 | inst.ReplaceUsesWith(converted); | ||
| 72 | } | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | |||
| 76 | default: | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | void PatchFragCoord(IR::Block& block, IR::Inst& inst) { | ||
| 82 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 83 | const IR::F32 down_factor{ir.ResolutionDownFactor()}; | ||
| 84 | const IR::F32 frag_coord{ir.GetAttribute(inst.Arg(0).Attribute())}; | ||
| 85 | const IR::F32 downscaled_frag_coord{ir.FPMul(frag_coord, down_factor)}; | ||
| 86 | inst.ReplaceUsesWith(downscaled_frag_coord); | ||
| 87 | } | ||
| 88 | |||
| 89 | void PatchPointSize(IR::Block& block, IR::Inst& inst) { | ||
| 90 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 91 | const IR::F32 point_value{inst.Arg(1)}; | ||
| 92 | const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())}; | ||
| 93 | const IR::F32 upscaled_point_value{ir.FPMul(point_value, up_factor)}; | ||
| 94 | inst.SetArg(1, upscaled_point_value); | ||
| 95 | } | ||
| 96 | |||
| 97 | [[nodiscard]] IR::U32 Scale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) { | ||
| 98 | IR::U32 scaled_value{value}; | ||
| 99 | if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) { | ||
| 100 | scaled_value = ir.IMul(scaled_value, ir.Imm32(up_scale)); | ||
| 101 | } | ||
| 102 | if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) { | ||
| 103 | scaled_value = ir.ShiftRightArithmetic(scaled_value, ir.Imm32(down_shift)); | ||
| 104 | } | ||
| 105 | return IR::U32{ir.Select(is_scaled, scaled_value, value)}; | ||
| 106 | } | ||
| 107 | |||
| 108 | [[nodiscard]] IR::U32 SubScale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value, | ||
| 109 | const IR::Attribute attrib) { | ||
| 110 | const IR::F32 up_factor{ir.Imm32(Settings::values.resolution_info.up_factor)}; | ||
| 111 | const IR::F32 base{ir.FPMul(ir.ConvertUToF(32, 32, value), up_factor)}; | ||
| 112 | const IR::F32 frag_coord{ir.GetAttribute(attrib)}; | ||
| 113 | const IR::F32 down_factor{ir.Imm32(Settings::values.resolution_info.down_factor)}; | ||
| 114 | const IR::F32 floor{ir.FPMul(up_factor, ir.FPFloor(ir.FPMul(frag_coord, down_factor)))}; | ||
| 115 | const IR::F16F32F64 deviation{ir.FPAdd(base, ir.FPAdd(frag_coord, ir.FPNeg(floor)))}; | ||
| 116 | return IR::U32{ir.Select(is_scaled, ir.ConvertFToU(32, deviation), value)}; | ||
| 117 | } | ||
| 118 | |||
| 119 | [[nodiscard]] IR::U32 DownScale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) { | ||
| 120 | IR::U32 scaled_value{value}; | ||
| 121 | if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) { | ||
| 122 | scaled_value = ir.ShiftLeftLogical(scaled_value, ir.Imm32(down_shift)); | ||
| 123 | } | ||
| 124 | if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) { | ||
| 125 | scaled_value = ir.IDiv(scaled_value, ir.Imm32(up_scale)); | ||
| 126 | } | ||
| 127 | return IR::U32{ir.Select(is_scaled, scaled_value, value)}; | ||
| 128 | } | ||
| 129 | |||
| 130 | void PatchImageQueryDimensions(IR::Block& block, IR::Inst& inst) { | ||
| 131 | const auto it{IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 132 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 133 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 134 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; | ||
| 135 | switch (info.type) { | ||
| 136 | case TextureType::Color2D: | ||
| 137 | case TextureType::ColorArray2D: { | ||
| 138 | const IR::Value new_inst{&*block.PrependNewInst(it, inst)}; | ||
| 139 | const IR::U32 width{DownScale(ir, is_scaled, IR::U32{ir.CompositeExtract(new_inst, 0)})}; | ||
| 140 | const IR::U32 height{DownScale(ir, is_scaled, IR::U32{ir.CompositeExtract(new_inst, 1)})}; | ||
| 141 | const IR::Value replacement{ir.CompositeConstruct( | ||
| 142 | width, height, ir.CompositeExtract(new_inst, 2), ir.CompositeExtract(new_inst, 3))}; | ||
| 143 | inst.ReplaceUsesWith(replacement); | ||
| 144 | break; | ||
| 145 | } | ||
| 146 | case TextureType::Color1D: | ||
| 147 | case TextureType::ColorArray1D: | ||
| 148 | case TextureType::Color3D: | ||
| 149 | case TextureType::ColorCube: | ||
| 150 | case TextureType::ColorArrayCube: | ||
| 151 | case TextureType::Buffer: | ||
| 152 | // Nothing to patch here | ||
| 153 | break; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | void ScaleIntegerComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled, | ||
| 158 | size_t index) { | ||
| 159 | const IR::Value composite{inst.Arg(index)}; | ||
| 160 | if (composite.IsEmpty()) { | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 164 | const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 0)})}; | ||
| 165 | const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 1)})}; | ||
| 166 | switch (info.type) { | ||
| 167 | case TextureType::Color2D: | ||
| 168 | inst.SetArg(index, ir.CompositeConstruct(x, y)); | ||
| 169 | break; | ||
| 170 | case TextureType::ColorArray2D: { | ||
| 171 | const IR::U32 z{ir.CompositeExtract(composite, 2)}; | ||
| 172 | inst.SetArg(index, ir.CompositeConstruct(x, y, z)); | ||
| 173 | break; | ||
| 174 | } | ||
| 175 | case TextureType::Color1D: | ||
| 176 | case TextureType::ColorArray1D: | ||
| 177 | case TextureType::Color3D: | ||
| 178 | case TextureType::ColorCube: | ||
| 179 | case TextureType::ColorArrayCube: | ||
| 180 | case TextureType::Buffer: | ||
| 181 | // Nothing to patch here | ||
| 182 | break; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | void SubScaleCoord(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled) { | ||
| 187 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 188 | const IR::Value coord{inst.Arg(1)}; | ||
| 189 | const IR::U32 coord_x{ir.CompositeExtract(coord, 0)}; | ||
| 190 | const IR::U32 coord_y{ir.CompositeExtract(coord, 1)}; | ||
| 191 | |||
| 192 | const IR::U32 scaled_x{SubScale(ir, is_scaled, coord_x, IR::Attribute::PositionX)}; | ||
| 193 | const IR::U32 scaled_y{SubScale(ir, is_scaled, coord_y, IR::Attribute::PositionY)}; | ||
| 194 | switch (info.type) { | ||
| 195 | case TextureType::Color2D: | ||
| 196 | inst.SetArg(1, ir.CompositeConstruct(scaled_x, scaled_y)); | ||
| 197 | break; | ||
| 198 | case TextureType::ColorArray2D: { | ||
| 199 | const IR::U32 z{ir.CompositeExtract(coord, 2)}; | ||
| 200 | inst.SetArg(1, ir.CompositeConstruct(scaled_x, scaled_y, z)); | ||
| 201 | break; | ||
| 202 | } | ||
| 203 | case TextureType::Color1D: | ||
| 204 | case TextureType::ColorArray1D: | ||
| 205 | case TextureType::Color3D: | ||
| 206 | case TextureType::ColorCube: | ||
| 207 | case TextureType::ColorArrayCube: | ||
| 208 | case TextureType::Buffer: | ||
| 209 | // Nothing to patch here | ||
| 210 | break; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | void SubScaleImageFetch(IR::Block& block, IR::Inst& inst) { | ||
| 215 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 216 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 217 | if (!IsTextureTypeRescalable(info.type)) { | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; | ||
| 221 | SubScaleCoord(ir, inst, is_scaled); | ||
| 222 | // Scale ImageFetch offset | ||
| 223 | ScaleIntegerComposite(ir, inst, is_scaled, 2); | ||
| 224 | } | ||
| 225 | |||
| 226 | void SubScaleImageRead(IR::Block& block, IR::Inst& inst) { | ||
| 227 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 228 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 229 | if (!IsTextureTypeRescalable(info.type)) { | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))}; | ||
| 233 | SubScaleCoord(ir, inst, is_scaled); | ||
| 234 | } | ||
| 235 | |||
| 236 | void PatchImageFetch(IR::Block& block, IR::Inst& inst) { | ||
| 237 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 238 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 239 | if (!IsTextureTypeRescalable(info.type)) { | ||
| 240 | return; | ||
| 241 | } | ||
| 242 | const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))}; | ||
| 243 | ScaleIntegerComposite(ir, inst, is_scaled, 1); | ||
| 244 | // Scale ImageFetch offset | ||
| 245 | ScaleIntegerComposite(ir, inst, is_scaled, 2); | ||
| 246 | } | ||
| 247 | |||
| 248 | void PatchImageRead(IR::Block& block, IR::Inst& inst) { | ||
| 249 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 250 | const auto info{inst.Flags<IR::TextureInstInfo>()}; | ||
| 251 | if (!IsTextureTypeRescalable(info.type)) { | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))}; | ||
| 255 | ScaleIntegerComposite(ir, inst, is_scaled, 1); | ||
| 256 | } | ||
| 257 | |||
| 258 | void Visit(const IR::Program& program, IR::Block& block, IR::Inst& inst) { | ||
| 259 | const bool is_fragment_shader{program.stage == Stage::Fragment}; | ||
| 260 | switch (inst.GetOpcode()) { | ||
| 261 | case IR::Opcode::GetAttribute: { | ||
| 262 | const IR::Attribute attr{inst.Arg(0).Attribute()}; | ||
| 263 | switch (attr) { | ||
| 264 | case IR::Attribute::PositionX: | ||
| 265 | case IR::Attribute::PositionY: | ||
| 266 | if (is_fragment_shader && inst.Flags<u32>() != 0xDEADBEEF) { | ||
| 267 | PatchFragCoord(block, inst); | ||
| 268 | } | ||
| 269 | break; | ||
| 270 | default: | ||
| 271 | break; | ||
| 272 | } | ||
| 273 | break; | ||
| 274 | } | ||
| 275 | case IR::Opcode::SetAttribute: { | ||
| 276 | const IR::Attribute attr{inst.Arg(0).Attribute()}; | ||
| 277 | switch (attr) { | ||
| 278 | case IR::Attribute::PointSize: | ||
| 279 | if (inst.Flags<u32>() != 0xDEADBEEF) { | ||
| 280 | PatchPointSize(block, inst); | ||
| 281 | } | ||
| 282 | break; | ||
| 283 | default: | ||
| 284 | break; | ||
| 285 | } | ||
| 286 | break; | ||
| 287 | } | ||
| 288 | case IR::Opcode::ImageQueryDimensions: | ||
| 289 | PatchImageQueryDimensions(block, inst); | ||
| 290 | break; | ||
| 291 | case IR::Opcode::ImageFetch: | ||
| 292 | if (is_fragment_shader) { | ||
| 293 | SubScaleImageFetch(block, inst); | ||
| 294 | } else { | ||
| 295 | PatchImageFetch(block, inst); | ||
| 296 | } | ||
| 297 | break; | ||
| 298 | case IR::Opcode::ImageRead: | ||
| 299 | if (is_fragment_shader) { | ||
| 300 | SubScaleImageRead(block, inst); | ||
| 301 | } else { | ||
| 302 | PatchImageRead(block, inst); | ||
| 303 | } | ||
| 304 | break; | ||
| 305 | default: | ||
| 306 | break; | ||
| 307 | } | ||
| 308 | } | ||
| 309 | } // Anonymous namespace | ||
| 310 | |||
| 311 | void RescalingPass(IR::Program& program) { | ||
| 312 | const bool is_fragment_shader{program.stage == Stage::Fragment}; | ||
| 313 | if (is_fragment_shader) { | ||
| 314 | for (IR::Block* const block : program.post_order_blocks) { | ||
| 315 | for (IR::Inst& inst : block->Instructions()) { | ||
| 316 | VisitMark(*block, inst); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | for (IR::Block* const block : program.post_order_blocks) { | ||
| 321 | for (IR::Inst& inst : block->Instructions()) { | ||
| 322 | Visit(program, *block, inst); | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | } // namespace Shader::Optimization | ||
diff --git a/src/shader_recompiler/shader_info.h b/src/shader_recompiler/shader_info.h index 4ef4dbd40..9f375c30e 100644 --- a/src/shader_recompiler/shader_info.h +++ b/src/shader_recompiler/shader_info.h | |||
| @@ -172,6 +172,7 @@ struct Info { | |||
| 172 | bool uses_global_memory{}; | 172 | bool uses_global_memory{}; |
| 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 | 176 | ||
| 176 | IR::Type used_constant_buffer_types{}; | 177 | IR::Type used_constant_buffer_types{}; |
| 177 | IR::Type used_storage_buffer_types{}; | 178 | IR::Type used_storage_buffer_types{}; |
| @@ -190,4 +191,13 @@ struct Info { | |||
| 190 | ImageDescriptors image_descriptors; | 191 | ImageDescriptors image_descriptors; |
| 191 | }; | 192 | }; |
| 192 | 193 | ||
| 194 | template <typename Descriptors> | ||
| 195 | u32 NumDescriptors(const Descriptors& descriptors) { | ||
| 196 | u32 num{}; | ||
| 197 | for (const auto& desc : descriptors) { | ||
| 198 | num += desc.count; | ||
| 199 | } | ||
| 200 | return num; | ||
| 201 | } | ||
| 202 | |||
| 193 | } // namespace Shader | 203 | } // namespace Shader |