SoftwareRasterizer.cpp 26 KB

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