gl_rasterizer.h 6.7 KB

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