vertex_shader.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <stack>
  5. #include <boost/range/algorithm.hpp>
  6. #include <common/file_util.h>
  7. #include <nihstro/shader_bytecode.h>
  8. #include "common/profiler.h"
  9. #include "pica.h"
  10. #include "vertex_shader.h"
  11. #include "debug_utils/debug_utils.h"
  12. using nihstro::OpCode;
  13. using nihstro::Instruction;
  14. using nihstro::RegisterType;
  15. using nihstro::SourceRegister;
  16. using nihstro::SwizzlePattern;
  17. namespace Pica {
  18. namespace VertexShader {
  19. struct VertexShaderState {
  20. const u32* program_counter;
  21. const float24* input_register_table[16];
  22. Math::Vec4<float24> output_registers[16];
  23. Math::Vec4<float24> temporary_registers[16];
  24. bool conditional_code[2];
  25. // Two Address registers and one loop counter
  26. // TODO: How many bits do these actually have?
  27. s32 address_registers[3];
  28. enum {
  29. INVALID_ADDRESS = 0xFFFFFFFF
  30. };
  31. struct CallStackElement {
  32. u32 final_address; // Address upon which we jump to return_address
  33. u32 return_address; // Where to jump when leaving scope
  34. u8 repeat_counter; // How often to repeat until this call stack element is removed
  35. u8 loop_increment; // Which value to add to the loop counter after an iteration
  36. // TODO: Should this be a signed value? Does it even matter?
  37. u32 loop_address; // The address where we'll return to after each loop iteration
  38. };
  39. // TODO: Is there a maximal size for this?
  40. std::stack<CallStackElement> call_stack;
  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. const auto& uniforms = g_state.vs.uniforms;
  48. const auto& swizzle_data = g_state.vs.swizzle_data;
  49. const auto& program_code = g_state.vs.program_code;
  50. // Placeholder for invalid inputs
  51. static float24 dummy_vec4_float24[4];
  52. while (true) {
  53. if (!state.call_stack.empty()) {
  54. auto& top = state.call_stack.top();
  55. if (state.program_counter - program_code.data() == top.final_address) {
  56. state.address_registers[2] += top.loop_increment;
  57. if (top.repeat_counter-- == 0) {
  58. state.program_counter = &program_code[top.return_address];
  59. state.call_stack.pop();
  60. } else {
  61. state.program_counter = &program_code[top.loop_address];
  62. }
  63. // TODO: Is "trying again" accurate to hardware?
  64. continue;
  65. }
  66. }
  67. bool exit_loop = false;
  68. const Instruction& instr = *(const Instruction*)state.program_counter;
  69. const SwizzlePattern& swizzle = *(SwizzlePattern*)&swizzle_data[instr.common.operand_desc_id];
  70. static auto call = [&program_code](VertexShaderState& state, u32 offset, u32 num_instructions,
  71. u32 return_offset, u8 repeat_count, u8 loop_increment) {
  72. state.program_counter = &program_code[offset] - 1; // -1 to make sure when incrementing the PC we end up at the correct offset
  73. state.call_stack.push({ offset + num_instructions, return_offset, repeat_count, loop_increment, offset });
  74. };
  75. u32 binary_offset = state.program_counter - program_code.data();
  76. state.debug.max_offset = std::max<u32>(state.debug.max_offset, 1 + binary_offset);
  77. auto LookupSourceRegister = [&](const SourceRegister& source_reg) -> const float24* {
  78. switch (source_reg.GetRegisterType()) {
  79. case RegisterType::Input:
  80. return state.input_register_table[source_reg.GetIndex()];
  81. case RegisterType::Temporary:
  82. return &state.temporary_registers[source_reg.GetIndex()].x;
  83. case RegisterType::FloatUniform:
  84. return &uniforms.f[source_reg.GetIndex()].x;
  85. default:
  86. return dummy_vec4_float24;
  87. }
  88. };
  89. switch (instr.opcode.Value().GetInfo().type) {
  90. case OpCode::Type::Arithmetic:
  91. {
  92. const bool is_inverted = (0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed));
  93. const int address_offset = (instr.common.address_register_index == 0)
  94. ? 0 : state.address_registers[instr.common.address_register_index - 1];
  95. const float24* src1_ = LookupSourceRegister(instr.common.GetSrc1(is_inverted) + (!is_inverted * address_offset));
  96. const float24* src2_ = LookupSourceRegister(instr.common.GetSrc2(is_inverted) + ( is_inverted * address_offset));
  97. const bool negate_src1 = ((bool)swizzle.negate_src1 != false);
  98. const bool negate_src2 = ((bool)swizzle.negate_src2 != false);
  99. float24 src1[4] = {
  100. src1_[(int)swizzle.GetSelectorSrc1(0)],
  101. src1_[(int)swizzle.GetSelectorSrc1(1)],
  102. src1_[(int)swizzle.GetSelectorSrc1(2)],
  103. src1_[(int)swizzle.GetSelectorSrc1(3)],
  104. };
  105. if (negate_src1) {
  106. src1[0] = src1[0] * float24::FromFloat32(-1);
  107. src1[1] = src1[1] * float24::FromFloat32(-1);
  108. src1[2] = src1[2] * float24::FromFloat32(-1);
  109. src1[3] = src1[3] * float24::FromFloat32(-1);
  110. }
  111. float24 src2[4] = {
  112. src2_[(int)swizzle.GetSelectorSrc2(0)],
  113. src2_[(int)swizzle.GetSelectorSrc2(1)],
  114. src2_[(int)swizzle.GetSelectorSrc2(2)],
  115. src2_[(int)swizzle.GetSelectorSrc2(3)],
  116. };
  117. if (negate_src2) {
  118. src2[0] = src2[0] * float24::FromFloat32(-1);
  119. src2[1] = src2[1] * float24::FromFloat32(-1);
  120. src2[2] = src2[2] * float24::FromFloat32(-1);
  121. src2[3] = src2[3] * float24::FromFloat32(-1);
  122. }
  123. float24* dest = (instr.common.dest.Value() < 0x10) ? &state.output_registers[instr.common.dest.Value().GetIndex()][0]
  124. : (instr.common.dest.Value() < 0x20) ? &state.temporary_registers[instr.common.dest.Value().GetIndex()][0]
  125. : dummy_vec4_float24;
  126. state.debug.max_opdesc_id = std::max<u32>(state.debug.max_opdesc_id, 1+instr.common.operand_desc_id);
  127. switch (instr.opcode.Value().EffectiveOpCode()) {
  128. case OpCode::Id::ADD:
  129. {
  130. for (int i = 0; i < 4; ++i) {
  131. if (!swizzle.DestComponentEnabled(i))
  132. continue;
  133. dest[i] = src1[i] + src2[i];
  134. }
  135. break;
  136. }
  137. case OpCode::Id::MUL:
  138. {
  139. for (int i = 0; i < 4; ++i) {
  140. if (!swizzle.DestComponentEnabled(i))
  141. continue;
  142. dest[i] = src1[i] * src2[i];
  143. }
  144. break;
  145. }
  146. case OpCode::Id::FLR:
  147. for (int i = 0; i < 4; ++i) {
  148. if (!swizzle.DestComponentEnabled(i))
  149. continue;
  150. dest[i] = float24::FromFloat32(std::floor(src1[i].ToFloat32()));
  151. }
  152. break;
  153. case OpCode::Id::MAX:
  154. for (int i = 0; i < 4; ++i) {
  155. if (!swizzle.DestComponentEnabled(i))
  156. continue;
  157. dest[i] = std::max(src1[i], src2[i]);
  158. }
  159. break;
  160. case OpCode::Id::MIN:
  161. for (int i = 0; i < 4; ++i) {
  162. if (!swizzle.DestComponentEnabled(i))
  163. continue;
  164. dest[i] = std::min(src1[i], src2[i]);
  165. }
  166. break;
  167. case OpCode::Id::DP3:
  168. case OpCode::Id::DP4:
  169. {
  170. float24 dot = float24::FromFloat32(0.f);
  171. int num_components = (instr.opcode.Value() == OpCode::Id::DP3) ? 3 : 4;
  172. for (int i = 0; i < num_components; ++i)
  173. dot = dot + src1[i] * src2[i];
  174. for (int i = 0; i < num_components; ++i) {
  175. if (!swizzle.DestComponentEnabled(i))
  176. continue;
  177. dest[i] = dot;
  178. }
  179. break;
  180. }
  181. // Reciprocal
  182. case OpCode::Id::RCP:
  183. {
  184. for (int i = 0; i < 4; ++i) {
  185. if (!swizzle.DestComponentEnabled(i))
  186. continue;
  187. // TODO: Be stable against division by zero!
  188. // TODO: I think this might be wrong... we should only use one component here
  189. dest[i] = float24::FromFloat32(1.0f / src1[i].ToFloat32());
  190. }
  191. break;
  192. }
  193. // Reciprocal Square Root
  194. case OpCode::Id::RSQ:
  195. {
  196. for (int i = 0; i < 4; ++i) {
  197. if (!swizzle.DestComponentEnabled(i))
  198. continue;
  199. // TODO: Be stable against division by zero!
  200. // TODO: I think this might be wrong... we should only use one component here
  201. dest[i] = float24::FromFloat32(1.0f / sqrt(src1[i].ToFloat32()));
  202. }
  203. break;
  204. }
  205. case OpCode::Id::MOVA:
  206. {
  207. for (int i = 0; i < 2; ++i) {
  208. if (!swizzle.DestComponentEnabled(i))
  209. continue;
  210. // TODO: Figure out how the rounding is done on hardware
  211. state.address_registers[i] = static_cast<s32>(src1[i].ToFloat32());
  212. }
  213. break;
  214. }
  215. case OpCode::Id::MOV:
  216. {
  217. for (int i = 0; i < 4; ++i) {
  218. if (!swizzle.DestComponentEnabled(i))
  219. continue;
  220. dest[i] = src1[i];
  221. }
  222. break;
  223. }
  224. case OpCode::Id::SLT:
  225. case OpCode::Id::SLTI:
  226. for (int i = 0; i < 4; ++i) {
  227. if (!swizzle.DestComponentEnabled(i))
  228. continue;
  229. dest[i] = (src1[i] < src2[i]) ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
  230. }
  231. break;
  232. case OpCode::Id::CMP:
  233. for (int i = 0; i < 2; ++i) {
  234. // TODO: Can you restrict to one compare via dest masking?
  235. auto compare_op = instr.common.compare_op;
  236. auto op = (i == 0) ? compare_op.x.Value() : compare_op.y.Value();
  237. switch (op) {
  238. case compare_op.Equal:
  239. state.conditional_code[i] = (src1[i] == src2[i]);
  240. break;
  241. case compare_op.NotEqual:
  242. state.conditional_code[i] = (src1[i] != src2[i]);
  243. break;
  244. case compare_op.LessThan:
  245. state.conditional_code[i] = (src1[i] < src2[i]);
  246. break;
  247. case compare_op.LessEqual:
  248. state.conditional_code[i] = (src1[i] <= src2[i]);
  249. break;
  250. case compare_op.GreaterThan:
  251. state.conditional_code[i] = (src1[i] > src2[i]);
  252. break;
  253. case compare_op.GreaterEqual:
  254. state.conditional_code[i] = (src1[i] >= src2[i]);
  255. break;
  256. default:
  257. LOG_ERROR(HW_GPU, "Unknown compare mode %x", static_cast<int>(op));
  258. break;
  259. }
  260. }
  261. break;
  262. default:
  263. LOG_ERROR(HW_GPU, "Unhandled arithmetic instruction: 0x%02x (%s): 0x%08x",
  264. (int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
  265. DEBUG_ASSERT(false);
  266. break;
  267. }
  268. break;
  269. }
  270. case OpCode::Type::MultiplyAdd:
  271. {
  272. if ((instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MAD) ||
  273. (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI)) {
  274. const SwizzlePattern& swizzle = *(SwizzlePattern*)&swizzle_data[instr.mad.operand_desc_id];
  275. bool is_inverted = (instr.opcode.Value().EffectiveOpCode() == OpCode::Id::MADI);
  276. const float24* src1_ = LookupSourceRegister(instr.mad.GetSrc1(is_inverted));
  277. const float24* src2_ = LookupSourceRegister(instr.mad.GetSrc2(is_inverted));
  278. const float24* src3_ = LookupSourceRegister(instr.mad.GetSrc3(is_inverted));
  279. const bool negate_src1 = ((bool)swizzle.negate_src1 != false);
  280. const bool negate_src2 = ((bool)swizzle.negate_src2 != false);
  281. const bool negate_src3 = ((bool)swizzle.negate_src3 != false);
  282. float24 src1[4] = {
  283. src1_[(int)swizzle.GetSelectorSrc1(0)],
  284. src1_[(int)swizzle.GetSelectorSrc1(1)],
  285. src1_[(int)swizzle.GetSelectorSrc1(2)],
  286. src1_[(int)swizzle.GetSelectorSrc1(3)],
  287. };
  288. if (negate_src1) {
  289. src1[0] = src1[0] * float24::FromFloat32(-1);
  290. src1[1] = src1[1] * float24::FromFloat32(-1);
  291. src1[2] = src1[2] * float24::FromFloat32(-1);
  292. src1[3] = src1[3] * float24::FromFloat32(-1);
  293. }
  294. float24 src2[4] = {
  295. src2_[(int)swizzle.GetSelectorSrc2(0)],
  296. src2_[(int)swizzle.GetSelectorSrc2(1)],
  297. src2_[(int)swizzle.GetSelectorSrc2(2)],
  298. src2_[(int)swizzle.GetSelectorSrc2(3)],
  299. };
  300. if (negate_src2) {
  301. src2[0] = src2[0] * float24::FromFloat32(-1);
  302. src2[1] = src2[1] * float24::FromFloat32(-1);
  303. src2[2] = src2[2] * float24::FromFloat32(-1);
  304. src2[3] = src2[3] * float24::FromFloat32(-1);
  305. }
  306. float24 src3[4] = {
  307. src3_[(int)swizzle.GetSelectorSrc3(0)],
  308. src3_[(int)swizzle.GetSelectorSrc3(1)],
  309. src3_[(int)swizzle.GetSelectorSrc3(2)],
  310. src3_[(int)swizzle.GetSelectorSrc3(3)],
  311. };
  312. if (negate_src3) {
  313. src3[0] = src3[0] * float24::FromFloat32(-1);
  314. src3[1] = src3[1] * float24::FromFloat32(-1);
  315. src3[2] = src3[2] * float24::FromFloat32(-1);
  316. src3[3] = src3[3] * float24::FromFloat32(-1);
  317. }
  318. float24* dest = (instr.mad.dest.Value() < 0x10) ? &state.output_registers[instr.mad.dest.Value().GetIndex()][0]
  319. : (instr.mad.dest.Value() < 0x20) ? &state.temporary_registers[instr.mad.dest.Value().GetIndex()][0]
  320. : dummy_vec4_float24;
  321. for (int i = 0; i < 4; ++i) {
  322. if (!swizzle.DestComponentEnabled(i))
  323. continue;
  324. dest[i] = src1[i] * src2[i] + src3[i];
  325. }
  326. } else {
  327. LOG_ERROR(HW_GPU, "Unhandled multiply-add instruction: 0x%02x (%s): 0x%08x",
  328. (int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
  329. }
  330. break;
  331. }
  332. default:
  333. {
  334. static auto evaluate_condition = [](const VertexShaderState& state, bool refx, bool refy, Instruction::FlowControlType flow_control) {
  335. bool results[2] = { refx == state.conditional_code[0],
  336. refy == state.conditional_code[1] };
  337. switch (flow_control.op) {
  338. case flow_control.Or:
  339. return results[0] || results[1];
  340. case flow_control.And:
  341. return results[0] && results[1];
  342. case flow_control.JustX:
  343. return results[0];
  344. case flow_control.JustY:
  345. return results[1];
  346. }
  347. };
  348. // Handle each instruction on its own
  349. switch (instr.opcode.Value()) {
  350. case OpCode::Id::END:
  351. exit_loop = true;
  352. break;
  353. case OpCode::Id::JMPC:
  354. if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
  355. state.program_counter = &program_code[instr.flow_control.dest_offset] - 1;
  356. }
  357. break;
  358. case OpCode::Id::JMPU:
  359. if (uniforms.b[instr.flow_control.bool_uniform_id]) {
  360. state.program_counter = &program_code[instr.flow_control.dest_offset] - 1;
  361. }
  362. break;
  363. case OpCode::Id::CALL:
  364. call(state,
  365. instr.flow_control.dest_offset,
  366. instr.flow_control.num_instructions,
  367. binary_offset + 1, 0, 0);
  368. break;
  369. case OpCode::Id::CALLU:
  370. if (uniforms.b[instr.flow_control.bool_uniform_id]) {
  371. call(state,
  372. instr.flow_control.dest_offset,
  373. instr.flow_control.num_instructions,
  374. binary_offset + 1, 0, 0);
  375. }
  376. break;
  377. case OpCode::Id::CALLC:
  378. if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
  379. call(state,
  380. instr.flow_control.dest_offset,
  381. instr.flow_control.num_instructions,
  382. binary_offset + 1, 0, 0);
  383. }
  384. break;
  385. case OpCode::Id::NOP:
  386. break;
  387. case OpCode::Id::IFU:
  388. if (uniforms.b[instr.flow_control.bool_uniform_id]) {
  389. call(state,
  390. binary_offset + 1,
  391. instr.flow_control.dest_offset - binary_offset - 1,
  392. instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
  393. } else {
  394. call(state,
  395. instr.flow_control.dest_offset,
  396. instr.flow_control.num_instructions,
  397. instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
  398. }
  399. break;
  400. case OpCode::Id::IFC:
  401. {
  402. // TODO: Do we need to consider swizzlers here?
  403. if (evaluate_condition(state, instr.flow_control.refx, instr.flow_control.refy, instr.flow_control)) {
  404. call(state,
  405. binary_offset + 1,
  406. instr.flow_control.dest_offset - binary_offset - 1,
  407. instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
  408. } else {
  409. call(state,
  410. instr.flow_control.dest_offset,
  411. instr.flow_control.num_instructions,
  412. instr.flow_control.dest_offset + instr.flow_control.num_instructions, 0, 0);
  413. }
  414. break;
  415. }
  416. case OpCode::Id::LOOP:
  417. {
  418. state.address_registers[2] = uniforms.i[instr.flow_control.int_uniform_id].y;
  419. call(state,
  420. binary_offset + 1,
  421. instr.flow_control.dest_offset - binary_offset + 1,
  422. instr.flow_control.dest_offset + 1,
  423. uniforms.i[instr.flow_control.int_uniform_id].x,
  424. uniforms.i[instr.flow_control.int_uniform_id].z);
  425. break;
  426. }
  427. default:
  428. LOG_ERROR(HW_GPU, "Unhandled instruction: 0x%02x (%s): 0x%08x",
  429. (int)instr.opcode.Value().EffectiveOpCode(), instr.opcode.Value().GetInfo().name, instr.hex);
  430. break;
  431. }
  432. break;
  433. }
  434. }
  435. ++state.program_counter;
  436. if (exit_loop)
  437. break;
  438. }
  439. }
  440. static Common::Profiling::TimingCategory shader_category("Vertex Shader");
  441. OutputVertex RunShader(const InputVertex& input, int num_attributes) {
  442. Common::Profiling::ScopeTimer timer(shader_category);
  443. const auto& regs = g_state.regs;
  444. const auto& vs = g_state.vs;
  445. VertexShaderState state;
  446. const u32* main = &vs.program_code[regs.vs_main_offset];
  447. state.program_counter = (u32*)main;
  448. state.debug.max_offset = 0;
  449. state.debug.max_opdesc_id = 0;
  450. // Setup input register table
  451. const auto& attribute_register_map = regs.vs_input_register_map;
  452. float24 dummy_register;
  453. boost::fill(state.input_register_table, &dummy_register);
  454. if (num_attributes > 0) state.input_register_table[attribute_register_map.attribute0_register] = &input.attr[0].x;
  455. if (num_attributes > 1) state.input_register_table[attribute_register_map.attribute1_register] = &input.attr[1].x;
  456. if (num_attributes > 2) state.input_register_table[attribute_register_map.attribute2_register] = &input.attr[2].x;
  457. if (num_attributes > 3) state.input_register_table[attribute_register_map.attribute3_register] = &input.attr[3].x;
  458. if (num_attributes > 4) state.input_register_table[attribute_register_map.attribute4_register] = &input.attr[4].x;
  459. if (num_attributes > 5) state.input_register_table[attribute_register_map.attribute5_register] = &input.attr[5].x;
  460. if (num_attributes > 6) state.input_register_table[attribute_register_map.attribute6_register] = &input.attr[6].x;
  461. if (num_attributes > 7) state.input_register_table[attribute_register_map.attribute7_register] = &input.attr[7].x;
  462. if (num_attributes > 8) state.input_register_table[attribute_register_map.attribute8_register] = &input.attr[8].x;
  463. if (num_attributes > 9) state.input_register_table[attribute_register_map.attribute9_register] = &input.attr[9].x;
  464. if (num_attributes > 10) state.input_register_table[attribute_register_map.attribute10_register] = &input.attr[10].x;
  465. if (num_attributes > 11) state.input_register_table[attribute_register_map.attribute11_register] = &input.attr[11].x;
  466. if (num_attributes > 12) state.input_register_table[attribute_register_map.attribute12_register] = &input.attr[12].x;
  467. if (num_attributes > 13) state.input_register_table[attribute_register_map.attribute13_register] = &input.attr[13].x;
  468. if (num_attributes > 14) state.input_register_table[attribute_register_map.attribute14_register] = &input.attr[14].x;
  469. if (num_attributes > 15) state.input_register_table[attribute_register_map.attribute15_register] = &input.attr[15].x;
  470. state.conditional_code[0] = false;
  471. state.conditional_code[1] = false;
  472. ProcessShaderCode(state);
  473. DebugUtils::DumpShader(vs.program_code.data(), state.debug.max_offset, vs.swizzle_data.data(),
  474. state.debug.max_opdesc_id, regs.vs_main_offset,
  475. regs.vs_output_attributes);
  476. // Setup output data
  477. OutputVertex ret;
  478. // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
  479. // figure out what those circumstances are and enable the remaining outputs then.
  480. for (int i = 0; i < 7; ++i) {
  481. const auto& output_register_map = regs.vs_output_attributes[i];
  482. u32 semantics[4] = {
  483. output_register_map.map_x, output_register_map.map_y,
  484. output_register_map.map_z, output_register_map.map_w
  485. };
  486. for (int comp = 0; comp < 4; ++comp) {
  487. float24* out = ((float24*)&ret) + semantics[comp];
  488. if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
  489. *out = state.output_registers[i][comp];
  490. } else {
  491. // Zero output so that attributes which aren't output won't have denormals in them,
  492. // which would slow us down later.
  493. memset(out, 0, sizeof(*out));
  494. }
  495. }
  496. }
  497. LOG_TRACE(Render_Software, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
  498. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
  499. ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  500. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());
  501. return ret;
  502. }
  503. } // namespace
  504. } // namespace