Device.cpp 41 KB

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