Shader.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2009-2023, 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/Shader.h>
  6. #include <AnKi/Gr/gl/ShaderImpl.h>
  7. #include <AnKi/Gr/gl/CommandBufferImpl.h>
  8. #include <AnKi/Gr/GrManager.h>
  9. namespace anki {
  10. Shader* Shader::newInstance(GrManager* manager, const ShaderInitInfo& init)
  11. {
  12. class ShaderCreateCommand final : public GlCommand
  13. {
  14. public:
  15. ShaderPtr m_shader;
  16. StringRaii m_source;
  17. DynamicArrayRaii<ShaderSpecializationConstValue> m_constValues;
  18. ShaderCreateCommand(Shader* shader, ConstWeakArray<U8> bin,
  19. ConstWeakArray<ShaderSpecializationConstValue> constValues,
  20. const CommandBufferAllocator<U8>& alloc)
  21. : m_shader(shader)
  22. , m_source(alloc)
  23. , m_constValues(alloc)
  24. {
  25. m_source.create(reinterpret_cast<const char*>(&bin[0]));
  26. if(constValues.getSize())
  27. {
  28. m_constValues.create(constValues.getSize());
  29. memcpy(&m_constValues[0], &constValues[0], m_constValues.getByteSize());
  30. }
  31. }
  32. Error operator()(GlState&)
  33. {
  34. ShaderImpl& impl = static_cast<ShaderImpl&>(*m_shader);
  35. Error err = impl.init(m_source.toCString(), m_constValues);
  36. GlObject::State oldState =
  37. impl.setStateAtomically((err) ? GlObject::State::ERROR : GlObject::State::CREATED);
  38. ANKI_ASSERT(oldState == GlObject::State::TO_BE_CREATED);
  39. (void)oldState;
  40. return err;
  41. }
  42. };
  43. ANKI_ASSERT(!init.m_binary.isEmpty());
  44. ShaderImpl* impl = manager->getAllocator().newInstance<ShaderImpl>(manager, init.getName());
  45. impl->getRefcount().fetchAdd(1); // Hold a reference in case the command finishes and deletes quickly
  46. // Need to pre-init because some funcs ask for members and we don't want to serialize
  47. impl->preInit(init);
  48. // Copy source to the command buffer
  49. CommandBufferPtr cmdb = manager->newCommandBuffer(CommandBufferInitInfo());
  50. CommandBufferImpl& cmdbimpl = static_cast<CommandBufferImpl&>(*cmdb);
  51. CommandBufferAllocator<U8> alloc = cmdbimpl.getInternalAllocator();
  52. cmdbimpl.pushBackNewCommand<ShaderCreateCommand>(impl, init.m_binary, init.m_constValues, alloc);
  53. cmdbimpl.flush();
  54. return impl;
  55. }
  56. } // end namespace anki