command_processor.cpp 19 KB

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