gl_rasterizer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <glad/glad.h>
  14. #include "common/common_types.h"
  15. #include "video_core/engines/const_buffer_info.h"
  16. #include "video_core/engines/maxwell_3d.h"
  17. #include "video_core/rasterizer_accelerated.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_resource_manager.h"
  24. #include "video_core/renderer_opengl/gl_sampler_cache.h"
  25. #include "video_core/renderer_opengl/gl_shader_cache.h"
  26. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  27. #include "video_core/renderer_opengl/gl_shader_manager.h"
  28. #include "video_core/renderer_opengl/gl_state.h"
  29. #include "video_core/renderer_opengl/gl_texture_cache.h"
  30. #include "video_core/renderer_opengl/utils.h"
  31. #include "video_core/textures/texture.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::RasterizerAccelerated {
  45. public:
  46. explicit RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
  47. ScreenInfo& info);
  48. ~RasterizerOpenGL() override;
  49. bool DrawBatch(bool is_indexed) override;
  50. bool DrawMultiBatch(bool is_indexed) override;
  51. void Clear() override;
  52. void DispatchCompute(GPUVAddr code_addr) override;
  53. void FlushAll() override;
  54. void FlushRegion(CacheAddr addr, u64 size) override;
  55. void InvalidateRegion(CacheAddr addr, u64 size) override;
  56. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  57. void FlushCommands() override;
  58. void TickFrame() override;
  59. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  60. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  61. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  62. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  63. u32 pixel_stride) override;
  64. void LoadDiskResources(const std::atomic_bool& stop_loading,
  65. const VideoCore::DiskResourceLoadCallback& callback) override;
  66. private:
  67. /// Configures the color and depth framebuffer states.
  68. void ConfigureFramebuffers();
  69. void ConfigureClearFramebuffer(OpenGLState& current_state, bool using_color_fb,
  70. bool using_depth_fb, bool using_stencil_fb);
  71. /// Configures the current constbuffers to use for the draw command.
  72. void SetupDrawConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  73. const Shader& shader);
  74. /// Configures the current constbuffers to use for the kernel invocation.
  75. void SetupComputeConstBuffers(const Shader& kernel);
  76. /// Configures a constant buffer.
  77. void SetupConstBuffer(const Tegra::Engines::ConstBufferInfo& buffer,
  78. const GLShader::ConstBufferEntry& entry);
  79. /// Configures the current global memory entries to use for the draw command.
  80. void SetupDrawGlobalMemory(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  81. const Shader& shader);
  82. /// Configures the current global memory entries to use for the kernel invocation.
  83. void SetupComputeGlobalMemory(const Shader& kernel);
  84. /// Configures a constant buffer.
  85. void SetupGlobalMemory(const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
  86. std::size_t size);
  87. /// Syncs all the state, shaders, render targets and textures setting before a draw call.
  88. void DrawPrelude();
  89. /// Configures the current textures to use for the draw command.
  90. void SetupDrawTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, const Shader& shader,
  91. BaseBindings base_bindings);
  92. /// Configures the textures used in a compute shader.
  93. void SetupComputeTextures(const Shader& kernel);
  94. /// Configures a texture.
  95. void SetupTexture(u32 binding, const Tegra::Texture::FullTextureInfo& texture,
  96. const GLShader::SamplerEntry& entry);
  97. /// Configures images in a compute shader.
  98. void SetupComputeImages(const Shader& shader);
  99. /// Configures an image.
  100. void SetupImage(u32 binding, const Tegra::Texture::TICEntry& tic,
  101. const GLShader::ImageEntry& entry);
  102. /// Syncs the viewport and depth range to match the guest state
  103. void SyncViewport(OpenGLState& current_state);
  104. /// Syncs the clip enabled status to match the guest state
  105. void SyncClipEnabled(
  106. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& clip_mask);
  107. /// Syncs the clip coefficients to match the guest state
  108. void SyncClipCoef();
  109. /// Syncs the cull mode to match the guest state
  110. void SyncCullMode();
  111. /// Syncs the primitve restart to match the guest state
  112. void SyncPrimitiveRestart();
  113. /// Syncs the depth test state to match the guest state
  114. void SyncDepthTestState();
  115. /// Syncs the stencil test state to match the guest state
  116. void SyncStencilTestState();
  117. /// Syncs the blend state to match the guest state
  118. void SyncBlendState();
  119. /// Syncs the LogicOp state to match the guest state
  120. void SyncLogicOpState();
  121. /// Syncs the the color clamp state
  122. void SyncFragmentColorClampState();
  123. /// Syncs the alpha coverage and alpha to one
  124. void SyncMultiSampleState();
  125. /// Syncs the scissor test state to match the guest state
  126. void SyncScissorTest(OpenGLState& current_state);
  127. /// Syncs the transform feedback state to match the guest state
  128. void SyncTransformFeedback();
  129. /// Syncs the point state to match the guest state
  130. void SyncPointState();
  131. /// Syncs Color Mask
  132. void SyncColorMask();
  133. /// Syncs the polygon offsets
  134. void SyncPolygonOffset();
  135. /// Syncs the alpha test state to match the guest state
  136. void SyncAlphaTest();
  137. /// Check for extension that are not strictly required
  138. /// but are needed for correct emulation
  139. void CheckExtensions();
  140. const Device device;
  141. OpenGLState state;
  142. TextureCacheOpenGL texture_cache;
  143. ShaderCacheOpenGL shader_cache;
  144. SamplerCacheOpenGL sampler_cache;
  145. FramebufferCacheOpenGL framebuffer_cache;
  146. Core::System& system;
  147. ScreenInfo& screen_info;
  148. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  149. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  150. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  151. OGLVertexArray>
  152. vertex_array_cache;
  153. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  154. OGLBufferCache buffer_cache;
  155. VertexArrayPushBuffer vertex_array_pushbuffer;
  156. BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
  157. BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
  158. std::size_t CalculateVertexArraysSize() const;
  159. std::size_t CalculateIndexBufferSize() const;
  160. /// Updates and returns a vertex array object representing current vertex format
  161. GLuint SetupVertexFormat();
  162. void SetupVertexBuffer(GLuint vao);
  163. void SetupVertexInstances(GLuint vao);
  164. GLintptr SetupIndexBuffer();
  165. GLintptr index_buffer_offset;
  166. void SetupShaders(GLenum primitive_mode);
  167. enum class AccelDraw { Disabled, Arrays, Indexed };
  168. AccelDraw accelerate_draw = AccelDraw::Disabled;
  169. OGLFramebuffer clear_framebuffer;
  170. };
  171. } // namespace OpenGL