gl_rasterizer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <cstring>
  8. #include <memory>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include <glad/glad.h>
  12. #include "common/bit_field.h"
  13. #include "common/common_types.h"
  14. #include "common/hash.h"
  15. #include "common/vector_math.h"
  16. #include "video_core/engines/maxwell_3d.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_gen.h"
  21. #include "video_core/renderer_opengl/gl_shader_manager.h"
  22. #include "video_core/renderer_opengl/gl_state.h"
  23. #include "video_core/renderer_opengl/gl_stream_buffer.h"
  24. struct ScreenInfo;
  25. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  26. public:
  27. RasterizerOpenGL();
  28. ~RasterizerOpenGL() override;
  29. void DrawArrays() override;
  30. void NotifyMaxwellRegisterChanged(u32 id) override;
  31. void FlushAll() override;
  32. void FlushRegion(VAddr addr, u64 size) override;
  33. void InvalidateRegion(VAddr addr, u64 size) override;
  34. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  35. bool AccelerateDisplayTransfer(const void* config) override;
  36. bool AccelerateTextureCopy(const void* config) override;
  37. bool AccelerateFill(const void* config) override;
  38. bool AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer, VAddr framebuffer_addr,
  39. u32 pixel_stride, ScreenInfo& screen_info) override;
  40. bool AccelerateDrawBatch(bool is_indexed) override;
  41. /// OpenGL shader generated for a given Maxwell register state
  42. struct MaxwellShader {
  43. /// OpenGL shader resource
  44. OGLProgram shader;
  45. };
  46. struct VertexShader {
  47. OGLShader shader;
  48. };
  49. struct FragmentShader {
  50. OGLShader shader;
  51. };
  52. private:
  53. class SamplerInfo {
  54. public:
  55. OGLSampler sampler;
  56. /// Creates the sampler object, initializing its state so that it's in sync with the
  57. /// SamplerInfo struct.
  58. void Create();
  59. /// Syncs the sampler object with the config, updating any necessary state.
  60. void SyncWithConfig(const Tegra::Texture::TSCEntry& config);
  61. private:
  62. Tegra::Texture::TextureFilter mag_filter;
  63. Tegra::Texture::TextureFilter min_filter;
  64. Tegra::Texture::WrapMode wrap_u;
  65. Tegra::Texture::WrapMode wrap_v;
  66. u32 border_color_r;
  67. u32 border_color_g;
  68. u32 border_color_b;
  69. u32 border_color_a;
  70. };
  71. /// Binds the framebuffer color and depth surface
  72. void BindFramebufferSurfaces(const Surface& color_surface, const Surface& depth_surface,
  73. bool has_stencil);
  74. /// Binds the required textures to OpenGL before drawing a batch.
  75. void BindTextures();
  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 base_name The name prefix of the buffer objects in the GLSL shaders.
  81. * @param current_bindpoint The offset at which to start counting new buffer bindpoints.
  82. * @param entries Vector describing the buffers that are actually used in the guest shader.
  83. * @returns The next available bindpoint for use in the next shader stage.
  84. */
  85. u32 SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, GLuint program,
  86. const std::string& base_name, u32 current_bindpoint,
  87. const std::vector<GLShader::ConstBufferEntry>& entries);
  88. /// Syncs the viewport to match the guest state
  89. void SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale);
  90. /// Syncs the clip enabled status to match the guest state
  91. void SyncClipEnabled();
  92. /// Syncs the clip coefficients to match the guest state
  93. void SyncClipCoef();
  94. /// Syncs the cull mode to match the guest state
  95. void SyncCullMode();
  96. /// Syncs the depth scale to match the guest state
  97. void SyncDepthScale();
  98. /// Syncs the depth offset to match the guest state
  99. void SyncDepthOffset();
  100. /// Syncs the blend enabled status to match the guest state
  101. void SyncBlendEnabled();
  102. /// Syncs the blend functions to match the guest state
  103. void SyncBlendFuncs();
  104. /// Syncs the blend color to match the guest state
  105. void SyncBlendColor();
  106. bool has_ARB_buffer_storage;
  107. bool has_ARB_direct_state_access;
  108. bool has_ARB_separate_shader_objects;
  109. bool has_ARB_vertex_attrib_binding;
  110. OpenGLState state;
  111. RasterizerCacheOpenGL res_cache;
  112. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  113. OGLVertexArray sw_vao;
  114. OGLVertexArray hw_vao;
  115. std::array<bool, 16> hw_vao_enabled_attributes;
  116. std::array<SamplerInfo, GLShader::NumTextureSamplers> texture_samplers;
  117. std::array<std::array<OGLBuffer, Tegra::Engines::Maxwell3D::Regs::MaxConstBuffers>,
  118. Tegra::Engines::Maxwell3D::Regs::MaxShaderStage>
  119. ssbos;
  120. static constexpr size_t VERTEX_BUFFER_SIZE = 128 * 1024 * 1024;
  121. std::unique_ptr<OGLStreamBuffer> vertex_buffer;
  122. OGLBuffer uniform_buffer;
  123. OGLFramebuffer framebuffer;
  124. static constexpr size_t STREAM_BUFFER_SIZE = 4 * 1024 * 1024;
  125. std::unique_ptr<OGLStreamBuffer> stream_buffer;
  126. GLsizeiptr vs_input_size;
  127. void AnalyzeVertexArray(bool is_indexed);
  128. void SetupVertexArray(u8* array_ptr, GLintptr buffer_offset);
  129. std::array<OGLBuffer, Tegra::Engines::Maxwell3D::Regs::MaxShaderStage> uniform_buffers;
  130. void SetupShaders(u8* buffer_ptr, GLintptr buffer_offset, size_t ptr_pos);
  131. enum class AccelDraw { Disabled, Arrays, Indexed };
  132. AccelDraw accelerate_draw;
  133. };