command_processor.cpp 14 KB

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