Device.cpp 41 KB

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