vertex_shader.cpp 25 KB

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