CommandBufferBuilder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2022, Sahan Fernando <sahan.h.fernando@gmail.com>
  3. * Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/StringView.h>
  9. #include <Kernel/API/VirGL.h>
  10. #include <sys/ioctl.h>
  11. #include <LibVirtGPU/CommandBufferBuilder.h>
  12. #include <LibVirtGPU/Commands.h>
  13. #include <LibVirtGPU/VirGLProtocol.h>
  14. namespace VirtGPU {
  15. static u32 encode_command(u16 length, Protocol::ObjectType object_type, Protocol::VirGLCommand command)
  16. {
  17. u8 command_value = to_underlying(command);
  18. u8 object_type_value = to_underlying(object_type);
  19. return (length << 16) | (object_type_value << 8) | command_value;
  20. }
  21. class CommandBuilder {
  22. public:
  23. CommandBuilder(Vector<u32>& buffer, Protocol::VirGLCommand command, Protocol::ObjectType object_type)
  24. : m_buffer(buffer)
  25. , m_start_offset(buffer.size())
  26. , m_command(command)
  27. , m_object_type(object_type)
  28. {
  29. m_buffer.append(0);
  30. }
  31. void appendu32(u32 value)
  32. {
  33. VERIFY(!m_finalized);
  34. m_buffer.append(value);
  35. }
  36. void appendf32(float value)
  37. {
  38. VERIFY(!m_finalized);
  39. m_buffer.append(bit_cast<u32>(value));
  40. }
  41. void appendf64(double value)
  42. {
  43. VERIFY(!m_finalized);
  44. m_buffer.append(0);
  45. m_buffer.append(0);
  46. auto* depth = reinterpret_cast<u64*>(&m_buffer[m_buffer.size() - 2]);
  47. *depth = bit_cast<u64>(value);
  48. }
  49. void append_string_null_padded(StringView string)
  50. {
  51. VERIFY(!m_finalized);
  52. // Remember to have at least one null terminator byte
  53. auto length = string.length() + 1;
  54. auto num_required_words = (length + sizeof(u32) - 1) / sizeof(u32);
  55. m_buffer.resize(m_buffer.size() + num_required_words);
  56. char* dest = reinterpret_cast<char*>(&m_buffer[m_buffer.size() - num_required_words]);
  57. memcpy(dest, string.characters_without_null_termination(), string.length());
  58. // Pad end with null bytes
  59. memset(&dest[string.length()], 0, 4 * num_required_words - string.length());
  60. }
  61. void finalize()
  62. {
  63. if (!m_finalized) {
  64. m_finalized = true;
  65. size_t num_elems = m_buffer.size() - m_start_offset - 1;
  66. VERIFY(num_elems <= NumericLimits<u16>::max());
  67. m_buffer[m_start_offset] = encode_command(static_cast<u16>(num_elems), m_object_type, m_command);
  68. }
  69. }
  70. ~CommandBuilder()
  71. {
  72. if (!m_finalized)
  73. finalize();
  74. }
  75. private:
  76. Vector<u32>& m_buffer;
  77. size_t m_start_offset;
  78. Protocol::VirGLCommand m_command;
  79. Protocol::ObjectType m_object_type;
  80. bool m_finalized { false };
  81. };
  82. void CommandBufferBuilder::append_set_tweaks(u32 id, u32 value)
  83. {
  84. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::SET_TWEAKS, Protocol::ObjectType::NONE);
  85. builder.appendu32(id);
  86. builder.appendu32(value);
  87. }
  88. void CommandBufferBuilder::append_transfer3d(Protocol::ResourceID resource, size_t width, size_t height, size_t depth, size_t direction)
  89. {
  90. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::TRANSFER3D, Protocol::ObjectType::NONE);
  91. builder.appendu32(resource.value()); // res_handle
  92. builder.appendu32(0); // level
  93. // FIXME: It is not clear what this magic 242 value does.
  94. // According to https://gitlab.freedesktop.org/virgl/virglrenderer/-/blob/master/src/vrend_decode.c#L1398 it is unused
  95. // But ccapitalK had to specifically set it to prevent rendering failures.
  96. builder.appendu32(242); // usage
  97. builder.appendu32(0); // stride
  98. builder.appendu32(0); // layer_stride
  99. builder.appendu32(0); // x
  100. builder.appendu32(0); // y
  101. builder.appendu32(0); // z
  102. builder.appendu32(width); // width
  103. builder.appendu32(height); // height
  104. builder.appendu32(depth); // depth
  105. builder.appendu32(0); // data_offset
  106. builder.appendu32(direction); // direction
  107. }
  108. void CommandBufferBuilder::append_end_transfers_3d()
  109. {
  110. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::END_TRANSFERS, Protocol::ObjectType::NONE);
  111. }
  112. void CommandBufferBuilder::append_draw_vbo(Protocol::PipePrimitiveTypes primitive_type, u32 count)
  113. {
  114. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::DRAW_VBO, Protocol::ObjectType::NONE);
  115. builder.appendu32(0); // start
  116. builder.appendu32(count); // count
  117. builder.appendu32(to_underlying(primitive_type)); // mode
  118. builder.appendu32(0); // indexed
  119. builder.appendu32(1); // instance_count
  120. builder.appendu32(0); // index_bias
  121. builder.appendu32(0); // start_instance
  122. builder.appendu32(0); // primitive_restart
  123. builder.appendu32(0); // restart_index
  124. builder.appendu32(0); // min_index
  125. builder.appendu32(0xffffffff); // max_index
  126. builder.appendu32(0); // cso
  127. }
  128. void CommandBufferBuilder::append_clear(float r, float g, float b, float a)
  129. {
  130. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CLEAR, Protocol::ObjectType::NONE);
  131. Protocol::ClearType clear_flags {};
  132. clear_flags.flags.color0 = 1;
  133. builder.appendu32(clear_flags.value);
  134. builder.appendf32(r);
  135. builder.appendf32(g);
  136. builder.appendf32(b);
  137. builder.appendf32(a);
  138. builder.appendf64(1.0);
  139. builder.appendu32(0);
  140. }
  141. void CommandBufferBuilder::append_clear(double depth)
  142. {
  143. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CLEAR, Protocol::ObjectType::NONE);
  144. Protocol::ClearType clear_flags {};
  145. clear_flags.flags.depth = 1;
  146. builder.appendu32(clear_flags.value);
  147. builder.appendf32(0);
  148. builder.appendf32(0);
  149. builder.appendf32(0);
  150. builder.appendf32(0);
  151. builder.appendf64(depth);
  152. builder.appendu32(0);
  153. }
  154. void CommandBufferBuilder::append_set_vertex_buffers(u32 stride, u32 offset, Protocol::ResourceID resource)
  155. {
  156. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::SET_VERTEX_BUFFERS, Protocol::ObjectType::NONE);
  157. builder.appendu32(stride);
  158. builder.appendu32(offset);
  159. builder.appendu32(resource.value());
  160. }
  161. void CommandBufferBuilder::append_create_blend(Protocol::ObjectHandle handle)
  162. {
  163. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, Protocol::ObjectType::BLEND);
  164. CreateBlendCommand::S0Flags s0 {};
  165. CreateBlendCommand::S1Flags s1 {};
  166. CreateBlendCommand::S2Flags s2 {};
  167. s0.dither = 1;
  168. s2.colormask = 0xf;
  169. builder.appendu32(handle.value());
  170. builder.appendu32(s0.u32_value);
  171. builder.appendu32(s1.u32_value);
  172. builder.appendu32(s2.u32_value);
  173. for (size_t i = 1; i < 8; ++i) {
  174. builder.appendu32(0); // Explicitly disable all flags for other color buffers
  175. }
  176. }
  177. void CommandBufferBuilder::append_bind_blend(Protocol::ObjectHandle handle)
  178. {
  179. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::BIND_OBJECT, Protocol::ObjectType::BLEND);
  180. builder.appendu32(handle.value()); // VIRGL_OBJ_BIND_HANDLE
  181. }
  182. void CommandBufferBuilder::append_create_vertex_elements(Protocol::ObjectHandle handle, Vector<CreateVertexElementsCommand::ElementBinding> const& bindings)
  183. {
  184. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, Protocol::ObjectType::VERTEX_ELEMENTS);
  185. builder.appendu32(handle.value());
  186. for (auto& binding : bindings) {
  187. builder.appendu32(binding.offset);
  188. builder.appendu32(binding.divisor);
  189. builder.appendu32(binding.vertex_buffer_index);
  190. builder.appendu32(to_underlying(binding.format));
  191. }
  192. }
  193. void CommandBufferBuilder::append_bind_vertex_elements(Protocol::ObjectHandle handle)
  194. {
  195. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::BIND_OBJECT, Protocol::ObjectType::VERTEX_ELEMENTS);
  196. builder.appendu32(handle.value()); // VIRGL_OBJ_BIND_HANDLE
  197. }
  198. void CommandBufferBuilder::append_create_surface(Protocol::ResourceID drawtarget_resource, Protocol::ObjectHandle drawtarget_handle, Protocol::TextureFormat format)
  199. {
  200. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, Protocol::ObjectType::SURFACE);
  201. builder.appendu32(drawtarget_handle.value());
  202. builder.appendu32(drawtarget_resource.value());
  203. builder.appendu32(to_underlying(format));
  204. builder.appendu32(0); // First element / Texture Level
  205. builder.appendu32(0); // Last element / Texture Element
  206. }
  207. void CommandBufferBuilder::append_set_framebuffer_state(Protocol::ObjectHandle drawtarget, Protocol::ObjectHandle depthbuffer)
  208. {
  209. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::SET_FRAMEBUFFER_STATE, Protocol::ObjectType::NONE);
  210. builder.appendu32(1); // nr_cbufs
  211. builder.appendu32(depthbuffer.value()); // zsurf_handle
  212. builder.appendu32(drawtarget.value()); // surf_handle
  213. }
  214. void CommandBufferBuilder::append_viewport(Gfx::IntSize size)
  215. {
  216. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::SET_VIEWPORT_STATE, Protocol::ObjectType::NONE);
  217. builder.appendu32(0);
  218. builder.appendf32(size.width() / 2); // scale_x
  219. builder.appendf32(size.height() / 2); // scale_y (flipped, due to VirGL being different from our coordinate space)
  220. builder.appendf32(0.5f); // scale_z
  221. builder.appendf32(size.width() / 2); // translate_x
  222. builder.appendf32(size.height() / 2); // translate_y
  223. builder.appendf32(0.5f); // translate_z
  224. }
  225. void CommandBufferBuilder::append_set_framebuffer_state_no_attach(Gfx::IntSize size)
  226. {
  227. VERIFY(size.width() <= NumericLimits<u16>::max());
  228. VERIFY(size.height() <= NumericLimits<u16>::max());
  229. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::SET_FRAMEBUFFER_STATE_NO_ATTACH, Protocol::ObjectType::NONE);
  230. u16 samples = 0;
  231. u16 layers = 0;
  232. builder.appendu32((size.height() << 16) | size.width());
  233. builder.appendu32((samples << 16) | layers);
  234. }
  235. void CommandBufferBuilder::append_set_constant_buffer(Vector<float> const& constant_buffer)
  236. {
  237. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::SET_CONSTANT_BUFFER, Protocol::ObjectType::NONE);
  238. builder.appendu32(to_underlying(Gallium::ShaderType::SHADER_VERTEX));
  239. builder.appendu32(0); // index (currently unused according to virglrenderer source code)
  240. for (auto v : constant_buffer) {
  241. builder.appendf32(v);
  242. }
  243. }
  244. void CommandBufferBuilder::append_create_shader(Protocol::ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data)
  245. {
  246. size_t shader_len = shader_data.length() + 1; // Need to remember to copy null terminator as well if needed
  247. VERIFY(shader_len <= NumericLimits<u32>::max());
  248. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, Protocol::ObjectType::SHADER);
  249. builder.appendu32(handle.value()); // VIRGL_OBJ_CREATE_HANDLE
  250. builder.appendu32(to_underlying(shader_type));
  251. builder.appendu32(0); // VIRGL_OBJ_SHADER_OFFSET
  252. builder.appendu32(shader_len);
  253. builder.appendu32(0); // VIRGL_OBJ_SHADER_NUM_TOKENS
  254. builder.append_string_null_padded(shader_data);
  255. }
  256. void CommandBufferBuilder::append_bind_shader(Protocol::ObjectHandle handle, Gallium::ShaderType shader_type)
  257. {
  258. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::BIND_SHADER, Protocol::ObjectType::NONE);
  259. builder.appendu32(handle.value()); // VIRGL_OBJ_BIND_HANDLE
  260. builder.appendu32(to_underlying(shader_type));
  261. }
  262. void CommandBufferBuilder::append_create_rasterizer(Protocol::ObjectHandle handle)
  263. {
  264. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, Protocol::ObjectType::RASTERIZER);
  265. CreateRasterizerCommand::S0Flags s0 {};
  266. CreateRasterizerCommand::S3Flags s3 {};
  267. s0.depth_clip = 1;
  268. builder.appendu32(handle.value()); // Handle
  269. builder.appendu32(s0.u32_value); // S0 (bitfield of state bits)
  270. builder.appendf32(1.0); // Point size
  271. builder.appendu32(0); // Sprite coord enable
  272. builder.appendu32(s3.u32_value); // S3 (bitfield of state bits)
  273. builder.appendf32(0.1); // Line width
  274. builder.appendf32(0.0); // Offset units
  275. builder.appendf32(0.0); // offset scale
  276. builder.appendf32(0.0); // Offset clamp
  277. }
  278. void CommandBufferBuilder::append_bind_rasterizer(Protocol::ObjectHandle handle)
  279. {
  280. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::BIND_OBJECT, Protocol::ObjectType::RASTERIZER);
  281. builder.appendu32(handle.value()); // VIRGL_OBJ_BIND_HANDLE
  282. }
  283. void CommandBufferBuilder::append_create_dsa(Protocol::ObjectHandle handle)
  284. {
  285. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, Protocol::ObjectType::DSA);
  286. CreateDSACommand::S0Flags s0 {};
  287. CreateDSACommand::S1Flags s1[2] {};
  288. s0.depth_enabled = 1;
  289. s0.depth_writemask = 1;
  290. s0.depth_func = 1;
  291. builder.appendu32(handle.value()); // Handle
  292. builder.appendu32(s0.u32_value); // S0 (bitset for depth buffer)
  293. builder.appendu32(s1[0].u32_value); // S1 (bitset for 1st stencil buffer)
  294. builder.appendu32(s1[1].u32_value); // S2 (bitset for 2nd stencil buffer)
  295. builder.appendf32(1.0); // Alpha Ref
  296. }
  297. void CommandBufferBuilder::append_bind_dsa(Protocol::ObjectHandle handle)
  298. {
  299. CommandBuilder builder(m_buffer, Protocol::VirGLCommand::BIND_OBJECT, Protocol::ObjectType::DSA);
  300. builder.appendu32(handle.value()); // VIRGL_OBJ_BIND_HANDLE
  301. }
  302. }