diff options
Diffstat (limited to 'src/shader_recompiler')
35 files changed, 355 insertions, 123 deletions
diff --git a/src/shader_recompiler/CMakeLists.txt b/src/shader_recompiler/CMakeLists.txt index bcdd60db9..545d69c7e 100644 --- a/src/shader_recompiler/CMakeLists.txt +++ b/src/shader_recompiler/CMakeLists.txt | |||
| @@ -224,6 +224,7 @@ add_library(shader_recompiler STATIC | |||
| 224 | ir_opt/lower_fp16_to_fp32.cpp | 224 | ir_opt/lower_fp16_to_fp32.cpp |
| 225 | ir_opt/lower_int64_to_int32.cpp | 225 | ir_opt/lower_int64_to_int32.cpp |
| 226 | ir_opt/passes.h | 226 | ir_opt/passes.h |
| 227 | ir_opt/position_pass.cpp | ||
| 227 | ir_opt/rescaling_pass.cpp | 228 | ir_opt/rescaling_pass.cpp |
| 228 | ir_opt/ssa_rewrite_pass.cpp | 229 | ir_opt/ssa_rewrite_pass.cpp |
| 229 | ir_opt/texture_pass.cpp | 230 | ir_opt/texture_pass.cpp |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm.cpp b/src/shader_recompiler/backend/glasm/emit_glasm.cpp index 01f9abc71..3b0176bf6 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm.cpp | |||
| @@ -450,6 +450,9 @@ std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, I | |||
| 450 | if (program.info.uses_rescaling_uniform) { | 450 | if (program.info.uses_rescaling_uniform) { |
| 451 | header += "PARAM scaling[1]={program.local[0..0]};"; | 451 | header += "PARAM scaling[1]={program.local[0..0]};"; |
| 452 | } | 452 | } |
| 453 | if (program.info.uses_render_area) { | ||
| 454 | header += "PARAM render_area[1]={program.local[1..1]};"; | ||
| 455 | } | ||
| 453 | header += "TEMP "; | 456 | header += "TEMP "; |
| 454 | for (size_t index = 0; index < ctx.reg_alloc.NumUsedRegisters(); ++index) { | 457 | for (size_t index = 0; index < ctx.reg_alloc.NumUsedRegisters(); ++index) { |
| 455 | header += fmt::format("R{},", index); | 458 | header += fmt::format("R{},", index); |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_bitwise_conversion.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_bitwise_conversion.cpp index 2fc2a0ac6..5bfdecc09 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_bitwise_conversion.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm_bitwise_conversion.cpp | |||
| @@ -43,6 +43,10 @@ void EmitBitCastU64F64(EmitContext&, IR::Inst& inst, const IR::Value& value) { | |||
| 43 | Alias(inst, value); | 43 | Alias(inst, value); |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | void EmitBitCastS32F32(EmitContext&, IR::Inst& inst, const IR::Value& value) { | ||
| 47 | Alias(inst, value); | ||
| 48 | } | ||
| 49 | |||
| 46 | void EmitBitCastF16U16(EmitContext&, IR::Inst& inst, const IR::Value& value) { | 50 | void EmitBitCastF16U16(EmitContext&, IR::Inst& inst, const IR::Value& value) { |
| 47 | Alias(inst, value); | 51 | Alias(inst, value); |
| 48 | } | 52 | } |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp index 7e8f37563..d6562c842 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp +++ b/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp | |||
| @@ -379,6 +379,18 @@ void EmitInvocationId(EmitContext& ctx, IR::Inst& inst) { | |||
| 379 | ctx.Add("MOV.S {}.x,primitive_invocation.x;", inst); | 379 | ctx.Add("MOV.S {}.x,primitive_invocation.x;", inst); |
| 380 | } | 380 | } |
| 381 | 381 | ||
| 382 | void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst) { | ||
| 383 | switch (ctx.stage) { | ||
| 384 | case Stage::TessellationControl: | ||
| 385 | case Stage::TessellationEval: | ||
| 386 | ctx.Add("SHL.U {}.x,primitive.vertexcount,16;", inst); | ||
| 387 | break; | ||
| 388 | default: | ||
| 389 | LOG_WARNING(Shader, "(STUBBED) called"); | ||
| 390 | ctx.Add("MOV.S {}.x,0x00ff0000;", inst); | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 382 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst) { | 394 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst) { |
| 383 | ctx.Add("MOV.S {}.x,fragment.sampleid.x;", inst); | 395 | ctx.Add("MOV.S {}.x,fragment.sampleid.x;", inst); |
| 384 | } | 396 | } |
| @@ -396,6 +408,10 @@ void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) { | |||
| 396 | ctx.Add("MOV.F {}.x,scaling[0].z;", inst); | 408 | ctx.Add("MOV.F {}.x,scaling[0].z;", inst); |
| 397 | } | 409 | } |
| 398 | 410 | ||
| 411 | void EmitRenderArea(EmitContext& ctx, IR::Inst& inst) { | ||
| 412 | ctx.Add("MOV.F {},render_area[0];", inst); | ||
| 413 | } | ||
| 414 | |||
| 399 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) { | 415 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) { |
| 400 | ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset); | 416 | ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset); |
| 401 | } | 417 | } |
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h index 8b0ac3031..eaaf9ba39 100644 --- a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h +++ b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h | |||
| @@ -69,10 +69,12 @@ void EmitSetOFlag(EmitContext& ctx); | |||
| 69 | void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst); | 69 | void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst); |
| 70 | void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst); | 70 | void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst); |
| 71 | void EmitInvocationId(EmitContext& ctx, IR::Inst& inst); | 71 | void EmitInvocationId(EmitContext& ctx, IR::Inst& inst); |
| 72 | void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst); | ||
| 72 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); | 73 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); |
| 73 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); | 74 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); |
| 74 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); | 75 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); |
| 75 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst); | 76 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst); |
| 77 | void EmitRenderArea(EmitContext& ctx, IR::Inst& inst); | ||
| 76 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset); | 78 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset); |
| 77 | void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value); | 79 | void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value); |
| 78 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); | 80 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); |
| @@ -195,6 +197,7 @@ void EmitSelectF64(EmitContext& ctx, ScalarS32 cond, Register true_value, Regist | |||
| 195 | void EmitBitCastU16F16(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | 197 | void EmitBitCastU16F16(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); |
| 196 | void EmitBitCastU32F32(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | 198 | void EmitBitCastU32F32(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); |
| 197 | void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | 199 | void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); |
| 200 | void EmitBitCastS32F32(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | ||
| 198 | void EmitBitCastF16U16(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | 201 | void EmitBitCastF16U16(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); |
| 199 | void EmitBitCastF32U32(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | 202 | void EmitBitCastF32U32(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); |
| 200 | void EmitBitCastF64U64(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); | 203 | void EmitBitCastF64U64(EmitContext& ctx, IR::Inst& inst, const IR::Value& value); |
diff --git a/src/shader_recompiler/backend/glasm/glasm_emit_context.cpp b/src/shader_recompiler/backend/glasm/glasm_emit_context.cpp index 89603c1c4..333a91cc5 100644 --- a/src/shader_recompiler/backend/glasm/glasm_emit_context.cpp +++ b/src/shader_recompiler/backend/glasm/glasm_emit_context.cpp | |||
| @@ -95,6 +95,10 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile | |||
| 95 | if (info.uses_invocation_id) { | 95 | if (info.uses_invocation_id) { |
| 96 | Add("ATTRIB primitive_invocation=primitive.invocation;"); | 96 | Add("ATTRIB primitive_invocation=primitive.invocation;"); |
| 97 | } | 97 | } |
| 98 | if (info.uses_invocation_info && | ||
| 99 | (stage == Stage::TessellationControl || stage == Stage::TessellationEval)) { | ||
| 100 | Add("ATTRIB primitive_vertexcount = primitive.vertexcount;"); | ||
| 101 | } | ||
| 98 | if (info.stores_tess_level_outer) { | 102 | if (info.stores_tess_level_outer) { |
| 99 | Add("OUTPUT result_patch_tessouter[]={{result.patch.tessouter[0..3]}};"); | 103 | Add("OUTPUT result_patch_tessouter[]={{result.patch.tessouter[0..3]}};"); |
| 100 | } | 104 | } |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp index 1be4a0f59..8e5e6cf1f 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp | |||
| @@ -48,6 +48,10 @@ void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) | |||
| 48 | ctx.AddU64("{}=doubleBitsToUint64({});", inst, value); | 48 | ctx.AddU64("{}=doubleBitsToUint64({});", inst, value); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | void EmitBitCastS32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { | ||
| 52 | ctx.AddF32("{}=ftoi({});", inst, value); | ||
| 53 | } | ||
| 54 | |||
| 51 | void EmitBitCastF16U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) { | 55 | void EmitBitCastF16U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) { |
| 52 | NotImplemented(); | 56 | NotImplemented(); |
| 53 | } | 57 | } |
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 fad8d1e30..c1671c37b 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 | |||
| @@ -399,6 +399,18 @@ void EmitInvocationId(EmitContext& ctx, IR::Inst& inst) { | |||
| 399 | ctx.AddU32("{}=uint(gl_InvocationID);", inst); | 399 | ctx.AddU32("{}=uint(gl_InvocationID);", inst); |
| 400 | } | 400 | } |
| 401 | 401 | ||
| 402 | void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst) { | ||
| 403 | switch (ctx.stage) { | ||
| 404 | case Stage::TessellationControl: | ||
| 405 | case Stage::TessellationEval: | ||
| 406 | ctx.AddU32("{}=uint(gl_PatchVerticesIn)<<16;", inst); | ||
| 407 | break; | ||
| 408 | default: | ||
| 409 | LOG_WARNING(Shader, "(STUBBED) called"); | ||
| 410 | ctx.AddU32("{}=uint(0x00ff0000);", inst); | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 402 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst) { | 414 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst) { |
| 403 | ctx.AddU32("{}=uint(gl_SampleID);", inst); | 415 | ctx.AddU32("{}=uint(gl_SampleID);", inst); |
| 404 | } | 416 | } |
| @@ -416,6 +428,10 @@ void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) { | |||
| 416 | ctx.AddF32("{}=scaling.z;", inst); | 428 | ctx.AddF32("{}=scaling.z;", inst); |
| 417 | } | 429 | } |
| 418 | 430 | ||
| 431 | void EmitRenderArea(EmitContext& ctx, IR::Inst& inst) { | ||
| 432 | ctx.AddF32x4("{}=render_area;", inst); | ||
| 433 | } | ||
| 434 | |||
| 419 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) { | 435 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) { |
| 420 | ctx.AddU32("{}=lmem[{}];", inst, word_offset); | 436 | ctx.AddU32("{}=lmem[{}];", inst, word_offset); |
| 421 | } | 437 | } |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h index 639691ba6..4151c89de 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h +++ b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h | |||
| @@ -83,10 +83,12 @@ void EmitSetOFlag(EmitContext& ctx); | |||
| 83 | void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst); | 83 | void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst); |
| 84 | void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst); | 84 | void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst); |
| 85 | void EmitInvocationId(EmitContext& ctx, IR::Inst& inst); | 85 | void EmitInvocationId(EmitContext& ctx, IR::Inst& inst); |
| 86 | void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst); | ||
| 86 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); | 87 | void EmitSampleId(EmitContext& ctx, IR::Inst& inst); |
| 87 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); | 88 | void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst); |
| 88 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); | 89 | void EmitYDirection(EmitContext& ctx, IR::Inst& inst); |
| 89 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst); | 90 | void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst); |
| 91 | void EmitRenderArea(EmitContext& ctx, IR::Inst& inst); | ||
| 90 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset); | 92 | void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset); |
| 91 | void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value); | 93 | void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value); |
| 92 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); | 94 | void EmitUndefU1(EmitContext& ctx, IR::Inst& inst); |
| @@ -229,6 +231,7 @@ void EmitSelectF64(EmitContext& ctx, IR::Inst& inst, std::string_view cond, | |||
| 229 | void EmitBitCastU16F16(EmitContext& ctx, IR::Inst& inst); | 231 | void EmitBitCastU16F16(EmitContext& ctx, IR::Inst& inst); |
| 230 | void EmitBitCastU32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 232 | void EmitBitCastU32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
| 231 | void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 233 | void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
| 234 | void EmitBitCastS32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value); | ||
| 232 | void EmitBitCastF16U16(EmitContext& ctx, IR::Inst& inst); | 235 | void EmitBitCastF16U16(EmitContext& ctx, IR::Inst& inst); |
| 233 | void EmitBitCastF32U32(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 236 | void EmitBitCastF32U32(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
| 234 | void EmitBitCastF64U64(EmitContext& ctx, IR::Inst& inst, std::string_view value); | 237 | void EmitBitCastF64U64(EmitContext& ctx, IR::Inst& inst, std::string_view value); |
diff --git a/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp b/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp index c767a9dc3..5d01ec0cd 100644 --- a/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp +++ b/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp | |||
| @@ -358,6 +358,9 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile | |||
| 358 | if (info.uses_rescaling_uniform) { | 358 | if (info.uses_rescaling_uniform) { |
| 359 | header += "layout(location=0) uniform vec4 scaling;"; | 359 | header += "layout(location=0) uniform vec4 scaling;"; |
| 360 | } | 360 | } |
| 361 | if (info.uses_render_area) { | ||
| 362 | header += "layout(location=1) uniform vec4 render_area;"; | ||
| 363 | } | ||
| 361 | DefineConstantBuffers(bindings); | 364 | DefineConstantBuffers(bindings); |
| 362 | DefineConstantBufferIndirect(); | 365 | DefineConstantBufferIndirect(); |
| 363 | DefineStorageBuffers(bindings); | 366 | DefineStorageBuffers(bindings); |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.h b/src/shader_recompiler/backend/spirv/emit_spirv.h index 7567b6fc9..937881484 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv.h | |||
| @@ -23,8 +23,12 @@ struct RescalingLayout { | |||
| 23 | alignas(16) std::array<u32, NUM_IMAGE_SCALING_WORDS> rescaling_images; | 23 | alignas(16) std::array<u32, NUM_IMAGE_SCALING_WORDS> rescaling_images; |
| 24 | u32 down_factor; | 24 | u32 down_factor; |
| 25 | }; | 25 | }; |
| 26 | struct RenderAreaLayout { | ||
| 27 | std::array<f32, 4> render_area; | ||
| 28 | }; | ||
| 26 | constexpr u32 RESCALING_LAYOUT_WORDS_OFFSET = offsetof(RescalingLayout, rescaling_textures); | 29 | constexpr u32 RESCALING_LAYOUT_WORDS_OFFSET = offsetof(RescalingLayout, rescaling_textures); |
| 27 | constexpr u32 RESCALING_LAYOUT_DOWN_FACTOR_OFFSET = offsetof(RescalingLayout, down_factor); | 30 | constexpr u32 RESCALING_LAYOUT_DOWN_FACTOR_OFFSET = offsetof(RescalingLayout, down_factor); |
| 31 | constexpr u32 RENDERAREA_LAYOUT_OFFSET = offsetof(RenderAreaLayout, render_area); | ||
| 28 | 32 | ||
| 29 | [[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, | 33 | [[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, |
| 30 | IR::Program& program, Bindings& bindings); | 34 | IR::Program& program, Bindings& bindings); |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_bitwise_conversion.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_bitwise_conversion.cpp index c4ca28d11..50daacd95 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_bitwise_conversion.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_bitwise_conversion.cpp | |||
| @@ -18,6 +18,10 @@ void EmitBitCastU64F64(EmitContext&) { | |||
| 18 | throw NotImplementedException("SPIR-V Instruction"); | 18 | throw NotImplementedException("SPIR-V Instruction"); |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | void EmitBitCastS32F32(EmitContext&) { | ||
| 22 | throw NotImplementedException("SPIR-V Instruction"); | ||
| 23 | } | ||
| 24 | |||
| 21 | void EmitBitCastF16U16(EmitContext&) { | 25 | void EmitBitCastF16U16(EmitContext&) { |
| 22 | throw NotImplementedException("SPIR-V Instruction"); | 26 | throw NotImplementedException("SPIR-V Instruction"); |
| 23 | } | 27 | } |
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 2c68aba39..5b3b5d1f3 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 | |||
| @@ -353,7 +353,6 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) { | |||
| 353 | case IR::Attribute::TessellationEvaluationPointV: | 353 | case IR::Attribute::TessellationEvaluationPointV: |
| 354 | return ctx.OpLoad(ctx.F32[1], | 354 | return ctx.OpLoad(ctx.F32[1], |
| 355 | ctx.OpAccessChain(ctx.input_f32, ctx.tess_coord, ctx.Const(1U))); | 355 | ctx.OpAccessChain(ctx.input_f32, ctx.tess_coord, ctx.Const(1U))); |
| 356 | |||
| 357 | default: | 356 | default: |
| 358 | throw NotImplementedException("Read attribute {}", attr); | 357 | throw NotImplementedException("Read attribute {}", attr); |
| 359 | } | 358 | } |
| @@ -513,6 +512,18 @@ Id EmitInvocationId(EmitContext& ctx) { | |||
| 513 | return ctx.OpLoad(ctx.U32[1], ctx.invocation_id); | 512 | return ctx.OpLoad(ctx.U32[1], ctx.invocation_id); |
| 514 | } | 513 | } |
| 515 | 514 | ||
| 515 | Id EmitInvocationInfo(EmitContext& ctx) { | ||
| 516 | switch (ctx.stage) { | ||
| 517 | case Stage::TessellationControl: | ||
| 518 | case Stage::TessellationEval: | ||
| 519 | return ctx.OpShiftLeftLogical(ctx.U32[1], ctx.OpLoad(ctx.U32[1], ctx.patch_vertices_in), | ||
| 520 | ctx.Const(16u)); | ||
| 521 | default: | ||
| 522 | LOG_WARNING(Shader, "(STUBBED) called"); | ||
| 523 | return ctx.Const(0x00ff0000u); | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 516 | Id EmitSampleId(EmitContext& ctx) { | 527 | Id EmitSampleId(EmitContext& ctx) { |
| 517 | return ctx.OpLoad(ctx.U32[1], ctx.sample_id); | 528 | return ctx.OpLoad(ctx.U32[1], ctx.sample_id); |
| 518 | } | 529 | } |
| @@ -537,6 +548,17 @@ Id EmitResolutionDownFactor(EmitContext& ctx) { | |||
| 537 | } | 548 | } |
| 538 | } | 549 | } |
| 539 | 550 | ||
| 551 | Id EmitRenderArea(EmitContext& ctx) { | ||
| 552 | if (ctx.profile.unified_descriptor_binding) { | ||
| 553 | const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[4])}; | ||
| 554 | const Id index{ctx.Const(ctx.render_are_member_index)}; | ||
| 555 | const Id pointer{ctx.OpAccessChain(pointer_type, ctx.render_area_push_constant, index)}; | ||
| 556 | return ctx.OpLoad(ctx.F32[4], pointer); | ||
| 557 | } else { | ||
| 558 | throw NotImplementedException("SPIR-V Instruction"); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 540 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset) { | 562 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset) { |
| 541 | const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)}; | 563 | const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)}; |
| 542 | return ctx.OpLoad(ctx.U32[1], pointer); | 564 | return ctx.OpLoad(ctx.U32[1], pointer); |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h index 984d072b4..e31cdc5e8 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h | |||
| @@ -72,10 +72,12 @@ void EmitSetOFlag(EmitContext& ctx); | |||
| 72 | Id EmitWorkgroupId(EmitContext& ctx); | 72 | Id EmitWorkgroupId(EmitContext& ctx); |
| 73 | Id EmitLocalInvocationId(EmitContext& ctx); | 73 | Id EmitLocalInvocationId(EmitContext& ctx); |
| 74 | Id EmitInvocationId(EmitContext& ctx); | 74 | Id EmitInvocationId(EmitContext& ctx); |
| 75 | Id EmitInvocationInfo(EmitContext& ctx); | ||
| 75 | Id EmitSampleId(EmitContext& ctx); | 76 | Id EmitSampleId(EmitContext& ctx); |
| 76 | Id EmitIsHelperInvocation(EmitContext& ctx); | 77 | Id EmitIsHelperInvocation(EmitContext& ctx); |
| 77 | Id EmitYDirection(EmitContext& ctx); | 78 | Id EmitYDirection(EmitContext& ctx); |
| 78 | Id EmitResolutionDownFactor(EmitContext& ctx); | 79 | Id EmitResolutionDownFactor(EmitContext& ctx); |
| 80 | Id EmitRenderArea(EmitContext& ctx); | ||
| 79 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset); | 81 | Id EmitLoadLocal(EmitContext& ctx, Id word_offset); |
| 80 | void EmitWriteLocal(EmitContext& ctx, Id word_offset, Id value); | 82 | void EmitWriteLocal(EmitContext& ctx, Id word_offset, Id value); |
| 81 | Id EmitUndefU1(EmitContext& ctx); | 83 | Id EmitUndefU1(EmitContext& ctx); |
| @@ -177,7 +179,8 @@ Id EmitSelectF64(EmitContext& ctx, Id cond, Id true_value, Id false_value); | |||
| 177 | void EmitBitCastU16F16(EmitContext& ctx); | 179 | void EmitBitCastU16F16(EmitContext& ctx); |
| 178 | Id EmitBitCastU32F32(EmitContext& ctx, Id value); | 180 | Id EmitBitCastU32F32(EmitContext& ctx, Id value); |
| 179 | void EmitBitCastU64F64(EmitContext& ctx); | 181 | void EmitBitCastU64F64(EmitContext& ctx); |
| 180 | void EmitBitCastF16U16(EmitContext& ctx); | 182 | void EmitBitCastS32F32(EmitContext& ctx); |
| 183 | void EmitBitCastF16U16(EmitContext&); | ||
| 181 | Id EmitBitCastF32U32(EmitContext& ctx, Id value); | 184 | Id EmitBitCastF32U32(EmitContext& ctx, Id value); |
| 182 | void EmitBitCastF64U64(EmitContext& ctx); | 185 | void EmitBitCastF64U64(EmitContext& ctx); |
| 183 | Id EmitPackUint2x32(EmitContext& ctx, Id value); | 186 | Id EmitPackUint2x32(EmitContext& ctx, Id value); |
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp index aecc4c612..0bfc2dd89 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp | |||
| @@ -473,6 +473,7 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf | |||
| 473 | DefineAttributeMemAccess(program.info); | 473 | DefineAttributeMemAccess(program.info); |
| 474 | DefineGlobalMemoryFunctions(program.info); | 474 | DefineGlobalMemoryFunctions(program.info); |
| 475 | DefineRescalingInput(program.info); | 475 | DefineRescalingInput(program.info); |
| 476 | DefineRenderArea(program.info); | ||
| 476 | } | 477 | } |
| 477 | 478 | ||
| 478 | EmitContext::~EmitContext() = default; | 479 | EmitContext::~EmitContext() = default; |
| @@ -982,6 +983,36 @@ void EmitContext::DefineRescalingInputUniformConstant() { | |||
| 982 | } | 983 | } |
| 983 | } | 984 | } |
| 984 | 985 | ||
| 986 | void EmitContext::DefineRenderArea(const Info& info) { | ||
| 987 | if (!info.uses_render_area) { | ||
| 988 | return; | ||
| 989 | } | ||
| 990 | |||
| 991 | if (profile.unified_descriptor_binding) { | ||
| 992 | boost::container::static_vector<Id, 1> members{}; | ||
| 993 | u32 member_index{0}; | ||
| 994 | |||
| 995 | members.push_back(F32[4]); | ||
| 996 | render_are_member_index = member_index++; | ||
| 997 | |||
| 998 | const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))}; | ||
| 999 | Decorate(push_constant_struct, spv::Decoration::Block); | ||
| 1000 | Name(push_constant_struct, "RenderAreaInfo"); | ||
| 1001 | |||
| 1002 | MemberDecorate(push_constant_struct, render_are_member_index, spv::Decoration::Offset, 0); | ||
| 1003 | MemberName(push_constant_struct, render_are_member_index, "render_area"); | ||
| 1004 | |||
| 1005 | const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)}; | ||
| 1006 | render_area_push_constant = | ||
| 1007 | AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant); | ||
| 1008 | Name(render_area_push_constant, "render_area_push_constants"); | ||
| 1009 | |||
| 1010 | if (profile.supported_spirv >= 0x00010400) { | ||
| 1011 | interfaces.push_back(render_area_push_constant); | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | |||
| 985 | void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { | 1016 | void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { |
| 986 | if (info.constant_buffer_descriptors.empty()) { | 1017 | if (info.constant_buffer_descriptors.empty()) { |
| 987 | return; | 1018 | return; |
| @@ -1294,6 +1325,10 @@ void EmitContext::DefineInputs(const IR::Program& program) { | |||
| 1294 | if (info.uses_invocation_id) { | 1325 | if (info.uses_invocation_id) { |
| 1295 | invocation_id = DefineInput(*this, U32[1], false, spv::BuiltIn::InvocationId); | 1326 | invocation_id = DefineInput(*this, U32[1], false, spv::BuiltIn::InvocationId); |
| 1296 | } | 1327 | } |
| 1328 | if (info.uses_invocation_info && | ||
| 1329 | (stage == Shader::Stage::TessellationControl || stage == Shader::Stage::TessellationEval)) { | ||
| 1330 | patch_vertices_in = DefineInput(*this, U32[1], false, spv::BuiltIn::PatchVertices); | ||
| 1331 | } | ||
| 1297 | if (info.uses_sample_id) { | 1332 | if (info.uses_sample_id) { |
| 1298 | sample_id = DefineInput(*this, U32[1], false, spv::BuiltIn::SampleId); | 1333 | sample_id = DefineInput(*this, U32[1], false, spv::BuiltIn::SampleId); |
| 1299 | } | 1334 | } |
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h index bc25b8b84..dde45b4bc 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h | |||
| @@ -204,6 +204,7 @@ public: | |||
| 204 | Id workgroup_id{}; | 204 | Id workgroup_id{}; |
| 205 | Id local_invocation_id{}; | 205 | Id local_invocation_id{}; |
| 206 | Id invocation_id{}; | 206 | Id invocation_id{}; |
| 207 | Id patch_vertices_in{}; | ||
| 207 | Id sample_id{}; | 208 | Id sample_id{}; |
| 208 | Id is_helper_invocation{}; | 209 | Id is_helper_invocation{}; |
| 209 | Id subgroup_local_invocation_id{}; | 210 | Id subgroup_local_invocation_id{}; |
| @@ -243,6 +244,9 @@ public: | |||
| 243 | u32 texture_rescaling_index{}; | 244 | u32 texture_rescaling_index{}; |
| 244 | u32 image_rescaling_index{}; | 245 | u32 image_rescaling_index{}; |
| 245 | 246 | ||
| 247 | Id render_area_push_constant{}; | ||
| 248 | u32 render_are_member_index{}; | ||
| 249 | |||
| 246 | Id local_memory{}; | 250 | Id local_memory{}; |
| 247 | 251 | ||
| 248 | Id shared_memory_u8{}; | 252 | Id shared_memory_u8{}; |
| @@ -318,6 +322,7 @@ private: | |||
| 318 | void DefineRescalingInput(const Info& info); | 322 | void DefineRescalingInput(const Info& info); |
| 319 | void DefineRescalingInputPushConstant(); | 323 | void DefineRescalingInputPushConstant(); |
| 320 | void DefineRescalingInputUniformConstant(); | 324 | void DefineRescalingInputUniformConstant(); |
| 325 | void DefineRenderArea(const Info& info); | ||
| 321 | 326 | ||
| 322 | void DefineInputs(const IR::Program& program); | 327 | void DefineInputs(const IR::Program& program); |
| 323 | void DefineOutputs(const IR::Program& program); | 328 | void DefineOutputs(const IR::Program& program); |
diff --git a/src/shader_recompiler/environment.h b/src/shader_recompiler/environment.h index 9729d48c6..402f2664f 100644 --- a/src/shader_recompiler/environment.h +++ b/src/shader_recompiler/environment.h | |||
| @@ -22,6 +22,10 @@ public: | |||
| 22 | 22 | ||
| 23 | [[nodiscard]] virtual TextureType ReadTextureType(u32 raw_handle) = 0; | 23 | [[nodiscard]] virtual TextureType ReadTextureType(u32 raw_handle) = 0; |
| 24 | 24 | ||
| 25 | [[nodiscard]] virtual TexturePixelFormat ReadTexturePixelFormat(u32 raw_handle) = 0; | ||
| 26 | |||
| 27 | [[nodiscard]] virtual u32 ReadViewportTransformState() = 0; | ||
| 28 | |||
| 25 | [[nodiscard]] virtual u32 TextureBoundBuffer() const = 0; | 29 | [[nodiscard]] virtual u32 TextureBoundBuffer() const = 0; |
| 26 | 30 | ||
| 27 | [[nodiscard]] virtual u32 LocalMemorySize() const = 0; | 31 | [[nodiscard]] virtual u32 LocalMemorySize() const = 0; |
diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.cpp b/src/shader_recompiler/frontend/ir/ir_emitter.cpp index 11086ed8c..0cdac0eff 100644 --- a/src/shader_recompiler/frontend/ir/ir_emitter.cpp +++ b/src/shader_recompiler/frontend/ir/ir_emitter.cpp | |||
| @@ -362,6 +362,10 @@ U32 IREmitter::InvocationId() { | |||
| 362 | return Inst<U32>(Opcode::InvocationId); | 362 | return Inst<U32>(Opcode::InvocationId); |
| 363 | } | 363 | } |
| 364 | 364 | ||
| 365 | U32 IREmitter::InvocationInfo() { | ||
| 366 | return Inst<U32>(Opcode::InvocationInfo); | ||
| 367 | } | ||
| 368 | |||
| 365 | U32 IREmitter::SampleId() { | 369 | U32 IREmitter::SampleId() { |
| 366 | return Inst<U32>(Opcode::SampleId); | 370 | return Inst<U32>(Opcode::SampleId); |
| 367 | } | 371 | } |
| @@ -378,6 +382,14 @@ F32 IREmitter::ResolutionDownFactor() { | |||
| 378 | return Inst<F32>(Opcode::ResolutionDownFactor); | 382 | return Inst<F32>(Opcode::ResolutionDownFactor); |
| 379 | } | 383 | } |
| 380 | 384 | ||
| 385 | F32 IREmitter::RenderAreaWidth() { | ||
| 386 | return F32(CompositeExtract(Inst<Value>(Opcode::RenderArea), 0)); | ||
| 387 | } | ||
| 388 | |||
| 389 | F32 IREmitter::RenderAreaHeight() { | ||
| 390 | return F32(CompositeExtract(Inst<Value>(Opcode::RenderArea), 1)); | ||
| 391 | } | ||
| 392 | |||
| 381 | U32 IREmitter::LaneId() { | 393 | U32 IREmitter::LaneId() { |
| 382 | return Inst<U32>(Opcode::LaneId); | 394 | return Inst<U32>(Opcode::LaneId); |
| 383 | } | 395 | } |
| @@ -684,6 +696,11 @@ IR::U32 IREmitter::BitCast<IR::U32, IR::F32>(const IR::F32& value) { | |||
| 684 | } | 696 | } |
| 685 | 697 | ||
| 686 | template <> | 698 | template <> |
| 699 | IR::S32 IREmitter::BitCast<IR::S32, IR::F32>(const IR::F32& value) { | ||
| 700 | return Inst<IR::S32>(Opcode::BitCastS32F32, value); | ||
| 701 | } | ||
| 702 | |||
| 703 | template <> | ||
| 687 | IR::F32 IREmitter::BitCast<IR::F32, IR::U32>(const IR::U32& value) { | 704 | IR::F32 IREmitter::BitCast<IR::F32, IR::U32>(const IR::U32& value) { |
| 688 | return Inst<IR::F32>(Opcode::BitCastF32U32, value); | 705 | return Inst<IR::F32>(Opcode::BitCastF32U32, value); |
| 689 | } | 706 | } |
diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.h b/src/shader_recompiler/frontend/ir/ir_emitter.h index 25839a371..2df992feb 100644 --- a/src/shader_recompiler/frontend/ir/ir_emitter.h +++ b/src/shader_recompiler/frontend/ir/ir_emitter.h | |||
| @@ -97,12 +97,16 @@ public: | |||
| 97 | [[nodiscard]] U32 LocalInvocationIdZ(); | 97 | [[nodiscard]] U32 LocalInvocationIdZ(); |
| 98 | 98 | ||
| 99 | [[nodiscard]] U32 InvocationId(); | 99 | [[nodiscard]] U32 InvocationId(); |
| 100 | [[nodiscard]] U32 InvocationInfo(); | ||
| 100 | [[nodiscard]] U32 SampleId(); | 101 | [[nodiscard]] U32 SampleId(); |
| 101 | [[nodiscard]] U1 IsHelperInvocation(); | 102 | [[nodiscard]] U1 IsHelperInvocation(); |
| 102 | [[nodiscard]] F32 YDirection(); | 103 | [[nodiscard]] F32 YDirection(); |
| 103 | 104 | ||
| 104 | [[nodiscard]] F32 ResolutionDownFactor(); | 105 | [[nodiscard]] F32 ResolutionDownFactor(); |
| 105 | 106 | ||
| 107 | [[nodiscard]] F32 RenderAreaWidth(); | ||
| 108 | [[nodiscard]] F32 RenderAreaHeight(); | ||
| 109 | |||
| 106 | [[nodiscard]] U32 LaneId(); | 110 | [[nodiscard]] U32 LaneId(); |
| 107 | 111 | ||
| 108 | [[nodiscard]] U32 LoadGlobalU8(const U64& address); | 112 | [[nodiscard]] U32 LoadGlobalU8(const U64& address); |
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.cpp b/src/shader_recompiler/frontend/ir/microinstruction.cpp index 468782eb1..84417980b 100644 --- a/src/shader_recompiler/frontend/ir/microinstruction.cpp +++ b/src/shader_recompiler/frontend/ir/microinstruction.cpp | |||
| @@ -325,11 +325,6 @@ void Inst::AddPhiOperand(Block* predecessor, const Value& value) { | |||
| 325 | phi_args.emplace_back(predecessor, value); | 325 | phi_args.emplace_back(predecessor, value); |
| 326 | } | 326 | } |
| 327 | 327 | ||
| 328 | void Inst::ErasePhiOperand(size_t index) { | ||
| 329 | const auto operand_it{phi_args.begin() + static_cast<ptrdiff_t>(index)}; | ||
| 330 | phi_args.erase(operand_it); | ||
| 331 | } | ||
| 332 | |||
| 333 | void Inst::OrderPhiArgs() { | 328 | void Inst::OrderPhiArgs() { |
| 334 | if (op != Opcode::Phi) { | 329 | if (op != Opcode::Phi) { |
| 335 | throw LogicError("{} is not a Phi instruction", op); | 330 | throw LogicError("{} is not a Phi instruction", op); |
diff --git a/src/shader_recompiler/frontend/ir/opcodes.h b/src/shader_recompiler/frontend/ir/opcodes.h index 752879a18..e70d7745c 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.h +++ b/src/shader_recompiler/frontend/ir/opcodes.h | |||
| @@ -37,6 +37,7 @@ constexpr Type U8{Type::U8}; | |||
| 37 | constexpr Type U16{Type::U16}; | 37 | constexpr Type U16{Type::U16}; |
| 38 | constexpr Type U32{Type::U32}; | 38 | constexpr Type U32{Type::U32}; |
| 39 | constexpr Type U64{Type::U64}; | 39 | constexpr Type U64{Type::U64}; |
| 40 | constexpr Type S32{Type::S32}; | ||
| 40 | constexpr Type F16{Type::F16}; | 41 | constexpr Type F16{Type::F16}; |
| 41 | constexpr Type F32{Type::F32}; | 42 | constexpr Type F32{Type::F32}; |
| 42 | constexpr Type F64{Type::F64}; | 43 | constexpr Type F64{Type::F64}; |
diff --git a/src/shader_recompiler/frontend/ir/opcodes.inc b/src/shader_recompiler/frontend/ir/opcodes.inc index 86410ddfc..1fe3749cc 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.inc +++ b/src/shader_recompiler/frontend/ir/opcodes.inc | |||
| @@ -59,10 +59,12 @@ OPCODE(SetOFlag, Void, U1, | |||
| 59 | OPCODE(WorkgroupId, U32x3, ) | 59 | OPCODE(WorkgroupId, U32x3, ) |
| 60 | OPCODE(LocalInvocationId, U32x3, ) | 60 | OPCODE(LocalInvocationId, U32x3, ) |
| 61 | OPCODE(InvocationId, U32, ) | 61 | OPCODE(InvocationId, U32, ) |
| 62 | OPCODE(InvocationInfo, U32, ) | ||
| 62 | OPCODE(SampleId, U32, ) | 63 | OPCODE(SampleId, U32, ) |
| 63 | OPCODE(IsHelperInvocation, U1, ) | 64 | OPCODE(IsHelperInvocation, U1, ) |
| 64 | OPCODE(YDirection, F32, ) | 65 | OPCODE(YDirection, F32, ) |
| 65 | OPCODE(ResolutionDownFactor, F32, ) | 66 | OPCODE(ResolutionDownFactor, F32, ) |
| 67 | OPCODE(RenderArea, F32x4, ) | ||
| 66 | 68 | ||
| 67 | // Undefined | 69 | // Undefined |
| 68 | OPCODE(UndefU1, U1, ) | 70 | OPCODE(UndefU1, U1, ) |
| @@ -173,6 +175,7 @@ OPCODE(SelectF64, F64, U1, | |||
| 173 | OPCODE(BitCastU16F16, U16, F16, ) | 175 | OPCODE(BitCastU16F16, U16, F16, ) |
| 174 | OPCODE(BitCastU32F32, U32, F32, ) | 176 | OPCODE(BitCastU32F32, U32, F32, ) |
| 175 | OPCODE(BitCastU64F64, U64, F64, ) | 177 | OPCODE(BitCastU64F64, U64, F64, ) |
| 178 | OPCODE(BitCastS32F32, S32, F32, ) | ||
| 176 | OPCODE(BitCastF16U16, F16, U16, ) | 179 | OPCODE(BitCastF16U16, F16, U16, ) |
| 177 | OPCODE(BitCastF32U32, F32, U32, ) | 180 | OPCODE(BitCastF32U32, F32, U32, ) |
| 178 | OPCODE(BitCastF64U64, F64, U64, ) | 181 | OPCODE(BitCastF64U64, F64, U64, ) |
diff --git a/src/shader_recompiler/frontend/ir/patch.h b/src/shader_recompiler/frontend/ir/patch.h index 1e37c8eb6..5077e56c2 100644 --- a/src/shader_recompiler/frontend/ir/patch.h +++ b/src/shader_recompiler/frontend/ir/patch.h | |||
| @@ -14,8 +14,6 @@ enum class Patch : u64 { | |||
| 14 | TessellationLodBottom, | 14 | TessellationLodBottom, |
| 15 | TessellationLodInteriorU, | 15 | TessellationLodInteriorU, |
| 16 | TessellationLodInteriorV, | 16 | TessellationLodInteriorV, |
| 17 | ComponentPadding0, | ||
| 18 | ComponentPadding1, | ||
| 19 | Component0, | 17 | Component0, |
| 20 | Component1, | 18 | Component1, |
| 21 | Component2, | 19 | Component2, |
| @@ -137,7 +135,7 @@ enum class Patch : u64 { | |||
| 137 | Component118, | 135 | Component118, |
| 138 | Component119, | 136 | Component119, |
| 139 | }; | 137 | }; |
| 140 | static_assert(static_cast<u64>(Patch::Component119) == 127); | 138 | static_assert(static_cast<u64>(Patch::Component119) == 125); |
| 141 | 139 | ||
| 142 | [[nodiscard]] bool IsGeneric(Patch patch) noexcept; | 140 | [[nodiscard]] bool IsGeneric(Patch patch) noexcept; |
| 143 | 141 | ||
diff --git a/src/shader_recompiler/frontend/ir/type.h b/src/shader_recompiler/frontend/ir/type.h index 04c8c4ddb..5a7c706ad 100644 --- a/src/shader_recompiler/frontend/ir/type.h +++ b/src/shader_recompiler/frontend/ir/type.h | |||
| @@ -24,21 +24,22 @@ enum class Type { | |||
| 24 | U16 = 1 << 7, | 24 | U16 = 1 << 7, |
| 25 | U32 = 1 << 8, | 25 | U32 = 1 << 8, |
| 26 | U64 = 1 << 9, | 26 | U64 = 1 << 9, |
| 27 | F16 = 1 << 10, | 27 | S32 = 1 << 10, |
| 28 | F32 = 1 << 11, | 28 | F16 = 1 << 11, |
| 29 | F64 = 1 << 12, | 29 | F32 = 1 << 12, |
| 30 | U32x2 = 1 << 13, | 30 | F64 = 1 << 13, |
| 31 | U32x3 = 1 << 14, | 31 | U32x2 = 1 << 14, |
| 32 | U32x4 = 1 << 15, | 32 | U32x3 = 1 << 15, |
| 33 | F16x2 = 1 << 16, | 33 | U32x4 = 1 << 16, |
| 34 | F16x3 = 1 << 17, | 34 | F16x2 = 1 << 17, |
| 35 | F16x4 = 1 << 18, | 35 | F16x3 = 1 << 18, |
| 36 | F32x2 = 1 << 19, | 36 | F16x4 = 1 << 19, |
| 37 | F32x3 = 1 << 20, | 37 | F32x2 = 1 << 20, |
| 38 | F32x4 = 1 << 21, | 38 | F32x3 = 1 << 21, |
| 39 | F64x2 = 1 << 22, | 39 | F32x4 = 1 << 22, |
| 40 | F64x3 = 1 << 23, | 40 | F64x2 = 1 << 23, |
| 41 | F64x4 = 1 << 24, | 41 | F64x3 = 1 << 24, |
| 42 | F64x4 = 1 << 25, | ||
| 42 | }; | 43 | }; |
| 43 | DECLARE_ENUM_FLAG_OPERATORS(Type) | 44 | DECLARE_ENUM_FLAG_OPERATORS(Type) |
| 44 | 45 | ||
diff --git a/src/shader_recompiler/frontend/ir/value.cpp b/src/shader_recompiler/frontend/ir/value.cpp index 346169328..30ba12316 100644 --- a/src/shader_recompiler/frontend/ir/value.cpp +++ b/src/shader_recompiler/frontend/ir/value.cpp | |||
| @@ -23,6 +23,8 @@ Value::Value(u16 value) noexcept : type{Type::U16}, imm_u16{value} {} | |||
| 23 | 23 | ||
| 24 | Value::Value(u32 value) noexcept : type{Type::U32}, imm_u32{value} {} | 24 | Value::Value(u32 value) noexcept : type{Type::U32}, imm_u32{value} {} |
| 25 | 25 | ||
| 26 | Value::Value(s32 value) noexcept : type{Type::S32}, imm_s32{value} {} | ||
| 27 | |||
| 26 | Value::Value(f32 value) noexcept : type{Type::F32}, imm_f32{value} {} | 28 | Value::Value(f32 value) noexcept : type{Type::F32}, imm_f32{value} {} |
| 27 | 29 | ||
| 28 | Value::Value(u64 value) noexcept : type{Type::U64}, imm_u64{value} {} | 30 | Value::Value(u64 value) noexcept : type{Type::U64}, imm_u64{value} {} |
| @@ -69,6 +71,7 @@ bool Value::operator==(const Value& other) const { | |||
| 69 | return imm_u16 == other.imm_u16; | 71 | return imm_u16 == other.imm_u16; |
| 70 | case Type::U32: | 72 | case Type::U32: |
| 71 | case Type::F32: | 73 | case Type::F32: |
| 74 | case Type::S32: | ||
| 72 | return imm_u32 == other.imm_u32; | 75 | return imm_u32 == other.imm_u32; |
| 73 | case Type::U64: | 76 | case Type::U64: |
| 74 | case Type::F64: | 77 | case Type::F64: |
diff --git a/src/shader_recompiler/frontend/ir/value.h b/src/shader_recompiler/frontend/ir/value.h index 1a2e4ccb6..e8bbb93a5 100644 --- a/src/shader_recompiler/frontend/ir/value.h +++ b/src/shader_recompiler/frontend/ir/value.h | |||
| @@ -44,6 +44,7 @@ public: | |||
| 44 | explicit Value(u8 value) noexcept; | 44 | explicit Value(u8 value) noexcept; |
| 45 | explicit Value(u16 value) noexcept; | 45 | explicit Value(u16 value) noexcept; |
| 46 | explicit Value(u32 value) noexcept; | 46 | explicit Value(u32 value) noexcept; |
| 47 | explicit Value(s32 value) noexcept; | ||
| 47 | explicit Value(f32 value) noexcept; | 48 | explicit Value(f32 value) noexcept; |
| 48 | explicit Value(u64 value) noexcept; | 49 | explicit Value(u64 value) noexcept; |
| 49 | explicit Value(f64 value) noexcept; | 50 | explicit Value(f64 value) noexcept; |
| @@ -66,6 +67,7 @@ public: | |||
| 66 | [[nodiscard]] u8 U8() const; | 67 | [[nodiscard]] u8 U8() const; |
| 67 | [[nodiscard]] u16 U16() const; | 68 | [[nodiscard]] u16 U16() const; |
| 68 | [[nodiscard]] u32 U32() const; | 69 | [[nodiscard]] u32 U32() const; |
| 70 | [[nodiscard]] s32 S32() const; | ||
| 69 | [[nodiscard]] f32 F32() const; | 71 | [[nodiscard]] f32 F32() const; |
| 70 | [[nodiscard]] u64 U64() const; | 72 | [[nodiscard]] u64 U64() const; |
| 71 | [[nodiscard]] f64 F64() const; | 73 | [[nodiscard]] f64 F64() const; |
| @@ -85,6 +87,7 @@ private: | |||
| 85 | u8 imm_u8; | 87 | u8 imm_u8; |
| 86 | u16 imm_u16; | 88 | u16 imm_u16; |
| 87 | u32 imm_u32; | 89 | u32 imm_u32; |
| 90 | s32 imm_s32; | ||
| 88 | f32 imm_f32; | 91 | f32 imm_f32; |
| 89 | u64 imm_u64; | 92 | u64 imm_u64; |
| 90 | f64 imm_f64; | 93 | f64 imm_f64; |
| @@ -178,13 +181,9 @@ public: | |||
| 178 | 181 | ||
| 179 | /// Get a pointer to the block of a phi argument. | 182 | /// Get a pointer to the block of a phi argument. |
| 180 | [[nodiscard]] Block* PhiBlock(size_t index) const; | 183 | [[nodiscard]] Block* PhiBlock(size_t index) const; |
| 181 | |||
| 182 | /// Add phi operand to a phi instruction. | 184 | /// Add phi operand to a phi instruction. |
| 183 | void AddPhiOperand(Block* predecessor, const Value& value); | 185 | void AddPhiOperand(Block* predecessor, const Value& value); |
| 184 | 186 | ||
| 185 | // Erase the phi operand at the given index. | ||
| 186 | void ErasePhiOperand(size_t index); | ||
| 187 | |||
| 188 | /// Orders the Phi arguments from farthest away to nearest. | 187 | /// Orders the Phi arguments from farthest away to nearest. |
| 189 | void OrderPhiArgs(); | 188 | void OrderPhiArgs(); |
| 190 | 189 | ||
| @@ -270,6 +269,7 @@ using U8 = TypedValue<Type::U8>; | |||
| 270 | using U16 = TypedValue<Type::U16>; | 269 | using U16 = TypedValue<Type::U16>; |
| 271 | using U32 = TypedValue<Type::U32>; | 270 | using U32 = TypedValue<Type::U32>; |
| 272 | using U64 = TypedValue<Type::U64>; | 271 | using U64 = TypedValue<Type::U64>; |
| 272 | using S32 = TypedValue<Type::S32>; | ||
| 273 | using F16 = TypedValue<Type::F16>; | 273 | using F16 = TypedValue<Type::F16>; |
| 274 | using F32 = TypedValue<Type::F32>; | 274 | using F32 = TypedValue<Type::F32>; |
| 275 | using F64 = TypedValue<Type::F64>; | 275 | using F64 = TypedValue<Type::F64>; |
| @@ -381,6 +381,14 @@ inline u32 Value::U32() const { | |||
| 381 | return imm_u32; | 381 | return imm_u32; |
| 382 | } | 382 | } |
| 383 | 383 | ||
| 384 | inline s32 Value::S32() const { | ||
| 385 | if (IsIdentity()) { | ||
| 386 | return inst->Arg(0).S32(); | ||
| 387 | } | ||
| 388 | DEBUG_ASSERT(type == Type::S32); | ||
| 389 | return imm_s32; | ||
| 390 | } | ||
| 391 | |||
| 384 | inline f32 Value::F32() const { | 392 | inline f32 Value::F32() const { |
| 385 | if (IsIdentity()) { | 393 | if (IsIdentity()) { |
| 386 | return inst->Arg(0).F32(); | 394 | return inst->Arg(0).F32(); |
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/move_special_register.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/move_special_register.cpp index 52be12f9c..753c62098 100644 --- a/src/shader_recompiler/frontend/maxwell/translate/impl/move_special_register.cpp +++ b/src/shader_recompiler/frontend/maxwell/translate/impl/move_special_register.cpp | |||
| @@ -117,8 +117,7 @@ enum class SpecialRegister : u64 { | |||
| 117 | case SpecialRegister::SR_THREAD_KILL: | 117 | case SpecialRegister::SR_THREAD_KILL: |
| 118 | return IR::U32{ir.Select(ir.IsHelperInvocation(), ir.Imm32(-1), ir.Imm32(0))}; | 118 | return IR::U32{ir.Select(ir.IsHelperInvocation(), ir.Imm32(-1), ir.Imm32(0))}; |
| 119 | case SpecialRegister::SR_INVOCATION_INFO: | 119 | case SpecialRegister::SR_INVOCATION_INFO: |
| 120 | LOG_WARNING(Shader, "(STUBBED) SR_INVOCATION_INFO"); | 120 | return ir.InvocationInfo(); |
| 121 | return ir.Imm32(0x00ff'0000); | ||
| 122 | case SpecialRegister::SR_TID: { | 121 | case SpecialRegister::SR_TID: { |
| 123 | const IR::Value tid{ir.LocalInvocationId()}; | 122 | const IR::Value tid{ir.LocalInvocationId()}; |
| 124 | return ir.BitFieldInsert(ir.BitFieldInsert(IR::U32{ir.CompositeExtract(tid, 0)}, | 123 | return ir.BitFieldInsert(ir.BitFieldInsert(IR::U32{ir.CompositeExtract(tid, 0)}, |
diff --git a/src/shader_recompiler/frontend/maxwell/translate_program.cpp b/src/shader_recompiler/frontend/maxwell/translate_program.cpp index b58741d4d..376aae0ea 100644 --- a/src/shader_recompiler/frontend/maxwell/translate_program.cpp +++ b/src/shader_recompiler/frontend/maxwell/translate_program.cpp | |||
| @@ -220,8 +220,10 @@ IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Blo | |||
| 220 | 220 | ||
| 221 | Optimization::ConstantPropagationPass(program); | 221 | Optimization::ConstantPropagationPass(program); |
| 222 | 222 | ||
| 223 | Optimization::PositionPass(env, program); | ||
| 224 | |||
| 223 | Optimization::GlobalMemoryToStorageBufferPass(program); | 225 | Optimization::GlobalMemoryToStorageBufferPass(program); |
| 224 | Optimization::TexturePass(env, program); | 226 | Optimization::TexturePass(env, program, host_info); |
| 225 | 227 | ||
| 226 | if (Settings::values.resolution_info.active) { | 228 | if (Settings::values.resolution_info.active) { |
| 227 | Optimization::RescalingPass(program); | 229 | Optimization::RescalingPass(program); |
diff --git a/src/shader_recompiler/host_translate_info.h b/src/shader_recompiler/host_translate_info.h index 881874310..cc1500690 100644 --- a/src/shader_recompiler/host_translate_info.h +++ b/src/shader_recompiler/host_translate_info.h | |||
| @@ -13,6 +13,7 @@ struct HostTranslateInfo { | |||
| 13 | bool support_float16{}; ///< True when the device supports 16-bit floats | 13 | bool support_float16{}; ///< True when the device supports 16-bit floats |
| 14 | bool support_int64{}; ///< True when the device supports 64-bit integers | 14 | bool support_int64{}; ///< True when the device supports 64-bit integers |
| 15 | bool needs_demote_reorder{}; ///< True when the device needs DemoteToHelperInvocation reordered | 15 | bool needs_demote_reorder{}; ///< True when the device needs DemoteToHelperInvocation reordered |
| 16 | bool support_snorm_render_buffer{}; ///< True when the device supports SNORM render buffers | ||
| 16 | }; | 17 | }; |
| 17 | 18 | ||
| 18 | } // namespace Shader | 19 | } // namespace Shader |
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 7cff8ecdc..5a4195217 100644 --- a/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp +++ b/src/shader_recompiler/ir_opt/collect_shader_info_pass.cpp | |||
| @@ -468,6 +468,9 @@ void VisitUsages(Info& info, IR::Inst& inst) { | |||
| 468 | case IR::Opcode::InvocationId: | 468 | case IR::Opcode::InvocationId: |
| 469 | info.uses_invocation_id = true; | 469 | info.uses_invocation_id = true; |
| 470 | break; | 470 | break; |
| 471 | case IR::Opcode::InvocationInfo: | ||
| 472 | info.uses_invocation_info = true; | ||
| 473 | break; | ||
| 471 | case IR::Opcode::SampleId: | 474 | case IR::Opcode::SampleId: |
| 472 | info.uses_sample_id = true; | 475 | info.uses_sample_id = true; |
| 473 | break; | 476 | break; |
diff --git a/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp b/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp index 9a7d47344..1bd8afd6f 100644 --- a/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp +++ b/src/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp | |||
| @@ -1,104 +1,24 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include <algorithm> | ||
| 5 | |||
| 6 | #include <boost/container/small_vector.hpp> | ||
| 7 | |||
| 8 | #include "shader_recompiler/frontend/ir/basic_block.h" | 4 | #include "shader_recompiler/frontend/ir/basic_block.h" |
| 9 | #include "shader_recompiler/frontend/ir/value.h" | 5 | #include "shader_recompiler/frontend/ir/value.h" |
| 10 | #include "shader_recompiler/ir_opt/passes.h" | 6 | #include "shader_recompiler/ir_opt/passes.h" |
| 11 | 7 | ||
| 12 | namespace Shader::Optimization { | 8 | namespace Shader::Optimization { |
| 13 | namespace { | 9 | |
| 14 | template <bool TEST_USES> | 10 | void DeadCodeEliminationPass(IR::Program& program) { |
| 15 | void DeadInstElimination(IR::Block* const block) { | ||
| 16 | // We iterate over the instructions in reverse order. | 11 | // We iterate over the instructions in reverse order. |
| 17 | // This is because removing an instruction reduces the number of uses for earlier instructions. | 12 | // This is because removing an instruction reduces the number of uses for earlier instructions. |
| 18 | auto it{block->end()}; | 13 | for (IR::Block* const block : program.post_order_blocks) { |
| 19 | while (it != block->begin()) { | 14 | auto it{block->end()}; |
| 20 | --it; | 15 | while (it != block->begin()) { |
| 21 | if constexpr (TEST_USES) { | 16 | --it; |
| 22 | if (it->HasUses() || it->MayHaveSideEffects()) { | 17 | if (!it->HasUses() && !it->MayHaveSideEffects()) { |
| 23 | continue; | 18 | it->Invalidate(); |
| 24 | } | 19 | it = block->Instructions().erase(it); |
| 25 | } | ||
| 26 | it->Invalidate(); | ||
| 27 | it = block->Instructions().erase(it); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | void DeletedPhiArgElimination(IR::Program& program, std::span<const IR::Block*> dead_blocks) { | ||
| 32 | for (IR::Block* const block : program.blocks) { | ||
| 33 | for (IR::Inst& phi : *block) { | ||
| 34 | if (!IR::IsPhi(phi)) { | ||
| 35 | continue; | ||
| 36 | } | ||
| 37 | for (size_t i = 0; i < phi.NumArgs(); ++i) { | ||
| 38 | if (std::ranges::find(dead_blocks, phi.PhiBlock(i)) == dead_blocks.end()) { | ||
| 39 | continue; | ||
| 40 | } | ||
| 41 | // Phi operand at this index is an unreachable block | ||
| 42 | phi.ErasePhiOperand(i); | ||
| 43 | --i; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | void DeadBranchElimination(IR::Program& program) { | ||
| 50 | boost::container::small_vector<const IR::Block*, 3> dead_blocks; | ||
| 51 | const auto begin_it{program.syntax_list.begin()}; | ||
| 52 | for (auto node_it = begin_it; node_it != program.syntax_list.end(); ++node_it) { | ||
| 53 | if (node_it->type != IR::AbstractSyntaxNode::Type::If) { | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | IR::Inst* const cond_ref{node_it->data.if_node.cond.Inst()}; | ||
| 57 | const IR::U1 cond{cond_ref->Arg(0)}; | ||
| 58 | if (!cond.IsImmediate()) { | ||
| 59 | continue; | ||
| 60 | } | ||
| 61 | if (cond.U1()) { | ||
| 62 | continue; | ||
| 63 | } | ||
| 64 | // False immediate condition. Remove condition ref, erase the entire branch. | ||
| 65 | cond_ref->Invalidate(); | ||
| 66 | // Account for nested if-statements within the if(false) branch | ||
| 67 | u32 nested_ifs{1u}; | ||
| 68 | while (node_it->type != IR::AbstractSyntaxNode::Type::EndIf || nested_ifs > 0) { | ||
| 69 | node_it = program.syntax_list.erase(node_it); | ||
| 70 | switch (node_it->type) { | ||
| 71 | case IR::AbstractSyntaxNode::Type::If: | ||
| 72 | ++nested_ifs; | ||
| 73 | break; | ||
| 74 | case IR::AbstractSyntaxNode::Type::EndIf: | ||
| 75 | --nested_ifs; | ||
| 76 | break; | ||
| 77 | case IR::AbstractSyntaxNode::Type::Block: { | ||
| 78 | IR::Block* const block{node_it->data.block}; | ||
| 79 | DeadInstElimination<false>(block); | ||
| 80 | dead_blocks.push_back(block); | ||
| 81 | break; | ||
| 82 | } | ||
| 83 | default: | ||
| 84 | break; | ||
| 85 | } | 20 | } |
| 86 | } | 21 | } |
| 87 | // Erase EndIf node of the if(false) branch | ||
| 88 | node_it = program.syntax_list.erase(node_it); | ||
| 89 | // Account for loop increment | ||
| 90 | --node_it; | ||
| 91 | } | ||
| 92 | if (!dead_blocks.empty()) { | ||
| 93 | DeletedPhiArgElimination(program, std::span(dead_blocks.data(), dead_blocks.size())); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } // namespace | ||
| 97 | |||
| 98 | void DeadCodeEliminationPass(IR::Program& program) { | ||
| 99 | DeadBranchElimination(program); | ||
| 100 | for (IR::Block* const block : program.post_order_blocks) { | ||
| 101 | DeadInstElimination<true>(block); | ||
| 102 | } | 22 | } |
| 103 | } | 23 | } |
| 104 | 24 | ||
diff --git a/src/shader_recompiler/ir_opt/passes.h b/src/shader_recompiler/ir_opt/passes.h index 6ff8e4266..586a0668f 100644 --- a/src/shader_recompiler/ir_opt/passes.h +++ b/src/shader_recompiler/ir_opt/passes.h | |||
| @@ -6,6 +6,10 @@ | |||
| 6 | #include "shader_recompiler/environment.h" | 6 | #include "shader_recompiler/environment.h" |
| 7 | #include "shader_recompiler/frontend/ir/program.h" | 7 | #include "shader_recompiler/frontend/ir/program.h" |
| 8 | 8 | ||
| 9 | namespace Shader { | ||
| 10 | struct HostTranslateInfo; | ||
| 11 | } | ||
| 12 | |||
| 9 | namespace Shader::Optimization { | 13 | namespace Shader::Optimization { |
| 10 | 14 | ||
| 11 | void CollectShaderInfoPass(Environment& env, IR::Program& program); | 15 | void CollectShaderInfoPass(Environment& env, IR::Program& program); |
| @@ -17,7 +21,8 @@ void LowerFp16ToFp32(IR::Program& program); | |||
| 17 | void LowerInt64ToInt32(IR::Program& program); | 21 | void LowerInt64ToInt32(IR::Program& program); |
| 18 | void RescalingPass(IR::Program& program); | 22 | void RescalingPass(IR::Program& program); |
| 19 | void SsaRewritePass(IR::Program& program); | 23 | void SsaRewritePass(IR::Program& program); |
| 20 | void TexturePass(Environment& env, IR::Program& program); | 24 | void PositionPass(Environment& env, IR::Program& program); |
| 25 | void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info); | ||
| 21 | void VerificationPass(const IR::Program& program); | 26 | void VerificationPass(const IR::Program& program); |
| 22 | 27 | ||
| 23 | // Dual Vertex | 28 | // Dual Vertex |
diff --git a/src/shader_recompiler/ir_opt/position_pass.cpp b/src/shader_recompiler/ir_opt/position_pass.cpp new file mode 100644 index 000000000..3c20b7189 --- /dev/null +++ b/src/shader_recompiler/ir_opt/position_pass.cpp | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <boost/container/small_vector.hpp> | ||
| 5 | |||
| 6 | #include "shader_recompiler/frontend/ir/basic_block.h" | ||
| 7 | #include "shader_recompiler/frontend/ir/ir_emitter.h" | ||
| 8 | #include "shader_recompiler/frontend/ir/value.h" | ||
| 9 | #include "shader_recompiler/ir_opt/passes.h" | ||
| 10 | |||
| 11 | namespace Shader::Optimization { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | struct PositionInst { | ||
| 15 | IR::Inst* inst; | ||
| 16 | IR::Block* block; | ||
| 17 | IR::Attribute attr; | ||
| 18 | }; | ||
| 19 | using PositionInstVector = boost::container::small_vector<PositionInst, 24>; | ||
| 20 | } // Anonymous namespace | ||
| 21 | |||
| 22 | void PositionPass(Environment& env, IR::Program& program) { | ||
| 23 | if (env.ShaderStage() != Stage::VertexB || env.ReadViewportTransformState()) { | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | Info& info{program.info}; | ||
| 28 | info.uses_render_area = true; | ||
| 29 | |||
| 30 | PositionInstVector to_replace; | ||
| 31 | for (IR::Block* const block : program.post_order_blocks) { | ||
| 32 | for (IR::Inst& inst : block->Instructions()) { | ||
| 33 | switch (inst.GetOpcode()) { | ||
| 34 | case IR::Opcode::SetAttribute: { | ||
| 35 | const IR::Attribute attr{inst.Arg(0).Attribute()}; | ||
| 36 | switch (attr) { | ||
| 37 | case IR::Attribute::PositionX: | ||
| 38 | case IR::Attribute::PositionY: { | ||
| 39 | to_replace.push_back(PositionInst{.inst = &inst, .block = block, .attr = attr}); | ||
| 40 | break; | ||
| 41 | } | ||
| 42 | default: | ||
| 43 | break; | ||
| 44 | } | ||
| 45 | break; | ||
| 46 | } | ||
| 47 | default: | ||
| 48 | break; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | for (PositionInst& position_inst : to_replace) { | ||
| 54 | IR::IREmitter ir{*position_inst.block, | ||
| 55 | IR::Block::InstructionList::s_iterator_to(*position_inst.inst)}; | ||
| 56 | const IR::F32 value(position_inst.inst->Arg(1)); | ||
| 57 | const IR::F32F64 scale(ir.Imm32(2.f)); | ||
| 58 | const IR::F32 negative_one{ir.Imm32(-1.f)}; | ||
| 59 | switch (position_inst.attr) { | ||
| 60 | case IR::Attribute::PositionX: { | ||
| 61 | position_inst.inst->SetArg( | ||
| 62 | 1, | ||
| 63 | ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaWidth()), scale), negative_one)); | ||
| 64 | break; | ||
| 65 | } | ||
| 66 | case IR::Attribute::PositionY: { | ||
| 67 | position_inst.inst->SetArg( | ||
| 68 | 1, | ||
| 69 | ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaHeight()), scale), negative_one)); | ||
| 70 | break; | ||
| 71 | } | ||
| 72 | default: | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } // namespace Shader::Optimization | ||
diff --git a/src/shader_recompiler/ir_opt/texture_pass.cpp b/src/shader_recompiler/ir_opt/texture_pass.cpp index e8be58357..f5c86fcb1 100644 --- a/src/shader_recompiler/ir_opt/texture_pass.cpp +++ b/src/shader_recompiler/ir_opt/texture_pass.cpp | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | #include "shader_recompiler/frontend/ir/basic_block.h" | 11 | #include "shader_recompiler/frontend/ir/basic_block.h" |
| 12 | #include "shader_recompiler/frontend/ir/breadth_first_search.h" | 12 | #include "shader_recompiler/frontend/ir/breadth_first_search.h" |
| 13 | #include "shader_recompiler/frontend/ir/ir_emitter.h" | 13 | #include "shader_recompiler/frontend/ir/ir_emitter.h" |
| 14 | #include "shader_recompiler/host_translate_info.h" | ||
| 14 | #include "shader_recompiler/ir_opt/passes.h" | 15 | #include "shader_recompiler/ir_opt/passes.h" |
| 15 | #include "shader_recompiler/shader_info.h" | 16 | #include "shader_recompiler/shader_info.h" |
| 16 | 17 | ||
| @@ -363,6 +364,14 @@ TextureType ReadTextureType(Environment& env, const ConstBufferAddr& cbuf) { | |||
| 363 | return env.ReadTextureType(lhs_raw | rhs_raw); | 364 | return env.ReadTextureType(lhs_raw | rhs_raw); |
| 364 | } | 365 | } |
| 365 | 366 | ||
| 367 | TexturePixelFormat ReadTexturePixelFormat(Environment& env, const ConstBufferAddr& cbuf) { | ||
| 368 | const u32 secondary_index{cbuf.has_secondary ? cbuf.secondary_index : cbuf.index}; | ||
| 369 | const u32 secondary_offset{cbuf.has_secondary ? cbuf.secondary_offset : cbuf.offset}; | ||
| 370 | const u32 lhs_raw{env.ReadCbufValue(cbuf.index, cbuf.offset)}; | ||
| 371 | const u32 rhs_raw{env.ReadCbufValue(secondary_index, secondary_offset)}; | ||
| 372 | return env.ReadTexturePixelFormat(lhs_raw | rhs_raw); | ||
| 373 | } | ||
| 374 | |||
| 366 | class Descriptors { | 375 | class Descriptors { |
| 367 | public: | 376 | public: |
| 368 | explicit Descriptors(TextureBufferDescriptors& texture_buffer_descriptors_, | 377 | explicit Descriptors(TextureBufferDescriptors& texture_buffer_descriptors_, |
| @@ -451,9 +460,41 @@ void PatchImageSampleImplicitLod(IR::Block& block, IR::Inst& inst) { | |||
| 451 | ir.FPMul(IR::F32(ir.CompositeExtract(coord, 1)), | 460 | ir.FPMul(IR::F32(ir.CompositeExtract(coord, 1)), |
| 452 | ir.FPRecip(ir.ConvertUToF(32, 32, ir.CompositeExtract(texture_size, 1)))))); | 461 | ir.FPRecip(ir.ConvertUToF(32, 32, ir.CompositeExtract(texture_size, 1)))))); |
| 453 | } | 462 | } |
| 463 | |||
| 464 | void PatchTexelFetch(IR::Block& block, IR::Inst& inst, TexturePixelFormat pixel_format) { | ||
| 465 | const auto it{IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 466 | IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; | ||
| 467 | auto get_max_value = [pixel_format]() -> float { | ||
| 468 | switch (pixel_format) { | ||
| 469 | case TexturePixelFormat::A8B8G8R8_SNORM: | ||
| 470 | case TexturePixelFormat::R8G8_SNORM: | ||
| 471 | case TexturePixelFormat::R8_SNORM: | ||
| 472 | return 1.f / std::numeric_limits<char>::max(); | ||
| 473 | case TexturePixelFormat::R16G16B16A16_SNORM: | ||
| 474 | case TexturePixelFormat::R16G16_SNORM: | ||
| 475 | case TexturePixelFormat::R16_SNORM: | ||
| 476 | return 1.f / std::numeric_limits<short>::max(); | ||
| 477 | default: | ||
| 478 | throw InvalidArgument("Invalid texture pixel format"); | ||
| 479 | } | ||
| 480 | }; | ||
| 481 | |||
| 482 | const IR::Value new_inst{&*block.PrependNewInst(it, inst)}; | ||
| 483 | const IR::F32 x(ir.CompositeExtract(new_inst, 0)); | ||
| 484 | const IR::F32 y(ir.CompositeExtract(new_inst, 1)); | ||
| 485 | const IR::F32 z(ir.CompositeExtract(new_inst, 2)); | ||
| 486 | const IR::F32 w(ir.CompositeExtract(new_inst, 3)); | ||
| 487 | const IR::F16F32F64 max_value(ir.Imm32(get_max_value())); | ||
| 488 | const IR::Value converted = | ||
| 489 | ir.CompositeConstruct(ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::S32>(x)), max_value), | ||
| 490 | ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::S32>(y)), max_value), | ||
| 491 | ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::S32>(z)), max_value), | ||
| 492 | ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::S32>(w)), max_value)); | ||
| 493 | inst.ReplaceUsesWith(converted); | ||
| 494 | } | ||
| 454 | } // Anonymous namespace | 495 | } // Anonymous namespace |
| 455 | 496 | ||
| 456 | void TexturePass(Environment& env, IR::Program& program) { | 497 | void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info) { |
| 457 | TextureInstVector to_replace; | 498 | TextureInstVector to_replace; |
| 458 | for (IR::Block* const block : program.post_order_blocks) { | 499 | for (IR::Block* const block : program.post_order_blocks) { |
| 459 | for (IR::Inst& inst : block->Instructions()) { | 500 | for (IR::Inst& inst : block->Instructions()) { |
| @@ -597,6 +638,14 @@ void TexturePass(Environment& env, IR::Program& program) { | |||
| 597 | } else { | 638 | } else { |
| 598 | inst->SetArg(0, IR::Value{}); | 639 | inst->SetArg(0, IR::Value{}); |
| 599 | } | 640 | } |
| 641 | |||
| 642 | if (!host_info.support_snorm_render_buffer && inst->GetOpcode() == IR::Opcode::ImageFetch && | ||
| 643 | flags.type == TextureType::Buffer) { | ||
| 644 | const auto pixel_format = ReadTexturePixelFormat(env, cbuf); | ||
| 645 | if (pixel_format != TexturePixelFormat::OTHER) { | ||
| 646 | PatchTexelFetch(*texture_inst.block, *texture_inst.inst, pixel_format); | ||
| 647 | } | ||
| 648 | } | ||
| 600 | } | 649 | } |
| 601 | } | 650 | } |
| 602 | 651 | ||
diff --git a/src/shader_recompiler/shader_info.h b/src/shader_recompiler/shader_info.h index 81097bf1a..ee6252bb5 100644 --- a/src/shader_recompiler/shader_info.h +++ b/src/shader_recompiler/shader_info.h | |||
| @@ -29,6 +29,16 @@ enum class TextureType : u32 { | |||
| 29 | }; | 29 | }; |
| 30 | constexpr u32 NUM_TEXTURE_TYPES = 9; | 30 | constexpr u32 NUM_TEXTURE_TYPES = 9; |
| 31 | 31 | ||
| 32 | enum class TexturePixelFormat : u32 { | ||
| 33 | A8B8G8R8_SNORM, | ||
| 34 | R8_SNORM, | ||
| 35 | R8G8_SNORM, | ||
| 36 | R16G16B16A16_SNORM, | ||
| 37 | R16G16_SNORM, | ||
| 38 | R16_SNORM, | ||
| 39 | OTHER | ||
| 40 | }; | ||
| 41 | |||
| 32 | enum class ImageFormat : u32 { | 42 | enum class ImageFormat : u32 { |
| 33 | Typeless, | 43 | Typeless, |
| 34 | R8_UINT, | 44 | R8_UINT, |
| @@ -117,6 +127,7 @@ struct Info { | |||
| 117 | bool uses_workgroup_id{}; | 127 | bool uses_workgroup_id{}; |
| 118 | bool uses_local_invocation_id{}; | 128 | bool uses_local_invocation_id{}; |
| 119 | bool uses_invocation_id{}; | 129 | bool uses_invocation_id{}; |
| 130 | bool uses_invocation_info{}; | ||
| 120 | bool uses_sample_id{}; | 131 | bool uses_sample_id{}; |
| 121 | bool uses_is_helper_invocation{}; | 132 | bool uses_is_helper_invocation{}; |
| 122 | bool uses_subgroup_invocation_id{}; | 133 | bool uses_subgroup_invocation_id{}; |
| @@ -182,6 +193,7 @@ struct Info { | |||
| 182 | bool uses_shadow_lod{}; | 193 | bool uses_shadow_lod{}; |
| 183 | bool uses_rescaling_uniform{}; | 194 | bool uses_rescaling_uniform{}; |
| 184 | bool uses_cbuf_indirect{}; | 195 | bool uses_cbuf_indirect{}; |
| 196 | bool uses_render_area{}; | ||
| 185 | 197 | ||
| 186 | IR::Type used_constant_buffer_types{}; | 198 | IR::Type used_constant_buffer_types{}; |
| 187 | IR::Type used_storage_buffer_types{}; | 199 | IR::Type used_storage_buffer_types{}; |