junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 9 | #ifndef GrGLProgram_DEFINED |
| 10 | #define GrGLProgram_DEFINED |
| 11 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 12 | #include "builders/GrGLProgramBuilder.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 13 | #include "GrDrawState.h" |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 14 | #include "GrGLContext.h" |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 15 | #include "GrGLProgramDesc.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 16 | #include "GrGLSL.h" |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 17 | #include "GrGLTexture.h" |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 18 | #include "GrGLProgramDataManager.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 20 | #include "SkString.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 21 | #include "SkXfermode.h" |
| 22 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 23 | class GrGLEffect; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 24 | class GrGLProgramEffects; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 25 | class GrGLProgramBuilder; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * This class manages a GPU program and records per-program information. |
| 29 | * We can specify the attribute locations so that they are constant |
| 30 | * across our shaders. But the driver determines the uniform locations |
| 31 | * at link time. We don't need to remember the sampler uniform location |
| 32 | * because we will bind a texture slot to it and never change it |
| 33 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 34 | * previous uniform state after a program change. |
| 35 | */ |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 36 | class GrGLProgram : public SkRefCnt { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 37 | public: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 38 | SK_DECLARE_INST_COUNT(GrGLProgram) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 39 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 40 | typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 41 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 42 | static GrGLProgram* Create(GrGpuGL* gpu, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 43 | const GrGLProgramDesc& desc, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 44 | const GrEffectStage* colorStages[], |
| 45 | const GrEffectStage* coverageStages[]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 47 | virtual ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 48 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Call to abandon GL objects owned by this program. |
| 51 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 52 | void abandon(); |
| 53 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | /** |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 55 | * The shader may modify the blend coefficients. Params are in/out. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 57 | void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 58 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 59 | const GrGLProgramDesc& getDesc() { return fDesc; } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 61 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 62 | * Gets the GL program ID for this program. |
| 63 | */ |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 64 | GrGLuint programID() const { return fProgramID; } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 65 | |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 66 | bool hasVertexShader() const { return fHasVertexShader; } |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 67 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 68 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 69 | * Some GL state that is relevant to programs is not stored per-program. In particular color |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 70 | * and coverage attributes can be global state. This struct is read and updated by |
| 71 | * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 72 | * redundantly. |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 73 | */ |
| 74 | struct SharedGLState { |
| 75 | GrColor fConstAttribColor; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 76 | int fConstAttribColorIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 77 | GrColor fConstAttribCoverage; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 78 | int fConstAttribCoverageIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 79 | |
| 80 | SharedGLState() { this->invalidate(); } |
| 81 | void invalidate() { |
| 82 | fConstAttribColor = GrColor_ILLEGAL; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 83 | fConstAttribColorIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 84 | fConstAttribCoverage = GrColor_ILLEGAL; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 85 | fConstAttribCoverageIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
| 89 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 90 | * The GrDrawState's view matrix along with the aspects of the render target determine the |
| 91 | * matrix sent to GL. The size of the render target affects the GL matrix because we must |
| 92 | * convert from Skia device coords to GL's normalized coords. Also the origin of the render |
| 93 | * target may require us to perform a mirror-flip. |
| 94 | */ |
| 95 | struct MatrixState { |
| 96 | SkMatrix fViewMatrix; |
| 97 | SkISize fRenderTargetSize; |
| 98 | GrSurfaceOrigin fRenderTargetOrigin; |
| 99 | |
| 100 | MatrixState() { this->invalidate(); } |
| 101 | void invalidate() { |
| 102 | fViewMatrix = SkMatrix::InvalidMatrix(); |
bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 103 | fRenderTargetSize.fWidth = -1; |
| 104 | fRenderTargetSize.fHeight = -1; |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 105 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 106 | } |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Gets a matrix that goes from local coords to Skia's device coordinates. |
| 110 | */ |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 111 | template<int Size> void getGLMatrix(GrGLfloat* destMatrix) { |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 112 | GrGLGetMatrix<Size>(destMatrix, fViewMatrix); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Gets a matrix that goes from local coordinates to GL normalized device coords. |
| 117 | */ |
| 118 | template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) { |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 119 | SkMatrix combined; |
| 120 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 121 | combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, |
| 122 | 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1, |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 123 | 0, 0, 1); |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 124 | } else { |
| 125 | combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, |
| 126 | 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1, |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 127 | 0, 0, 1); |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 128 | } |
commit-bot@chromium.org | b930cc3 | 2014-03-28 14:29:23 +0000 | [diff] [blame] | 129 | combined.preConcat(fViewMatrix); |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 130 | GrGLGetMatrix<Size>(destMatrix, combined); |
| 131 | } |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device |
| 135 | * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is |
| 136 | * applied as such: |
| 137 | * pos.x = dot(v.xy, pos.xz) |
| 138 | * pos.y = dot(v.zq, pos.yz) |
| 139 | */ |
| 140 | void getRTAdjustmentVec(GrGLfloat* destVec) { |
| 141 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 142 | destVec[1] = -1.f; |
| 143 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 144 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 145 | destVec[3] = 1.f; |
| 146 | } else { |
| 147 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 148 | destVec[3] = -1.f; |
| 149 | } |
| 150 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /** |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 154 | * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a |
| 155 | * draw occurs using the program after the program has already been bound. It also uses the |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 156 | * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage |
| 157 | * stages come from GrGLProgramDesc::Build(). |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 158 | */ |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 159 | void setData(GrGpu::DrawType, |
| 160 | GrDrawState::BlendOptFlags, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 161 | const GrEffectStage* colorStages[], |
| 162 | const GrEffectStage* coverageStages[], |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 163 | const GrDeviceCoordTexture* dstCopy, // can be NULL |
| 164 | SharedGLState*); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 165 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 166 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 167 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 168 | |
commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 169 | GrGLProgram(GrGpuGL*, |
| 170 | const GrGLProgramDesc&, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 171 | const GrGLProgramBuilder&); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 172 | |
commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 173 | // Sets the texture units for samplers. |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 174 | void initSamplerUniforms(); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 175 | |
| 176 | // Helper for setData(). Makes GL calls to specify the initial color when there is not |
| 177 | // per-vertex colors. |
| 178 | void setColor(const GrDrawState&, GrColor color, SharedGLState*); |
| 179 | |
| 180 | // Helper for setData(). Makes GL calls to specify the initial coverage when there is not |
| 181 | // per-vertex coverages. |
| 182 | void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*); |
| 183 | |
| 184 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 185 | void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrDrawState&); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 186 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 187 | // these reflect the current values of uniforms (GL uniform values travel with program) |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 188 | MatrixState fMatrixState; |
| 189 | GrColor fColor; |
| 190 | GrColor fCoverage; |
| 191 | int fDstCopyTexUnit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 192 | |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 193 | BuiltinUniformHandles fBuiltinUniformHandles; |
| 194 | SkAutoTUnref<GrGLProgramEffects> fColorEffects; |
| 195 | SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; |
| 196 | GrGLuint fProgramID; |
| 197 | bool fHasVertexShader; |
| 198 | int fTexCoordSetCnt; |
skia.committer@gmail.com | 9681eeb | 2014-05-30 03:06:10 +0000 | [diff] [blame] | 199 | |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 200 | GrGLProgramDesc fDesc; |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 201 | GrGpuGL* fGpu; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 202 | |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 203 | GrGLProgramDataManager fProgramDataManager; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 204 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 205 | typedef SkRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | #endif |