Buffer.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include <AnKi/Gr/Buffer.h>
  6. #include <AnKi/Gr/GrManager.h>
  7. #include <AnKi/Gr/gl/BufferImpl.h>
  8. #include <AnKi/Gr/gl/CommandBufferImpl.h>
  9. namespace anki {
  10. Buffer* Buffer::newInstance(GrManager* manager, const BufferInitInfo& inf)
  11. {
  12. class BufferCreateCommand final : public GlCommand
  13. {
  14. public:
  15. BufferPtr m_buff;
  16. BufferCreateCommand(Buffer* buff)
  17. : m_buff(buff)
  18. {
  19. }
  20. Error operator()(GlState&)
  21. {
  22. BufferImpl& impl = static_cast<BufferImpl&>(*m_buff);
  23. impl.init();
  24. GlObject::State oldState = impl.setStateAtomically(GlObject::State::CREATED);
  25. (void)oldState;
  26. ANKI_ASSERT(oldState == GlObject::State::TO_BE_CREATED);
  27. return Error::NONE;
  28. }
  29. };
  30. BufferImpl* impl = manager->getAllocator().newInstance<BufferImpl>(manager, inf.getName());
  31. impl->getRefcount().fetchAdd(1); // Hold a reference in case the command finishes and deletes quickly
  32. impl->preInit(inf);
  33. CommandBufferPtr cmdb = manager->newCommandBuffer(CommandBufferInitInfo());
  34. static_cast<CommandBufferImpl&>(*cmdb).pushBackNewCommand<BufferCreateCommand>(impl);
  35. static_cast<CommandBufferImpl&>(*cmdb).flush();
  36. return impl;
  37. }
  38. void* Buffer::map(PtrSize offset, PtrSize range, BufferMapAccessBit access)
  39. {
  40. ANKI_GL_SELF(BufferImpl);
  41. return self.map(offset, range, access);
  42. }
  43. void Buffer::unmap()
  44. {
  45. ANKI_GL_SELF(BufferImpl);
  46. self.unmap();
  47. }
  48. } // end namespace anki