RenderState.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // Copyright 2016 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoCommon/RenderState.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include "VideoCommon/BPMemory.h"
  7. #include "VideoCommon/TextureConfig.h"
  8. void RasterizationState::Generate(const BPMemory& bp, PrimitiveType primitive_type)
  9. {
  10. cullmode = bp.genMode.cullmode;
  11. primitive = primitive_type;
  12. // Back-face culling should be disabled for points/lines.
  13. if (primitive_type != PrimitiveType::Triangles && primitive_type != PrimitiveType::TriangleStrip)
  14. cullmode = CullMode::None;
  15. }
  16. void DepthState::Generate(const BPMemory& bp)
  17. {
  18. testenable = bp.zmode.testenable.Value();
  19. updateenable = bp.zmode.updateenable.Value();
  20. func = bp.zmode.func.Value();
  21. }
  22. static bool IsDualSrc(SrcBlendFactor factor)
  23. {
  24. return factor == SrcBlendFactor::SrcAlpha || factor == SrcBlendFactor::InvSrcAlpha;
  25. }
  26. static bool IsDualSrc(DstBlendFactor factor)
  27. {
  28. return factor == DstBlendFactor::SrcAlpha || factor == DstBlendFactor::InvSrcAlpha;
  29. }
  30. bool BlendingState::RequiresDualSrc() const
  31. {
  32. bool requires_dual_src = false;
  33. requires_dual_src |= IsDualSrc(srcfactor) || IsDualSrc(srcfactoralpha);
  34. requires_dual_src |= IsDualSrc(dstfactor) || IsDualSrc(dstfactoralpha);
  35. requires_dual_src &= blendenable && usedualsrc;
  36. return requires_dual_src;
  37. }
  38. // If the framebuffer format has no alpha channel, it is assumed to
  39. // ONE on blending. As the backends may emulate this framebuffer
  40. // configuration with an alpha channel, we just drop all references
  41. // to the destination alpha channel.
  42. static SrcBlendFactor RemoveDstAlphaUsage(SrcBlendFactor factor)
  43. {
  44. switch (factor)
  45. {
  46. case SrcBlendFactor::DstAlpha:
  47. return SrcBlendFactor::One;
  48. case SrcBlendFactor::InvDstAlpha:
  49. return SrcBlendFactor::Zero;
  50. default:
  51. return factor;
  52. }
  53. }
  54. static DstBlendFactor RemoveDstAlphaUsage(DstBlendFactor factor)
  55. {
  56. switch (factor)
  57. {
  58. case DstBlendFactor::DstAlpha:
  59. return DstBlendFactor::One;
  60. case DstBlendFactor::InvDstAlpha:
  61. return DstBlendFactor::Zero;
  62. default:
  63. return factor;
  64. }
  65. }
  66. // We separate the blending parameter for rgb and alpha. For blending
  67. // the alpha component, CLR and ALPHA are indentical. So just always
  68. // use ALPHA as this makes it easier for the backends to use the second
  69. // alpha value of dual source blending.
  70. static DstBlendFactor RemoveSrcColorUsage(DstBlendFactor factor)
  71. {
  72. switch (factor)
  73. {
  74. case DstBlendFactor::SrcClr:
  75. return DstBlendFactor::SrcAlpha;
  76. case DstBlendFactor::InvSrcClr:
  77. return DstBlendFactor::InvSrcAlpha;
  78. default:
  79. return factor;
  80. }
  81. }
  82. // Same as RemoveSrcColorUsage, but because of the overlapping enum,
  83. // this must be written as another function.
  84. static SrcBlendFactor RemoveDstColorUsage(SrcBlendFactor factor)
  85. {
  86. switch (factor)
  87. {
  88. case SrcBlendFactor::DstClr:
  89. return SrcBlendFactor::DstAlpha;
  90. case SrcBlendFactor::InvDstClr:
  91. return SrcBlendFactor::InvDstAlpha;
  92. default:
  93. return factor;
  94. }
  95. }
  96. void BlendingState::Generate(const BPMemory& bp)
  97. {
  98. // Start with everything disabled.
  99. hex = 0;
  100. const bool target_has_alpha = bp.zcontrol.pixel_format == PixelFormat::RGBA6_Z24;
  101. const bool alpha_test_may_succeed = bp.alpha_test.TestResult() != AlphaTestResult::Fail;
  102. colorupdate = bp.blendmode.colorupdate && alpha_test_may_succeed;
  103. alphaupdate = bp.blendmode.alphaupdate && target_has_alpha && alpha_test_may_succeed;
  104. const bool dstalpha = bp.dstalpha.enable && alphaupdate;
  105. usedualsrc = true;
  106. if (bp.blendmode.blendenable)
  107. {
  108. if (bp.blendmode.subtract)
  109. {
  110. blendenable = true;
  111. subtractAlpha = subtract = true;
  112. srcfactoralpha = srcfactor = SrcBlendFactor::One;
  113. dstfactoralpha = dstfactor = DstBlendFactor::One;
  114. if (dstalpha)
  115. {
  116. subtractAlpha = false;
  117. srcfactoralpha = SrcBlendFactor::One;
  118. dstfactoralpha = DstBlendFactor::Zero;
  119. }
  120. }
  121. else
  122. {
  123. blendenable = true;
  124. srcfactor = bp.blendmode.srcfactor;
  125. dstfactor = bp.blendmode.dstfactor;
  126. if (!target_has_alpha)
  127. {
  128. // uses ONE instead of DSTALPHA
  129. srcfactor = RemoveDstAlphaUsage(srcfactor);
  130. dstfactor = RemoveDstAlphaUsage(dstfactor);
  131. }
  132. // replaces SrcClr with SrcAlpha and DstClr with DstAlpha, it is important to
  133. // use the dst function for the src factor and vice versa
  134. srcfactoralpha = RemoveDstColorUsage(srcfactor);
  135. dstfactoralpha = RemoveSrcColorUsage(dstfactor);
  136. if (dstalpha)
  137. {
  138. srcfactoralpha = SrcBlendFactor::One;
  139. dstfactoralpha = DstBlendFactor::Zero;
  140. }
  141. }
  142. }
  143. else if (bp.blendmode.logicopenable)
  144. {
  145. if (bp.blendmode.logicmode == LogicOp::NoOp)
  146. {
  147. // Fast path for Kirby's Return to Dreamland, they use it with dstAlpha.
  148. colorupdate = false;
  149. alphaupdate = alphaupdate && dstalpha;
  150. }
  151. else
  152. {
  153. logicopenable = true;
  154. logicmode = bp.blendmode.logicmode;
  155. if (dstalpha)
  156. {
  157. // TODO: Not supported by backends.
  158. }
  159. }
  160. }
  161. // If we aren't writing color or alpha, don't blend it.
  162. // Intel GPUs on D3D12 seem to have issues with dual-source blend if the second source is used in
  163. // the blend state but not actually written (i.e. the alpha src or dst factor is src alpha, but
  164. // alpha update is disabled). So, change the blending configuration to not use a dual-source
  165. // factor. Note that in theory, disabling writing should render these irrelevant.
  166. if (!colorupdate)
  167. {
  168. srcfactor = SrcBlendFactor::Zero;
  169. dstfactor = DstBlendFactor::One;
  170. }
  171. if (!alphaupdate)
  172. {
  173. srcfactoralpha = SrcBlendFactor::Zero;
  174. dstfactoralpha = DstBlendFactor::One;
  175. }
  176. }
  177. void BlendingState::ApproximateLogicOpWithBlending()
  178. {
  179. struct LogicOpApproximation
  180. {
  181. bool blendEnable;
  182. bool subtract;
  183. SrcBlendFactor srcfactor;
  184. DstBlendFactor dstfactor;
  185. };
  186. // TODO: This previously had a warning about SRC and DST being aliased and not to mix them,
  187. // but INVSRCCLR and INVDSTCLR were also aliased and were mixed.
  188. // Thus, NOR, EQUIV, INVERT, COPY_INVERTED, and OR_INVERTED duplicate(d) other values.
  189. static constexpr std::array<LogicOpApproximation, 16> approximations = {{
  190. // clang-format off
  191. {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // CLEAR (Shader outputs 0)
  192. {true, false, SrcBlendFactor::DstClr, DstBlendFactor::Zero}, // AND
  193. {true, true, SrcBlendFactor::One, DstBlendFactor::InvSrcClr}, // AND_REVERSE
  194. {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // COPY
  195. {true, true, SrcBlendFactor::DstClr, DstBlendFactor::One}, // AND_INVERTED
  196. {true, false, SrcBlendFactor::Zero, DstBlendFactor::One}, // NOOP
  197. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::InvSrcClr}, // XOR
  198. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::One}, // OR
  199. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::InvSrcClr}, // NOR
  200. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::Zero}, // EQUIV
  201. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::Zero}, // INVERT (Shader outputs 255)
  202. {true, false, SrcBlendFactor::One, DstBlendFactor::InvDstAlpha}, // OR_REVERSE
  203. {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // COPY_INVERTED (Shader inverts)
  204. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::One}, // OR_INVERTED
  205. {true, false, SrcBlendFactor::InvDstClr, DstBlendFactor::InvSrcClr}, // NAND
  206. {false, false, SrcBlendFactor::One, DstBlendFactor::Zero}, // SET (Shader outputs 255)
  207. // clang-format on
  208. }};
  209. logicopenable = false;
  210. usedualsrc = false;
  211. const LogicOpApproximation& approximation = approximations[static_cast<u32>(logicmode.Value())];
  212. if (approximation.blendEnable)
  213. {
  214. blendenable = true;
  215. subtract = approximation.subtract;
  216. srcfactor = approximation.srcfactor;
  217. srcfactoralpha = approximation.srcfactor;
  218. dstfactor = approximation.dstfactor;
  219. dstfactoralpha = approximation.dstfactor;
  220. }
  221. }
  222. bool BlendingState::LogicOpApproximationIsExact()
  223. {
  224. switch (logicmode.Value())
  225. {
  226. case LogicOp::Clear:
  227. case LogicOp::Set:
  228. case LogicOp::NoOp:
  229. case LogicOp::Invert:
  230. case LogicOp::CopyInverted:
  231. case LogicOp::Copy:
  232. return true;
  233. default:
  234. return false;
  235. }
  236. }
  237. bool BlendingState::LogicOpApproximationWantsShaderHelp()
  238. {
  239. switch (logicmode.Value())
  240. {
  241. case LogicOp::Clear:
  242. case LogicOp::Set:
  243. case LogicOp::NoOp:
  244. case LogicOp::Invert:
  245. case LogicOp::CopyInverted:
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. void SamplerState::Generate(const BPMemory& bp, u32 index)
  252. {
  253. auto tex = bp.tex.GetUnit(index);
  254. const TexMode0& bp_tm0 = tex.texMode0;
  255. const TexMode1& bp_tm1 = tex.texMode1;
  256. // GX can configure the mip filter to none. However, D3D and Vulkan can't express this in their
  257. // sampler states. Therefore, we set the min/max LOD to zero if this option is used.
  258. tm0.min_filter = bp_tm0.min_filter;
  259. tm0.mipmap_filter =
  260. bp_tm0.mipmap_filter == MipMode::Linear ? FilterMode::Linear : FilterMode::Near;
  261. tm0.mag_filter = bp_tm0.mag_filter;
  262. // If mipmaps are disabled, clamp min/max lod
  263. if (bp_tm0.mipmap_filter == MipMode::None)
  264. {
  265. tm1.max_lod = 0;
  266. tm1.min_lod = 0;
  267. tm0.lod_bias = 0;
  268. }
  269. else
  270. {
  271. // NOTE: When comparing, max is checked first, then min; if max is less than min, max wins
  272. tm1.max_lod = bp_tm1.max_lod.Value();
  273. tm1.min_lod = std::min(tm1.max_lod.Value(), bp_tm1.min_lod.Value());
  274. tm0.lod_bias = bp_tm0.lod_bias * (256 / 32);
  275. }
  276. // Wrap modes
  277. // Hardware testing indicates that wrap_mode set to 3 behaves the same as clamp.
  278. auto filter_invalid_wrap = [](WrapMode mode) {
  279. return (mode <= WrapMode::Mirror) ? mode : WrapMode::Clamp;
  280. };
  281. tm0.wrap_u = filter_invalid_wrap(bp_tm0.wrap_s);
  282. tm0.wrap_v = filter_invalid_wrap(bp_tm0.wrap_t);
  283. tm0.diag_lod = bp_tm0.diag_lod;
  284. tm0.anisotropic_filtering = false; // TODO: Respect BP anisotropic filtering mode
  285. tm0.lod_clamp = bp_tm0.lod_clamp; // TODO: What does this do?
  286. }
  287. namespace RenderState
  288. {
  289. RasterizationState GetInvalidRasterizationState()
  290. {
  291. RasterizationState state;
  292. state.hex = UINT32_C(0xFFFFFFFF);
  293. return state;
  294. }
  295. RasterizationState GetNoCullRasterizationState(PrimitiveType primitive)
  296. {
  297. RasterizationState state = {};
  298. state.cullmode = CullMode::None;
  299. state.primitive = primitive;
  300. return state;
  301. }
  302. RasterizationState GetCullBackFaceRasterizationState(PrimitiveType primitive)
  303. {
  304. RasterizationState state = {};
  305. state.cullmode = CullMode::Back;
  306. state.primitive = primitive;
  307. return state;
  308. }
  309. DepthState GetInvalidDepthState()
  310. {
  311. DepthState state;
  312. state.hex = UINT32_C(0xFFFFFFFF);
  313. return state;
  314. }
  315. DepthState GetNoDepthTestingDepthState()
  316. {
  317. DepthState state = {};
  318. state.testenable = false;
  319. state.updateenable = false;
  320. state.func = CompareMode::Always;
  321. return state;
  322. }
  323. DepthState GetAlwaysWriteDepthState()
  324. {
  325. DepthState state = {};
  326. state.testenable = true;
  327. state.updateenable = true;
  328. state.func = CompareMode::Always;
  329. return state;
  330. }
  331. BlendingState GetInvalidBlendingState()
  332. {
  333. BlendingState state;
  334. state.hex = UINT32_C(0xFFFFFFFF);
  335. return state;
  336. }
  337. BlendingState GetNoBlendingBlendState()
  338. {
  339. BlendingState state = {};
  340. state.usedualsrc = false;
  341. state.blendenable = false;
  342. state.srcfactor = SrcBlendFactor::One;
  343. state.srcfactoralpha = SrcBlendFactor::One;
  344. state.dstfactor = DstBlendFactor::Zero;
  345. state.dstfactoralpha = DstBlendFactor::Zero;
  346. state.logicopenable = false;
  347. state.colorupdate = true;
  348. state.alphaupdate = true;
  349. return state;
  350. }
  351. BlendingState GetNoColorWriteBlendState()
  352. {
  353. BlendingState state = {};
  354. state.usedualsrc = false;
  355. state.blendenable = false;
  356. state.srcfactor = SrcBlendFactor::One;
  357. state.srcfactoralpha = SrcBlendFactor::One;
  358. state.dstfactor = DstBlendFactor::Zero;
  359. state.dstfactoralpha = DstBlendFactor::Zero;
  360. state.logicopenable = false;
  361. state.colorupdate = false;
  362. state.alphaupdate = false;
  363. return state;
  364. }
  365. SamplerState GetInvalidSamplerState()
  366. {
  367. SamplerState state;
  368. state.tm0.hex = 0xFFFFFFFF;
  369. state.tm1.hex = 0xFFFFFFFF;
  370. return state;
  371. }
  372. SamplerState GetPointSamplerState()
  373. {
  374. SamplerState state = {};
  375. state.tm0.min_filter = FilterMode::Near;
  376. state.tm0.mag_filter = FilterMode::Near;
  377. state.tm0.mipmap_filter = FilterMode::Near;
  378. state.tm0.wrap_u = WrapMode::Clamp;
  379. state.tm0.wrap_v = WrapMode::Clamp;
  380. state.tm1.min_lod = 0;
  381. state.tm1.max_lod = 255;
  382. state.tm0.lod_bias = 0;
  383. state.tm0.anisotropic_filtering = false;
  384. state.tm0.diag_lod = LODType::Edge;
  385. state.tm0.lod_clamp = false;
  386. return state;
  387. }
  388. SamplerState GetLinearSamplerState()
  389. {
  390. SamplerState state = {};
  391. state.tm0.min_filter = FilterMode::Linear;
  392. state.tm0.mag_filter = FilterMode::Linear;
  393. state.tm0.mipmap_filter = FilterMode::Linear;
  394. state.tm0.wrap_u = WrapMode::Clamp;
  395. state.tm0.wrap_v = WrapMode::Clamp;
  396. state.tm1.min_lod = 0;
  397. state.tm1.max_lod = 255;
  398. state.tm0.lod_bias = 0;
  399. state.tm0.anisotropic_filtering = false;
  400. state.tm0.diag_lod = LODType::Edge;
  401. state.tm0.lod_clamp = false;
  402. return state;
  403. }
  404. FramebufferState GetColorFramebufferState(AbstractTextureFormat format)
  405. {
  406. FramebufferState state = {};
  407. state.color_texture_format = format;
  408. state.depth_texture_format = AbstractTextureFormat::Undefined;
  409. state.per_sample_shading = false;
  410. state.samples = 1;
  411. state.additional_color_attachment_count = 0;
  412. return state;
  413. }
  414. FramebufferState GetRGBA8FramebufferState()
  415. {
  416. return GetColorFramebufferState(AbstractTextureFormat::RGBA8);
  417. }
  418. } // namespace RenderState