michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrGLUniformManager_DEFINED michael@0: #define GrGLUniformManager_DEFINED michael@0: michael@0: #include "gl/GrGLShaderVar.h" michael@0: #include "gl/GrGLSL.h" michael@0: #include "GrAllocator.h" michael@0: michael@0: #include "SkTArray.h" michael@0: michael@0: class GrGpuGL; michael@0: class SkMatrix; michael@0: michael@0: /** Manages a program's uniforms. michael@0: */ michael@0: class GrGLUniformManager { michael@0: public: michael@0: // Opaque handle to a uniform michael@0: class UniformHandle { michael@0: public: michael@0: static UniformHandle CreateFromUniformIndex(int i); michael@0: michael@0: bool isValid() const { return 0 != fValue; } michael@0: michael@0: bool operator==(const UniformHandle& other) const { return other.fValue == fValue; } michael@0: michael@0: UniformHandle() michael@0: : fValue(0) { michael@0: } michael@0: michael@0: private: michael@0: UniformHandle(int value) michael@0: : fValue(~value) { michael@0: SkASSERT(isValid()); michael@0: } michael@0: michael@0: int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; } michael@0: michael@0: int fValue; michael@0: friend class GrGLUniformManager; // For accessing toUniformIndex(). michael@0: }; michael@0: michael@0: GrGLUniformManager(GrGpuGL* gpu); michael@0: michael@0: UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::kNonArray); michael@0: michael@0: /** Functions for uploading uniform values. The varities ending in v can be used to upload to an michael@0: * array of uniforms. arrayCount must be <= the array count of the uniform. michael@0: */ michael@0: void setSampler(UniformHandle, GrGLint texUnit) const; michael@0: void set1f(UniformHandle, GrGLfloat v0) const; michael@0: void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; michael@0: void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; michael@0: void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; michael@0: void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; michael@0: void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; michael@0: void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; michael@0: void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; michael@0: // matrices are column-major, the first three upload a single matrix, the latter three upload michael@0: // arrayCount matrices into a uniform array. michael@0: void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; michael@0: void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; michael@0: void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; michael@0: void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; michael@0: michael@0: // convenience method for uploading a SkMatrix to a 3x3 matrix uniform michael@0: void setSkMatrix(UniformHandle, const SkMatrix&) const; michael@0: michael@0: struct BuilderUniform { michael@0: GrGLShaderVar fVariable; michael@0: uint32_t fVisibility; michael@0: }; michael@0: // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory michael@0: // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their michael@0: // name strings. Otherwise, we'd have to hand out copies. michael@0: typedef GrTAllocator BuilderUniformArray; michael@0: michael@0: /** michael@0: * Called by the GrGLShaderBuilder to know if the manager is using michael@0: * BindUniformLocation. In that case getUniformLocations must be called michael@0: * before the program is linked. michael@0: */ michael@0: bool isUsingBindUniform() const { return fUsingBindUniform; } michael@0: michael@0: /** michael@0: * Called by the GrGLShaderBuilder to get GL locations for all uniforms. michael@0: */ michael@0: void getUniformLocations(GrGLuint programID, const BuilderUniformArray& uniforms); michael@0: michael@0: /** michael@0: * Called by the GrGLShaderBuilder to access the array by the handle (index). michael@0: */ michael@0: const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUniformManager::UniformHandle) const; michael@0: michael@0: private: michael@0: enum { michael@0: kUnusedUniform = -1, michael@0: }; michael@0: michael@0: struct Uniform { michael@0: GrGLint fVSLocation; michael@0: GrGLint fFSLocation; michael@0: GrSLType fType; michael@0: int fArrayCount; michael@0: }; michael@0: michael@0: bool fUsingBindUniform; michael@0: SkTArray fUniforms; michael@0: GrGpuGL* fGpu; michael@0: }; michael@0: michael@0: #endif