36 using gfx::ResourceManager;
39 using gfx::ShaderProgram;
46 class ShaderManager::ShaderManagerData :
public base::Allocatable {
54 : program(program_in) {}
59 typedef base::AllocMap<std::string, ProgramInfo> ProgramMap;
61 explicit ShaderManagerData(
const base::Allocatable& owner)
63 ~ShaderManagerData()
override {}
66 void AddProgramInfo(
const std::string&
name,
const ProgramInfo& info) {
68 programs_[
name] = info;
75 ProgramMap::iterator it = FindProgramInfo(name);
76 return GetProgramFromInfo(&it);
80 std::vector<std::string> names;
81 names.reserve(programs_.size());
82 for (ProgramMap::iterator it = programs_.begin(); it != programs_.end();) {
83 if (GetProgramFromInfo(&it).Get()) {
84 names.push_back(it->first);
96 ProgramMap::iterator it = FindProgramInfo(name);
97 if (it != programs_.end()) {
98 if (vertex_source_composer)
99 *vertex_source_composer = it->second.vertex_source_composer;
100 if (fragment_source_composer)
101 *fragment_source_composer = it->second.fragment_source_composer;
103 if (vertex_source_composer)
104 *vertex_source_composer = NULL;
105 if (fragment_source_composer)
106 *fragment_source_composer = NULL;
112 for (ProgramMap::iterator it = programs_.begin(); it != programs_.end();) {
115 const ProgramInfo& info = it->second;
118 if (Shader* shader = program->GetVertexShader().Get())
119 shader->SetSource(info.vertex_source_composer->GetSource());
120 if (Shader* shader = program->GetFragmentShader().Get())
121 shader->SetSource(info.fragment_source_composer->GetSource());
129 for (ProgramMap::iterator it = programs_.begin(); it != programs_.end();) {
132 const ProgramInfo& info = it->second;
133 if (info.vertex_source_composer->DependsOn(dependency))
134 if (Shader* shader = program->GetVertexShader().Get())
135 shader->SetSource(info.vertex_source_composer->GetSource());
136 if (info.fragment_source_composer->DependsOn(dependency))
137 if (Shader* shader = program->GetFragmentShader().Get())
138 shader->SetSource(info.fragment_source_composer->GetSource());
148 ProgramMap::iterator FindProgramInfo(
const std::string& name) {
149 ProgramMap::iterator it = programs_.find(name);
150 if (!GetProgramFromInfo(&it).Get())
151 it = programs_.end();
161 if ((*it) != programs_.end()) {
162 program = (*it)->second.program.Acquire();
163 if (!program.Get()) {
166 *it = programs_.erase(*it);
173 ProgramMap programs_;
180 : data_(new(GetAllocator()) ShaderManagerData(*this)) {}
182 ShaderManager::~ShaderManager() {}
191 ShaderManagerData::ProgramInfo info(program);
192 program->SetLabel(name);
193 program->SetVertexShader(
195 Shader(vertex_source_composer->GetSource())));
196 program->GetVertexShader()->SetLabel(name +
" vertex shader");
197 program->SetFragmentShader(
199 Shader(fragment_source_composer->GetSource())));
200 program->GetFragmentShader()->SetLabel(name +
" fragment shader");
201 info.vertex_source_composer = vertex_source_composer;
202 info.fragment_source_composer = fragment_source_composer;
203 data_->AddProgramInfo(name, info);
209 const std::string& name) {
210 return data_->GetShaderProgram(name);
214 return data_->GetShaderProgramNames();
218 const std::string& name,
221 data_->GetShaderProgramComposers(name, vertex_source_composer,
222 fragment_source_composer);
226 data_->RecreateAllShaderPrograms();
230 const std::string& dependency) {
231 data_->RecreateShaderProgramsThatDependOn(dependency);
void RecreateShaderProgramsThatDependOn(const std::string &dependency)
Reconstructs all shaders that depend on the named dependency.
A ShaderProgram represents an OpenGL shader program that can be applied to shapes.
GenericLockGuard< port::Mutex > LockGuard
Convenient typedefs for ion::port::Mutex.
const gfx::ShaderProgramPtr CreateShaderProgram(const std::string &name, const ion::gfx::ShaderInputRegistryPtr ®istry, const ShaderSourceComposerPtr &vertex_source_composer, const ShaderSourceComposerPtr &fragment_source_composer)
Creates and returns a ShaderProgram with the passed name using the passed composers and registry...
void RecreateAllShaderPrograms()
Reconstructs all shaders from their composers.
base::WeakReferentPtr< ShaderProgram > ShaderProgramWeakPtr
const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Convenience function that returns the Allocator to use to allocate an object with a specific lifetime...
const std::vector< std::string > GetShaderProgramNames()
Gets a vector of the names of the shader programs created through the manager.
void GetShaderProgramComposers(const std::string &name, ShaderSourceComposerPtr *vertex_source_composer, ShaderSourceComposerPtr *fragment_source_composer)
Gets the composers used to construct the named program's shaders.
base::ReferentPtr< ShaderProgram >::Type ShaderProgramPtr
base::ReferentPtr< Shader >::Type ShaderPtr
Convenience typedef for shared pointers to Shaders.
base::ReferentPtr< GraphicsManager >::Type GraphicsManagerPtr
Convenience typedef for shared pointer to a GraphicsManager.
const gfx::ShaderProgramPtr GetShaderProgram(const std::string &name)
Returns a ReferentPtr to a ShaderProgram that has the passed name.
port::Mutex mutex_
Protects shared access to the Allocator and FT_Library.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
base::ReferentPtr< ShaderSourceComposer >::Type ShaderSourceComposerPtr
kMediumTerm is used for objects that don't fall into the kShortTerm or kLongTerm categories.