gl_rasterizer.h 9.9 KB

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