gl_rasterizer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <cstddef>
  7. #include <memory>
  8. #include <tuple>
  9. #include <utility>
  10. #include <vector>
  11. #include <boost/icl/interval_map.hpp>
  12. #include <boost/range/iterator_range.hpp>
  13. #include <glad/glad.h>
  14. #include "common/common_types.h"
  15. #include "video_core/engines/maxwell_3d.h"
  16. #include "video_core/memory_manager.h"
  17. #include "video_core/rasterizer_interface.h"
  18. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  19. #include "video_core/renderer_opengl/gl_resource_manager.h"
  20. #include "video_core/renderer_opengl/gl_shader_cache.h"
  21. #include "video_core/renderer_opengl/gl_shader_gen.h"
  22. #include "video_core/renderer_opengl/gl_shader_manager.h"
  23. #include "video_core/renderer_opengl/gl_state.h"
  24. #include "video_core/renderer_opengl/gl_stream_buffer.h"
  25. namespace Core::Frontend {
  26. class EmuWindow;
  27. }
  28. namespace OpenGL {
  29. struct ScreenInfo;
  30. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  31. public:
  32. explicit RasterizerOpenGL(Core::Frontend::EmuWindow& renderer, ScreenInfo& info);
  33. ~RasterizerOpenGL() override;
  34. void DrawArrays() override;
  35. void Clear() override;
  36. void NotifyMaxwellRegisterChanged(u32 method) override;
  37. void FlushAll() override;
  38. void FlushRegion(VAddr addr, u64 size) override;
  39. void InvalidateRegion(VAddr addr, u64 size) override;
  40. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  41. bool AccelerateDisplayTransfer(const void* config) override;
  42. bool AccelerateTextureCopy(const void* config) override;
  43. bool AccelerateFill(const void* config) override;
  44. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  45. u32 pixel_stride) override;
  46. bool AccelerateDrawBatch(bool is_indexed) override;
  47. void UpdatePagesCachedCount(Tegra::GPUVAddr addr, u64 size, int delta) override;
  48. /// OpenGL shader generated for a given Maxwell register state
  49. struct MaxwellShader {
  50. /// OpenGL shader resource
  51. OGLProgram shader;
  52. };
  53. struct VertexShader {
  54. OGLShader shader;
  55. };
  56. struct FragmentShader {
  57. OGLShader shader;
  58. };
  59. /// Maximum supported size that a constbuffer can have in bytes.
  60. static constexpr size_t MaxConstbufferSize = 0x10000;
  61. static_assert(MaxConstbufferSize % sizeof(GLvec4) == 0,
  62. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  63. private:
  64. class SamplerInfo {
  65. public:
  66. OGLSampler sampler;
  67. /// Creates the sampler object, initializing its state so that it's in sync with the
  68. /// SamplerInfo struct.
  69. void Create();
  70. /// Syncs the sampler object with the config, updating any necessary state.
  71. void SyncWithConfig(const Tegra::Texture::TSCEntry& config);
  72. private:
  73. Tegra::Texture::TextureFilter mag_filter;
  74. Tegra::Texture::TextureFilter min_filter;
  75. Tegra::Texture::WrapMode wrap_u;
  76. Tegra::Texture::WrapMode wrap_v;
  77. GLvec4 border_color;
  78. };
  79. /// Configures the color and depth framebuffer states and returns the dirty <Color, Depth>
  80. /// surfaces if writing was enabled.
  81. std::pair<Surface, Surface> ConfigureFramebuffers(bool using_color_fb, bool using_depth_fb,
  82. bool preserve_contents);
  83. /// Binds the framebuffer color and depth surface
  84. void BindFramebufferSurfaces(const Surface& color_surface, const Surface& depth_surface,
  85. bool has_stencil);
  86. /*
  87. * Configures the current constbuffers to use for the draw command.
  88. * @param stage The shader stage to configure buffers for.
  89. * @param shader The shader object that contains the specified stage.
  90. * @param current_bindpoint The offset at which to start counting new buffer bindpoints.
  91. * @returns The next available bindpoint for use in the next shader stage.
  92. */
  93. std::tuple<u8*, GLintptr, u32> SetupConstBuffers(
  94. u8* buffer_ptr, GLintptr buffer_offset, Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  95. Shader& shader, u32 current_bindpoint);
  96. /*
  97. * Configures the current textures to use for the draw command.
  98. * @param stage The shader stage to configure textures for.
  99. * @param shader The shader object that contains the specified stage.
  100. * @param current_unit The offset at which to start counting unused texture units.
  101. * @returns The next available bindpoint for use in the next shader stage.
  102. */
  103. u32 SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
  104. u32 current_unit);
  105. /// Syncs the viewport to match the guest state
  106. void SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect);
  107. /// Syncs the clip enabled status to match the guest state
  108. void SyncClipEnabled();
  109. /// Syncs the clip coefficients to match the guest state
  110. void SyncClipCoef();
  111. /// Syncs the cull mode to match the guest state
  112. void SyncCullMode();
  113. /// Syncs the depth scale to match the guest state
  114. void SyncDepthScale();
  115. /// Syncs the depth offset to match the guest state
  116. void SyncDepthOffset();
  117. /// Syncs the depth test state to match the guest state
  118. void SyncDepthTestState();
  119. /// Syncs the stencil test state to match the guest state
  120. void SyncStencilTestState();
  121. /// Syncs the blend state to match the guest state
  122. void SyncBlendState();
  123. /// Syncs the LogicOp state to match the guest state
  124. void SyncLogicOpState();
  125. bool has_ARB_direct_state_access = false;
  126. bool has_ARB_separate_shader_objects = false;
  127. bool has_ARB_vertex_attrib_binding = false;
  128. OpenGLState state;
  129. RasterizerCacheOpenGL res_cache;
  130. ShaderCacheOpenGL shader_cache;
  131. Core::Frontend::EmuWindow& emu_window;
  132. ScreenInfo& screen_info;
  133. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  134. OGLVertexArray sw_vao;
  135. OGLVertexArray hw_vao;
  136. std::array<SamplerInfo, GLShader::NumTextureSamplers> texture_samplers;
  137. static constexpr size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  138. OGLStreamBuffer stream_buffer;
  139. OGLBuffer uniform_buffer;
  140. OGLFramebuffer framebuffer;
  141. GLint uniform_buffer_alignment;
  142. size_t CalculateVertexArraysSize() const;
  143. std::pair<u8*, GLintptr> SetupVertexArrays(u8* array_ptr, GLintptr buffer_offset);
  144. std::pair<u8*, GLintptr> SetupShaders(u8* buffer_ptr, GLintptr buffer_offset);
  145. std::pair<u8*, GLintptr> AlignBuffer(u8* buffer_ptr, GLintptr buffer_offset, size_t alignment);
  146. std::tuple<u8*, GLintptr, GLintptr> UploadMemory(u8* buffer_ptr, GLintptr buffer_offset,
  147. Tegra::GPUVAddr gpu_addr, size_t size,
  148. size_t alignment = 4);
  149. enum class AccelDraw { Disabled, Arrays, Indexed };
  150. AccelDraw accelerate_draw = AccelDraw::Disabled;
  151. using CachedPageMap = boost::icl::interval_map<u64, int>;
  152. CachedPageMap cached_pages;
  153. };
  154. } // namespace OpenGL