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