command_processor.cpp 19 KB

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