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