command_processor.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include <boost/range/algorithm/fill.hpp>
  6. #include "common/microprofile.h"
  7. #include "common/profiler.h"
  8. #include "core/settings.h"
  9. #include "core/hle/service/gsp_gpu.h"
  10. #include "core/hw/gpu.h"
  11. #include "video_core/clipper.h"
  12. #include "video_core/command_processor.h"
  13. #include "video_core/pica.h"
  14. #include "video_core/pica_state.h"
  15. #include "video_core/primitive_assembly.h"
  16. #include "video_core/renderer_base.h"
  17. #include "video_core/video_core.h"
  18. #include "video_core/debug_utils/debug_utils.h"
  19. #include "video_core/shader/shader_interpreter.h"
  20. namespace Pica {
  21. namespace CommandProcessor {
  22. static int float_regs_counter = 0;
  23. static u32 uniform_write_buffer[4];
  24. static int default_attr_counter = 0;
  25. static u32 default_attr_write_buffer[3];
  26. Common::Profiling::TimingCategory category_drawing("Drawing");
  27. // Expand a 4-bit mask to 4-byte mask, e.g. 0b0101 -> 0x00FF00FF
  28. static const u32 expand_bits_to_bytes[] = {
  29. 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
  30. 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
  31. 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
  32. 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff
  33. };
  34. MICROPROFILE_DEFINE(GPU_Drawing, "GPU", "Drawing", MP_RGB(50, 50, 240));
  35. static void WritePicaReg(u32 id, u32 value, u32 mask) {
  36. auto& regs = g_state.regs;
  37. if (id >= regs.NumIds())
  38. return;
  39. // If we're skipping this frame, only allow trigger IRQ
  40. if (GPU::g_skip_frame && id != PICA_REG_INDEX(trigger_irq))
  41. return;
  42. // TODO: Figure out how register masking acts on e.g. vs.uniform_setup.set_value
  43. u32 old_value = regs[id];
  44. const u32 write_mask = expand_bits_to_bytes[mask];
  45. regs[id] = (old_value & ~write_mask) | (value & write_mask);
  46. DebugUtils::OnPicaRegWrite({ (u16)id, (u16)mask, regs[id] });
  47. if (g_debug_context)
  48. g_debug_context->OnEvent(DebugContext::Event::PicaCommandLoaded, reinterpret_cast<void*>(&id));
  49. switch(id) {
  50. // Trigger IRQ
  51. case PICA_REG_INDEX(trigger_irq):
  52. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::P3D);
  53. break;
  54. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.index, 0x232):
  55. if (regs.vs_default_attributes_setup.index == 15) {
  56. // Reset immediate primitive state
  57. g_state.immediate.primitive_assembler.Reconfigure(regs.triangle_topology);
  58. g_state.immediate.attribute_id = 0;
  59. }
  60. break;
  61. // Load default vertex input attributes
  62. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[0], 0x233):
  63. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[1], 0x234):
  64. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[2], 0x235):
  65. {
  66. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  67. // it directly write the values?
  68. default_attr_write_buffer[default_attr_counter++] = value;
  69. // Default attributes 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 (default_attr_counter >= 3) {
  73. default_attr_counter = 0;
  74. auto& setup = regs.vs_default_attributes_setup;
  75. if (setup.index >= 16) {
  76. LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
  77. break;
  78. }
  79. Math::Vec4<float24>& attribute = g_state.vs.default_attributes[setup.index];
  80. // NOTE: The destination component order indeed is "backwards"
  81. attribute.w = float24::FromRaw(default_attr_write_buffer[0] >> 8);
  82. attribute.z = float24::FromRaw(((default_attr_write_buffer[0] & 0xFF) << 16) | ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
  83. attribute.y = float24::FromRaw(((default_attr_write_buffer[1] & 0xFFFF) << 8) | ((default_attr_write_buffer[2] >> 24) & 0xFF));
  84. attribute.x = float24::FromRaw(default_attr_write_buffer[2] & 0xFFFFFF);
  85. LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
  86. attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
  87. attribute.w.ToFloat32());
  88. // TODO: Verify that this actually modifies the register!
  89. if (setup.index < 15) {
  90. setup.index++;
  91. } else {
  92. // Put each attribute into an immediate input buffer.
  93. // When all specified immediate attributes are present, the Vertex Shader is invoked and everything is
  94. // sent to the primitive assembler.
  95. auto& immediate_input = g_state.immediate.input;
  96. auto& immediate_attribute_id = g_state.immediate.attribute_id;
  97. const auto& attribute_config = regs.vertex_attributes;
  98. immediate_input.attr[immediate_attribute_id++] = attribute;
  99. if (immediate_attribute_id >= attribute_config.GetNumTotalAttributes()) {
  100. immediate_attribute_id = 0;
  101. Shader::UnitState<false> shader_unit;
  102. Shader::Setup(shader_unit);
  103. // Send to vertex shader
  104. Shader::OutputVertex output = Shader::Run(shader_unit, immediate_input, attribute_config.GetNumTotalAttributes());
  105. // Send to renderer
  106. using Pica::Shader::OutputVertex;
  107. auto AddTriangle = [](const OutputVertex& v0, const OutputVertex& v1, const OutputVertex& v2) {
  108. VideoCore::g_renderer->Rasterizer()->AddTriangle(v0, v1, v2);
  109. };
  110. g_state.immediate.primitive_assembler.SubmitVertex(output, AddTriangle);
  111. }
  112. }
  113. }
  114. break;
  115. }
  116. case PICA_REG_INDEX(gpu_mode):
  117. if (regs.gpu_mode == Regs::GPUMode::Configuring && regs.vs_default_attributes_setup.index == 15) {
  118. // Draw immediate mode triangles when GPU Mode is set to GPUMode::Configuring
  119. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  120. }
  121. break;
  122. case PICA_REG_INDEX_WORKAROUND(command_buffer.trigger[0], 0x23c):
  123. case PICA_REG_INDEX_WORKAROUND(command_buffer.trigger[1], 0x23d):
  124. {
  125. unsigned index = static_cast<unsigned>(id - PICA_REG_INDEX(command_buffer.trigger[0]));
  126. u32* head_ptr = (u32*)Memory::GetPhysicalPointer(regs.command_buffer.GetPhysicalAddress(index));
  127. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = head_ptr;
  128. g_state.cmd_list.length = regs.command_buffer.GetSize(index) / sizeof(u32);
  129. break;
  130. }
  131. // It seems like these trigger vertex rendering
  132. case PICA_REG_INDEX(trigger_draw):
  133. case PICA_REG_INDEX(trigger_draw_indexed):
  134. {
  135. Common::Profiling::ScopeTimer scope_timer(category_drawing);
  136. MICROPROFILE_SCOPE(GPU_Drawing);
  137. #if PICA_LOG_TEV
  138. DebugUtils::DumpTevStageConfig(regs.GetTevStages());
  139. #endif
  140. if (g_debug_context)
  141. g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  142. const auto& attribute_config = regs.vertex_attributes;
  143. const u32 base_address = attribute_config.GetPhysicalBaseAddress();
  144. // Information about internal vertex attributes
  145. u32 vertex_attribute_sources[16];
  146. boost::fill(vertex_attribute_sources, 0xdeadbeef);
  147. u32 vertex_attribute_strides[16] = {};
  148. Regs::VertexAttributeFormat vertex_attribute_formats[16] = {};
  149. u32 vertex_attribute_elements[16] = {};
  150. u32 vertex_attribute_element_size[16] = {};
  151. // Setup attribute data from loaders
  152. for (int loader = 0; loader < 12; ++loader) {
  153. const auto& loader_config = attribute_config.attribute_loaders[loader];
  154. u32 load_address = base_address + loader_config.data_offset;
  155. // TODO: What happens if a loader overwrites a previous one's data?
  156. for (unsigned component = 0; component < loader_config.component_count; ++component) {
  157. if (component >= 12) {
  158. LOG_ERROR(HW_GPU, "Overflow in the vertex attribute loader %u trying to load component %u", loader, component);
  159. continue;
  160. }
  161. u32 attribute_index = loader_config.GetComponent(component);
  162. if (attribute_index < 12) {
  163. vertex_attribute_sources[attribute_index] = load_address;
  164. vertex_attribute_strides[attribute_index] = static_cast<u32>(loader_config.byte_count);
  165. vertex_attribute_formats[attribute_index] = attribute_config.GetFormat(attribute_index);
  166. vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index);
  167. vertex_attribute_element_size[attribute_index] = attribute_config.GetElementSizeInBytes(attribute_index);
  168. load_address += attribute_config.GetStride(attribute_index);
  169. } else if (attribute_index < 16) {
  170. // Attribute ids 12, 13, 14 and 15 signify 4, 8, 12 and 16-byte paddings, respectively
  171. load_address += (attribute_index - 11) * 4;
  172. } else {
  173. UNREACHABLE(); // This is truly unreachable due to the number of bits for each component
  174. }
  175. }
  176. }
  177. // Load vertices
  178. bool is_indexed = (id == PICA_REG_INDEX(trigger_draw_indexed));
  179. const auto& index_info = regs.index_array;
  180. const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
  181. const u16* index_address_16 = (u16*)index_address_8;
  182. bool index_u16 = index_info.format != 0;
  183. #if PICA_DUMP_GEOMETRY
  184. DebugUtils::GeometryDumper geometry_dumper;
  185. PrimitiveAssembler<DebugUtils::GeometryDumper::Vertex> dumping_primitive_assembler(regs.triangle_topology.Value());
  186. #endif
  187. PrimitiveAssembler<Shader::OutputVertex> primitive_assembler(regs.triangle_topology.Value());
  188. if (g_debug_context) {
  189. for (int i = 0; i < 3; ++i) {
  190. const auto texture = regs.GetTextures()[i];
  191. if (!texture.enabled)
  192. continue;
  193. u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
  194. if (g_debug_context && Pica::g_debug_context->recorder)
  195. g_debug_context->recorder->MemoryAccessed(texture_data, Pica::Regs::NibblesPerPixel(texture.format) * texture.config.width / 2 * texture.config.height, texture.config.GetPhysicalAddress());
  196. }
  197. }
  198. class {
  199. /// Combine overlapping and close ranges
  200. void SimplifyRanges() {
  201. for (auto it = ranges.begin(); it != ranges.end(); ++it) {
  202. // NOTE: We add 32 to the range end address to make sure "close" ranges are combined, too
  203. auto it2 = std::next(it);
  204. while (it2 != ranges.end() && it->first + it->second + 32 >= it2->first) {
  205. it->second = std::max(it->second, it2->first + it2->second - it->first);
  206. it2 = ranges.erase(it2);
  207. }
  208. }
  209. }
  210. public:
  211. /// Record a particular memory access in the list
  212. void AddAccess(u32 paddr, u32 size) {
  213. // Create new range or extend existing one
  214. ranges[paddr] = std::max(ranges[paddr], size);
  215. // Simplify ranges...
  216. SimplifyRanges();
  217. }
  218. /// Map of accessed ranges (mapping start address to range size)
  219. std::map<u32, u32> ranges;
  220. } memory_accesses;
  221. // Simple circular-replacement vertex cache
  222. // The size has been tuned for optimal balance between hit-rate and the cost of lookup
  223. const size_t VERTEX_CACHE_SIZE = 32;
  224. std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
  225. std::array<Shader::OutputVertex, VERTEX_CACHE_SIZE> vertex_cache;
  226. unsigned int vertex_cache_pos = 0;
  227. vertex_cache_ids.fill(-1);
  228. Shader::UnitState<false> shader_unit;
  229. Shader::Setup(shader_unit);
  230. for (unsigned int index = 0; index < regs.num_vertices; ++index)
  231. {
  232. // Indexed rendering doesn't use the start offset
  233. unsigned int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : (index + regs.vertex_offset);
  234. // -1 is a common special value used for primitive restart. Since it's unknown if
  235. // the PICA supports it, and it would mess up the caching, guard against it here.
  236. ASSERT(vertex != -1);
  237. bool vertex_cache_hit = false;
  238. Shader::OutputVertex output;
  239. if (is_indexed) {
  240. if (g_debug_context && Pica::g_debug_context->recorder) {
  241. int size = index_u16 ? 2 : 1;
  242. memory_accesses.AddAccess(base_address + index_info.offset + size * index, size);
  243. }
  244. for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
  245. if (vertex == vertex_cache_ids[i]) {
  246. output = vertex_cache[i];
  247. vertex_cache_hit = true;
  248. break;
  249. }
  250. }
  251. }
  252. if (!vertex_cache_hit) {
  253. // Initialize data for the current vertex
  254. Shader::InputVertex input;
  255. for (int i = 0; i < attribute_config.GetNumTotalAttributes(); ++i) {
  256. if (vertex_attribute_elements[i] != 0) {
  257. // Default attribute values set if array elements have < 4 components. This
  258. // is *not* carried over from the default attribute settings even if they're
  259. // enabled for this attribute.
  260. static const float24 zero = float24::FromFloat32(0.0f);
  261. static const float24 one = float24::FromFloat32(1.0f);
  262. input.attr[i] = Math::Vec4<float24>(zero, zero, zero, one);
  263. // Load per-vertex data from the loader arrays
  264. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  265. u32 source_addr = vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i];
  266. const u8* srcdata = Memory::GetPhysicalPointer(source_addr);
  267. if (g_debug_context && Pica::g_debug_context->recorder) {
  268. memory_accesses.AddAccess(source_addr,
  269. (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::FLOAT) ? 4
  270. : (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? 2 : 1);
  271. }
  272. const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata :
  273. (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata :
  274. (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? *(s16*)srcdata :
  275. *(float*)srcdata;
  276. input.attr[i][comp] = float24::FromFloat32(srcval);
  277. LOG_TRACE(HW_GPU, "Loaded component %x of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08x + 0x%04x: %f",
  278. comp, i, vertex, index,
  279. attribute_config.GetPhysicalBaseAddress(),
  280. vertex_attribute_sources[i] - base_address,
  281. vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i],
  282. input.attr[i][comp].ToFloat32());
  283. }
  284. } else if (attribute_config.IsDefaultAttribute(i)) {
  285. // Load the default attribute if we're configured to do so
  286. input.attr[i] = g_state.vs.default_attributes[i];
  287. LOG_TRACE(HW_GPU, "Loaded default attribute %x for vertex %x (index %x): (%f, %f, %f, %f)",
  288. i, vertex, index,
  289. input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  290. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  291. } else {
  292. // TODO(yuriks): In this case, no data gets loaded and the vertex
  293. // remains with the last value it had. This isn't currently maintained
  294. // as global state, however, and so won't work in Citra yet.
  295. }
  296. }
  297. if (g_debug_context)
  298. g_debug_context->OnEvent(DebugContext::Event::VertexLoaded, (void*)&input);
  299. #if PICA_DUMP_GEOMETRY
  300. // NOTE: When dumping geometry, we simply assume that the first input attribute
  301. // corresponds to the position for now.
  302. DebugUtils::GeometryDumper::Vertex dumped_vertex = {
  303. input.attr[0][0].ToFloat32(), input.attr[0][1].ToFloat32(), input.attr[0][2].ToFloat32()
  304. };
  305. using namespace std::placeholders;
  306. dumping_primitive_assembler.SubmitVertex(dumped_vertex,
  307. std::bind(&DebugUtils::GeometryDumper::AddTriangle,
  308. &geometry_dumper, _1, _2, _3));
  309. #endif
  310. // Send to vertex shader
  311. output = Shader::Run(shader_unit, input, attribute_config.GetNumTotalAttributes());
  312. if (is_indexed) {
  313. vertex_cache[vertex_cache_pos] = output;
  314. vertex_cache_ids[vertex_cache_pos] = vertex;
  315. vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
  316. }
  317. }
  318. // Send to renderer
  319. using Pica::Shader::OutputVertex;
  320. auto AddTriangle = [](
  321. const OutputVertex& v0, const OutputVertex& v1, const OutputVertex& v2) {
  322. VideoCore::g_renderer->Rasterizer()->AddTriangle(v0, v1, v2);
  323. };
  324. primitive_assembler.SubmitVertex(output, AddTriangle);
  325. }
  326. for (auto& range : memory_accesses.ranges) {
  327. g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
  328. range.second, range.first);
  329. }
  330. VideoCore::g_renderer->Rasterizer()->DrawTriangles();
  331. #if PICA_DUMP_GEOMETRY
  332. geometry_dumper.Dump();
  333. #endif
  334. if (g_debug_context) {
  335. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  336. }
  337. break;
  338. }
  339. case PICA_REG_INDEX(vs.bool_uniforms):
  340. for (unsigned i = 0; i < 16; ++i)
  341. g_state.vs.uniforms.b[i] = (regs.vs.bool_uniforms.Value() & (1 << i)) != 0;
  342. break;
  343. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1):
  344. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[1], 0x2b2):
  345. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[2], 0x2b3):
  346. case PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[3], 0x2b4):
  347. {
  348. int index = (id - PICA_REG_INDEX_WORKAROUND(vs.int_uniforms[0], 0x2b1));
  349. auto values = regs.vs.int_uniforms[index];
  350. g_state.vs.uniforms.i[index] = Math::Vec4<u8>(values.x, values.y, values.z, values.w);
  351. LOG_TRACE(HW_GPU, "Set integer uniform %d to %02x %02x %02x %02x",
  352. index, values.x.Value(), values.y.Value(), values.z.Value(), values.w.Value());
  353. break;
  354. }
  355. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[0], 0x2c1):
  356. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[1], 0x2c2):
  357. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[2], 0x2c3):
  358. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[3], 0x2c4):
  359. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[4], 0x2c5):
  360. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[5], 0x2c6):
  361. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[6], 0x2c7):
  362. case PICA_REG_INDEX_WORKAROUND(vs.uniform_setup.set_value[7], 0x2c8):
  363. {
  364. auto& uniform_setup = regs.vs.uniform_setup;
  365. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  366. // it directly write the values?
  367. uniform_write_buffer[float_regs_counter++] = value;
  368. // Uniforms are written in a packed format such that four float24 values are encoded in
  369. // three 32-bit numbers. We write to internal memory once a full such vector is
  370. // written.
  371. if ((float_regs_counter >= 4 && uniform_setup.IsFloat32()) ||
  372. (float_regs_counter >= 3 && !uniform_setup.IsFloat32())) {
  373. float_regs_counter = 0;
  374. auto& uniform = g_state.vs.uniforms.f[uniform_setup.index];
  375. if (uniform_setup.index > 95) {
  376. LOG_ERROR(HW_GPU, "Invalid VS uniform index %d", (int)uniform_setup.index);
  377. break;
  378. }
  379. // NOTE: The destination component order indeed is "backwards"
  380. if (uniform_setup.IsFloat32()) {
  381. for (auto i : {0,1,2,3})
  382. uniform[3 - i] = float24::FromFloat32(*(float*)(&uniform_write_buffer[i]));
  383. } else {
  384. // TODO: Untested
  385. uniform.w = float24::FromRaw(uniform_write_buffer[0] >> 8);
  386. uniform.z = float24::FromRaw(((uniform_write_buffer[0] & 0xFF) << 16) | ((uniform_write_buffer[1] >> 16) & 0xFFFF));
  387. uniform.y = float24::FromRaw(((uniform_write_buffer[1] & 0xFFFF) << 8) | ((uniform_write_buffer[2] >> 24) & 0xFF));
  388. uniform.x = float24::FromRaw(uniform_write_buffer[2] & 0xFFFFFF);
  389. }
  390. LOG_TRACE(HW_GPU, "Set uniform %x to (%f %f %f %f)", (int)uniform_setup.index,
  391. uniform.x.ToFloat32(), uniform.y.ToFloat32(), uniform.z.ToFloat32(),
  392. uniform.w.ToFloat32());
  393. // TODO: Verify that this actually modifies the register!
  394. uniform_setup.index.Assign(uniform_setup.index + 1);
  395. }
  396. break;
  397. }
  398. // Load shader program code
  399. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[0], 0x2cc):
  400. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[1], 0x2cd):
  401. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[2], 0x2ce):
  402. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[3], 0x2cf):
  403. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[4], 0x2d0):
  404. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[5], 0x2d1):
  405. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[6], 0x2d2):
  406. case PICA_REG_INDEX_WORKAROUND(vs.program.set_word[7], 0x2d3):
  407. {
  408. g_state.vs.program_code[regs.vs.program.offset] = value;
  409. regs.vs.program.offset++;
  410. break;
  411. }
  412. // Load swizzle pattern data
  413. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[0], 0x2d6):
  414. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[1], 0x2d7):
  415. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[2], 0x2d8):
  416. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[3], 0x2d9):
  417. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[4], 0x2da):
  418. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[5], 0x2db):
  419. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[6], 0x2dc):
  420. case PICA_REG_INDEX_WORKAROUND(vs.swizzle_patterns.set_word[7], 0x2dd):
  421. {
  422. g_state.vs.swizzle_data[regs.vs.swizzle_patterns.offset] = value;
  423. regs.vs.swizzle_patterns.offset++;
  424. break;
  425. }
  426. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[0], 0x1c8):
  427. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[1], 0x1c9):
  428. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[2], 0x1ca):
  429. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[3], 0x1cb):
  430. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[4], 0x1cc):
  431. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[5], 0x1cd):
  432. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[6], 0x1ce):
  433. case PICA_REG_INDEX_WORKAROUND(lighting.lut_data[7], 0x1cf):
  434. {
  435. auto& lut_config = regs.lighting.lut_config;
  436. ASSERT_MSG(lut_config.index < 256, "lut_config.index exceeded maximum value of 255!");
  437. g_state.lighting.luts[lut_config.type][lut_config.index].raw = value;
  438. lut_config.index.Assign(lut_config.index + 1);
  439. break;
  440. }
  441. default:
  442. break;
  443. }
  444. VideoCore::g_renderer->Rasterizer()->NotifyPicaRegisterChanged(id);
  445. if (g_debug_context)
  446. g_debug_context->OnEvent(DebugContext::Event::PicaCommandProcessed, reinterpret_cast<void*>(&id));
  447. }
  448. void ProcessCommandList(const u32* list, u32 size) {
  449. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = list;
  450. g_state.cmd_list.length = size / sizeof(u32);
  451. while (g_state.cmd_list.current_ptr < g_state.cmd_list.head_ptr + g_state.cmd_list.length) {
  452. // Align read pointer to 8 bytes
  453. if ((g_state.cmd_list.head_ptr - g_state.cmd_list.current_ptr) % 2 != 0)
  454. ++g_state.cmd_list.current_ptr;
  455. u32 value = *g_state.cmd_list.current_ptr++;
  456. const CommandHeader header = { *g_state.cmd_list.current_ptr++ };
  457. WritePicaReg(header.cmd_id, value, header.parameter_mask);
  458. for (unsigned i = 0; i < header.extra_data_length; ++i) {
  459. u32 cmd = header.cmd_id + (header.group_commands ? i + 1 : 0);
  460. WritePicaReg(cmd, *g_state.cmd_list.current_ptr++, header.parameter_mask);
  461. }
  462. }
  463. }
  464. } // namespace
  465. } // namespace