GrManagerImplSdl.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// SDL implementation of WindowingBackend
  15. class WindowingBackend
  16. {
  17. public:
  18. SDL_GLContext m_context = nullptr;
  19. SDL_Window* m_window = nullptr;
  20. ~WindowingBackend()
  21. {
  22. if(m_context)
  23. {
  24. SDL_GL_DeleteContext(m_context);
  25. }
  26. }
  27. Error createContext(GrManagerInitInfo& init)
  28. {
  29. m_window = init.m_window->getNative().m_window;
  30. ANKI_GL_LOGI("Creating GL %u.%u context...", U(init.m_config->getNumber("gr.glmajor")),
  31. U(init.m_config->getNumber("gr.glminor")));
  32. if(init.m_config->getNumber("gr_debugContext"))
  33. {
  34. if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG))
  35. {
  36. ANKI_GL_LOGE("SDL_GL_SetAttribute() failed");
  37. return Error::FUNCTION_FAILED;
  38. }
  39. }
  40. if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, init.m_config->getNumber("gr.glmajor"))
  41. || SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, init.m_config->getNumber("gr.glminor"))
  42. || SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE))
  43. {
  44. ANKI_GL_LOGE("SDL_GL_SetAttribute() failed");
  45. return Error::FUNCTION_FAILED;
  46. }
  47. // Create context
  48. m_context = SDL_GL_CreateContext(m_window);
  49. if(m_context == nullptr)
  50. {
  51. ANKI_GL_LOGE("SDL_GL_CreateContext() failed: %s", SDL_GetError());
  52. return Error::FUNCTION_FAILED;
  53. }
  54. // GLEW
  55. glewExperimental = GL_TRUE;
  56. if(glewInit() != GLEW_OK)
  57. {
  58. ANKI_GL_LOGE("GLEW initialization failed");
  59. return Error::FUNCTION_FAILED;
  60. }
  61. glGetError();
  62. return Error::NONE;
  63. }
  64. void pinContextToCurrentThread(Bool pin)
  65. {
  66. SDL_GLContext pinCtx = (pin) ? m_context : nullptr;
  67. SDL_GL_MakeCurrent(m_window, pinCtx);
  68. }
  69. void swapBuffers()
  70. {
  71. SDL_GL_SwapWindow(m_window);
  72. }
  73. };
  74. Error GrManagerImpl::createBackend(GrManagerInitInfo& init)
  75. {
  76. ANKI_ASSERT(m_backend == nullptr);
  77. m_backend = getAllocator().newInstance<WindowingBackend>();
  78. return m_backend->createContext(init);
  79. }
  80. void GrManagerImpl::destroyBackend()
  81. {
  82. if(m_backend)
  83. {
  84. getAllocator().deleteInstance(m_backend);
  85. }
  86. }
  87. void GrManagerImpl::swapBuffers()
  88. {
  89. ANKI_ASSERT(m_backend);
  90. m_backend->swapBuffers();
  91. }
  92. void GrManagerImpl::pinContextToCurrentThread(Bool pin)
  93. {
  94. ANKI_ASSERT(m_backend);
  95. m_backend->pinContextToCurrentThread(pin);
  96. }
  97. } // end namespace anki