command_processor.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 WritePicaReg(u32 id, u32 value, u32 mask) {
  103. auto& regs = g_state.regs;
  104. if (id >= Regs::NUM_REGS) {
  105. LOG_ERROR(HW_GPU,
  106. "Commandlist tried to write to invalid register 0x%03X (value: %08X, mask: %X)",
  107. id, value, mask);
  108. return;
  109. }
  110. // TODO: Figure out how register masking acts on e.g. vs.uniform_setup.set_value
  111. u32 old_value = regs.reg_array[id];
  112. const u32 write_mask = expand_bits_to_bytes[mask];
  113. regs.reg_array[id] = (old_value & ~write_mask) | (value & write_mask);
  114. // Double check for is_pica_tracing to avoid call overhead
  115. if (DebugUtils::IsPicaTracing()) {
  116. DebugUtils::OnPicaRegWrite({(u16)id, (u16)mask, regs.reg_array[id]});
  117. }
  118. if (g_debug_context)
  119. g_debug_context->OnEvent(DebugContext::Event::PicaCommandLoaded,
  120. reinterpret_cast<void*>(&id));
  121. switch (id) {
  122. // Trigger IRQ
  123. case PICA_REG_INDEX(trigger_irq):
  124. Service::GSP::SignalInterrupt(Service::GSP::InterruptId::P3D);
  125. break;
  126. case PICA_REG_INDEX(pipeline.triangle_topology):
  127. g_state.primitive_assembler.Reconfigure(regs.pipeline.triangle_topology);
  128. break;
  129. case PICA_REG_INDEX(pipeline.restart_primitive):
  130. g_state.primitive_assembler.Reset();
  131. break;
  132. case PICA_REG_INDEX(pipeline.vs_default_attributes_setup.index):
  133. g_state.immediate.current_attribute = 0;
  134. g_state.immediate.reset_geometry_pipeline = true;
  135. default_attr_counter = 0;
  136. break;
  137. // Load default vertex input attributes
  138. case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[0], 0x233):
  139. case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[1], 0x234):
  140. case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[2], 0x235): {
  141. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  142. // it directly write the values?
  143. default_attr_write_buffer[default_attr_counter++] = value;
  144. // Default attributes are written in a packed format such that four float24 values are
  145. // encoded in
  146. // three 32-bit numbers. We write to internal memory once a full such vector is
  147. // written.
  148. if (default_attr_counter >= 3) {
  149. default_attr_counter = 0;
  150. auto& setup = regs.pipeline.vs_default_attributes_setup;
  151. if (setup.index >= 16) {
  152. LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
  153. break;
  154. }
  155. Math::Vec4<float24> attribute;
  156. // NOTE: The destination component order indeed is "backwards"
  157. attribute.w = float24::FromRaw(default_attr_write_buffer[0] >> 8);
  158. attribute.z = float24::FromRaw(((default_attr_write_buffer[0] & 0xFF) << 16) |
  159. ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
  160. attribute.y = float24::FromRaw(((default_attr_write_buffer[1] & 0xFFFF) << 8) |
  161. ((default_attr_write_buffer[2] >> 24) & 0xFF));
  162. attribute.x = float24::FromRaw(default_attr_write_buffer[2] & 0xFFFFFF);
  163. LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
  164. attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
  165. attribute.w.ToFloat32());
  166. // TODO: Verify that this actually modifies the register!
  167. if (setup.index < 15) {
  168. g_state.input_default_attributes.attr[setup.index] = attribute;
  169. setup.index++;
  170. } else {
  171. // Put each attribute into an immediate input buffer. When all specified immediate
  172. // attributes are present, the Vertex Shader is invoked and everything is sent to
  173. // the primitive assembler.
  174. auto& immediate_input = g_state.immediate.input_vertex;
  175. auto& immediate_attribute_id = g_state.immediate.current_attribute;
  176. immediate_input.attr[immediate_attribute_id] = attribute;
  177. if (immediate_attribute_id < regs.pipeline.max_input_attrib_index) {
  178. immediate_attribute_id += 1;
  179. } else {
  180. MICROPROFILE_SCOPE(GPU_Drawing);
  181. immediate_attribute_id = 0;
  182. auto* shader_engine = Shader::GetEngine();
  183. shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
  184. // Send to vertex shader
  185. if (g_debug_context)
  186. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  187. static_cast<void*>(&immediate_input));
  188. Shader::UnitState shader_unit;
  189. Shader::AttributeBuffer output{};
  190. shader_unit.LoadInput(regs.vs, immediate_input);
  191. shader_engine->Run(g_state.vs, shader_unit);
  192. shader_unit.WriteOutput(regs.vs, output);
  193. // Send to geometry pipeline
  194. if (g_state.immediate.reset_geometry_pipeline) {
  195. g_state.geometry_pipeline.Reconfigure();
  196. g_state.immediate.reset_geometry_pipeline = false;
  197. }
  198. ASSERT(!g_state.geometry_pipeline.NeedIndexInput());
  199. g_state.geometry_pipeline.Setup(shader_engine);
  200. g_state.geometry_pipeline.SubmitVertex(output);
  201. // TODO: If drawing after every immediate mode triangle kills performance,
  202. // change it to flush triangles whenever a drawing config register changes
  203. // See: https://github.com/citra-emu/citra/pull/2866#issuecomment-327011550
  204. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  205. if (g_debug_context) {
  206. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch,
  207. nullptr);
  208. }
  209. }
  210. }
  211. }
  212. break;
  213. }
  214. case PICA_REG_INDEX(pipeline.gpu_mode):
  215. // This register likely just enables vertex processing and doesn't need any special handling
  216. break;
  217. case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[0], 0x23c):
  218. case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[1], 0x23d): {
  219. unsigned index =
  220. static_cast<unsigned>(id - PICA_REG_INDEX(pipeline.command_buffer.trigger[0]));
  221. u32* head_ptr = (u32*)Memory::GetPhysicalPointer(
  222. regs.pipeline.command_buffer.GetPhysicalAddress(index));
  223. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = head_ptr;
  224. g_state.cmd_list.length = regs.pipeline.command_buffer.GetSize(index) / sizeof(u32);
  225. break;
  226. }
  227. // It seems like these trigger vertex rendering
  228. case PICA_REG_INDEX(pipeline.trigger_draw):
  229. case PICA_REG_INDEX(pipeline.trigger_draw_indexed): {
  230. MICROPROFILE_SCOPE(GPU_Drawing);
  231. #if PICA_LOG_TEV
  232. DebugUtils::DumpTevStageConfig(regs.GetTevStages());
  233. #endif
  234. if (g_debug_context)
  235. g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  236. // Processes information about internal vertex attributes to figure out how a vertex is
  237. // loaded.
  238. // Later, these can be compiled and cached.
  239. const u32 base_address = regs.pipeline.vertex_attributes.GetPhysicalBaseAddress();
  240. VertexLoader loader(regs.pipeline);
  241. // Load vertices
  242. bool is_indexed = (id == PICA_REG_INDEX(pipeline.trigger_draw_indexed));
  243. const auto& index_info = regs.pipeline.index_array;
  244. const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
  245. const u16* index_address_16 = reinterpret_cast<const u16*>(index_address_8);
  246. bool index_u16 = index_info.format != 0;
  247. PrimitiveAssembler<Shader::OutputVertex>& primitive_assembler = g_state.primitive_assembler;
  248. if (g_debug_context && g_debug_context->recorder) {
  249. for (int i = 0; i < 3; ++i) {
  250. const auto texture = regs.texturing.GetTextures()[i];
  251. if (!texture.enabled)
  252. continue;
  253. u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
  254. g_debug_context->recorder->MemoryAccessed(
  255. texture_data, Pica::TexturingRegs::NibblesPerPixel(texture.format) *
  256. texture.config.width / 2 * texture.config.height,
  257. texture.config.GetPhysicalAddress());
  258. }
  259. }
  260. DebugUtils::MemoryAccessTracker memory_accesses;
  261. // Simple circular-replacement vertex cache
  262. // The size has been tuned for optimal balance between hit-rate and the cost of lookup
  263. const size_t VERTEX_CACHE_SIZE = 32;
  264. std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
  265. std::array<Shader::AttributeBuffer, VERTEX_CACHE_SIZE> vertex_cache;
  266. Shader::AttributeBuffer vs_output;
  267. unsigned int vertex_cache_pos = 0;
  268. vertex_cache_ids.fill(-1);
  269. auto* shader_engine = Shader::GetEngine();
  270. Shader::UnitState shader_unit;
  271. shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
  272. g_state.geometry_pipeline.Reconfigure();
  273. g_state.geometry_pipeline.Setup(shader_engine);
  274. if (g_state.geometry_pipeline.NeedIndexInput())
  275. ASSERT(is_indexed);
  276. for (unsigned int index = 0; index < regs.pipeline.num_vertices; ++index) {
  277. // Indexed rendering doesn't use the start offset
  278. unsigned int vertex =
  279. is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index])
  280. : (index + regs.pipeline.vertex_offset);
  281. // -1 is a common special value used for primitive restart. Since it's unknown if
  282. // the PICA supports it, and it would mess up the caching, guard against it here.
  283. ASSERT(vertex != -1);
  284. bool vertex_cache_hit = false;
  285. if (is_indexed) {
  286. if (g_state.geometry_pipeline.NeedIndexInput()) {
  287. g_state.geometry_pipeline.SubmitIndex(vertex);
  288. continue;
  289. }
  290. if (g_debug_context && Pica::g_debug_context->recorder) {
  291. int size = index_u16 ? 2 : 1;
  292. memory_accesses.AddAccess(base_address + index_info.offset + size * index,
  293. size);
  294. }
  295. for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
  296. if (vertex == vertex_cache_ids[i]) {
  297. vs_output = vertex_cache[i];
  298. vertex_cache_hit = true;
  299. break;
  300. }
  301. }
  302. }
  303. if (!vertex_cache_hit) {
  304. // Initialize data for the current vertex
  305. Shader::AttributeBuffer input;
  306. loader.LoadVertex(base_address, index, vertex, input, memory_accesses);
  307. // Send to vertex shader
  308. if (g_debug_context)
  309. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  310. (void*)&input);
  311. shader_unit.LoadInput(regs.vs, input);
  312. shader_engine->Run(g_state.vs, shader_unit);
  313. shader_unit.WriteOutput(regs.vs, vs_output);
  314. if (is_indexed) {
  315. vertex_cache[vertex_cache_pos] = vs_output;
  316. vertex_cache_ids[vertex_cache_pos] = vertex;
  317. vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
  318. }
  319. }
  320. // Send to geometry pipeline
  321. g_state.geometry_pipeline.SubmitVertex(vs_output);
  322. }
  323. for (auto& range : memory_accesses.ranges) {
  324. g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
  325. range.second, range.first);
  326. }
  327. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  328. if (g_debug_context) {
  329. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  330. }
  331. break;
  332. }
  333. case PICA_REG_INDEX(gs.bool_uniforms):
  334. WriteUniformBoolReg(g_state.gs, g_state.regs.gs.bool_uniforms.Value());
  335. break;
  336. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[0], 0x281):
  337. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[1], 0x282):
  338. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[2], 0x283):
  339. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[3], 0x284): {
  340. unsigned index = (id - PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[0], 0x281));
  341. auto values = regs.gs.int_uniforms[index];
  342. WriteUniformIntReg(g_state.gs, index,
  343. Math::Vec4<u8>(values.x, values.y, values.z, values.w));
  344. break;
  345. }
  346. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[0], 0x291):
  347. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[1], 0x292):
  348. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[2], 0x293):
  349. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[3], 0x294):
  350. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[4], 0x295):
  351. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[5], 0x296):
  352. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[6], 0x297):
  353. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[7], 0x298): {
  354. WriteUniformFloatReg(g_state.regs.gs, g_state.gs, gs_float_regs_counter,
  355. gs_uniform_write_buffer, value);
  356. break;
  357. }
  358. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[0], 0x29c):
  359. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[1], 0x29d):
  360. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[2], 0x29e):
  361. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[3], 0x29f):
  362. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[4], 0x2a0):
  363. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[5], 0x2a1):
  364. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[6], 0x2a2):
  365. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[7], 0x2a3): {
  366. u32& offset = g_state.regs.gs.program.offset;
  367. if (offset >= 4096) {
  368. LOG_ERROR(HW_GPU, "Invalid GS program offset %u", offset);
  369. } else {
  370. g_state.gs.program_code[offset] = value;
  371. offset++;
  372. }
  373. break;
  374. }
  375. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[0], 0x2a6):
  376. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[1], 0x2a7):
  377. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[2], 0x2a8):
  378. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[3], 0x2a9):
  379. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[4], 0x2aa):
  380. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[5], 0x2ab):
  381. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[6], 0x2ac):
  382. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[7], 0x2ad): {
  383. u32& offset = g_state.regs.gs.swizzle_patterns.offset;
  384. if (offset >= g_state.gs.swizzle_data.size()) {
  385. LOG_ERROR(HW_GPU, "Invalid GS swizzle pattern offset %u", offset);
  386. } else {
  387. g_state.gs.swizzle_data[offset] = value;
  388. offset++;
  389. }
  390. break;
  391. }
  392. case PICA_REG_INDEX(vs.bool_uniforms):
  393. // TODO (wwylele): does regs.pipeline.gs_unit_exclusive_configuration affect this?
  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. // TODO (wwylele): does regs.pipeline.gs_unit_exclusive_configuration affect this?
  401. unsigned index = (id - PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1));
  402. auto values = regs.vs.int_uniforms[index];
  403. WriteUniformIntReg(g_state.vs, index,
  404. Math::Vec4<u8>(values.x, values.y, values.z, values.w));
  405. break;
  406. }
  407. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[0], 0x2c1):
  408. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[1], 0x2c2):
  409. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[2], 0x2c3):
  410. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[3], 0x2c4):
  411. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[4], 0x2c5):
  412. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[5], 0x2c6):
  413. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[6], 0x2c7):
  414. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[7], 0x2c8): {
  415. // TODO (wwylele): does regs.pipeline.gs_unit_exclusive_configuration affect this?
  416. WriteUniformFloatReg(g_state.regs.vs, g_state.vs, vs_float_regs_counter,
  417. vs_uniform_write_buffer, value);
  418. break;
  419. }
  420. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[0], 0x2cc):
  421. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[1], 0x2cd):
  422. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[2], 0x2ce):
  423. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[3], 0x2cf):
  424. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[4], 0x2d0):
  425. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[5], 0x2d1):
  426. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[6], 0x2d2):
  427. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[7], 0x2d3): {
  428. u32& offset = g_state.regs.vs.program.offset;
  429. if (offset >= 512) {
  430. LOG_ERROR(HW_GPU, "Invalid VS program offset %u", offset);
  431. } else {
  432. g_state.vs.program_code[offset] = value;
  433. if (!g_state.regs.pipeline.gs_unit_exclusive_configuration) {
  434. g_state.gs.program_code[offset] = value;
  435. }
  436. offset++;
  437. }
  438. break;
  439. }
  440. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[0], 0x2d6):
  441. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[1], 0x2d7):
  442. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[2], 0x2d8):
  443. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[3], 0x2d9):
  444. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[4], 0x2da):
  445. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[5], 0x2db):
  446. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[6], 0x2dc):
  447. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[7], 0x2dd): {
  448. u32& offset = g_state.regs.vs.swizzle_patterns.offset;
  449. if (offset >= g_state.vs.swizzle_data.size()) {
  450. LOG_ERROR(HW_GPU, "Invalid VS swizzle pattern offset %u", offset);
  451. } else {
  452. g_state.vs.swizzle_data[offset] = value;
  453. if (!g_state.regs.pipeline.gs_unit_exclusive_configuration) {
  454. g_state.gs.swizzle_data[offset] = value;
  455. }
  456. offset++;
  457. }
  458. break;
  459. }
  460. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  461. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  462. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  463. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  464. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  465. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  466. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  467. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf): {
  468. auto& lut_config = regs.lighting.lut_config;
  469. ASSERT_MSG(lut_config.index < 256, "lut_config.index exceeded maximum value of 255!");
  470. g_state.lighting.luts[lut_config.type][lut_config.index].raw = value;
  471. lut_config.index.Assign(lut_config.index + 1);
  472. break;
  473. }
  474. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[0], 0xe8):
  475. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[1], 0xe9):
  476. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[2], 0xea):
  477. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[3], 0xeb):
  478. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[4], 0xec):
  479. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[5], 0xed):
  480. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[6], 0xee):
  481. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[7], 0xef): {
  482. g_state.fog.lut[regs.texturing.fog_lut_offset % 128].raw = value;
  483. regs.texturing.fog_lut_offset.Assign(regs.texturing.fog_lut_offset + 1);
  484. break;
  485. }
  486. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[0], 0xb0):
  487. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[1], 0xb1):
  488. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[2], 0xb2):
  489. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[3], 0xb3):
  490. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[4], 0xb4):
  491. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[5], 0xb5):
  492. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[6], 0xb6):
  493. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[7], 0xb7): {
  494. auto& index = regs.texturing.proctex_lut_config.index;
  495. auto& pt = g_state.proctex;
  496. switch (regs.texturing.proctex_lut_config.ref_table.Value()) {
  497. case TexturingRegs::ProcTexLutTable::Noise:
  498. pt.noise_table[index % pt.noise_table.size()].raw = value;
  499. break;
  500. case TexturingRegs::ProcTexLutTable::ColorMap:
  501. pt.color_map_table[index % pt.color_map_table.size()].raw = value;
  502. break;
  503. case TexturingRegs::ProcTexLutTable::AlphaMap:
  504. pt.alpha_map_table[index % pt.alpha_map_table.size()].raw = value;
  505. break;
  506. case TexturingRegs::ProcTexLutTable::Color:
  507. pt.color_table[index % pt.color_table.size()].raw = value;
  508. break;
  509. case TexturingRegs::ProcTexLutTable::ColorDiff:
  510. pt.color_diff_table[index % pt.color_diff_table.size()].raw = value;
  511. break;
  512. }
  513. index.Assign(index + 1);
  514. break;
  515. }
  516. default:
  517. break;
  518. }
  519. VideoCore::g_renderer->Rasterizer()->NotifyPicaRegisterChanged(id);
  520. if (g_debug_context)
  521. g_debug_context->OnEvent(DebugContext::Event::PicaCommandProcessed,
  522. reinterpret_cast<void*>(&id));
  523. }
  524. void ProcessCommandList(const u32* list, u32 size) {
  525. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = list;
  526. g_state.cmd_list.length = size / sizeof(u32);
  527. while (g_state.cmd_list.current_ptr < g_state.cmd_list.head_ptr + g_state.cmd_list.length) {
  528. // Align read pointer to 8 bytes
  529. if ((g_state.cmd_list.head_ptr - g_state.cmd_list.current_ptr) % 2 != 0)
  530. ++g_state.cmd_list.current_ptr;
  531. u32 value = *g_state.cmd_list.current_ptr++;
  532. const CommandHeader header = {*g_state.cmd_list.current_ptr++};
  533. WritePicaReg(header.cmd_id, value, header.parameter_mask);
  534. for (unsigned i = 0; i < header.extra_data_length; ++i) {
  535. u32 cmd = header.cmd_id + (header.group_commands ? i + 1 : 0);
  536. WritePicaReg(cmd, *g_state.cmd_list.current_ptr++, header.parameter_mask);
  537. }
  538. }
  539. }
  540. } // namespace CommandProcessor
  541. } // namespace Pica