diff options
Diffstat (limited to 'src/video_core/vertex_shader.cpp')
| -rw-r--r-- | src/video_core/vertex_shader.cpp | 210 |
1 files changed, 165 insertions, 45 deletions
diff --git a/src/video_core/vertex_shader.cpp b/src/video_core/vertex_shader.cpp index ff825e2e1..bc8c0041c 100644 --- a/src/video_core/vertex_shader.cpp +++ b/src/video_core/vertex_shader.cpp @@ -85,8 +85,12 @@ struct VertexShaderState { }; struct CallStackElement { - u32 final_address; - u32 return_address; + u32 final_address; // Address upon which we jump to return_address + u32 return_address; // Where to jump when leaving scope + u8 repeat_counter; // How often to repeat until this call stack element is removed + u8 loop_increment; // Which value to add to the loop counter after an iteration + // TODO: Should this be a signed value? Does it even matter? + u32 loop_address; // The address where we'll return to after each loop iteration }; // TODO: Is there a maximal size for this? @@ -105,9 +109,16 @@ static void ProcessShaderCode(VertexShaderState& state) { while (true) { if (!state.call_stack.empty()) { - if (state.program_counter - shader_memory.data() == state.call_stack.top().final_address) { - state.program_counter = &shader_memory[state.call_stack.top().return_address]; - state.call_stack.pop(); + auto& top = state.call_stack.top(); + if (state.program_counter - shader_memory.data() == top.final_address) { + state.address_registers[2] += top.loop_increment; + + if (top.repeat_counter-- == 0) { + state.program_counter = &shader_memory[top.return_address]; + state.call_stack.pop(); + } else { + state.program_counter = &shader_memory[top.loop_address]; + } // TODO: Is "trying again" accurate to hardware? continue; @@ -118,9 +129,10 @@ static void ProcessShaderCode(VertexShaderState& state) { const Instruction& instr = *(const Instruction*)state.program_counter; const SwizzlePattern& swizzle = *(SwizzlePattern*)&swizzle_data[instr.common.operand_desc_id]; - auto call = [&](VertexShaderState& state, u32 offset, u32 num_instructions, u32 return_offset) { + static auto call = [](VertexShaderState& state, u32 offset, u32 num_instructions, + u32 return_offset, u8 repeat_count, u8 loop_increment) { state.program_counter = &shader_memory[offset] - 1; // -1 to make sure when incrementing the PC we end up at the correct offset - state.call_stack.push({ offset + num_instructions, return_offset }); + state.call_stack.push({ offset + num_instructions, return_offset, repeat_count, loop_increment, offset }); }; u32 binary_offset = state.program_counter - shader_memory.data(); @@ -146,13 +158,10 @@ static void ProcessShaderCode(VertexShaderState& state) { case Instruction::OpCodeType::Arithmetic: { bool is_inverted = 0 != (instr.opcode.GetInfo().subtype & Instruction::OpCodeInfo::SrcInversed); - if (is_inverted) { - // TODO: We don't really support this properly: For instance, the address register - // offset needs to be applied to SRC2 instead, etc. - // For now, we just abort in this situation. - LOG_CRITICAL(HW_GPU, "Bad condition..."); - exit(0); - } + // TODO: We don't really support this properly: For instance, the address register + // offset needs to be applied to SRC2 instead, etc. + // For now, we just abort in this situation. + ASSERT_MSG(!is_inverted, "Bad condition..."); const int address_offset = (instr.common.address_register_index == 0) ? 0 : state.address_registers[instr.common.address_register_index - 1]; @@ -255,7 +264,7 @@ static void ProcessShaderCode(VertexShaderState& state) { // TODO: Be stable against division by zero! // TODO: I think this might be wrong... we should only use one component here - dest[i] = float24::FromFloat32(1.0 / src1[i].ToFloat32()); + dest[i] = float24::FromFloat32(1.0f / src1[i].ToFloat32()); } break; @@ -270,7 +279,7 @@ static void ProcessShaderCode(VertexShaderState& state) { // TODO: Be stable against division by zero! // TODO: I think this might be wrong... we should only use one component here - dest[i] = float24::FromFloat32(1.0 / sqrt(src1[i].ToFloat32())); + dest[i] = float24::FromFloat32(1.0f / sqrt(src1[i].ToFloat32())); } break; @@ -342,24 +351,143 @@ static void ProcessShaderCode(VertexShaderState& state) { default: LOG_ERROR(HW_GPU, "Unhandled arithmetic instruction: 0x%02x (%s): 0x%08x", (int)instr.opcode.Value(), instr.opcode.GetInfo().name, instr.hex); - _dbg_assert_(HW_GPU, 0); + DEBUG_ASSERT(false); break; } break; } + + case Instruction::OpCodeType::MultiplyAdd: + { + if (instr.opcode.EffectiveOpCode() == Instruction::OpCode::MAD) { + const SwizzlePattern& swizzle = *(SwizzlePattern*)&swizzle_data[instr.mad.operand_desc_id]; + + const float24* src1_ = LookupSourceRegister(instr.mad.src1); + const float24* src2_ = LookupSourceRegister(instr.mad.src2); + const float24* src3_ = LookupSourceRegister(instr.mad.src3); + + const bool negate_src1 = ((bool)swizzle.negate_src1 != false); + const bool negate_src2 = ((bool)swizzle.negate_src2 != false); + const bool negate_src3 = ((bool)swizzle.negate_src3 != false); + + float24 src1[4] = { + src1_[(int)swizzle.GetSelectorSrc1(0)], + src1_[(int)swizzle.GetSelectorSrc1(1)], + src1_[(int)swizzle.GetSelectorSrc1(2)], + src1_[(int)swizzle.GetSelectorSrc1(3)], + }; + if (negate_src1) { + src1[0] = src1[0] * float24::FromFloat32(-1); + src1[1] = src1[1] * float24::FromFloat32(-1); + src1[2] = src1[2] * float24::FromFloat32(-1); + src1[3] = src1[3] * float24::FromFloat32(-1); + } + float24 src2[4] = { + src2_[(int)swizzle.GetSelectorSrc2(0)], + src2_[(int)swizzle.GetSelectorSrc2(1)], + src2_[(int)swizzle.GetSelectorSrc2(2)], + src2_[(int)swizzle.GetSelectorSrc2(3)], + }; + if (negate_src2) { + src2[0] = src2[0] * float24::FromFloat32(-1); + src2[1] = src2[1] * float24::FromFloat32(-1); + src2[2] = src2[2] * float24::FromFloat32(-1); + src2[3] = src2[3] * float24::FromFloat32(-1); + } + float24 src3[4] = { + src3_[(int)swizzle.GetSelectorSrc3(0)], + src3_[(int)swizzle.GetSelectorSrc3(1)], + src3_[(int)swizzle.GetSelectorSrc3(2)], + src3_[(int)swizzle.GetSelectorSrc3(3)], + }; + if (negate_src3) { + src3[0] = src3[0] * float24::FromFloat32(-1); + src3[1] = src3[1] * float24::FromFloat32(-1); + src3[2] = src3[2] * float24::FromFloat32(-1); + src3[3] = src3[3] * float24::FromFloat32(-1); + } + + float24* dest = (instr.mad.dest < 0x08) ? state.output_register_table[4*instr.mad.dest.GetIndex()] + : (instr.mad.dest < 0x10) ? dummy_vec4_float24 + : (instr.mad.dest < 0x20) ? &state.temporary_registers[instr.mad.dest.GetIndex()][0] + : dummy_vec4_float24; + + for (int i = 0; i < 4; ++i) { + if (!swizzle.DestComponentEnabled(i)) + continue; + + dest[i] = src1[i] * src2[i] + src3[i]; + } + } else { + LOG_ERROR(HW_GPU, "Unhandled multiply-add instruction: 0x%02x (%s): 0x%08x", + (int)instr.opcode.Value(), instr.opcode.GetInfo().name, instr.hex); + } + break; + } + default: + { + static auto evaluate_condition = [](const VertexShaderState& state, bool refx, bool refy, Instruction::FlowControlType flow_control) { + bool results[2] = { refx == state.conditional_code[0], + refy == state.conditional_code[1] }; + + switch (flow_control.op) { + case flow_control.Or: + return results[0] || results[1]; + + case flow_control.And: + return results[0] && results[1]; + + case flow_control.JustX: + return results[0]; + + case flow_control.JustY: + return results[1]; + } + }; + // Handle each instruction on its own switch (instr.opcode) { case Instruction::OpCode::END: exit_loop = true; break; + case Instruction::OpCode::JMPC: + if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) { + state.program_counter = &shader_memory[instr.flow_control.dest_offset] - 1; + } + break; + + case Instruction::OpCode::JMPU: + if (shader_uniforms.b[instr.flow_control.bool_uniform_id]) { + state.program_counter = &shader_memory[instr.flow_control.dest_offset] - 1; + } + break; + case Instruction::OpCode::CALL: call(state, instr.flow_control.dest_offset, instr.flow_control.num_instructions, - binary_offset + 1); + binary_offset + 1, 0, 0); + break; + + case Instruction::OpCode::CALLU: + if (shader_uniforms.b[instr.flow_control.bool_uniform_id]) { + call(state, + instr.flow_control.dest_offset, + instr.flow_control.num_instructions, + binary_offset + 1, 0, 0); + } + break; + + case Instruction::OpCode::CALLC: + if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) { + call(state, + instr.flow_control.dest_offset, + instr.flow_control.num_instructions, + binary_offset + 1, 0, 0); + } break; case Instruction::OpCode::NOP: @@ -370,12 +498,12 @@ static void ProcessShaderCode(VertexShaderState& state) { call(state, binary_offset + 1, instr.flow_control.dest_offset - binary_offset - 1, - instr.flow_control.dest_offset + instr.flow_control.num_instructions); + instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0); } else { call(state, instr.flow_control.dest_offset, instr.flow_control.num_instructions, - instr.flow_control.dest_offset + instr.flow_control.num_instructions); + instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0); } break; @@ -384,43 +512,34 @@ static void ProcessShaderCode(VertexShaderState& state) { { // TODO: Do we need to consider swizzlers here? - auto flow_control = instr.flow_control; - bool results[3] = { (bool)flow_control.refx == state.conditional_code[0], - (bool)flow_control.refy == state.conditional_code[1] }; - - switch (flow_control.op) { - case flow_control.Or: - results[2] = results[0] || results[1]; - break; - - case flow_control.And: - results[2] = results[0] && results[1]; - break; - - case flow_control.JustX: - results[2] = results[0]; - break; - - case flow_control.JustY: - results[2] = results[1]; - break; - } - - if (results[2]) { + if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) { call(state, binary_offset + 1, instr.flow_control.dest_offset - binary_offset - 1, - instr.flow_control.dest_offset + instr.flow_control.num_instructions); + instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0); } else { call(state, instr.flow_control.dest_offset, instr.flow_control.num_instructions, - instr.flow_control.dest_offset + instr.flow_control.num_instructions); + instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0); } break; } + case Instruction::OpCode::LOOP: + { + state.address_registers[2] = shader_uniforms.i[instr.flow_control.int_uniform_id].y; + + call(state, + binary_offset + 1, + instr.flow_control.dest_offset - binary_offset + 1, + instr.flow_control.dest_offset + 1, + shader_uniforms.i[instr.flow_control.int_uniform_id].x, + shader_uniforms.i[instr.flow_control.int_uniform_id].z); + break; + } + default: LOG_ERROR(HW_GPU, "Unhandled instruction: 0x%02x (%s): 0x%08x", (int)instr.opcode.Value(), instr.opcode.GetInfo().name, instr.hex); @@ -429,6 +548,7 @@ static void ProcessShaderCode(VertexShaderState& state) { break; } + } ++state.program_counter; |
