Device.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include <AK/SIMDExtras.h>
  9. #include <AK/SIMDMath.h>
  10. #include <LibCore/ElapsedTimer.h>
  11. #include <LibGfx/Painter.h>
  12. #include <LibGfx/Vector2.h>
  13. #include <LibGfx/Vector3.h>
  14. #include <LibSoftGPU/Config.h>
  15. #include <LibSoftGPU/Device.h>
  16. #include <LibSoftGPU/PixelQuad.h>
  17. #include <LibSoftGPU/SIMD.h>
  18. namespace SoftGPU {
  19. static long long g_num_rasterized_triangles;
  20. static long long g_num_pixels;
  21. static long long g_num_pixels_shaded;
  22. static long long g_num_pixels_blended;
  23. static long long g_num_sampler_calls;
  24. static long long g_num_quads;
  25. using IntVector2 = Gfx::Vector2<int>;
  26. using IntVector3 = Gfx::Vector3<int>;
  27. using AK::SIMD::any;
  28. using AK::SIMD::exp;
  29. using AK::SIMD::expand4;
  30. using AK::SIMD::f32x4;
  31. using AK::SIMD::i32x4;
  32. using AK::SIMD::load4_masked;
  33. using AK::SIMD::maskbits;
  34. using AK::SIMD::maskcount;
  35. using AK::SIMD::none;
  36. using AK::SIMD::store4_masked;
  37. using AK::SIMD::to_f32x4;
  38. using AK::SIMD::to_u32x4;
  39. using AK::SIMD::u32x4;
  40. constexpr static int edge_function(const IntVector2& a, const IntVector2& b, const IntVector2& c)
  41. {
  42. return ((c.x() - a.x()) * (b.y() - a.y()) - (c.y() - a.y()) * (b.x() - a.x()));
  43. }
  44. constexpr static i32x4 edge_function4(const IntVector2& a, const IntVector2& b, const Vector2<i32x4>& c)
  45. {
  46. return ((c.x() - a.x()) * (b.y() - a.y()) - (c.y() - a.y()) * (b.x() - a.x()));
  47. }
  48. template<typename T, typename U>
  49. constexpr static auto interpolate(const T& v0, const T& v1, const T& v2, const Vector3<U>& barycentric_coords)
  50. {
  51. return v0 * barycentric_coords.x() + v1 * barycentric_coords.y() + v2 * barycentric_coords.z();
  52. }
  53. ALWAYS_INLINE static u32x4 to_rgba32(const Vector4<f32x4>& v)
  54. {
  55. auto clamped = v.clamped(expand4(0.0f), expand4(1.0f));
  56. auto r = to_u32x4(clamped.x() * 255);
  57. auto g = to_u32x4(clamped.y() * 255);
  58. auto b = to_u32x4(clamped.z() * 255);
  59. auto a = to_u32x4(clamped.w() * 255);
  60. return a << 24 | r << 16 | g << 8 | b;
  61. }
  62. static Vector4<f32x4> to_vec4(u32x4 rgba)
  63. {
  64. auto constexpr one_over_255 = expand4(1.0f / 255);
  65. return {
  66. to_f32x4((rgba >> 16) & 0xff) * one_over_255,
  67. to_f32x4((rgba >> 8) & 0xff) * one_over_255,
  68. to_f32x4(rgba & 0xff) * one_over_255,
  69. to_f32x4((rgba >> 24) & 0xff) * one_over_255,
  70. };
  71. }
  72. static Gfx::IntRect scissor_box_to_window_coordinates(Gfx::IntRect const& scissor_box, Gfx::IntRect const& window_rect)
  73. {
  74. return scissor_box.translated(0, window_rect.height() - 2 * scissor_box.y() - scissor_box.height());
  75. }
  76. void Device::setup_blend_factors()
  77. {
  78. m_alpha_blend_factors.src_constant = { 0.0f, 0.0f, 0.0f, 0.0f };
  79. m_alpha_blend_factors.src_factor_src_alpha = 0;
  80. m_alpha_blend_factors.src_factor_dst_alpha = 0;
  81. m_alpha_blend_factors.src_factor_src_color = 0;
  82. m_alpha_blend_factors.src_factor_dst_color = 0;
  83. switch (m_options.blend_source_factor) {
  84. case BlendFactor::Zero:
  85. break;
  86. case BlendFactor::One:
  87. m_alpha_blend_factors.src_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  88. break;
  89. case BlendFactor::SrcColor:
  90. m_alpha_blend_factors.src_factor_src_color = 1;
  91. break;
  92. case BlendFactor::OneMinusSrcColor:
  93. m_alpha_blend_factors.src_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  94. m_alpha_blend_factors.src_factor_src_color = -1;
  95. break;
  96. case BlendFactor::SrcAlpha:
  97. m_alpha_blend_factors.src_factor_src_alpha = 1;
  98. break;
  99. case BlendFactor::OneMinusSrcAlpha:
  100. m_alpha_blend_factors.src_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  101. m_alpha_blend_factors.src_factor_src_alpha = -1;
  102. break;
  103. case BlendFactor::DstAlpha:
  104. m_alpha_blend_factors.src_factor_dst_alpha = 1;
  105. break;
  106. case BlendFactor::OneMinusDstAlpha:
  107. m_alpha_blend_factors.src_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  108. m_alpha_blend_factors.src_factor_dst_alpha = -1;
  109. break;
  110. case BlendFactor::DstColor:
  111. m_alpha_blend_factors.src_factor_dst_color = 1;
  112. break;
  113. case BlendFactor::OneMinusDstColor:
  114. m_alpha_blend_factors.src_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  115. m_alpha_blend_factors.src_factor_dst_color = -1;
  116. break;
  117. case BlendFactor::SrcAlphaSaturate:
  118. default:
  119. VERIFY_NOT_REACHED();
  120. }
  121. m_alpha_blend_factors.dst_constant = { 0.0f, 0.0f, 0.0f, 0.0f };
  122. m_alpha_blend_factors.dst_factor_src_alpha = 0;
  123. m_alpha_blend_factors.dst_factor_dst_alpha = 0;
  124. m_alpha_blend_factors.dst_factor_src_color = 0;
  125. m_alpha_blend_factors.dst_factor_dst_color = 0;
  126. switch (m_options.blend_destination_factor) {
  127. case BlendFactor::Zero:
  128. break;
  129. case BlendFactor::One:
  130. m_alpha_blend_factors.dst_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  131. break;
  132. case BlendFactor::SrcColor:
  133. m_alpha_blend_factors.dst_factor_src_color = 1;
  134. break;
  135. case BlendFactor::OneMinusSrcColor:
  136. m_alpha_blend_factors.dst_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  137. m_alpha_blend_factors.dst_factor_src_color = -1;
  138. break;
  139. case BlendFactor::SrcAlpha:
  140. m_alpha_blend_factors.dst_factor_src_alpha = 1;
  141. break;
  142. case BlendFactor::OneMinusSrcAlpha:
  143. m_alpha_blend_factors.dst_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  144. m_alpha_blend_factors.dst_factor_src_alpha = -1;
  145. break;
  146. case BlendFactor::DstAlpha:
  147. m_alpha_blend_factors.dst_factor_dst_alpha = 1;
  148. break;
  149. case BlendFactor::OneMinusDstAlpha:
  150. m_alpha_blend_factors.dst_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  151. m_alpha_blend_factors.dst_factor_dst_alpha = -1;
  152. break;
  153. case BlendFactor::DstColor:
  154. m_alpha_blend_factors.dst_factor_dst_color = 1;
  155. break;
  156. case BlendFactor::OneMinusDstColor:
  157. m_alpha_blend_factors.dst_constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  158. m_alpha_blend_factors.dst_factor_dst_color = -1;
  159. break;
  160. case BlendFactor::SrcAlphaSaturate:
  161. default:
  162. VERIFY_NOT_REACHED();
  163. }
  164. }
  165. void Device::rasterize_triangle(const Triangle& triangle)
  166. {
  167. INCREASE_STATISTICS_COUNTER(g_num_rasterized_triangles, 1);
  168. // Since the algorithm is based on blocks of uniform size, we need
  169. // to ensure that our m_render_target size is actually a multiple of the block size
  170. VERIFY((m_render_target->width() % 2) == 0);
  171. VERIFY((m_render_target->height() % 2) == 0);
  172. // Return if alpha testing is a no-op
  173. if (m_options.enable_alpha_test && m_options.alpha_test_func == AlphaTestFunction::Never)
  174. return;
  175. // Vertices
  176. Vertex const vertex0 = triangle.vertices[0];
  177. Vertex const vertex1 = triangle.vertices[1];
  178. Vertex const vertex2 = triangle.vertices[2];
  179. constexpr int subpixel_factor = 1 << SUBPIXEL_BITS;
  180. // Calculate area of the triangle for later tests
  181. IntVector2 const v0 { static_cast<int>(vertex0.window_coordinates.x() * subpixel_factor), static_cast<int>(vertex0.window_coordinates.y() * subpixel_factor) };
  182. IntVector2 const v1 { static_cast<int>(vertex1.window_coordinates.x() * subpixel_factor), static_cast<int>(vertex1.window_coordinates.y() * subpixel_factor) };
  183. IntVector2 const v2 { static_cast<int>(vertex2.window_coordinates.x() * subpixel_factor), static_cast<int>(vertex2.window_coordinates.y() * subpixel_factor) };
  184. int area = edge_function(v0, v1, v2);
  185. if (area == 0)
  186. return;
  187. auto const one_over_area = 1.0f / area;
  188. auto render_bounds = m_render_target->rect();
  189. auto window_scissor_rect = scissor_box_to_window_coordinates(m_options.scissor_box, m_render_target->rect());
  190. if (m_options.scissor_enabled)
  191. render_bounds.intersect(window_scissor_rect);
  192. // Obey top-left rule:
  193. // This sets up "zero" for later pixel coverage tests.
  194. // Depending on where on the triangle the edge is located
  195. // it is either tested against 0 or 1, effectively
  196. // turning "< 0" into "<= 0"
  197. IntVector3 zero { 1, 1, 1 };
  198. if (v1.y() > v0.y() || (v1.y() == v0.y() && v1.x() < v0.x()))
  199. zero.set_z(0);
  200. if (v2.y() > v1.y() || (v2.y() == v1.y() && v2.x() < v1.x()))
  201. zero.set_x(0);
  202. if (v0.y() > v2.y() || (v0.y() == v2.y() && v0.x() < v2.x()))
  203. zero.set_y(0);
  204. // This function calculates the 3 edge values for the pixel relative to the triangle.
  205. auto calculate_edge_values4 = [v0, v1, v2](const Vector2<i32x4>& p) -> Vector3<i32x4> {
  206. return {
  207. edge_function4(v1, v2, p),
  208. edge_function4(v2, v0, p),
  209. edge_function4(v0, v1, p),
  210. };
  211. };
  212. // This function tests whether a point as identified by its 3 edge values lies within the triangle
  213. auto test_point4 = [zero](const Vector3<i32x4>& edges) -> i32x4 {
  214. return edges.x() >= zero.x()
  215. && edges.y() >= zero.y()
  216. && edges.z() >= zero.z();
  217. };
  218. auto test_scissor4 = [window_scissor_rect](const Vector2<i32x4>& screen_coordinates) -> i32x4 {
  219. return screen_coordinates.x() >= window_scissor_rect.x()
  220. && screen_coordinates.x() < window_scissor_rect.x() + window_scissor_rect.width()
  221. && screen_coordinates.y() >= window_scissor_rect.y()
  222. && screen_coordinates.y() < window_scissor_rect.y() + window_scissor_rect.height();
  223. };
  224. // Calculate block-based bounds
  225. // clang-format off
  226. int const bx0 = max(render_bounds.left(), min(min(v0.x(), v1.x()), v2.x()) / subpixel_factor) & ~1;
  227. int const bx1 = (min(render_bounds.right(), max(max(v0.x(), v1.x()), v2.x()) / subpixel_factor) & ~1) + 2;
  228. int const by0 = max(render_bounds.top(), min(min(v0.y(), v1.y()), v2.y()) / subpixel_factor) & ~1;
  229. int const by1 = (min(render_bounds.bottom(), max(max(v0.y(), v1.y()), v2.y()) / subpixel_factor) & ~1) + 2;
  230. // clang-format on
  231. // Fog depths
  232. float const vertex0_eye_absz = fabs(vertex0.eye_coordinates.z());
  233. float const vertex1_eye_absz = fabs(vertex1.eye_coordinates.z());
  234. float const vertex2_eye_absz = fabs(vertex2.eye_coordinates.z());
  235. // FIXME: implement stencil testing
  236. // Iterate over all blocks within the bounds of the triangle
  237. for (int by = by0; by < by1; by += 2) {
  238. for (int bx = bx0; bx < bx1; bx += 2) {
  239. PixelQuad quad;
  240. quad.screen_coordinates = {
  241. i32x4 { bx, bx + 1, bx, bx + 1 },
  242. i32x4 { by, by, by + 1, by + 1 },
  243. };
  244. auto edge_values = calculate_edge_values4(quad.screen_coordinates * subpixel_factor);
  245. // Generate triangle coverage mask
  246. quad.mask = test_point4(edge_values);
  247. if (m_options.scissor_enabled) {
  248. quad.mask &= test_scissor4(quad.screen_coordinates);
  249. }
  250. if (none(quad.mask))
  251. continue;
  252. INCREASE_STATISTICS_COUNTER(g_num_quads, 1);
  253. INCREASE_STATISTICS_COUNTER(g_num_pixels, maskcount(quad.mask));
  254. // Calculate barycentric coordinates from previously calculated edge values
  255. quad.barycentrics = Vector3<f32x4> {
  256. to_f32x4(edge_values.x()),
  257. to_f32x4(edge_values.y()),
  258. to_f32x4(edge_values.z()),
  259. } * one_over_area;
  260. float* depth_ptrs[4] = {
  261. &m_depth_buffer->scanline(by)[bx],
  262. &m_depth_buffer->scanline(by)[bx + 1],
  263. &m_depth_buffer->scanline(by + 1)[bx],
  264. &m_depth_buffer->scanline(by + 1)[bx + 1],
  265. };
  266. // AND the depth mask onto the coverage mask
  267. if (m_options.enable_depth_test) {
  268. auto depth = load4_masked(depth_ptrs[0], depth_ptrs[1], depth_ptrs[2], depth_ptrs[3], quad.mask);
  269. quad.depth = interpolate(vertex0.window_coordinates.z(), vertex1.window_coordinates.z(), vertex2.window_coordinates.z(), quad.barycentrics);
  270. // FIXME: Also apply depth_offset_factor which depends on the depth gradient
  271. quad.depth += m_options.depth_offset_constant * NumericLimits<float>::epsilon();
  272. switch (m_options.depth_func) {
  273. case DepthTestFunction::Always:
  274. break;
  275. case DepthTestFunction::Never:
  276. quad.mask ^= quad.mask;
  277. break;
  278. case DepthTestFunction::Greater:
  279. quad.mask &= quad.depth > depth;
  280. break;
  281. case DepthTestFunction::GreaterOrEqual:
  282. quad.mask &= quad.depth >= depth;
  283. break;
  284. case DepthTestFunction::NotEqual:
  285. #ifdef __SSE__
  286. quad.mask &= quad.depth != depth;
  287. #else
  288. quad.mask[0] = bit_cast<u32>(quad.depth[0]) != bit_cast<u32>(depth[0]) ? -1 : 0;
  289. quad.mask[1] = bit_cast<u32>(quad.depth[1]) != bit_cast<u32>(depth[1]) ? -1 : 0;
  290. quad.mask[2] = bit_cast<u32>(quad.depth[2]) != bit_cast<u32>(depth[2]) ? -1 : 0;
  291. quad.mask[3] = bit_cast<u32>(quad.depth[3]) != bit_cast<u32>(depth[3]) ? -1 : 0;
  292. #endif
  293. break;
  294. case DepthTestFunction::Equal:
  295. #ifdef __SSE__
  296. quad.mask &= quad.depth == depth;
  297. #else
  298. //
  299. // This is an interesting quirk that occurs due to us using the x87 FPU when Serenity is
  300. // compiled for the i386 target. When we calculate our depth value to be stored in the buffer,
  301. // it is an 80-bit x87 floating point number, however, when stored into the DepthBuffer, this is
  302. // truncated to 32 bits. This 38 bit loss of precision means that when x87 `FCOMP` is eventually
  303. // used here the comparison fails.
  304. // This could be solved by using a `long double` for the depth buffer, however this would take
  305. // up significantly more space and is completely overkill for a depth buffer. As such, comparing
  306. // the first 32-bits of this depth value is "good enough" that if we get a hit on it being
  307. // equal, we can pretty much guarantee that it's actually equal.
  308. //
  309. quad.mask[0] = bit_cast<u32>(quad.depth[0]) == bit_cast<u32>(depth[0]) ? -1 : 0;
  310. quad.mask[1] = bit_cast<u32>(quad.depth[1]) == bit_cast<u32>(depth[1]) ? -1 : 0;
  311. quad.mask[2] = bit_cast<u32>(quad.depth[2]) == bit_cast<u32>(depth[2]) ? -1 : 0;
  312. quad.mask[3] = bit_cast<u32>(quad.depth[3]) == bit_cast<u32>(depth[3]) ? -1 : 0;
  313. #endif
  314. break;
  315. case DepthTestFunction::LessOrEqual:
  316. quad.mask &= quad.depth <= depth;
  317. break;
  318. case DepthTestFunction::Less:
  319. quad.mask &= quad.depth < depth;
  320. break;
  321. }
  322. // Nice, no pixels passed the depth test -> block rejected by early z
  323. if (none(quad.mask))
  324. continue;
  325. }
  326. INCREASE_STATISTICS_COUNTER(g_num_pixels_shaded, maskcount(quad.mask));
  327. // Draw the pixels according to the previously generated mask
  328. auto const w_coordinates = Vector3<f32x4> {
  329. expand4(vertex0.window_coordinates.w()),
  330. expand4(vertex1.window_coordinates.w()),
  331. expand4(vertex2.window_coordinates.w()),
  332. };
  333. auto const interpolated_reciprocal_w = interpolate(w_coordinates.x(), w_coordinates.y(), w_coordinates.z(), quad.barycentrics);
  334. auto const interpolated_w = 1.0f / interpolated_reciprocal_w;
  335. quad.barycentrics = quad.barycentrics * w_coordinates * interpolated_w;
  336. // FIXME: make this more generic. We want to interpolate more than just color and uv
  337. if (m_options.shade_smooth) {
  338. quad.vertex_color = interpolate(expand4(vertex0.color), expand4(vertex1.color), expand4(vertex2.color), quad.barycentrics);
  339. } else {
  340. quad.vertex_color = expand4(vertex0.color);
  341. }
  342. quad.uv = interpolate(expand4(vertex0.tex_coord), expand4(vertex1.tex_coord), expand4(vertex2.tex_coord), quad.barycentrics);
  343. if (m_options.fog_enabled) {
  344. // Calculate depth of fragment for fog
  345. //
  346. // OpenGL 1.5 spec chapter 3.10: "An implementation may choose to approximate the
  347. // eye-coordinate distance from the eye to each fragment center by |Ze|."
  348. quad.fog_depth = interpolate(expand4(vertex0_eye_absz), expand4(vertex1_eye_absz), expand4(vertex2_eye_absz), quad.barycentrics);
  349. }
  350. shade_fragments(quad);
  351. if (m_options.enable_alpha_test && m_options.alpha_test_func != AlphaTestFunction::Always && !test_alpha(quad)) {
  352. continue;
  353. }
  354. // Write to depth buffer
  355. if (m_options.enable_depth_test && m_options.enable_depth_write) {
  356. store4_masked(quad.depth, depth_ptrs[0], depth_ptrs[1], depth_ptrs[2], depth_ptrs[3], quad.mask);
  357. }
  358. // We will not update the color buffer at all
  359. if (!m_options.color_mask || !m_options.enable_color_write)
  360. continue;
  361. Gfx::RGBA32* color_ptrs[4] = {
  362. &m_render_target->scanline(by)[bx],
  363. &m_render_target->scanline(by)[bx + 1],
  364. &m_render_target->scanline(by + 1)[bx],
  365. &m_render_target->scanline(by + 1)[bx + 1],
  366. };
  367. u32x4 dst_u32;
  368. if (m_options.enable_blending || m_options.color_mask != 0xffffffff)
  369. dst_u32 = load4_masked(color_ptrs[0], color_ptrs[1], color_ptrs[2], color_ptrs[3], quad.mask);
  370. if (m_options.enable_blending) {
  371. INCREASE_STATISTICS_COUNTER(g_num_pixels_blended, maskcount(quad.mask));
  372. // Blend color values from pixel_staging into m_render_target
  373. Vector4<f32x4> const& src = quad.out_color;
  374. auto dst = to_vec4(dst_u32);
  375. auto src_factor = expand4(m_alpha_blend_factors.src_constant)
  376. + src * m_alpha_blend_factors.src_factor_src_color
  377. + Vector4<f32x4> { src.w(), src.w(), src.w(), src.w() } * m_alpha_blend_factors.src_factor_src_alpha
  378. + dst * m_alpha_blend_factors.src_factor_dst_color
  379. + Vector4<f32x4> { dst.w(), dst.w(), dst.w(), dst.w() } * m_alpha_blend_factors.src_factor_dst_alpha;
  380. auto dst_factor = expand4(m_alpha_blend_factors.dst_constant)
  381. + src * m_alpha_blend_factors.dst_factor_src_color
  382. + Vector4<f32x4> { src.w(), src.w(), src.w(), src.w() } * m_alpha_blend_factors.dst_factor_src_alpha
  383. + dst * m_alpha_blend_factors.dst_factor_dst_color
  384. + Vector4<f32x4> { dst.w(), dst.w(), dst.w(), dst.w() } * m_alpha_blend_factors.dst_factor_dst_alpha;
  385. quad.out_color = src * src_factor + dst * dst_factor;
  386. }
  387. if (m_options.color_mask == 0xffffffff)
  388. store4_masked(to_rgba32(quad.out_color), color_ptrs[0], color_ptrs[1], color_ptrs[2], color_ptrs[3], quad.mask);
  389. else
  390. store4_masked((to_rgba32(quad.out_color) & m_options.color_mask) | (dst_u32 & ~m_options.color_mask), color_ptrs[0], color_ptrs[1], color_ptrs[2], color_ptrs[3], quad.mask);
  391. }
  392. }
  393. }
  394. static Gfx::IntSize closest_multiple(const Gfx::IntSize& min_size, size_t step)
  395. {
  396. int width = ((min_size.width() + step - 1) / step) * step;
  397. int height = ((min_size.height() + step - 1) / step) * step;
  398. return { width, height };
  399. }
  400. Device::Device(const Gfx::IntSize& min_size)
  401. : m_render_target { Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, closest_multiple(min_size, 2)).release_value_but_fixme_should_propagate_errors() }
  402. , m_depth_buffer { adopt_own(*new DepthBuffer(closest_multiple(min_size, 2))) }
  403. {
  404. m_options.scissor_box = m_render_target->rect();
  405. }
  406. DeviceInfo Device::info() const
  407. {
  408. return {
  409. .vendor_name = "SerenityOS",
  410. .device_name = "SoftGPU",
  411. .num_texture_units = NUM_SAMPLERS
  412. };
  413. }
  414. static void generate_texture_coordinates(Vertex& vertex, RasterizerOptions const& options)
  415. {
  416. auto generate_coordinate = [&](size_t config_index) -> float {
  417. auto mode = options.texcoord_generation_config[config_index].mode;
  418. switch (mode) {
  419. case TexCoordGenerationMode::ObjectLinear: {
  420. auto coefficients = options.texcoord_generation_config[config_index].coefficients;
  421. return coefficients.dot(vertex.position);
  422. }
  423. case TexCoordGenerationMode::EyeLinear: {
  424. auto coefficients = options.texcoord_generation_config[config_index].coefficients;
  425. return coefficients.dot(vertex.eye_coordinates);
  426. }
  427. case TexCoordGenerationMode::SphereMap: {
  428. auto const eye_unit = vertex.eye_coordinates.normalized();
  429. FloatVector3 const eye_unit_xyz = { eye_unit.x(), eye_unit.y(), eye_unit.z() };
  430. auto const normal = vertex.normal;
  431. auto reflection = eye_unit_xyz - normal * 2 * normal.dot(eye_unit_xyz);
  432. reflection.set_z(reflection.z() + 1);
  433. auto const reflection_value = (config_index == 0) ? reflection.x() : reflection.y();
  434. return reflection_value / (2 * reflection.length()) + 0.5f;
  435. }
  436. case TexCoordGenerationMode::ReflectionMap: {
  437. auto const eye_unit = vertex.eye_coordinates.normalized();
  438. FloatVector3 const eye_unit_xyz = { eye_unit.x(), eye_unit.y(), eye_unit.z() };
  439. auto const normal = vertex.normal;
  440. auto reflection = eye_unit_xyz - normal * 2 * normal.dot(eye_unit_xyz);
  441. switch (config_index) {
  442. case 0:
  443. return reflection.x();
  444. case 1:
  445. return reflection.y();
  446. case 2:
  447. return reflection.z();
  448. default:
  449. VERIFY_NOT_REACHED();
  450. }
  451. }
  452. case TexCoordGenerationMode::NormalMap: {
  453. auto const normal = vertex.normal;
  454. switch (config_index) {
  455. case 0:
  456. return normal.x();
  457. case 1:
  458. return normal.y();
  459. case 2:
  460. return normal.z();
  461. default:
  462. VERIFY_NOT_REACHED();
  463. }
  464. }
  465. default:
  466. VERIFY_NOT_REACHED();
  467. }
  468. };
  469. auto const enabled_coords = options.texcoord_generation_enabled_coordinates;
  470. vertex.tex_coord = {
  471. ((enabled_coords & TexCoordGenerationCoordinate::S) > 0) ? generate_coordinate(0) : vertex.tex_coord.x(),
  472. ((enabled_coords & TexCoordGenerationCoordinate::T) > 0) ? generate_coordinate(1) : vertex.tex_coord.y(),
  473. ((enabled_coords & TexCoordGenerationCoordinate::R) > 0) ? generate_coordinate(2) : vertex.tex_coord.z(),
  474. ((enabled_coords & TexCoordGenerationCoordinate::Q) > 0) ? generate_coordinate(3) : vertex.tex_coord.w(),
  475. };
  476. }
  477. void Device::draw_primitives(PrimitiveType primitive_type, FloatMatrix4x4 const& model_view_transform, FloatMatrix3x3 const& normal_transform,
  478. FloatMatrix4x4 const& projection_transform, FloatMatrix4x4 const& texture_transform, Vector<Vertex> const& vertices,
  479. Vector<size_t> const& enabled_texture_units)
  480. {
  481. // At this point, the user has effectively specified that they are done with defining the geometry
  482. // of what they want to draw. We now need to do a few things (https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview):
  483. //
  484. // 1. Transform all of the vertices in the current vertex list into eye space by multiplying the model-view matrix
  485. // 2. Transform all of the vertices from eye space into clip space by multiplying by the projection matrix
  486. // 3. If culling is enabled, we cull the desired faces (https://learnopengl.com/Advanced-OpenGL/Face-culling)
  487. // 4. Each element of the vertex is then divided by w to bring the positions into NDC (Normalized Device Coordinates)
  488. // 5. The vertices are sorted (for the rasterizer, how are we doing this? 3Dfx did this top to bottom in terms of vertex y coordinates)
  489. // 6. The vertices are then sent off to the rasterizer and drawn to the screen
  490. m_enabled_texture_units = enabled_texture_units;
  491. float scr_width = m_render_target->width();
  492. float scr_height = m_render_target->height();
  493. m_triangle_list.clear_with_capacity();
  494. m_processed_triangles.clear_with_capacity();
  495. // Let's construct some triangles
  496. if (primitive_type == PrimitiveType::Triangles) {
  497. Triangle triangle;
  498. for (size_t i = 0; i < vertices.size(); i += 3) {
  499. triangle.vertices[0] = vertices.at(i);
  500. triangle.vertices[1] = vertices.at(i + 1);
  501. triangle.vertices[2] = vertices.at(i + 2);
  502. m_triangle_list.append(triangle);
  503. }
  504. } else if (primitive_type == PrimitiveType::Quads) {
  505. // We need to construct two triangles to form the quad
  506. Triangle triangle;
  507. VERIFY(vertices.size() % 4 == 0);
  508. for (size_t i = 0; i < vertices.size(); i += 4) {
  509. // Triangle 1
  510. triangle.vertices[0] = vertices.at(i);
  511. triangle.vertices[1] = vertices.at(i + 1);
  512. triangle.vertices[2] = vertices.at(i + 2);
  513. m_triangle_list.append(triangle);
  514. // Triangle 2
  515. triangle.vertices[0] = vertices.at(i + 2);
  516. triangle.vertices[1] = vertices.at(i + 3);
  517. triangle.vertices[2] = vertices.at(i);
  518. m_triangle_list.append(triangle);
  519. }
  520. } else if (primitive_type == PrimitiveType::TriangleFan) {
  521. Triangle triangle;
  522. triangle.vertices[0] = vertices.at(0); // Root vertex is always the vertex defined first
  523. for (size_t i = 1; i < vertices.size() - 1; i++) // This is technically `n-2` triangles. We start at index 1
  524. {
  525. triangle.vertices[1] = vertices.at(i);
  526. triangle.vertices[2] = vertices.at(i + 1);
  527. m_triangle_list.append(triangle);
  528. }
  529. } else if (primitive_type == PrimitiveType::TriangleStrip) {
  530. Triangle triangle;
  531. for (size_t i = 0; i < vertices.size() - 2; i++) {
  532. if (i % 2 == 0) {
  533. triangle.vertices[0] = vertices.at(i);
  534. triangle.vertices[1] = vertices.at(i + 1);
  535. triangle.vertices[2] = vertices.at(i + 2);
  536. } else {
  537. triangle.vertices[0] = vertices.at(i + 1);
  538. triangle.vertices[1] = vertices.at(i);
  539. triangle.vertices[2] = vertices.at(i + 2);
  540. }
  541. m_triangle_list.append(triangle);
  542. }
  543. }
  544. // Now let's transform each triangle and send that to the GPU
  545. auto const depth_half_range = (m_options.depth_max - m_options.depth_min) / 2;
  546. auto const depth_halfway = (m_options.depth_min + m_options.depth_max) / 2;
  547. for (auto& triangle : m_triangle_list) {
  548. // Transform vertices into eye coordinates using the model-view transform
  549. triangle.vertices[0].eye_coordinates = model_view_transform * triangle.vertices[0].position;
  550. triangle.vertices[1].eye_coordinates = model_view_transform * triangle.vertices[1].position;
  551. triangle.vertices[2].eye_coordinates = model_view_transform * triangle.vertices[2].position;
  552. // Transform eye coordinates into clip coordinates using the projection transform
  553. triangle.vertices[0].clip_coordinates = projection_transform * triangle.vertices[0].eye_coordinates;
  554. triangle.vertices[1].clip_coordinates = projection_transform * triangle.vertices[1].eye_coordinates;
  555. triangle.vertices[2].clip_coordinates = projection_transform * triangle.vertices[2].eye_coordinates;
  556. // At this point, we're in clip space
  557. // Here's where we do the clipping. This is a really crude implementation of the
  558. // https://learnopengl.com/Getting-started/Coordinate-Systems
  559. // "Note that if only a part of a primitive e.g. a triangle is outside the clipping volume OpenGL
  560. // will reconstruct the triangle as one or more triangles to fit inside the clipping range. "
  561. //
  562. // ALL VERTICES ARE DEFINED IN A CLOCKWISE ORDER
  563. // Okay, let's do some face culling first
  564. m_clipped_vertices.clear_with_capacity();
  565. m_clipped_vertices.append(triangle.vertices[0]);
  566. m_clipped_vertices.append(triangle.vertices[1]);
  567. m_clipped_vertices.append(triangle.vertices[2]);
  568. m_clipper.clip_triangle_against_frustum(m_clipped_vertices);
  569. if (m_clipped_vertices.size() < 3)
  570. continue;
  571. for (auto& vec : m_clipped_vertices) {
  572. // To normalized device coordinates (NDC)
  573. auto const one_over_w = 1 / vec.clip_coordinates.w();
  574. auto const ndc_coordinates = FloatVector4 {
  575. vec.clip_coordinates.x() * one_over_w,
  576. vec.clip_coordinates.y() * one_over_w,
  577. vec.clip_coordinates.z() * one_over_w,
  578. one_over_w,
  579. };
  580. // To window coordinates
  581. // FIXME: implement viewport functionality
  582. vec.window_coordinates = {
  583. scr_width / 2 + ndc_coordinates.x() * scr_width / 2,
  584. scr_height / 2 - ndc_coordinates.y() * scr_height / 2,
  585. depth_half_range * ndc_coordinates.z() + depth_halfway,
  586. ndc_coordinates.w(),
  587. };
  588. }
  589. Triangle tri;
  590. tri.vertices[0] = m_clipped_vertices[0];
  591. for (size_t i = 1; i < m_clipped_vertices.size() - 1; i++) {
  592. tri.vertices[1] = m_clipped_vertices[i];
  593. tri.vertices[2] = m_clipped_vertices[i + 1];
  594. m_processed_triangles.append(tri);
  595. }
  596. }
  597. for (auto& triangle : m_processed_triangles) {
  598. // Let's calculate the (signed) area of the triangle
  599. // https://cp-algorithms.com/geometry/oriented-triangle-area.html
  600. float dxAB = triangle.vertices[0].window_coordinates.x() - triangle.vertices[1].window_coordinates.x(); // A.x - B.x
  601. float dxBC = triangle.vertices[1].window_coordinates.x() - triangle.vertices[2].window_coordinates.x(); // B.X - C.x
  602. float dyAB = triangle.vertices[0].window_coordinates.y() - triangle.vertices[1].window_coordinates.y();
  603. float dyBC = triangle.vertices[1].window_coordinates.y() - triangle.vertices[2].window_coordinates.y();
  604. float area = (dxAB * dyBC) - (dxBC * dyAB);
  605. if (area == 0.0f)
  606. continue;
  607. if (m_options.enable_culling) {
  608. bool is_front = (m_options.front_face == WindingOrder::CounterClockwise ? area < 0 : area > 0);
  609. if (!is_front && m_options.cull_back)
  610. continue;
  611. if (is_front && m_options.cull_front)
  612. continue;
  613. }
  614. if (area > 0)
  615. swap(triangle.vertices[0], triangle.vertices[1]);
  616. // Transform normals
  617. triangle.vertices[0].normal = normal_transform * triangle.vertices[0].normal;
  618. triangle.vertices[1].normal = normal_transform * triangle.vertices[1].normal;
  619. triangle.vertices[2].normal = normal_transform * triangle.vertices[2].normal;
  620. if (m_options.normalization_enabled) {
  621. triangle.vertices[0].normal.normalize();
  622. triangle.vertices[1].normal.normalize();
  623. triangle.vertices[2].normal.normalize();
  624. }
  625. // Generate texture coordinates if at least one coordinate is enabled
  626. if (m_options.texcoord_generation_enabled_coordinates != TexCoordGenerationCoordinate::None) {
  627. generate_texture_coordinates(triangle.vertices[0], m_options);
  628. generate_texture_coordinates(triangle.vertices[1], m_options);
  629. generate_texture_coordinates(triangle.vertices[2], m_options);
  630. }
  631. // Apply texture transformation
  632. // FIXME: implement multi-texturing: texcoords should be stored per texture unit
  633. triangle.vertices[0].tex_coord = texture_transform * triangle.vertices[0].tex_coord;
  634. triangle.vertices[1].tex_coord = texture_transform * triangle.vertices[1].tex_coord;
  635. triangle.vertices[2].tex_coord = texture_transform * triangle.vertices[2].tex_coord;
  636. rasterize_triangle(triangle);
  637. }
  638. }
  639. ALWAYS_INLINE void Device::shade_fragments(PixelQuad& quad)
  640. {
  641. quad.out_color = quad.vertex_color;
  642. for (size_t i : m_enabled_texture_units) {
  643. // FIXME: implement GL_TEXTURE_1D, GL_TEXTURE_3D and GL_TEXTURE_CUBE_MAP
  644. auto const& sampler = m_samplers[i];
  645. auto texel = sampler.sample_2d({ quad.uv.x(), quad.uv.y() });
  646. INCREASE_STATISTICS_COUNTER(g_num_sampler_calls, 1);
  647. // FIXME: Implement more blend modes
  648. switch (sampler.config().fixed_function_texture_env_mode) {
  649. case TextureEnvMode::Modulate:
  650. quad.out_color = quad.out_color * texel;
  651. break;
  652. case TextureEnvMode::Replace:
  653. quad.out_color = texel;
  654. break;
  655. case TextureEnvMode::Decal: {
  656. auto src_alpha = quad.out_color.w();
  657. quad.out_color.set_x(mix(quad.out_color.x(), texel.x(), src_alpha));
  658. quad.out_color.set_y(mix(quad.out_color.y(), texel.y(), src_alpha));
  659. quad.out_color.set_z(mix(quad.out_color.z(), texel.z(), src_alpha));
  660. break;
  661. }
  662. default:
  663. VERIFY_NOT_REACHED();
  664. }
  665. }
  666. // Calculate fog
  667. // Math from here: https://opengl-notes.readthedocs.io/en/latest/topics/texturing/aliasing.html
  668. // FIXME: exponential fog is not vectorized, we should add a SIMD exp function that calculates an approximation.
  669. if (m_options.fog_enabled) {
  670. auto factor = expand4(0.0f);
  671. switch (m_options.fog_mode) {
  672. case FogMode::Linear:
  673. factor = (m_options.fog_end - quad.fog_depth) / (m_options.fog_end - m_options.fog_start);
  674. break;
  675. case FogMode::Exp: {
  676. auto argument = -m_options.fog_density * quad.fog_depth;
  677. factor = exp(argument);
  678. } break;
  679. case FogMode::Exp2: {
  680. auto argument = m_options.fog_density * quad.fog_depth;
  681. argument *= -argument;
  682. factor = exp(argument);
  683. } break;
  684. default:
  685. VERIFY_NOT_REACHED();
  686. }
  687. // Mix texel's RGB with fog's RBG - leave alpha alone
  688. auto fog_color = expand4(m_options.fog_color);
  689. quad.out_color.set_x(mix(fog_color.x(), quad.out_color.x(), factor));
  690. quad.out_color.set_y(mix(fog_color.y(), quad.out_color.y(), factor));
  691. quad.out_color.set_z(mix(fog_color.z(), quad.out_color.z(), factor));
  692. }
  693. }
  694. ALWAYS_INLINE bool Device::test_alpha(PixelQuad& quad)
  695. {
  696. auto const alpha = quad.out_color.w();
  697. auto const ref_value = expand4(m_options.alpha_test_ref_value);
  698. switch (m_options.alpha_test_func) {
  699. case AlphaTestFunction::Less:
  700. quad.mask &= alpha < ref_value;
  701. break;
  702. case AlphaTestFunction::Equal:
  703. quad.mask &= alpha == ref_value;
  704. break;
  705. case AlphaTestFunction::LessOrEqual:
  706. quad.mask &= alpha <= ref_value;
  707. break;
  708. case AlphaTestFunction::Greater:
  709. quad.mask &= alpha > ref_value;
  710. break;
  711. case AlphaTestFunction::NotEqual:
  712. quad.mask &= alpha != ref_value;
  713. break;
  714. case AlphaTestFunction::GreaterOrEqual:
  715. quad.mask &= alpha >= ref_value;
  716. break;
  717. case AlphaTestFunction::Never:
  718. case AlphaTestFunction::Always:
  719. default:
  720. VERIFY_NOT_REACHED();
  721. }
  722. return any(quad.mask);
  723. }
  724. void Device::resize(const Gfx::IntSize& min_size)
  725. {
  726. wait_for_all_threads();
  727. m_render_target = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, closest_multiple(min_size, 2)).release_value_but_fixme_should_propagate_errors();
  728. m_depth_buffer = adopt_own(*new DepthBuffer(m_render_target->size()));
  729. }
  730. void Device::clear_color(const FloatVector4& color)
  731. {
  732. wait_for_all_threads();
  733. uint8_t r = static_cast<uint8_t>(clamp(color.x(), 0.0f, 1.0f) * 255);
  734. uint8_t g = static_cast<uint8_t>(clamp(color.y(), 0.0f, 1.0f) * 255);
  735. uint8_t b = static_cast<uint8_t>(clamp(color.z(), 0.0f, 1.0f) * 255);
  736. uint8_t a = static_cast<uint8_t>(clamp(color.w(), 0.0f, 1.0f) * 255);
  737. auto const fill_color = Gfx::Color(r, g, b, a);
  738. if (m_options.scissor_enabled) {
  739. auto fill_rect = m_render_target->rect();
  740. fill_rect.intersect(scissor_box_to_window_coordinates(m_options.scissor_box, fill_rect));
  741. Gfx::Painter painter { *m_render_target };
  742. painter.fill_rect(fill_rect, fill_color);
  743. return;
  744. }
  745. m_render_target->fill(fill_color);
  746. }
  747. void Device::clear_depth(float depth)
  748. {
  749. wait_for_all_threads();
  750. if (m_options.scissor_enabled) {
  751. m_depth_buffer->clear(scissor_box_to_window_coordinates(m_options.scissor_box, m_render_target->rect()), depth);
  752. return;
  753. }
  754. m_depth_buffer->clear(depth);
  755. }
  756. void Device::blit(Gfx::Bitmap const& source, int x, int y)
  757. {
  758. wait_for_all_threads();
  759. INCREASE_STATISTICS_COUNTER(g_num_pixels, source.width() * source.height());
  760. INCREASE_STATISTICS_COUNTER(g_num_pixels_shaded, source.width() * source.height());
  761. Gfx::Painter painter { *m_render_target };
  762. painter.blit({ x, y }, source, source.rect(), 1.0f, true);
  763. }
  764. void Device::blit_to(Gfx::Bitmap& target)
  765. {
  766. wait_for_all_threads();
  767. Gfx::Painter painter { target };
  768. painter.blit({ 0, 0 }, *m_render_target, m_render_target->rect(), 1.0f, false);
  769. if constexpr (ENABLE_STATISTICS_OVERLAY)
  770. draw_statistics_overlay(target);
  771. }
  772. void Device::draw_statistics_overlay(Gfx::Bitmap& target)
  773. {
  774. static Core::ElapsedTimer timer;
  775. static String debug_string;
  776. static int frame_counter;
  777. frame_counter++;
  778. int milliseconds = 0;
  779. if (timer.is_valid())
  780. milliseconds = timer.elapsed();
  781. else
  782. timer.start();
  783. Gfx::Painter painter { target };
  784. if (milliseconds > 500) {
  785. if (g_num_pixels == 0)
  786. g_num_pixels = 1;
  787. int num_rendertarget_pixels = m_render_target->width() * m_render_target->height();
  788. StringBuilder builder;
  789. builder.append(String::formatted("Timings : {:.1}ms {:.1}FPS\n",
  790. static_cast<double>(milliseconds) / frame_counter,
  791. (milliseconds > 0) ? 1000.0 * frame_counter / milliseconds : 9999.0));
  792. builder.append(String::formatted("Triangles : {}\n", g_num_rasterized_triangles));
  793. builder.append(String::formatted("SIMD usage : {}%\n", g_num_quads > 0 ? g_num_pixels_shaded * 25 / g_num_quads : 0));
  794. builder.append(String::formatted("Pixels : {}, Shaded: {}%, Blended: {}%, Overdraw: {}%\n",
  795. g_num_pixels,
  796. g_num_pixels_shaded * 100 / g_num_pixels,
  797. g_num_pixels_blended * 100 / g_num_pixels_shaded,
  798. g_num_pixels_shaded * 100 / num_rendertarget_pixels - 100));
  799. builder.append(String::formatted("Sampler calls: {}\n", g_num_sampler_calls));
  800. debug_string = builder.to_string();
  801. frame_counter = 0;
  802. timer.start();
  803. }
  804. g_num_rasterized_triangles = 0;
  805. g_num_pixels = 0;
  806. g_num_pixels_shaded = 0;
  807. g_num_pixels_blended = 0;
  808. g_num_sampler_calls = 0;
  809. g_num_quads = 0;
  810. auto& font = Gfx::FontDatabase::default_fixed_width_font();
  811. for (int y = -1; y < 2; y++)
  812. for (int x = -1; x < 2; x++)
  813. if (x != 0 && y != 0)
  814. painter.draw_text(target.rect().translated(x + 2, y + 2), debug_string, font, Gfx::TextAlignment::TopLeft, Gfx::Color::Black);
  815. painter.draw_text(target.rect().translated(2, 2), debug_string, font, Gfx::TextAlignment::TopLeft, Gfx::Color::White);
  816. }
  817. void Device::wait_for_all_threads() const
  818. {
  819. // FIXME: Wait for all render threads to finish when multithreading is being implemented
  820. }
  821. void Device::set_options(const RasterizerOptions& options)
  822. {
  823. wait_for_all_threads();
  824. m_options = options;
  825. if (m_options.enable_blending)
  826. setup_blend_factors();
  827. // FIXME: Recreate or reinitialize render threads here when multithreading is being implemented
  828. }
  829. Gfx::RGBA32 Device::get_backbuffer_pixel(int x, int y)
  830. {
  831. // FIXME: Reading individual pixels is very slow, rewrite this to transfer whole blocks
  832. if (x < 0 || y < 0 || x >= m_render_target->width() || y >= m_render_target->height())
  833. return 0;
  834. return m_render_target->scanline(y)[x];
  835. }
  836. float Device::get_depthbuffer_value(int x, int y)
  837. {
  838. // FIXME: Reading individual pixels is very slow, rewrite this to transfer whole blocks
  839. if (x < 0 || y < 0 || x >= m_render_target->width() || y >= m_render_target->height())
  840. return 1.0f;
  841. return m_depth_buffer->scanline(y)[x];
  842. }
  843. NonnullRefPtr<Image> Device::create_image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers)
  844. {
  845. VERIFY(width > 0);
  846. VERIFY(height > 0);
  847. VERIFY(depth > 0);
  848. VERIFY(levels > 0);
  849. VERIFY(layers > 0);
  850. return adopt_ref(*new Image(format, width, height, depth, levels, layers));
  851. }
  852. void Device::set_sampler_config(unsigned sampler, SamplerConfig const& config)
  853. {
  854. m_samplers[sampler].set_config(config);
  855. }
  856. }