command_processor.cpp 10 KB

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