command_processor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_state.h"
  19. #include "video_core/pica_types.h"
  20. #include "video_core/primitive_assembly.h"
  21. #include "video_core/rasterizer_interface.h"
  22. #include "video_core/regs.h"
  23. #include "video_core/regs_pipeline.h"
  24. #include "video_core/regs_texturing.h"
  25. #include "video_core/renderer_base.h"
  26. #include "video_core/shader/shader.h"
  27. #include "video_core/vertex_loader.h"
  28. #include "video_core/video_core.h"
  29. namespace Pica {
  30. namespace CommandProcessor {
  31. static int vs_float_regs_counter = 0;
  32. static u32 vs_uniform_write_buffer[4];
  33. static int gs_float_regs_counter = 0;
  34. static u32 gs_uniform_write_buffer[4];
  35. static int default_attr_counter = 0;
  36. static u32 default_attr_write_buffer[3];
  37. // Expand a 4-bit mask to 4-byte mask, e.g. 0b0101 -> 0x00FF00FF
  38. static const u32 expand_bits_to_bytes[] = {
  39. 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff, 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
  40. 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff,
  41. };
  42. MICROPROFILE_DEFINE(GPU_Drawing, "GPU", "Drawing", MP_RGB(50, 50, 240));
  43. static const char* GetShaderSetupTypeName(Shader::ShaderSetup& setup) {
  44. if (&setup == &g_state.vs) {
  45. return "vertex shader";
  46. }
  47. if (&setup == &g_state.gs) {
  48. return "geometry shader";
  49. }
  50. return "unknown shader";
  51. }
  52. static void WriteUniformBoolReg(Shader::ShaderSetup& setup, u32 value) {
  53. for (unsigned i = 0; i < setup.uniforms.b.size(); ++i)
  54. setup.uniforms.b[i] = (value & (1 << i)) != 0;
  55. }
  56. static void WriteUniformIntReg(Shader::ShaderSetup& setup, unsigned index,
  57. const Math::Vec4<u8>& values) {
  58. ASSERT(index < setup.uniforms.i.size());
  59. setup.uniforms.i[index] = values;
  60. LOG_TRACE(HW_GPU, "Set %s integer uniform %d to %02x %02x %02x %02x",
  61. GetShaderSetupTypeName(setup), index, values.x, values.y, values.z, values.w);
  62. }
  63. static void WriteUniformFloatReg(ShaderRegs& config, Shader::ShaderSetup& setup,
  64. int& float_regs_counter, u32 uniform_write_buffer[4], u32 value) {
  65. auto& uniform_setup = config.uniform_setup;
  66. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  67. // it directly write the values?
  68. uniform_write_buffer[float_regs_counter++] = value;
  69. // Uniforms are written in a packed format such that four float24 values are encoded in
  70. // three 32-bit numbers. We write to internal memory once a full such vector is
  71. // written.
  72. if ((float_regs_counter >= 4 && uniform_setup.IsFloat32()) ||
  73. (float_regs_counter >= 3 && !uniform_setup.IsFloat32())) {
  74. float_regs_counter = 0;
  75. auto& uniform = setup.uniforms.f[uniform_setup.index];
  76. if (uniform_setup.index >= 96) {
  77. LOG_ERROR(HW_GPU, "Invalid %s float uniform index %d", GetShaderSetupTypeName(setup),
  78. (int)uniform_setup.index);
  79. } else {
  80. // NOTE: The destination component order indeed is "backwards"
  81. if (uniform_setup.IsFloat32()) {
  82. for (auto i : {0, 1, 2, 3})
  83. uniform[3 - i] = float24::FromFloat32(*(float*)(&uniform_write_buffer[i]));
  84. } else {
  85. // TODO: Untested
  86. uniform.w = float24::FromRaw(uniform_write_buffer[0] >> 8);
  87. uniform.z = float24::FromRaw(((uniform_write_buffer[0] & 0xFF) << 16) |
  88. ((uniform_write_buffer[1] >> 16) & 0xFFFF));
  89. uniform.y = float24::FromRaw(((uniform_write_buffer[1] & 0xFFFF) << 8) |
  90. ((uniform_write_buffer[2] >> 24) & 0xFF));
  91. uniform.x = float24::FromRaw(uniform_write_buffer[2] & 0xFFFFFF);
  92. }
  93. LOG_TRACE(HW_GPU, "Set %s float uniform %x to (%f %f %f %f)",
  94. GetShaderSetupTypeName(setup), (int)uniform_setup.index,
  95. uniform.x.ToFloat32(), uniform.y.ToFloat32(), uniform.z.ToFloat32(),
  96. uniform.w.ToFloat32());
  97. // TODO: Verify that this actually modifies the register!
  98. uniform_setup.index.Assign(uniform_setup.index + 1);
  99. }
  100. }
  101. }
  102. static void WriteProgramCode(ShaderRegs& config, Shader::ShaderSetup& setup,
  103. unsigned max_program_code_length, u32 value) {
  104. if (config.program.offset >= max_program_code_length) {
  105. LOG_ERROR(HW_GPU, "Invalid %s program offset %d", GetShaderSetupTypeName(setup),
  106. (int)config.program.offset);
  107. } else {
  108. setup.program_code[config.program.offset] = value;
  109. config.program.offset++;
  110. }
  111. }
  112. static void WriteSwizzlePatterns(ShaderRegs& config, Shader::ShaderSetup& setup, u32 value) {
  113. if (config.swizzle_patterns.offset >= setup.swizzle_data.size()) {
  114. LOG_ERROR(HW_GPU, "Invalid %s swizzle pattern offset %d", GetShaderSetupTypeName(setup),
  115. (int)config.swizzle_patterns.offset);
  116. } else {
  117. setup.swizzle_data[config.swizzle_patterns.offset] = value;
  118. config.swizzle_patterns.offset++;
  119. }
  120. }
  121. static void WritePicaReg(u32 id, u32 value, u32 mask) {
  122. auto& regs = g_state.regs;
  123. if (id >= Regs::NUM_REGS) {
  124. LOG_ERROR(HW_GPU,
  125. "Commandlist tried to write to invalid register 0x%03X (value: %08X, mask: %X)",
  126. id, value, mask);
  127. return;
  128. }
  129. // TODO: Figure out how register masking acts on e.g. vs.uniform_setup.set_value
  130. u32 old_value = regs.reg_array[id];
  131. const u32 write_mask = expand_bits_to_bytes[mask];
  132. regs.reg_array[id] = (old_value & ~write_mask) | (value & write_mask);
  133. // Double check for is_pica_tracing to avoid call overhead
  134. if (DebugUtils::IsPicaTracing()) {
  135. DebugUtils::OnPicaRegWrite({(u16)id, (u16)mask, regs.reg_array[id]});
  136. }
  137. if (g_debug_context)
  138. g_debug_context->OnEvent(DebugContext::Event::PicaCommandLoaded,
  139. reinterpret_cast<void*>(&id));
  140. switch (id) {
  141. // Trigger IRQ
  142. case PICA_REG_INDEX(trigger_irq):
  143. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::P3D);
  144. break;
  145. case PICA_REG_INDEX(pipeline.triangle_topology):
  146. g_state.primitive_assembler.Reconfigure(regs.pipeline.triangle_topology);
  147. break;
  148. case PICA_REG_INDEX(pipeline.restart_primitive):
  149. g_state.primitive_assembler.Reset();
  150. break;
  151. case PICA_REG_INDEX(pipeline.vs_default_attributes_setup.index):
  152. g_state.immediate.current_attribute = 0;
  153. default_attr_counter = 0;
  154. break;
  155. // Load default vertex input attributes
  156. case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[0], 0x233):
  157. case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[1], 0x234):
  158. case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[2], 0x235): {
  159. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  160. // it directly write the values?
  161. default_attr_write_buffer[default_attr_counter++] = value;
  162. // Default attributes are written in a packed format such that four float24 values are
  163. // encoded in
  164. // three 32-bit numbers. We write to internal memory once a full such vector is
  165. // written.
  166. if (default_attr_counter >= 3) {
  167. default_attr_counter = 0;
  168. auto& setup = regs.pipeline.vs_default_attributes_setup;
  169. if (setup.index >= 16) {
  170. LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
  171. break;
  172. }
  173. Math::Vec4<float24> attribute;
  174. // NOTE: The destination component order indeed is "backwards"
  175. attribute.w = float24::FromRaw(default_attr_write_buffer[0] >> 8);
  176. attribute.z = float24::FromRaw(((default_attr_write_buffer[0] & 0xFF) << 16) |
  177. ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
  178. attribute.y = float24::FromRaw(((default_attr_write_buffer[1] & 0xFFFF) << 8) |
  179. ((default_attr_write_buffer[2] >> 24) & 0xFF));
  180. attribute.x = float24::FromRaw(default_attr_write_buffer[2] & 0xFFFFFF);
  181. LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
  182. attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
  183. attribute.w.ToFloat32());
  184. // TODO: Verify that this actually modifies the register!
  185. if (setup.index < 15) {
  186. g_state.input_default_attributes.attr[setup.index] = attribute;
  187. setup.index++;
  188. } else {
  189. // Put each attribute into an immediate input buffer. When all specified immediate
  190. // attributes are present, the Vertex Shader is invoked and everything is sent to
  191. // the primitive assembler.
  192. auto& immediate_input = g_state.immediate.input_vertex;
  193. auto& immediate_attribute_id = g_state.immediate.current_attribute;
  194. immediate_input.attr[immediate_attribute_id] = attribute;
  195. if (immediate_attribute_id < regs.pipeline.max_input_attrib_index) {
  196. immediate_attribute_id += 1;
  197. } else {
  198. MICROPROFILE_SCOPE(GPU_Drawing);
  199. immediate_attribute_id = 0;
  200. auto* shader_engine = Shader::GetEngine();
  201. shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
  202. // Send to vertex shader
  203. if (g_debug_context)
  204. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  205. static_cast<void*>(&immediate_input));
  206. Shader::UnitState shader_unit;
  207. Shader::AttributeBuffer output{};
  208. shader_unit.LoadInput(regs.vs, immediate_input);
  209. shader_engine->Run(g_state.vs, shader_unit);
  210. shader_unit.WriteOutput(regs.vs, output);
  211. // Send to renderer
  212. using Pica::Shader::OutputVertex;
  213. auto AddTriangle = [](const OutputVertex& v0, const OutputVertex& v1,
  214. const OutputVertex& v2) {
  215. VideoCore::g_renderer->Rasterizer()->AddTriangle(v0, v1, v2);
  216. };
  217. g_state.primitive_assembler.SubmitVertex(
  218. Shader::OutputVertex::FromAttributeBuffer(regs.rasterizer, output),
  219. AddTriangle);
  220. }
  221. }
  222. }
  223. break;
  224. }
  225. case PICA_REG_INDEX(pipeline.gpu_mode):
  226. if (regs.pipeline.gpu_mode == PipelineRegs::GPUMode::Configuring) {
  227. MICROPROFILE_SCOPE(GPU_Drawing);
  228. // Draw immediate mode triangles when GPU Mode is set to GPUMode::Configuring
  229. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  230. if (g_debug_context) {
  231. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  232. }
  233. }
  234. break;
  235. case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[0], 0x23c):
  236. case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[1], 0x23d): {
  237. unsigned index =
  238. static_cast<unsigned>(id - PICA_REG_INDEX(pipeline.command_buffer.trigger[0]));
  239. u32* head_ptr = (u32*)Memory::GetPhysicalPointer(
  240. regs.pipeline.command_buffer.GetPhysicalAddress(index));
  241. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = head_ptr;
  242. g_state.cmd_list.length = regs.pipeline.command_buffer.GetSize(index) / sizeof(u32);
  243. break;
  244. }
  245. // It seems like these trigger vertex rendering
  246. case PICA_REG_INDEX(pipeline.trigger_draw):
  247. case PICA_REG_INDEX(pipeline.trigger_draw_indexed): {
  248. MICROPROFILE_SCOPE(GPU_Drawing);
  249. #if PICA_LOG_TEV
  250. DebugUtils::DumpTevStageConfig(regs.GetTevStages());
  251. #endif
  252. if (g_debug_context)
  253. g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  254. // Processes information about internal vertex attributes to figure out how a vertex is
  255. // loaded.
  256. // Later, these can be compiled and cached.
  257. const u32 base_address = regs.pipeline.vertex_attributes.GetPhysicalBaseAddress();
  258. VertexLoader loader(regs.pipeline);
  259. // Load vertices
  260. bool is_indexed = (id == PICA_REG_INDEX(pipeline.trigger_draw_indexed));
  261. const auto& index_info = regs.pipeline.index_array;
  262. const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
  263. const u16* index_address_16 = reinterpret_cast<const u16*>(index_address_8);
  264. bool index_u16 = index_info.format != 0;
  265. PrimitiveAssembler<Shader::OutputVertex>& primitive_assembler = g_state.primitive_assembler;
  266. if (g_debug_context && g_debug_context->recorder) {
  267. for (int i = 0; i < 3; ++i) {
  268. const auto texture = regs.texturing.GetTextures()[i];
  269. if (!texture.enabled)
  270. continue;
  271. u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
  272. g_debug_context->recorder->MemoryAccessed(
  273. texture_data, Pica::TexturingRegs::NibblesPerPixel(texture.format) *
  274. texture.config.width / 2 * texture.config.height,
  275. texture.config.GetPhysicalAddress());
  276. }
  277. }
  278. DebugUtils::MemoryAccessTracker memory_accesses;
  279. // Simple circular-replacement vertex cache
  280. // The size has been tuned for optimal balance between hit-rate and the cost of lookup
  281. const size_t VERTEX_CACHE_SIZE = 32;
  282. std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
  283. std::array<Shader::OutputVertex, VERTEX_CACHE_SIZE> vertex_cache;
  284. Shader::OutputVertex output_vertex;
  285. unsigned int vertex_cache_pos = 0;
  286. vertex_cache_ids.fill(-1);
  287. auto* shader_engine = Shader::GetEngine();
  288. Shader::UnitState shader_unit;
  289. shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
  290. for (unsigned int index = 0; index < regs.pipeline.num_vertices; ++index) {
  291. // Indexed rendering doesn't use the start offset
  292. unsigned int vertex =
  293. is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index])
  294. : (index + regs.pipeline.vertex_offset);
  295. // -1 is a common special value used for primitive restart. Since it's unknown if
  296. // the PICA supports it, and it would mess up the caching, guard against it here.
  297. ASSERT(vertex != -1);
  298. bool vertex_cache_hit = false;
  299. if (is_indexed) {
  300. if (g_debug_context && Pica::g_debug_context->recorder) {
  301. int size = index_u16 ? 2 : 1;
  302. memory_accesses.AddAccess(base_address + index_info.offset + size * index,
  303. size);
  304. }
  305. for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
  306. if (vertex == vertex_cache_ids[i]) {
  307. output_vertex = vertex_cache[i];
  308. vertex_cache_hit = true;
  309. break;
  310. }
  311. }
  312. }
  313. if (!vertex_cache_hit) {
  314. // Initialize data for the current vertex
  315. Shader::AttributeBuffer input, output{};
  316. loader.LoadVertex(base_address, index, vertex, input, memory_accesses);
  317. // Send to vertex shader
  318. if (g_debug_context)
  319. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  320. (void*)&input);
  321. shader_unit.LoadInput(regs.vs, input);
  322. shader_engine->Run(g_state.vs, shader_unit);
  323. shader_unit.WriteOutput(regs.vs, output);
  324. // Retrieve vertex from register data
  325. output_vertex = Shader::OutputVertex::FromAttributeBuffer(regs.rasterizer, output);
  326. if (is_indexed) {
  327. vertex_cache[vertex_cache_pos] = output_vertex;
  328. vertex_cache_ids[vertex_cache_pos] = vertex;
  329. vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
  330. }
  331. }
  332. // Send to renderer
  333. using Pica::Shader::OutputVertex;
  334. auto AddTriangle = [](const OutputVertex& v0, const OutputVertex& v1,
  335. const OutputVertex& v2) {
  336. VideoCore::g_renderer->Rasterizer()->AddTriangle(v0, v1, v2);
  337. };
  338. primitive_assembler.SubmitVertex(output_vertex, AddTriangle);
  339. }
  340. for (auto& range : memory_accesses.ranges) {
  341. g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
  342. range.second, range.first);
  343. }
  344. break;
  345. }
  346. case PICA_REG_INDEX(gs.bool_uniforms):
  347. WriteUniformBoolReg(g_state.gs, g_state.regs.gs.bool_uniforms.Value());
  348. break;
  349. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[0], 0x281):
  350. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[1], 0x282):
  351. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[2], 0x283):
  352. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[3], 0x284): {
  353. unsigned index = (id - PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[0], 0x281));
  354. auto values = regs.gs.int_uniforms[index];
  355. WriteUniformIntReg(g_state.gs, index,
  356. Math::Vec4<u8>(values.x, values.y, values.z, values.w));
  357. break;
  358. }
  359. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[0], 0x291):
  360. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[1], 0x292):
  361. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[2], 0x293):
  362. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[3], 0x294):
  363. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[4], 0x295):
  364. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[5], 0x296):
  365. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[6], 0x297):
  366. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[7], 0x298): {
  367. WriteUniformFloatReg(g_state.regs.gs, g_state.gs, gs_float_regs_counter,
  368. gs_uniform_write_buffer, value);
  369. break;
  370. }
  371. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[0], 0x29c):
  372. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[1], 0x29d):
  373. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[2], 0x29e):
  374. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[3], 0x29f):
  375. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[4], 0x2a0):
  376. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[5], 0x2a1):
  377. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[6], 0x2a2):
  378. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[7], 0x2a3): {
  379. WriteProgramCode(g_state.regs.gs, g_state.gs, 4096, value);
  380. break;
  381. }
  382. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[0], 0x2a6):
  383. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[1], 0x2a7):
  384. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[2], 0x2a8):
  385. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[3], 0x2a9):
  386. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[4], 0x2aa):
  387. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[5], 0x2ab):
  388. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[6], 0x2ac):
  389. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[7], 0x2ad): {
  390. WriteSwizzlePatterns(g_state.regs.gs, g_state.gs, value);
  391. break;
  392. }
  393. case PICA_REG_INDEX(vs.bool_uniforms):
  394. WriteUniformBoolReg(g_state.vs, g_state.regs.vs.bool_uniforms.Value());
  395. break;
  396. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1):
  397. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[1], 0x2b2):
  398. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[2], 0x2b3):
  399. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[3], 0x2b4): {
  400. unsigned index = (id - PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1));
  401. auto values = regs.vs.int_uniforms[index];
  402. WriteUniformIntReg(g_state.vs, index,
  403. Math::Vec4<u8>(values.x, values.y, values.z, values.w));
  404. break;
  405. }
  406. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[0], 0x2c1):
  407. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[1], 0x2c2):
  408. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[2], 0x2c3):
  409. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[3], 0x2c4):
  410. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[4], 0x2c5):
  411. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[5], 0x2c6):
  412. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[6], 0x2c7):
  413. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[7], 0x2c8): {
  414. WriteUniformFloatReg(g_state.regs.vs, g_state.vs, vs_float_regs_counter,
  415. vs_uniform_write_buffer, value);
  416. break;
  417. }
  418. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[0], 0x2cc):
  419. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[1], 0x2cd):
  420. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[2], 0x2ce):
  421. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[3], 0x2cf):
  422. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[4], 0x2d0):
  423. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[5], 0x2d1):
  424. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[6], 0x2d2):
  425. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[7], 0x2d3): {
  426. WriteProgramCode(g_state.regs.vs, g_state.vs, 512, value);
  427. break;
  428. }
  429. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[0], 0x2d6):
  430. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[1], 0x2d7):
  431. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[2], 0x2d8):
  432. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[3], 0x2d9):
  433. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[4], 0x2da):
  434. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[5], 0x2db):
  435. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[6], 0x2dc):
  436. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[7], 0x2dd): {
  437. WriteSwizzlePatterns(g_state.regs.vs, g_state.vs, value);
  438. break;
  439. }
  440. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  441. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  442. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  443. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  444. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  445. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  446. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  447. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf): {
  448. auto& lut_config = regs.lighting.lut_config;
  449. ASSERT_MSG(lut_config.index < 256, "lut_config.index exceeded maximum value of 255!");
  450. g_state.lighting.luts[lut_config.type][lut_config.index].raw = value;
  451. lut_config.index.Assign(lut_config.index + 1);
  452. break;
  453. }
  454. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[0], 0xe8):
  455. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[1], 0xe9):
  456. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[2], 0xea):
  457. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[3], 0xeb):
  458. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[4], 0xec):
  459. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[5], 0xed):
  460. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[6], 0xee):
  461. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[7], 0xef): {
  462. g_state.fog.lut[regs.texturing.fog_lut_offset % 128].raw = value;
  463. regs.texturing.fog_lut_offset.Assign(regs.texturing.fog_lut_offset + 1);
  464. break;
  465. }
  466. default:
  467. break;
  468. }
  469. VideoCore::g_renderer->Rasterizer()->NotifyPicaRegisterChanged(id);
  470. if (g_debug_context)
  471. g_debug_context->OnEvent(DebugContext::Event::PicaCommandProcessed,
  472. reinterpret_cast<void*>(&id));
  473. }
  474. void ProcessCommandList(const u32* list, u32 size) {
  475. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = list;
  476. g_state.cmd_list.length = size / sizeof(u32);
  477. while (g_state.cmd_list.current_ptr < g_state.cmd_list.head_ptr + g_state.cmd_list.length) {
  478. // Align read pointer to 8 bytes
  479. if ((g_state.cmd_list.head_ptr - g_state.cmd_list.current_ptr) % 2 != 0)
  480. ++g_state.cmd_list.current_ptr;
  481. u32 value = *g_state.cmd_list.current_ptr++;
  482. const CommandHeader header = {*g_state.cmd_list.current_ptr++};
  483. WritePicaReg(header.cmd_id, value, header.parameter_mask);
  484. for (unsigned i = 0; i < header.extra_data_length; ++i) {
  485. u32 cmd = header.cmd_id + (header.group_commands ? i + 1 : 0);
  486. WritePicaReg(cmd, *g_state.cmd_list.current_ptr++, header.parameter_mask);
  487. }
  488. }
  489. }
  490. } // namespace
  491. } // namespace