epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #include "GrGpuGL.h" |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 11 | #include "GrGLStencilBuffer.h" |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 12 | #include "GrTypes.h" |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 13 | #include "SkTemplates.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 15 | static const GrGLuint GR_MAX_GLUINT = ~0; |
| 16 | static const GrGLint GR_INVAL_GLINT = ~0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 18 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 19 | #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 21 | // we use a spare texture unit to avoid |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 22 | // mucking with the state of any of the stages. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 23 | static const int SPARE_TEX_UNIT = GrDrawState::kNumStages; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 24 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | #define SKIP_CACHE_CHECK true |
| 26 | |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 27 | #if GR_GL_CHECK_ALLOC_WITH_GET_ERROR |
| 28 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface) |
| 29 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call) |
| 30 | #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface) |
| 31 | #else |
| 32 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) |
| 33 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call) |
| 34 | #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR |
| 35 | #endif |
| 36 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 37 | static const GrGLenum gXfermodeCoeff2Blend[] = { |
| 38 | GR_GL_ZERO, |
| 39 | GR_GL_ONE, |
| 40 | GR_GL_SRC_COLOR, |
| 41 | GR_GL_ONE_MINUS_SRC_COLOR, |
| 42 | GR_GL_DST_COLOR, |
| 43 | GR_GL_ONE_MINUS_DST_COLOR, |
| 44 | GR_GL_SRC_ALPHA, |
| 45 | GR_GL_ONE_MINUS_SRC_ALPHA, |
| 46 | GR_GL_DST_ALPHA, |
| 47 | GR_GL_ONE_MINUS_DST_ALPHA, |
| 48 | GR_GL_CONSTANT_COLOR, |
| 49 | GR_GL_ONE_MINUS_CONSTANT_COLOR, |
| 50 | GR_GL_CONSTANT_ALPHA, |
| 51 | GR_GL_ONE_MINUS_CONSTANT_ALPHA, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 52 | |
| 53 | // extended blend coeffs |
| 54 | GR_GL_SRC1_COLOR, |
| 55 | GR_GL_ONE_MINUS_SRC1_COLOR, |
| 56 | GR_GL_SRC1_ALPHA, |
| 57 | GR_GL_ONE_MINUS_SRC1_ALPHA, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 60 | bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 61 | static const bool gCoeffReferencesBlendConst[] = { |
| 62 | false, |
| 63 | false, |
| 64 | false, |
| 65 | false, |
| 66 | false, |
| 67 | false, |
| 68 | false, |
| 69 | false, |
| 70 | false, |
| 71 | false, |
| 72 | true, |
| 73 | true, |
| 74 | true, |
| 75 | true, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 76 | |
| 77 | // extended blend coeffs |
| 78 | false, |
| 79 | false, |
| 80 | false, |
| 81 | false, |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 82 | }; |
| 83 | return gCoeffReferencesBlendConst[coeff]; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 84 | GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
| 85 | |
| 86 | GR_STATIC_ASSERT(0 == kZero_BlendCoeff); |
| 87 | GR_STATIC_ASSERT(1 == kOne_BlendCoeff); |
| 88 | GR_STATIC_ASSERT(2 == kSC_BlendCoeff); |
| 89 | GR_STATIC_ASSERT(3 == kISC_BlendCoeff); |
| 90 | GR_STATIC_ASSERT(4 == kDC_BlendCoeff); |
| 91 | GR_STATIC_ASSERT(5 == kIDC_BlendCoeff); |
| 92 | GR_STATIC_ASSERT(6 == kSA_BlendCoeff); |
| 93 | GR_STATIC_ASSERT(7 == kISA_BlendCoeff); |
| 94 | GR_STATIC_ASSERT(8 == kDA_BlendCoeff); |
| 95 | GR_STATIC_ASSERT(9 == kIDA_BlendCoeff); |
| 96 | GR_STATIC_ASSERT(10 == kConstC_BlendCoeff); |
| 97 | GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff); |
| 98 | GR_STATIC_ASSERT(12 == kConstA_BlendCoeff); |
| 99 | GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff); |
| 100 | |
| 101 | GR_STATIC_ASSERT(14 == kS2C_BlendCoeff); |
| 102 | GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff); |
| 103 | GR_STATIC_ASSERT(16 == kS2A_BlendCoeff); |
| 104 | GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff); |
| 105 | |
| 106 | // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope |
| 107 | GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend)); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 112 | void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture, |
| 113 | GrSamplerState::SampleMode mode, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 114 | GrMatrix* matrix) { |
| 115 | GrAssert(NULL != texture); |
| 116 | GrAssert(NULL != matrix); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 117 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 118 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 119 | GrMatrix invY; |
| 120 | invY.setAll(GR_Scalar1, 0, 0, |
| 121 | 0, -GR_Scalar1, GR_Scalar1, |
| 122 | 0, 0, GrMatrix::I()[8]); |
| 123 | matrix->postConcat(invY); |
| 124 | } else { |
| 125 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
| 126 | } |
| 127 | } |
| 128 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 129 | bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 130 | const GrSamplerState& sampler) { |
| 131 | GrAssert(NULL != texture); |
| 132 | if (!sampler.getMatrix().isIdentity()) { |
| 133 | return false; |
| 134 | } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 135 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 136 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 137 | return false; |
| 138 | } else { |
| 139 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 146 | static bool gPrintStartupSpew; |
| 147 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 148 | static bool fbo_test(const GrGLInterface* gl, int w, int h) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 149 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 150 | GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 151 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 152 | GrGLuint testFBO; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 153 | GR_GL_CALL(gl, GenFramebuffers(1, &testFBO)); |
| 154 | GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 155 | GrGLuint testRTTex; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 156 | GR_GL_CALL(gl, GenTextures(1, &testRTTex)); |
| 157 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex)); |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 158 | // some implementations require texture to be mip-map complete before |
| 159 | // FBO with level 0 bound as color attachment will be framebuffer complete. |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 160 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 161 | GR_GL_TEXTURE_MIN_FILTER, |
| 162 | GR_GL_NEAREST)); |
| 163 | GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h, |
| 164 | 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL)); |
| 165 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 166 | GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 167 | GR_GL_COLOR_ATTACHMENT0, |
| 168 | GR_GL_TEXTURE_2D, testRTTex, 0)); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 169 | GrGLenum status; |
| 170 | GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 171 | GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO)); |
| 172 | GR_GL_CALL(gl, DeleteTextures(1, &testRTTex)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 174 | return status == GR_GL_FRAMEBUFFER_COMPLETE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | } |
| 176 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 177 | GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) { |
tomhudson@google.com | 747bf29 | 2011-06-14 18:16:52 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 179 | fPrintedCaps = false; |
bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 181 | gl->ref(); |
| 182 | fGL = gl; |
| 183 | fGLBinding = glBinding; |
| 184 | switch (glBinding) { |
| 185 | case kDesktop_GrGLBinding: |
| 186 | GrAssert(gl->supportsDesktop()); |
| 187 | break; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 188 | case kES2_GrGLBinding: |
| 189 | GrAssert(gl->supportsES2()); |
| 190 | break; |
| 191 | default: |
| 192 | GrCrash("Expect exactly one valid GL binding bit to be in use."); |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 193 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 195 | GrGLClearErr(fGL); |
| 196 | |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 197 | const GrGLubyte* ext; |
| 198 | GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 199 | if (gPrintStartupSpew) { |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 200 | const GrGLubyte* vendor; |
| 201 | const GrGLubyte* renderer; |
| 202 | const GrGLubyte* version; |
| 203 | GL_CALL_RET(vendor, GetString(GR_GL_VENDOR)); |
| 204 | GL_CALL_RET(renderer, GetString(GR_GL_RENDERER)); |
| 205 | GL_CALL_RET(version, GetString(GR_GL_VERSION)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 206 | GrPrintf("------------------------- create GrGpuGL %p --------------\n", |
| 207 | this); |
| 208 | GrPrintf("------ VENDOR %s\n", vendor); |
| 209 | GrPrintf("------ RENDERER %s\n", renderer); |
| 210 | GrPrintf("------ VERSION %s\n", version); |
| 211 | GrPrintf("------ EXTENSIONS\n %s \n", ext); |
| 212 | } |
| 213 | |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 214 | fGLVersion = GrGLGetVersion(gl); |
| 215 | GrAssert(0 != fGLVersion); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 216 | fExtensionString = (const char*) ext; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | |
bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 218 | this->resetDirtyFlags(); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 219 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 220 | this->initCaps(); |
tomhudson@google.com | 747bf29 | 2011-06-14 18:16:52 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 222 | fLastSuccessfulStencilFmtIdx = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | GrGpuGL::~GrGpuGL() { |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 226 | // This must be called by before the GrDrawTarget destructor |
| 227 | this->releaseGeometry(); |
bsalomon@google.com | 15b11df | 2011-09-16 21:18:29 +0000 | [diff] [blame] | 228 | // This subclass must do this before the base class destructor runs |
| 229 | // since we will unref the GrGLInterface. |
| 230 | this->releaseResources(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 231 | fGL->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | } |
| 233 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 234 | /////////////////////////////////////////////////////////////////////////////// |
| 235 | |
| 236 | static const GrGLuint kUnknownBitCount = ~0; |
| 237 | |
| 238 | void GrGpuGL::initCaps() { |
| 239 | GrGLint maxTextureUnits; |
| 240 | // check FS and fixed-function texture unit limits |
| 241 | // we only use textures in the fragment stage currently. |
| 242 | // checks are > to make sure we have a spare unit. |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 243 | GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 244 | GrAssert(maxTextureUnits > GrDrawState::kNumStages); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 245 | if (kES2_GrGLBinding != this->glBinding()) { |
| 246 | GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 247 | GrAssert(maxTextureUnits > GrDrawState::kNumStages); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 248 | } |
| 249 | if (kES2_GrGLBinding == this->glBinding()) { |
| 250 | GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS, |
| 251 | &fGLCaps.fMaxFragmentUniformVectors); |
| 252 | } else if (kDesktop_GrGLBinding != this->glBinding()) { |
| 253 | GrGLint max; |
| 254 | GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max); |
| 255 | fGLCaps.fMaxFragmentUniformVectors = max / 4; |
| 256 | } else { |
| 257 | fGLCaps.fMaxFragmentUniformVectors = 16; |
| 258 | } |
| 259 | |
| 260 | GrGLint numFormats; |
| 261 | GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats); |
| 262 | SkAutoSTMalloc<10, GrGLint> formats(numFormats); |
| 263 | GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats); |
| 264 | for (int i = 0; i < numFormats; ++i) { |
| 265 | if (formats[i] == GR_GL_PALETTE8_RGBA8) { |
| 266 | fCaps.f8BitPaletteSupport = true; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 272 | // we could also look for GL_ATI_separate_stencil extension or |
| 273 | // GL_EXT_stencil_two_side but they use different function signatures |
| 274 | // than GL2.0+ (and than each other). |
| 275 | fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0)); |
| 276 | // supported on GL 1.4 and higher or by extension |
| 277 | fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) || |
| 278 | this->hasExtension("GL_EXT_stencil_wrap"); |
| 279 | } else { |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 280 | // ES 2 has two sided stencil and stencil wrap |
| 281 | fCaps.fTwoSidedStencilSupport = true; |
| 282 | fCaps.fStencilWrapOpsSupport = true; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 286 | fGLCaps.fRGBA8RenderbufferSupport = true; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 287 | } else { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 288 | fGLCaps.fRGBA8RenderbufferSupport = |
| 289 | this->hasExtension("GL_OES_rgb8_rgba8") || |
| 290 | this->hasExtension("GL_ARM_rgba8"); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 294 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 295 | fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) || |
| 296 | this->hasExtension("GL_EXT_bgra"); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 297 | } else { |
| 298 | bool hasBGRAExt = false; |
| 299 | if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 300 | fGLCaps.fBGRAFormatSupport = true; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 301 | } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 302 | fGLCaps.fBGRAFormatSupport = true; |
| 303 | fGLCaps.fBGRAIsInternalFormat = true; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 304 | } |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 305 | GrAssert(fGLCaps.fBGRAFormatSupport || |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 306 | kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 310 | fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) || |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 311 | this->hasExtension("GL_ARB_texture_swizzle"); |
| 312 | } else { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 313 | fGLCaps.fTextureSwizzleSupport = false; |
| 314 | } |
| 315 | |
| 316 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 317 | fGLCaps.fUnpackRowLengthSupport = true; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 318 | fGLCaps.fUnpackFlipYSupport = false; |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 319 | fGLCaps.fPackRowLengthSupport = true; |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 320 | fGLCaps.fPackFlipYSupport = false; |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 321 | } else { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 322 | fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage"); |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 323 | fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy"); |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 324 | // no extension for pack row length |
| 325 | fGLCaps.fPackRowLengthSupport = false; |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 326 | fGLCaps.fPackFlipYSupport = |
| 327 | this->hasExtension("GL_ANGLE_pack_reverse_row_order"); |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 331 | fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO |
| 332 | // extension includes glMapBuffer. |
| 333 | } else { |
| 334 | fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer"); |
| 335 | } |
| 336 | |
| 337 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 338 | if (fGLVersion >= GR_GL_VER(2,0) || |
| 339 | this->hasExtension("GL_ARB_texture_non_power_of_two")) { |
| 340 | fCaps.fNPOTTextureTileSupport = true; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 341 | } else { |
| 342 | fCaps.fNPOTTextureTileSupport = false; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 343 | } |
| 344 | } else { |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 345 | // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 346 | fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot"); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 347 | } |
| 348 | |
bsalomon@google.com | 07dd2bf | 2011-12-09 19:40:36 +0000 | [diff] [blame] | 349 | fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) && |
| 350 | this->hasExtension("GL_ANGLE_texture_usage"); |
| 351 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 352 | // Tex storage is in desktop 4.2 and can be an extension to desktop or ES. |
| 353 | fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() && |
| 354 | fGLVersion >= GR_GL_VER(4,2)) || |
| 355 | this->hasExtension("GL_ARB_texture_storage") || |
| 356 | this->hasExtension("GL_EXT_texture_storage"); |
| 357 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 358 | fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding()); |
| 359 | |
| 360 | //////////////////////////////////////////////////////////////////////////// |
| 361 | // Experiments to determine limitations that can't be queried. |
| 362 | // TODO: Make these a preprocess that generate some compile time constants. |
| 363 | // TODO: probe once at startup, rather than once per context creation. |
| 364 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 365 | GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize); |
| 366 | GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize); |
| 367 | // Our render targets are always created with textures as the color |
| 368 | // attachment, hence this min: |
| 369 | fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize); |
| 370 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 371 | this->initFSAASupport(); |
| 372 | this->initStencilFormats(); |
| 373 | } |
| 374 | |
| 375 | void GrGpuGL::initFSAASupport() { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 376 | |
| 377 | fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO; |
| 378 | if (kDesktop_GrGLBinding != this->glBinding()) { |
| 379 | if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) { |
| 380 | // chrome's extension is equivalent to the EXT msaa |
| 381 | // and fbo_blit extensions. |
| 382 | fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO; |
| 383 | } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) { |
| 384 | fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO; |
| 385 | } |
| 386 | } else { |
| 387 | if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) { |
| 388 | fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO; |
| 389 | } else if (this->hasExtension("GL_EXT_framebuffer_multisample") && |
| 390 | this->hasExtension("GL_EXT_framebuffer_blit")) { |
| 391 | fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO; |
| 392 | } |
| 393 | } |
| 394 | |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 395 | fCaps.fFSAASupport = GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void GrGpuGL::initStencilFormats() { |
| 399 | |
| 400 | // Build up list of legal stencil formats (though perhaps not supported on |
| 401 | // the particular gpu/driver) from most preferred to least. |
| 402 | |
| 403 | // these consts are in order of most preferred to least preferred |
| 404 | // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8 |
| 405 | static const GrGLStencilBuffer::Format |
| 406 | // internal Format stencil bits total bits packed? |
| 407 | gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false}, |
| 408 | gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false}, |
| 409 | gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true }, |
| 410 | gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false}, |
| 411 | gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false}, |
| 412 | gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true }; |
| 413 | |
| 414 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 415 | bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) || |
| 416 | this->hasExtension("GL_EXT_packed_depth_stencil") || |
| 417 | this->hasExtension("GL_ARB_framebuffer_object"); |
| 418 | |
| 419 | // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we |
| 420 | // require FBO support we can expect these are legal formats and don't |
| 421 | // check. These also all support the unsized GL_STENCIL_INDEX. |
| 422 | fGLCaps.fStencilFormats.push_back() = gS8; |
| 423 | fGLCaps.fStencilFormats.push_back() = gS16; |
| 424 | if (supportsPackedDS) { |
| 425 | fGLCaps.fStencilFormats.push_back() = gD24S8; |
| 426 | } |
| 427 | fGLCaps.fStencilFormats.push_back() = gS4; |
| 428 | if (supportsPackedDS) { |
| 429 | fGLCaps.fStencilFormats.push_back() = gDS; |
| 430 | } |
| 431 | } else { |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 432 | // ES2 has STENCIL_INDEX8 without extensions but requires extensions |
| 433 | // for other formats. |
| 434 | // ES doesn't support using the unsized format. |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 435 | |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 436 | fGLCaps.fStencilFormats.push_back() = gS8; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 437 | //fStencilFormats.push_back() = gS16; |
| 438 | if (this->hasExtension("GL_OES_packed_depth_stencil")) { |
| 439 | fGLCaps.fStencilFormats.push_back() = gD24S8; |
| 440 | } |
| 441 | if (this->hasExtension("GL_OES_stencil4")) { |
| 442 | fGLCaps.fStencilFormats.push_back() = gS4; |
| 443 | } |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 447 | GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const { |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 448 | if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) { |
| 449 | return GrPixelConfigSwapRAndB(config); |
| 450 | } else { |
| 451 | return config; |
| 452 | } |
| 453 | } |
| 454 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 455 | GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const { |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 456 | if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) { |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 457 | return GrPixelConfigSwapRAndB(config); |
| 458 | } else { |
| 459 | return config; |
| 460 | } |
| 461 | } |
| 462 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 463 | bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const { |
| 464 | return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL); |
| 465 | } |
| 466 | |
bsalomon@google.com | 1bf1c21 | 2011-11-05 12:18:58 +0000 | [diff] [blame] | 467 | void GrGpuGL::onResetContext() { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 468 | if (gPrintStartupSpew && !fPrintedCaps) { |
| 469 | fPrintedCaps = true; |
| 470 | this->getCaps().print(); |
| 471 | fGLCaps.print(); |
| 472 | } |
| 473 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 474 | // We detect cases when blending is effectively off |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 475 | fHWBlendDisabled = false; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 476 | GL_CALL(Enable(GR_GL_BLEND)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 477 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 478 | // we don't use the zb at all |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 479 | GL_CALL(Disable(GR_GL_DEPTH_TEST)); |
| 480 | GL_CALL(DepthMask(GR_GL_FALSE)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 481 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 482 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
| 483 | GL_CALL(FrontFace(GR_GL_CCW)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 484 | fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 486 | GL_CALL(Disable(GR_GL_DITHER)); |
| 487 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 488 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
| 489 | GL_CALL(Disable(GR_GL_POINT_SMOOTH)); |
| 490 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 491 | fHWAAState.fMSAAEnabled = false; |
| 492 | fHWAAState.fSmoothLineEnabled = false; |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 493 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 494 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 495 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 496 | fHWDrawState.resetStateFlags(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 497 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 498 | // we only ever use lines in hairline mode |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 499 | GL_CALL(LineWidth(1)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 500 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 501 | // invalid |
| 502 | fActiveTextureUnitIdx = -1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 503 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 504 | // illegal values |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 505 | fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 506 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 507 | fHWDrawState.setBlendConstant(0x00000000); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 508 | GL_CALL(BlendColor(0,0,0,0)); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 509 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 510 | fHWDrawState.setColor(GrColor_ILLEGAL); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 511 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 512 | fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix()); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 513 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 514 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 515 | fHWDrawState.setTexture(s, NULL); |
| 516 | fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax, |
| 517 | -GR_ScalarMax, |
| 518 | true); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 519 | *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 520 | fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 521 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 522 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 523 | fHWBounds.fScissorRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 524 | fHWBounds.fScissorEnabled = false; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 525 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 526 | fHWBounds.fViewportRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 527 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 528 | fHWDrawState.stencil()->invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 529 | fHWStencilClip = false; |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 530 | fClipInStencil = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 531 | |
| 532 | fHWGeometryState.fIndexBuffer = NULL; |
| 533 | fHWGeometryState.fVertexBuffer = NULL; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 534 | |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 535 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 536 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 537 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 538 | fHWDrawState.setRenderTarget(NULL); |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 539 | |
| 540 | // we assume these values |
| 541 | if (this->glCaps().fUnpackRowLengthSupport) { |
| 542 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 543 | } |
| 544 | if (this->glCaps().fPackRowLengthSupport) { |
| 545 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 546 | } |
| 547 | if (this->glCaps().fUnpackFlipYSupport) { |
| 548 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 549 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 550 | if (this->glCaps().fPackFlipYSupport) { |
| 551 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE)); |
| 552 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 553 | } |
| 554 | |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 555 | GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 556 | GrGLTexture::Desc glTexDesc; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 557 | if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 558 | return NULL; |
| 559 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 560 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 561 | glTexDesc.fWidth = desc.fWidth; |
| 562 | glTexDesc.fHeight = desc.fHeight; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 563 | glTexDesc.fConfig = desc.fConfig; |
| 564 | glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle); |
| 565 | glTexDesc.fOwnsID = false; |
| 566 | glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation; |
| 567 | |
| 568 | GrGLTexture* texture = NULL; |
| 569 | if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) { |
| 570 | GrGLRenderTarget::Desc glRTDesc; |
| 571 | glRTDesc.fRTFBOID = 0; |
| 572 | glRTDesc.fTexFBOID = 0; |
| 573 | glRTDesc.fMSColorRenderbufferID = 0; |
| 574 | glRTDesc.fOwnIDs = true; |
| 575 | glRTDesc.fConfig = desc.fConfig; |
| 576 | glRTDesc.fSampleCnt = desc.fSampleCnt; |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 577 | if (!this->createRenderTargetObjects(glTexDesc.fWidth, |
| 578 | glTexDesc.fHeight, |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 579 | glTexDesc.fTextureID, |
| 580 | &glRTDesc)) { |
| 581 | return NULL; |
| 582 | } |
| 583 | texture = new GrGLTexture(this, glTexDesc, glRTDesc); |
| 584 | } else { |
| 585 | texture = new GrGLTexture(this, glTexDesc); |
| 586 | } |
| 587 | if (NULL == texture) { |
| 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | this->setSpareTextureUnit(); |
| 592 | return texture; |
| 593 | } |
| 594 | |
| 595 | GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) { |
| 596 | GrGLRenderTarget::Desc glDesc; |
| 597 | glDesc.fConfig = desc.fConfig; |
| 598 | glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle); |
| 599 | glDesc.fMSColorRenderbufferID = 0; |
| 600 | glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
| 601 | glDesc.fSampleCnt = desc.fSampleCnt; |
| 602 | glDesc.fOwnIDs = false; |
| 603 | GrGLIRect viewport; |
| 604 | viewport.fLeft = 0; |
| 605 | viewport.fBottom = 0; |
| 606 | viewport.fWidth = desc.fWidth; |
| 607 | viewport.fHeight = desc.fHeight; |
| 608 | |
| 609 | GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport); |
| 610 | if (desc.fStencilBits) { |
| 611 | GrGLStencilBuffer::Format format; |
| 612 | format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat; |
| 613 | format.fPacked = false; |
| 614 | format.fStencilBits = desc.fStencilBits; |
| 615 | format.fTotalBits = desc.fStencilBits; |
| 616 | GrGLStencilBuffer* sb = new GrGLStencilBuffer(this, |
| 617 | 0, |
| 618 | desc.fWidth, |
| 619 | desc.fHeight, |
| 620 | desc.fSampleCnt, |
| 621 | format); |
| 622 | tgt->setStencilBuffer(sb); |
| 623 | sb->unref(); |
| 624 | } |
| 625 | return tgt; |
| 626 | } |
| 627 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 628 | //////////////////////////////////////////////////////////////////////////////// |
| 629 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 630 | void GrGpuGL::onWriteTexturePixels(GrTexture* texture, |
| 631 | int left, int top, int width, int height, |
| 632 | GrPixelConfig config, const void* buffer, |
| 633 | size_t rowBytes) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 634 | if (NULL == buffer) { |
| 635 | return; |
| 636 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 637 | GrGLTexture* glTex = static_cast<GrGLTexture*>(texture); |
| 638 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 639 | this->setSpareTextureUnit(); |
| 640 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID())); |
| 641 | GrGLTexture::Desc desc; |
| 642 | desc.fConfig = glTex->config(); |
| 643 | desc.fWidth = glTex->width(); |
| 644 | desc.fHeight = glTex->height(); |
| 645 | desc.fOrientation = glTex->orientation(); |
| 646 | desc.fTextureID = glTex->textureID(); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 647 | |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 648 | this->uploadTexData(desc, false, |
| 649 | left, top, width, height, |
| 650 | config, buffer, rowBytes); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 651 | } |
| 652 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 653 | namespace { |
| 654 | bool adjust_pixel_ops_params(int surfaceWidth, |
| 655 | int surfaceHeight, |
| 656 | size_t bpp, |
| 657 | int* left, int* top, int* width, int* height, |
| 658 | const void** data, |
| 659 | size_t* rowBytes) { |
| 660 | if (!*rowBytes) { |
| 661 | *rowBytes = *width * bpp; |
| 662 | } |
| 663 | |
| 664 | GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height); |
| 665 | GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 666 | |
| 667 | if (!subRect.intersect(bounds)) { |
| 668 | return false; |
| 669 | } |
| 670 | *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) + |
| 671 | (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp); |
| 672 | |
| 673 | *left = subRect.fLeft; |
| 674 | *top = subRect.fTop; |
| 675 | *width = subRect.width(); |
| 676 | *height = subRect.height(); |
| 677 | return true; |
| 678 | } |
| 679 | } |
| 680 | |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 681 | bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc, |
| 682 | bool isNewTexture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 683 | int left, int top, int width, int height, |
| 684 | GrPixelConfig dataConfig, |
| 685 | const void* data, |
| 686 | size_t rowBytes) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 687 | GrAssert(NULL != data || isNewTexture); |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 688 | |
| 689 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 690 | if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 691 | &width, &height, &data, &rowBytes)) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 692 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 693 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 694 | size_t trimRowBytes = width * bpp; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 695 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 696 | // in case we need a temporary, trimmed copy of the src pixels |
| 697 | SkAutoSMalloc<128 * 128> tempStorage; |
| 698 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 699 | bool useTexStorage = isNewTexture && |
| 700 | this->glCaps().fTexStorageSupport; |
| 701 | if (useTexStorage) { |
| 702 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 703 | // 565 is not a sized internal format on desktop GL. So on desktop |
| 704 | // with 565 we always use an unsized internal format to let the |
| 705 | // system pick the best sized format to convert the 565 data to. |
| 706 | // Since glTexStorage only allows sized internal formats we will |
| 707 | // instead fallback to glTexImage2D. |
| 708 | useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig; |
| 709 | } else { |
| 710 | // ES doesn't allow paletted textures to be used with tex storage |
| 711 | useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | GrGLenum internalFormat; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 716 | GrGLenum externalFormat; |
| 717 | GrGLenum externalType; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 718 | // glTexStorage requires sized internal formats on both desktop and ES. ES |
| 719 | // glTexImage requires an unsized format. |
| 720 | if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat, |
| 721 | &externalFormat, &externalType)) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 722 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 723 | } |
| 724 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 725 | if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) { |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 726 | // paletted textures cannot be updated |
| 727 | return false; |
| 728 | } |
| 729 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 730 | /* |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 731 | * check whether to allocate a temporary buffer for flipping y or |
| 732 | * because our srcData has extra bytes past each row. If so, we need |
| 733 | * to trim those off here, since GL ES may not let us specify |
| 734 | * GL_UNPACK_ROW_LENGTH. |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 735 | */ |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 736 | bool restoreGLRowLength = false; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 737 | bool swFlipY = false; |
| 738 | bool glFlipY = false; |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 739 | if (NULL != data) { |
| 740 | if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) { |
| 741 | if (this->glCaps().fUnpackFlipYSupport) { |
| 742 | glFlipY = true; |
| 743 | } else { |
| 744 | swFlipY = true; |
| 745 | } |
| 746 | } |
| 747 | if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) { |
| 748 | // can't use this for flipping, only non-neg values allowed. :( |
| 749 | if (rowBytes != trimRowBytes) { |
| 750 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
| 751 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
| 752 | restoreGLRowLength = true; |
| 753 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 754 | } else { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 755 | if (trimRowBytes != rowBytes || swFlipY) { |
| 756 | // copy data into our new storage, skipping the trailing bytes |
| 757 | size_t trimSize = height * trimRowBytes; |
| 758 | const char* src = (const char*)data; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 759 | if (swFlipY) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 760 | src += (height - 1) * rowBytes; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 761 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 762 | char* dst = (char*)tempStorage.reset(trimSize); |
| 763 | for (int y = 0; y < height; y++) { |
| 764 | memcpy(dst, src, trimRowBytes); |
| 765 | if (swFlipY) { |
| 766 | src -= rowBytes; |
| 767 | } else { |
| 768 | src += rowBytes; |
| 769 | } |
| 770 | dst += trimRowBytes; |
| 771 | } |
| 772 | // now point data to our copied version |
| 773 | data = tempStorage.get(); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 774 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 775 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 776 | if (glFlipY) { |
| 777 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE)); |
| 778 | } |
| 779 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp))); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 780 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 781 | bool succeeded = true; |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 782 | if (isNewTexture && |
| 783 | 0 == left && 0 == top && |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 784 | desc.fWidth == width && desc.fHeight == height) { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 785 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 786 | if (useTexStorage) { |
| 787 | // We never resize or change formats of textures. We don't use |
| 788 | // mipmaps currently. |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 789 | GL_ALLOC_CALL(this->glInterface(), |
| 790 | TexStorage2D(GR_GL_TEXTURE_2D, |
| 791 | 1, // levels |
| 792 | internalFormat, |
| 793 | desc.fWidth, desc.fHeight)); |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 794 | } else { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 795 | if (GR_GL_PALETTE8_RGBA8 == internalFormat) { |
| 796 | GrGLsizei imageSize = desc.fWidth * desc.fHeight + |
| 797 | kGrColorTableSize; |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 798 | GL_ALLOC_CALL(this->glInterface(), |
| 799 | CompressedTexImage2D(GR_GL_TEXTURE_2D, |
| 800 | 0, // level |
| 801 | internalFormat, |
| 802 | desc.fWidth, desc.fHeight, |
| 803 | 0, // border |
| 804 | imageSize, |
| 805 | data)); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 806 | } else { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 807 | GL_ALLOC_CALL(this->glInterface(), |
| 808 | TexImage2D(GR_GL_TEXTURE_2D, |
| 809 | 0, // level |
| 810 | internalFormat, |
| 811 | desc.fWidth, desc.fHeight, |
| 812 | 0, // border |
| 813 | externalFormat, externalType, |
| 814 | data)); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 815 | } |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 816 | } |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 817 | GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface()); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 818 | if (error != GR_GL_NO_ERROR) { |
| 819 | succeeded = false; |
| 820 | } else { |
| 821 | // if we have data and we used TexStorage to create the texture, we |
| 822 | // now upload with TexSubImage. |
| 823 | if (NULL != data && useTexStorage) { |
| 824 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, |
| 825 | 0, // level |
| 826 | left, top, |
| 827 | width, height, |
| 828 | externalFormat, externalType, |
| 829 | data)); |
| 830 | } |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 831 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 832 | } else { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 833 | if (swFlipY || glFlipY) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 834 | top = desc.fHeight - (top + height); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 835 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 836 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, |
| 837 | 0, // level |
| 838 | left, top, |
| 839 | width, height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 840 | externalFormat, externalType, data)); |
| 841 | } |
| 842 | |
| 843 | if (restoreGLRowLength) { |
| 844 | GrAssert(this->glCaps().fUnpackRowLengthSupport); |
| 845 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 846 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 847 | if (glFlipY) { |
| 848 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 849 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 850 | return succeeded; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 851 | } |
| 852 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 853 | bool GrGpuGL::createRenderTargetObjects(int width, int height, |
| 854 | GrGLuint texID, |
| 855 | GrGLRenderTarget::Desc* desc) { |
| 856 | desc->fMSColorRenderbufferID = 0; |
| 857 | desc->fRTFBOID = 0; |
| 858 | desc->fTexFBOID = 0; |
| 859 | desc->fOwnIDs = true; |
| 860 | |
| 861 | GrGLenum status; |
| 862 | GrGLint err; |
| 863 | |
bsalomon@google.com | ab15d61 | 2011-08-09 12:57:56 +0000 | [diff] [blame] | 864 | GrGLenum msColorFormat = 0; // suppress warning |
| 865 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 866 | GL_CALL(GenFramebuffers(1, &desc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 867 | if (!desc->fTexFBOID) { |
| 868 | goto FAILED; |
| 869 | } |
| 870 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 871 | |
| 872 | // If we are using multisampling we will create two FBOS. We render |
| 873 | // to one and then resolve to the texture bound to the other. |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 874 | if (desc->fSampleCnt > 0) { |
| 875 | if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) { |
| 876 | goto FAILED; |
| 877 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 878 | GL_CALL(GenFramebuffers(1, &desc->fRTFBOID)); |
| 879 | GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 880 | if (!desc->fRTFBOID || |
| 881 | !desc->fMSColorRenderbufferID || |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 882 | !this->configToGLFormats(desc->fConfig, |
| 883 | // GLES requires sized internal formats |
| 884 | kES2_GrGLBinding == this->glBinding(), |
| 885 | &msColorFormat, NULL, NULL)) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 886 | goto FAILED; |
| 887 | } |
| 888 | } else { |
| 889 | desc->fRTFBOID = desc->fTexFBOID; |
| 890 | } |
| 891 | |
bsalomon@google.com | 0e9b41a | 2012-01-04 22:11:43 +0000 | [diff] [blame] | 892 | // below here we may bind the FBO |
| 893 | fHWDrawState.setRenderTarget(NULL); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 894 | if (desc->fRTFBOID != desc->fTexFBOID) { |
| 895 | GrAssert(desc->fSampleCnt > 1); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 896 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 897 | desc->fMSColorRenderbufferID)); |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 898 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 899 | GL_ALLOC_CALL(this->glInterface(), |
| 900 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 901 | desc->fSampleCnt, |
| 902 | msColorFormat, |
| 903 | width, height)); |
| 904 | err = CHECK_ALLOC_ERROR(this->glInterface()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 905 | if (err != GR_GL_NO_ERROR) { |
| 906 | goto FAILED; |
| 907 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 908 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID)); |
| 909 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 910 | GR_GL_COLOR_ATTACHMENT0, |
| 911 | GR_GL_RENDERBUFFER, |
| 912 | desc->fMSColorRenderbufferID)); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 913 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 914 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 915 | goto FAILED; |
| 916 | } |
| 917 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 918 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 919 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 920 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 921 | GR_GL_COLOR_ATTACHMENT0, |
| 922 | GR_GL_TEXTURE_2D, |
| 923 | texID, 0)); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 924 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 925 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 926 | goto FAILED; |
| 927 | } |
| 928 | |
| 929 | return true; |
| 930 | |
| 931 | FAILED: |
| 932 | if (desc->fMSColorRenderbufferID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 933 | GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 934 | } |
| 935 | if (desc->fRTFBOID != desc->fTexFBOID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 936 | GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 937 | } |
| 938 | if (desc->fTexFBOID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 939 | GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 940 | } |
| 941 | return false; |
| 942 | } |
| 943 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 944 | // good to set a break-point here to know when createTexture fails |
| 945 | static GrTexture* return_null_texture() { |
| 946 | // GrAssert(!"null texture"); |
| 947 | return NULL; |
| 948 | } |
| 949 | |
| 950 | #if GR_DEBUG |
| 951 | static size_t as_size_t(int x) { |
| 952 | return x; |
| 953 | } |
| 954 | #endif |
| 955 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 956 | GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc, |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 957 | const void* srcData, |
| 958 | size_t rowBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 959 | |
| 960 | #if GR_COLLECT_STATS |
| 961 | ++fStats.fTextureCreateCnt; |
| 962 | #endif |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 963 | |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 964 | GrGLTexture::Desc glTexDesc; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 965 | GrGLRenderTarget::Desc glRTDesc; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 966 | |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 967 | // Attempt to catch un- or wrongly initialized sample counts; |
| 968 | GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
| 969 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 970 | glTexDesc.fWidth = desc.fWidth; |
| 971 | glTexDesc.fHeight = desc.fHeight; |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 972 | glTexDesc.fConfig = desc.fConfig; |
| 973 | glTexDesc.fOwnsID = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 974 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 975 | glRTDesc.fMSColorRenderbufferID = 0; |
| 976 | glRTDesc.fRTFBOID = 0; |
| 977 | glRTDesc.fTexFBOID = 0; |
| 978 | glRTDesc.fOwnIDs = true; |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 979 | glRTDesc.fConfig = glTexDesc.fConfig; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 980 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 981 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 982 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 983 | const Caps& caps = this->getCaps(); |
| 984 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 985 | // We keep GrRenderTargets in GL's normal orientation so that they |
| 986 | // can be drawn to by the outside world without the client having |
| 987 | // to render upside down. |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 988 | glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation : |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 989 | GrGLTexture::kTopDown_Orientation; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 990 | |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 991 | glRTDesc.fSampleCnt = desc.fSampleCnt; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 992 | if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType && |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 993 | desc.fSampleCnt) { |
| 994 | GrPrintf("MSAA RT requested but not supported on this platform."); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 995 | } |
| 996 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 997 | if (renderTarget) { |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 998 | if (glTexDesc.fWidth > caps.fMaxRenderTargetSize || |
| 999 | glTexDesc.fHeight > caps.fMaxRenderTargetSize) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1000 | return return_null_texture(); |
| 1001 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1004 | GL_CALL(GenTextures(1, &glTexDesc.fTextureID)); |
bsalomon@google.com | 07dd2bf | 2011-12-09 19:40:36 +0000 | [diff] [blame] | 1005 | if (renderTarget && this->glCaps().fTextureUsageSupport) { |
| 1006 | // provides a hint about how this texture will be used |
| 1007 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1008 | GR_GL_TEXTURE_USAGE, |
| 1009 | GR_GL_FRAMEBUFFER_ATTACHMENT)); |
| 1010 | } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 1011 | if (!glTexDesc.fTextureID) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 1012 | return return_null_texture(); |
| 1013 | } |
| 1014 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1015 | this->setSpareTextureUnit(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1016 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID)); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 1017 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1018 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1019 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1020 | // texture that is not mipmap complete (considering the filter in use). |
| 1021 | GrGLTexture::TexParams initialTexParams; |
| 1022 | // we only set a subset here so invalidate first |
| 1023 | initialTexParams.invalidate(); |
| 1024 | initialTexParams.fFilter = GR_GL_NEAREST; |
| 1025 | initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1026 | initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1027 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1028 | GR_GL_TEXTURE_MAG_FILTER, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1029 | initialTexParams.fFilter)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1030 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1031 | GR_GL_TEXTURE_MIN_FILTER, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1032 | initialTexParams.fFilter)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1033 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1034 | GR_GL_TEXTURE_WRAP_S, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1035 | initialTexParams.fWrapS)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1036 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1037 | GR_GL_TEXTURE_WRAP_T, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1038 | initialTexParams.fWrapT)); |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1039 | if (!this->uploadTexData(glTexDesc, true, 0, 0, |
| 1040 | glTexDesc.fWidth, glTexDesc.fHeight, |
| 1041 | desc.fConfig, srcData, rowBytes)) { |
| 1042 | GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID)); |
| 1043 | return return_null_texture(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1044 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1045 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1046 | GrGLTexture* tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1047 | if (renderTarget) { |
| 1048 | #if GR_COLLECT_STATS |
| 1049 | ++fStats.fRenderTargetCreateCnt; |
| 1050 | #endif |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 1051 | if (!this->createRenderTargetObjects(glTexDesc.fWidth, |
| 1052 | glTexDesc.fHeight, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1053 | glTexDesc.fTextureID, |
| 1054 | &glRTDesc)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1055 | GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1056 | return return_null_texture(); |
| 1057 | } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 1058 | tex = new GrGLTexture(this, glTexDesc, glRTDesc); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1059 | } else { |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 1060 | tex = new GrGLTexture(this, glTexDesc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1061 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1062 | tex->setCachedTexParams(initialTexParams, this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1063 | #ifdef TRACE_TEXTURE_CREATION |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 1064 | GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n", |
| 1065 | glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1066 | #endif |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1067 | return tex; |
| 1068 | } |
| 1069 | |
| 1070 | namespace { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1071 | void inline get_stencil_rb_sizes(const GrGLInterface* gl, |
| 1072 | GrGLuint rb, |
| 1073 | GrGLStencilBuffer::Format* format) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1074 | // we shouldn't ever know one size and not the other |
| 1075 | GrAssert((kUnknownBitCount == format->fStencilBits) == |
| 1076 | (kUnknownBitCount == format->fTotalBits)); |
| 1077 | if (kUnknownBitCount == format->fStencilBits) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1078 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1079 | GR_GL_RENDERBUFFER_STENCIL_SIZE, |
| 1080 | (GrGLint*)&format->fStencilBits); |
| 1081 | if (format->fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1082 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1083 | GR_GL_RENDERBUFFER_DEPTH_SIZE, |
| 1084 | (GrGLint*)&format->fTotalBits); |
| 1085 | format->fTotalBits += format->fStencilBits; |
| 1086 | } else { |
| 1087 | format->fTotalBits = format->fStencilBits; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt, |
| 1094 | int width, int height) { |
| 1095 | |
| 1096 | // All internally created RTs are also textures. We don't create |
| 1097 | // SBs for a client's standalone RT (that is RT that isnt also a texture). |
| 1098 | GrAssert(rt->asTexture()); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 1099 | GrAssert(width >= rt->width()); |
| 1100 | GrAssert(height >= rt->height()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1101 | |
| 1102 | int samples = rt->numSamples(); |
| 1103 | GrGLuint sbID; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1104 | GL_CALL(GenRenderbuffers(1, &sbID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1105 | if (!sbID) { |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | GrGLStencilBuffer* sb = NULL; |
| 1110 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1111 | int stencilFmtCnt = fGLCaps.fStencilFormats.count(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1112 | for (int i = 0; i < stencilFmtCnt; ++i) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1113 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1114 | // we start with the last stencil format that succeeded in hopes |
| 1115 | // that we won't go through this loop more than once after the |
| 1116 | // first (painful) stencil creation. |
| 1117 | int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1118 | const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx]; |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1119 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1120 | // we do this "if" so that we don't call the multisample |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1121 | // version on a GL that doesn't have an MSAA extension. |
| 1122 | if (samples > 1) { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1123 | GL_ALLOC_CALL(this->glInterface(), |
| 1124 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 1125 | samples, |
| 1126 | sFmt.fInternalFormat, |
| 1127 | width, height)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1128 | } else { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1129 | GL_ALLOC_CALL(this->glInterface(), |
| 1130 | RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1131 | sFmt.fInternalFormat, |
| 1132 | width, height)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1135 | GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1136 | if (err == GR_GL_NO_ERROR) { |
| 1137 | // After sized formats we attempt an unsized format and take whatever |
| 1138 | // sizes GL gives us. In that case we query for the size. |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1139 | GrGLStencilBuffer::Format format = sFmt; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1140 | get_stencil_rb_sizes(this->glInterface(), sbID, &format); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1141 | sb = new GrGLStencilBuffer(this, sbID, width, height, |
| 1142 | samples, format); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1143 | if (this->attachStencilBufferToRenderTarget(sb, rt)) { |
| 1144 | fLastSuccessfulStencilFmtIdx = sIdx; |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1145 | rt->setStencilBuffer(sb); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1146 | sb->unref(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1147 | return true; |
| 1148 | } |
| 1149 | sb->abandon(); // otherwise we lose sbID |
| 1150 | sb->unref(); |
| 1151 | } |
| 1152 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1153 | GL_CALL(DeleteRenderbuffers(1, &sbID)); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1154 | return false; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, |
| 1158 | GrRenderTarget* rt) { |
| 1159 | GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt; |
| 1160 | |
| 1161 | GrGLuint fbo = glrt->renderFBOID(); |
| 1162 | |
| 1163 | if (NULL == sb) { |
| 1164 | if (NULL != rt->getStencilBuffer()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1165 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1166 | GR_GL_STENCIL_ATTACHMENT, |
| 1167 | GR_GL_RENDERBUFFER, 0)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1168 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1169 | GR_GL_DEPTH_ATTACHMENT, |
| 1170 | GR_GL_RENDERBUFFER, 0)); |
| 1171 | #if GR_DEBUG |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1172 | GrGLenum status; |
| 1173 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1174 | GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status); |
| 1175 | #endif |
| 1176 | } |
| 1177 | return true; |
| 1178 | } else { |
| 1179 | GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb; |
| 1180 | GrGLuint rb = glsb->renderbufferID(); |
| 1181 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1182 | fHWDrawState.setRenderTarget(NULL); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1183 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo)); |
| 1184 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1185 | GR_GL_STENCIL_ATTACHMENT, |
| 1186 | GR_GL_RENDERBUFFER, rb)); |
| 1187 | if (glsb->format().fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1188 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1189 | GR_GL_DEPTH_ATTACHMENT, |
| 1190 | GR_GL_RENDERBUFFER, rb)); |
| 1191 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1192 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1193 | GR_GL_DEPTH_ATTACHMENT, |
| 1194 | GR_GL_RENDERBUFFER, 0)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1195 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1196 | |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1197 | GrGLenum status; |
| 1198 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1199 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1200 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1201 | GR_GL_STENCIL_ATTACHMENT, |
| 1202 | GR_GL_RENDERBUFFER, 0)); |
| 1203 | if (glsb->format().fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1204 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1205 | GR_GL_DEPTH_ATTACHMENT, |
| 1206 | GR_GL_RENDERBUFFER, 0)); |
| 1207 | } |
| 1208 | return false; |
| 1209 | } else { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1210 | return true; |
| 1211 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1212 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1215 | //////////////////////////////////////////////////////////////////////////////// |
| 1216 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1217 | GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1218 | GrGLuint id; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1219 | GL_CALL(GenBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1220 | if (id) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1221 | GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id)); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1222 | fHWGeometryState.fArrayPtrsDirty = true; |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1223 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1224 | // make sure driver can allocate memory for this buffer |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1225 | GL_ALLOC_CALL(this->glInterface(), |
| 1226 | BufferData(GR_GL_ARRAY_BUFFER, |
| 1227 | size, |
| 1228 | NULL, // data ptr |
| 1229 | dynamic ? GR_GL_DYNAMIC_DRAW : |
| 1230 | GR_GL_STATIC_DRAW)); |
| 1231 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1232 | GL_CALL(DeleteBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1233 | // deleting bound buffer does implicit bind to 0 |
| 1234 | fHWGeometryState.fVertexBuffer = NULL; |
| 1235 | return NULL; |
| 1236 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1237 | GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1238 | size, dynamic); |
| 1239 | fHWGeometryState.fVertexBuffer = vertexBuffer; |
| 1240 | return vertexBuffer; |
| 1241 | } |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1245 | GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1246 | GrGLuint id; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1247 | GL_CALL(GenBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1248 | if (id) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1249 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id)); |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1250 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1251 | // make sure driver can allocate memory for this buffer |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1252 | GL_ALLOC_CALL(this->glInterface(), |
| 1253 | BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 1254 | size, |
| 1255 | NULL, // data ptr |
| 1256 | dynamic ? GR_GL_DYNAMIC_DRAW : |
| 1257 | GR_GL_STATIC_DRAW)); |
| 1258 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1259 | GL_CALL(DeleteBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1260 | // deleting bound buffer does implicit bind to 0 |
| 1261 | fHWGeometryState.fIndexBuffer = NULL; |
| 1262 | return NULL; |
| 1263 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1264 | GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1265 | size, dynamic); |
| 1266 | fHWGeometryState.fIndexBuffer = indexBuffer; |
| 1267 | return indexBuffer; |
| 1268 | } |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1272 | void GrGpuGL::flushScissor(const GrIRect* rect) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1273 | const GrDrawState& drawState = this->getDrawState(); |
| 1274 | const GrGLRenderTarget* rt = |
| 1275 | static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget()); |
| 1276 | |
| 1277 | GrAssert(NULL != rt); |
| 1278 | const GrGLIRect& vp = rt->getViewport(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1279 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1280 | GrGLIRect scissor; |
| 1281 | if (NULL != rect) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1282 | scissor.setRelativeTo(vp, rect->fLeft, rect->fTop, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1283 | rect->width(), rect->height()); |
| 1284 | if (scissor.contains(vp)) { |
| 1285 | rect = NULL; |
| 1286 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | if (NULL != rect) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1290 | if (fHWBounds.fScissorRect != scissor) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1291 | scissor.pushToGLScissor(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1292 | fHWBounds.fScissorRect = scissor; |
| 1293 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1294 | if (!fHWBounds.fScissorEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1295 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1296 | fHWBounds.fScissorEnabled = true; |
| 1297 | } |
| 1298 | } else { |
| 1299 | if (fHWBounds.fScissorEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1300 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1301 | fHWBounds.fScissorEnabled = false; |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1306 | void GrGpuGL::onClear(const GrIRect* rect, GrColor color) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1307 | const GrDrawState& drawState = this->getDrawState(); |
| 1308 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1309 | // parent class should never let us get here with no RT |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1310 | GrAssert(NULL != rt); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1311 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1312 | GrIRect clippedRect; |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1313 | if (NULL != rect) { |
| 1314 | // flushScissor expects rect to be clipped to the target. |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1315 | clippedRect = *rect; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1316 | GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height()); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1317 | if (clippedRect.intersect(rtRect)) { |
| 1318 | rect = &clippedRect; |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1319 | } else { |
| 1320 | return; |
| 1321 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1322 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1323 | this->flushRenderTarget(rect); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1324 | this->flushScissor(rect); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1325 | |
| 1326 | GrGLfloat r, g, b, a; |
| 1327 | static const GrGLfloat scale255 = 1.f / 255.f; |
| 1328 | a = GrColorUnpackA(color) * scale255; |
| 1329 | GrGLfloat scaleRGB = scale255; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1330 | if (GrPixelConfigIsUnpremultiplied(rt->config())) { |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1331 | scaleRGB *= a; |
| 1332 | } |
| 1333 | r = GrColorUnpackR(color) * scaleRGB; |
| 1334 | g = GrColorUnpackG(color) * scaleRGB; |
| 1335 | b = GrColorUnpackB(color) * scaleRGB; |
| 1336 | |
| 1337 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1338 | fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1339 | GL_CALL(ClearColor(r, g, b, a)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1340 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 1343 | void GrGpuGL::clearStencil() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1344 | if (NULL == this->getDrawState().getRenderTarget()) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1347 | |
| 1348 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
| 1349 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1350 | if (fHWBounds.fScissorEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1351 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1352 | fHWBounds.fScissorEnabled = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1353 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1354 | GL_CALL(StencilMask(0xffffffff)); |
| 1355 | GL_CALL(ClearStencil(0)); |
| 1356 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1357 | fHWDrawState.stencil()->invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1360 | void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1361 | const GrDrawState& drawState = this->getDrawState(); |
| 1362 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 1363 | GrAssert(NULL != rt); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1364 | |
| 1365 | // this should only be called internally when we know we have a |
| 1366 | // stencil buffer. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1367 | GrAssert(NULL != rt->getStencilBuffer()); |
| 1368 | GrGLint stencilBitCount = rt->getStencilBuffer()->bits(); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1369 | #if 0 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1370 | GrAssert(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1371 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1372 | #else |
| 1373 | // we could just clear the clip bit but when we go through |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1374 | // ANGLE a partial stencil mask will cause clears to be |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1375 | // turned into draws. Our contract on GrDrawTarget says that |
| 1376 | // changing the clip between stencil passes may or may not |
| 1377 | // zero the client's clip bits. So we just clear the whole thing. |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1378 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1379 | #endif |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1380 | GrGLint value; |
| 1381 | if (insideClip) { |
| 1382 | value = (1 << (stencilBitCount - 1)); |
| 1383 | } else { |
| 1384 | value = 0; |
| 1385 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1386 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1387 | this->flushScissor(&rect); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1388 | GL_CALL(StencilMask(clipStencilMask)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1389 | GL_CALL(ClearStencil(value)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1390 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1391 | fHWDrawState.stencil()->invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1394 | void GrGpuGL::onForceRenderTargetFlush() { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1395 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1398 | bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 1399 | int left, int top, |
| 1400 | int width, int height, |
| 1401 | GrPixelConfig config, |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1402 | size_t rowBytes) const { |
| 1403 | // if GL can do the flip then we'll never pay for it. |
| 1404 | if (this->glCaps().fPackFlipYSupport) { |
| 1405 | return false; |
| 1406 | } |
| 1407 | |
| 1408 | // If we have to do memcpy to handle non-trim rowBytes then we |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 1409 | // get the flip for free. Otherwise it costs. |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1410 | if (this->glCaps().fPackRowLengthSupport) { |
| 1411 | return true; |
| 1412 | } |
| 1413 | // If we have to do memcpys to handle rowBytes then y-flip is free |
| 1414 | // Note the rowBytes might be tight to the passed in data, but if data |
| 1415 | // gets clipped in x to the target the rowBytes will no longer be tight. |
| 1416 | if (left >= 0 && (left + width) < renderTarget->width()) { |
| 1417 | return 0 == rowBytes || |
| 1418 | GrBytesPerPixel(config) * width == rowBytes; |
| 1419 | } else { |
| 1420 | return false; |
| 1421 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1424 | bool GrGpuGL::onReadPixels(GrRenderTarget* target, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1425 | int left, int top, |
| 1426 | int width, int height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1427 | GrPixelConfig config, |
| 1428 | void* buffer, |
| 1429 | size_t rowBytes, |
| 1430 | bool invertY) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1431 | GrGLenum format; |
| 1432 | GrGLenum type; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 1433 | if (!this->configToGLFormats(config, false, NULL, &format, &type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1434 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1435 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1436 | size_t bpp = GrBytesPerPixel(config); |
| 1437 | if (!adjust_pixel_ops_params(target->width(), target->height(), bpp, |
| 1438 | &left, &top, &width, &height, |
| 1439 | const_cast<const void**>(&buffer), |
| 1440 | &rowBytes)) { |
| 1441 | return false; |
| 1442 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1443 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1444 | // resolve the render target if necessary |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1445 | GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1446 | GrDrawState::AutoRenderTargetRestore artr; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1447 | switch (tgt->getResolveType()) { |
| 1448 | case GrGLRenderTarget::kCantResolve_ResolveType: |
| 1449 | return false; |
| 1450 | case GrGLRenderTarget::kAutoResolves_ResolveType: |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1451 | artr.set(this->drawState(), target); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1452 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1453 | break; |
| 1454 | case GrGLRenderTarget::kCanResolve_ResolveType: |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame^] | 1455 | this->onResolveRenderTarget(tgt); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1456 | // we don't track the state of the READ FBO ID. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1457 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, |
| 1458 | tgt->textureFBOID())); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1459 | break; |
| 1460 | default: |
| 1461 | GrCrash("Unknown resolve type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1464 | const GrGLIRect& glvp = tgt->getViewport(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1465 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1466 | // the read rect is viewport-relative |
| 1467 | GrGLIRect readRect; |
| 1468 | readRect.setRelativeTo(glvp, left, top, width, height); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1469 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1470 | size_t tightRowBytes = bpp * width; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1471 | if (0 == rowBytes) { |
| 1472 | rowBytes = tightRowBytes; |
| 1473 | } |
| 1474 | size_t readDstRowBytes = tightRowBytes; |
| 1475 | void* readDst = buffer; |
| 1476 | |
| 1477 | // determine if GL can read using the passed rowBytes or if we need |
| 1478 | // a scratch buffer. |
| 1479 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
| 1480 | if (rowBytes != tightRowBytes) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 1481 | if (this->glCaps().fPackRowLengthSupport) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1482 | GrAssert(!(rowBytes % sizeof(GrColor))); |
| 1483 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor))); |
| 1484 | readDstRowBytes = rowBytes; |
| 1485 | } else { |
| 1486 | scratch.reset(tightRowBytes * height); |
| 1487 | readDst = scratch.get(); |
| 1488 | } |
| 1489 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1490 | if (!invertY && this->glCaps().fPackFlipYSupport) { |
| 1491 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1)); |
| 1492 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1493 | GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, |
| 1494 | readRect.fWidth, readRect.fHeight, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1495 | format, type, readDst)); |
| 1496 | if (readDstRowBytes != tightRowBytes) { |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 1497 | GrAssert(this->glCaps().fPackRowLengthSupport); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1498 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 1499 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1500 | if (!invertY && this->glCaps().fPackFlipYSupport) { |
| 1501 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0)); |
| 1502 | invertY = true; |
| 1503 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1504 | |
| 1505 | // now reverse the order of the rows, since GL's are bottom-to-top, but our |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1506 | // API presents top-to-bottom. We must preserve the padding contents. Note |
| 1507 | // that the above readPixels did not overwrite the padding. |
| 1508 | if (readDst == buffer) { |
| 1509 | GrAssert(rowBytes == readDstRowBytes); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1510 | if (!invertY) { |
| 1511 | scratch.reset(tightRowBytes); |
| 1512 | void* tmpRow = scratch.get(); |
| 1513 | // flip y in-place by rows |
| 1514 | const int halfY = height >> 1; |
| 1515 | char* top = reinterpret_cast<char*>(buffer); |
| 1516 | char* bottom = top + (height - 1) * rowBytes; |
| 1517 | for (int y = 0; y < halfY; y++) { |
| 1518 | memcpy(tmpRow, top, tightRowBytes); |
| 1519 | memcpy(top, bottom, tightRowBytes); |
| 1520 | memcpy(bottom, tmpRow, tightRowBytes); |
| 1521 | top += rowBytes; |
| 1522 | bottom -= rowBytes; |
| 1523 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1524 | } |
| 1525 | } else { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1526 | GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1527 | // copy from readDst to buffer while flipping y |
| 1528 | const int halfY = height >> 1; |
| 1529 | const char* src = reinterpret_cast<const char*>(readDst); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1530 | char* dst = reinterpret_cast<char*>(buffer); |
| 1531 | if (!invertY) { |
| 1532 | dst += (height-1) * rowBytes; |
| 1533 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1534 | for (int y = 0; y < height; y++) { |
| 1535 | memcpy(dst, src, tightRowBytes); |
| 1536 | src += readDstRowBytes; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1537 | if (invertY) { |
| 1538 | dst += rowBytes; |
| 1539 | } else { |
| 1540 | dst -= rowBytes; |
| 1541 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | return true; |
| 1545 | } |
| 1546 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1547 | void GrGpuGL::flushRenderTarget(const GrIRect* bound) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1548 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1549 | GrGLRenderTarget* rt = |
| 1550 | static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget()); |
| 1551 | GrAssert(NULL != rt); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1552 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1553 | if (fHWDrawState.getRenderTarget() != rt) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1554 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1555 | #if GR_COLLECT_STATS |
| 1556 | ++fStats.fRenderTargetChngCnt; |
| 1557 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1558 | #if GR_DEBUG |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1559 | GrGLenum status; |
| 1560 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1561 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
reed@google.com | b9255d5 | 2011-06-13 18:54:59 +0000 | [diff] [blame] | 1562 | GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1563 | } |
| 1564 | #endif |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1565 | fDirtyFlags.fRenderTargetChanged = true; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1566 | fHWDrawState.setRenderTarget(rt); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1567 | const GrGLIRect& vp = rt->getViewport(); |
bsalomon@google.com | 649a862 | 2011-03-10 14:53:38 +0000 | [diff] [blame] | 1568 | if (fHWBounds.fViewportRect != vp) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1569 | vp.pushToGLViewport(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1570 | fHWBounds.fViewportRect = vp; |
| 1571 | } |
| 1572 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1573 | if (NULL == bound || !bound->isEmpty()) { |
| 1574 | rt->flagAsNeedingResolve(bound); |
| 1575 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1578 | GrGLenum gPrimitiveType2GLMode[] = { |
| 1579 | GR_GL_TRIANGLES, |
| 1580 | GR_GL_TRIANGLE_STRIP, |
| 1581 | GR_GL_TRIANGLE_FAN, |
| 1582 | GR_GL_POINTS, |
| 1583 | GR_GL_LINES, |
| 1584 | GR_GL_LINE_STRIP |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1585 | }; |
| 1586 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1587 | #define SWAP_PER_DRAW 0 |
| 1588 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1589 | #if SWAP_PER_DRAW |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1590 | #if GR_MAC_BUILD |
| 1591 | #include <AGL/agl.h> |
| 1592 | #elif GR_WIN32_BUILD |
| 1593 | void SwapBuf() { |
| 1594 | DWORD procID = GetCurrentProcessId(); |
| 1595 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 1596 | while(hwnd) { |
| 1597 | DWORD wndProcID = 0; |
| 1598 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 1599 | if(wndProcID == procID) { |
| 1600 | SwapBuffers(GetDC(hwnd)); |
| 1601 | } |
| 1602 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 1603 | } |
| 1604 | } |
| 1605 | #endif |
| 1606 | #endif |
| 1607 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1608 | void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type, |
| 1609 | uint32_t startVertex, |
| 1610 | uint32_t startIndex, |
| 1611 | uint32_t vertexCount, |
| 1612 | uint32_t indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1613 | GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode)); |
| 1614 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1615 | GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1616 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1617 | GrAssert(NULL != fHWGeometryState.fIndexBuffer); |
| 1618 | GrAssert(NULL != fHWGeometryState.fVertexBuffer); |
| 1619 | |
| 1620 | // our setupGeometry better have adjusted this to zero since |
| 1621 | // DrawElements always draws from the begining of the arrays for idx 0. |
| 1622 | GrAssert(0 == startVertex); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1623 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1624 | GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount, |
| 1625 | GR_GL_UNSIGNED_SHORT, indices)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1626 | #if SWAP_PER_DRAW |
| 1627 | glFlush(); |
| 1628 | #if GR_MAC_BUILD |
| 1629 | aglSwapBuffers(aglGetCurrentContext()); |
| 1630 | int set_a_break_pt_here = 9; |
| 1631 | aglSwapBuffers(aglGetCurrentContext()); |
| 1632 | #elif GR_WIN32_BUILD |
| 1633 | SwapBuf(); |
| 1634 | int set_a_break_pt_here = 9; |
| 1635 | SwapBuf(); |
| 1636 | #endif |
| 1637 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1640 | void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type, |
| 1641 | uint32_t startVertex, |
| 1642 | uint32_t vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1643 | GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode)); |
| 1644 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1645 | GrAssert(NULL != fHWGeometryState.fVertexBuffer); |
| 1646 | |
| 1647 | // our setupGeometry better have adjusted this to zero. |
| 1648 | // DrawElements doesn't take an offset so we always adjus the startVertex. |
| 1649 | GrAssert(0 == startVertex); |
| 1650 | |
| 1651 | // pass 0 for parameter first. We have to adjust gl*Pointer() to |
| 1652 | // account for startVertex in the DrawElements case. So we always |
| 1653 | // rely on setupGeometry to have accounted for startVertex. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1654 | GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1655 | #if SWAP_PER_DRAW |
| 1656 | glFlush(); |
| 1657 | #if GR_MAC_BUILD |
| 1658 | aglSwapBuffers(aglGetCurrentContext()); |
| 1659 | int set_a_break_pt_here = 9; |
| 1660 | aglSwapBuffers(aglGetCurrentContext()); |
| 1661 | #elif GR_WIN32_BUILD |
| 1662 | SwapBuf(); |
| 1663 | int set_a_break_pt_here = 9; |
| 1664 | SwapBuf(); |
| 1665 | #endif |
| 1666 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame^] | 1669 | void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) { |
| 1670 | |
| 1671 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1672 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1673 | if (rt->needsResolve()) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1674 | GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1675 | GrAssert(rt->textureFBOID() != rt->renderFBOID()); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1676 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, |
| 1677 | rt->renderFBOID())); |
| 1678 | GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, |
| 1679 | rt->textureFBOID())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1680 | #if GR_COLLECT_STATS |
| 1681 | ++fStats.fRenderTargetChngCnt; |
| 1682 | #endif |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1683 | // make sure we go through flushRenderTarget() since we've modified |
| 1684 | // the bound DRAW FBO ID. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1685 | fHWDrawState.setRenderTarget(NULL); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1686 | const GrGLIRect& vp = rt->getViewport(); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1687 | const GrIRect dirtyRect = rt->getResolveRect(); |
| 1688 | GrGLIRect r; |
| 1689 | r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop, |
| 1690 | dirtyRect.width(), dirtyRect.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1691 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1692 | if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) { |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1693 | // Apple's extension uses the scissor as the blit bounds. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1694 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1695 | GL_CALL(Scissor(r.fLeft, r.fBottom, |
| 1696 | r.fWidth, r.fHeight)); |
| 1697 | GL_CALL(ResolveMultisampleFramebuffer()); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1698 | fHWBounds.fScissorRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1699 | fHWBounds.fScissorEnabled = true; |
| 1700 | } else { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1701 | if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) { |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1702 | // this respects the scissor during the blit, so disable it. |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1703 | GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType); |
| 1704 | this->flushScissor(NULL); |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1705 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1706 | int right = r.fLeft + r.fWidth; |
| 1707 | int top = r.fBottom + r.fHeight; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1708 | GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top, |
| 1709 | r.fLeft, r.fBottom, right, top, |
| 1710 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1711 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1712 | rt->flagAsResolved(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1716 | static const GrGLenum grToGLStencilFunc[] = { |
| 1717 | GR_GL_ALWAYS, // kAlways_StencilFunc |
| 1718 | GR_GL_NEVER, // kNever_StencilFunc |
| 1719 | GR_GL_GREATER, // kGreater_StencilFunc |
| 1720 | GR_GL_GEQUAL, // kGEqual_StencilFunc |
| 1721 | GR_GL_LESS, // kLess_StencilFunc |
| 1722 | GR_GL_LEQUAL, // kLEqual_StencilFunc, |
| 1723 | GR_GL_EQUAL, // kEqual_StencilFunc, |
| 1724 | GR_GL_NOTEQUAL, // kNotEqual_StencilFunc, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1725 | }; |
| 1726 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount); |
| 1727 | GR_STATIC_ASSERT(0 == kAlways_StencilFunc); |
| 1728 | GR_STATIC_ASSERT(1 == kNever_StencilFunc); |
| 1729 | GR_STATIC_ASSERT(2 == kGreater_StencilFunc); |
| 1730 | GR_STATIC_ASSERT(3 == kGEqual_StencilFunc); |
| 1731 | GR_STATIC_ASSERT(4 == kLess_StencilFunc); |
| 1732 | GR_STATIC_ASSERT(5 == kLEqual_StencilFunc); |
| 1733 | GR_STATIC_ASSERT(6 == kEqual_StencilFunc); |
| 1734 | GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc); |
| 1735 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1736 | static const GrGLenum grToGLStencilOp[] = { |
| 1737 | GR_GL_KEEP, // kKeep_StencilOp |
| 1738 | GR_GL_REPLACE, // kReplace_StencilOp |
| 1739 | GR_GL_INCR_WRAP, // kIncWrap_StencilOp |
| 1740 | GR_GL_INCR, // kIncClamp_StencilOp |
| 1741 | GR_GL_DECR_WRAP, // kDecWrap_StencilOp |
| 1742 | GR_GL_DECR, // kDecClamp_StencilOp |
| 1743 | GR_GL_ZERO, // kZero_StencilOp |
| 1744 | GR_GL_INVERT, // kInvert_StencilOp |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1745 | }; |
| 1746 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount); |
| 1747 | GR_STATIC_ASSERT(0 == kKeep_StencilOp); |
| 1748 | GR_STATIC_ASSERT(1 == kReplace_StencilOp); |
| 1749 | GR_STATIC_ASSERT(2 == kIncWrap_StencilOp); |
| 1750 | GR_STATIC_ASSERT(3 == kIncClamp_StencilOp); |
| 1751 | GR_STATIC_ASSERT(4 == kDecWrap_StencilOp); |
| 1752 | GR_STATIC_ASSERT(5 == kDecClamp_StencilOp); |
| 1753 | GR_STATIC_ASSERT(6 == kZero_StencilOp); |
| 1754 | GR_STATIC_ASSERT(7 == kInvert_StencilOp); |
| 1755 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1756 | void GrGpuGL::flushStencil() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1757 | const GrDrawState& drawState = this->getDrawState(); |
| 1758 | |
| 1759 | const GrStencilSettings* settings = &drawState.getStencil(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1760 | |
| 1761 | // use stencil for clipping if clipping is enabled and the clip |
| 1762 | // has been written into the stencil. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1763 | bool stencilClip = fClipInStencil && drawState.isClipState(); |
| 1764 | bool drawClipToStencil = |
| 1765 | drawState.isStateFlagEnabled(kModifyStencilClip_StateBit); |
bsalomon@google.com | 39dab77 | 2012-01-03 19:39:31 +0000 | [diff] [blame] | 1766 | bool stencilChange = (fHWDrawState.getStencil() != *settings) || |
| 1767 | (fHWStencilClip != stencilClip) || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1768 | (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) != |
| 1769 | drawClipToStencil); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1770 | |
| 1771 | if (stencilChange) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1772 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1773 | // we can't simultaneously perform stencil-clipping and |
| 1774 | // modify the stencil clip |
| 1775 | GrAssert(!stencilClip || !drawClipToStencil); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1776 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1777 | if (settings->isDisabled()) { |
| 1778 | if (stencilClip) { |
| 1779 | settings = &gClipStencilSettings; |
| 1780 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1781 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1782 | |
| 1783 | if (settings->isDisabled()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1784 | GL_CALL(Disable(GR_GL_STENCIL_TEST)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1785 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1786 | GL_CALL(Enable(GR_GL_STENCIL_TEST)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1787 | #if GR_DEBUG |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1788 | if (!this->getCaps().fStencilWrapOpsSupport) { |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1789 | GrAssert(settings->frontPassOp() != kIncWrap_StencilOp); |
| 1790 | GrAssert(settings->frontPassOp() != kDecWrap_StencilOp); |
| 1791 | GrAssert(settings->frontFailOp() != kIncWrap_StencilOp); |
| 1792 | GrAssert(settings->backFailOp() != kDecWrap_StencilOp); |
| 1793 | GrAssert(settings->backPassOp() != kIncWrap_StencilOp); |
| 1794 | GrAssert(settings->backPassOp() != kDecWrap_StencilOp); |
| 1795 | GrAssert(settings->backFailOp() != kIncWrap_StencilOp); |
| 1796 | GrAssert(settings->frontFailOp() != kDecWrap_StencilOp); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1797 | } |
| 1798 | #endif |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1799 | int stencilBits = 0; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1800 | GrStencilBuffer* stencilBuffer = |
| 1801 | drawState.getRenderTarget()->getStencilBuffer(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1802 | if (NULL != stencilBuffer) { |
| 1803 | stencilBits = stencilBuffer->bits(); |
| 1804 | } |
| 1805 | // TODO: dynamically attach a stencil buffer |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1806 | GrAssert(stencilBits || settings->isDisabled()); |
bsalomon@google.com | 2cdfade | 2011-11-23 16:53:42 +0000 | [diff] [blame] | 1807 | |
| 1808 | GrGLuint clipStencilMask = 0; |
| 1809 | GrGLuint userStencilMask = ~0; |
| 1810 | if (stencilBits > 0) { |
| 1811 | clipStencilMask = 1 << (stencilBits - 1); |
| 1812 | userStencilMask = clipStencilMask - 1; |
| 1813 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1814 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1815 | unsigned int frontRef = settings->frontFuncRef(); |
| 1816 | unsigned int frontMask = settings->frontFuncMask(); |
| 1817 | unsigned int frontWriteMask = settings->frontWriteMask(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1818 | GrGLenum frontFunc; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1819 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1820 | if (drawClipToStencil) { |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1821 | GrAssert(settings->frontFunc() < kBasicStencilFuncCount); |
| 1822 | frontFunc = grToGLStencilFunc[settings->frontFunc()]; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1823 | } else { |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1824 | frontFunc = grToGLStencilFunc[ConvertStencilFunc( |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1825 | stencilClip, settings->frontFunc())]; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1826 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1827 | ConvertStencilFuncAndMask(settings->frontFunc(), |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1828 | stencilClip, |
| 1829 | clipStencilMask, |
| 1830 | userStencilMask, |
| 1831 | &frontRef, |
| 1832 | &frontMask); |
| 1833 | frontWriteMask &= userStencilMask; |
| 1834 | } |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1835 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1836 | settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1837 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1838 | settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1839 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1840 | settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1841 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1842 | settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1843 | if (this->getCaps().fTwoSidedStencilSupport) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1844 | GrGLenum backFunc; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1845 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1846 | unsigned int backRef = settings->backFuncRef(); |
| 1847 | unsigned int backMask = settings->backFuncMask(); |
| 1848 | unsigned int backWriteMask = settings->backWriteMask(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1849 | |
| 1850 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1851 | if (drawClipToStencil) { |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1852 | GrAssert(settings->backFunc() < kBasicStencilFuncCount); |
| 1853 | backFunc = grToGLStencilFunc[settings->backFunc()]; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1854 | } else { |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1855 | backFunc = grToGLStencilFunc[ConvertStencilFunc( |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1856 | stencilClip, settings->backFunc())]; |
| 1857 | ConvertStencilFuncAndMask(settings->backFunc(), |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1858 | stencilClip, |
| 1859 | clipStencilMask, |
| 1860 | userStencilMask, |
| 1861 | &backRef, |
| 1862 | &backMask); |
| 1863 | backWriteMask &= userStencilMask; |
| 1864 | } |
| 1865 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1866 | GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, |
| 1867 | frontRef, frontMask)); |
| 1868 | GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask)); |
| 1869 | GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc, |
| 1870 | backRef, backMask)); |
| 1871 | GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask)); |
| 1872 | GL_CALL(StencilOpSeparate(GR_GL_FRONT, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1873 | grToGLStencilOp[settings->frontFailOp()], |
| 1874 | grToGLStencilOp[settings->frontPassOp()], |
| 1875 | grToGLStencilOp[settings->frontPassOp()])); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1876 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1877 | GL_CALL(StencilOpSeparate(GR_GL_BACK, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1878 | grToGLStencilOp[settings->backFailOp()], |
| 1879 | grToGLStencilOp[settings->backPassOp()], |
| 1880 | grToGLStencilOp[settings->backPassOp()])); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1881 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1882 | GL_CALL(StencilFunc(frontFunc, frontRef, frontMask)); |
| 1883 | GL_CALL(StencilMask(frontWriteMask)); |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1884 | GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()], |
| 1885 | grToGLStencilOp[settings->frontPassOp()], |
| 1886 | grToGLStencilOp[settings->frontPassOp()])); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1887 | } |
| 1888 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1889 | *fHWDrawState.stencil() = *settings; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1890 | fHWStencilClip = stencilClip; |
| 1891 | } |
| 1892 | } |
| 1893 | |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1894 | void GrGpuGL::flushAAState(GrPrimitiveType type) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1895 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1896 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1897 | // ES doesn't support toggling GL_MULTISAMPLE and doesn't have |
| 1898 | // smooth lines. |
| 1899 | |
| 1900 | // we prefer smooth lines over multisampled lines |
| 1901 | // msaa should be disabled if drawing smooth lines. |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1902 | if (GrIsPrimTypeLines(type)) { |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1903 | bool smooth = this->willUseHWAALines(); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1904 | if (!fHWAAState.fSmoothLineEnabled && smooth) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1905 | GL_CALL(Enable(GR_GL_LINE_SMOOTH)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1906 | fHWAAState.fSmoothLineEnabled = true; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1907 | } else if (fHWAAState.fSmoothLineEnabled && !smooth) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1908 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1909 | fHWAAState.fSmoothLineEnabled = false; |
| 1910 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1911 | if (rt->isMultisampled() && |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1912 | fHWAAState.fMSAAEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1913 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1914 | fHWAAState.fMSAAEnabled = false; |
| 1915 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1916 | } else if (rt->isMultisampled() && |
| 1917 | this->getDrawState().isHWAntialiasState() != |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1918 | fHWAAState.fMSAAEnabled) { |
| 1919 | if (fHWAAState.fMSAAEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1920 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1921 | fHWAAState.fMSAAEnabled = false; |
| 1922 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1923 | GL_CALL(Enable(GR_GL_MULTISAMPLE)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1924 | fHWAAState.fMSAAEnabled = true; |
| 1925 | } |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1930 | void GrGpuGL::flushBlend(GrPrimitiveType type, |
| 1931 | GrBlendCoeff srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1932 | GrBlendCoeff dstCoeff) { |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 1933 | if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) { |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1934 | if (fHWBlendDisabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1935 | GL_CALL(Enable(GR_GL_BLEND)); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1936 | fHWBlendDisabled = false; |
| 1937 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1938 | if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() || |
| 1939 | kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1940 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff], |
| 1941 | gXfermodeCoeff2Blend[kISA_BlendCoeff])); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1942 | fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1943 | } |
| 1944 | } else { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1945 | // any optimization to disable blending should |
| 1946 | // have already been applied and tweaked the coeffs |
| 1947 | // to (1, 0). |
| 1948 | bool blendOff = kOne_BlendCoeff == srcCoeff && |
| 1949 | kZero_BlendCoeff == dstCoeff; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1950 | if (fHWBlendDisabled != blendOff) { |
| 1951 | if (blendOff) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1952 | GL_CALL(Disable(GR_GL_BLEND)); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1953 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1954 | GL_CALL(Enable(GR_GL_BLEND)); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1955 | } |
| 1956 | fHWBlendDisabled = blendOff; |
| 1957 | } |
| 1958 | if (!blendOff) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1959 | if (fHWDrawState.getSrcBlendCoeff() != srcCoeff || |
| 1960 | fHWDrawState.getDstBlendCoeff() != dstCoeff) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1961 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff], |
| 1962 | gXfermodeCoeff2Blend[dstCoeff])); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1963 | fHWDrawState.setBlendFunc(srcCoeff, dstCoeff); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1964 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1965 | GrColor blendConst = fCurrDrawState.getBlendConstant(); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1966 | if ((BlendCoeffReferencesConstant(srcCoeff) || |
| 1967 | BlendCoeffReferencesConstant(dstCoeff)) && |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1968 | fHWDrawState.getBlendConstant() != blendConst) { |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1969 | |
| 1970 | float c[] = { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1971 | GrColorUnpackR(blendConst) / 255.f, |
| 1972 | GrColorUnpackG(blendConst) / 255.f, |
| 1973 | GrColorUnpackB(blendConst) / 255.f, |
| 1974 | GrColorUnpackA(blendConst) / 255.f |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1975 | }; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1976 | GL_CALL(BlendColor(c[0], c[1], c[2], c[3])); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1977 | fHWDrawState.setBlendConstant(blendConst); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | } |
| 1982 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1983 | namespace { |
| 1984 | |
| 1985 | unsigned gr_to_gl_filter(GrSamplerState::Filter filter) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1986 | switch (filter) { |
| 1987 | case GrSamplerState::kBilinear_Filter: |
| 1988 | case GrSamplerState::k4x4Downsample_Filter: |
| 1989 | return GR_GL_LINEAR; |
| 1990 | case GrSamplerState::kNearest_Filter: |
| 1991 | case GrSamplerState::kConvolution_Filter: |
| 1992 | return GR_GL_NEAREST; |
| 1993 | default: |
| 1994 | GrAssert(!"Unknown filter type"); |
| 1995 | return GR_GL_LINEAR; |
| 1996 | } |
| 1997 | } |
| 1998 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1999 | const GrGLenum* get_swizzle(GrPixelConfig config, |
| 2000 | const GrSamplerState& sampler) { |
| 2001 | if (GrPixelConfigIsAlphaOnly(config)) { |
| 2002 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 2003 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 2004 | return gAlphaSmear; |
| 2005 | } else if (sampler.swapsRAndB()) { |
| 2006 | static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN, |
| 2007 | GR_GL_RED, GR_GL_ALPHA }; |
| 2008 | return gRedBlueSwap; |
| 2009 | } else { |
| 2010 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, |
| 2011 | GR_GL_BLUE, GR_GL_ALPHA }; |
| 2012 | return gStraight; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) { |
| 2017 | // should add texparameteri to interface to make 1 instead of 4 calls here |
| 2018 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, |
| 2019 | GR_GL_TEXTURE_SWIZZLE_R, |
| 2020 | swizzle[0])); |
| 2021 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, |
| 2022 | GR_GL_TEXTURE_SWIZZLE_G, |
| 2023 | swizzle[1])); |
| 2024 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, |
| 2025 | GR_GL_TEXTURE_SWIZZLE_B, |
| 2026 | swizzle[2])); |
| 2027 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, |
| 2028 | GR_GL_TEXTURE_SWIZZLE_A, |
| 2029 | swizzle[3])); |
| 2030 | } |
| 2031 | } |
| 2032 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 2033 | bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2034 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2035 | GrDrawState* drawState = this->drawState(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2036 | // GrGpu::setupClipAndFlushState should have already checked this |
| 2037 | // and bailed if not true. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2038 | GrAssert(NULL != drawState->getRenderTarget()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2039 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2040 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2041 | // bind texture and set sampler state |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2042 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2043 | GrGLTexture* nextTexture = |
| 2044 | static_cast<GrGLTexture*>(drawState->getTexture(s)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2045 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2046 | // true for now, but maybe not with GrEffect. |
| 2047 | GrAssert(NULL != nextTexture); |
| 2048 | // if we created a rt/tex and rendered to it without using a |
| 2049 | // texture and now we're texuring from the rt it will still be |
| 2050 | // the last bound texture, but it needs resolving. So keep this |
| 2051 | // out of the "last != next" check. |
| 2052 | GrGLRenderTarget* texRT = |
| 2053 | static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget()); |
| 2054 | if (NULL != texRT) { |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame^] | 2055 | this->onResolveRenderTarget(texRT); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2056 | } |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2057 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2058 | if (fHWDrawState.getTexture(s) != nextTexture) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2059 | setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2060 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID())); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2061 | #if GR_COLLECT_STATS |
| 2062 | ++fStats.fTextureChngCnt; |
| 2063 | #endif |
| 2064 | //GrPrintf("---- bindtexture %d\n", nextTexture->textureID()); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2065 | fHWDrawState.setTexture(s, nextTexture); |
bsalomon@google.com | cd19a5f | 2011-06-15 14:00:23 +0000 | [diff] [blame] | 2066 | // The texture matrix has to compensate for texture width/height |
| 2067 | // and NPOT-embedded-in-POT |
| 2068 | fDirtyFlags.fTextureChangedMask |= (1 << s); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2071 | const GrSamplerState& sampler = drawState->getSampler(s); |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 2072 | ResetTimestamp timestamp; |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2073 | const GrGLTexture::TexParams& oldTexParams = |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 2074 | nextTexture->getCachedTexParams(×tamp); |
| 2075 | bool setAll = timestamp < this->getResetTimestamp(); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2076 | GrGLTexture::TexParams newTexParams; |
| 2077 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2078 | newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 2079 | |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 2080 | const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2081 | newTexParams.fWrapS = wraps[sampler.getWrapX()]; |
| 2082 | newTexParams.fWrapT = wraps[sampler.getWrapY()]; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2083 | memcpy(newTexParams.fSwizzleRGBA, |
| 2084 | get_swizzle(nextTexture->config(), sampler), |
| 2085 | sizeof(newTexParams.fSwizzleRGBA)); |
| 2086 | if (setAll || newTexParams.fFilter != oldTexParams.fFilter) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2087 | setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2088 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2089 | GR_GL_TEXTURE_MAG_FILTER, |
| 2090 | newTexParams.fFilter)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2091 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2092 | GR_GL_TEXTURE_MIN_FILTER, |
| 2093 | newTexParams.fFilter)); |
| 2094 | } |
| 2095 | if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) { |
| 2096 | setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2097 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2098 | GR_GL_TEXTURE_WRAP_S, |
| 2099 | newTexParams.fWrapS)); |
| 2100 | } |
| 2101 | if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) { |
| 2102 | setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2103 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2104 | GR_GL_TEXTURE_WRAP_T, |
| 2105 | newTexParams.fWrapT)); |
| 2106 | } |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2107 | if (this->glCaps().fTextureSwizzleSupport && |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2108 | (setAll || |
| 2109 | memcmp(newTexParams.fSwizzleRGBA, |
| 2110 | oldTexParams.fSwizzleRGBA, |
| 2111 | sizeof(newTexParams.fSwizzleRGBA)))) { |
| 2112 | setTextureUnit(s); |
| 2113 | set_tex_swizzle(newTexParams.fSwizzleRGBA, |
| 2114 | this->glInterface()); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2115 | } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 2116 | nextTexture->setCachedTexParams(newTexParams, |
| 2117 | this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 2121 | GrIRect* rect = NULL; |
| 2122 | GrIRect clipBounds; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2123 | if (drawState->isClipState() && |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 2124 | fClip.hasConservativeBounds()) { |
| 2125 | fClip.getConservativeBounds().roundOut(&clipBounds); |
| 2126 | rect = &clipBounds; |
| 2127 | } |
| 2128 | this->flushRenderTarget(rect); |
| 2129 | this->flushAAState(type); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2130 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2131 | if (drawState->isDitherState() != fHWDrawState.isDitherState()) { |
| 2132 | if (drawState->isDitherState()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2133 | GL_CALL(Enable(GR_GL_DITHER)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2134 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2135 | GL_CALL(Disable(GR_GL_DITHER)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2139 | if (drawState->isColorWriteDisabled() != |
| 2140 | fHWDrawState.isColorWriteDisabled()) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2141 | GrGLenum mask; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2142 | if (drawState->isColorWriteDisabled()) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2143 | mask = GR_GL_FALSE; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2144 | } else { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2145 | mask = GR_GL_TRUE; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2146 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2147 | GL_CALL(ColorMask(mask, mask, mask, mask)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2150 | if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) { |
| 2151 | switch (fCurrDrawState.getDrawFace()) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2152 | case GrDrawState::kCCW_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2153 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2154 | GL_CALL(CullFace(GR_GL_BACK)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2155 | break; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2156 | case GrDrawState::kCW_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2157 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2158 | GL_CALL(CullFace(GR_GL_FRONT)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2159 | break; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2160 | case GrDrawState::kBoth_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2161 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2162 | break; |
| 2163 | default: |
| 2164 | GrCrash("Unknown draw face."); |
| 2165 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2166 | fHWDrawState.setDrawFace(drawState->getDrawFace()); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2169 | #if GR_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2170 | // check for circular rendering |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2171 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2172 | GrAssert(!this->isStageEnabled(s) || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2173 | NULL == drawState->getRenderTarget() || |
| 2174 | NULL == drawState->getTexture(s) || |
| 2175 | drawState->getTexture(s)->asRenderTarget() != |
| 2176 | drawState->getRenderTarget()); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2177 | } |
| 2178 | #endif |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 2179 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2180 | this->flushStencil(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2181 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2182 | // This copy must happen after flushStencil() is called. flushStencil() |
| 2183 | // relies on detecting when the kModifyStencilClip_StateBit state has |
| 2184 | // changed since the last draw. |
| 2185 | fHWDrawState.copyStateFlags(*drawState); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2186 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2190 | if (fHWGeometryState.fVertexBuffer != buffer) { |
| 2191 | fHWGeometryState.fArrayPtrsDirty = true; |
| 2192 | fHWGeometryState.fVertexBuffer = buffer; |
| 2193 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2197 | if (fHWGeometryState.fVertexBuffer == buffer) { |
| 2198 | // deleting bound buffer does implied bind to 0 |
| 2199 | fHWGeometryState.fVertexBuffer = NULL; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2200 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2205 | fHWGeometryState.fIndexBuffer = buffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2209 | if (fHWGeometryState.fIndexBuffer == buffer) { |
| 2210 | // deleting bound buffer does implied bind to 0 |
| 2211 | fHWGeometryState.fIndexBuffer = NULL; |
| 2212 | } |
| 2213 | } |
| 2214 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2215 | void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) { |
| 2216 | GrAssert(NULL != renderTarget); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2217 | GrDrawState* drawState = this->drawState(); |
| 2218 | if (drawState->getRenderTarget() == renderTarget) { |
| 2219 | drawState->setRenderTarget(NULL); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2220 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2221 | if (fHWDrawState.getRenderTarget() == renderTarget) { |
| 2222 | fHWDrawState.setRenderTarget(NULL); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2223 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2227 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2228 | GrDrawState* drawState = this->drawState(); |
| 2229 | if (drawState->getTexture(s) == texture) { |
| 2230 | fCurrDrawState.setTexture(s, NULL); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2231 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2232 | if (fHWDrawState.getTexture(s) == texture) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2233 | // deleting bound texture does implied bind to 0 |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2234 | fHWDrawState.setTexture(s, NULL); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2235 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2236 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2239 | bool GrGpuGL::configToGLFormats(GrPixelConfig config, |
| 2240 | bool getSizedInternalFormat, |
| 2241 | GrGLenum* internalFormat, |
| 2242 | GrGLenum* externalFormat, |
| 2243 | GrGLenum* externalType) { |
| 2244 | GrGLenum dontCare; |
| 2245 | if (NULL == internalFormat) { |
| 2246 | internalFormat = &dontCare; |
| 2247 | } |
| 2248 | if (NULL == externalFormat) { |
| 2249 | externalFormat = &dontCare; |
| 2250 | } |
| 2251 | if (NULL == externalType) { |
| 2252 | externalType = &dontCare; |
| 2253 | } |
| 2254 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2255 | switch (config) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2256 | case kRGBA_8888_PM_GrPixelConfig: |
| 2257 | case kRGBA_8888_UPM_GrPixelConfig: |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2258 | *internalFormat = GR_GL_RGBA; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2259 | *externalFormat = GR_GL_RGBA; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2260 | if (getSizedInternalFormat) { |
| 2261 | *internalFormat = GR_GL_RGBA8; |
| 2262 | } else { |
| 2263 | *internalFormat = GR_GL_RGBA; |
| 2264 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2265 | *externalType = GR_GL_UNSIGNED_BYTE; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2266 | break; |
| 2267 | case kBGRA_8888_PM_GrPixelConfig: |
| 2268 | case kBGRA_8888_UPM_GrPixelConfig: |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2269 | if (!fGLCaps.fBGRAFormatSupport) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2270 | return false; |
| 2271 | } |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2272 | if (fGLCaps.fBGRAIsInternalFormat) { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2273 | if (getSizedInternalFormat) { |
| 2274 | *internalFormat = GR_GL_BGRA8; |
| 2275 | } else { |
| 2276 | *internalFormat = GR_GL_BGRA; |
| 2277 | } |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 2278 | } else { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2279 | if (getSizedInternalFormat) { |
| 2280 | *internalFormat = GR_GL_RGBA8; |
| 2281 | } else { |
| 2282 | *internalFormat = GR_GL_RGBA; |
| 2283 | } |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 2284 | } |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2285 | *externalFormat = GR_GL_BGRA; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2286 | *externalType = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2287 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2288 | case kRGB_565_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2289 | *internalFormat = GR_GL_RGB; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2290 | *externalFormat = GR_GL_RGB; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2291 | if (getSizedInternalFormat) { |
| 2292 | if (this->glBinding() == kDesktop_GrGLBinding) { |
| 2293 | return false; |
| 2294 | } else { |
| 2295 | *internalFormat = GR_GL_RGB565; |
| 2296 | } |
| 2297 | } else { |
| 2298 | *internalFormat = GR_GL_RGB; |
| 2299 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2300 | *externalType = GR_GL_UNSIGNED_SHORT_5_6_5; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2301 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2302 | case kRGBA_4444_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2303 | *internalFormat = GR_GL_RGBA; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2304 | *externalFormat = GR_GL_RGBA; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2305 | if (getSizedInternalFormat) { |
| 2306 | *internalFormat = GR_GL_RGBA4; |
| 2307 | } else { |
| 2308 | *internalFormat = GR_GL_RGBA; |
| 2309 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2310 | *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2311 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2312 | case kIndex_8_GrPixelConfig: |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 2313 | if (this->getCaps().f8BitPaletteSupport) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 2314 | *internalFormat = GR_GL_PALETTE8_RGBA8; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2315 | // glCompressedTexImage doesn't take external params |
| 2316 | *externalFormat = GR_GL_PALETTE8_RGBA8; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2317 | // no sized/unsized internal format distinction here |
| 2318 | *internalFormat = GR_GL_PALETTE8_RGBA8; |
| 2319 | // unused with CompressedTexImage |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2320 | *externalType = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2321 | } else { |
| 2322 | return false; |
| 2323 | } |
| 2324 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2325 | case kAlpha_8_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2326 | *internalFormat = GR_GL_ALPHA; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2327 | *externalFormat = GR_GL_ALPHA; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2328 | if (getSizedInternalFormat) { |
| 2329 | *internalFormat = GR_GL_ALPHA8; |
| 2330 | } else { |
| 2331 | *internalFormat = GR_GL_ALPHA; |
| 2332 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2333 | *externalType = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | default: |
| 2336 | return false; |
| 2337 | } |
| 2338 | return true; |
| 2339 | } |
| 2340 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2341 | void GrGpuGL::setTextureUnit(int unit) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2342 | GrAssert(unit >= 0 && unit < GrDrawState::kNumStages); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2343 | if (fActiveTextureUnitIdx != unit) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2344 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2345 | fActiveTextureUnitIdx = unit; |
| 2346 | } |
| 2347 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 2348 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2349 | void GrGpuGL::setSpareTextureUnit() { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2350 | if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2351 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2352 | fActiveTextureUnitIdx = SPARE_TEX_UNIT; |
| 2353 | } |
| 2354 | } |
| 2355 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 2356 | void GrGpuGL::resetDirtyFlags() { |
| 2357 | Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags)); |
| 2358 | } |
| 2359 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2360 | void GrGpuGL::setBuffers(bool indexed, |
| 2361 | int* extraVertexOffset, |
| 2362 | int* extraIndexOffset) { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2363 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2364 | GrAssert(NULL != extraVertexOffset); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2365 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2366 | const GeometryPoolState& geoPoolState = this->getGeomPoolState(); |
| 2367 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2368 | GrGLVertexBuffer* vbuf; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2369 | switch (this->getGeomSrc().fVertexSrc) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2370 | case kBuffer_GeometrySrcType: |
| 2371 | *extraVertexOffset = 0; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2372 | vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2373 | break; |
| 2374 | case kArray_GeometrySrcType: |
| 2375 | case kReserved_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2376 | this->finalizeReservedVertices(); |
| 2377 | *extraVertexOffset = geoPoolState.fPoolStartVertex; |
| 2378 | vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2379 | break; |
| 2380 | default: |
| 2381 | vbuf = NULL; // suppress warning |
| 2382 | GrCrash("Unknown geometry src type!"); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2385 | GrAssert(NULL != vbuf); |
| 2386 | GrAssert(!vbuf->isLocked()); |
| 2387 | if (fHWGeometryState.fVertexBuffer != vbuf) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2388 | GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID())); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2389 | fHWGeometryState.fArrayPtrsDirty = true; |
| 2390 | fHWGeometryState.fVertexBuffer = vbuf; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2391 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2392 | |
| 2393 | if (indexed) { |
| 2394 | GrAssert(NULL != extraIndexOffset); |
| 2395 | |
| 2396 | GrGLIndexBuffer* ibuf; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2397 | switch (this->getGeomSrc().fIndexSrc) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2398 | case kBuffer_GeometrySrcType: |
| 2399 | *extraIndexOffset = 0; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2400 | ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2401 | break; |
| 2402 | case kArray_GeometrySrcType: |
| 2403 | case kReserved_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2404 | this->finalizeReservedIndices(); |
| 2405 | *extraIndexOffset = geoPoolState.fPoolStartIndex; |
| 2406 | ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2407 | break; |
| 2408 | default: |
| 2409 | ibuf = NULL; // suppress warning |
| 2410 | GrCrash("Unknown geometry src type!"); |
| 2411 | } |
| 2412 | |
| 2413 | GrAssert(NULL != ibuf); |
| 2414 | GrAssert(!ibuf->isLocked()); |
| 2415 | if (fHWGeometryState.fIndexBuffer != ibuf) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2416 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID())); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2417 | fHWGeometryState.fIndexBuffer = ibuf; |
| 2418 | } |
| 2419 | } |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2420 | } |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 2421 | |
| 2422 | int GrGpuGL::getMaxEdges() const { |
| 2423 | // FIXME: This is a pessimistic estimate based on how many other things |
| 2424 | // want to add uniforms. This should be centralized somewhere. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2425 | return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8, |
| 2426 | GrDrawState::kMaxEdges); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 2427 | } |
bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 2428 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 2429 | void GrGpuGL::GLCaps::print() const { |
| 2430 | for (int i = 0; i < fStencilFormats.count(); ++i) { |
| 2431 | GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n", |
| 2432 | i, |
| 2433 | fStencilFormats[i].fStencilBits, |
| 2434 | fStencilFormats[i].fTotalBits); |
| 2435 | } |
| 2436 | |
| 2437 | GR_STATIC_ASSERT(0 == kNone_MSFBO); |
| 2438 | GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO); |
| 2439 | GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO); |
| 2440 | GR_STATIC_ASSERT(3 == kAppleES_MSFBO); |
| 2441 | static const char* gMSFBOExtStr[] = { |
| 2442 | "None", |
| 2443 | "ARB", |
| 2444 | "EXT", |
| 2445 | "Apple", |
| 2446 | }; |
| 2447 | GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 2448 | GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors); |
| 2449 | GrPrintf("Support RGBA8 Render Buffer: %s\n", |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2450 | (fRGBA8RenderbufferSupport ? "YES": "NO")); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2451 | GrPrintf("BGRA is an internal format: %s\n", |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2452 | (fBGRAIsInternalFormat ? "YES": "NO")); |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 2453 | GrPrintf("Support texture swizzle: %s\n", |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2454 | (fTextureSwizzleSupport ? "YES": "NO")); |
| 2455 | GrPrintf("Unpack Row length support: %s\n", |
| 2456 | (fUnpackRowLengthSupport ? "YES": "NO")); |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 2457 | GrPrintf("Unpack Flip Y support: %s\n", |
| 2458 | (fUnpackFlipYSupport ? "YES": "NO")); |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2459 | GrPrintf("Pack Row length support: %s\n", |
| 2460 | (fPackRowLengthSupport ? "YES": "NO")); |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2461 | GrPrintf("Pack Flip Y support: %s\n", |
| 2462 | (fPackFlipYSupport ? "YES": "NO")); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 2463 | } |