Shader.cpp 2.0 KB

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