gl_rasterizer.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/const_buffer_info.h"
  17. #include "video_core/engines/maxwell_3d.h"
  18. #include "video_core/rasterizer_cache.h"
  19. #include "video_core/rasterizer_interface.h"
  20. #include "video_core/renderer_opengl/gl_buffer_cache.h"
  21. #include "video_core/renderer_opengl/gl_device.h"
  22. #include "video_core/renderer_opengl/gl_framebuffer_cache.h"
  23. #include "video_core/renderer_opengl/gl_global_cache.h"
  24. #include "video_core/renderer_opengl/gl_resource_manager.h"
  25. #include "video_core/renderer_opengl/gl_sampler_cache.h"
  26. #include "video_core/renderer_opengl/gl_shader_cache.h"
  27. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  28. #include "video_core/renderer_opengl/gl_shader_manager.h"
  29. #include "video_core/renderer_opengl/gl_state.h"
  30. #include "video_core/renderer_opengl/gl_texture_cache.h"
  31. #include "video_core/renderer_opengl/utils.h"
  32. namespace Core {
  33. class System;
  34. }
  35. namespace Core::Frontend {
  36. class EmuWindow;
  37. }
  38. namespace Tegra {
  39. class MemoryManager;
  40. }
  41. namespace OpenGL {
  42. struct ScreenInfo;
  43. struct DrawParameters;
  44. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  45. public:
  46. explicit RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
  47. ScreenInfo& info);
  48. ~RasterizerOpenGL() override;
  49. void InitMemoryMananger(Tegra::MemoryManager& memory_manager) override;
  50. void DrawArrays() override;
  51. void Clear() override;
  52. void FlushAll() override;
  53. void FlushRegion(CacheAddr addr, u64 size) override;
  54. void InvalidateRegion(CacheAddr addr, u64 size) override;
  55. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  56. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  57. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  58. const Common::Rectangle<u32>& src_rect,
  59. const Common::Rectangle<u32>& dst_rect) override;
  60. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  61. u32 pixel_stride) override;
  62. bool AccelerateDrawBatch(bool is_indexed) override;
  63. void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) override;
  64. void LoadDiskResources(const std::atomic_bool& stop_loading,
  65. const VideoCore::DiskResourceLoadCallback& callback) override;
  66. /// Maximum supported size that a constbuffer can have in bytes.
  67. static constexpr std::size_t MaxConstbufferSize = 0x10000;
  68. static_assert(MaxConstbufferSize % sizeof(GLvec4) == 0,
  69. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  70. private:
  71. struct FramebufferConfigState {
  72. bool using_color_fb{};
  73. bool using_depth_fb{};
  74. bool preserve_contents{};
  75. std::optional<std::size_t> single_color_target;
  76. bool operator==(const FramebufferConfigState& rhs) const {
  77. return std::tie(using_color_fb, using_depth_fb, preserve_contents,
  78. single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb,
  79. rhs.preserve_contents,
  80. rhs.single_color_target);
  81. }
  82. bool operator!=(const FramebufferConfigState& rhs) const {
  83. return !operator==(rhs);
  84. }
  85. };
  86. /**
  87. * Configures the color and depth framebuffer states.
  88. * @param must_reconfigure If true, tells the framebuffer to skip the cache and reconfigure
  89. * again. Used by the texture cache to solve texception conflicts
  90. * @param use_color_fb If true, configure color framebuffers.
  91. * @param using_depth_fb If true, configure the depth/stencil framebuffer.
  92. * @param preserve_contents If true, tries to preserve data from a previously used framebuffer.
  93. * @param single_color_target Specifies if a single color buffer target should be used.
  94. * @returns If depth (first) or stencil (second) are being stored in the bound zeta texture
  95. * (requires using_depth_fb to be true)
  96. */
  97. std::pair<bool, bool> ConfigureFramebuffers(
  98. OpenGLState& current_state, bool use_color_fb = true, bool using_depth_fb = true,
  99. bool preserve_contents = true, std::optional<std::size_t> single_color_target = {});
  100. /// Configures the current constbuffers to use for the draw command.
  101. void SetupDrawConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  102. const Shader& shader);
  103. /// Configures a constant buffer.
  104. void SetupConstBuffer(const Tegra::Engines::ConstBufferInfo& buffer,
  105. const GLShader::ConstBufferEntry& entry);
  106. /// Configures the current global memory entries to use for the draw command.
  107. void SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  108. const Shader& shader);
  109. /// Configures the current textures to use for the draw command. Returns shaders texture buffer
  110. /// usage.
  111. TextureBufferUsage SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  112. const Shader& shader, BaseBindings base_bindings);
  113. /// Syncs the viewport and depth range to match the guest state
  114. void SyncViewport(OpenGLState& current_state);
  115. /// Syncs the clip enabled status to match the guest state
  116. void SyncClipEnabled(
  117. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& clip_mask);
  118. /// Syncs the clip coefficients to match the guest state
  119. void SyncClipCoef();
  120. /// Syncs the cull mode to match the guest state
  121. void SyncCullMode();
  122. /// Syncs the primitve restart to match the guest state
  123. void SyncPrimitiveRestart();
  124. /// Syncs the depth test state to match the guest state
  125. void SyncDepthTestState();
  126. /// Syncs the stencil test state to match the guest state
  127. void SyncStencilTestState();
  128. /// Syncs the blend state to match the guest state
  129. void SyncBlendState();
  130. /// Syncs the LogicOp state to match the guest state
  131. void SyncLogicOpState();
  132. /// Syncs the the color clamp state
  133. void SyncFragmentColorClampState();
  134. /// Syncs the alpha coverage and alpha to one
  135. void SyncMultiSampleState();
  136. /// Syncs the scissor test state to match the guest state
  137. void SyncScissorTest(OpenGLState& current_state);
  138. /// Syncs the transform feedback state to match the guest state
  139. void SyncTransformFeedback();
  140. /// Syncs the point state to match the guest state
  141. void SyncPointState();
  142. /// Syncs Color Mask
  143. void SyncColorMask();
  144. /// Syncs the polygon offsets
  145. void SyncPolygonOffset();
  146. /// Syncs the alpha test state to match the guest state
  147. void SyncAlphaTest();
  148. /// Check for extension that are not strictly required
  149. /// but are needed for correct emulation
  150. void CheckExtensions();
  151. const Device device;
  152. OpenGLState state;
  153. TextureCacheOpenGL texture_cache;
  154. ShaderCacheOpenGL shader_cache;
  155. GlobalRegionCacheOpenGL global_cache;
  156. SamplerCacheOpenGL sampler_cache;
  157. FramebufferCacheOpenGL framebuffer_cache;
  158. Core::System& system;
  159. ScreenInfo& screen_info;
  160. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  161. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  162. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  163. OGLVertexArray>
  164. vertex_array_cache;
  165. FramebufferConfigState current_framebuffer_config_state;
  166. std::pair<bool, bool> current_depth_stencil_usage{};
  167. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  168. OGLBufferCache buffer_cache;
  169. BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
  170. BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
  171. std::size_t CalculateVertexArraysSize() const;
  172. std::size_t CalculateIndexBufferSize() const;
  173. /// Updates and returns a vertex array object representing current vertex format
  174. GLuint SetupVertexFormat();
  175. void SetupVertexBuffer(GLuint vao);
  176. DrawParameters SetupDraw();
  177. void SetupShaders(GLenum primitive_mode);
  178. enum class AccelDraw { Disabled, Arrays, Indexed };
  179. AccelDraw accelerate_draw = AccelDraw::Disabled;
  180. using CachedPageMap = boost::icl::interval_map<u64, int>;
  181. CachedPageMap cached_pages;
  182. };
  183. } // namespace OpenGL