rasterizer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 GetSource = [&](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. static auto GetColorModifier = [](ColorModifier factor, const Math::Vec4<u8>& values) -> Math::Vec3<u8> {
  239. switch (factor) {
  240. case ColorModifier::SourceColor:
  241. return values.rgb();
  242. case ColorModifier::OneMinusSourceColor:
  243. return (Math::Vec3<u8>(255, 255, 255) - values.rgb()).Cast<u8>();
  244. case ColorModifier::SourceAlpha:
  245. return values.aaa();
  246. case ColorModifier::OneMinusSourceAlpha:
  247. return (Math::Vec3<u8>(255, 255, 255) - values.aaa()).Cast<u8>();
  248. case ColorModifier::SourceRed:
  249. return values.rrr();
  250. case ColorModifier::OneMinusSourceRed:
  251. return (Math::Vec3<u8>(255, 255, 255) - values.rrr()).Cast<u8>();
  252. case ColorModifier::SourceGreen:
  253. return values.ggg();
  254. case ColorModifier::OneMinusSourceGreen:
  255. return (Math::Vec3<u8>(255, 255, 255) - values.ggg()).Cast<u8>();
  256. case ColorModifier::SourceBlue:
  257. return values.bbb();
  258. case ColorModifier::OneMinusSourceBlue:
  259. return (Math::Vec3<u8>(255, 255, 255) - values.bbb()).Cast<u8>();
  260. }
  261. };
  262. static auto GetAlphaModifier = [](AlphaModifier factor, const Math::Vec4<u8>& values) -> u8 {
  263. switch (factor) {
  264. case AlphaModifier::SourceAlpha:
  265. return values.a();
  266. case AlphaModifier::OneMinusSourceAlpha:
  267. return 255 - values.a();
  268. case AlphaModifier::SourceRed:
  269. return values.r();
  270. case AlphaModifier::OneMinusSourceRed:
  271. return 255 - values.r();
  272. case AlphaModifier::SourceGreen:
  273. return values.g();
  274. case AlphaModifier::OneMinusSourceGreen:
  275. return 255 - values.g();
  276. case AlphaModifier::SourceBlue:
  277. return values.b();
  278. case AlphaModifier::OneMinusSourceBlue:
  279. return 255 - values.b();
  280. }
  281. };
  282. static auto ColorCombine = [](Operation op, const Math::Vec3<u8> input[3]) -> Math::Vec3<u8> {
  283. switch (op) {
  284. case Operation::Replace:
  285. return input[0];
  286. case Operation::Modulate:
  287. return ((input[0] * input[1]) / 255).Cast<u8>();
  288. case Operation::Add:
  289. {
  290. auto result = input[0] + input[1];
  291. result.r() = std::min(255, result.r());
  292. result.g() = std::min(255, result.g());
  293. result.b() = std::min(255, result.b());
  294. return result.Cast<u8>();
  295. }
  296. case Operation::Lerp:
  297. return ((input[0] * input[2] + input[1] * (Math::MakeVec<u8>(255, 255, 255) - input[2]).Cast<u8>()) / 255).Cast<u8>();
  298. case Operation::Subtract:
  299. {
  300. auto result = input[0].Cast<int>() - input[1].Cast<int>();
  301. result.r() = std::max(0, result.r());
  302. result.g() = std::max(0, result.g());
  303. result.b() = std::max(0, result.b());
  304. return result.Cast<u8>();
  305. }
  306. default:
  307. LOG_ERROR(HW_GPU, "Unknown color combiner operation %d\n", (int)op);
  308. _dbg_assert_(HW_GPU, 0);
  309. return {};
  310. }
  311. };
  312. static auto AlphaCombine = [](Operation op, const std::array<u8,3>& input) -> u8 {
  313. switch (op) {
  314. case Operation::Replace:
  315. return input[0];
  316. case Operation::Modulate:
  317. return input[0] * input[1] / 255;
  318. case Operation::Add:
  319. return std::min(255, input[0] + input[1]);
  320. case Operation::Lerp:
  321. return (input[0] * input[2] + input[1] * (255 - input[2])) / 255;
  322. case Operation::Subtract:
  323. return std::max(0, (int)input[0] - (int)input[1]);
  324. default:
  325. LOG_ERROR(HW_GPU, "Unknown alpha combiner operation %d\n", (int)op);
  326. _dbg_assert_(HW_GPU, 0);
  327. return 0;
  328. }
  329. };
  330. // color combiner
  331. // NOTE: Not sure if the alpha combiner might use the color output of the previous
  332. // stage as input. Hence, we currently don't directly write the result to
  333. // combiner_output.rgb(), but instead store it in a temporary variable until
  334. // alpha combining has been done.
  335. Math::Vec3<u8> color_result[3] = {
  336. GetColorModifier(tev_stage.color_modifier1, GetSource(tev_stage.color_source1)),
  337. GetColorModifier(tev_stage.color_modifier2, GetSource(tev_stage.color_source2)),
  338. GetColorModifier(tev_stage.color_modifier3, GetSource(tev_stage.color_source3))
  339. };
  340. auto color_output = ColorCombine(tev_stage.color_op, color_result);
  341. // alpha combiner
  342. std::array<u8,3> alpha_result = {
  343. GetAlphaModifier(tev_stage.alpha_modifier1, GetSource(tev_stage.alpha_source1)),
  344. GetAlphaModifier(tev_stage.alpha_modifier2, GetSource(tev_stage.alpha_source2)),
  345. GetAlphaModifier(tev_stage.alpha_modifier3, GetSource(tev_stage.alpha_source3))
  346. };
  347. auto alpha_output = AlphaCombine(tev_stage.alpha_op, alpha_result);
  348. combiner_output = Math::MakeVec(color_output, alpha_output);
  349. }
  350. if (registers.output_merger.alpha_test.enable) {
  351. bool pass = false;
  352. switch (registers.output_merger.alpha_test.func) {
  353. case registers.output_merger.Never:
  354. pass = false;
  355. break;
  356. case registers.output_merger.Always:
  357. pass = true;
  358. break;
  359. case registers.output_merger.Equal:
  360. pass = combiner_output.a() == registers.output_merger.alpha_test.ref;
  361. break;
  362. case registers.output_merger.NotEqual:
  363. pass = combiner_output.a() != registers.output_merger.alpha_test.ref;
  364. break;
  365. case registers.output_merger.LessThan:
  366. pass = combiner_output.a() < registers.output_merger.alpha_test.ref;
  367. break;
  368. case registers.output_merger.LessThanOrEqual:
  369. pass = combiner_output.a() <= registers.output_merger.alpha_test.ref;
  370. break;
  371. case registers.output_merger.GreaterThan:
  372. pass = combiner_output.a() > registers.output_merger.alpha_test.ref;
  373. break;
  374. case registers.output_merger.GreaterThanOrEqual:
  375. pass = combiner_output.a() >= registers.output_merger.alpha_test.ref;
  376. break;
  377. }
  378. if (!pass)
  379. continue;
  380. }
  381. // TODO: Does depth indeed only get written even if depth testing is enabled?
  382. if (registers.output_merger.depth_test_enable) {
  383. u16 z = (u16)(-(v0.screenpos[2].ToFloat32() * w0 +
  384. v1.screenpos[2].ToFloat32() * w1 +
  385. v2.screenpos[2].ToFloat32() * w2) * 65535.f / wsum);
  386. u16 ref_z = GetDepth(x >> 4, y >> 4);
  387. bool pass = false;
  388. switch (registers.output_merger.depth_test_func) {
  389. case registers.output_merger.Never:
  390. pass = false;
  391. break;
  392. case registers.output_merger.Always:
  393. pass = true;
  394. break;
  395. case registers.output_merger.Equal:
  396. pass = z == ref_z;
  397. break;
  398. case registers.output_merger.NotEqual:
  399. pass = z != ref_z;
  400. break;
  401. case registers.output_merger.LessThan:
  402. pass = z < ref_z;
  403. break;
  404. case registers.output_merger.LessThanOrEqual:
  405. pass = z <= ref_z;
  406. break;
  407. case registers.output_merger.GreaterThan:
  408. pass = z > ref_z;
  409. break;
  410. case registers.output_merger.GreaterThanOrEqual:
  411. pass = z >= ref_z;
  412. break;
  413. }
  414. if (!pass)
  415. continue;
  416. if (registers.output_merger.depth_write_enable)
  417. SetDepth(x >> 4, y >> 4, z);
  418. }
  419. auto dest = GetPixel(x >> 4, y >> 4);
  420. if (registers.output_merger.alphablend_enable) {
  421. auto params = registers.output_merger.alpha_blending;
  422. auto LookupFactorRGB = [&](decltype(params)::BlendFactor factor) -> Math::Vec3<u8> {
  423. switch (factor) {
  424. case params.Zero:
  425. return Math::Vec3<u8>(0, 0, 0);
  426. case params.One:
  427. return Math::Vec3<u8>(255, 255, 255);
  428. case params.SourceColor:
  429. return combiner_output.rgb();
  430. case params.OneMinusSourceColor:
  431. return Math::Vec3<u8>(255 - combiner_output.r(), 255 - combiner_output.g(), 255 - combiner_output.b());
  432. case params.DestColor:
  433. return dest.rgb();
  434. case params.OneMinusDestColor:
  435. return Math::Vec3<u8>(255 - dest.r(), 255 - dest.g(), 255 - dest.b());
  436. case params.SourceAlpha:
  437. return Math::Vec3<u8>(combiner_output.a(), combiner_output.a(), combiner_output.a());
  438. case params.OneMinusSourceAlpha:
  439. return Math::Vec3<u8>(255 - combiner_output.a(), 255 - combiner_output.a(), 255 - combiner_output.a());
  440. case params.DestAlpha:
  441. return Math::Vec3<u8>(dest.a(), dest.a(), dest.a());
  442. case params.OneMinusDestAlpha:
  443. return Math::Vec3<u8>(255 - dest.a(), 255 - dest.a(), 255 - dest.a());
  444. case params.ConstantColor:
  445. return Math::Vec3<u8>(registers.output_merger.blend_const.r, registers.output_merger.blend_const.g, registers.output_merger.blend_const.b);
  446. case params.OneMinusConstantColor:
  447. return Math::Vec3<u8>(255 - registers.output_merger.blend_const.r, 255 - registers.output_merger.blend_const.g, 255 - registers.output_merger.blend_const.b);
  448. case params.ConstantAlpha:
  449. return Math::Vec3<u8>(registers.output_merger.blend_const.a, registers.output_merger.blend_const.a, registers.output_merger.blend_const.a);
  450. case params.OneMinusConstantAlpha:
  451. return Math::Vec3<u8>(255 - registers.output_merger.blend_const.a, 255 - registers.output_merger.blend_const.a, 255 - registers.output_merger.blend_const.a);
  452. default:
  453. LOG_CRITICAL(HW_GPU, "Unknown color blend factor %x", factor);
  454. exit(0);
  455. break;
  456. }
  457. };
  458. auto LookupFactorA = [&](decltype(params)::BlendFactor factor) -> u8 {
  459. switch (factor) {
  460. case params.Zero:
  461. return 0;
  462. case params.One:
  463. return 255;
  464. case params.SourceAlpha:
  465. return combiner_output.a();
  466. case params.OneMinusSourceAlpha:
  467. return 255 - combiner_output.a();
  468. case params.DestAlpha:
  469. return dest.a();
  470. case params.OneMinusDestAlpha:
  471. return 255 - dest.a();
  472. case params.ConstantAlpha:
  473. return registers.output_merger.blend_const.a;
  474. case params.OneMinusConstantAlpha:
  475. return 255 - registers.output_merger.blend_const.a;
  476. default:
  477. LOG_CRITICAL(HW_GPU, "Unknown alpha blend factor %x", factor);
  478. exit(0);
  479. break;
  480. }
  481. };
  482. auto srcfactor = Math::MakeVec(LookupFactorRGB(params.factor_source_rgb),
  483. LookupFactorA(params.factor_source_a));
  484. auto dstfactor = Math::MakeVec(LookupFactorRGB(params.factor_dest_rgb),
  485. LookupFactorA(params.factor_dest_a));
  486. switch (params.blend_equation_rgb) {
  487. case params.Add:
  488. {
  489. auto result = (combiner_output * srcfactor + dest * dstfactor) / 255;
  490. result.r() = std::min(255, result.r());
  491. result.g() = std::min(255, result.g());
  492. result.b() = std::min(255, result.b());
  493. combiner_output = result.Cast<u8>();
  494. break;
  495. }
  496. default:
  497. LOG_CRITICAL(HW_GPU, "Unknown RGB blend equation %x", params.blend_equation_rgb.Value());
  498. exit(0);
  499. }
  500. } else {
  501. LOG_CRITICAL(HW_GPU, "logic op: %x", registers.output_merger.logic_op);
  502. exit(0);
  503. }
  504. const Math::Vec4<u8> result = {
  505. registers.output_merger.red_enable ? combiner_output.r() : dest.r(),
  506. registers.output_merger.green_enable ? combiner_output.g() : dest.g(),
  507. registers.output_merger.blue_enable ? combiner_output.b() : dest.b(),
  508. registers.output_merger.alpha_enable ? combiner_output.a() : dest.a()
  509. };
  510. DrawPixel(x >> 4, y >> 4, result);
  511. }
  512. }
  513. }
  514. } // namespace Rasterizer
  515. } // namespace Pica