gl_rasterizer.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <string>
  6. #include <tuple>
  7. #include <utility>
  8. #include <glad/glad.h>
  9. #include "common/alignment.h"
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "common/math_util.h"
  13. #include "common/microprofile.h"
  14. #include "common/scope_exit.h"
  15. #include "common/vector_math.h"
  16. #include "core/settings.h"
  17. #include "video_core/renderer_opengl/gl_rasterizer.h"
  18. #include "video_core/renderer_opengl/gl_shader_gen.h"
  19. #include "video_core/renderer_opengl/renderer_opengl.h"
  20. using PixelFormat = SurfaceParams::PixelFormat;
  21. using SurfaceType = SurfaceParams::SurfaceType;
  22. MICROPROFILE_DEFINE(OpenGL_VAO, "OpenGL", "Vertex Array Setup", MP_RGB(128, 128, 192));
  23. MICROPROFILE_DEFINE(OpenGL_VS, "OpenGL", "Vertex Shader Setup", MP_RGB(128, 128, 192));
  24. MICROPROFILE_DEFINE(OpenGL_FS, "OpenGL", "Fragment Shader Setup", MP_RGB(128, 128, 192));
  25. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  26. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255));
  27. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100));
  28. enum class UniformBindings : GLuint { Common, VS, FS };
  29. static void SetShaderUniformBlockBinding(GLuint shader, const char* name, UniformBindings binding,
  30. size_t expected_size) {
  31. GLuint ub_index = glGetUniformBlockIndex(shader, name);
  32. if (ub_index != GL_INVALID_INDEX) {
  33. GLint ub_size = 0;
  34. glGetActiveUniformBlockiv(shader, ub_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ub_size);
  35. ASSERT_MSG(ub_size == expected_size,
  36. "Uniform block size did not match! Got %d, expected %zu",
  37. static_cast<int>(ub_size), expected_size);
  38. glUniformBlockBinding(shader, ub_index, static_cast<GLuint>(binding));
  39. }
  40. }
  41. static void SetShaderUniformBlockBindings(GLuint shader) {
  42. SetShaderUniformBlockBinding(shader, "shader_data", UniformBindings::Common,
  43. sizeof(RasterizerOpenGL::UniformData));
  44. SetShaderUniformBlockBinding(shader, "vs_config", UniformBindings::VS,
  45. sizeof(RasterizerOpenGL::VSUniformData));
  46. SetShaderUniformBlockBinding(shader, "fs_config", UniformBindings::FS,
  47. sizeof(RasterizerOpenGL::FSUniformData));
  48. }
  49. RasterizerOpenGL::RasterizerOpenGL() {
  50. has_ARB_buffer_storage = false;
  51. has_ARB_direct_state_access = false;
  52. has_ARB_separate_shader_objects = false;
  53. has_ARB_vertex_attrib_binding = false;
  54. GLint ext_num;
  55. glGetIntegerv(GL_NUM_EXTENSIONS, &ext_num);
  56. for (GLint i = 0; i < ext_num; i++) {
  57. std::string extension{reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i))};
  58. if (extension == "GL_ARB_buffer_storage") {
  59. has_ARB_buffer_storage = true;
  60. } else if (extension == "GL_ARB_direct_state_access") {
  61. has_ARB_direct_state_access = true;
  62. } else if (extension == "GL_ARB_separate_shader_objects") {
  63. has_ARB_separate_shader_objects = true;
  64. } else if (extension == "GL_ARB_vertex_attrib_binding") {
  65. has_ARB_vertex_attrib_binding = true;
  66. }
  67. }
  68. // Clipping plane 0 is always enabled for PICA fixed clip plane z <= 0
  69. state.clip_distance[0] = true;
  70. // Generate VBO, VAO and UBO
  71. vertex_buffer = OGLStreamBuffer::MakeBuffer(GLAD_GL_ARB_buffer_storage, GL_ARRAY_BUFFER);
  72. vertex_buffer->Create(VERTEX_BUFFER_SIZE, VERTEX_BUFFER_SIZE / 2);
  73. sw_vao.Create();
  74. uniform_buffer.Create();
  75. state.draw.vertex_array = sw_vao.handle;
  76. state.draw.vertex_buffer = vertex_buffer->GetHandle();
  77. state.draw.uniform_buffer = uniform_buffer.handle;
  78. state.Apply();
  79. glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), nullptr, GL_STATIC_DRAW);
  80. glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
  81. uniform_block_data.dirty = true;
  82. // Create render framebuffer
  83. framebuffer.Create();
  84. if (has_ARB_separate_shader_objects) {
  85. hw_vao.Create();
  86. hw_vao_enabled_attributes.fill(false);
  87. stream_buffer = OGLStreamBuffer::MakeBuffer(has_ARB_buffer_storage, GL_ARRAY_BUFFER);
  88. stream_buffer->Create(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE / 2);
  89. state.draw.vertex_buffer = stream_buffer->GetHandle();
  90. pipeline.Create();
  91. vs_input_index_min = 0;
  92. vs_input_index_max = 0;
  93. state.draw.program_pipeline = pipeline.handle;
  94. state.draw.shader_program = 0;
  95. state.draw.vertex_array = hw_vao.handle;
  96. state.Apply();
  97. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, stream_buffer->GetHandle());
  98. vs_uniform_buffer.Create();
  99. glBindBuffer(GL_UNIFORM_BUFFER, vs_uniform_buffer.handle);
  100. glBufferData(GL_UNIFORM_BUFFER, sizeof(VSUniformData), nullptr, GL_STREAM_COPY);
  101. glBindBufferBase(GL_UNIFORM_BUFFER, 1, vs_uniform_buffer.handle);
  102. } else {
  103. UNIMPLEMENTED();
  104. }
  105. accelerate_draw = AccelDraw::Disabled;
  106. glEnable(GL_BLEND);
  107. // Sync fixed function OpenGL state
  108. SyncClipEnabled();
  109. SyncClipCoef();
  110. SyncCullMode();
  111. SyncBlendEnabled();
  112. SyncBlendFuncs();
  113. SyncBlendColor();
  114. }
  115. RasterizerOpenGL::~RasterizerOpenGL() {
  116. if (stream_buffer != nullptr) {
  117. state.draw.vertex_buffer = stream_buffer->GetHandle();
  118. state.Apply();
  119. stream_buffer->Release();
  120. }
  121. }
  122. static constexpr std::array<GLenum, 4> vs_attrib_types{
  123. GL_BYTE, // VertexAttributeFormat::BYTE
  124. GL_UNSIGNED_BYTE, // VertexAttributeFormat::UBYTE
  125. GL_SHORT, // VertexAttributeFormat::SHORT
  126. GL_FLOAT // VertexAttributeFormat::FLOAT
  127. };
  128. void RasterizerOpenGL::AnalyzeVertexArray(bool is_indexed) {
  129. UNIMPLEMENTED();
  130. }
  131. void RasterizerOpenGL::SetupVertexArray(u8* array_ptr, GLintptr buffer_offset) {
  132. MICROPROFILE_SCOPE(OpenGL_VAO);
  133. UNIMPLEMENTED();
  134. }
  135. void RasterizerOpenGL::SetupVertexShader(VSUniformData* ub_ptr, GLintptr buffer_offset) {
  136. MICROPROFILE_SCOPE(OpenGL_VS);
  137. UNIMPLEMENTED();
  138. }
  139. void RasterizerOpenGL::SetupFragmentShader(FSUniformData* ub_ptr, GLintptr buffer_offset) {
  140. MICROPROFILE_SCOPE(OpenGL_FS);
  141. UNIMPLEMENTED();
  142. }
  143. bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
  144. if (!has_ARB_separate_shader_objects) {
  145. UNIMPLEMENTED();
  146. return false;
  147. }
  148. accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
  149. DrawTriangles();
  150. return true;
  151. }
  152. void RasterizerOpenGL::DrawTriangles() {
  153. MICROPROFILE_SCOPE(OpenGL_Drawing);
  154. UNIMPLEMENTED();
  155. }
  156. void RasterizerOpenGL::NotifyMaxwellRegisterChanged(u32 id) {}
  157. void RasterizerOpenGL::FlushAll() {
  158. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  159. res_cache.FlushAll();
  160. }
  161. void RasterizerOpenGL::FlushRegion(PAddr addr, u32 size) {
  162. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  163. res_cache.FlushRegion(addr, size);
  164. }
  165. void RasterizerOpenGL::InvalidateRegion(PAddr addr, u32 size) {
  166. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  167. res_cache.InvalidateRegion(addr, size, nullptr);
  168. }
  169. void RasterizerOpenGL::FlushAndInvalidateRegion(PAddr addr, u32 size) {
  170. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  171. res_cache.FlushRegion(addr, size);
  172. res_cache.InvalidateRegion(addr, size, nullptr);
  173. }
  174. bool RasterizerOpenGL::AccelerateDisplayTransfer(const void* config) {
  175. MICROPROFILE_SCOPE(OpenGL_Blits);
  176. UNIMPLEMENTED();
  177. return true;
  178. }
  179. bool RasterizerOpenGL::AccelerateTextureCopy(const void* config) {
  180. UNIMPLEMENTED();
  181. return true;
  182. }
  183. bool RasterizerOpenGL::AccelerateFill(const void* config) {
  184. UNIMPLEMENTED();
  185. return true;
  186. }
  187. bool RasterizerOpenGL::AccelerateDisplay(const void* config, PAddr framebuffer_addr,
  188. u32 pixel_stride, ScreenInfo& screen_info) {
  189. UNIMPLEMENTED();
  190. return true;
  191. }
  192. void RasterizerOpenGL::SetShader() {
  193. UNIMPLEMENTED();
  194. }
  195. void RasterizerOpenGL::SyncClipEnabled() {
  196. UNIMPLEMENTED();
  197. }
  198. void RasterizerOpenGL::SyncClipCoef() {
  199. UNIMPLEMENTED();
  200. }
  201. void RasterizerOpenGL::SyncCullMode() {
  202. UNIMPLEMENTED();
  203. }
  204. void RasterizerOpenGL::SyncDepthScale() {
  205. UNIMPLEMENTED();
  206. }
  207. void RasterizerOpenGL::SyncDepthOffset() {
  208. UNIMPLEMENTED();
  209. }
  210. void RasterizerOpenGL::SyncBlendEnabled() {
  211. UNIMPLEMENTED();
  212. }
  213. void RasterizerOpenGL::SyncBlendFuncs() {
  214. UNIMPLEMENTED();
  215. }
  216. void RasterizerOpenGL::SyncBlendColor() {
  217. UNIMPLEMENTED();
  218. }