Texture.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/Texture.h>
  6. #include <AnKi/Gr/gl/TextureImpl.h>
  7. #include <AnKi/Gr/gl/CommandBufferImpl.h>
  8. #include <AnKi/Gr/GrManager.h>
  9. namespace anki
  10. {
  11. Texture* Texture::newInstance(GrManager* manager, const TextureInitInfo& init)
  12. {
  13. class CreateTextureCommand final : public GlCommand
  14. {
  15. public:
  16. TexturePtr m_tex;
  17. TextureInitInfo m_init;
  18. CreateTextureCommand(Texture* tex, const TextureInitInfo& init)
  19. : m_tex(tex)
  20. , m_init(init)
  21. {
  22. }
  23. Error operator()(GlState&)
  24. {
  25. TextureImpl& impl = static_cast<TextureImpl&>(*m_tex);
  26. impl.init(m_init);
  27. GlObject::State oldState = impl.setStateAtomically(GlObject::State::CREATED);
  28. ANKI_ASSERT(oldState == GlObject::State::TO_BE_CREATED);
  29. (void)oldState;
  30. return Error::NONE;
  31. }
  32. };
  33. TextureImpl* impl = manager->getAllocator().newInstance<TextureImpl>(manager, init.getName());
  34. impl->getRefcount().fetchAdd(1); // Hold a reference in case the command finishes and deletes quickly
  35. // Need to pre-init because some funcs ask for members and we don't want to serialize
  36. impl->preInit(init);
  37. CommandBufferPtr cmdb = manager->newCommandBuffer(CommandBufferInitInfo());
  38. static_cast<CommandBufferImpl&>(*cmdb).pushBackNewCommand<CreateTextureCommand>(impl, init);
  39. static_cast<CommandBufferImpl&>(*cmdb).flush();
  40. return impl;
  41. }
  42. } // end namespace anki