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 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 12 | #include "GrDrawState.h" |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 13 | #include "GrGLContext.h" |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 14 | #include "GrGLProgramDesc.h" |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 15 | #include "GrGLShaderBuilder.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" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 18 | #include "GrGLUniformManager.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 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 23 | class GrBinHashKeyBuilder; |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 24 | class GrGLEffect; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 25 | class GrGLShaderBuilder; |
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 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 40 | static GrGLProgram* Create(GrGpuGL* gpu, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 41 | const GrGLProgramDesc& desc, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 42 | const GrEffectStage* colorStages[], |
| 43 | const GrEffectStage* coverageStages[]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 45 | virtual ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Call to abandon GL objects owned by this program. |
| 49 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 50 | void abandon(); |
| 51 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 52 | /** |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 53 | * The shader may modify the blend coefficients. Params are in/out. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 55 | void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 57 | const GrGLProgramDesc& getDesc() { return fDesc; } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 58 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 59 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 60 | * Gets the GL program ID for this program. |
| 61 | */ |
| 62 | GrGLuint programID() const { return fProgramID; } |
| 63 | |
| 64 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 65 | * 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] | 66 | * and coverage attributes can be global state. This struct is read and updated by |
| 67 | * 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] | 68 | * redundantly. |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 69 | */ |
| 70 | struct SharedGLState { |
| 71 | GrColor fConstAttribColor; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 72 | int fConstAttribColorIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 73 | GrColor fConstAttribCoverage; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 74 | int fConstAttribCoverageIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 75 | |
| 76 | SharedGLState() { this->invalidate(); } |
| 77 | void invalidate() { |
| 78 | fConstAttribColor = GrColor_ILLEGAL; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 79 | fConstAttribColorIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 80 | fConstAttribCoverage = GrColor_ILLEGAL; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 81 | fConstAttribCoverageIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 86 | * The GrDrawState's view matrix along with the aspects of the render target determine the |
| 87 | * matrix sent to GL. The size of the render target affects the GL matrix because we must |
| 88 | * convert from Skia device coords to GL's normalized coords. Also the origin of the render |
| 89 | * target may require us to perform a mirror-flip. |
| 90 | */ |
| 91 | struct MatrixState { |
| 92 | SkMatrix fViewMatrix; |
| 93 | SkISize fRenderTargetSize; |
| 94 | GrSurfaceOrigin fRenderTargetOrigin; |
| 95 | |
| 96 | MatrixState() { this->invalidate(); } |
| 97 | void invalidate() { |
| 98 | fViewMatrix = SkMatrix::InvalidMatrix(); |
bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 99 | fRenderTargetSize.fWidth = -1; |
| 100 | fRenderTargetSize.fHeight = -1; |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 101 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 102 | } |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 103 | template<int Size> void getGLMatrix(GrGLfloat* destMatrix) { |
| 104 | SkMatrix combined; |
| 105 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 106 | combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, |
| 107 | 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1, |
| 108 | 0, 0, SkMatrix::I()[8]); |
| 109 | } else { |
| 110 | combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, |
| 111 | 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1, |
| 112 | 0, 0, SkMatrix::I()[8]); |
| 113 | } |
| 114 | combined.setConcat(combined, fViewMatrix); |
| 115 | GrGLGetMatrix<Size>(destMatrix, combined); |
| 116 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /** |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 120 | * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a |
| 121 | * 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] | 122 | * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage |
| 123 | * stages come from GrGLProgramDesc::Build(). |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 124 | */ |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 125 | void setData(GrDrawState::BlendOptFlags, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 126 | const GrEffectStage* colorStages[], |
| 127 | const GrEffectStage* coverageStages[], |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 128 | const GrDeviceCoordTexture* dstCopy, // can be NULL |
| 129 | SharedGLState*); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 130 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 131 | private: |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 132 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 133 | |
bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 134 | // handles for uniforms (aside from per-effect samplers) |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 135 | struct UniformHandles { |
| 136 | UniformHandle fViewMatrixUni; |
| 137 | UniformHandle fColorUni; |
| 138 | UniformHandle fCoverageUni; |
| 139 | UniformHandle fColorFilterUni; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 140 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 141 | // We use the render target height to provide a y-down frag coord when specifying |
| 142 | // origin_upper_left is not supported. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 143 | UniformHandle fRTHeightUni; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 144 | |
| 145 | // Uniforms for computing texture coords to do the dst-copy lookup |
| 146 | UniformHandle fDstCopyTopLeftUni; |
| 147 | UniformHandle fDstCopyScaleUni; |
| 148 | UniformHandle fDstCopySamplerUni; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 151 | typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray; |
| 152 | typedef SkSTArray<4, int, true> TextureUnitSArray; |
| 153 | |
| 154 | struct EffectAndSamplers { |
| 155 | EffectAndSamplers() : fGLEffect(NULL) {} |
| 156 | ~EffectAndSamplers() { delete fGLEffect; } |
| 157 | GrGLEffect* fGLEffect; |
| 158 | SamplerUniSArray fSamplerUnis; // sampler uni handles for effect's GrTextureAccess |
| 159 | TextureUnitSArray fTextureUnits; // texture unit used for each entry of fSamplerUnis |
| 160 | }; |
| 161 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 162 | GrGLProgram(GrGpuGL* gpu, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 163 | const GrGLProgramDesc& desc, |
| 164 | const GrEffectStage* colorStages[], |
| 165 | const GrEffectStage* coverageStages[]); |
| 166 | |
| 167 | bool succeeded() const { return 0 != fProgramID; } |
| 168 | |
| 169 | /** |
| 170 | * This is the heavy initialization routine for building a GLProgram. colorStages and |
| 171 | * coverageStages correspond to the output of GrGLProgramDesc::Build(). |
| 172 | */ |
| 173 | bool genProgram(const GrEffectStage* colorStages[], const GrEffectStage* coverageStages[]); |
| 174 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 175 | // Sets the texture units for samplers |
| 176 | void initSamplerUniforms(); |
| 177 | void initEffectSamplerUniforms(EffectAndSamplers* effect, int* texUnitIdx); |
| 178 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 179 | // Helper for setData(). |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 180 | void setEffectData(const GrEffectStage& stage, const EffectAndSamplers& effect); |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 181 | |
| 182 | // Helper for setData(). Makes GL calls to specify the initial color when there is not |
| 183 | // per-vertex colors. |
| 184 | void setColor(const GrDrawState&, GrColor color, SharedGLState*); |
| 185 | |
| 186 | // Helper for setData(). Makes GL calls to specify the initial coverage when there is not |
| 187 | // per-vertex coverages. |
| 188 | void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*); |
| 189 | |
| 190 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
| 191 | void setMatrixAndRenderTargetHeight(const GrDrawState&); |
| 192 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame^] | 193 | // GL program ID |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 194 | GrGLuint fProgramID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 195 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 196 | // these reflect the current values of uniforms (GL uniform values travel with program) |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 197 | MatrixState fMatrixState; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 198 | GrColor fColor; |
| 199 | GrColor fCoverage; |
| 200 | GrColor fColorFilterColor; |
bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 201 | int fDstCopyTexUnit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 203 | SkTArray<EffectAndSamplers> fColorEffects; |
| 204 | SkTArray<EffectAndSamplers> fCoverageEffects; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 205 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 206 | GrGLProgramDesc fDesc; |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 207 | GrGpuGL* fGpu; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 208 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 209 | GrGLUniformManager fUniformManager; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 210 | UniformHandles fUniformHandles; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 211 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 212 | typedef SkRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | #endif |