gl_rasterizer.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <cstddef>
  8. #include <map>
  9. #include <memory>
  10. #include <optional>
  11. #include <tuple>
  12. #include <utility>
  13. #include <boost/icl/interval_map.hpp>
  14. #include <glad/glad.h>
  15. #include "common/common_types.h"
  16. #include "video_core/engines/maxwell_3d.h"
  17. #include "video_core/rasterizer_cache.h"
  18. #include "video_core/rasterizer_interface.h"
  19. #include "video_core/renderer_opengl/gl_buffer_cache.h"
  20. #include "video_core/renderer_opengl/gl_global_cache.h"
  21. #include "video_core/renderer_opengl/gl_primitive_assembler.h"
  22. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  23. #include "video_core/renderer_opengl/gl_resource_manager.h"
  24. #include "video_core/renderer_opengl/gl_shader_cache.h"
  25. #include "video_core/renderer_opengl/gl_shader_manager.h"
  26. #include "video_core/renderer_opengl/gl_state.h"
  27. #include "video_core/renderer_opengl/utils.h"
  28. namespace Core {
  29. class System;
  30. }
  31. namespace Core::Frontend {
  32. class EmuWindow;
  33. }
  34. namespace OpenGL {
  35. struct ScreenInfo;
  36. struct DrawParameters;
  37. struct FramebufferCacheKey;
  38. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  39. public:
  40. explicit RasterizerOpenGL(Core::System& system, ScreenInfo& info);
  41. ~RasterizerOpenGL() override;
  42. void DrawArrays() override;
  43. void Clear() override;
  44. void FlushAll() override;
  45. void FlushRegion(CacheAddr addr, u64 size) override;
  46. void InvalidateRegion(CacheAddr addr, u64 size) override;
  47. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  48. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  49. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  50. const Common::Rectangle<u32>& src_rect,
  51. const Common::Rectangle<u32>& dst_rect) override;
  52. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  53. u32 pixel_stride) override;
  54. bool AccelerateDrawBatch(bool is_indexed) override;
  55. void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) override;
  56. void LoadDiskResources(const std::atomic_bool& stop_loading,
  57. const VideoCore::DiskResourceLoadCallback& callback) override;
  58. /// Maximum supported size that a constbuffer can have in bytes.
  59. static constexpr std::size_t MaxConstbufferSize = 0x10000;
  60. static_assert(MaxConstbufferSize % sizeof(GLvec4) == 0,
  61. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  62. private:
  63. class SamplerInfo {
  64. public:
  65. OGLSampler sampler;
  66. /// Creates the sampler object, initializing its state so that it's in sync with the
  67. /// SamplerInfo struct.
  68. void Create();
  69. /// Syncs the sampler object with the config, updating any necessary state.
  70. void SyncWithConfig(const Tegra::Texture::TSCEntry& info);
  71. private:
  72. Tegra::Texture::TextureFilter mag_filter = Tegra::Texture::TextureFilter::Nearest;
  73. Tegra::Texture::TextureFilter min_filter = Tegra::Texture::TextureFilter::Nearest;
  74. Tegra::Texture::TextureMipmapFilter mipmap_filter =
  75. Tegra::Texture::TextureMipmapFilter::None;
  76. Tegra::Texture::WrapMode wrap_u = Tegra::Texture::WrapMode::ClampToEdge;
  77. Tegra::Texture::WrapMode wrap_v = Tegra::Texture::WrapMode::ClampToEdge;
  78. Tegra::Texture::WrapMode wrap_p = Tegra::Texture::WrapMode::ClampToEdge;
  79. bool use_depth_compare = false;
  80. Tegra::Texture::DepthCompareFunc depth_compare_func =
  81. Tegra::Texture::DepthCompareFunc::Always;
  82. GLvec4 border_color = {};
  83. float min_lod = 0.0f;
  84. float max_lod = 16.0f;
  85. float lod_bias = 0.0f;
  86. float max_anisotropic = 1.0f;
  87. };
  88. struct FramebufferConfigState {
  89. bool using_color_fb{};
  90. bool using_depth_fb{};
  91. bool preserve_contents{};
  92. std::optional<std::size_t> single_color_target;
  93. bool operator==(const FramebufferConfigState& rhs) const {
  94. return std::tie(using_color_fb, using_depth_fb, preserve_contents,
  95. single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb,
  96. rhs.preserve_contents,
  97. rhs.single_color_target);
  98. }
  99. bool operator!=(const FramebufferConfigState& rhs) const {
  100. return !operator==(rhs);
  101. }
  102. };
  103. /**
  104. * Configures the color and depth framebuffer states.
  105. * @param use_color_fb If true, configure color framebuffers.
  106. * @param using_depth_fb If true, configure the depth/stencil framebuffer.
  107. * @param preserve_contents If true, tries to preserve data from a previously used framebuffer.
  108. * @param single_color_target Specifies if a single color buffer target should be used.
  109. * @returns If depth (first) or stencil (second) are being stored in the bound zeta texture
  110. * (requires using_depth_fb to be true)
  111. */
  112. std::pair<bool, bool> ConfigureFramebuffers(
  113. OpenGLState& current_state, bool use_color_fb = true, bool using_depth_fb = true,
  114. bool preserve_contents = true, std::optional<std::size_t> single_color_target = {});
  115. /// Configures the current constbuffers to use for the draw command.
  116. void SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, const Shader& shader,
  117. GLuint program_handle, BaseBindings base_bindings);
  118. /// Configures the current global memory entries to use for the draw command.
  119. void SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  120. const Shader& shader, GLenum primitive_mode,
  121. BaseBindings base_bindings);
  122. /// Configures the current textures to use for the draw command.
  123. void SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, const Shader& shader,
  124. GLuint program_handle, BaseBindings base_bindings);
  125. /// Syncs the viewport and depth range to match the guest state
  126. void SyncViewport(OpenGLState& current_state);
  127. /// Syncs the clip enabled status to match the guest state
  128. void SyncClipEnabled(
  129. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& clip_mask);
  130. /// Syncs the clip coefficients to match the guest state
  131. void SyncClipCoef();
  132. /// Syncs the cull mode to match the guest state
  133. void SyncCullMode();
  134. /// Syncs the primitve restart to match the guest state
  135. void SyncPrimitiveRestart();
  136. /// Syncs the depth test state to match the guest state
  137. void SyncDepthTestState();
  138. /// Syncs the stencil test state to match the guest state
  139. void SyncStencilTestState();
  140. /// Syncs the blend state to match the guest state
  141. void SyncBlendState();
  142. /// Syncs the LogicOp state to match the guest state
  143. void SyncLogicOpState();
  144. /// Syncs the the color clamp state
  145. void SyncFragmentColorClampState();
  146. /// Syncs the alpha coverage and alpha to one
  147. void SyncMultiSampleState();
  148. /// Syncs the scissor test state to match the guest state
  149. void SyncScissorTest(OpenGLState& current_state);
  150. /// Syncs the transform feedback state to match the guest state
  151. void SyncTransformFeedback();
  152. /// Syncs the point state to match the guest state
  153. void SyncPointState();
  154. /// Syncs Color Mask
  155. void SyncColorMask();
  156. /// Syncs the polygon offsets
  157. void SyncPolygonOffset();
  158. /// Check asserts for alpha testing.
  159. void CheckAlphaTests();
  160. /// Check for extension that are not strictly required
  161. /// but are needed for correct emulation
  162. void CheckExtensions();
  163. OpenGLState state;
  164. RasterizerCacheOpenGL res_cache;
  165. ShaderCacheOpenGL shader_cache;
  166. GlobalRegionCacheOpenGL global_cache;
  167. Core::System& system;
  168. ScreenInfo& screen_info;
  169. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  170. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  171. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  172. OGLVertexArray>
  173. vertex_array_cache;
  174. std::map<FramebufferCacheKey, OGLFramebuffer> framebuffer_cache;
  175. FramebufferConfigState current_framebuffer_config_state;
  176. std::pair<bool, bool> current_depth_stencil_usage{};
  177. std::array<SamplerInfo, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_samplers;
  178. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  179. OGLBufferCache buffer_cache;
  180. PrimitiveAssembler primitive_assembler{buffer_cache};
  181. GLint uniform_buffer_alignment;
  182. BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
  183. BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
  184. std::size_t CalculateVertexArraysSize() const;
  185. std::size_t CalculateIndexBufferSize() const;
  186. /// Updates and returns a vertex array object representing current vertex format
  187. GLuint SetupVertexFormat();
  188. void SetupVertexBuffer(GLuint vao);
  189. DrawParameters SetupDraw();
  190. void SetupShaders(GLenum primitive_mode);
  191. void SetupCachedFramebuffer(const FramebufferCacheKey& fbkey, OpenGLState& current_state);
  192. enum class AccelDraw { Disabled, Arrays, Indexed };
  193. AccelDraw accelerate_draw = AccelDraw::Disabled;
  194. using CachedPageMap = boost::icl::interval_map<u64, int>;
  195. CachedPageMap cached_pages;
  196. };
  197. } // namespace OpenGL