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" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 15 | #include "GrGLSL.h" |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 16 | #include "GrGLTexture.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 17 | #include "GrGLUniformManager.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 19 | #include "SkString.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 20 | #include "SkXfermode.h" |
| 21 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 22 | class GrBinHashKeyBuilder; |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 23 | class GrGLEffect; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 24 | class GrGLShaderBuilder; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * This class manages a GPU program and records per-program information. |
| 28 | * We can specify the attribute locations so that they are constant |
| 29 | * across our shaders. But the driver determines the uniform locations |
| 30 | * at link time. We don't need to remember the sampler uniform location |
| 31 | * because we will bind a texture slot to it and never change it |
| 32 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 33 | * previous uniform state after a program change. |
| 34 | */ |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 35 | class GrGLProgram : public GrRefCnt { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 36 | public: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 37 | SK_DECLARE_INST_COUNT(GrGLProgram) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 38 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 39 | static GrGLProgram* Create(const GrGLContext& gl, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 40 | const GrGLProgramDesc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 41 | const GrEffectStage* stages[]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 43 | virtual ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Call to abandon GL objects owned by this program. |
| 47 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 48 | void abandon(); |
| 49 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 50 | /** |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 51 | * The shader may modify the blend coefficients. Params are in/out. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 52 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 53 | void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 55 | const GrGLProgramDesc& getDesc() { return fDesc; } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 57 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 58 | * Gets the GL program ID for this program. |
| 59 | */ |
| 60 | GrGLuint programID() const { return fProgramID; } |
| 61 | |
| 62 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 63 | * 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] | 64 | * and coverage attributes can be global state. This struct is read and updated by |
| 65 | * 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] | 66 | * redundantly. |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 67 | */ |
| 68 | struct SharedGLState { |
| 69 | GrColor fConstAttribColor; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 70 | int fConstAttribColorIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 71 | GrColor fConstAttribCoverage; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 72 | int fConstAttribCoverageIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 73 | |
| 74 | SharedGLState() { this->invalidate(); } |
| 75 | void invalidate() { |
| 76 | fConstAttribColor = GrColor_ILLEGAL; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 77 | fConstAttribColorIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 78 | fConstAttribCoverage = GrColor_ILLEGAL; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 79 | fConstAttribCoverageIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
| 83 | /** |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 84 | * The GrDrawState's view matrix along with the aspects of the render target determine the |
| 85 | * matrix sent to GL. The size of the render target affects the GL matrix because we must |
| 86 | * convert from Skia device coords to GL's normalized coords. Also the origin of the render |
| 87 | * target may require us to perform a mirror-flip. |
| 88 | */ |
| 89 | struct MatrixState { |
| 90 | SkMatrix fViewMatrix; |
| 91 | SkISize fRenderTargetSize; |
| 92 | GrSurfaceOrigin fRenderTargetOrigin; |
| 93 | |
| 94 | MatrixState() { this->invalidate(); } |
| 95 | void invalidate() { |
| 96 | fViewMatrix = SkMatrix::InvalidMatrix(); |
bsalomon@google.com | 45a412e | 2013-02-13 16:13:13 +0000 | [diff] [blame] | 97 | fRenderTargetSize.fWidth = -1; |
| 98 | fRenderTargetSize.fHeight = -1; |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 99 | fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | /** |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 104 | * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a |
| 105 | * draw occurs using the program after the program has already been bound. It also uses the |
| 106 | * GrGpuGL object to bind the textures required by the GrGLEffects. |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 107 | */ |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 108 | void setData(GrGpuGL*, |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 109 | GrDrawState::BlendOptFlags, |
| 110 | const GrEffectStage* stages[], // output of GrGLProgramDesc:Build() |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 111 | const GrDeviceCoordTexture* dstCopy, // can be NULL |
| 112 | SharedGLState*); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 113 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 114 | private: |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 115 | GrGLProgram(const GrGLContext& gl, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 116 | const GrGLProgramDesc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 117 | const GrEffectStage* stages[]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 118 | |
| 119 | bool succeeded() const { return 0 != fProgramID; } |
| 120 | |
| 121 | /** |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame^] | 122 | * This is the heavy initialization routine for building a GLProgram. stages is all the enabled |
| 123 | * color stages followed by all the enabled coverage stages as output by |
| 124 | * GrGLProgramDesc::Build() |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 125 | */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 126 | bool genProgram(const GrEffectStage* stages[]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 127 | |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 128 | GrSLConstantVec genInputColor(GrGLShaderBuilder* builder, SkString* inColor); |
| 129 | |
| 130 | GrSLConstantVec genInputCoverage(GrGLShaderBuilder* builder, SkString* inCoverage); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 131 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 132 | void genGeometryShader(GrGLShaderBuilder* segments) const; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 133 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 134 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 135 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 136 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 137 | bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 138 | bool bindColorOut, |
| 139 | bool bindDualSrcOut); |
| 140 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 141 | // Sets the texture units for samplers |
| 142 | void initSamplerUniforms(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 143 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 144 | bool compileShaders(const GrGLShaderBuilder& builder); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 145 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 146 | const char* adjustInColor(const SkString& inColor) const; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 147 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 148 | // Helper for setData(). Makes GL calls to specify the initial color when there is not |
| 149 | // per-vertex colors. |
| 150 | void setColor(const GrDrawState&, GrColor color, SharedGLState*); |
| 151 | |
| 152 | // Helper for setData(). Makes GL calls to specify the initial coverage when there is not |
| 153 | // per-vertex coverages. |
| 154 | void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*); |
| 155 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 156 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
| 157 | void setMatrixAndRenderTargetHeight(const GrDrawState&); |
| 158 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 159 | typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 160 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 161 | struct UniformHandles { |
| 162 | UniformHandle fViewMatrixUni; |
| 163 | UniformHandle fColorUni; |
| 164 | UniformHandle fCoverageUni; |
| 165 | UniformHandle fColorFilterUni; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 166 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 167 | // We use the render target height to provide a y-down frag coord when specifying |
| 168 | // origin_upper_left is not supported. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 169 | UniformHandle fRTHeightUni; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 170 | |
| 171 | // Uniforms for computing texture coords to do the dst-copy lookup |
| 172 | UniformHandle fDstCopyTopLeftUni; |
| 173 | UniformHandle fDstCopyScaleUni; |
| 174 | UniformHandle fDstCopySamplerUni; |
| 175 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 176 | // An array of sampler uniform handles for each effect. |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 177 | SamplerUniSArray fEffectSamplerUnis[GrDrawState::kNumStages]; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 179 | UniformHandles() { |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 180 | fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle; |
| 181 | fColorUni = GrGLUniformManager::kInvalidUniformHandle; |
| 182 | fCoverageUni = GrGLUniformManager::kInvalidUniformHandle; |
| 183 | fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 184 | fRTHeightUni = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 185 | fDstCopyTopLeftUni = GrGLUniformManager::kInvalidUniformHandle; |
| 186 | fDstCopyScaleUni = GrGLUniformManager::kInvalidUniformHandle; |
| 187 | fDstCopySamplerUni = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 188 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 191 | // GL IDs |
| 192 | GrGLuint fVShaderID; |
| 193 | GrGLuint fGShaderID; |
| 194 | GrGLuint fFShaderID; |
| 195 | GrGLuint fProgramID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 197 | // 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] | 198 | MatrixState fMatrixState; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 199 | GrColor fColor; |
| 200 | GrColor fCoverage; |
| 201 | GrColor fColorFilterColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 203 | GrGLEffect* fEffects[GrDrawState::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 204 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 205 | GrGLProgramDesc fDesc; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 206 | const GrGLContext& fContext; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 207 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 208 | GrGLUniformManager fUniformManager; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 209 | UniformHandles fUniformHandles; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 210 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 211 | typedef GrRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | #endif |