command_processor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "clipper.h"
  5. #include "command_processor.h"
  6. #include "math.h"
  7. #include "pica.h"
  8. #include "primitive_assembly.h"
  9. #include "vertex_shader.h"
  10. #include "debug_utils/debug_utils.h"
  11. namespace Pica {
  12. Regs registers;
  13. namespace CommandProcessor {
  14. static int float_regs_counter = 0;
  15. static u32 uniform_write_buffer[4];
  16. // Used for VSLoadProgramData and VSLoadSwizzleData
  17. static u32 vs_binary_write_offset = 0;
  18. static u32 vs_swizzle_write_offset = 0;
  19. static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
  20. if (id >= registers.NumIds())
  21. return;
  22. // TODO: Figure out how register masking acts on e.g. vs_uniform_setup.set_value
  23. u32 old_value = registers[id];
  24. registers[id] = (old_value & ~mask) | (value & mask);
  25. DebugUtils::OnPicaRegWrite(id, registers[id]);
  26. switch(id) {
  27. // It seems like these trigger vertex rendering
  28. case PICA_REG_INDEX(trigger_draw):
  29. case PICA_REG_INDEX(trigger_draw_indexed):
  30. {
  31. DebugUtils::DumpTevStageConfig(registers.GetTevStages());
  32. const auto& attribute_config = registers.vertex_attributes;
  33. const u8* const base_address = Memory::GetPointer(attribute_config.GetBaseAddress());
  34. // Information about internal vertex attributes
  35. const u8* vertex_attribute_sources[16];
  36. u32 vertex_attribute_strides[16];
  37. u32 vertex_attribute_formats[16];
  38. u32 vertex_attribute_elements[16];
  39. u32 vertex_attribute_element_size[16];
  40. // Setup attribute data from loaders
  41. for (int loader = 0; loader < 12; ++loader) {
  42. const auto& loader_config = attribute_config.attribute_loaders[loader];
  43. const u8* load_address = base_address + loader_config.data_offset;
  44. // TODO: What happens if a loader overwrites a previous one's data?
  45. for (int component = 0; component < loader_config.component_count; ++component) {
  46. u32 attribute_index = loader_config.GetComponent(component);
  47. vertex_attribute_sources[attribute_index] = load_address;
  48. vertex_attribute_strides[attribute_index] = loader_config.byte_count;
  49. vertex_attribute_formats[attribute_index] = (u32)attribute_config.GetFormat(attribute_index);
  50. vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index);
  51. vertex_attribute_element_size[attribute_index] = attribute_config.GetElementSizeInBytes(attribute_index);
  52. load_address += attribute_config.GetStride(attribute_index);
  53. }
  54. }
  55. // Load vertices
  56. bool is_indexed = (id == PICA_REG_INDEX(trigger_draw_indexed));
  57. const auto& index_info = registers.index_array;
  58. const u8* index_address_8 = (u8*)base_address + index_info.offset;
  59. const u16* index_address_16 = (u16*)index_address_8;
  60. bool index_u16 = (bool)index_info.format;
  61. DebugUtils::GeometryDumper geometry_dumper;
  62. PrimitiveAssembler<VertexShader::OutputVertex> clipper_primitive_assembler(registers.triangle_topology.Value());
  63. PrimitiveAssembler<DebugUtils::GeometryDumper::Vertex> dumping_primitive_assembler(registers.triangle_topology.Value());
  64. for (int index = 0; index < registers.num_vertices; ++index)
  65. {
  66. int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index;
  67. if (is_indexed) {
  68. // TODO: Implement some sort of vertex cache!
  69. }
  70. // Initialize data for the current vertex
  71. VertexShader::InputVertex input;
  72. for (int i = 0; i < attribute_config.GetNumTotalAttributes(); ++i) {
  73. for (int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  74. const u8* srcdata = vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i];
  75. const float srcval = (vertex_attribute_formats[i] == 0) ? *(s8*)srcdata :
  76. (vertex_attribute_formats[i] == 1) ? *(u8*)srcdata :
  77. (vertex_attribute_formats[i] == 2) ? *(s16*)srcdata :
  78. *(float*)srcdata;
  79. input.attr[i][comp] = float24::FromFloat32(srcval);
  80. DEBUG_LOG(GPU, "Loaded component %x of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08x + 0x%04x: %f",
  81. comp, i, vertex, index,
  82. attribute_config.GetBaseAddress(),
  83. vertex_attribute_sources[i] - base_address,
  84. srcdata - vertex_attribute_sources[i],
  85. input.attr[i][comp].ToFloat32());
  86. }
  87. }
  88. // NOTE: When dumping geometry, we simply assume that the first input attribute
  89. // corresponds to the position for now.
  90. DebugUtils::GeometryDumper::Vertex dumped_vertex = {
  91. input.attr[0][0].ToFloat32(), input.attr[0][1].ToFloat32(), input.attr[0][2].ToFloat32()
  92. };
  93. using namespace std::placeholders;
  94. dumping_primitive_assembler.SubmitVertex(dumped_vertex,
  95. std::bind(&DebugUtils::GeometryDumper::AddTriangle,
  96. &geometry_dumper, _1, _2, _3));
  97. // Send to vertex shader
  98. VertexShader::OutputVertex output = VertexShader::RunShader(input, attribute_config.GetNumTotalAttributes());
  99. if (is_indexed) {
  100. // TODO: Add processed vertex to vertex cache!
  101. }
  102. // Send to triangle clipper
  103. clipper_primitive_assembler.SubmitVertex(output, Clipper::ProcessTriangle);
  104. }
  105. geometry_dumper.Dump();
  106. break;
  107. }
  108. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[0], 0x2c1):
  109. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[1], 0x2c2):
  110. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[2], 0x2c3):
  111. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[3], 0x2c4):
  112. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[4], 0x2c5):
  113. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[5], 0x2c6):
  114. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[6], 0x2c7):
  115. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[7], 0x2c8):
  116. {
  117. auto& uniform_setup = registers.vs_uniform_setup;
  118. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  119. // it directly write the values?
  120. uniform_write_buffer[float_regs_counter++] = value;
  121. // Uniforms are written in a packed format such that 4 float24 values are encoded in
  122. // three 32-bit numbers. We write to internal memory once a full such vector is
  123. // written.
  124. if ((float_regs_counter >= 4 && uniform_setup.IsFloat32()) ||
  125. (float_regs_counter >= 3 && !uniform_setup.IsFloat32())) {
  126. float_regs_counter = 0;
  127. auto& uniform = VertexShader::GetFloatUniform(uniform_setup.index);
  128. if (uniform_setup.index > 95) {
  129. ERROR_LOG(GPU, "Invalid VS uniform index %d", (int)uniform_setup.index);
  130. break;
  131. }
  132. // NOTE: The destination component order indeed is "backwards"
  133. if (uniform_setup.IsFloat32()) {
  134. for (auto i : {0,1,2,3})
  135. uniform[3 - i] = float24::FromFloat32(*(float*)(&uniform_write_buffer[i]));
  136. } else {
  137. // TODO: Untested
  138. uniform.w = float24::FromRawFloat24(uniform_write_buffer[0] >> 8);
  139. uniform.z = float24::FromRawFloat24(((uniform_write_buffer[0] & 0xFF)<<16) | ((uniform_write_buffer[1] >> 16) & 0xFFFF));
  140. uniform.y = float24::FromRawFloat24(((uniform_write_buffer[1] & 0xFFFF)<<8) | ((uniform_write_buffer[2] >> 24) & 0xFF));
  141. uniform.x = float24::FromRawFloat24(uniform_write_buffer[2] & 0xFFFFFF);
  142. }
  143. DEBUG_LOG(GPU, "Set uniform %x to (%f %f %f %f)", (int)uniform_setup.index,
  144. uniform.x.ToFloat32(), uniform.y.ToFloat32(), uniform.z.ToFloat32(),
  145. uniform.w.ToFloat32());
  146. // TODO: Verify that this actually modifies the register!
  147. uniform_setup.index = uniform_setup.index + 1;
  148. }
  149. break;
  150. }
  151. // Seems to be used to reset the write pointer for VSLoadProgramData
  152. case PICA_REG_INDEX(vs_program.begin_load):
  153. vs_binary_write_offset = 0;
  154. break;
  155. // Load shader program code
  156. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[0], 0x2cc):
  157. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[1], 0x2cd):
  158. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[2], 0x2ce):
  159. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[3], 0x2cf):
  160. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[4], 0x2d0):
  161. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[5], 0x2d1):
  162. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[6], 0x2d2):
  163. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[7], 0x2d3):
  164. {
  165. VertexShader::SubmitShaderMemoryChange(vs_binary_write_offset, value);
  166. vs_binary_write_offset++;
  167. break;
  168. }
  169. // Seems to be used to reset the write pointer for VSLoadSwizzleData
  170. case PICA_REG_INDEX(vs_swizzle_patterns.begin_load):
  171. vs_swizzle_write_offset = 0;
  172. break;
  173. // Load swizzle pattern data
  174. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[0], 0x2d6):
  175. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[1], 0x2d7):
  176. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[2], 0x2d8):
  177. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[3], 0x2d9):
  178. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[4], 0x2da):
  179. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[5], 0x2db):
  180. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[6], 0x2dc):
  181. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[7], 0x2dd):
  182. {
  183. VertexShader::SubmitSwizzleDataChange(vs_swizzle_write_offset, value);
  184. vs_swizzle_write_offset++;
  185. break;
  186. }
  187. default:
  188. break;
  189. }
  190. }
  191. static std::ptrdiff_t ExecuteCommandBlock(const u32* first_command_word) {
  192. const CommandHeader& header = *(const CommandHeader*)(&first_command_word[1]);
  193. u32* read_pointer = (u32*)first_command_word;
  194. const u32 write_mask = ((header.parameter_mask & 0x1) ? (0xFFu << 0) : 0u) |
  195. ((header.parameter_mask & 0x2) ? (0xFFu << 8) : 0u) |
  196. ((header.parameter_mask & 0x4) ? (0xFFu << 16) : 0u) |
  197. ((header.parameter_mask & 0x8) ? (0xFFu << 24) : 0u);
  198. WritePicaReg(header.cmd_id, *read_pointer, write_mask);
  199. read_pointer += 2;
  200. for (int i = 1; i < 1+header.extra_data_length; ++i) {
  201. u32 cmd = header.cmd_id + ((header.group_commands) ? i : 0);
  202. WritePicaReg(cmd, *read_pointer, write_mask);
  203. ++read_pointer;
  204. }
  205. // align read pointer to 8 bytes
  206. if ((first_command_word - read_pointer) % 2)
  207. ++read_pointer;
  208. return read_pointer - first_command_word;
  209. }
  210. void ProcessCommandList(const u32* list, u32 size) {
  211. u32* read_pointer = (u32*)list;
  212. while (read_pointer < list + size) {
  213. read_pointer += ExecuteCommandBlock(read_pointer);
  214. }
  215. }
  216. } // namespace
  217. } // namespace