vertex_shader.cpp 12 KB

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