GrManagerImplSdl.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. /// @file Contains the SDL specific bits of the GL implamentation
  6. #include <AnKi/Gr/gl/GrManagerImpl.h>
  7. #include <AnKi/Gr/GrManager.h>
  8. #include <AnKi/Core/NativeWindow.h>
  9. #include <AnKi/Core/NativeWindowSdl.h>
  10. #include <AnKi/Core/Config.h>
  11. #include <SDL.h>
  12. #include <GL/glew.h>
  13. namespace anki
  14. {
  15. /// SDL implementation of WindowingBackend
  16. class WindowingBackend
  17. {
  18. public:
  19. SDL_GLContext m_context = nullptr;
  20. SDL_Window* m_window = nullptr;
  21. ~WindowingBackend()
  22. {
  23. if(m_context)
  24. {
  25. SDL_GL_DeleteContext(m_context);
  26. }
  27. }
  28. Error createContext(GrManagerInitInfo& init)
  29. {
  30. m_window = init.m_window->getNative().m_window;
  31. ANKI_GL_LOGI("Creating GL %u.%u context...", U(init.m_config->getNumber("gr.glmajor")),
  32. U(init.m_config->getNumber("gr.glminor")));
  33. if(init.m_config->getNumber("gr_debugContext"))
  34. {
  35. if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG))
  36. {
  37. ANKI_GL_LOGE("SDL_GL_SetAttribute() failed");
  38. return Error::FUNCTION_FAILED;
  39. }
  40. }
  41. if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, init.m_config->getNumber("gr.glmajor"))
  42. || SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, init.m_config->getNumber("gr.glminor"))
  43. || SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE))
  44. {
  45. ANKI_GL_LOGE("SDL_GL_SetAttribute() failed");
  46. return Error::FUNCTION_FAILED;
  47. }
  48. // Create context
  49. m_context = SDL_GL_CreateContext(m_window);
  50. if(m_context == nullptr)
  51. {
  52. ANKI_GL_LOGE("SDL_GL_CreateContext() failed: %s", SDL_GetError());
  53. return Error::FUNCTION_FAILED;
  54. }
  55. // GLEW
  56. glewExperimental = GL_TRUE;
  57. if(glewInit() != GLEW_OK)
  58. {
  59. ANKI_GL_LOGE("GLEW initialization failed");
  60. return Error::FUNCTION_FAILED;
  61. }
  62. glGetError();
  63. return Error::NONE;
  64. }
  65. void pinContextToCurrentThread(Bool pin)
  66. {
  67. SDL_GLContext pinCtx = (pin) ? m_context : nullptr;
  68. SDL_GL_MakeCurrent(m_window, pinCtx);
  69. }
  70. void swapBuffers()
  71. {
  72. SDL_GL_SwapWindow(m_window);
  73. }
  74. };
  75. Error GrManagerImpl::createBackend(GrManagerInitInfo& init)
  76. {
  77. ANKI_ASSERT(m_backend == nullptr);
  78. m_backend = getAllocator().newInstance<WindowingBackend>();
  79. return m_backend->createContext(init);
  80. }
  81. void GrManagerImpl::destroyBackend()
  82. {
  83. if(m_backend)
  84. {
  85. getAllocator().deleteInstance(m_backend);
  86. }
  87. }
  88. void GrManagerImpl::swapBuffers()
  89. {
  90. ANKI_ASSERT(m_backend);
  91. m_backend->swapBuffers();
  92. }
  93. void GrManagerImpl::pinContextToCurrentThread(Bool pin)
  94. {
  95. ANKI_ASSERT(m_backend);
  96. m_backend->pinContextToCurrentThread(pin);
  97. }
  98. } // end namespace anki