bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrGLUniformManager_DEFINED |
| 9 | #define GrGLUniformManager_DEFINED |
| 10 | |
| 11 | #include "gl/GrGLShaderVar.h" |
| 12 | #include "gl/GrGLSL.h" |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 13 | #include "GrAllocator.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 14 | |
| 15 | #include "SkTArray.h" |
| 16 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 17 | class GrGpuGL; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 18 | class SkMatrix; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 19 | |
| 20 | /** Manages a program's uniforms. |
| 21 | */ |
| 22 | class GrGLUniformManager { |
| 23 | public: |
| 24 | // Opaque handle to a uniform |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 25 | class UniformHandle { |
| 26 | public: |
| 27 | static UniformHandle CreateFromUniformIndex(int i); |
| 28 | |
| 29 | bool isValid() const { return 0 != fValue; } |
| 30 | |
| 31 | bool operator==(const UniformHandle& other) const { return other.fValue == fValue; } |
| 32 | |
| 33 | UniformHandle() |
| 34 | : fValue(0) { |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | UniformHandle(int value) |
| 39 | : fValue(~value) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 40 | SkASSERT(isValid()); |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 41 | } |
| 42 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 43 | int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; } |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 44 | |
| 45 | int fValue; |
| 46 | friend class GrGLUniformManager; // For accessing toUniformIndex(). |
| 47 | }; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 49 | GrGLUniformManager(GrGpuGL* gpu) : fGpu(gpu) {} |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 50 | |
| 51 | UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::kNonArray); |
| 52 | |
| 53 | /** Functions for uploading uniform values. The varities ending in v can be used to upload to an |
| 54 | * array of uniforms. offset + arrayCount must be <= the array count of the uniform. |
| 55 | */ |
| 56 | void setSampler(UniformHandle, GrGLint texUnit) const; |
| 57 | void set1f(UniformHandle, GrGLfloat v0) const; |
| 58 | void set1fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const; |
| 59 | void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; |
| 60 | void set2fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const; |
| 61 | void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; |
| 62 | void set3fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const; |
| 63 | void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; |
| 64 | void set4fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const; |
| 65 | // matrices are column-major, the first three upload a single matrix, the latter three upload |
| 66 | // arrayCount matrices into a uniform array. |
| 67 | void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; |
| 68 | void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; |
| 69 | void setMatrix3fv(UniformHandle, int offset, int arrayCount, const GrGLfloat matrices[]) const; |
| 70 | void setMatrix4fv(UniformHandle, int offset, int arrayCount, const GrGLfloat matrices[]) const; |
| 71 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 72 | // convenience method for uploading a SkMatrix to a 3x3 matrix uniform |
| 73 | void setSkMatrix(UniformHandle, const SkMatrix&) const; |
| 74 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 75 | struct BuilderUniform { |
| 76 | GrGLShaderVar fVariable; |
| 77 | uint32_t fVisibility; |
| 78 | }; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 79 | // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory |
| 80 | // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their |
| 81 | // name strings. Otherwise, we'd have to hand out copies. |
| 82 | typedef GrTAllocator<BuilderUniform> BuilderUniformArray; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Called by the GrGLShaderBuilder to get GL locations for all uniforms. |
| 86 | */ |
| 87 | void getUniformLocations(GrGLuint programID, const BuilderUniformArray& uniforms); |
| 88 | |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Called by the GrGLShaderBuilder to access the array by the handle (index). |
| 91 | */ |
| 92 | const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUniformManager::UniformHandle) const; |
| 93 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 94 | private: |
| 95 | enum { |
| 96 | kUnusedUniform = -1, |
| 97 | }; |
| 98 | |
| 99 | struct Uniform { |
| 100 | GrGLint fVSLocation; |
| 101 | GrGLint fFSLocation; |
| 102 | GrSLType fType; |
| 103 | int fArrayCount; |
| 104 | }; |
| 105 | |
| 106 | SkTArray<Uniform, true> fUniforms; |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 107 | GrGpuGL* fGpu; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | #endif |