vertex_shader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "pica.h"
  5. #include "vertex_shader.h"
  6. #include <core/mem_map.h>
  7. #include <common/file_util.h>
  8. namespace Pica {
  9. namespace VertexShader {
  10. static struct {
  11. Math::Vec4<float24> f[96];
  12. } shader_uniforms;
  13. // TODO: Not sure where the shader binary and swizzle patterns are supposed to be loaded to!
  14. // For now, we just keep these local arrays around.
  15. static u32 shader_memory[1024];
  16. static u32 swizzle_data[1024];
  17. void SubmitShaderMemoryChange(u32 addr, u32 value)
  18. {
  19. shader_memory[addr] = value;
  20. }
  21. void SubmitSwizzleDataChange(u32 addr, u32 value)
  22. {
  23. swizzle_data[addr] = value;
  24. }
  25. Math::Vec4<float24>& GetFloatUniform(u32 index)
  26. {
  27. return shader_uniforms.f[index];
  28. }
  29. struct VertexShaderState {
  30. u32* program_counter;
  31. const float24* input_register_table[16];
  32. float24* output_register_table[7*4];
  33. Math::Vec4<float24> temporary_registers[16];
  34. bool status_registers[2];
  35. enum {
  36. INVALID_ADDRESS = 0xFFFFFFFF
  37. };
  38. u32 call_stack[8]; // TODO: What is the maximal call stack depth?
  39. u32* call_stack_pointer;
  40. };
  41. static void ProcessShaderCode(VertexShaderState& state) {
  42. while (true) {
  43. bool increment_pc = true;
  44. bool exit_loop = false;
  45. const Instruction& instr = *(const Instruction*)state.program_counter;
  46. const float24* src1_ = (instr.common.src1 < 0x10) ? state.input_register_table[instr.common.src1]
  47. : (instr.common.src1 < 0x20) ? &state.temporary_registers[instr.common.src1-0x10].x
  48. : (instr.common.src1 < 0x80) ? &shader_uniforms.f[instr.common.src1-0x20].x
  49. : nullptr;
  50. const float24* src2_ = (instr.common.src2 < 0x10) ? state.input_register_table[instr.common.src2]
  51. : &state.temporary_registers[instr.common.src2-0x10].x;
  52. // TODO: Unsure about the limit values
  53. float24* dest = (instr.common.dest <= 0x1C) ? state.output_register_table[instr.common.dest]
  54. : (instr.common.dest <= 0x3C) ? nullptr
  55. : (instr.common.dest <= 0x7C) ? &state.temporary_registers[(instr.common.dest-0x40)/4][instr.common.dest%4]
  56. : nullptr;
  57. const SwizzlePattern& swizzle = *(SwizzlePattern*)&swizzle_data[instr.common.operand_desc_id];
  58. const float24 src1[4] = {
  59. src1_[(int)swizzle.GetSelectorSrc1(0)],
  60. src1_[(int)swizzle.GetSelectorSrc1(1)],
  61. src1_[(int)swizzle.GetSelectorSrc1(2)],
  62. src1_[(int)swizzle.GetSelectorSrc1(3)],
  63. };
  64. const float24 src2[4] = {
  65. src2_[(int)swizzle.GetSelectorSrc2(0)],
  66. src2_[(int)swizzle.GetSelectorSrc2(1)],
  67. src2_[(int)swizzle.GetSelectorSrc2(2)],
  68. src2_[(int)swizzle.GetSelectorSrc2(3)],
  69. };
  70. switch (instr.opcode) {
  71. case Instruction::OpCode::ADD:
  72. {
  73. for (int i = 0; i < 4; ++i) {
  74. if (!swizzle.DestComponentEnabled(i))
  75. continue;
  76. dest[i] = src1[i] + src2[i];
  77. }
  78. break;
  79. }
  80. case Instruction::OpCode::MUL:
  81. {
  82. for (int i = 0; i < 4; ++i) {
  83. if (!swizzle.DestComponentEnabled(i))
  84. continue;
  85. dest[i] = src1[i] * src2[i];
  86. }
  87. break;
  88. }
  89. case Instruction::OpCode::DP3:
  90. case Instruction::OpCode::DP4:
  91. {
  92. float24 dot = float24::FromFloat32(0.f);
  93. int num_components = (instr.opcode == Instruction::OpCode::DP3) ? 3 : 4;
  94. for (int i = 0; i < num_components; ++i)
  95. dot = dot + src1[i] * src2[i];
  96. for (int i = 0; i < num_components; ++i) {
  97. if (!swizzle.DestComponentEnabled(i))
  98. continue;
  99. dest[i] = dot;
  100. }
  101. break;
  102. }
  103. // Reciprocal
  104. case Instruction::OpCode::RCP:
  105. {
  106. for (int i = 0; i < 4; ++i) {
  107. if (!swizzle.DestComponentEnabled(i))
  108. continue;
  109. // TODO: Be stable against division by zero!
  110. // TODO: I think this might be wrong... we should only use one component here
  111. dest[i] = float24::FromFloat32(1.0 / src1[i].ToFloat32());
  112. }
  113. break;
  114. }
  115. // Reciprocal Square Root
  116. case Instruction::OpCode::RSQ:
  117. {
  118. for (int i = 0; i < 4; ++i) {
  119. if (!swizzle.DestComponentEnabled(i))
  120. continue;
  121. // TODO: Be stable against division by zero!
  122. // TODO: I think this might be wrong... we should only use one component here
  123. dest[i] = float24::FromFloat32(1.0 / sqrt(src1[i].ToFloat32()));
  124. }
  125. break;
  126. }
  127. case Instruction::OpCode::MOV:
  128. {
  129. for (int i = 0; i < 4; ++i) {
  130. if (!swizzle.DestComponentEnabled(i))
  131. continue;
  132. dest[i] = src1[i];
  133. }
  134. break;
  135. }
  136. case Instruction::OpCode::RET:
  137. if (*state.call_stack_pointer == VertexShaderState::INVALID_ADDRESS) {
  138. exit_loop = true;
  139. } else {
  140. state.program_counter = &shader_memory[*state.call_stack_pointer--];
  141. *state.call_stack_pointer = VertexShaderState::INVALID_ADDRESS;
  142. }
  143. break;
  144. case Instruction::OpCode::CALL:
  145. increment_pc = false;
  146. _dbg_assert_(GPU, state.call_stack_pointer - state.call_stack < sizeof(state.call_stack));
  147. *++state.call_stack_pointer = state.program_counter - shader_memory;
  148. // TODO: Does this offset refer to the beginning of shader memory?
  149. state.program_counter = &shader_memory[instr.flow_control.offset_words];
  150. break;
  151. case Instruction::OpCode::FLS:
  152. // TODO: Do whatever needs to be done here?
  153. break;
  154. default:
  155. ERROR_LOG(GPU, "Unhandled instruction: 0x%02x (%s): 0x%08x",
  156. (int)instr.opcode.Value(), instr.GetOpCodeName().c_str(), instr.hex);
  157. break;
  158. }
  159. if (increment_pc)
  160. ++state.program_counter;
  161. if (exit_loop)
  162. break;
  163. }
  164. }
  165. OutputVertex RunShader(const InputVertex& input, int num_attributes)
  166. {
  167. VertexShaderState state;
  168. const u32* main = &shader_memory[registers.vs_main_offset];
  169. state.program_counter = (u32*)main;
  170. // Setup input register table
  171. const auto& attribute_register_map = registers.vs_input_register_map;
  172. float24 dummy_register;
  173. std::fill(&state.input_register_table[0], &state.input_register_table[16], &dummy_register);
  174. if(num_attributes > 0) state.input_register_table[attribute_register_map.attribute0_register] = &input.attr[0].x;
  175. if(num_attributes > 1) state.input_register_table[attribute_register_map.attribute1_register] = &input.attr[1].x;
  176. if(num_attributes > 2) state.input_register_table[attribute_register_map.attribute2_register] = &input.attr[2].x;
  177. if(num_attributes > 3) state.input_register_table[attribute_register_map.attribute3_register] = &input.attr[3].x;
  178. if(num_attributes > 4) state.input_register_table[attribute_register_map.attribute4_register] = &input.attr[4].x;
  179. if(num_attributes > 5) state.input_register_table[attribute_register_map.attribute5_register] = &input.attr[5].x;
  180. if(num_attributes > 6) state.input_register_table[attribute_register_map.attribute6_register] = &input.attr[6].x;
  181. if(num_attributes > 7) state.input_register_table[attribute_register_map.attribute7_register] = &input.attr[7].x;
  182. if(num_attributes > 8) state.input_register_table[attribute_register_map.attribute8_register] = &input.attr[8].x;
  183. if(num_attributes > 9) state.input_register_table[attribute_register_map.attribute9_register] = &input.attr[9].x;
  184. if(num_attributes > 10) state.input_register_table[attribute_register_map.attribute10_register] = &input.attr[10].x;
  185. if(num_attributes > 11) state.input_register_table[attribute_register_map.attribute11_register] = &input.attr[11].x;
  186. if(num_attributes > 12) state.input_register_table[attribute_register_map.attribute12_register] = &input.attr[12].x;
  187. if(num_attributes > 13) state.input_register_table[attribute_register_map.attribute13_register] = &input.attr[13].x;
  188. if(num_attributes > 14) state.input_register_table[attribute_register_map.attribute14_register] = &input.attr[14].x;
  189. if(num_attributes > 15) state.input_register_table[attribute_register_map.attribute15_register] = &input.attr[15].x;
  190. // Setup output register table
  191. OutputVertex ret;
  192. for (int i = 0; i < 7; ++i) {
  193. const auto& output_register_map = registers.vs_output_attributes[i];
  194. u32 semantics[4] = {
  195. output_register_map.map_x, output_register_map.map_y,
  196. output_register_map.map_z, output_register_map.map_w
  197. };
  198. for (int comp = 0; comp < 4; ++comp)
  199. state.output_register_table[4*i+comp] = ((float24*)&ret) + semantics[comp];
  200. }
  201. state.status_registers[0] = false;
  202. state.status_registers[1] = false;
  203. std::fill(state.call_stack, state.call_stack + sizeof(state.call_stack) / sizeof(state.call_stack[0]),
  204. VertexShaderState::INVALID_ADDRESS);
  205. state.call_stack_pointer = &state.call_stack[0];
  206. ProcessShaderCode(state);
  207. DEBUG_LOG(GPU, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
  208. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
  209. ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  210. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());
  211. return ret;
  212. }
  213. } // namespace
  214. } // namespace