SoftwareRasterizer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SoftwareRasterizer.h"
  7. #include <AK/Function.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibGfx/Vector2.h>
  10. #include <LibGfx/Vector3.h>
  11. namespace GL {
  12. using IntVector2 = Gfx::Vector2<int>;
  13. using IntVector3 = Gfx::Vector3<int>;
  14. static constexpr int RASTERIZER_BLOCK_SIZE = 16;
  15. constexpr static int edge_function(const IntVector2& a, const IntVector2& b, const IntVector2& c)
  16. {
  17. return ((c.x() - a.x()) * (b.y() - a.y()) - (c.y() - a.y()) * (b.x() - a.x()));
  18. }
  19. template<typename T>
  20. constexpr static T interpolate(const T& v0, const T& v1, const T& v2, const FloatVector3& barycentric_coords)
  21. {
  22. return v0 * barycentric_coords.x() + v1 * barycentric_coords.y() + v2 * barycentric_coords.z();
  23. }
  24. template<typename T>
  25. constexpr static T mix(const T& x, const T& y, float interp)
  26. {
  27. return x * (1 - interp) + y * interp;
  28. }
  29. static Gfx::RGBA32 to_rgba32(const FloatVector4& v)
  30. {
  31. auto clamped = v.clamped(0, 1);
  32. u8 r = clamped.x() * 255;
  33. u8 g = clamped.y() * 255;
  34. u8 b = clamped.z() * 255;
  35. u8 a = clamped.w() * 255;
  36. return a << 24 | r << 16 | g << 8 | b;
  37. }
  38. static FloatVector4 to_vec4(Gfx::RGBA32 rgba)
  39. {
  40. return {
  41. ((rgba >> 16) & 0xff) / 255.0f,
  42. ((rgba >> 8) & 0xff) / 255.0f,
  43. (rgba & 0xff) / 255.0f,
  44. ((rgba >> 24) & 0xff) / 255.0f
  45. };
  46. }
  47. static constexpr void setup_blend_factors(GLenum mode, FloatVector4& constant, float& src_alpha, float& dst_alpha, float& src_color, float& dst_color)
  48. {
  49. constant = { 0.0f, 0.0f, 0.0f, 0.0f };
  50. src_alpha = 0;
  51. dst_alpha = 0;
  52. src_color = 0;
  53. dst_color = 0;
  54. switch (mode) {
  55. case GL_ZERO:
  56. break;
  57. case GL_ONE:
  58. constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  59. break;
  60. case GL_SRC_COLOR:
  61. src_color = 1;
  62. break;
  63. case GL_ONE_MINUS_SRC_COLOR:
  64. constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  65. src_color = -1;
  66. break;
  67. case GL_SRC_ALPHA:
  68. src_alpha = 1;
  69. break;
  70. case GL_ONE_MINUS_SRC_ALPHA:
  71. constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  72. src_alpha = -1;
  73. break;
  74. case GL_DST_ALPHA:
  75. dst_alpha = 1;
  76. break;
  77. case GL_ONE_MINUS_DST_ALPHA:
  78. constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  79. dst_alpha = -1;
  80. break;
  81. case GL_DST_COLOR:
  82. dst_color = 1;
  83. break;
  84. case GL_ONE_MINUS_DST_COLOR:
  85. constant = { 1.0f, 1.0f, 1.0f, 1.0f };
  86. dst_color = -1;
  87. break;
  88. case GL_SRC_ALPHA_SATURATE:
  89. // FIXME: How do we implement this?
  90. break;
  91. default:
  92. VERIFY_NOT_REACHED();
  93. }
  94. }
  95. template<typename PS>
  96. static void rasterize_triangle(const RasterizerOptions& options, Gfx::Bitmap& render_target, DepthBuffer& depth_buffer, const GLTriangle& triangle, PS pixel_shader)
  97. {
  98. // Since the algorithm is based on blocks of uniform size, we need
  99. // to ensure that our render_target size is actually a multiple of the block size
  100. VERIFY((render_target.width() % RASTERIZER_BLOCK_SIZE) == 0);
  101. VERIFY((render_target.height() % RASTERIZER_BLOCK_SIZE) == 0);
  102. // Calculate area of the triangle for later tests
  103. IntVector2 v0 { (int)triangle.vertices[0].position.x(), (int)triangle.vertices[0].position.y() };
  104. IntVector2 v1 { (int)triangle.vertices[1].position.x(), (int)triangle.vertices[1].position.y() };
  105. IntVector2 v2 { (int)triangle.vertices[2].position.x(), (int)triangle.vertices[2].position.y() };
  106. int area = edge_function(v0, v1, v2);
  107. if (area == 0)
  108. return;
  109. float one_over_area = 1.0f / area;
  110. FloatVector4 src_constant {};
  111. float src_factor_src_alpha = 0;
  112. float src_factor_dst_alpha = 0;
  113. float src_factor_src_color = 0;
  114. float src_factor_dst_color = 0;
  115. FloatVector4 dst_constant {};
  116. float dst_factor_src_alpha = 0;
  117. float dst_factor_dst_alpha = 0;
  118. float dst_factor_src_color = 0;
  119. float dst_factor_dst_color = 0;
  120. if (options.enable_blending) {
  121. setup_blend_factors(
  122. options.blend_source_factor,
  123. src_constant,
  124. src_factor_src_alpha,
  125. src_factor_dst_alpha,
  126. src_factor_src_color,
  127. src_factor_dst_color);
  128. setup_blend_factors(
  129. options.blend_destination_factor,
  130. dst_constant,
  131. dst_factor_src_alpha,
  132. dst_factor_dst_alpha,
  133. dst_factor_src_color,
  134. dst_factor_dst_color);
  135. }
  136. // Obey top-left rule:
  137. // This sets up "zero" for later pixel coverage tests.
  138. // Depending on where on the triangle the edge is located
  139. // it is either tested against 0 or 1, effectively
  140. // turning "< 0" into "<= 0"
  141. IntVector3 zero { 1, 1, 1 };
  142. if (v1.y() > v0.y() || (v1.y() == v0.y() && v1.x() < v0.x()))
  143. zero.set_z(0);
  144. if (v2.y() > v1.y() || (v2.y() == v1.y() && v2.x() < v1.x()))
  145. zero.set_x(0);
  146. if (v0.y() > v2.y() || (v0.y() == v2.y() && v0.x() < v2.x()))
  147. zero.set_y(0);
  148. // This function calculates the 3 edge values for the pixel relative to the triangle.
  149. auto calculate_edge_values = [v0, v1, v2](const IntVector2& p) -> IntVector3 {
  150. return {
  151. edge_function(v1, v2, p),
  152. edge_function(v2, v0, p),
  153. edge_function(v0, v1, p),
  154. };
  155. };
  156. // This function tests whether a point as identified by its 3 edge values lies within the triangle
  157. auto test_point = [zero](const IntVector3& edges) -> bool {
  158. return edges.x() >= zero.x()
  159. && edges.y() >= zero.y()
  160. && edges.z() >= zero.z();
  161. };
  162. // Calculate block-based bounds
  163. auto render_bounds = render_target.rect();
  164. if (options.scissor_enabled)
  165. render_bounds.intersect(options.scissor_box);
  166. int const block_padding = RASTERIZER_BLOCK_SIZE - 1;
  167. // clang-format off
  168. int const bx0 = max(render_bounds.left(), min(min(v0.x(), v1.x()), v2.x())) / RASTERIZER_BLOCK_SIZE;
  169. int const bx1 = (min(render_bounds.right(), max(max(v0.x(), v1.x()), v2.x())) + block_padding) / RASTERIZER_BLOCK_SIZE;
  170. int const by0 = max(render_bounds.top(), min(min(v0.y(), v1.y()), v2.y())) / RASTERIZER_BLOCK_SIZE;
  171. int const by1 = (min(render_bounds.bottom(), max(max(v0.y(), v1.y()), v2.y())) + block_padding) / RASTERIZER_BLOCK_SIZE;
  172. // clang-format on
  173. static_assert(RASTERIZER_BLOCK_SIZE < sizeof(int) * 8, "RASTERIZER_BLOCK_SIZE must be smaller than the pixel_mask's width in bits");
  174. int pixel_mask[RASTERIZER_BLOCK_SIZE];
  175. FloatVector4 pixel_buffer[RASTERIZER_BLOCK_SIZE][RASTERIZER_BLOCK_SIZE];
  176. // Iterate over all blocks within the bounds of the triangle
  177. for (int by = by0; by < by1; by++) {
  178. for (int bx = bx0; bx < bx1; bx++) {
  179. // Edge values of the 4 block corners
  180. // clang-format off
  181. auto b0 = calculate_edge_values({ bx * RASTERIZER_BLOCK_SIZE, by * RASTERIZER_BLOCK_SIZE });
  182. auto b1 = calculate_edge_values({ bx * RASTERIZER_BLOCK_SIZE + RASTERIZER_BLOCK_SIZE, by * RASTERIZER_BLOCK_SIZE });
  183. auto b2 = calculate_edge_values({ bx * RASTERIZER_BLOCK_SIZE, by * RASTERIZER_BLOCK_SIZE + RASTERIZER_BLOCK_SIZE });
  184. auto b3 = calculate_edge_values({ bx * RASTERIZER_BLOCK_SIZE + RASTERIZER_BLOCK_SIZE, by * RASTERIZER_BLOCK_SIZE + RASTERIZER_BLOCK_SIZE });
  185. // clang-format on
  186. // If the whole block is outside any of the triangle edges we can discard it completely
  187. // We test this by and'ing the relevant edge function values together for all block corners
  188. // and checking if the negative sign bit is set for all of them
  189. if ((b0.x() & b1.x() & b2.x() & b3.x()) & 0x80000000)
  190. continue;
  191. if ((b0.y() & b1.y() & b2.y() & b3.y()) & 0x80000000)
  192. continue;
  193. if ((b0.z() & b1.z() & b2.z() & b3.z()) & 0x80000000)
  194. continue;
  195. // edge value derivatives
  196. auto dbdx = (b1 - b0) / RASTERIZER_BLOCK_SIZE;
  197. auto dbdy = (b2 - b0) / RASTERIZER_BLOCK_SIZE;
  198. // step edge value after each horizontal span: 1 down, BLOCK_SIZE left
  199. auto step_y = dbdy - dbdx * RASTERIZER_BLOCK_SIZE;
  200. int x0 = bx * RASTERIZER_BLOCK_SIZE;
  201. int y0 = by * RASTERIZER_BLOCK_SIZE;
  202. // Generate the coverage mask
  203. if (!options.scissor_enabled && test_point(b0) && test_point(b1) && test_point(b2) && test_point(b3)) {
  204. // The block is fully contained within the triangle. Fill the mask with all 1s
  205. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++)
  206. pixel_mask[y] = -1;
  207. } else {
  208. // The block overlaps at least one triangle edge.
  209. // We need to test coverage of every pixel within the block.
  210. auto coords = b0;
  211. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++, coords += step_y) {
  212. pixel_mask[y] = 0;
  213. for (int x = 0; x < RASTERIZER_BLOCK_SIZE; x++, coords += dbdx) {
  214. if (test_point(coords) && (!options.scissor_enabled || render_bounds.contains(x0 + x, y0 + y)))
  215. pixel_mask[y] |= 1 << x;
  216. }
  217. }
  218. }
  219. // AND the depth mask onto the coverage mask
  220. if (options.enable_depth_test) {
  221. int z_pass_count = 0;
  222. auto coords = b0;
  223. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++, coords += step_y) {
  224. if (pixel_mask[y] == 0) {
  225. coords += dbdx * RASTERIZER_BLOCK_SIZE;
  226. continue;
  227. }
  228. auto* depth = &depth_buffer.scanline(y0 + y)[x0];
  229. for (int x = 0; x < RASTERIZER_BLOCK_SIZE; x++, coords += dbdx, depth++) {
  230. if (~pixel_mask[y] & (1 << x))
  231. continue;
  232. auto barycentric = FloatVector3(coords.x(), coords.y(), coords.z()) * one_over_area;
  233. float z = interpolate(triangle.vertices[0].position.z(), triangle.vertices[1].position.z(), triangle.vertices[2].position.z(), barycentric);
  234. z = options.depth_min + (options.depth_max - options.depth_min) * (z + 1) / 2;
  235. // FIXME: Also apply depth_offset_factor which depends on the depth gradient
  236. z += options.depth_offset_constant * NumericLimits<float>::epsilon();
  237. bool pass = false;
  238. switch (options.depth_func) {
  239. case GL_ALWAYS:
  240. pass = true;
  241. break;
  242. case GL_NEVER:
  243. pass = false;
  244. break;
  245. case GL_GREATER:
  246. pass = z > *depth;
  247. break;
  248. case GL_GEQUAL:
  249. pass = z >= *depth;
  250. break;
  251. case GL_NOTEQUAL:
  252. #ifdef __SSE__
  253. pass = z != *depth;
  254. #else
  255. pass = bit_cast<u32>(z) != bit_cast<u32>(*depth);
  256. #endif
  257. break;
  258. case GL_EQUAL:
  259. #ifdef __SSE__
  260. pass = z == *depth;
  261. #else
  262. //
  263. // This is an interesting quirk that occurs due to us using the x87 FPU when Serenity is
  264. // compiled for the i386 target. When we calculate our depth value to be stored in the buffer,
  265. // it is an 80-bit x87 floating point number, however, when stored into the DepthBuffer, this is
  266. // truncated to 32 bits. This 38 bit loss of precision means that when x87 `FCOMP` is eventually
  267. // used here the comparison fails.
  268. // This could be solved by using a `long double` for the depth buffer, however this would take
  269. // up significantly more space and is completely overkill for a depth buffer. As such, comparing
  270. // the first 32-bits of this depth value is "good enough" that if we get a hit on it being
  271. // equal, we can pretty much guarantee that it's actually equal.
  272. //
  273. pass = bit_cast<u32>(z) == bit_cast<u32>(*depth);
  274. #endif
  275. break;
  276. case GL_LEQUAL:
  277. pass = z <= *depth;
  278. break;
  279. case GL_LESS:
  280. pass = z < *depth;
  281. break;
  282. }
  283. if (!pass) {
  284. pixel_mask[y] ^= 1 << x;
  285. continue;
  286. }
  287. if (options.enable_depth_write)
  288. *depth = z;
  289. z_pass_count++;
  290. }
  291. }
  292. // Nice, no pixels passed the depth test -> block rejected by early z
  293. if (z_pass_count == 0)
  294. continue;
  295. }
  296. // We will not update the color buffer at all
  297. if (!options.color_mask || options.draw_buffer == GL_NONE)
  298. continue;
  299. // Draw the pixels according to the previously generated mask
  300. auto coords = b0;
  301. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++, coords += step_y) {
  302. if (pixel_mask[y] == 0) {
  303. coords += dbdx * RASTERIZER_BLOCK_SIZE;
  304. continue;
  305. }
  306. auto* pixel = pixel_buffer[y];
  307. for (int x = 0; x < RASTERIZER_BLOCK_SIZE; x++, coords += dbdx, pixel++) {
  308. if (~pixel_mask[y] & (1 << x))
  309. continue;
  310. // Perspective correct barycentric coordinates
  311. auto barycentric = FloatVector3(coords.x(), coords.y(), coords.z()) * one_over_area;
  312. float interpolated_reciprocal_w = interpolate(triangle.vertices[0].position.w(), triangle.vertices[1].position.w(), triangle.vertices[2].position.w(), barycentric);
  313. float interpolated_w = 1 / interpolated_reciprocal_w;
  314. barycentric = barycentric * FloatVector3(triangle.vertices[0].position.w(), triangle.vertices[1].position.w(), triangle.vertices[2].position.w()) * interpolated_w;
  315. // FIXME: make this more generic. We want to interpolate more than just color and uv
  316. FloatVector4 vertex_color;
  317. if (options.shade_smooth) {
  318. vertex_color = interpolate(
  319. triangle.vertices[0].color,
  320. triangle.vertices[1].color,
  321. triangle.vertices[2].color,
  322. barycentric);
  323. } else {
  324. vertex_color = triangle.vertices[0].color;
  325. }
  326. auto uv = interpolate(
  327. triangle.vertices[0].tex_coord,
  328. triangle.vertices[1].tex_coord,
  329. triangle.vertices[2].tex_coord,
  330. barycentric);
  331. // Calculate depth of fragment for fog
  332. float z = interpolate(triangle.vertices[0].position.z(), triangle.vertices[1].position.z(), triangle.vertices[2].position.z(), barycentric);
  333. z = options.depth_min + (options.depth_max - options.depth_min) * (z + 1) / 2;
  334. *pixel = pixel_shader(uv, vertex_color, z);
  335. }
  336. }
  337. if (options.enable_alpha_test && options.alpha_test_func != GL_ALWAYS) {
  338. // FIXME: I'm not sure if this is the right place to test this.
  339. // If we tested this right at the beginning of our rasterizer routine
  340. // we could skip a lot of work but the GL spec might disagree.
  341. if (options.alpha_test_func == GL_NEVER)
  342. continue;
  343. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++) {
  344. auto src = pixel_buffer[y];
  345. for (int x = 0; x < RASTERIZER_BLOCK_SIZE; x++, src++) {
  346. if (~pixel_mask[y] & (1 << x))
  347. continue;
  348. bool passed = true;
  349. switch (options.alpha_test_func) {
  350. case GL_LESS:
  351. passed = src->w() < options.alpha_test_ref_value;
  352. break;
  353. case GL_EQUAL:
  354. passed = src->w() == options.alpha_test_ref_value;
  355. break;
  356. case GL_LEQUAL:
  357. passed = src->w() <= options.alpha_test_ref_value;
  358. break;
  359. case GL_GREATER:
  360. passed = src->w() > options.alpha_test_ref_value;
  361. break;
  362. case GL_NOTEQUAL:
  363. passed = src->w() != options.alpha_test_ref_value;
  364. break;
  365. case GL_GEQUAL:
  366. passed = src->w() >= options.alpha_test_ref_value;
  367. break;
  368. }
  369. if (!passed)
  370. pixel_mask[y] ^= (1 << x);
  371. }
  372. }
  373. }
  374. if (options.enable_blending) {
  375. // Blend color values from pixel_buffer into render_target
  376. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++) {
  377. auto src = pixel_buffer[y];
  378. auto dst = &render_target.scanline(y + y0)[x0];
  379. for (int x = 0; x < RASTERIZER_BLOCK_SIZE; x++, src++, dst++) {
  380. if (~pixel_mask[y] & (1 << x))
  381. continue;
  382. auto float_dst = to_vec4(*dst);
  383. auto src_factor = src_constant
  384. + *src * src_factor_src_color
  385. + FloatVector4(src->w(), src->w(), src->w(), src->w()) * src_factor_src_alpha
  386. + float_dst * src_factor_dst_color
  387. + FloatVector4(float_dst.w(), float_dst.w(), float_dst.w(), float_dst.w()) * src_factor_dst_alpha;
  388. auto dst_factor = dst_constant
  389. + *src * dst_factor_src_color
  390. + FloatVector4(src->w(), src->w(), src->w(), src->w()) * dst_factor_src_alpha
  391. + float_dst * dst_factor_dst_color
  392. + FloatVector4(float_dst.w(), float_dst.w(), float_dst.w(), float_dst.w()) * dst_factor_dst_alpha;
  393. *dst = (*dst & ~options.color_mask) | (to_rgba32(*src * src_factor + float_dst * dst_factor) & options.color_mask);
  394. }
  395. }
  396. } else {
  397. // Copy color values from pixel_buffer into render_target
  398. for (int y = 0; y < RASTERIZER_BLOCK_SIZE; y++) {
  399. auto src = pixel_buffer[y];
  400. auto dst = &render_target.scanline(y + y0)[x0];
  401. for (int x = 0; x < RASTERIZER_BLOCK_SIZE; x++, src++, dst++) {
  402. if (~pixel_mask[y] & (1 << x))
  403. continue;
  404. *dst = (*dst & ~options.color_mask) | (to_rgba32(*src) & options.color_mask);
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. static Gfx::IntSize closest_multiple(const Gfx::IntSize& min_size, size_t step)
  412. {
  413. int width = ((min_size.width() + step - 1) / step) * step;
  414. int height = ((min_size.height() + step - 1) / step) * step;
  415. return { width, height };
  416. }
  417. SoftwareRasterizer::SoftwareRasterizer(const Gfx::IntSize& min_size)
  418. : m_render_target { Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, closest_multiple(min_size, RASTERIZER_BLOCK_SIZE)).release_value_but_fixme_should_propagate_errors() }
  419. , m_depth_buffer { adopt_own(*new DepthBuffer(closest_multiple(min_size, RASTERIZER_BLOCK_SIZE))) }
  420. {
  421. m_options.scissor_box = m_render_target->rect();
  422. }
  423. void SoftwareRasterizer::submit_triangle(const GLTriangle& triangle, const Array<TextureUnit, 32>& texture_units)
  424. {
  425. rasterize_triangle(m_options, *m_render_target, *m_depth_buffer, triangle, [this, &texture_units](const FloatVector2& uv, const FloatVector4& color, float z) -> FloatVector4 {
  426. FloatVector4 fragment = color;
  427. for (const auto& texture_unit : texture_units) {
  428. // No texture is bound to this texture unit
  429. if (!texture_unit.is_bound())
  430. continue;
  431. // FIXME: Don't assume Texture2D
  432. auto texel = texture_unit.bound_texture_2d()->sampler().sample(uv);
  433. // FIXME: Implement more blend modes
  434. switch (texture_unit.env_mode()) {
  435. case GL_MODULATE:
  436. default:
  437. fragment = fragment * texel;
  438. break;
  439. case GL_REPLACE:
  440. fragment = texel;
  441. break;
  442. case GL_DECAL: {
  443. float src_alpha = fragment.w();
  444. float one_minus_src_alpha = 1 - src_alpha;
  445. fragment.set_x(texel.x() * src_alpha + fragment.x() * one_minus_src_alpha);
  446. fragment.set_y(texel.y() * src_alpha + fragment.y() * one_minus_src_alpha);
  447. fragment.set_z(texel.z() * src_alpha + fragment.z() * one_minus_src_alpha);
  448. break;
  449. }
  450. }
  451. }
  452. // Calculate fog
  453. // Math from here: https://opengl-notes.readthedocs.io/en/latest/topics/texturing/aliasing.html
  454. if (m_options.fog_enabled) {
  455. float factor = 0.0f;
  456. switch (m_options.fog_mode) {
  457. case GL_LINEAR:
  458. factor = (m_options.fog_end - z) / (m_options.fog_end - m_options.fog_start);
  459. break;
  460. case GL_EXP:
  461. factor = exp(-((m_options.fog_density * z)));
  462. break;
  463. case GL_EXP2:
  464. factor = exp(-((m_options.fog_density * z) * (m_options.fog_density * z)));
  465. break;
  466. default:
  467. break;
  468. }
  469. // Mix texel with fog
  470. fragment = mix(m_options.fog_color, fragment, factor);
  471. }
  472. return fragment;
  473. });
  474. }
  475. void SoftwareRasterizer::resize(const Gfx::IntSize& min_size)
  476. {
  477. wait_for_all_threads();
  478. m_render_target = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, closest_multiple(min_size, RASTERIZER_BLOCK_SIZE)).release_value_but_fixme_should_propagate_errors();
  479. m_depth_buffer = adopt_own(*new DepthBuffer(m_render_target->size()));
  480. }
  481. void SoftwareRasterizer::clear_color(const FloatVector4& color)
  482. {
  483. wait_for_all_threads();
  484. uint8_t r = static_cast<uint8_t>(clamp(color.x(), 0.0f, 1.0f) * 255);
  485. uint8_t g = static_cast<uint8_t>(clamp(color.y(), 0.0f, 1.0f) * 255);
  486. uint8_t b = static_cast<uint8_t>(clamp(color.z(), 0.0f, 1.0f) * 255);
  487. uint8_t a = static_cast<uint8_t>(clamp(color.w(), 0.0f, 1.0f) * 255);
  488. auto const fill_color = Gfx::Color(r, g, b, a);
  489. if (m_options.scissor_enabled) {
  490. auto fill_rect = m_render_target->rect();
  491. fill_rect.intersect(m_options.scissor_box);
  492. Gfx::Painter painter { *m_render_target };
  493. painter.fill_rect(fill_rect, fill_color);
  494. return;
  495. }
  496. m_render_target->fill(fill_color);
  497. }
  498. void SoftwareRasterizer::clear_depth(float depth)
  499. {
  500. wait_for_all_threads();
  501. if (m_options.scissor_enabled) {
  502. m_depth_buffer->clear(m_options.scissor_box, depth);
  503. return;
  504. }
  505. m_depth_buffer->clear(depth);
  506. }
  507. void SoftwareRasterizer::blit_to(Gfx::Bitmap& target)
  508. {
  509. wait_for_all_threads();
  510. Gfx::Painter painter { target };
  511. painter.blit({ 0, 0 }, *m_render_target, m_render_target->rect(), 1.0f, false);
  512. }
  513. void SoftwareRasterizer::wait_for_all_threads() const
  514. {
  515. // FIXME: Wait for all render threads to finish when multithreading is being implemented
  516. }
  517. void SoftwareRasterizer::set_options(const RasterizerOptions& options)
  518. {
  519. wait_for_all_threads();
  520. m_options = options;
  521. // FIXME: Recreate or reinitialize render threads here when multithreading is being implemented
  522. }
  523. Gfx::RGBA32 SoftwareRasterizer::get_backbuffer_pixel(int x, int y)
  524. {
  525. // FIXME: Reading individual pixels is very slow, rewrite this to transfer whole blocks
  526. if (x < 0 || y < 0 || x >= m_render_target->width() || y >= m_render_target->height())
  527. return 0;
  528. return m_render_target->scanline(y)[x];
  529. }
  530. float SoftwareRasterizer::get_depthbuffer_value(int x, int y)
  531. {
  532. // FIXME: Reading individual pixels is very slow, rewrite this to transfer whole blocks
  533. if (x < 0 || y < 0 || x >= m_render_target->width() || y >= m_render_target->height())
  534. return 1.0f;
  535. return m_depth_buffer->scanline(y)[x];
  536. }
  537. }