GrManager.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "anki/gr/GlDevice.h"
  6. #include "anki/core/Timestamp.h"
  7. #include <cstring>
  8. namespace anki {
  9. //==============================================================================
  10. Error GlDevice::create(
  11. AllocAlignedCallback alloc, void* allocUserData,
  12. const CString& cacheDir)
  13. {
  14. m_alloc = HeapAllocator<U8>(alloc, allocUserData);
  15. // Allocate cache dir
  16. auto len = cacheDir.getLength();
  17. m_cacheDir = reinterpret_cast<char*>(m_alloc.allocate(len + 1));
  18. std::memcpy(m_cacheDir, &cacheDir[0], len + 1);
  19. // Create queue
  20. m_queue = m_alloc.newInstance<RenderingThread>(
  21. this, alloc, allocUserData);
  22. #if ANKI_QUEUE_DISABLE_ASYNC
  23. ANKI_LOGW("GL queue works in synchronous mode");
  24. #endif
  25. return ErrorCode::NONE;
  26. }
  27. //==============================================================================
  28. void GlDevice::destroy()
  29. {
  30. if(m_queue)
  31. {
  32. if(m_queueStarted)
  33. {
  34. m_queue->stop();
  35. }
  36. m_alloc.deleteInstance(m_queue);
  37. }
  38. if(m_cacheDir)
  39. {
  40. m_alloc.deallocate(m_cacheDir, std::strlen(m_cacheDir) + 1);
  41. }
  42. }
  43. //==============================================================================
  44. Error GlDevice::startServer(
  45. GlMakeCurrentCallback makeCurrentCb, void* makeCurrentCbData, void* ctx,
  46. GlCallback swapBuffersCallback, void* swapBuffersCbData,
  47. Bool registerDebugMessages)
  48. {
  49. Error err = m_queue->start(makeCurrentCb, makeCurrentCbData, ctx,
  50. swapBuffersCallback, swapBuffersCbData,
  51. registerDebugMessages);
  52. if(!err)
  53. {
  54. syncClientServer();
  55. m_queueStarted = true;
  56. }
  57. return err;
  58. }
  59. //==============================================================================
  60. void GlDevice::syncClientServer()
  61. {
  62. m_queue->syncClientServer();
  63. }
  64. //==============================================================================
  65. void GlDevice::swapBuffers()
  66. {
  67. m_queue->swapBuffers();
  68. }
  69. //==============================================================================
  70. PtrSize GlDevice::getBufferOffsetAlignment(GLenum target) const
  71. {
  72. const State& state = m_queue->getState();
  73. if(target == GL_UNIFORM_BUFFER)
  74. {
  75. return state.m_uniBuffOffsetAlignment;
  76. }
  77. else
  78. {
  79. ANKI_ASSERT(target == GL_SHADER_STORAGE_BUFFER);
  80. return state.m_ssBuffOffsetAlignment;
  81. }
  82. }
  83. } // end namespace anki