command_processor.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 draing 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. if (regs.pipeline.gpu_mode == PipelineRegs::GPUMode::Configuring) {
  216. MICROPROFILE_SCOPE(GPU_Drawing);
  217. // Draw immediate mode triangles when GPU Mode is set to GPUMode::Configuring
  218. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  219. if (g_debug_context) {
  220. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  221. }
  222. }
  223. break;
  224. case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[0], 0x23c):
  225. case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[1], 0x23d): {
  226. unsigned index =
  227. static_cast<unsigned>(id - PICA_REG_INDEX(pipeline.command_buffer.trigger[0]));
  228. u32* head_ptr = (u32*)Memory::GetPhysicalPointer(
  229. regs.pipeline.command_buffer.GetPhysicalAddress(index));
  230. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = head_ptr;
  231. g_state.cmd_list.length = regs.pipeline.command_buffer.GetSize(index) / sizeof(u32);
  232. break;
  233. }
  234. // It seems like these trigger vertex rendering
  235. case PICA_REG_INDEX(pipeline.trigger_draw):
  236. case PICA_REG_INDEX(pipeline.trigger_draw_indexed): {
  237. MICROPROFILE_SCOPE(GPU_Drawing);
  238. #if PICA_LOG_TEV
  239. DebugUtils::DumpTevStageConfig(regs.GetTevStages());
  240. #endif
  241. if (g_debug_context)
  242. g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  243. // Processes information about internal vertex attributes to figure out how a vertex is
  244. // loaded.
  245. // Later, these can be compiled and cached.
  246. const u32 base_address = regs.pipeline.vertex_attributes.GetPhysicalBaseAddress();
  247. VertexLoader loader(regs.pipeline);
  248. // Load vertices
  249. bool is_indexed = (id == PICA_REG_INDEX(pipeline.trigger_draw_indexed));
  250. const auto& index_info = regs.pipeline.index_array;
  251. const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
  252. const u16* index_address_16 = reinterpret_cast<const u16*>(index_address_8);
  253. bool index_u16 = index_info.format != 0;
  254. PrimitiveAssembler<Shader::OutputVertex>& primitive_assembler = g_state.primitive_assembler;
  255. if (g_debug_context && g_debug_context->recorder) {
  256. for (int i = 0; i < 3; ++i) {
  257. const auto texture = regs.texturing.GetTextures()[i];
  258. if (!texture.enabled)
  259. continue;
  260. u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
  261. g_debug_context->recorder->MemoryAccessed(
  262. texture_data, Pica::TexturingRegs::NibblesPerPixel(texture.format) *
  263. texture.config.width / 2 * texture.config.height,
  264. texture.config.GetPhysicalAddress());
  265. }
  266. }
  267. DebugUtils::MemoryAccessTracker memory_accesses;
  268. // Simple circular-replacement vertex cache
  269. // The size has been tuned for optimal balance between hit-rate and the cost of lookup
  270. const size_t VERTEX_CACHE_SIZE = 32;
  271. std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
  272. std::array<Shader::AttributeBuffer, VERTEX_CACHE_SIZE> vertex_cache;
  273. Shader::AttributeBuffer vs_output;
  274. unsigned int vertex_cache_pos = 0;
  275. vertex_cache_ids.fill(-1);
  276. auto* shader_engine = Shader::GetEngine();
  277. Shader::UnitState shader_unit;
  278. shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
  279. g_state.geometry_pipeline.Reconfigure();
  280. g_state.geometry_pipeline.Setup(shader_engine);
  281. if (g_state.geometry_pipeline.NeedIndexInput())
  282. ASSERT(is_indexed);
  283. for (unsigned int index = 0; index < regs.pipeline.num_vertices; ++index) {
  284. // Indexed rendering doesn't use the start offset
  285. unsigned int vertex =
  286. is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index])
  287. : (index + regs.pipeline.vertex_offset);
  288. // -1 is a common special value used for primitive restart. Since it's unknown if
  289. // the PICA supports it, and it would mess up the caching, guard against it here.
  290. ASSERT(vertex != -1);
  291. bool vertex_cache_hit = false;
  292. if (is_indexed) {
  293. if (g_state.geometry_pipeline.NeedIndexInput()) {
  294. g_state.geometry_pipeline.SubmitIndex(vertex);
  295. continue;
  296. }
  297. if (g_debug_context && Pica::g_debug_context->recorder) {
  298. int size = index_u16 ? 2 : 1;
  299. memory_accesses.AddAccess(base_address + index_info.offset + size * index,
  300. size);
  301. }
  302. for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
  303. if (vertex == vertex_cache_ids[i]) {
  304. vs_output = vertex_cache[i];
  305. vertex_cache_hit = true;
  306. break;
  307. }
  308. }
  309. }
  310. if (!vertex_cache_hit) {
  311. // Initialize data for the current vertex
  312. Shader::AttributeBuffer input;
  313. loader.LoadVertex(base_address, index, vertex, input, memory_accesses);
  314. // Send to vertex shader
  315. if (g_debug_context)
  316. g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
  317. (void*)&input);
  318. shader_unit.LoadInput(regs.vs, input);
  319. shader_engine->Run(g_state.vs, shader_unit);
  320. shader_unit.WriteOutput(regs.vs, vs_output);
  321. if (is_indexed) {
  322. vertex_cache[vertex_cache_pos] = vs_output;
  323. vertex_cache_ids[vertex_cache_pos] = vertex;
  324. vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
  325. }
  326. }
  327. // Send to geometry pipeline
  328. g_state.geometry_pipeline.SubmitVertex(vs_output);
  329. }
  330. for (auto& range : memory_accesses.ranges) {
  331. g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
  332. range.second, range.first);
  333. }
  334. MICROPROFILE_SCOPE(GPU_Drawing);
  335. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  336. if (g_debug_context) {
  337. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  338. }
  339. break;
  340. }
  341. case PICA_REG_INDEX(gs.bool_uniforms):
  342. WriteUniformBoolReg(g_state.gs, g_state.regs.gs.bool_uniforms.Value());
  343. break;
  344. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[0], 0x281):
  345. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[1], 0x282):
  346. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[2], 0x283):
  347. case PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[3], 0x284): {
  348. unsigned index = (id - PICA_REG_INDEX_WORKAROUND(gs.int_uniforms[0], 0x281));
  349. auto values = regs.gs.int_uniforms[index];
  350. WriteUniformIntReg(g_state.gs, index,
  351. Math::Vec4<u8>(values.x, values.y, values.z, values.w));
  352. break;
  353. }
  354. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[0], 0x291):
  355. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[1], 0x292):
  356. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[2], 0x293):
  357. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[3], 0x294):
  358. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[4], 0x295):
  359. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[5], 0x296):
  360. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[6], 0x297):
  361. case PICA_REG_INDEX_WORKAROUND(gs.uniform_setup.set_value[7], 0x298): {
  362. WriteUniformFloatReg(g_state.regs.gs, g_state.gs, gs_float_regs_counter,
  363. gs_uniform_write_buffer, value);
  364. break;
  365. }
  366. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[0], 0x29c):
  367. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[1], 0x29d):
  368. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[2], 0x29e):
  369. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[3], 0x29f):
  370. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[4], 0x2a0):
  371. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[5], 0x2a1):
  372. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[6], 0x2a2):
  373. case PICA_REG_INDEX_WORKAROUND(gs.program.set_word[7], 0x2a3): {
  374. u32& offset = g_state.regs.gs.program.offset;
  375. if (offset >= 4096) {
  376. LOG_ERROR(HW_GPU, "Invalid GS program offset %u", offset);
  377. } else {
  378. g_state.gs.program_code[offset] = value;
  379. offset++;
  380. }
  381. break;
  382. }
  383. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[0], 0x2a6):
  384. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[1], 0x2a7):
  385. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[2], 0x2a8):
  386. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[3], 0x2a9):
  387. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[4], 0x2aa):
  388. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[5], 0x2ab):
  389. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[6], 0x2ac):
  390. case PICA_REG_INDEX_WORKAROUND(gs.swizzle_patterns.set_word[7], 0x2ad): {
  391. u32& offset = g_state.regs.gs.swizzle_patterns.offset;
  392. if (offset >= g_state.gs.swizzle_data.size()) {
  393. LOG_ERROR(HW_GPU, "Invalid GS swizzle pattern offset %u", offset);
  394. } else {
  395. g_state.gs.swizzle_data[offset] = value;
  396. offset++;
  397. }
  398. break;
  399. }
  400. case PICA_REG_INDEX(vs.bool_uniforms):
  401. // TODO (wwylele): does regs.pipeline.gs_unit_exclusive_configuration affect this?
  402. WriteUniformBoolReg(g_state.vs, g_state.regs.vs.bool_uniforms.Value());
  403. break;
  404. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1):
  405. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[1], 0x2b2):
  406. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[2], 0x2b3):
  407. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[3], 0x2b4): {
  408. // TODO (wwylele): does regs.pipeline.gs_unit_exclusive_configuration affect this?
  409. unsigned index = (id - PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1));
  410. auto values = regs.vs.int_uniforms[index];
  411. WriteUniformIntReg(g_state.vs, index,
  412. Math::Vec4<u8>(values.x, values.y, values.z, values.w));
  413. break;
  414. }
  415. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[0], 0x2c1):
  416. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[1], 0x2c2):
  417. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[2], 0x2c3):
  418. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[3], 0x2c4):
  419. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[4], 0x2c5):
  420. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[5], 0x2c6):
  421. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[6], 0x2c7):
  422. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[7], 0x2c8): {
  423. // TODO (wwylele): does regs.pipeline.gs_unit_exclusive_configuration affect this?
  424. WriteUniformFloatReg(g_state.regs.vs, g_state.vs, vs_float_regs_counter,
  425. vs_uniform_write_buffer, value);
  426. break;
  427. }
  428. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[0], 0x2cc):
  429. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[1], 0x2cd):
  430. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[2], 0x2ce):
  431. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[3], 0x2cf):
  432. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[4], 0x2d0):
  433. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[5], 0x2d1):
  434. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[6], 0x2d2):
  435. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[7], 0x2d3): {
  436. u32& offset = g_state.regs.vs.program.offset;
  437. if (offset >= 512) {
  438. LOG_ERROR(HW_GPU, "Invalid VS program offset %u", offset);
  439. } else {
  440. g_state.vs.program_code[offset] = value;
  441. if (!g_state.regs.pipeline.gs_unit_exclusive_configuration) {
  442. g_state.gs.program_code[offset] = value;
  443. }
  444. offset++;
  445. }
  446. break;
  447. }
  448. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[0], 0x2d6):
  449. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[1], 0x2d7):
  450. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[2], 0x2d8):
  451. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[3], 0x2d9):
  452. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[4], 0x2da):
  453. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[5], 0x2db):
  454. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[6], 0x2dc):
  455. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[7], 0x2dd): {
  456. u32& offset = g_state.regs.vs.swizzle_patterns.offset;
  457. if (offset >= g_state.vs.swizzle_data.size()) {
  458. LOG_ERROR(HW_GPU, "Invalid VS swizzle pattern offset %u", offset);
  459. } else {
  460. g_state.vs.swizzle_data[offset] = value;
  461. if (!g_state.regs.pipeline.gs_unit_exclusive_configuration) {
  462. g_state.gs.swizzle_data[offset] = value;
  463. }
  464. offset++;
  465. }
  466. break;
  467. }
  468. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  469. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  470. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  471. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  472. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  473. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  474. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  475. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf): {
  476. auto& lut_config = regs.lighting.lut_config;
  477. ASSERT_MSG(lut_config.index < 256, "lut_config.index exceeded maximum value of 255!");
  478. g_state.lighting.luts[lut_config.type][lut_config.index].raw = value;
  479. lut_config.index.Assign(lut_config.index + 1);
  480. break;
  481. }
  482. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[0], 0xe8):
  483. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[1], 0xe9):
  484. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[2], 0xea):
  485. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[3], 0xeb):
  486. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[4], 0xec):
  487. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[5], 0xed):
  488. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[6], 0xee):
  489. case PICA_REG_INDEX_WORKAROUND(texturing.fog_lut_data[7], 0xef): {
  490. g_state.fog.lut[regs.texturing.fog_lut_offset % 128].raw = value;
  491. regs.texturing.fog_lut_offset.Assign(regs.texturing.fog_lut_offset + 1);
  492. break;
  493. }
  494. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[0], 0xb0):
  495. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[1], 0xb1):
  496. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[2], 0xb2):
  497. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[3], 0xb3):
  498. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[4], 0xb4):
  499. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[5], 0xb5):
  500. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[6], 0xb6):
  501. case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[7], 0xb7): {
  502. auto& index = regs.texturing.proctex_lut_config.index;
  503. auto& pt = g_state.proctex;
  504. switch (regs.texturing.proctex_lut_config.ref_table.Value()) {
  505. case TexturingRegs::ProcTexLutTable::Noise:
  506. pt.noise_table[index % pt.noise_table.size()].raw = value;
  507. break;
  508. case TexturingRegs::ProcTexLutTable::ColorMap:
  509. pt.color_map_table[index % pt.color_map_table.size()].raw = value;
  510. break;
  511. case TexturingRegs::ProcTexLutTable::AlphaMap:
  512. pt.alpha_map_table[index % pt.alpha_map_table.size()].raw = value;
  513. break;
  514. case TexturingRegs::ProcTexLutTable::Color:
  515. pt.color_table[index % pt.color_table.size()].raw = value;
  516. break;
  517. case TexturingRegs::ProcTexLutTable::ColorDiff:
  518. pt.color_diff_table[index % pt.color_diff_table.size()].raw = value;
  519. break;
  520. }
  521. index.Assign(index + 1);
  522. break;
  523. }
  524. default:
  525. break;
  526. }
  527. VideoCore::g_renderer->Rasterizer()->NotifyPicaRegisterChanged(id);
  528. if (g_debug_context)
  529. g_debug_context->OnEvent(DebugContext::Event::PicaCommandProcessed,
  530. reinterpret_cast<void*>(&id));
  531. }
  532. void ProcessCommandList(const u32* list, u32 size) {
  533. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = list;
  534. g_state.cmd_list.length = size / sizeof(u32);
  535. while (g_state.cmd_list.current_ptr < g_state.cmd_list.head_ptr + g_state.cmd_list.length) {
  536. // Align read pointer to 8 bytes
  537. if ((g_state.cmd_list.head_ptr - g_state.cmd_list.current_ptr) % 2 != 0)
  538. ++g_state.cmd_list.current_ptr;
  539. u32 value = *g_state.cmd_list.current_ptr++;
  540. const CommandHeader header = {*g_state.cmd_list.current_ptr++};
  541. WritePicaReg(header.cmd_id, value, header.parameter_mask);
  542. for (unsigned i = 0; i < header.extra_data_length; ++i) {
  543. u32 cmd = header.cmd_id + (header.group_commands ? i + 1 : 0);
  544. WritePicaReg(cmd, *g_state.cmd_list.current_ptr++, header.parameter_mask);
  545. }
  546. }
  547. }
  548. } // namespace CommandProcessor
  549. } // namespace Pica