command_processor.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstddef>
  6. #include <memory>
  7. #include <utility>
  8. #include "common/assert.h"
  9. #include "common/logging/log.h"
  10. #include "common/microprofile.h"
  11. #include "common/vector_math.h"
  12. #include "core/hle/service/gsp_gpu.h"
  13. #include "core/hw/gpu.h"
  14. #include "core/memory.h"
  15. #include "core/tracer/recorder.h"
  16. #include "video_core/command_processor.h"
  17. #include "video_core/debug_utils/debug_utils.h"
  18. #include "video_core/pica.h"
  19. #include "video_core/pica_state.h"
  20. #include "video_core/pica_types.h"
  21. #include "video_core/primitive_assembly.h"
  22. #include "video_core/rasterizer_interface.h"
  23. #include "video_core/renderer_base.h"
  24. #include "video_core/shader/shader.h"
  25. #include "video_core/vertex_loader.h"
  26. #include "video_core/video_core.h"
  27. namespace Pica {
  28. namespace CommandProcessor {
  29. static int float_regs_counter = 0;
  30. static u32 uniform_write_buffer[4];
  31. static int default_attr_counter = 0;
  32. static u32 default_attr_write_buffer[3];
  33. // Expand a 4-bit mask to 4-byte mask, e.g. 0b0101 -> 0x00FF00FF
  34. static const u32 expand_bits_to_bytes[] = {
  35. 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff, 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
  36. 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff,
  37. };
  38. MICROPROFILE_DEFINE(GPU_Drawing, "GPU", "Drawing", MP_RGB(50, 50, 240));
  39. static void WritePicaReg(u32 id, u32 value, u32 mask) {
  40. auto& regs = g_state.regs;
  41. if (id >= regs.NumIds())
  42. return;
  43. // TODO: Figure out how register masking acts on e.g. vs.uniform_setup.set_value
  44. u32 old_value = regs[id];
  45. const u32 write_mask = expand_bits_to_bytes[mask];
  46. regs[id] = (old_value & ~write_mask) | (value & write_mask);
  47. DebugUtils::OnPicaRegWrite({(u16)id, (u16)mask, regs[id]});
  48. if (g_debug_context)
  49. g_debug_context->OnEvent(DebugContext::Event::PicaCommandLoaded,
  50. reinterpret_cast<void*>(&id));
  51. switch (id) {
  52. // Trigger IRQ
  53. case PICA_REG_INDEX(trigger_irq):
  54. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::P3D);
  55. break;
  56. case PICA_REG_INDEX_WORKAROUND(triangle_topology, 0x25E):
  57. g_state.primitive_assembler.Reconfigure(regs.triangle_topology);
  58. break;
  59. case PICA_REG_INDEX_WORKAROUND(restart_primitive, 0x25F):
  60. g_state.primitive_assembler.Reset();
  61. break;
  62. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.index, 0x232):
  63. g_state.immediate.current_attribute = 0;
  64. default_attr_counter = 0;
  65. break;
  66. // Load default vertex input attributes
  67. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[0], 0x233):
  68. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[1], 0x234):
  69. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[2], 0x235): {
  70. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  71. // it directly write the values?
  72. default_attr_write_buffer[default_attr_counter++] = value;
  73. // Default attributes are written in a packed format such that four float24 values are
  74. // encoded in
  75. // three 32-bit numbers. We write to internal memory once a full such vector is
  76. // written.
  77. if (default_attr_counter >= 3) {
  78. default_attr_counter = 0;
  79. auto& setup = regs.vs_default_attributes_setup;
  80. if (setup.index >= 16) {
  81. LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
  82. break;
  83. }
  84. Math::Vec4<float24> attribute;
  85. // NOTE: The destination component order indeed is "backwards"
  86. attribute.w = float24::FromRaw(default_attr_write_buffer[0] >> 8);
  87. attribute.z = float24::FromRaw(((default_attr_write_buffer[0] & 0xFF) << 16) |
  88. ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
  89. attribute.y = float24::FromRaw(((default_attr_write_buffer[1] & 0xFFFF) << 8) |
  90. ((default_attr_write_buffer[2] >> 24) & 0xFF));
  91. attribute.x = float24::FromRaw(default_attr_write_buffer[2] & 0xFFFFFF);
  92. LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
  93. attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
  94. attribute.w.ToFloat32());
  95. // TODO: Verify that this actually modifies the register!
  96. if (setup.index < 15) {
  97. g_state.vs_default_attributes[setup.index] = attribute;
  98. setup.index++;
  99. } else {
  100. // Put each attribute into an immediate input buffer.
  101. // When all specified immediate attributes are present, the Vertex Shader is invoked
  102. // and everything is
  103. // sent to the primitive assembler.
  104. auto& immediate_input = g_state.immediate.input_vertex;
  105. auto& immediate_attribute_id = g_state.immediate.current_attribute;
  106. immediate_input.attr[immediate_attribute_id++] = attribute;
  107. if (immediate_attribute_id >= regs.vs.num_input_attributes + 1) {
  108. immediate_attribute_id = 0;
  109. Shader::UnitState<false> shader_unit;
  110. g_state.vs.Setup();
  111. // Send to vertex shader
  112. if (g_debug_context)
  113. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  114. static_cast<void*>(&immediate_input));
  115. g_state.vs.Run(shader_unit, immediate_input, regs.vs.num_input_attributes + 1);
  116. Shader::OutputVertex output_vertex =
  117. shader_unit.output_registers.ToVertex(regs.vs);
  118. // Send to renderer
  119. using Pica::Shader::OutputVertex;
  120. auto AddTriangle = [](const OutputVertex& v0, const OutputVertex& v1,
  121. const OutputVertex& v2) {
  122. VideoCore::g_renderer->Rasterizer()->AddTriangle(v0, v1, v2);
  123. };
  124. g_state.primitive_assembler.SubmitVertex(output_vertex, AddTriangle);
  125. }
  126. }
  127. }
  128. break;
  129. }
  130. case PICA_REG_INDEX(gpu_mode):
  131. if (regs.gpu_mode == Regs::GPUMode::Configuring) {
  132. // Draw immediate mode triangles when GPU Mode is set to GPUMode::Configuring
  133. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  134. if (g_debug_context) {
  135. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  136. }
  137. }
  138. break;
  139. case PICA_REG_INDEX_WORKAROUND(command_buffer.trigger[0], 0x23c):
  140. case PICA_REG_INDEX_WORKAROUND(command_buffer.trigger[1], 0x23d): {
  141. unsigned index = static_cast<unsigned>(id - PICA_REG_INDEX(command_buffer.trigger[0]));
  142. u32* head_ptr =
  143. (u32*)Memory::GetPhysicalPointer(regs.command_buffer.GetPhysicalAddress(index));
  144. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = head_ptr;
  145. g_state.cmd_list.length = regs.command_buffer.GetSize(index) / sizeof(u32);
  146. break;
  147. }
  148. // It seems like these trigger vertex rendering
  149. case PICA_REG_INDEX(trigger_draw):
  150. case PICA_REG_INDEX(trigger_draw_indexed): {
  151. MICROPROFILE_SCOPE(GPU_Drawing);
  152. #if PICA_LOG_TEV
  153. DebugUtils::DumpTevStageConfig(regs.GetTevStages());
  154. #endif
  155. if (g_debug_context)
  156. g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  157. // Processes information about internal vertex attributes to figure out how a vertex is
  158. // loaded.
  159. // Later, these can be compiled and cached.
  160. const u32 base_address = regs.vertex_attributes.GetPhysicalBaseAddress();
  161. VertexLoader loader(regs);
  162. // Load vertices
  163. bool is_indexed = (id == PICA_REG_INDEX(trigger_draw_indexed));
  164. const auto& index_info = regs.index_array;
  165. const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
  166. const u16* index_address_16 = reinterpret_cast<const u16*>(index_address_8);
  167. bool index_u16 = index_info.format != 0;
  168. PrimitiveAssembler<Shader::OutputVertex>& primitive_assembler = g_state.primitive_assembler;
  169. if (g_debug_context && g_debug_context->recorder) {
  170. for (int i = 0; i < 3; ++i) {
  171. const auto texture = regs.GetTextures()[i];
  172. if (!texture.enabled)
  173. continue;
  174. u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
  175. g_debug_context->recorder->MemoryAccessed(
  176. texture_data, Pica::Regs::NibblesPerPixel(texture.format) *
  177. texture.config.width / 2 * texture.config.height,
  178. texture.config.GetPhysicalAddress());
  179. }
  180. }
  181. DebugUtils::MemoryAccessTracker memory_accesses;
  182. // Simple circular-replacement vertex cache
  183. // The size has been tuned for optimal balance between hit-rate and the cost of lookup
  184. const size_t VERTEX_CACHE_SIZE = 32;
  185. std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
  186. std::array<Shader::OutputVertex, VERTEX_CACHE_SIZE> vertex_cache;
  187. Shader::OutputVertex output_vertex;
  188. unsigned int vertex_cache_pos = 0;
  189. vertex_cache_ids.fill(-1);
  190. Shader::UnitState<false> shader_unit;
  191. g_state.vs.Setup();
  192. for (unsigned int index = 0; index < regs.num_vertices; ++index) {
  193. // Indexed rendering doesn't use the start offset
  194. unsigned int vertex =
  195. is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index])
  196. : (index + regs.vertex_offset);
  197. // -1 is a common special value used for primitive restart. Since it's unknown if
  198. // the PICA supports it, and it would mess up the caching, guard against it here.
  199. ASSERT(vertex != -1);
  200. bool vertex_cache_hit = false;
  201. Shader::OutputRegisters output_registers;
  202. if (is_indexed) {
  203. if (g_debug_context && Pica::g_debug_context->recorder) {
  204. int size = index_u16 ? 2 : 1;
  205. memory_accesses.AddAccess(base_address + index_info.offset + size * index,
  206. size);
  207. }
  208. for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
  209. if (vertex == vertex_cache_ids[i]) {
  210. output_vertex = vertex_cache[i];
  211. vertex_cache_hit = true;
  212. break;
  213. }
  214. }
  215. }
  216. if (!vertex_cache_hit) {
  217. // Initialize data for the current vertex
  218. Shader::InputVertex input;
  219. loader.LoadVertex(base_address, index, vertex, input, memory_accesses);
  220. // Send to vertex shader
  221. if (g_debug_context)
  222. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  223. (void*)&input);
  224. g_state.vs.Run(shader_unit, input, loader.GetNumTotalAttributes());
  225. output_registers = shader_unit.output_registers;
  226. // Retrieve vertex from register data
  227. output_vertex = output_registers.ToVertex(regs.vs);
  228. if (is_indexed) {
  229. vertex_cache[vertex_cache_pos] = output_vertex;
  230. vertex_cache_ids[vertex_cache_pos] = vertex;
  231. vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
  232. }
  233. }
  234. // Send to renderer
  235. using Pica::Shader::OutputVertex;
  236. auto AddTriangle = [](const OutputVertex& v0, const OutputVertex& v1,
  237. const OutputVertex& v2) {
  238. VideoCore::g_renderer->Rasterizer()->AddTriangle(v0, v1, v2);
  239. };
  240. primitive_assembler.SubmitVertex(output_vertex, AddTriangle);
  241. }
  242. for (auto& range : memory_accesses.ranges) {
  243. g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
  244. range.second, range.first);
  245. }
  246. break;
  247. }
  248. case PICA_REG_INDEX(vs.bool_uniforms):
  249. for (unsigned i = 0; i < 16; ++i)
  250. g_state.vs.uniforms.b[i] = (regs.vs.bool_uniforms.Value() & (1 << i)) != 0;
  251. break;
  252. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1):
  253. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[1], 0x2b2):
  254. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[2], 0x2b3):
  255. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[3], 0x2b4): {
  256. int index = (id - PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1));
  257. auto values = regs.vs.int_uniforms[index];
  258. g_state.vs.uniforms.i[index] = Math::Vec4<u8>(values.x, values.y, values.z, values.w);
  259. LOG_TRACE(HW_GPU, "Set integer uniform %d to %02x %02x %02x %02x", index, values.x.Value(),
  260. values.y.Value(), values.z.Value(), values.w.Value());
  261. break;
  262. }
  263. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[0], 0x2c1):
  264. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[1], 0x2c2):
  265. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[2], 0x2c3):
  266. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[3], 0x2c4):
  267. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[4], 0x2c5):
  268. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[5], 0x2c6):
  269. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[6], 0x2c7):
  270. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[7], 0x2c8): {
  271. auto& uniform_setup = regs.vs.uniform_setup;
  272. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  273. // it directly write the values?
  274. uniform_write_buffer[float_regs_counter++] = value;
  275. // Uniforms are written in a packed format such that four float24 values are encoded in
  276. // three 32-bit numbers. We write to internal memory once a full such vector is
  277. // written.
  278. if ((float_regs_counter >= 4 && uniform_setup.IsFloat32()) ||
  279. (float_regs_counter >= 3 && !uniform_setup.IsFloat32())) {
  280. float_regs_counter = 0;
  281. auto& uniform = g_state.vs.uniforms.f[uniform_setup.index];
  282. if (uniform_setup.index > 95) {
  283. LOG_ERROR(HW_GPU, "Invalid VS uniform index %d", (int)uniform_setup.index);
  284. break;
  285. }
  286. // NOTE: The destination component order indeed is "backwards"
  287. if (uniform_setup.IsFloat32()) {
  288. for (auto i : {0, 1, 2, 3})
  289. uniform[3 - i] = float24::FromFloat32(*(float*)(&uniform_write_buffer[i]));
  290. } else {
  291. // TODO: Untested
  292. uniform.w = float24::FromRaw(uniform_write_buffer[0] >> 8);
  293. uniform.z = float24::FromRaw(((uniform_write_buffer[0] & 0xFF) << 16) |
  294. ((uniform_write_buffer[1] >> 16) & 0xFFFF));
  295. uniform.y = float24::FromRaw(((uniform_write_buffer[1] & 0xFFFF) << 8) |
  296. ((uniform_write_buffer[2] >> 24) & 0xFF));
  297. uniform.x = float24::FromRaw(uniform_write_buffer[2] & 0xFFFFFF);
  298. }
  299. LOG_TRACE(HW_GPU, "Set uniform %x to (%f %f %f %f)", (int)uniform_setup.index,
  300. uniform.x.ToFloat32(), uniform.y.ToFloat32(), uniform.z.ToFloat32(),
  301. uniform.w.ToFloat32());
  302. // TODO: Verify that this actually modifies the register!
  303. uniform_setup.index.Assign(uniform_setup.index + 1);
  304. }
  305. break;
  306. }
  307. // Load shader program code
  308. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[0], 0x2cc):
  309. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[1], 0x2cd):
  310. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[2], 0x2ce):
  311. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[3], 0x2cf):
  312. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[4], 0x2d0):
  313. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[5], 0x2d1):
  314. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[6], 0x2d2):
  315. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[7], 0x2d3): {
  316. g_state.vs.program_code[regs.vs.program.offset] = value;
  317. regs.vs.program.offset++;
  318. break;
  319. }
  320. // Load swizzle pattern data
  321. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[0], 0x2d6):
  322. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[1], 0x2d7):
  323. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[2], 0x2d8):
  324. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[3], 0x2d9):
  325. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[4], 0x2da):
  326. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[5], 0x2db):
  327. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[6], 0x2dc):
  328. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[7], 0x2dd): {
  329. g_state.vs.swizzle_data[regs.vs.swizzle_patterns.offset] = value;
  330. regs.vs.swizzle_patterns.offset++;
  331. break;
  332. }
  333. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  334. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  335. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  336. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  337. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  338. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  339. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  340. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf): {
  341. auto& lut_config = regs.lighting.lut_config;
  342. ASSERT_MSG(lut_config.index < 256, "lut_config.index exceeded maximum value of 255!");
  343. g_state.lighting.luts[lut_config.type][lut_config.index].raw = value;
  344. lut_config.index.Assign(lut_config.index + 1);
  345. break;
  346. }
  347. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[0], 0xe8):
  348. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[1], 0xe9):
  349. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[2], 0xea):
  350. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[3], 0xeb):
  351. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[4], 0xec):
  352. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[5], 0xed):
  353. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[6], 0xee):
  354. case PICA_REG_INDEX_WORKAROUND(fog_lut_data[7], 0xef): {
  355. g_state.fog.lut[regs.fog_lut_offset % 128].raw = value;
  356. regs.fog_lut_offset.Assign(regs.fog_lut_offset + 1);
  357. break;
  358. }
  359. default:
  360. break;
  361. }
  362. VideoCore::g_renderer->Rasterizer()->NotifyPicaRegisterChanged(id);
  363. if (g_debug_context)
  364. g_debug_context->OnEvent(DebugContext::Event::PicaCommandProcessed,
  365. reinterpret_cast<void*>(&id));
  366. }
  367. void ProcessCommandList(const u32* list, u32 size) {
  368. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = list;
  369. g_state.cmd_list.length = size / sizeof(u32);
  370. while (g_state.cmd_list.current_ptr < g_state.cmd_list.head_ptr + g_state.cmd_list.length) {
  371. // Align read pointer to 8 bytes
  372. if ((g_state.cmd_list.head_ptr - g_state.cmd_list.current_ptr) % 2 != 0)
  373. ++g_state.cmd_list.current_ptr;
  374. u32 value = *g_state.cmd_list.current_ptr++;
  375. const CommandHeader header = {*g_state.cmd_list.current_ptr++};
  376. WritePicaReg(header.cmd_id, value, header.parameter_mask);
  377. for (unsigned i = 0; i < header.extra_data_length; ++i) {
  378. u32 cmd = header.cmd_id + (header.group_commands ? i + 1 : 0);
  379. WritePicaReg(cmd, *g_state.cmd_list.current_ptr++, header.parameter_mask);
  380. }
  381. }
  382. }
  383. } // namespace
  384. } // namespace