Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shaderprogram.cc
Go to the documentation of this file.
1 
18 #include "ion/gfx/shaderprogram.h"
19 
21 
22 namespace ion {
23 namespace gfx {
24 
26  : vertex_shader_(kVertexShaderChanged, ShaderPtr(), this),
27  fragment_shader_(kFragmentShaderChanged, ShaderPtr(), this),
28  registry_(registry),
29  concurrent_(false),
30  concurrent_set_(false) {
31  DCHECK(registry_.Get());
32 }
33 
35  if (Shader* shader = vertex_shader_.Get().Get())
36  shader->RemoveReceiver(this);
37  if (Shader* shader = fragment_shader_.Get().Get())
38  shader->RemoveReceiver(this);
39 }
40 
42  const std::string& id_string,
43  const ShaderInputRegistryPtr& registry_ptr,
44  const std::string& vertex_shader_string,
45  const std::string& fragment_shader_string,
46  const base::AllocatorPtr& allocator) {
47  ShaderProgramPtr program(new(allocator) ShaderProgram(registry_ptr));
48  program->SetLabel(id_string);
49  program->SetVertexShader(
50  ShaderPtr(new(allocator) Shader(vertex_shader_string)));
51  program->GetVertexShader()->SetLabel(id_string + " vertex shader");
52  program->SetFragmentShader(
53  ShaderPtr(new(allocator) Shader(fragment_shader_string)));
54  program->GetFragmentShader()->SetLabel(id_string + " fragment shader");
55  return program;
56 }
57 
58 void ShaderProgram::OnNotify(const base::Notifier* notifier) {
59  if (GetResourceCount()) {
60  if (notifier == vertex_shader_.Get().Get())
62  else if (notifier == fragment_shader_.Get().Get())
64  }
65 }
66 
68  if (concurrent_set_) {
70  if (value != concurrent_) {
71  LOG(WARNING) << "Shader program resources already created"
72  << " - cannot change concurrency" << std::endl;
73  }
74  } else {
75  concurrent_ = value;
76  concurrent_set_ = true;
77  }
78 }
79 
80 } // namespace gfx
81 } // namespace ion
#define DCHECK(expr)
Definition: logging.h:331
double value
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
A Notifier both sends notifications to and receives notifications from other Notifiers.
Definition: notifier.h:35
void OnChanged(int bit) const
Forwards OnChanged to all resources.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
void SetConcurrent(bool value)
Sets/returns whether this shader program should have per-thread state.
base::ReferentPtr< Shader >::Type ShaderPtr
Convenience typedef for shared pointers to Shaders.
static const ShaderProgramPtr BuildFromStrings(const std::string &id_string, const ShaderInputRegistryPtr &registry_ptr, const std::string &vertex_shader_string, const std::string &fragment_shader_string, const base::AllocatorPtr &allocator)
Convenience function that builds and returns a new ShaderProgram instance that uses the given ShaderI...
A Shader represents an OpenGL shader stage.
Definition: shader.h:59
int GetResourceCount() const
Returns the number of resources that this holder holds.
ShaderProgram(const ShaderInputRegistryPtr &registry)
A valid ShaderInputRegistryPtr must be passed to the constructor.
~ShaderProgram() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60