command_processor.cpp 28 KB

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