command_processor.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <boost/range/algorithm/fill.hpp>
  5. #include "common/profiler.h"
  6. #include "clipper.h"
  7. #include "command_processor.h"
  8. #include "math.h"
  9. #include "pica.h"
  10. #include "primitive_assembly.h"
  11. #include "vertex_shader.h"
  12. #include "video_core.h"
  13. #include "core/hle/service/gsp_gpu.h"
  14. #include "core/hw/gpu.h"
  15. #include "core/settings.h"
  16. #include "debug_utils/debug_utils.h"
  17. namespace Pica {
  18. namespace CommandProcessor {
  19. static int float_regs_counter = 0;
  20. static u32 uniform_write_buffer[4];
  21. static int default_attr_counter = 0;
  22. static u32 default_attr_write_buffer[3];
  23. Common::Profiling::TimingCategory category_drawing("Drawing");
  24. static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
  25. auto& regs = g_state.regs;
  26. if (id >= regs.NumIds())
  27. return;
  28. // If we're skipping this frame, only allow trigger IRQ
  29. if (GPU::g_skip_frame && id != PICA_REG_INDEX(trigger_irq))
  30. return;
  31. // TODO: Figure out how register masking acts on e.g. vs_uniform_setup.set_value
  32. u32 old_value = regs[id];
  33. regs[id] = (old_value & ~mask) | (value & mask);
  34. if (g_debug_context)
  35. g_debug_context->OnEvent(DebugContext::Event::CommandLoaded, reinterpret_cast<void*>(&id));
  36. DebugUtils::OnPicaRegWrite(id, regs[id]);
  37. switch(id) {
  38. // Trigger IRQ
  39. case PICA_REG_INDEX(trigger_irq):
  40. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::P3D);
  41. break;
  42. case PICA_REG_INDEX_WORKAROUND(command_buffer.trigger[0], 0x23c):
  43. case PICA_REG_INDEX_WORKAROUND(command_buffer.trigger[1], 0x23d):
  44. {
  45. unsigned index = id - PICA_REG_INDEX(command_buffer.trigger[0]);
  46. u32* head_ptr = (u32*)Memory::GetPhysicalPointer(regs.command_buffer.GetPhysicalAddress(index));
  47. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = head_ptr;
  48. g_state.cmd_list.length = regs.command_buffer.GetSize(index) / sizeof(u32);
  49. break;
  50. }
  51. // It seems like these trigger vertex rendering
  52. case PICA_REG_INDEX(trigger_draw):
  53. case PICA_REG_INDEX(trigger_draw_indexed):
  54. {
  55. Common::Profiling::ScopeTimer scope_timer(category_drawing);
  56. DebugUtils::DumpTevStageConfig(regs.GetTevStages());
  57. if (g_debug_context)
  58. g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  59. const auto& attribute_config = regs.vertex_attributes;
  60. const u32 base_address = attribute_config.GetPhysicalBaseAddress();
  61. // Information about internal vertex attributes
  62. u32 vertex_attribute_sources[16];
  63. boost::fill(vertex_attribute_sources, 0xdeadbeef);
  64. u32 vertex_attribute_strides[16] = {};
  65. Regs::VertexAttributeFormat vertex_attribute_formats[16] = {};
  66. u32 vertex_attribute_elements[16] = {};
  67. u32 vertex_attribute_element_size[16] = {};
  68. // Setup attribute data from loaders
  69. for (int loader = 0; loader < 12; ++loader) {
  70. const auto& loader_config = attribute_config.attribute_loaders[loader];
  71. u32 load_address = base_address + loader_config.data_offset;
  72. // TODO: What happens if a loader overwrites a previous one's data?
  73. for (unsigned component = 0; component < loader_config.component_count; ++component) {
  74. u32 attribute_index = loader_config.GetComponent(component);
  75. vertex_attribute_sources[attribute_index] = load_address;
  76. vertex_attribute_strides[attribute_index] = static_cast<u32>(loader_config.byte_count);
  77. vertex_attribute_formats[attribute_index] = attribute_config.GetFormat(attribute_index);
  78. vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index);
  79. vertex_attribute_element_size[attribute_index] = attribute_config.GetElementSizeInBytes(attribute_index);
  80. load_address += attribute_config.GetStride(attribute_index);
  81. }
  82. }
  83. // Load vertices
  84. bool is_indexed = (id == PICA_REG_INDEX(trigger_draw_indexed));
  85. const auto& index_info = regs.index_array;
  86. const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
  87. const u16* index_address_16 = (u16*)index_address_8;
  88. bool index_u16 = index_info.format != 0;
  89. DebugUtils::GeometryDumper geometry_dumper;
  90. PrimitiveAssembler<VertexShader::OutputVertex> primitive_assembler(regs.triangle_topology.Value());
  91. PrimitiveAssembler<DebugUtils::GeometryDumper::Vertex> dumping_primitive_assembler(regs.triangle_topology.Value());
  92. for (unsigned int index = 0; index < regs.num_vertices; ++index)
  93. {
  94. unsigned int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index;
  95. if (is_indexed) {
  96. // TODO: Implement some sort of vertex cache!
  97. }
  98. // Initialize data for the current vertex
  99. VertexShader::InputVertex input;
  100. // Load a debugging token to check whether this gets loaded by the running
  101. // application or not.
  102. static const float24 debug_token = float24::FromRawFloat24(0x00abcdef);
  103. input.attr[0].w = debug_token;
  104. for (int i = 0; i < attribute_config.GetNumTotalAttributes(); ++i) {
  105. // Load the default attribute if we're configured to do so, this data will be overwritten by the loader data if it's set
  106. if (attribute_config.IsDefaultAttribute(i)) {
  107. input.attr[i] = g_state.vs.default_attributes[i];
  108. LOG_TRACE(HW_GPU, "Loaded default attribute %x for vertex %x (index %x): (%f, %f, %f, %f)",
  109. i, vertex, index,
  110. input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  111. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  112. }
  113. // Load per-vertex data from the loader arrays
  114. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  115. const u8* srcdata = Memory::GetPhysicalPointer(vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i]);
  116. const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata :
  117. (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata :
  118. (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? *(s16*)srcdata :
  119. *(float*)srcdata;
  120. input.attr[i][comp] = float24::FromFloat32(srcval);
  121. LOG_TRACE(HW_GPU, "Loaded component %x of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08lx + 0x%04lx: %f",
  122. comp, i, vertex, index,
  123. attribute_config.GetPhysicalBaseAddress(),
  124. vertex_attribute_sources[i] - base_address,
  125. vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i],
  126. input.attr[i][comp].ToFloat32());
  127. }
  128. }
  129. // HACK: Some games do not initialize the vertex position's w component. This leads
  130. // to critical issues since it messes up perspective division. As a
  131. // workaround, we force the fourth component to 1.0 if we find this to be the
  132. // case.
  133. // To do this, we additionally have to assume that the first input attribute
  134. // is the vertex position, since there's no information about this other than
  135. // the empiric observation that this is usually the case.
  136. if (input.attr[0].w == debug_token)
  137. input.attr[0].w = float24::FromFloat32(1.0);
  138. if (g_debug_context)
  139. g_debug_context->OnEvent(DebugContext::Event::VertexLoaded, (void*)&input);
  140. // NOTE: When dumping geometry, we simply assume that the first input attribute
  141. // corresponds to the position for now.
  142. DebugUtils::GeometryDumper::Vertex dumped_vertex = {
  143. input.attr[0][0].ToFloat32(), input.attr[0][1].ToFloat32(), input.attr[0][2].ToFloat32()
  144. };
  145. using namespace std::placeholders;
  146. dumping_primitive_assembler.SubmitVertex(dumped_vertex,
  147. std::bind(&DebugUtils::GeometryDumper::AddTriangle,
  148. &geometry_dumper, _1, _2, _3));
  149. // Send to vertex shader
  150. VertexShader::OutputVertex output = VertexShader::RunShader(input, attribute_config.GetNumTotalAttributes());
  151. if (is_indexed) {
  152. // TODO: Add processed vertex to vertex cache!
  153. }
  154. if (Settings::values.use_hw_renderer) {
  155. // Send to hardware renderer
  156. static auto AddHWTriangle = [](const Pica::VertexShader::OutputVertex& v0,
  157. const Pica::VertexShader::OutputVertex& v1,
  158. const Pica::VertexShader::OutputVertex& v2) {
  159. VideoCore::g_renderer->hw_rasterizer->AddTriangle(v0, v1, v2);
  160. };
  161. primitive_assembler.SubmitVertex(output, AddHWTriangle);
  162. } else {
  163. // Send to triangle clipper
  164. primitive_assembler.SubmitVertex(output, Clipper::ProcessTriangle);
  165. }
  166. }
  167. if (Settings::values.use_hw_renderer) {
  168. VideoCore::g_renderer->hw_rasterizer->DrawTriangles();
  169. }
  170. geometry_dumper.Dump();
  171. if (g_debug_context)
  172. g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  173. break;
  174. }
  175. case PICA_REG_INDEX(vs_bool_uniforms):
  176. for (unsigned i = 0; i < 16; ++i)
  177. g_state.vs.uniforms.b[i] = (regs.vs_bool_uniforms.Value() & (1 << i)) != 0;
  178. break;
  179. case PICA_REG_INDEX_WORKAROUND(vs_int_uniforms[0], 0x2b1):
  180. case PICA_REG_INDEX_WORKAROUND(vs_int_uniforms[1], 0x2b2):
  181. case PICA_REG_INDEX_WORKAROUND(vs_int_uniforms[2], 0x2b3):
  182. case PICA_REG_INDEX_WORKAROUND(vs_int_uniforms[3], 0x2b4):
  183. {
  184. int index = (id - PICA_REG_INDEX_WORKAROUND(vs_int_uniforms[0], 0x2b1));
  185. auto values = regs.vs_int_uniforms[index];
  186. g_state.vs.uniforms.i[index] = Math::Vec4<u8>(values.x, values.y, values.z, values.w);
  187. LOG_TRACE(HW_GPU, "Set integer uniform %d to %02x %02x %02x %02x",
  188. index, values.x.Value(), values.y.Value(), values.z.Value(), values.w.Value());
  189. break;
  190. }
  191. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[0], 0x2c1):
  192. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[1], 0x2c2):
  193. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[2], 0x2c3):
  194. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[3], 0x2c4):
  195. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[4], 0x2c5):
  196. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[5], 0x2c6):
  197. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[6], 0x2c7):
  198. case PICA_REG_INDEX_WORKAROUND(vs_uniform_setup.set_value[7], 0x2c8):
  199. {
  200. auto& uniform_setup = regs.vs_uniform_setup;
  201. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  202. // it directly write the values?
  203. uniform_write_buffer[float_regs_counter++] = value;
  204. // Uniforms are written in a packed format such that four float24 values are encoded in
  205. // three 32-bit numbers. We write to internal memory once a full such vector is
  206. // written.
  207. if ((float_regs_counter >= 4 && uniform_setup.IsFloat32()) ||
  208. (float_regs_counter >= 3 && !uniform_setup.IsFloat32())) {
  209. float_regs_counter = 0;
  210. auto& uniform = g_state.vs.uniforms.f[uniform_setup.index];
  211. if (uniform_setup.index > 95) {
  212. LOG_ERROR(HW_GPU, "Invalid VS uniform index %d", (int)uniform_setup.index);
  213. break;
  214. }
  215. // NOTE: The destination component order indeed is "backwards"
  216. if (uniform_setup.IsFloat32()) {
  217. for (auto i : {0,1,2,3})
  218. uniform[3 - i] = float24::FromFloat32(*(float*)(&uniform_write_buffer[i]));
  219. } else {
  220. // TODO: Untested
  221. uniform.w = float24::FromRawFloat24(uniform_write_buffer[0] >> 8);
  222. uniform.z = float24::FromRawFloat24(((uniform_write_buffer[0] & 0xFF)<<16) | ((uniform_write_buffer[1] >> 16) & 0xFFFF));
  223. uniform.y = float24::FromRawFloat24(((uniform_write_buffer[1] & 0xFFFF)<<8) | ((uniform_write_buffer[2] >> 24) & 0xFF));
  224. uniform.x = float24::FromRawFloat24(uniform_write_buffer[2] & 0xFFFFFF);
  225. }
  226. LOG_TRACE(HW_GPU, "Set uniform %x to (%f %f %f %f)", (int)uniform_setup.index,
  227. uniform.x.ToFloat32(), uniform.y.ToFloat32(), uniform.z.ToFloat32(),
  228. uniform.w.ToFloat32());
  229. // TODO: Verify that this actually modifies the register!
  230. uniform_setup.index = uniform_setup.index + 1;
  231. }
  232. break;
  233. }
  234. // Load default vertex input attributes
  235. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[0], 0x233):
  236. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[1], 0x234):
  237. case PICA_REG_INDEX_WORKAROUND(vs_default_attributes_setup.set_value[2], 0x235):
  238. {
  239. // TODO: Does actual hardware indeed keep an intermediate buffer or does
  240. // it directly write the values?
  241. default_attr_write_buffer[default_attr_counter++] = value;
  242. // Default attributes are written in a packed format such that four float24 values are encoded in
  243. // three 32-bit numbers. We write to internal memory once a full such vector is
  244. // written.
  245. if (default_attr_counter >= 3) {
  246. default_attr_counter = 0;
  247. auto& setup = regs.vs_default_attributes_setup;
  248. if (setup.index >= 16) {
  249. LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
  250. break;
  251. }
  252. Math::Vec4<float24>& attribute = g_state.vs.default_attributes[setup.index];
  253. // NOTE: The destination component order indeed is "backwards"
  254. attribute.w = float24::FromRawFloat24(default_attr_write_buffer[0] >> 8);
  255. attribute.z = float24::FromRawFloat24(((default_attr_write_buffer[0] & 0xFF) << 16) | ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
  256. attribute.y = float24::FromRawFloat24(((default_attr_write_buffer[1] & 0xFFFF) << 8) | ((default_attr_write_buffer[2] >> 24) & 0xFF));
  257. attribute.x = float24::FromRawFloat24(default_attr_write_buffer[2] & 0xFFFFFF);
  258. LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
  259. attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
  260. attribute.w.ToFloat32());
  261. // TODO: Verify that this actually modifies the register!
  262. setup.index = setup.index + 1;
  263. }
  264. break;
  265. }
  266. // Load shader program code
  267. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[0], 0x2cc):
  268. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[1], 0x2cd):
  269. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[2], 0x2ce):
  270. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[3], 0x2cf):
  271. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[4], 0x2d0):
  272. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[5], 0x2d1):
  273. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[6], 0x2d2):
  274. case PICA_REG_INDEX_WORKAROUND(vs_program.set_word[7], 0x2d3):
  275. {
  276. g_state.vs.program_code[regs.vs_program.offset] = value;
  277. regs.vs_program.offset++;
  278. break;
  279. }
  280. // Load swizzle pattern data
  281. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[0], 0x2d6):
  282. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[1], 0x2d7):
  283. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[2], 0x2d8):
  284. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[3], 0x2d9):
  285. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[4], 0x2da):
  286. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[5], 0x2db):
  287. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[6], 0x2dc):
  288. case PICA_REG_INDEX_WORKAROUND(vs_swizzle_patterns.set_word[7], 0x2dd):
  289. {
  290. g_state.vs.swizzle_data[regs.vs_swizzle_patterns.offset] = value;
  291. regs.vs_swizzle_patterns.offset++;
  292. break;
  293. }
  294. default:
  295. break;
  296. }
  297. VideoCore::g_renderer->hw_rasterizer->NotifyPicaRegisterChanged(id);
  298. if (g_debug_context)
  299. g_debug_context->OnEvent(DebugContext::Event::CommandProcessed, reinterpret_cast<void*>(&id));
  300. }
  301. void ProcessCommandList(const u32* list, u32 size) {
  302. g_state.cmd_list.head_ptr = g_state.cmd_list.current_ptr = list;
  303. g_state.cmd_list.length = size / sizeof(u32);
  304. while (g_state.cmd_list.current_ptr < g_state.cmd_list.head_ptr + g_state.cmd_list.length) {
  305. // Expand a 4-bit mask to 4-byte mask, e.g. 0b0101 -> 0x00FF00FF
  306. static const u32 expand_bits_to_bytes[] = {
  307. 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
  308. 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
  309. 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
  310. 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff
  311. };
  312. // Align read pointer to 8 bytes
  313. if ((g_state.cmd_list.head_ptr - g_state.cmd_list.current_ptr) % 2 != 0)
  314. ++g_state.cmd_list.current_ptr;
  315. u32 value = *g_state.cmd_list.current_ptr++;
  316. const CommandHeader header = { *g_state.cmd_list.current_ptr++ };
  317. const u32 write_mask = expand_bits_to_bytes[header.parameter_mask];
  318. u32 cmd = header.cmd_id;
  319. WritePicaReg(cmd, value, write_mask);
  320. for (unsigned i = 0; i < header.extra_data_length; ++i) {
  321. u32 cmd = header.cmd_id + (header.group_commands ? i + 1 : 0);
  322. WritePicaReg(cmd, *g_state.cmd_list.current_ptr++, write_mask);
  323. }
  324. }
  325. }
  326. } // namespace
  327. } // namespace