rasterizer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include "common/common_types.h"
  6. #include "math.h"
  7. #include "pica.h"
  8. #include "rasterizer.h"
  9. #include "vertex_shader.h"
  10. #include "debug_utils/debug_utils.h"
  11. namespace Pica {
  12. namespace Rasterizer {
  13. static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
  14. const PAddr addr = registers.framebuffer.GetColorBufferPhysicalAddress();
  15. u32* color_buffer = reinterpret_cast<u32*>(Memory::GetPointer(PAddrToVAddr(addr)));
  16. u32 value = (color.a() << 24) | (color.r() << 16) | (color.g() << 8) | color.b();
  17. // Assuming RGBA8 format until actual framebuffer format handling is implemented
  18. *(color_buffer + x + y * registers.framebuffer.GetWidth()) = value;
  19. }
  20. static const Math::Vec4<u8> GetPixel(int x, int y) {
  21. const PAddr addr = registers.framebuffer.GetColorBufferPhysicalAddress();
  22. u32* color_buffer_u32 = reinterpret_cast<u32*>(Memory::GetPointer(PAddrToVAddr(addr)));
  23. u32 value = *(color_buffer_u32 + x + y * registers.framebuffer.GetWidth());
  24. Math::Vec4<u8> ret;
  25. ret.a() = value >> 24;
  26. ret.r() = (value >> 16) & 0xFF;
  27. ret.g() = (value >> 8) & 0xFF;
  28. ret.b() = value & 0xFF;
  29. return ret;
  30. }
  31. static u32 GetDepth(int x, int y) {
  32. const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress();
  33. u16* depth_buffer = reinterpret_cast<u16*>(Memory::GetPointer(PAddrToVAddr(addr)));
  34. // Assuming 16-bit depth buffer format until actual format handling is implemented
  35. return *(depth_buffer + x + y * registers.framebuffer.GetWidth());
  36. }
  37. static void SetDepth(int x, int y, u16 value) {
  38. const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress();
  39. u16* depth_buffer = reinterpret_cast<u16*>(Memory::GetPointer(PAddrToVAddr(addr)));
  40. // Assuming 16-bit depth buffer format until actual format handling is implemented
  41. *(depth_buffer + x + y * registers.framebuffer.GetWidth()) = value;
  42. }
  43. // NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values
  44. struct Fix12P4 {
  45. Fix12P4() {}
  46. Fix12P4(u16 val) : val(val) {}
  47. static u16 FracMask() { return 0xF; }
  48. static u16 IntMask() { return (u16)~0xF; }
  49. operator u16() const {
  50. return val;
  51. }
  52. bool operator < (const Fix12P4& oth) const {
  53. return (u16)*this < (u16)oth;
  54. }
  55. private:
  56. u16 val;
  57. };
  58. /**
  59. * Calculate signed area of the triangle spanned by the three argument vertices.
  60. * The sign denotes an orientation.
  61. *
  62. * @todo define orientation concretely.
  63. */
  64. static int SignedArea (const Math::Vec2<Fix12P4>& vtx1,
  65. const Math::Vec2<Fix12P4>& vtx2,
  66. const Math::Vec2<Fix12P4>& vtx3) {
  67. const auto vec1 = Math::MakeVec(vtx2 - vtx1, 0);
  68. const auto vec2 = Math::MakeVec(vtx3 - vtx1, 0);
  69. // TODO: There is a very small chance this will overflow for sizeof(int) == 4
  70. return Math::Cross(vec1, vec2).z;
  71. };
  72. void ProcessTriangle(const VertexShader::OutputVertex& v0,
  73. const VertexShader::OutputVertex& v1,
  74. const VertexShader::OutputVertex& v2)
  75. {
  76. // vertex positions in rasterizer coordinates
  77. auto FloatToFix = [](float24 flt) {
  78. return Fix12P4(static_cast<unsigned short>(flt.ToFloat32() * 16.0f));
  79. };
  80. auto ScreenToRasterizerCoordinates = [FloatToFix](const Math::Vec3<float24> vec) {
  81. return Math::Vec3<Fix12P4>{FloatToFix(vec.x), FloatToFix(vec.y), FloatToFix(vec.z)};
  82. };
  83. Math::Vec3<Fix12P4> vtxpos[3]{ ScreenToRasterizerCoordinates(v0.screenpos),
  84. ScreenToRasterizerCoordinates(v1.screenpos),
  85. ScreenToRasterizerCoordinates(v2.screenpos) };
  86. if (registers.cull_mode == Regs::CullMode::KeepClockWise) {
  87. // Reverse vertex order and use the CCW code path.
  88. std::swap(vtxpos[1], vtxpos[2]);
  89. }
  90. if (registers.cull_mode != Regs::CullMode::KeepAll) {
  91. // Cull away triangles which are wound clockwise.
  92. // TODO: A check for degenerate triangles ("== 0") should be considered for CullMode::KeepAll
  93. if (SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) <= 0)
  94. return;
  95. }
  96. // TODO: Proper scissor rect test!
  97. u16 min_x = std::min({vtxpos[0].x, vtxpos[1].x, vtxpos[2].x});
  98. u16 min_y = std::min({vtxpos[0].y, vtxpos[1].y, vtxpos[2].y});
  99. u16 max_x = std::max({vtxpos[0].x, vtxpos[1].x, vtxpos[2].x});
  100. u16 max_y = std::max({vtxpos[0].y, vtxpos[1].y, vtxpos[2].y});
  101. min_x &= Fix12P4::IntMask();
  102. min_y &= Fix12P4::IntMask();
  103. max_x = ((max_x + Fix12P4::FracMask()) & Fix12P4::IntMask());
  104. max_y = ((max_y + Fix12P4::FracMask()) & Fix12P4::IntMask());
  105. // Triangle filling rules: Pixels on the right-sided edge or on flat bottom edges are not
  106. // drawn. Pixels on any other triangle border are drawn. This is implemented with three bias
  107. // values which are added to the barycentric coordinates w0, w1 and w2, respectively.
  108. // NOTE: These are the PSP filling rules. Not sure if the 3DS uses the same ones...
  109. auto IsRightSideOrFlatBottomEdge = [](const Math::Vec2<Fix12P4>& vtx,
  110. const Math::Vec2<Fix12P4>& line1,
  111. const Math::Vec2<Fix12P4>& line2)
  112. {
  113. if (line1.y == line2.y) {
  114. // just check if vertex is above us => bottom line parallel to x-axis
  115. return vtx.y < line1.y;
  116. } else {
  117. // check if vertex is on our left => right side
  118. // TODO: Not sure how likely this is to overflow
  119. return (int)vtx.x < (int)line1.x + ((int)line2.x - (int)line1.x) * ((int)vtx.y - (int)line1.y) / ((int)line2.y - (int)line1.y);
  120. }
  121. };
  122. int bias0 = IsRightSideOrFlatBottomEdge(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) ? -1 : 0;
  123. int bias1 = IsRightSideOrFlatBottomEdge(vtxpos[1].xy(), vtxpos[2].xy(), vtxpos[0].xy()) ? -1 : 0;
  124. int bias2 = IsRightSideOrFlatBottomEdge(vtxpos[2].xy(), vtxpos[0].xy(), vtxpos[1].xy()) ? -1 : 0;
  125. auto w_inverse = Math::MakeVec(v0.pos.w, v1.pos.w, v2.pos.w);
  126. auto textures = registers.GetTextures();
  127. auto tev_stages = registers.GetTevStages();
  128. // TODO: Not sure if looping through x first might be faster
  129. for (u16 y = min_y; y < max_y; y += 0x10) {
  130. for (u16 x = min_x; x < max_x; x += 0x10) {
  131. // Calculate the barycentric coordinates w0, w1 and w2
  132. int w0 = bias0 + SignedArea(vtxpos[1].xy(), vtxpos[2].xy(), {x, y});
  133. int w1 = bias1 + SignedArea(vtxpos[2].xy(), vtxpos[0].xy(), {x, y});
  134. int w2 = bias2 + SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), {x, y});
  135. int wsum = w0 + w1 + w2;
  136. // If current pixel is not covered by the current primitive
  137. if (w0 < 0 || w1 < 0 || w2 < 0)
  138. continue;
  139. auto baricentric_coordinates = Math::MakeVec(float24::FromFloat32(static_cast<float>(w0)),
  140. float24::FromFloat32(static_cast<float>(w1)),
  141. float24::FromFloat32(static_cast<float>(w2)));
  142. float24 interpolated_w_inverse = float24::FromFloat32(1.0f) / Math::Dot(w_inverse, baricentric_coordinates);
  143. // Perspective correct attribute interpolation:
  144. // Attribute values cannot be calculated by simple linear interpolation since
  145. // they are not linear in screen space. For example, when interpolating a
  146. // texture coordinate across two vertices, something simple like
  147. // u = (u0*w0 + u1*w1)/(w0+w1)
  148. // will not work. However, the attribute value divided by the
  149. // clipspace w-coordinate (u/w) and and the inverse w-coordinate (1/w) are linear
  150. // in screenspace. Hence, we can linearly interpolate these two independently and
  151. // calculate the interpolated attribute by dividing the results.
  152. // I.e.
  153. // u_over_w = ((u0/v0.pos.w)*w0 + (u1/v1.pos.w)*w1)/(w0+w1)
  154. // one_over_w = (( 1/v0.pos.w)*w0 + ( 1/v1.pos.w)*w1)/(w0+w1)
  155. // u = u_over_w / one_over_w
  156. //
  157. // The generalization to three vertices is straightforward in baricentric coordinates.
  158. auto GetInterpolatedAttribute = [&](float24 attr0, float24 attr1, float24 attr2) {
  159. auto attr_over_w = Math::MakeVec(attr0, attr1, attr2);
  160. float24 interpolated_attr_over_w = Math::Dot(attr_over_w, baricentric_coordinates);
  161. return interpolated_attr_over_w * interpolated_w_inverse;
  162. };
  163. Math::Vec4<u8> primary_color{
  164. (u8)(GetInterpolatedAttribute(v0.color.r(), v1.color.r(), v2.color.r()).ToFloat32() * 255),
  165. (u8)(GetInterpolatedAttribute(v0.color.g(), v1.color.g(), v2.color.g()).ToFloat32() * 255),
  166. (u8)(GetInterpolatedAttribute(v0.color.b(), v1.color.b(), v2.color.b()).ToFloat32() * 255),
  167. (u8)(GetInterpolatedAttribute(v0.color.a(), v1.color.a(), v2.color.a()).ToFloat32() * 255)
  168. };
  169. Math::Vec2<float24> uv[3];
  170. uv[0].u() = GetInterpolatedAttribute(v0.tc0.u(), v1.tc0.u(), v2.tc0.u());
  171. uv[0].v() = GetInterpolatedAttribute(v0.tc0.v(), v1.tc0.v(), v2.tc0.v());
  172. uv[1].u() = GetInterpolatedAttribute(v0.tc1.u(), v1.tc1.u(), v2.tc1.u());
  173. uv[1].v() = GetInterpolatedAttribute(v0.tc1.v(), v1.tc1.v(), v2.tc1.v());
  174. uv[2].u() = GetInterpolatedAttribute(v0.tc2.u(), v1.tc2.u(), v2.tc2.u());
  175. uv[2].v() = GetInterpolatedAttribute(v0.tc2.v(), v1.tc2.v(), v2.tc2.v());
  176. Math::Vec4<u8> texture_color[3]{};
  177. for (int i = 0; i < 3; ++i) {
  178. const auto& texture = textures[i];
  179. if (!texture.enabled)
  180. continue;
  181. _dbg_assert_(HW_GPU, 0 != texture.config.address);
  182. int s = (int)(uv[i].u() * float24::FromFloat32(static_cast<float>(texture.config.width))).ToFloat32();
  183. int t = (int)(uv[i].v() * float24::FromFloat32(static_cast<float>(texture.config.height))).ToFloat32();
  184. auto GetWrappedTexCoord = [](Regs::TextureConfig::WrapMode mode, int val, unsigned size) {
  185. switch (mode) {
  186. case Regs::TextureConfig::ClampToEdge:
  187. val = std::max(val, 0);
  188. val = std::min(val, (int)size - 1);
  189. return val;
  190. case Regs::TextureConfig::Repeat:
  191. return (int)(((unsigned)val) % size);
  192. default:
  193. LOG_ERROR(HW_GPU, "Unknown texture coordinate wrapping mode %x\n", (int)mode);
  194. _dbg_assert_(HW_GPU, 0);
  195. return 0;
  196. }
  197. };
  198. s = GetWrappedTexCoord(texture.config.wrap_s, s, texture.config.width);
  199. t = texture.config.height - 1 - GetWrappedTexCoord(texture.config.wrap_t, t, texture.config.height);
  200. u8* texture_data = Memory::GetPointer(PAddrToVAddr(texture.config.GetPhysicalAddress()));
  201. auto info = DebugUtils::TextureInfo::FromPicaRegister(texture.config, texture.format);
  202. texture_color[i] = DebugUtils::LookupTexture(texture_data, s, t, info);
  203. DebugUtils::DumpTexture(texture.config, texture_data);
  204. }
  205. // Texture environment - consists of 6 stages of color and alpha combining.
  206. //
  207. // Color combiners take three input color values from some source (e.g. interpolated
  208. // vertex color, texture color, previous stage, etc), perform some very simple
  209. // operations on each of them (e.g. inversion) and then calculate the output color
  210. // with some basic arithmetic. Alpha combiners can be configured separately but work
  211. // analogously.
  212. Math::Vec4<u8> combiner_output;
  213. for (const auto& tev_stage : tev_stages) {
  214. using Source = Regs::TevStageConfig::Source;
  215. using ColorModifier = Regs::TevStageConfig::ColorModifier;
  216. using AlphaModifier = Regs::TevStageConfig::AlphaModifier;
  217. using Operation = Regs::TevStageConfig::Operation;
  218. auto GetColorSource = [&](Source source) -> Math::Vec4<u8> {
  219. switch (source) {
  220. case Source::PrimaryColor:
  221. return primary_color;
  222. case Source::Texture0:
  223. return texture_color[0];
  224. case Source::Texture1:
  225. return texture_color[1];
  226. case Source::Texture2:
  227. return texture_color[2];
  228. case Source::Constant:
  229. return {tev_stage.const_r, tev_stage.const_g, tev_stage.const_b, tev_stage.const_a};
  230. case Source::Previous:
  231. return combiner_output;
  232. default:
  233. LOG_ERROR(HW_GPU, "Unknown color combiner source %d\n", (int)source);
  234. _dbg_assert_(HW_GPU, 0);
  235. return {};
  236. }
  237. };
  238. auto GetAlphaSource = [&](Source source) -> u8 {
  239. switch (source) {
  240. case Source::PrimaryColor:
  241. return primary_color.a();
  242. case Source::Texture0:
  243. return texture_color[0].a();
  244. case Source::Texture1:
  245. return texture_color[1].a();
  246. case Source::Texture2:
  247. return texture_color[2].a();
  248. case Source::Constant:
  249. return tev_stage.const_a;
  250. case Source::Previous:
  251. return combiner_output.a();
  252. default:
  253. LOG_ERROR(HW_GPU, "Unknown alpha combiner source %d\n", (int)source);
  254. _dbg_assert_(HW_GPU, 0);
  255. return 0;
  256. }
  257. };
  258. static auto GetColorModifier = [](ColorModifier factor, const Math::Vec4<u8>& values) -> Math::Vec3<u8> {
  259. switch (factor)
  260. {
  261. case ColorModifier::SourceColor:
  262. return values.rgb();
  263. case ColorModifier::OneMinusSourceColor:
  264. return (Math::Vec3<u8>(255, 255, 255) - values.rgb()).Cast<u8>();
  265. case ColorModifier::SourceAlpha:
  266. return { values.a(), values.a(), values.a() };
  267. default:
  268. LOG_ERROR(HW_GPU, "Unknown color factor %d\n", (int)factor);
  269. _dbg_assert_(HW_GPU, 0);
  270. return {};
  271. }
  272. };
  273. static auto GetAlphaModifier = [](AlphaModifier factor, u8 value) -> u8 {
  274. switch (factor) {
  275. case AlphaModifier::SourceAlpha:
  276. return value;
  277. case AlphaModifier::OneMinusSourceAlpha:
  278. return 255 - value;
  279. default:
  280. LOG_ERROR(HW_GPU, "Unknown alpha factor %d\n", (int)factor);
  281. _dbg_assert_(HW_GPU, 0);
  282. return 0;
  283. }
  284. };
  285. static auto ColorCombine = [](Operation op, const Math::Vec3<u8> input[3]) -> Math::Vec3<u8> {
  286. switch (op) {
  287. case Operation::Replace:
  288. return input[0];
  289. case Operation::Modulate:
  290. return ((input[0] * input[1]) / 255).Cast<u8>();
  291. case Operation::Add:
  292. {
  293. auto result = input[0] + input[1];
  294. result.r() = std::min(255, result.r());
  295. result.g() = std::min(255, result.g());
  296. result.b() = std::min(255, result.b());
  297. return result.Cast<u8>();
  298. }
  299. case Operation::Lerp:
  300. return ((input[0] * input[2] + input[1] * (Math::MakeVec<u8>(255, 255, 255) - input[2]).Cast<u8>()) / 255).Cast<u8>();
  301. case Operation::Subtract:
  302. {
  303. auto result = input[0].Cast<int>() - input[1].Cast<int>();
  304. result.r() = std::max(0, result.r());
  305. result.g() = std::max(0, result.g());
  306. result.b() = std::max(0, result.b());
  307. return result.Cast<u8>();
  308. }
  309. default:
  310. LOG_ERROR(HW_GPU, "Unknown color combiner operation %d\n", (int)op);
  311. _dbg_assert_(HW_GPU, 0);
  312. return {};
  313. }
  314. };
  315. static auto AlphaCombine = [](Operation op, const std::array<u8,3>& input) -> u8 {
  316. switch (op) {
  317. case Operation::Replace:
  318. return input[0];
  319. case Operation::Modulate:
  320. return input[0] * input[1] / 255;
  321. case Operation::Add:
  322. return std::min(255, input[0] + input[1]);
  323. case Operation::Lerp:
  324. return (input[0] * input[2] + input[1] * (255 - input[2])) / 255;
  325. case Operation::Subtract:
  326. return std::max(0, (int)input[0] - (int)input[1]);
  327. default:
  328. LOG_ERROR(HW_GPU, "Unknown alpha combiner operation %d\n", (int)op);
  329. _dbg_assert_(HW_GPU, 0);
  330. return 0;
  331. }
  332. };
  333. // color combiner
  334. // NOTE: Not sure if the alpha combiner might use the color output of the previous
  335. // stage as input. Hence, we currently don't directly write the result to
  336. // combiner_output.rgb(), but instead store it in a temporary variable until
  337. // alpha combining has been done.
  338. Math::Vec3<u8> color_result[3] = {
  339. GetColorModifier(tev_stage.color_modifier1, GetColorSource(tev_stage.color_source1)),
  340. GetColorModifier(tev_stage.color_modifier2, GetColorSource(tev_stage.color_source2)),
  341. GetColorModifier(tev_stage.color_modifier3, GetColorSource(tev_stage.color_source3))
  342. };
  343. auto color_output = ColorCombine(tev_stage.color_op, color_result);
  344. // alpha combiner
  345. std::array<u8,3> alpha_result = {
  346. GetAlphaModifier(tev_stage.alpha_modifier1, GetAlphaSource(tev_stage.alpha_source1)),
  347. GetAlphaModifier(tev_stage.alpha_modifier2, GetAlphaSource(tev_stage.alpha_source2)),
  348. GetAlphaModifier(tev_stage.alpha_modifier3, GetAlphaSource(tev_stage.alpha_source3))
  349. };
  350. auto alpha_output = AlphaCombine(tev_stage.alpha_op, alpha_result);
  351. combiner_output = Math::MakeVec(color_output, alpha_output);
  352. }
  353. if (registers.output_merger.alpha_test.enable) {
  354. bool pass = false;
  355. switch (registers.output_merger.alpha_test.func) {
  356. case registers.output_merger.Never:
  357. pass = false;
  358. break;
  359. case registers.output_merger.Always:
  360. pass = true;
  361. break;
  362. case registers.output_merger.Equal:
  363. pass = combiner_output.a() == registers.output_merger.alpha_test.ref;
  364. break;
  365. case registers.output_merger.NotEqual:
  366. pass = combiner_output.a() != registers.output_merger.alpha_test.ref;
  367. break;
  368. case registers.output_merger.LessThan:
  369. pass = combiner_output.a() < registers.output_merger.alpha_test.ref;
  370. break;
  371. case registers.output_merger.LessThanOrEqual:
  372. pass = combiner_output.a() <= registers.output_merger.alpha_test.ref;
  373. break;
  374. case registers.output_merger.GreaterThan:
  375. pass = combiner_output.a() > registers.output_merger.alpha_test.ref;
  376. break;
  377. case registers.output_merger.GreaterThanOrEqual:
  378. pass = combiner_output.a() >= registers.output_merger.alpha_test.ref;
  379. break;
  380. }
  381. if (!pass)
  382. continue;
  383. }
  384. // TODO: Does depth indeed only get written even if depth testing is enabled?
  385. if (registers.output_merger.depth_test_enable) {
  386. u16 z = (u16)(-(v0.screenpos[2].ToFloat32() * w0 +
  387. v1.screenpos[2].ToFloat32() * w1 +
  388. v2.screenpos[2].ToFloat32() * w2) * 65535.f / wsum);
  389. u16 ref_z = GetDepth(x >> 4, y >> 4);
  390. bool pass = false;
  391. switch (registers.output_merger.depth_test_func) {
  392. case registers.output_merger.Never:
  393. pass = false;
  394. break;
  395. case registers.output_merger.Always:
  396. pass = true;
  397. break;
  398. case registers.output_merger.Equal:
  399. pass = z == ref_z;
  400. break;
  401. case registers.output_merger.NotEqual:
  402. pass = z != ref_z;
  403. break;
  404. case registers.output_merger.LessThan:
  405. pass = z < ref_z;
  406. break;
  407. case registers.output_merger.LessThanOrEqual:
  408. pass = z <= ref_z;
  409. break;
  410. case registers.output_merger.GreaterThan:
  411. pass = z > ref_z;
  412. break;
  413. case registers.output_merger.GreaterThanOrEqual:
  414. pass = z >= ref_z;
  415. break;
  416. }
  417. if (!pass)
  418. continue;
  419. if (registers.output_merger.depth_write_enable)
  420. SetDepth(x >> 4, y >> 4, z);
  421. }
  422. auto dest = GetPixel(x >> 4, y >> 4);
  423. if (registers.output_merger.alphablend_enable) {
  424. auto params = registers.output_merger.alpha_blending;
  425. auto LookupFactorRGB = [&](decltype(params)::BlendFactor factor) -> Math::Vec3<u8> {
  426. switch(factor) {
  427. case params.Zero:
  428. return Math::Vec3<u8>(0, 0, 0);
  429. case params.One:
  430. return Math::Vec3<u8>(255, 255, 255);
  431. case params.SourceAlpha:
  432. return Math::MakeVec(combiner_output.a(), combiner_output.a(), combiner_output.a());
  433. case params.OneMinusSourceAlpha:
  434. return Math::Vec3<u8>(255-combiner_output.a(), 255-combiner_output.a(), 255-combiner_output.a());
  435. default:
  436. LOG_CRITICAL(HW_GPU, "Unknown color blend factor %x", factor);
  437. exit(0);
  438. break;
  439. }
  440. };
  441. auto LookupFactorA = [&](decltype(params)::BlendFactor factor) -> u8 {
  442. switch(factor) {
  443. case params.Zero:
  444. return 0;
  445. case params.One:
  446. return 255;
  447. case params.SourceAlpha:
  448. return combiner_output.a();
  449. case params.OneMinusSourceAlpha:
  450. return 255 - combiner_output.a();
  451. default:
  452. LOG_CRITICAL(HW_GPU, "Unknown alpha blend factor %x", factor);
  453. exit(0);
  454. break;
  455. }
  456. };
  457. auto srcfactor = Math::MakeVec(LookupFactorRGB(params.factor_source_rgb),
  458. LookupFactorA(params.factor_source_a));
  459. auto dstfactor = Math::MakeVec(LookupFactorRGB(params.factor_dest_rgb),
  460. LookupFactorA(params.factor_dest_a));
  461. switch (params.blend_equation_rgb) {
  462. case params.Add:
  463. {
  464. auto result = (combiner_output * srcfactor + dest * dstfactor) / 255;
  465. result.r() = std::min(255, result.r());
  466. result.g() = std::min(255, result.g());
  467. result.b() = std::min(255, result.b());
  468. combiner_output = result.Cast<u8>();
  469. break;
  470. }
  471. default:
  472. LOG_CRITICAL(HW_GPU, "Unknown RGB blend equation %x", params.blend_equation_rgb.Value());
  473. exit(0);
  474. }
  475. } else {
  476. LOG_CRITICAL(HW_GPU, "logic op: %x", registers.output_merger.logic_op);
  477. exit(0);
  478. }
  479. const Math::Vec4<u8> result = {
  480. registers.output_merger.red_enable ? combiner_output.r() : dest.r(),
  481. registers.output_merger.green_enable ? combiner_output.g() : dest.g(),
  482. registers.output_merger.blue_enable ? combiner_output.b() : dest.b(),
  483. registers.output_merger.alpha_enable ? combiner_output.a() : dest.a()
  484. };
  485. DrawPixel(x >> 4, y >> 4, result);
  486. }
  487. }
  488. }
  489. } // namespace Rasterizer
  490. } // namespace Pica