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 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 17 | class GrGLGpu; |
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(). |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 57 | friend class GrGLGeometryProcessor; |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 58 | }; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 59 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 60 | struct UniformInfo { |
| 61 | GrGLShaderVar fVariable; |
| 62 | uint32_t fVisibility; |
| 63 | GrGLint fLocation; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 66 | // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory |
| 67 | // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their |
| 68 | // name strings. Otherwise, we'd have to hand out copies. |
| 69 | typedef GrTAllocator<UniformInfo> UniformInfoArray; |
| 70 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 71 | GrGLProgramDataManager(GrGLGpu*, const UniformInfoArray&); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 72 | |
| 73 | /** 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] | 74 | * 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] | 75 | */ |
| 76 | void setSampler(UniformHandle, GrGLint texUnit) const; |
| 77 | void set1f(UniformHandle, GrGLfloat v0) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 78 | void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 79 | void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 80 | void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 81 | void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 82 | void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 83 | void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 84 | void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 85 | // matrices are column-major, the first three upload a single matrix, the latter three upload |
| 86 | // arrayCount matrices into a uniform array. |
| 87 | void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; |
| 88 | void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 89 | void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
| 90 | void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 92 | // convenience method for uploading a SkMatrix to a 3x3 matrix uniform |
| 93 | void setSkMatrix(UniformHandle, const SkMatrix&) const; |
| 94 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 95 | private: |
| 96 | enum { |
| 97 | kUnusedUniform = -1, |
| 98 | }; |
| 99 | |
| 100 | struct Uniform { |
| 101 | GrGLint fVSLocation; |
| 102 | GrGLint fFSLocation; |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 103 | SkDEBUGCODE( |
| 104 | GrSLType fType; |
| 105 | int fArrayCount; |
| 106 | ); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
joshualitt | 7d022d6 | 2015-05-12 12:03:50 -0700 | [diff] [blame^] | 109 | SkDEBUGCODE(void printUnused(const Uniform&) const;) |
joshualitt | af2d56d | 2015-05-11 06:21:34 -0700 | [diff] [blame] | 110 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 111 | SkTArray<Uniform, true> fUniforms; |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 112 | GrGLGpu* fGpu; |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 113 | |
| 114 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 115 | }; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 116 | #endif |