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