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 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 8 | #ifndef GrGLProgramDataManager_DEFINED |
| 9 | #define GrGLProgramDataManager_DEFINED |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 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; |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 19 | class GrGLProgram; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 20 | class GrGLProgramBuilder; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 21 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 22 | /** Manages the resources used by a shader program. |
| 23 | * The resources are objects the program uses to communicate with the |
| 24 | * application code. |
| 25 | */ |
| 26 | class GrGLProgramDataManager : public SkRefCnt { |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 27 | public: |
| 28 | // Opaque handle to a uniform |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 29 | class ShaderResourceHandle { |
| 30 | public: |
| 31 | bool isValid() const { return -1 != fValue; } |
| 32 | ShaderResourceHandle() |
| 33 | : fValue(-1) { |
| 34 | } |
| 35 | protected: |
| 36 | ShaderResourceHandle(int value) |
| 37 | : fValue(value) { |
| 38 | SkASSERT(isValid()); |
| 39 | } |
| 40 | int fValue; |
| 41 | }; |
| 42 | |
| 43 | class UniformHandle : public ShaderResourceHandle { |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 44 | public: |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 45 | /** Creates a reference to an unifrom of a GrGLShaderBuilder. |
| 46 | * The ref can be used to set the uniform with corresponding the GrGLProgramDataManager.*/ |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 47 | static UniformHandle CreateFromUniformIndex(int i); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 48 | UniformHandle() { } |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 49 | bool operator==(const UniformHandle& other) const { return other.fValue == fValue; } |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 50 | private: |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 51 | UniformHandle(int value) : ShaderResourceHandle(value) { } |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 52 | int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; } |
| 53 | int toShaderBuilderIndex() const { return toProgramDataIndex(); } |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 54 | |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 55 | friend class GrGLProgramDataManager; // For accessing toProgramDataIndex(). |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 56 | friend class GrGLProgramBuilder; // For accessing toShaderBuilderIndex(). |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 57 | }; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 58 | |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 59 | class VaryingHandle : public ShaderResourceHandle { |
| 60 | public: |
| 61 | /** Creates a reference to a varying in separable varyings of a GrGLShaderBuilder. |
| 62 | * The ref can be used to set the varying with the corresponding GrGLProgramDataManager.*/ |
| 63 | static VaryingHandle CreateFromSeparableVaryingIndex(int i) { |
| 64 | return VaryingHandle(i); |
| 65 | } |
| 66 | VaryingHandle() { } |
| 67 | bool operator==(const VaryingHandle& other) const { return other.fValue == fValue; } |
| 68 | private: |
| 69 | VaryingHandle(int value) : ShaderResourceHandle(value) { } |
| 70 | int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; } |
| 71 | friend class GrGLProgramDataManager; // For accessing toProgramDataIndex(). |
| 72 | }; |
| 73 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 74 | GrGLProgramDataManager(GrGpuGL*, GrGLProgram*, const GrGLProgramBuilder&); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 75 | |
| 76 | /** Functions for uploading uniform values. The varities ending in v can be used to upload to an |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 77 | * array of uniforms. arrayCount must be <= the array count of the uniform. |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 78 | */ |
| 79 | void setSampler(UniformHandle, GrGLint texUnit) const; |
| 80 | void set1f(UniformHandle, GrGLfloat v0) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 81 | void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 82 | void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 83 | void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 84 | void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 85 | void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 86 | void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 87 | void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 88 | // matrices are column-major, the first three upload a single matrix, the latter three upload |
| 89 | // arrayCount matrices into a uniform array. |
| 90 | void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; |
| 91 | void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 92 | void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
| 93 | void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 94 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 95 | // convenience method for uploading a SkMatrix to a 3x3 matrix uniform |
| 96 | void setSkMatrix(UniformHandle, const SkMatrix&) const; |
| 97 | |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 98 | void setProgramPathFragmentInputTransform(VaryingHandle i, |
| 99 | unsigned components, |
| 100 | const SkMatrix& matrix) const; |
| 101 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 102 | private: |
| 103 | enum { |
| 104 | kUnusedUniform = -1, |
| 105 | }; |
| 106 | |
| 107 | struct Uniform { |
| 108 | GrGLint fVSLocation; |
| 109 | GrGLint fFSLocation; |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 110 | SkDEBUGCODE( |
| 111 | GrSLType fType; |
| 112 | int fArrayCount; |
| 113 | ); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 114 | }; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 115 | struct Varying { |
| 116 | GrGLint fLocation; |
| 117 | SkDEBUGCODE( |
| 118 | GrSLType fType; |
| 119 | ); |
| 120 | }; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 121 | |
| 122 | SkTArray<Uniform, true> fUniforms; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 123 | SkTArray<Varying, true> fVaryings; |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 124 | GrGpuGL* fGpu; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 125 | GrGLProgram* fProgram; |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 126 | |
| 127 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | #endif |