vertex_shader.cpp 13 KB

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