Device.cpp 40 KB

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