gl_rasterizer.h 6.8 KB

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