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 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 8 | #include "GrGpuGL.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 10 | #include "GrEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 11 | #include "GrGLEffect.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 12 | #include "GrGpuVertex.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 14 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 15 | static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle; |
| 16 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | #define SKIP_CACHE_CHECK true |
| 18 | #define GR_UINT32_MAX static_cast<uint32_t>(-1) |
| 19 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 20 | GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl) |
| 21 | : fCount(0) |
| 22 | , fCurrLRUStamp(0) |
| 23 | , fGL(gl) { |
| 24 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 26 | void GrGpuGL::ProgramCache::abandon() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 27 | for (int i = 0; i < fCount; ++i) { |
| 28 | GrAssert(NULL != fEntries[i].fProgram.get()); |
| 29 | fEntries[i].fProgram->abandon(); |
| 30 | fEntries[i].fProgram.reset(NULL); |
| 31 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 32 | fCount = 0; |
| 33 | } |
| 34 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 35 | GrGLProgram* GrGpuGL::ProgramCache::getProgram(const ProgramDesc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 36 | const GrEffectStage* stages[]) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 37 | Entry newEntry; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 38 | newEntry.fKey.setKeyData(desc.asKey()); |
| 39 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 40 | Entry* entry = fHashCache.find(newEntry.fKey); |
| 41 | if (NULL == entry) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 42 | newEntry.fProgram.reset(GrGLProgram::Create(fGL, desc, stages)); |
| 43 | if (NULL == newEntry.fProgram.get()) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
| 46 | if (fCount < kMaxEntries) { |
| 47 | entry = fEntries + fCount; |
| 48 | ++fCount; |
| 49 | } else { |
| 50 | GrAssert(kMaxEntries == fCount); |
| 51 | entry = fEntries; |
| 52 | for (int i = 1; i < kMaxEntries; ++i) { |
| 53 | if (fEntries[i].fLRUStamp < entry->fLRUStamp) { |
| 54 | entry = fEntries + i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 55 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 57 | fHashCache.remove(entry->fKey, entry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 58 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 59 | *entry = newEntry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 60 | fHashCache.insert(entry->fKey, entry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 61 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 62 | |
| 63 | entry->fLRUStamp = fCurrLRUStamp; |
| 64 | if (GR_UINT32_MAX == fCurrLRUStamp) { |
| 65 | // wrap around! just trash our LRU, one time hit. |
| 66 | for (int i = 0; i < fCount; ++i) { |
| 67 | fEntries[i].fLRUStamp = 0; |
| 68 | } |
| 69 | } |
| 70 | ++fCurrLRUStamp; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 71 | return entry->fProgram; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 72 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 74 | //////////////////////////////////////////////////////////////////////////////// |
| 75 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 76 | void GrGpuGL::abandonResources(){ |
| 77 | INHERITED::abandonResources(); |
| 78 | fProgramCache->abandon(); |
| 79 | fHWProgramID = 0; |
| 80 | } |
| 81 | |
| 82 | //////////////////////////////////////////////////////////////////////////////// |
| 83 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 84 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 85 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 86 | void GrGpuGL::flushViewMatrix(DrawType type) { |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 87 | const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget()); |
| 88 | SkISize viewportSize; |
| 89 | const GrGLIRect& viewport = rt->getViewport(); |
| 90 | viewportSize.set(viewport.fWidth, viewport.fHeight); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 92 | const SkMatrix& vm = this->getDrawState().getViewMatrix(); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 94 | if (kStencilPath_DrawType == type) { |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 95 | if (fHWPathMatrixState.fLastOrigin != rt->origin() || |
| 96 | fHWPathMatrixState.fViewMatrix != vm || |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 97 | fHWPathMatrixState.fRTSize != viewportSize) { |
| 98 | // rescale the coords from skia's "device" coords to GL's normalized coords, |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 99 | // and perform a y-flip if required. |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 100 | SkMatrix m; |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 101 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 102 | m.setScale(SkIntToScalar(2) / rt->width(), SkIntToScalar(-2) / rt->height()); |
| 103 | m.postTranslate(-SK_Scalar1, SK_Scalar1); |
| 104 | } else { |
| 105 | m.setScale(SkIntToScalar(2) / rt->width(), SkIntToScalar(2) / rt->height()); |
| 106 | m.postTranslate(-SK_Scalar1, -SK_Scalar1); |
| 107 | } |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 108 | m.preConcat(vm); |
| 109 | |
| 110 | // GL wants a column-major 4x4. |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 111 | GrGLfloat mv[] = { |
| 112 | // col 0 |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 113 | SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 114 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 115 | 0, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 116 | SkScalarToFloat(m[SkMatrix::kMPersp0]), |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 117 | |
| 118 | // col 1 |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 119 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 120 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 121 | 0, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 122 | SkScalarToFloat(m[SkMatrix::kMPersp1]), |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 123 | |
| 124 | // col 2 |
| 125 | 0, 0, 0, 0, |
| 126 | |
| 127 | // col3 |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 128 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 129 | SkScalarToFloat(m[SkMatrix::kMTransY]), |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 130 | 0.0f, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 131 | SkScalarToFloat(m[SkMatrix::kMPersp2]) |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 132 | }; |
| 133 | GL_CALL(MatrixMode(GR_GL_PROJECTION)); |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 134 | GL_CALL(LoadMatrixf(mv)); |
| 135 | fHWPathMatrixState.fViewMatrix = vm; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 136 | fHWPathMatrixState.fRTSize = viewportSize; |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 137 | fHWPathMatrixState.fLastOrigin = rt->origin(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 138 | } |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 139 | } else if (fCurrentProgram->fOrigin != rt->origin() || |
| 140 | !fCurrentProgram->fViewMatrix.cheapEqualTo(vm) || |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 141 | fCurrentProgram->fViewportSize != viewportSize) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 142 | SkMatrix m; |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 143 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 144 | m.setAll( |
| 145 | SkIntToScalar(2) / viewportSize.fWidth, 0, -SK_Scalar1, |
| 146 | 0,-SkIntToScalar(2) / viewportSize.fHeight, SK_Scalar1, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 147 | 0, 0, SkMatrix::I()[8]); |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 148 | } else { |
| 149 | m.setAll( |
| 150 | SkIntToScalar(2) / viewportSize.fWidth, 0, -SK_Scalar1, |
| 151 | 0, SkIntToScalar(2) / viewportSize.fHeight,-SK_Scalar1, |
| 152 | 0, 0, SkMatrix::I()[8]); |
| 153 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 154 | m.setConcat(m, vm); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 155 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 156 | // ES doesn't allow you to pass true to the transpose param, |
| 157 | // so do our own transpose |
| 158 | GrGLfloat mt[] = { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 159 | SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 160 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
| 161 | SkScalarToFloat(m[SkMatrix::kMPersp0]), |
| 162 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 163 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
| 164 | SkScalarToFloat(m[SkMatrix::kMPersp1]), |
| 165 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 166 | SkScalarToFloat(m[SkMatrix::kMTransY]), |
| 167 | SkScalarToFloat(m[SkMatrix::kMPersp2]) |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 168 | }; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 169 | fCurrentProgram->fUniformManager.setMatrix3f( |
| 170 | fCurrentProgram->fUniformHandles.fViewMatrixUni, |
| 171 | mt); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 172 | fCurrentProgram->fViewMatrix = vm; |
| 173 | fCurrentProgram->fViewportSize = viewportSize; |
senorblanco@chromium.org | ed8659b | 2013-02-04 17:29:51 +0000 | [diff] [blame] | 174 | fCurrentProgram->fOrigin = rt->origin(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 175 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 178 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 180 | void GrGpuGL::flushColor(GrColor color) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 181 | const ProgramDesc& desc = fCurrentProgram->getDesc(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 182 | const GrDrawState& drawState = this->getDrawState(); |
| 183 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 184 | if (this->getVertexLayout() & GrDrawState::kColor_VertexLayoutBit) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 185 | // color will be specified per-vertex as an attribute |
| 186 | // invalidate the const vertex attrib color |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 187 | fHWConstAttribColor = GrColor_ILLEGAL; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 188 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 189 | switch (desc.fColorInput) { |
| 190 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 191 | if (fHWConstAttribColor != color) { |
bsalomon@google.com | 7534747 | 2012-09-17 17:23:21 +0000 | [diff] [blame] | 192 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 193 | GrGLfloat c[4]; |
| 194 | GrColorToRGBAFloat(color, c); |
| 195 | GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 196 | fHWConstAttribColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 197 | } |
| 198 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 199 | case ProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 200 | if (fCurrentProgram->fColor != color) { |
bsalomon@google.com | 7534747 | 2012-09-17 17:23:21 +0000 | [diff] [blame] | 201 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 202 | GrGLfloat c[4]; |
| 203 | GrColorToRGBAFloat(color, c); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 204 | GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniformHandles.fColorUni); |
| 205 | fCurrentProgram->fUniformManager.set4fv( |
| 206 | fCurrentProgram->fUniformHandles.fColorUni, |
| 207 | 0, 1, c); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 208 | fCurrentProgram->fColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 209 | } |
| 210 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 211 | case ProgramDesc::kSolidWhite_ColorInput: |
| 212 | case ProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 213 | break; |
| 214 | default: |
| 215 | GrCrash("Unknown color type."); |
| 216 | } |
| 217 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 218 | UniformHandle filterColorUni = fCurrentProgram->fUniformHandles.fColorFilterUni; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 219 | if (kInvalidUniformHandle != filterColorUni && |
| 220 | fCurrentProgram->fColorFilterColor != drawState.getColorFilterColor()) { |
bsalomon@google.com | 7534747 | 2012-09-17 17:23:21 +0000 | [diff] [blame] | 221 | GrGLfloat c[4]; |
| 222 | GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 223 | fCurrentProgram->fUniformManager.set4fv(filterColorUni, 0, 1, c); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 224 | fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor(); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 225 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 226 | } |
| 227 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 228 | void GrGpuGL::flushCoverage(GrColor coverage) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 229 | const ProgramDesc& desc = fCurrentProgram->getDesc(); |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 230 | // const GrDrawState& drawState = this->getDrawState(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 231 | |
| 232 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 233 | if (this->getVertexLayout() & GrDrawState::kCoverage_VertexLayoutBit) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 234 | // coverage will be specified per-vertex as an attribute |
| 235 | // invalidate the const vertex attrib coverage |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 236 | fHWConstAttribCoverage = GrColor_ILLEGAL; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 237 | } else { |
| 238 | switch (desc.fCoverageInput) { |
| 239 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 240 | if (fHWConstAttribCoverage != coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 241 | // OpenGL ES only supports the float varieties of |
| 242 | // glVertexAttrib |
bsalomon@google.com | 7534747 | 2012-09-17 17:23:21 +0000 | [diff] [blame] | 243 | GrGLfloat c[4]; |
| 244 | GrColorToRGBAFloat(coverage, c); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 245 | GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(), |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 246 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 247 | fHWConstAttribCoverage = coverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 248 | } |
| 249 | break; |
| 250 | case ProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 251 | if (fCurrentProgram->fCoverage != coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 252 | // OpenGL ES doesn't support unsigned byte varieties of |
| 253 | // glUniform |
bsalomon@google.com | 7534747 | 2012-09-17 17:23:21 +0000 | [diff] [blame] | 254 | GrGLfloat c[4]; |
| 255 | GrColorToRGBAFloat(coverage, c); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 256 | GrAssert(kInvalidUniformHandle != |
| 257 | fCurrentProgram->fUniformHandles.fCoverageUni); |
| 258 | fCurrentProgram->fUniformManager.set4fv( |
| 259 | fCurrentProgram->fUniformHandles.fCoverageUni, |
| 260 | 0, 1, c); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 261 | fCurrentProgram->fCoverage = coverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | case ProgramDesc::kSolidWhite_ColorInput: |
| 265 | case ProgramDesc::kTransBlack_ColorInput: |
| 266 | break; |
| 267 | default: |
| 268 | GrCrash("Unknown coverage type."); |
| 269 | } |
| 270 | } |
| 271 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 272 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 273 | bool GrGpuGL::flushGraphicsState(DrawType type) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 274 | const GrDrawState& drawState = this->getDrawState(); |
| 275 | |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 276 | // GrGpu::setupClipAndFlushState should have already checked this |
| 277 | // and bailed if not true. |
| 278 | GrAssert(NULL != drawState.getRenderTarget()); |
| 279 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 280 | if (kStencilPath_DrawType != type) { |
| 281 | this->flushMiscFixedFunctionState(); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 282 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 283 | GrBlendCoeff srcCoeff; |
| 284 | GrBlendCoeff dstCoeff; |
| 285 | BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 286 | if (kSkipDraw_BlendOptFlag & blendOpts) { |
| 287 | return false; |
| 288 | } |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 289 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 290 | const GrEffectStage* stages[GrDrawState::kNumStages]; |
| 291 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
| 292 | stages[i] = drawState.isStageEnabled(i) ? &drawState.getStage(i) : NULL; |
| 293 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 294 | GrGLProgram::Desc desc; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 295 | this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, &desc); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 296 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 297 | fCurrentProgram.reset(fProgramCache->getProgram(desc, stages)); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 298 | if (NULL == fCurrentProgram.get()) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 299 | GrAssert(!"Failed to create program!"); |
| 300 | return false; |
| 301 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 302 | fCurrentProgram.get()->ref(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 303 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 304 | if (fHWProgramID != fCurrentProgram->fProgramID) { |
| 305 | GL_CALL(UseProgram(fCurrentProgram->fProgramID)); |
| 306 | fHWProgramID = fCurrentProgram->fProgramID; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 307 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 308 | fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 309 | this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 310 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 311 | GrColor color; |
| 312 | GrColor coverage; |
| 313 | if (blendOpts & kEmitTransBlack_BlendOptFlag) { |
| 314 | color = 0; |
| 315 | coverage = 0; |
| 316 | } else if (blendOpts & kEmitCoverage_BlendOptFlag) { |
| 317 | color = 0xffffffff; |
| 318 | coverage = drawState.getCoverage(); |
| 319 | } else { |
| 320 | color = drawState.getColor(); |
| 321 | coverage = drawState.getCoverage(); |
| 322 | } |
| 323 | this->flushColor(color); |
| 324 | this->flushCoverage(coverage); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 325 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 326 | fCurrentProgram->setData(this); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 327 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 328 | this->flushStencil(type); |
| 329 | this->flushViewMatrix(type); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 330 | this->flushScissor(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 331 | this->flushAAState(type); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 332 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 333 | GrIRect* devRect = NULL; |
| 334 | GrIRect devClipBounds; |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 335 | if (drawState.isClipState()) { |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 336 | this->getClip()->getConservativeBounds(drawState.getRenderTarget(), &devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 337 | devRect = &devClipBounds; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 338 | } |
| 339 | // This must come after textures are flushed because a texture may need |
| 340 | // to be msaa-resolved (which will modify bound FBO state). |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 341 | this->flushRenderTarget(devRect); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 342 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 346 | #if GR_TEXT_SCALAR_IS_USHORT |
| 347 | #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT |
| 348 | #define TEXT_COORDS_ARE_NORMALIZED 1 |
| 349 | #elif GR_TEXT_SCALAR_IS_FLOAT |
| 350 | #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT |
| 351 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 352 | #elif GR_TEXT_SCALAR_IS_FIXED |
| 353 | #define TEXT_COORDS_GL_TYPE GR_GL_FIXED |
| 354 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 355 | #else |
| 356 | #error "unknown GR_TEXT_SCALAR type" |
| 357 | #endif |
| 358 | |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 359 | void GrGpuGL::setupGeometry(const DrawInfo& info, int* startIndexOffset) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 360 | |
| 361 | int newColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 362 | int newCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 363 | int newTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 364 | int newEdgeOffset; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 366 | GrVertexLayout currLayout = this->getVertexLayout(); |
| 367 | |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 368 | GrGLsizei newStride = GrDrawState::VertexSizeAndOffsetsByIdx(currLayout, |
| 369 | newTexCoordOffsets, |
| 370 | &newColorOffset, |
| 371 | &newCoverageOffset, |
| 372 | &newEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 373 | int oldColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 374 | int oldCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 375 | int oldTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 376 | int oldEdgeOffset; |
| 377 | |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 378 | GrGLsizei oldStride = GrDrawState::VertexSizeAndOffsetsByIdx(fHWGeometryState.fVertexLayout, |
| 379 | oldTexCoordOffsets, |
| 380 | &oldColorOffset, |
| 381 | &oldCoverageOffset, |
| 382 | &oldEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 383 | |
| 384 | int extraVertexOffset; |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 385 | this->setBuffers(info.isIndexed(), &extraVertexOffset, startIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 386 | |
| 387 | GrGLenum scalarType; |
| 388 | bool texCoordNorm; |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 389 | if (currLayout & GrDrawState::kTextFormat_VertexLayoutBit) { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 390 | scalarType = TEXT_COORDS_GL_TYPE; |
| 391 | texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 392 | } else { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 393 | scalarType = GR_GL_FLOAT; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 394 | texCoordNorm = false; |
| 395 | } |
| 396 | |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 397 | size_t vertexOffset = (info.startVertex() + extraVertexOffset) * newStride; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 398 | |
| 399 | // all the Pointers must be set if any of these are true |
| 400 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 401 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 402 | newStride != oldStride; |
| 403 | |
| 404 | // position and tex coord offsets change if above conditions are true |
| 405 | // or the type/normalization changed based on text vs nontext type coords. |
| 406 | bool posAndTexChange = allOffsetsChange || |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 407 | (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) && |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 408 | (GrDrawState::kTextFormat_VertexLayoutBit & |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 409 | (fHWGeometryState.fVertexLayout ^ currLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 410 | |
| 411 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 412 | int idx = GrGLProgram::PositionAttributeIdx(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 413 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 414 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 415 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 416 | } |
| 417 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 418 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 419 | if (newTexCoordOffsets[t] > 0) { |
| 420 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 421 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 422 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 423 | GL_CALL(EnableVertexAttribArray(idx)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 424 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 425 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 426 | } else if (posAndTexChange || |
| 427 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 428 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 429 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 430 | } |
| 431 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 432 | GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | if (newColorOffset > 0) { |
| 437 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 438 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 439 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 440 | GL_CALL(EnableVertexAttribArray(idx)); |
| 441 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 442 | true, newStride, colorOffset)); |
| 443 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 444 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 445 | true, newStride, colorOffset)); |
| 446 | } |
| 447 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 448 | GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 449 | } |
| 450 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 451 | if (newCoverageOffset > 0) { |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 452 | GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 453 | int idx = GrGLProgram::CoverageAttributeIdx(); |
| 454 | if (oldCoverageOffset <= 0) { |
| 455 | GL_CALL(EnableVertexAttribArray(idx)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 456 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 457 | true, newStride, coverageOffset)); |
| 458 | } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 459 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 460 | true, newStride, coverageOffset)); |
| 461 | } |
| 462 | } else if (oldCoverageOffset > 0) { |
| 463 | GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx())); |
| 464 | } |
| 465 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 466 | if (newEdgeOffset > 0) { |
| 467 | GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset); |
| 468 | int idx = GrGLProgram::EdgeAttributeIdx(); |
| 469 | if (oldEdgeOffset <= 0) { |
| 470 | GL_CALL(EnableVertexAttribArray(idx)); |
| 471 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 472 | false, newStride, edgeOffset)); |
| 473 | } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) { |
| 474 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 475 | false, newStride, edgeOffset)); |
| 476 | } |
| 477 | } else if (oldEdgeOffset > 0) { |
| 478 | GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx())); |
| 479 | } |
| 480 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 481 | fHWGeometryState.fVertexLayout = currLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 482 | fHWGeometryState.fArrayPtrsDirty = false; |
| 483 | } |
| 484 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 485 | void GrGpuGL::buildProgram(bool isPoints, |
bsalomon@google.com | 4920939 | 2012-06-05 15:13:46 +0000 | [diff] [blame] | 486 | BlendOptFlags blendOpts, |
| 487 | GrBlendCoeff dstCoeff, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 488 | ProgramDesc* desc) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 489 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 490 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 491 | // This should already have been caught |
| 492 | GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts)); |
| 493 | |
| 494 | bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 495 | |
| 496 | bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag | |
| 497 | kEmitCoverage_BlendOptFlag)); |
| 498 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 499 | // The descriptor is used as a cache key. Thus when a field of the |
| 500 | // descriptor will not affect program generation (because of the vertex |
| 501 | // layout in use or other descriptor field settings) it should be set |
| 502 | // to a canonical value to avoid duplicate programs with different keys. |
| 503 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 504 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 505 | desc->fVertexLayout = this->getVertexLayout(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 506 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 507 | desc->fEmitsPointSize = isPoints; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 508 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 509 | bool requiresAttributeColors = !skipColor && |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 510 | SkToBool(desc->fVertexLayout & GrDrawState::kColor_VertexLayoutBit); |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 511 | bool requiresAttributeCoverage = !skipCoverage && |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 512 | SkToBool(desc->fVertexLayout & GrDrawState::kCoverage_VertexLayoutBit); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 513 | |
| 514 | // fColorInput/fCoverageInput records how colors are specified for the. |
| 515 | // program. So we strip the bits from the layout to avoid false negatives |
| 516 | // when searching for an existing program in the cache. |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 517 | desc->fVertexLayout &= ~(GrDrawState::kColor_VertexLayoutBit | GrDrawState::kCoverage_VertexLayoutBit); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 518 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 519 | desc->fColorFilterXfermode = skipColor ? |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 520 | SkXfermode::kDst_Mode : |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 521 | drawState.getColorFilterMode(); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 522 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 523 | // no reason to do edge aa or look at per-vertex coverage if coverage is |
| 524 | // ignored |
| 525 | if (skipCoverage) { |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 526 | desc->fVertexLayout &= ~(GrDrawState::kEdge_VertexLayoutBit | GrDrawState::kCoverage_VertexLayoutBit); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 530 | bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) || |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 531 | (!requiresAttributeColors && 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 532 | if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 533 | desc->fColorInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 534 | } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 535 | desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 536 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 537 | desc->fColorInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 538 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 539 | desc->fColorInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 540 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 541 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 542 | bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 543 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 544 | if (skipCoverage) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 545 | desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 546 | } else if (covIsSolidWhite) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 547 | desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 548 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 549 | desc->fCoverageInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 550 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 551 | desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 552 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 553 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 554 | int lastEnabledStage = -1; |
| 555 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 556 | if (!skipCoverage && (desc->fVertexLayout &GrDrawState::kEdge_VertexLayoutBit)) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 557 | desc->fVertexEdgeType = drawState.getVertexEdgeType(); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 558 | desc->fDiscardIfOutsideEdge = drawState.getStencil().doesWrite(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 559 | } else { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 560 | // Use canonical values when edge-aa is not enabled to avoid program cache misses. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 561 | desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 562 | desc->fDiscardIfOutsideEdge = false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 563 | } |
| 564 | |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 565 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 566 | |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 567 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage; |
| 568 | if (!skip && drawState.isStageEnabled(s)) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 569 | lastEnabledStage = s; |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 570 | const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 571 | const GrBackendEffectFactory& factory = effect->getFactory(); |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 572 | desc->fEffectKeys[s] = factory.glEffectKey(drawState.getStage(s), this->glCaps()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 573 | } else { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 574 | desc->fEffectKeys[s] = 0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 577 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 578 | desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 579 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 580 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 581 | // other than pass through values from the VS to the FS anyway). |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 582 | #if 0 && GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 583 | desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 584 | #endif |
| 585 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 586 | // We want to avoid generating programs with different "first cov stage" values when they would |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 587 | // compute the same result. We set field in the desc to kNumStages when either there are no |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 588 | // coverage stages or the distinction between coverage and color is immaterial. |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 589 | int firstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 590 | desc->fFirstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 591 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 592 | if (hasCoverage) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 593 | firstCoverageStage = drawState.getFirstCoverageStage(); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // other coverage inputs |
| 597 | if (!hasCoverage) { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 598 | hasCoverage = requiresAttributeCoverage || |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 599 | (desc->fVertexLayout & GrDrawState::kEdge_VertexLayoutBit); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (hasCoverage) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 603 | // color filter is applied between color/coverage computation |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 604 | if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) { |
| 605 | desc->fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 606 | } |
| 607 | |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 608 | if (this->getCaps().dualSourceBlendingSupport() && |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 609 | !(blendOpts & (kEmitCoverage_BlendOptFlag | kCoverageAsAlpha_BlendOptFlag))) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 610 | if (kZero_GrBlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 611 | // write the coverage value to second color |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 612 | desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
| 613 | desc->fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 614 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 615 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 616 | desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
| 617 | desc->fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 618 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 619 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 620 | desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
| 621 | desc->fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 625 | } |