vertex_shader.cpp 12 KB

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