command_processor.cpp 16 KB

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