15 #ifndef FPLBASE_SHADER_H
16 #define FPLBASE_SHADER_H
20 #include "fplbase/config.h"
23 #include "fplbase/handles.h"
24 #include "mathfu/glsl_mappings.h"
34 static const int kMaxTexturesPerShader = 8;
35 static const int kNumVec4sInAffineTransform = 3;
44 Shader(
const char *
filename,
const std::vector<std::string> &local_defines,
46 :
AsyncAsset(filename ? filename :
""), impl_(CreateShaderImpl()) {
48 Init(invalid , invalid , invalid ,
49 local_defines, renderer);
53 : impl_(CreateShaderImpl()) {
54 static const std::vector<std::string> empty_defines;
55 Init(program, vs, ps, empty_defines,
nullptr );
78 const std::vector<std::string> &global_defines_to_add,
79 const std::vector<std::string> &global_defines_to_omit);
97 bool IsValid() {
return ValidShaderHandle(program_); }
122 size_t num_components);
132 const mathfu::Vector<float, N> &value) {
145 const mathfu::Vector<float, N> &value) {
147 if (!ValidUniformHandle(loc))
return false;
161 if (!ValidUniformHandle(loc))
return false;
173 bool SetUniform(
const char *uniform_name,
const mathfu::mat4 &value) {
175 if (!ValidUniformHandle(loc))
return false;
176 SetUniform(loc, &value[0],
sizeof(value) /
sizeof(
float));
180 void InitializeUniforms();
184 bool HasDefine(
const char *define)
const {
185 return enabled_defines_.find(define) != enabled_defines_.end();
188 bool IsDirty()
const {
return dirty_; }
200 ShaderImpl *impl() {
return impl_; }
208 friend class RendererBase;
211 struct ShaderSourcePair {
212 std::string vertex_shader;
213 std::string fragment_shader;
217 void Init(ShaderHandle program, ShaderHandle vs, ShaderHandle ps,
218 const std::vector<std::string> &defines, Renderer *renderer);
223 void Reset(ShaderHandle program, ShaderHandle vs, ShaderHandle ps);
225 bool ReloadInternal();
227 ShaderSourcePair *LoadSourceFile();
231 static ShaderImpl *CreateShaderImpl();
232 static void DestroyShaderImpl(ShaderImpl *impl);
236 ShaderHandle program_;
240 UniformHandle uniform_model_view_projection_;
241 UniformHandle uniform_model_;
242 UniformHandle uniform_color_;
243 UniformHandle uniform_light_pos_;
244 UniformHandle uniform_camera_pos_;
245 UniformHandle uniform_time_;
246 UniformHandle uniform_bone_transforms_;
252 std::vector<std::string> local_defines_;
256 std::set<std::string> enabled_defines_;
265 #endif // FPLBASE_SHADER_H
bool IsValid()
Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetMana...
Definition: shader.h:97
void MarkDirty()
Call to mark the shader as needing to be reloaded.
Definition: shader.h:197
Represents a shader consisting of a vertex and pixel shader.
Definition: shader.h:42
const std::string & filename() const
The name of the file associated with the resource.
Definition: async_loader.h:107
bool SetUniform(const char *uniform_name, const mathfu::mat4 &value)
Set a non-standard uniform to a mat4 value.
Definition: shader.h:173
void SetUniform(UniformHandle uniform_loc, const float *value, size_t num_components)
Raw call to set any uniform (with 1/2/3/4/16 components).
virtual bool Finalize()
Creates a Shader from data_.
virtual void Load()
Loads and unpacks the Shader from filename_ into data_.
bool SetUniform(const char *uniform_name, const mathfu::Vector< float, N > &value)
Convenience call that does a Lookup and a Set if found.
Definition: shader.h:144
bool ReloadIfDirty()
If the shader has been marked dirty, reload it and clear the dirty flag.
bool SetUniform(const char *uniform_name, float value)
Set a non-standard uniform to a float value.
Definition: shader.h:159
void UpdateGlobalDefines(const std::vector< std::string > &global_defines_to_add, const std::vector< std::string > &global_defines_to_omit)
Recalculates the defines of this shader, and marks dirty if they differ from the last Reload()...
void Set(const Renderer &renderer) const
Activate this shader for subsequent draw calls.
static Shader * LoadFromShaderDef(const char *filename)
Loads a .fplshader file from disk. Used by the more convenient AssetManager interface, but can also be used without it.
AsyncAsset()
Default constructor for an empty AsyncAsset.
Definition: async_loader.h:54
UniformHandle FindUniform(const char *uniform_name)
Find a non-standard uniform by name.
Renderer is the main API class for rendering commands.
Definition: renderer.h:310
Definition: async_loader.h:48
void SetUniform(UniformHandle uniform_loc, const mathfu::Vector< float, N > &value)
Set an non-standard uniform to a vec2/3/4 value.
Definition: shader.h:131