Shader.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/Shader.h"
  6. #include "anki/gr/gl/ShaderImpl.h"
  7. #include "anki/gr/gl/CommandBufferImpl.h"
  8. namespace anki {
  9. //==============================================================================
  10. Shader::Shader(GrManager* manager)
  11. : GrObject(manager)
  12. {}
  13. //==============================================================================
  14. Shader::~Shader()
  15. {}
  16. //==============================================================================
  17. class ShaderCreateCommand final: public GlCommand
  18. {
  19. public:
  20. ShaderPtr m_shader;
  21. ShaderType m_type;
  22. char* m_source;
  23. CommandBufferAllocator<char> m_alloc;
  24. ShaderCreateCommand(Shader* shader, ShaderType type, char* source,
  25. const CommandBufferAllocator<char>& alloc)
  26. : m_shader(shader)
  27. , m_type(type)
  28. , m_source(source)
  29. , m_alloc(alloc)
  30. {}
  31. Error operator()(GlState&)
  32. {
  33. ShaderImpl& impl = m_shader->getImplementation();
  34. Error err = impl.create(m_type, m_source);
  35. GlObject::State oldState = impl.setStateAtomically(
  36. (err) ? GlObject::State::ERROR : GlObject::State::CREATED);
  37. ANKI_ASSERT(oldState == GlObject::State::TO_BE_CREATED);
  38. (void)oldState;
  39. // Delete source
  40. m_alloc.deallocate(m_source, 1);
  41. return err;
  42. }
  43. };
  44. void Shader::create(ShaderType shaderType, const void* source,
  45. PtrSize sourceSize)
  46. {
  47. ANKI_ASSERT(source);
  48. ANKI_ASSERT(sourceSize
  49. == CString(static_cast<const char*>(source)).getLength() + 1);
  50. m_impl.reset(getAllocator().newInstance<ShaderImpl>(&getManager()));
  51. CommandBufferPtr cmdb = getManager().newInstance<CommandBuffer>();
  52. // Copy source to the command buffer
  53. CommandBufferAllocator<char> alloc =
  54. cmdb->getImplementation().getInternalAllocator();
  55. char* src = alloc.allocate(sourceSize);
  56. memcpy(src, source, sourceSize);
  57. cmdb->getImplementation().pushBackNewCommand<ShaderCreateCommand>(
  58. this, shaderType, src, alloc);
  59. cmdb->flush();
  60. }
  61. } // end namespace anki