Device.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2022, Sahan Fernando <sahan.h.fernando@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <Kernel/API/VirGL.h>
  10. #include <LibCore/System.h>
  11. #include <LibVirtGPU/CommandBufferBuilder.h>
  12. #include <LibVirtGPU/Device.h>
  13. #include <LibVirtGPU/Image.h>
  14. #include <LibVirtGPU/Shader.h>
  15. #include <LibVirtGPU/VirGLProtocol.h>
  16. namespace VirtGPU {
  17. static constexpr auto frag_shader = "FRAG\n"
  18. "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n"
  19. "DCL IN[0], COLOR, COLOR\n"
  20. "DCL OUT[0], COLOR\n"
  21. " 0: MOV OUT[0], IN[0]\n"
  22. " 1: END\n"sv;
  23. static constexpr auto vert_shader = "VERT\n"
  24. "DCL IN[0]\n"
  25. "DCL IN[1]\n"
  26. "DCL OUT[0], POSITION\n"
  27. "DCL OUT[1], COLOR\n"
  28. "DCL CONST[0..3]\n"
  29. "DCL TEMP[0..1]\n"
  30. " 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n"
  31. " 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n"
  32. " 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n"
  33. " 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n"
  34. " 4: MOV_SAT OUT[1], IN[1]\n"
  35. " 5: END\n"sv;
  36. Device::Device(NonnullRefPtr<Core::File> gpu_file)
  37. : m_gpu_file { gpu_file }
  38. {
  39. }
  40. ErrorOr<NonnullOwnPtr<Device>> Device::create(Gfx::IntSize min_size)
  41. {
  42. auto file = TRY(Core::File::open("/dev/gpu/render0", Core::OpenMode::ReadWrite));
  43. auto device = make<Device>(file);
  44. TRY(device->initialize_context(min_size));
  45. return device;
  46. }
  47. ErrorOr<void> Device::initialize_context(Gfx::IntSize min_size)
  48. {
  49. // Create a virgl context for this file descriptor
  50. TRY(Core::System::ioctl(m_gpu_file->fd(), VIRGL_IOCTL_CREATE_CONTEXT));
  51. // Create a VertexElements resource
  52. VirGL3DResourceSpec vbo_spec {
  53. .target = to_underlying(Gallium::PipeTextureTarget::BUFFER), // pipe_texture_target
  54. .format = 0, // untyped buffer
  55. .bind = to_underlying(Protocol::BindTarget::VIRGL_BIND_VERTEX_BUFFER),
  56. .width = PAGE_SIZE * 256,
  57. .height = 1,
  58. .depth = 1,
  59. .array_size = 1,
  60. .last_level = 0,
  61. .nr_samples = 0,
  62. .flags = 0,
  63. .created_resource_id = 0,
  64. };
  65. m_vbo_resource_id = TRY(create_virgl_resource(vbo_spec));
  66. // Create a texture to draw to
  67. VirGL3DResourceSpec drawtarget_spec {
  68. .target = to_underlying(Gallium::PipeTextureTarget::TEXTURE_RECT), // pipe_texture_target
  69. .format = to_underlying(Protocol::TextureFormat::VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM), // pipe_to_virgl_format
  70. .bind = to_underlying(Protocol::BindTarget::VIRGL_BIND_RENDER_TARGET),
  71. .width = static_cast<u32>(min_size.width()),
  72. .height = static_cast<u32>(min_size.height()),
  73. .depth = 1,
  74. .array_size = 1,
  75. .last_level = 0,
  76. .nr_samples = 0,
  77. .flags = 0,
  78. .created_resource_id = 0,
  79. };
  80. m_drawtarget = TRY(create_virgl_resource(drawtarget_spec));
  81. // Create a depthbuffer surface
  82. VirGL3DResourceSpec depthbuffer_surface_spec {
  83. .target = to_underlying(Gallium::PipeTextureTarget::TEXTURE_RECT), // pipe_texture_target
  84. .format = to_underlying(Protocol::TextureFormat::VIRTIO_GPU_FORMAT_Z32_FLOAT), // pipe_to_virgl_format
  85. .bind = to_underlying(Protocol::BindTarget::VIRGL_BIND_RENDER_TARGET) | to_underlying(Protocol::BindTarget::VIRGL_BIND_DEPTH_STENCIL),
  86. .width = static_cast<u32>(min_size.width()),
  87. .height = static_cast<u32>(min_size.height()),
  88. .depth = 1,
  89. .array_size = 1,
  90. .last_level = 0,
  91. .nr_samples = 0,
  92. .flags = 0,
  93. .created_resource_id = 0,
  94. };
  95. m_depthbuffer_surface = TRY(create_virgl_resource(depthbuffer_surface_spec));
  96. // Initialize all required state
  97. CommandBufferBuilder builder;
  98. // Create and set the blend, to control the color mask
  99. m_blend_handle = allocate_handle();
  100. builder.append_create_blend(m_blend_handle);
  101. builder.append_bind_blend(m_blend_handle);
  102. // Create drawtarget surface
  103. m_drawtarget_surface_handle = allocate_handle();
  104. builder.append_create_surface(m_drawtarget, m_drawtarget_surface_handle, Protocol::TextureFormat::VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM);
  105. // Create depthbuffer surface
  106. m_depthbuffer_surface_handle = allocate_handle();
  107. builder.append_create_surface(m_depthbuffer_surface, m_depthbuffer_surface_handle, Protocol::TextureFormat::VIRTIO_GPU_FORMAT_Z32_FLOAT);
  108. // Set some framebuffer state (attached handle, framebuffer size, etc)
  109. builder.append_set_framebuffer_state(m_drawtarget_surface_handle, m_depthbuffer_surface_handle);
  110. builder.append_set_framebuffer_state_no_attach(min_size);
  111. // Set the vertex buffer
  112. builder.append_set_vertex_buffers(sizeof(VertexData), 0, m_vbo_resource_id);
  113. // Create and bind fragment shader
  114. m_frag_shader_handle = allocate_handle();
  115. builder.append_create_shader(m_frag_shader_handle, Gallium::ShaderType::SHADER_FRAGMENT, frag_shader);
  116. builder.append_bind_shader(m_frag_shader_handle, Gallium::ShaderType::SHADER_FRAGMENT);
  117. // Create and bind vertex shader
  118. m_vert_shader_handle = allocate_handle();
  119. builder.append_create_shader(m_vert_shader_handle, Gallium::ShaderType::SHADER_VERTEX, vert_shader);
  120. builder.append_bind_shader(m_vert_shader_handle, Gallium::ShaderType::SHADER_VERTEX);
  121. // Create a VertexElements object (used to specify layout of vertex data)
  122. m_ve_handle = allocate_handle();
  123. Vector<CreateVertexElementsCommand::ElementBinding> element_bindings {
  124. { .offset = 12, .divisor = 0, .vertex_buffer_index = 0, .format = Gallium::PipeFormat::R32G32B32_FLOAT },
  125. { .offset = 0, .divisor = 0, .vertex_buffer_index = 0, .format = Gallium::PipeFormat::R32G32B32_FLOAT },
  126. };
  127. builder.append_create_vertex_elements(m_ve_handle, element_bindings);
  128. builder.append_bind_vertex_elements(m_ve_handle);
  129. // Create a DepthStencilAlpha (DSA) object
  130. m_dsa_handle = allocate_handle();
  131. builder.append_create_dsa(m_dsa_handle);
  132. builder.append_bind_dsa(m_dsa_handle);
  133. // Create a Rasterizer object
  134. m_rasterizer_handle = allocate_handle();
  135. builder.append_create_rasterizer(m_rasterizer_handle);
  136. builder.append_bind_rasterizer(m_rasterizer_handle);
  137. // Set the Viewport
  138. builder.append_viewport(min_size);
  139. // Upload buffer
  140. TRY(upload_command_buffer(builder.build()));
  141. return {};
  142. }
  143. GPU::DeviceInfo Device::info() const
  144. {
  145. return {
  146. .vendor_name = "SerenityOS",
  147. .device_name = "VirtGPU",
  148. .num_texture_units = GPU::NUM_TEXTURE_UNITS,
  149. .num_lights = 8,
  150. .max_clip_planes = 6,
  151. .max_texture_size = 4096,
  152. .max_texture_lod_bias = 2.f,
  153. .stencil_bits = sizeof(GPU::StencilType) * 8,
  154. .supports_npot_textures = true,
  155. .supports_texture_clamp_to_edge = true,
  156. .supports_texture_env_add = true,
  157. };
  158. }
  159. void Device::draw_primitives(GPU::PrimitiveType, FloatMatrix4x4 const&, FloatMatrix4x4 const&, Vector<GPU::Vertex>&)
  160. {
  161. dbgln("VirtGPU::Device::draw_primitives(): unimplemented");
  162. }
  163. void Device::resize(Gfx::IntSize)
  164. {
  165. dbgln("VirtGPU::Device::resize(): unimplemented");
  166. }
  167. void Device::clear_color(FloatVector4 const&)
  168. {
  169. dbgln("VirtGPU::Device::clear_color(): unimplemented");
  170. }
  171. void Device::clear_depth(GPU::DepthType)
  172. {
  173. dbgln("VirtGPU::Device::clear_depth(): unimplemented");
  174. }
  175. void Device::clear_stencil(GPU::StencilType)
  176. {
  177. dbgln("VirtGPU::Device::clear_stencil(): unimplemented");
  178. }
  179. void Device::blit_from_color_buffer(Gfx::Bitmap&)
  180. {
  181. dbgln("VirtGPU::Device::blit_from_color_buffer(): unimplemented");
  182. }
  183. void Device::blit_from_color_buffer(NonnullRefPtr<GPU::Image>, u32, Vector2<u32>, Vector2<i32>, Vector3<i32>)
  184. {
  185. dbgln("VirtGPU::Device::blit_from_color_buffer(): unimplemented");
  186. }
  187. void Device::blit_from_color_buffer(void*, Vector2<i32>, GPU::ImageDataLayout const&)
  188. {
  189. dbgln("VirtGPU::Device::blit_from_color_buffer(): unimplemented");
  190. }
  191. void Device::blit_from_depth_buffer(void*, Vector2<i32>, GPU::ImageDataLayout const&)
  192. {
  193. dbgln("VirtGPU::Device::blit_from_depth_buffer(): unimplemented");
  194. }
  195. void Device::blit_from_depth_buffer(NonnullRefPtr<GPU::Image>, u32, Vector2<u32>, Vector2<i32>, Vector3<i32>)
  196. {
  197. dbgln("VirtGPU::Device::blit_from_depth_buffer(): unimplemented");
  198. }
  199. void Device::blit_to_color_buffer_at_raster_position(void const*, GPU::ImageDataLayout const&)
  200. {
  201. dbgln("VirtGPU::Device::blit_to_color_buffer_at_raster_position(): unimplemented");
  202. }
  203. void Device::blit_to_depth_buffer_at_raster_position(void const*, GPU::ImageDataLayout const&)
  204. {
  205. dbgln("VirtGPU::Device::blit_to_depth_buffer_at_raster_position(): unimplemented");
  206. }
  207. void Device::set_options(GPU::RasterizerOptions const&)
  208. {
  209. dbgln("VirtGPU::Device::set_options(): unimplemented");
  210. }
  211. void Device::set_light_model_params(GPU::LightModelParameters const&)
  212. {
  213. dbgln("VirtGPU::Device::set_light_model_params(): unimplemented");
  214. }
  215. GPU::RasterizerOptions Device::options() const
  216. {
  217. dbgln("VirtGPU::Device::options(): unimplemented");
  218. return {};
  219. }
  220. GPU::LightModelParameters Device::light_model() const
  221. {
  222. dbgln("VirtGPU::Device::light_model(): unimplemented");
  223. return {};
  224. }
  225. NonnullRefPtr<GPU::Image> Device::create_image(GPU::PixelFormat const& pixel_format, u32 width, u32 height, u32 depth, u32 max_levels)
  226. {
  227. dbgln("VirtGPU::Device::create_image(): unimplemented");
  228. return adopt_ref(*new Image(this, pixel_format, width, height, depth, max_levels));
  229. }
  230. ErrorOr<NonnullRefPtr<GPU::Shader>> Device::create_shader(GPU::IR::Shader const&)
  231. {
  232. dbgln("VirtGPU::Device::create_shader(): unimplemented");
  233. return adopt_ref(*new Shader(this));
  234. }
  235. void Device::set_sampler_config(unsigned, GPU::SamplerConfig const&)
  236. {
  237. dbgln("VirtGPU::Device::set_sampler_config(): unimplemented");
  238. }
  239. void Device::set_light_state(unsigned, GPU::Light const&)
  240. {
  241. dbgln("VirtGPU::Device::set_light_state(): unimplemented");
  242. }
  243. void Device::set_material_state(GPU::Face, GPU::Material const&)
  244. {
  245. dbgln("VirtGPU::Device::set_material_state(): unimplemented");
  246. }
  247. void Device::set_stencil_configuration(GPU::Face, GPU::StencilConfiguration const&)
  248. {
  249. dbgln("VirtGPU::Device::set_stencil_configuration(): unimplemented");
  250. }
  251. void Device::set_texture_unit_configuration(GPU::TextureUnitIndex, GPU::TextureUnitConfiguration const&)
  252. {
  253. dbgln("VirtGPU::Device::set_texture_unit_configuration(): unimplemented");
  254. }
  255. void Device::set_clip_planes(Vector<FloatVector4> const&)
  256. {
  257. dbgln("VirtGPU::Device::set_clip_planes(): unimplemented");
  258. }
  259. GPU::RasterPosition Device::raster_position() const
  260. {
  261. dbgln("VirtGPU::Device::raster_position(): unimplemented");
  262. return {};
  263. }
  264. void Device::set_raster_position(GPU::RasterPosition const&)
  265. {
  266. dbgln("VirtGPU::Device::set_raster_position(): unimplemented");
  267. }
  268. void Device::set_raster_position(FloatVector4 const&, FloatMatrix4x4 const&, FloatMatrix4x4 const&)
  269. {
  270. dbgln("VirtGPU::Device::set_raster_position(): unimplemented");
  271. }
  272. void Device::bind_fragment_shader(RefPtr<GPU::Shader>)
  273. {
  274. dbgln("VirtGPU::Device::bind_fragment_shader(): unimplemented");
  275. }
  276. Protocol::ObjectHandle Device::allocate_handle()
  277. {
  278. return { ++m_last_allocated_handle };
  279. }
  280. ErrorOr<void> Device::upload_command_buffer(Vector<u32> const& command_buffer)
  281. {
  282. VERIFY(command_buffer.size() <= NumericLimits<u32>::max());
  283. VirGLCommandBuffer command_buffer_descriptor {
  284. .data = command_buffer.data(),
  285. .num_elems = static_cast<u32>(command_buffer.size()),
  286. };
  287. TRY(Core::System::ioctl(m_gpu_file->fd(), VIRGL_IOCTL_SUBMIT_CMD, &command_buffer_descriptor));
  288. return {};
  289. }
  290. ErrorOr<Protocol::ResourceID> Device::create_virgl_resource(VirGL3DResourceSpec& spec)
  291. {
  292. TRY(Core::System::ioctl(m_gpu_file->fd(), VIRGL_IOCTL_CREATE_RESOURCE, &spec));
  293. return Protocol::ResourceID { spec.created_resource_id };
  294. }
  295. }
  296. extern "C" GPU::Device* serenity_gpu_create_device(Gfx::IntSize size)
  297. {
  298. auto device_or_error = VirtGPU::Device::create(size);
  299. if (device_or_error.is_error())
  300. return nullptr;
  301. return device_or_error.release_value().leak_ptr();
  302. }