RenderState.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2016 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "Common/BitField.h"
  5. #include "Common/CommonTypes.h"
  6. struct BPMemory;
  7. enum class AbstractTextureFormat : u32;
  8. enum class CompareMode : u32;
  9. enum class CullMode : u32;
  10. enum class DstBlendFactor : u32;
  11. enum class FilterMode : u32;
  12. enum class LODType : u32;
  13. enum class LogicOp : u32;
  14. enum class PixelFormat : u32;
  15. enum class SrcBlendFactor : u32;
  16. enum class WrapMode : u32;
  17. enum class PrimitiveType : u32
  18. {
  19. Points,
  20. Lines,
  21. Triangles,
  22. TriangleStrip,
  23. };
  24. union RasterizationState
  25. {
  26. void Generate(const BPMemory& bp, PrimitiveType primitive_type);
  27. RasterizationState() = default;
  28. RasterizationState(const RasterizationState&) = default;
  29. RasterizationState& operator=(const RasterizationState& rhs)
  30. {
  31. hex = rhs.hex;
  32. return *this;
  33. }
  34. RasterizationState(RasterizationState&&) = default;
  35. RasterizationState& operator=(RasterizationState&& rhs)
  36. {
  37. hex = rhs.hex;
  38. return *this;
  39. }
  40. bool operator==(const RasterizationState& rhs) const { return hex == rhs.hex; }
  41. bool operator<(const RasterizationState& rhs) const { return hex < rhs.hex; }
  42. BitField<0, 2, CullMode> cullmode;
  43. BitField<3, 2, PrimitiveType> primitive;
  44. u32 hex = 0;
  45. };
  46. union FramebufferState
  47. {
  48. FramebufferState() = default;
  49. FramebufferState(const FramebufferState&) = default;
  50. FramebufferState& operator=(const FramebufferState& rhs)
  51. {
  52. hex = rhs.hex;
  53. return *this;
  54. }
  55. FramebufferState(FramebufferState&&) = default;
  56. FramebufferState& operator=(FramebufferState&& rhs)
  57. {
  58. hex = rhs.hex;
  59. return *this;
  60. }
  61. bool operator==(const FramebufferState& rhs) const { return hex == rhs.hex; }
  62. BitField<0, 8, AbstractTextureFormat> color_texture_format;
  63. BitField<8, 8, AbstractTextureFormat> depth_texture_format;
  64. BitField<16, 8, u32> samples;
  65. BitField<24, 1, u32> per_sample_shading;
  66. // Note: all additional color attachments
  67. // have the same format as `color_texture_format`
  68. // TODO: in the future improve this so every attachment
  69. // can specify its own format
  70. BitField<25, 3, u32> additional_color_attachment_count;
  71. u32 hex = 0;
  72. };
  73. union DepthState
  74. {
  75. void Generate(const BPMemory& bp);
  76. DepthState() = default;
  77. DepthState(const DepthState&) = default;
  78. DepthState& operator=(const DepthState& rhs)
  79. {
  80. hex = rhs.hex;
  81. return *this;
  82. }
  83. DepthState(DepthState&&) = default;
  84. DepthState& operator=(DepthState&& rhs)
  85. {
  86. hex = rhs.hex;
  87. return *this;
  88. }
  89. bool operator==(const DepthState& rhs) const { return hex == rhs.hex; }
  90. bool operator<(const DepthState& rhs) const { return hex < rhs.hex; }
  91. BitField<0, 1, u32> testenable;
  92. BitField<1, 1, u32> updateenable;
  93. BitField<2, 3, CompareMode> func;
  94. u32 hex = 0;
  95. };
  96. union BlendingState
  97. {
  98. void Generate(const BPMemory& bp);
  99. // HACK: Replaces logical operations with blend operations.
  100. // Will not be bit-correct, and in some cases not even remotely in the same ballpark.
  101. void ApproximateLogicOpWithBlending();
  102. bool LogicOpApproximationIsExact();
  103. bool LogicOpApproximationWantsShaderHelp();
  104. BlendingState() = default;
  105. BlendingState(const BlendingState&) = default;
  106. BlendingState& operator=(const BlendingState& rhs)
  107. {
  108. hex = rhs.hex;
  109. return *this;
  110. }
  111. BlendingState(BlendingState&&) = default;
  112. BlendingState& operator=(BlendingState&& rhs)
  113. {
  114. hex = rhs.hex;
  115. return *this;
  116. }
  117. bool operator==(const BlendingState& rhs) const { return hex == rhs.hex; }
  118. bool operator<(const BlendingState& rhs) const { return hex < rhs.hex; }
  119. BitField<0, 1, u32> blendenable;
  120. BitField<1, 1, u32> logicopenable;
  121. BitField<3, 1, u32> colorupdate;
  122. BitField<4, 1, u32> alphaupdate;
  123. BitField<5, 1, u32> subtract;
  124. BitField<6, 1, u32> subtractAlpha;
  125. BitField<7, 1, u32> usedualsrc;
  126. BitField<8, 3, DstBlendFactor> dstfactor;
  127. BitField<11, 3, SrcBlendFactor> srcfactor;
  128. BitField<14, 3, DstBlendFactor> dstfactoralpha;
  129. BitField<17, 3, SrcBlendFactor> srcfactoralpha;
  130. BitField<20, 4, LogicOp> logicmode;
  131. bool RequiresDualSrc() const;
  132. u32 hex = 0;
  133. };
  134. struct SamplerState
  135. {
  136. void Generate(const BPMemory& bp, u32 index);
  137. SamplerState() = default;
  138. SamplerState(const SamplerState&) = default;
  139. SamplerState& operator=(const SamplerState& rhs)
  140. {
  141. tm0.hex = rhs.tm0.hex;
  142. tm1.hex = rhs.tm1.hex;
  143. return *this;
  144. }
  145. SamplerState(SamplerState&&) = default;
  146. SamplerState& operator=(SamplerState&& rhs)
  147. {
  148. tm0.hex = rhs.tm0.hex;
  149. tm1.hex = rhs.tm1.hex;
  150. return *this;
  151. }
  152. bool operator==(const SamplerState& rhs) const { return Hex() == rhs.Hex(); }
  153. bool operator<(const SamplerState& rhs) const { return Hex() < rhs.Hex(); }
  154. constexpr u64 Hex() const { return tm0.hex | (static_cast<u64>(tm1.hex) << 32); }
  155. // Based on BPMemory TexMode0/TexMode1, but with slightly higher precision and some
  156. // simplifications
  157. union TM0
  158. {
  159. // BP's mipmap_filter can be None, but that is represented here by setting min_lod and max_lod
  160. // to 0
  161. BitField<0, 1, FilterMode> min_filter;
  162. BitField<1, 1, FilterMode> mag_filter;
  163. BitField<2, 1, FilterMode> mipmap_filter;
  164. // Guaranteed to be valid values (i.e. not 3)
  165. BitField<3, 2, WrapMode> wrap_u;
  166. BitField<5, 2, WrapMode> wrap_v;
  167. BitField<7, 1, LODType> diag_lod;
  168. BitField<8, 16, s32> lod_bias; // multiplied by 256, higher precision than normal
  169. BitField<24, 1, bool, u32> lod_clamp; // TODO: This isn't currently implemented
  170. BitField<25, 1, bool, u32> anisotropic_filtering; // TODO: This doesn't use the BP one yet
  171. u32 hex = 0;
  172. };
  173. union TM1
  174. {
  175. // Min is guaranteed to be less than or equal to max
  176. BitField<0, 8, u32> min_lod; // multiplied by 16
  177. BitField<8, 8, u32> max_lod; // multiplied by 16
  178. u32 hex = 0;
  179. };
  180. TM0 tm0;
  181. TM1 tm1;
  182. };
  183. template <>
  184. struct std::hash<SamplerState>
  185. {
  186. std::size_t operator()(const SamplerState& state) const noexcept
  187. {
  188. return std::hash<u64>{}(state.Hex());
  189. }
  190. };
  191. namespace RenderState
  192. {
  193. RasterizationState GetInvalidRasterizationState();
  194. RasterizationState GetNoCullRasterizationState(PrimitiveType primitive);
  195. RasterizationState GetCullBackFaceRasterizationState(PrimitiveType primitive);
  196. DepthState GetInvalidDepthState();
  197. DepthState GetNoDepthTestingDepthState();
  198. DepthState GetAlwaysWriteDepthState();
  199. BlendingState GetInvalidBlendingState();
  200. BlendingState GetNoBlendingBlendState();
  201. BlendingState GetNoColorWriteBlendState();
  202. SamplerState GetInvalidSamplerState();
  203. SamplerState GetPointSamplerState();
  204. SamplerState GetLinearSamplerState();
  205. FramebufferState GetColorFramebufferState(AbstractTextureFormat format);
  206. FramebufferState GetRGBA8FramebufferState();
  207. } // namespace RenderState