| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Gr/Shader.h>
- #include <AnKi/Gr/gl/ShaderImpl.h>
- #include <AnKi/Gr/gl/CommandBufferImpl.h>
- #include <AnKi/Gr/GrManager.h>
- namespace anki {
- Shader* Shader::newInstance(GrManager* manager, const ShaderInitInfo& init)
- {
- class ShaderCreateCommand final : public GlCommand
- {
- public:
- ShaderPtr m_shader;
- StringRaii m_source;
- DynamicArrayRaii<ShaderSpecializationConstValue> m_constValues;
- ShaderCreateCommand(Shader* shader, ConstWeakArray<U8> bin, ConstWeakArray<ShaderSpecializationConstValue> constValues,
- const CommandBufferAllocator<U8>& alloc)
- : m_shader(shader)
- , m_source(alloc)
- , m_constValues(alloc)
- {
- m_source.create(reinterpret_cast<const char*>(&bin[0]));
- if(constValues.getSize())
- {
- m_constValues.create(constValues.getSize());
- memcpy(&m_constValues[0], &constValues[0], m_constValues.getByteSize());
- }
- }
- Error operator()(GlState&)
- {
- ShaderImpl& impl = static_cast<ShaderImpl&>(*m_shader);
- Error err = impl.init(m_source.toCString(), m_constValues);
- GlObject::State oldState = impl.setStateAtomically((err) ? GlObject::State::ERROR : GlObject::State::CREATED);
- ANKI_ASSERT(oldState == GlObject::State::TO_BE_CREATED);
- (void)oldState;
- return err;
- }
- };
- ANKI_ASSERT(!init.m_binary.isEmpty());
- ShaderImpl* impl = manager->getAllocator().newInstance<ShaderImpl>(manager, init.getName());
- impl->getRefcount().fetchAdd(1); // Hold a reference in case the command finishes and deletes quickly
- // Need to pre-init because some funcs ask for members and we don't want to serialize
- impl->preInit(init);
- // Copy source to the command buffer
- CommandBufferPtr cmdb = manager->newCommandBuffer(CommandBufferInitInfo());
- CommandBufferImpl& cmdbimpl = static_cast<CommandBufferImpl&>(*cmdb);
- CommandBufferAllocator<U8> alloc = cmdbimpl.getInternalAllocator();
- cmdbimpl.pushBackNewCommand<ShaderCreateCommand>(impl, init.m_binary, init.m_constValues, alloc);
- cmdbimpl.flush();
- return impl;
- }
- } // end namespace anki
|