From 465ba30d08fd294a6ccc05f9257894241a5240fc Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Fri, 13 Mar 2020 12:48:01 +0700 Subject: shader_decode: Reimplement BFE instructions --- src/video_core/shader/decode/bfe.cpp | 52 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index e02bcd097..c3ef88373 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -17,32 +17,34 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { const Instruction instr = {program_code[pc]}; const auto opcode = OpCode::Decode(instr); - UNIMPLEMENTED_IF(instr.bfe.negate_b); - Node op_a = GetRegister(instr.gpr8); - op_a = GetOperandAbsNegInteger(op_a, false, instr.bfe.negate_a, false); - - switch (opcode->get().GetId()) { - case OpCode::Id::BFE_IMM: { - UNIMPLEMENTED_IF_MSG(instr.generates_cc, - "Condition codes generation in BFE is not implemented"); - - const Node inner_shift_imm = Immediate(static_cast(instr.bfe.GetLeftShiftValue())); - const Node outer_shift_imm = - Immediate(static_cast(instr.bfe.GetLeftShiftValue() + instr.bfe.shift_position)); - - const Node inner_shift = - Operation(OperationCode::ILogicalShiftLeft, NO_PRECISE, op_a, inner_shift_imm); - const Node outer_shift = - Operation(OperationCode::ILogicalShiftRight, NO_PRECISE, inner_shift, outer_shift_imm); - - SetInternalFlagsFromInteger(bb, outer_shift, instr.generates_cc); - SetRegister(bb, instr.gpr0, outer_shift); - break; - } - default: - UNIMPLEMENTED_MSG("Unhandled BFE instruction: {}", opcode->get().GetName()); - } + Node op_b = [&] { + switch (opcode->get().GetId()) { + case OpCode::Id::BFE_R: + return GetRegister(instr.gpr20); + case OpCode::Id::BFE_C: + return GetConstBuffer(instr.cbuf34.index, instr.cbuf34.GetOffset()); + case OpCode::Id::BFE_IMM: + return Immediate(instr.alu.GetSignedImm20_20()); + default: + UNREACHABLE(); + return Immediate(0); + } + }(); + + UNIMPLEMENTED_IF_MSG(instr.bfe.rd_cc, "Condition codes in BFE is not implemented"); + UNIMPLEMENTED_IF_MSG(instr.bfe.brev, "BREV in BFE is not implemented"); + + const bool is_signed = instr.bfe.is_signed; + + const auto start_position = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, + Immediate(0), Immediate(8)); + const auto bits = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, + Immediate(8), Immediate(8)); + + auto result = + SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, start_position, bits); + SetRegister(bb, instr.gpr0, result); return pc; } -- cgit v1.2.3 From 96a4abe12d69a3fa35dd39e359438da18056f6ad Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Fri, 13 Mar 2020 14:13:31 +0700 Subject: shader_decode: implement BREV on BFE Implement reverse parallel follow: https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel --- src/video_core/shader/decode/bfe.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index c3ef88373..911d53657 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -33,17 +33,36 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { }(); UNIMPLEMENTED_IF_MSG(instr.bfe.rd_cc, "Condition codes in BFE is not implemented"); - UNIMPLEMENTED_IF_MSG(instr.bfe.brev, "BREV in BFE is not implemented"); const bool is_signed = instr.bfe.is_signed; - const auto start_position = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, - Immediate(0), Immediate(8)); + if (instr.bfe.brev) { + const auto swap = [&](u32 s, u32 mask) { + Node v1 = + SignedOperation(OperationCode::ILogicalShiftRight, is_signed, op_a, Immediate(s)); + if (mask != 0) { + v1 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, v1, Immediate(mask)); + } + Node v2 = op_a; + if (mask != 0) { + v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, op_a, Immediate(mask)); + } + v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, v2, Immediate(s)); + return SignedOperation(OperationCode::IBitwiseOr, is_signed, v1, v2); + }; + op_a = swap(1, 0x55555555U); + op_a = swap(2, 0x33333333U); + op_a = swap(4, 0x0F0F0F0FU); + op_a = swap(8, 0x00FF00FFU); + op_a = swap(16, 0); + } + + const auto offset = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, + Immediate(0), Immediate(8)); const auto bits = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, Immediate(8), Immediate(8)); - - auto result = - SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, start_position, bits); + const auto result = + SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits); SetRegister(bb, instr.gpr0, result); return pc; -- cgit v1.2.3 From 70ff82f72d4dfd0b296e885b0f1517d0a46e64e1 Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Fri, 13 Mar 2020 14:20:18 +0700 Subject: shader_decode: BFE add ref of reverse parallel method. --- src/video_core/shader/decode/bfe.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index 911d53657..39587e0df 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -36,6 +36,9 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { const bool is_signed = instr.bfe.is_signed; + // using reverse parallel method in + // https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel + // note for later if possible to implement faster method. if (instr.bfe.brev) { const auto swap = [&](u32 s, u32 mask) { Node v1 = -- cgit v1.2.3 From 325977c0c6947597618afca51494df62bacefb76 Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Fri, 13 Mar 2020 15:35:15 +0700 Subject: Apply suggestions from code review Co-Authored-By: Mat M. --- src/video_core/shader/decode/bfe.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index 39587e0df..7f2647973 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -44,14 +44,14 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { Node v1 = SignedOperation(OperationCode::ILogicalShiftRight, is_signed, op_a, Immediate(s)); if (mask != 0) { - v1 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, v1, Immediate(mask)); + v1 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v1), Immediate(mask)); } Node v2 = op_a; if (mask != 0) { - v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, op_a, Immediate(mask)); + v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v2), Immediate(mask)); } - v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, v2, Immediate(s)); - return SignedOperation(OperationCode::IBitwiseOr, is_signed, v1, v2); + v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, std::move(v2), Immediate(s)); + return SignedOperation(OperationCode::IBitwiseOr, is_signed, std::move(v1), std::move(v2)); }; op_a = swap(1, 0x55555555U); op_a = swap(2, 0x33333333U); @@ -66,7 +66,7 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { Immediate(8), Immediate(8)); const auto result = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits); - SetRegister(bb, instr.gpr0, result); + SetRegister(bb, instr.gpr0, std::move(result)); return pc; } -- cgit v1.2.3 From 00607fe1e018e9b9144c2e1d8dac96150455bc0b Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Fri, 13 Mar 2020 15:38:57 +0700 Subject: clang-format --- src/video_core/shader/decode/bfe.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index 7f2647973..80253aee3 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -44,14 +44,18 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { Node v1 = SignedOperation(OperationCode::ILogicalShiftRight, is_signed, op_a, Immediate(s)); if (mask != 0) { - v1 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v1), Immediate(mask)); + v1 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v1), + Immediate(mask)); } Node v2 = op_a; if (mask != 0) { - v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v2), Immediate(mask)); + v2 = SignedOperation(OperationCode::IBitwiseAnd, is_signed, std::move(v2), + Immediate(mask)); } - v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, std::move(v2), Immediate(s)); - return SignedOperation(OperationCode::IBitwiseOr, is_signed, std::move(v1), std::move(v2)); + v2 = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, std::move(v2), + Immediate(s)); + return SignedOperation(OperationCode::IBitwiseOr, is_signed, std::move(v1), + std::move(v2)); }; op_a = swap(1, 0x55555555U); op_a = swap(2, 0x33333333U); -- cgit v1.2.3 From 240d45830d16342cb357427894690ddde660e259 Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Sat, 14 Mar 2020 09:57:24 +0700 Subject: nit --- src/video_core/shader/decode/bfe.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index 80253aee3..69ef9013d 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -68,7 +68,7 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { Immediate(0), Immediate(8)); const auto bits = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, Immediate(8), Immediate(8)); - const auto result = + auto result = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits); SetRegister(bb, instr.gpr0, std::move(result)); -- cgit v1.2.3 From 3287b1247d97682b443f8db050b4ee73dac1d05b Mon Sep 17 00:00:00 2001 From: Nguyen Dac Nam Date: Sat, 14 Mar 2020 10:07:40 +0700 Subject: clang-format --- src/video_core/shader/decode/bfe.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/video_core/shader/decode/bfe.cpp') diff --git a/src/video_core/shader/decode/bfe.cpp b/src/video_core/shader/decode/bfe.cpp index 69ef9013d..8e3b46e8e 100644 --- a/src/video_core/shader/decode/bfe.cpp +++ b/src/video_core/shader/decode/bfe.cpp @@ -68,8 +68,7 @@ u32 ShaderIR::DecodeBfe(NodeBlock& bb, u32 pc) { Immediate(0), Immediate(8)); const auto bits = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_b, Immediate(8), Immediate(8)); - auto result = - SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits); + auto result = SignedOperation(OperationCode::IBitfieldExtract, is_signed, op_a, offset, bits); SetRegister(bb, instr.gpr0, std::move(result)); return pc; -- cgit v1.2.3