gl_rasterizer.h 6.5 KB

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