command_processor.cpp 20 KB

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