reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "GrGpuGL.h" |
| 18 | #include "GrMemory.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 20 | static const GrGLuint GR_MAX_GLUINT = ~0; |
| 21 | static const GrGLint GR_INVAL_GLINT = ~0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 23 | // we use a spare texture unit to avoid |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 24 | // mucking with the state of any of the stages. |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 25 | static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 26 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | #define SKIP_CACHE_CHECK true |
| 28 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 29 | static const GrGLenum gXfermodeCoeff2Blend[] = { |
| 30 | GR_GL_ZERO, |
| 31 | GR_GL_ONE, |
| 32 | GR_GL_SRC_COLOR, |
| 33 | GR_GL_ONE_MINUS_SRC_COLOR, |
| 34 | GR_GL_DST_COLOR, |
| 35 | GR_GL_ONE_MINUS_DST_COLOR, |
| 36 | GR_GL_SRC_ALPHA, |
| 37 | GR_GL_ONE_MINUS_SRC_ALPHA, |
| 38 | GR_GL_DST_ALPHA, |
| 39 | GR_GL_ONE_MINUS_DST_ALPHA, |
| 40 | GR_GL_CONSTANT_COLOR, |
| 41 | GR_GL_ONE_MINUS_CONSTANT_COLOR, |
| 42 | GR_GL_CONSTANT_ALPHA, |
| 43 | GR_GL_ONE_MINUS_CONSTANT_ALPHA, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 46 | bool GrGpuGL::BlendCoefReferencesConstant(GrBlendCoeff coeff) { |
| 47 | static const bool gCoeffReferencesBlendConst[] = { |
| 48 | false, |
| 49 | false, |
| 50 | false, |
| 51 | false, |
| 52 | false, |
| 53 | false, |
| 54 | false, |
| 55 | false, |
| 56 | false, |
| 57 | false, |
| 58 | true, |
| 59 | true, |
| 60 | true, |
| 61 | true, |
| 62 | }; |
| 63 | return gCoeffReferencesBlendConst[coeff]; |
| 64 | GR_STATIC_ASSERT(kBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
| 65 | } |
| 66 | |
| 67 | GR_STATIC_ASSERT(0 == kZero_BlendCoeff); |
| 68 | GR_STATIC_ASSERT(1 == kOne_BlendCoeff); |
| 69 | GR_STATIC_ASSERT(2 == kSC_BlendCoeff); |
| 70 | GR_STATIC_ASSERT(3 == kISC_BlendCoeff); |
| 71 | GR_STATIC_ASSERT(4 == kDC_BlendCoeff); |
| 72 | GR_STATIC_ASSERT(5 == kIDC_BlendCoeff); |
| 73 | GR_STATIC_ASSERT(6 == kSA_BlendCoeff); |
| 74 | GR_STATIC_ASSERT(7 == kISA_BlendCoeff); |
| 75 | GR_STATIC_ASSERT(8 == kDA_BlendCoeff); |
| 76 | GR_STATIC_ASSERT(9 == kIDA_BlendCoeff); |
| 77 | GR_STATIC_ASSERT(10 == kConstC_BlendCoeff); |
| 78 | GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff); |
| 79 | GR_STATIC_ASSERT(12 == kConstA_BlendCoeff); |
| 80 | GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff); |
| 81 | |
| 82 | GR_STATIC_ASSERT(kBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend)); |
| 83 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 86 | void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture, |
| 87 | GrSamplerState::SampleMode mode, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 88 | GrMatrix* matrix) { |
| 89 | GrAssert(NULL != texture); |
| 90 | GrAssert(NULL != matrix); |
| 91 | if (GR_Scalar1 != texture->contentScaleX() || |
| 92 | GR_Scalar1 != texture->contentScaleY()) { |
| 93 | if (GrSamplerState::kRadial_SampleMode == mode) { |
| 94 | GrMatrix scale; |
| 95 | scale.setScale(texture->contentScaleX(), texture->contentScaleX()); |
| 96 | matrix->postConcat(scale); |
| 97 | } else if (GrSamplerState::kNormal_SampleMode == mode) { |
| 98 | GrMatrix scale; |
| 99 | scale.setScale(texture->contentScaleX(), texture->contentScaleY()); |
| 100 | matrix->postConcat(scale); |
| 101 | } else { |
| 102 | GrPrintf("We haven't handled NPOT adjustment for other sample modes!"); |
| 103 | } |
| 104 | } |
| 105 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 106 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 107 | GrMatrix invY; |
| 108 | invY.setAll(GR_Scalar1, 0, 0, |
| 109 | 0, -GR_Scalar1, GR_Scalar1, |
| 110 | 0, 0, GrMatrix::I()[8]); |
| 111 | matrix->postConcat(invY); |
| 112 | } else { |
| 113 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
| 114 | } |
| 115 | } |
| 116 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 117 | bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 118 | const GrSamplerState& sampler) { |
| 119 | GrAssert(NULL != texture); |
| 120 | if (!sampler.getMatrix().isIdentity()) { |
| 121 | return false; |
| 122 | } |
| 123 | if (GR_Scalar1 != texture->contentScaleX() || |
| 124 | GR_Scalar1 != texture->contentScaleY()) { |
| 125 | return false; |
| 126 | } |
| 127 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 128 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 129 | return false; |
| 130 | } else { |
| 131 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 138 | static bool gPrintStartupSpew; |
| 139 | |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 140 | static bool fbo_test(int w, int h) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 141 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 142 | GrGLint savedFBO; |
| 143 | GrGLint savedTexUnit; |
| 144 | GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 145 | GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 146 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 147 | GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 148 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 149 | GrGLuint testFBO; |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 150 | GR_GL(GenFramebuffers(1, &testFBO)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 151 | GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 152 | GrGLuint testRTTex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | GR_GL(GenTextures(1, &testRTTex)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 154 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex)); |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 155 | // some implementations require texture to be mip-map complete before |
| 156 | // FBO with level 0 bound as color attachment will be framebuffer complete. |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 157 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); |
| 158 | GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h, |
| 159 | 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL)); |
| 160 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 161 | GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 162 | GR_GL_TEXTURE_2D, testRTTex, 0)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 163 | GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 164 | GR_GL(DeleteFramebuffers(1, &testFBO)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 165 | GR_GL(DeleteTextures(1, &testRTTex)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 166 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 167 | GR_GL(ActiveTexture(savedTexUnit)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 168 | GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 170 | return status == GR_GL_FRAMEBUFFER_COMPLETE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 171 | } |
| 172 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | GrGpuGL::GrGpuGL() { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 174 | |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 175 | if (gPrintStartupSpew) { |
| 176 | GrPrintf("------------------------- create GrGpuGL %p --------------\n", |
| 177 | this); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 178 | GrPrintf("------ VENDOR %s\n", |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 179 | GrGLGetGLInterface()->fGetString(GR_GL_VENDOR)); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 180 | GrPrintf("------ RENDERER %s\n", |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 181 | GrGLGetGLInterface()->fGetString(GR_GL_RENDERER)); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 182 | GrPrintf("------ VERSION %s\n", |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 183 | GrGLGetGLInterface()->fGetString(GR_GL_VERSION)); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 184 | GrPrintf("------ EXTENSIONS\n %s \n", |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 185 | GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS)); |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 186 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | |
| 188 | GrGLClearErr(); |
| 189 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 190 | resetDirtyFlags(); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 191 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 192 | GrGLint maxTextureUnits; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 193 | // check FS and fixed-function texture unit limits |
| 194 | // we only use textures in the fragment stage currently. |
| 195 | // checks are > to make sure we have a spare unit. |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 196 | if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) { |
| 197 | GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 198 | GrAssert(maxTextureUnits > kNumStages); |
| 199 | } |
| 200 | if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) { |
| 201 | GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits); |
| 202 | GrAssert(maxTextureUnits > kNumStages); |
| 203 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 204 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | //////////////////////////////////////////////////////////////////////////// |
| 206 | // Check for supported features. |
| 207 | |
| 208 | int major, minor; |
| 209 | gl_version(&major, &minor); |
| 210 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 211 | GrGLint numFormats; |
| 212 | GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats); |
| 213 | GrAutoSTMalloc<10, GrGLint> formats(numFormats); |
| 214 | GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 215 | for (int i = 0; i < numFormats; ++i) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 216 | if (formats[i] == GR_GL_PALETTE8_RGBA8) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | f8bitPaletteSupport = true; |
| 218 | break; |
| 219 | } |
| 220 | } |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 221 | |
| 222 | if (gPrintStartupSpew) { |
| 223 | GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO")); |
| 224 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 226 | GR_STATIC_ASSERT(0 == kNone_GrAALevel); |
| 227 | GR_STATIC_ASSERT(1 == kLow_GrAALevel); |
| 228 | GR_STATIC_ASSERT(2 == kMed_GrAALevel); |
| 229 | GR_STATIC_ASSERT(3 == kHigh_GrAALevel); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 230 | |
| 231 | memset(fAASamples, 0, sizeof(fAASamples)); |
| 232 | fMSFBOType = kNone_MSFBO; |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 233 | if (GR_GL_SUPPORT_ES) { |
| 234 | if (has_gl_extension("GL_CHROMIUM_framebuffer_multisample")) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 235 | // chrome's extension is equivalent to the EXT msaa |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 236 | // and fbo_blit extensions. |
| 237 | fMSFBOType = kDesktopEXT_MSFBO; |
| 238 | } else if (has_gl_extension("GL_APPLE_framebuffer_multisample")) { |
| 239 | fMSFBOType = kAppleES_MSFBO; |
| 240 | } |
| 241 | } else { |
| 242 | GrAssert(GR_GL_SUPPORT_DESKTOP); |
| 243 | if ((major >= 3) || has_gl_extension("GL_ARB_framebuffer_object")) { |
| 244 | fMSFBOType = kDesktopARB_MSFBO; |
| 245 | } else if (has_gl_extension("GL_EXT_framebuffer_multisample") && |
| 246 | has_gl_extension("GL_EXT_framebuffer_blit")) { |
| 247 | fMSFBOType = kDesktopEXT_MSFBO; |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 248 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | } |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 250 | if (gPrintStartupSpew) { |
| 251 | switch (fMSFBOType) { |
| 252 | case kNone_MSFBO: |
| 253 | GrPrintf("MSAA Support: NONE\n"); |
| 254 | break; |
| 255 | case kDesktopARB_MSFBO: |
| 256 | GrPrintf("MSAA Support: DESKTOP ARB.\n"); |
| 257 | break; |
| 258 | case kDesktopEXT_MSFBO: |
| 259 | GrPrintf("MSAA Support: DESKTOP EXT.\n"); |
| 260 | break; |
| 261 | case kAppleES_MSFBO: |
| 262 | GrPrintf("MSAA Support: APPLE ES.\n"); |
| 263 | break; |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 264 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (kNone_MSFBO != fMSFBOType) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 268 | GrGLint maxSamples; |
bsalomon@google.com | d1e43353 | 2011-03-21 21:38:40 +0000 | [diff] [blame] | 269 | GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | if (maxSamples > 1 ) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 271 | fAASamples[kNone_GrAALevel] = 0; |
| 272 | fAASamples[kLow_GrAALevel] = GrMax(2, |
| 273 | GrFixedFloorToInt((GR_FixedHalf) * |
| 274 | maxSamples)); |
| 275 | fAASamples[kMed_GrAALevel] = GrMax(2, |
| 276 | GrFixedFloorToInt(((GR_Fixed1*3)/4) * |
| 277 | maxSamples)); |
| 278 | fAASamples[kHigh_GrAALevel] = maxSamples; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 279 | } |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 280 | if (gPrintStartupSpew) { |
| 281 | GrPrintf("\tMax Samples: %d\n", maxSamples); |
| 282 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 283 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 284 | fFSAASupport = fAASamples[kHigh_GrAALevel] > 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 285 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 286 | if (GR_GL_SUPPORT_DESKTOP) { |
| 287 | fHasStencilWrap = (major >= 2 || (major == 1 && minor >= 4)) || |
| 288 | has_gl_extension("GL_EXT_stencil_wrap"); |
| 289 | } else { |
| 290 | fHasStencilWrap = (major >= 2) || has_gl_extension("GL_OES_stencil_wrap"); |
| 291 | } |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 292 | if (gPrintStartupSpew) { |
| 293 | GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO")); |
| 294 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 295 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 296 | if (GR_GL_SUPPORT_DESKTOP) { |
| 297 | // we could also look for GL_ATI_separate_stencil extension or |
| 298 | // GL_EXT_stencil_two_side but they use different function signatures |
| 299 | // than GL2.0+ (and than each other). |
| 300 | fTwoSidedStencilSupport = (major >= 2); |
| 301 | // supported on GL 1.4 and higher or by extension |
| 302 | fStencilWrapOpsSupport = (major > 1) || |
| 303 | ((1 == major) && (minor >= 4)) || |
| 304 | has_gl_extension("GL_EXT_stencil_wrap"); |
| 305 | } else { |
| 306 | // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be |
| 307 | // an ES1 extension. |
| 308 | fTwoSidedStencilSupport = (major >= 2); |
| 309 | // stencil wrap support is in ES2, ES1 requires extension. |
| 310 | fStencilWrapOpsSupport = (major > 1) || |
| 311 | has_gl_extension("GL_OES_stencil_wrap"); |
| 312 | } |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 313 | if (gPrintStartupSpew) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 314 | GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n", |
| 315 | (fTwoSidedStencilSupport ? "YES" : "NO"), |
| 316 | (fStencilWrapOpsSupport ? "YES" : "NO")); |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 317 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 318 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 319 | if (GR_GL_SUPPORT_DESKTOP) { |
| 320 | fRGBA8Renderbuffer = true; |
| 321 | } else { |
| 322 | fRGBA8Renderbuffer = has_gl_extension("GL_OES_rgb8_rgba8"); |
| 323 | } |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 324 | if (gPrintStartupSpew) { |
| 325 | GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO")); |
| 326 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | |
| 328 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 329 | if (GR_GL_SUPPORT_ES) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 330 | if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 331 | GrAssert(has_gl_extension("GL_EXT_texture_format_BGRA8888")); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (GR_GL_SUPPORT_DESKTOP) { |
| 336 | fBufferLockSupport = true; // we require VBO support and the desktop VBO |
| 337 | // extension includes glMapBuffer. |
| 338 | } else { |
| 339 | fBufferLockSupport = has_gl_extension("GL_OES_mapbuffer"); |
| 340 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 341 | |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 342 | if (gPrintStartupSpew) { |
| 343 | GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO")); |
| 344 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 346 | if (GR_GL_SUPPORT_DESKTOP) { |
| 347 | if (major >= 2 || has_gl_extension("GL_ARB_texture_non_power_of_two")) { |
| 348 | fNPOTTextureTileSupport = true; |
| 349 | fNPOTTextureSupport = true; |
| 350 | } else { |
| 351 | fNPOTTextureTileSupport = false; |
| 352 | fNPOTTextureSupport = false; |
| 353 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | } else { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 355 | if (major >= 2) { |
| 356 | fNPOTTextureSupport = true; |
| 357 | fNPOTTextureTileSupport = has_gl_extension("GL_OES_texture_npot"); |
| 358 | } else { |
| 359 | fNPOTTextureSupport = |
| 360 | has_gl_extension("GL_APPLE_texture_2D_limited_npot"); |
| 361 | fNPOTTextureTileSupport = false; |
| 362 | } |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 363 | } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 364 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 365 | fAALineSupport = GR_GL_SUPPORT_DESKTOP; |
| 366 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | //////////////////////////////////////////////////////////////////////////// |
| 368 | // Experiments to determine limitations that can't be queried. TODO: Make |
| 369 | // these a preprocess that generate some compile time constants. |
| 370 | |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 371 | // sanity check to make sure we can at least create an FBO from a POT texture |
bsalomon@google.com | 18908aa | 2011-02-07 14:51:55 +0000 | [diff] [blame] | 372 | |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 373 | bool simpleFBOSuccess = fbo_test(128, 128); |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 374 | if (gPrintStartupSpew) { |
| 375 | if (!simpleFBOSuccess) { |
| 376 | GrPrintf("FBO Sanity Test: FAILED\n"); |
| 377 | } else { |
| 378 | GrPrintf("FBO Sanity Test: PASSED\n"); |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 381 | GrAssert(simpleFBOSuccess); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 382 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 383 | /* Experimentation has found that some GLs that support NPOT textures |
| 384 | do not support FBOs with a NPOT texture. They report "unsupported" FBO |
| 385 | status. I don't know how to explicitly query for this. Do an |
| 386 | experiment. Note they may support NPOT with a renderbuffer but not a |
| 387 | texture. Presumably, the implementation bloats the renderbuffer |
| 388 | internally to the next POT. |
| 389 | */ |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 390 | bool fNPOTRenderTargetSupport = false; |
| 391 | if (fNPOTTextureSupport) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 392 | fNPOTRenderTargetSupport = fbo_test(200, 200); |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 393 | } |
bsalomon@google.com | 18908aa | 2011-02-07 14:51:55 +0000 | [diff] [blame] | 394 | |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 395 | if (gPrintStartupSpew) { |
| 396 | if (fNPOTTextureSupport) { |
| 397 | GrPrintf("NPOT textures supported\n"); |
| 398 | if (fNPOTTextureTileSupport) { |
| 399 | GrPrintf("NPOT texture tiling supported\n"); |
| 400 | } else { |
| 401 | GrPrintf("NPOT texture tiling NOT supported\n"); |
| 402 | } |
| 403 | if (fNPOTRenderTargetSupport) { |
| 404 | GrPrintf("NPOT render targets supported\n"); |
| 405 | } else { |
| 406 | GrPrintf("NPOT render targets NOT supported\n"); |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 407 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 408 | } else { |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 409 | GrPrintf("NPOT textures NOT supported\n"); |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 410 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 411 | } |
| 412 | |
bsalomon@google.com | 7aaee00 | 2011-04-11 19:54:04 +0000 | [diff] [blame] | 413 | GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureDimension); |
| 414 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 415 | /* The iPhone 4 has a restriction that for an FBO with texture color |
| 416 | attachment with height <= 8 then the width must be <= height. Here |
| 417 | we look for such a limitation. |
| 418 | */ |
| 419 | fMinRenderTargetHeight = GR_INVAL_GLINT; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 420 | GrGLint maxRenderSize; |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 421 | GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &maxRenderSize); |
bsalomon@google.com | 7aaee00 | 2011-04-11 19:54:04 +0000 | [diff] [blame] | 422 | // fbo_test creates FBOs with texture bound to the color attachment |
| 423 | maxRenderSize = GrMin(maxRenderSize, fMaxTextureDimension); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 424 | |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 425 | if (gPrintStartupSpew) { |
| 426 | GrPrintf("Small height FBO texture experiments\n"); |
| 427 | } |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 428 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 429 | for (GrGLuint i = 1; i <= 256; fNPOTRenderTargetSupport ? ++i : i *= 2) { |
| 430 | GrGLuint w = maxRenderSize; |
| 431 | GrGLuint h = i; |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 432 | if (fbo_test(w, h)) { |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 433 | if (gPrintStartupSpew) { |
| 434 | GrPrintf("\t[%d, %d]: PASSED\n", w, h); |
| 435 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 436 | fMinRenderTargetHeight = i; |
| 437 | break; |
| 438 | } else { |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 439 | if (gPrintStartupSpew) { |
| 440 | GrPrintf("\t[%d, %d]: FAILED\n", w, h); |
| 441 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | GrAssert(GR_INVAL_GLINT != fMinRenderTargetHeight); |
| 445 | |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 446 | if (gPrintStartupSpew) { |
| 447 | GrPrintf("Small width FBO texture experiments\n"); |
| 448 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | fMinRenderTargetWidth = GR_MAX_GLUINT; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 450 | for (GrGLuint i = 1; i <= 256; fNPOTRenderTargetSupport ? i *= 2 : ++i) { |
| 451 | GrGLuint w = i; |
| 452 | GrGLuint h = maxRenderSize; |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 453 | if (fbo_test(w, h)) { |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 454 | if (gPrintStartupSpew) { |
| 455 | GrPrintf("\t[%d, %d]: PASSED\n", w, h); |
| 456 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 457 | fMinRenderTargetWidth = i; |
| 458 | break; |
| 459 | } else { |
reed@google.com | eeeb5a0 | 2010-12-23 15:12:59 +0000 | [diff] [blame] | 460 | if (gPrintStartupSpew) { |
| 461 | GrPrintf("\t[%d, %d]: FAILED\n", w, h); |
| 462 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | GrAssert(GR_INVAL_GLINT != fMinRenderTargetWidth); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | GrGpuGL::~GrGpuGL() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 469 | } |
| 470 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 471 | void GrGpuGL::resetContext() { |
| 472 | // We detect cases when blending is effectively off |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 473 | fHWBlendDisabled = false; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 474 | GR_GL(Enable(GR_GL_BLEND)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 475 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 476 | // we don't use the zb at all |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 477 | GR_GL(Disable(GR_GL_DEPTH_TEST)); |
| 478 | GR_GL(DepthMask(GR_GL_FALSE)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 479 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 480 | GR_GL(Disable(GR_GL_CULL_FACE)); |
| 481 | GR_GL(FrontFace(GR_GL_CCW)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 482 | fHWDrawState.fDrawFace = kBoth_DrawFace; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 483 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 484 | GR_GL(Disable(GR_GL_DITHER)); |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 485 | if (GR_GL_SUPPORT_DESKTOP) { |
| 486 | GR_GL(Disable(GR_GL_LINE_SMOOTH)); |
| 487 | GR_GL(Disable(GR_GL_POINT_SMOOTH)); |
| 488 | GR_GL(Disable(GR_GL_MULTISAMPLE)); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 489 | fHWAAState.fMSAAEnabled = false; |
| 490 | fHWAAState.fSmoothLineEnabled = false; |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 491 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 492 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 493 | GR_GL(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] | 494 | fHWDrawState.fFlagBits = 0; |
| 495 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 496 | // we only ever use lines in hairline mode |
| 497 | GR_GL(LineWidth(1)); |
| 498 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 499 | // invalid |
| 500 | fActiveTextureUnitIdx = -1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 501 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 502 | // illegal values |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 503 | fHWDrawState.fSrcBlend = (GrBlendCoeff)-1; |
| 504 | fHWDrawState.fDstBlend = (GrBlendCoeff)-1; |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 505 | |
| 506 | fHWDrawState.fBlendConstant = 0x00000000; |
| 507 | GR_GL(BlendColor(0,0,0,0)); |
| 508 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 509 | fHWDrawState.fColor = GrColor_ILLEGAL; |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 510 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 511 | fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 512 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 513 | for (int s = 0; s < kNumStages; ++s) { |
| 514 | fHWDrawState.fTextures[s] = NULL; |
| 515 | fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax, |
| 516 | -GR_ScalarMax, |
| 517 | true); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 518 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 519 | fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix()); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 520 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 521 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 522 | fHWBounds.fScissorRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 523 | fHWBounds.fScissorEnabled = false; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 524 | GR_GL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 525 | fHWBounds.fViewportRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 526 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 527 | fHWDrawState.fStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 528 | fHWStencilClip = false; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 529 | fClipState.fClipIsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 530 | |
| 531 | fHWGeometryState.fIndexBuffer = NULL; |
| 532 | fHWGeometryState.fVertexBuffer = NULL; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 533 | |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 534 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 535 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 536 | GR_GL(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] | 537 | fHWDrawState.fRenderTarget = NULL; |
| 538 | } |
| 539 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 540 | GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) { |
| 541 | |
| 542 | bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType || |
| 543 | kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType; |
| 544 | bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType || |
| 545 | kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType; |
| 546 | |
| 547 | GrGLRenderTarget::GLRenderTargetIDs rtIDs; |
| 548 | if (isRenderTarget) { |
| 549 | rtIDs.fRTFBOID = desc.fPlatformRenderTarget; |
| 550 | if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) { |
| 551 | if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) { |
| 552 | rtIDs.fTexFBOID = desc.fPlatformResolveDestination; |
| 553 | } else { |
| 554 | GrAssert(!isTexture); // this should have been filtered by GrContext |
| 555 | rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
| 556 | } |
| 557 | } else { |
| 558 | rtIDs.fTexFBOID = desc.fPlatformRenderTarget; |
| 559 | } |
| 560 | // we don't know what the RB ids are without glGets and we don't care |
| 561 | // since we aren't responsible for deleting them. |
| 562 | rtIDs.fStencilRenderbufferID = 0; |
| 563 | rtIDs.fMSColorRenderbufferID = 0; |
| 564 | |
| 565 | rtIDs.fOwnIDs = false; |
| 566 | } else { |
| 567 | rtIDs.reset(); |
| 568 | } |
| 569 | |
| 570 | if (isTexture) { |
| 571 | GrGLTexture::GLTextureDesc texDesc; |
| 572 | GrGLenum dontCare; |
| 573 | if (!canBeTexture(desc.fConfig, &dontCare, |
| 574 | &texDesc.fUploadFormat, |
| 575 | &texDesc.fUploadType)) { |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | GrGLTexture::TexParams params; |
| 580 | |
| 581 | texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth; |
| 582 | texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight; |
| 583 | |
| 584 | texDesc.fFormat = texDesc.fFormat; |
| 585 | texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation; |
| 586 | texDesc.fStencilBits = desc.fStencilBits; |
| 587 | texDesc.fTextureID = desc.fPlatformTexture; |
| 588 | texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig); |
| 589 | texDesc.fOwnsID = false; |
| 590 | |
| 591 | params.invalidate(); // rather than do glGets. |
| 592 | |
| 593 | return new GrGLTexture(this, texDesc, rtIDs, params); |
| 594 | } else { |
| 595 | GrGLIRect viewport; |
| 596 | viewport.fLeft = 0; |
| 597 | viewport.fBottom = 0; |
| 598 | viewport.fWidth = desc.fWidth; |
| 599 | viewport.fHeight = desc.fHeight; |
| 600 | |
| 601 | return new GrGLRenderTarget(this, rtIDs, NULL, desc.fStencilBits, |
| 602 | kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags, |
| 603 | viewport, NULL); |
| 604 | } |
| 605 | } |
| 606 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 607 | GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget( |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 608 | intptr_t platformRenderTarget, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 609 | int stencilBits, |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 610 | bool isMultisampled, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 611 | int width, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 612 | int height) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 613 | GrGLRenderTarget::GLRenderTargetIDs rtIDs; |
| 614 | rtIDs.fStencilRenderbufferID = 0; |
| 615 | rtIDs.fMSColorRenderbufferID = 0; |
| 616 | rtIDs.fTexFBOID = 0; |
| 617 | rtIDs.fOwnIDs = false; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 618 | GrGLIRect viewport; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 619 | |
| 620 | // viewport is in GL coords (top >= bottom) |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 621 | viewport.fLeft = 0; |
| 622 | viewport.fBottom = 0; |
| 623 | viewport.fWidth = width; |
| 624 | viewport.fHeight = height; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 625 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 626 | rtIDs.fRTFBOID = (GrGLuint)platformRenderTarget; |
| 627 | rtIDs.fTexFBOID = (GrGLuint)platformRenderTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 628 | |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 629 | return new GrGLRenderTarget(this, rtIDs, NULL, stencilBits, |
| 630 | isMultisampled, viewport, NULL); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 631 | } |
| 632 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 633 | GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() { |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 634 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 635 | GrGLRenderTarget::GLRenderTargetIDs rtIDs; |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 636 | |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 637 | GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtIDs.fRTFBOID); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 638 | rtIDs.fTexFBOID = rtIDs.fRTFBOID; |
| 639 | rtIDs.fMSColorRenderbufferID = 0; |
| 640 | rtIDs.fStencilRenderbufferID = 0; |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 641 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 642 | GrGLIRect viewport; |
| 643 | viewport.setFromGLViewport(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 644 | GrGLuint stencilBits; |
| 645 | GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, (GrGLint*)&stencilBits); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 646 | |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 647 | GrGLint samples; |
| 648 | GR_GL_GetIntegerv(GR_GL_SAMPLES, &samples); |
| 649 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 650 | rtIDs.fOwnIDs = false; |
| 651 | |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 652 | return new GrGLRenderTarget(this, rtIDs, NULL, stencilBits, |
| 653 | (samples > 0), viewport, NULL); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 654 | } |
| 655 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 656 | /////////////////////////////////////////////////////////////////////////////// |
| 657 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 658 | static const GrGLuint UNKNOWN_BITS = ~0; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 659 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 660 | struct StencilFormat { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 661 | GrGLenum fEnum; |
| 662 | GrGLuint fBits; |
bsalomon@google.com | 9283b58 | 2011-04-08 19:00:04 +0000 | [diff] [blame] | 663 | bool fPacked; |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 664 | }; |
| 665 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 666 | const StencilFormat* GrGLStencilFormats() { |
| 667 | // defines stencil formats from more to less preferred |
| 668 | static const StencilFormat desktopStencilFormats[] = { |
bsalomon@google.com | 9283b58 | 2011-04-08 19:00:04 +0000 | [diff] [blame] | 669 | {GR_GL_STENCIL_INDEX8, 8, false}, |
| 670 | {GR_GL_STENCIL_INDEX16, 16, false}, |
| 671 | {GR_GL_DEPTH24_STENCIL8, 8, true }, |
| 672 | {GR_GL_STENCIL_INDEX4, 4, false}, |
| 673 | {GR_GL_STENCIL_INDEX, UNKNOWN_BITS, false}, |
| 674 | {GR_GL_DEPTH_STENCIL, UNKNOWN_BITS, true }, |
| 675 | {0, 0, false} |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | static const StencilFormat esStencilFormats[] = { |
bsalomon@google.com | 9283b58 | 2011-04-08 19:00:04 +0000 | [diff] [blame] | 679 | {GR_GL_STENCIL_INDEX8, 8, false}, |
| 680 | {GR_GL_DEPTH24_STENCIL8, 8, true }, |
| 681 | {GR_GL_STENCIL_INDEX4, 4, false}, |
| 682 | {0, 0, false} |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 683 | }; |
| 684 | |
| 685 | if (GR_GL_SUPPORT_DESKTOP) { |
| 686 | return desktopStencilFormats; |
| 687 | } else { |
| 688 | return esStencilFormats; |
| 689 | } |
| 690 | } |
| 691 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 692 | // good to set a break-point here to know when createTexture fails |
| 693 | static GrTexture* return_null_texture() { |
| 694 | // GrAssert(!"null texture"); |
| 695 | return NULL; |
| 696 | } |
| 697 | |
| 698 | #if GR_DEBUG |
| 699 | static size_t as_size_t(int x) { |
| 700 | return x; |
| 701 | } |
| 702 | #endif |
| 703 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 704 | GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc, |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 705 | const void* srcData, |
| 706 | size_t rowBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 707 | |
| 708 | #if GR_COLLECT_STATS |
| 709 | ++fStats.fTextureCreateCnt; |
| 710 | #endif |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 711 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 712 | this->setSpareTextureUnit(); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 713 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 714 | static const GrGLTexture::TexParams DEFAULT_PARAMS = { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 715 | GR_GL_NEAREST, |
| 716 | GR_GL_CLAMP_TO_EDGE, |
| 717 | GR_GL_CLAMP_TO_EDGE |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 718 | }; |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 719 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 720 | GrGLTexture::GLTextureDesc glDesc; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 721 | GrGLenum internalFormat; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 722 | |
| 723 | glDesc.fContentWidth = desc.fWidth; |
| 724 | glDesc.fContentHeight = desc.fHeight; |
| 725 | glDesc.fAllocWidth = desc.fWidth; |
| 726 | glDesc.fAllocHeight = desc.fHeight; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 727 | glDesc.fStencilBits = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 728 | glDesc.fFormat = desc.fFormat; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 729 | glDesc.fOwnsID = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 730 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 731 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 732 | if (!canBeTexture(desc.fFormat, |
| 733 | &internalFormat, |
| 734 | &glDesc.fUploadFormat, |
| 735 | &glDesc.fUploadType)) { |
| 736 | return return_null_texture(); |
| 737 | } |
| 738 | |
| 739 | GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 740 | GrGLint samples = fAASamples[desc.fAALevel]; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 741 | if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 742 | GrPrintf("AA RT requested but not supported on this platform."); |
| 743 | } |
| 744 | |
| 745 | GR_GL(GenTextures(1, &glDesc.fTextureID)); |
| 746 | if (!glDesc.fTextureID) { |
| 747 | return return_null_texture(); |
| 748 | } |
| 749 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 750 | glDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 751 | |
reed@google.com | 5e76223 | 2011-04-04 18:15:49 +0000 | [diff] [blame] | 752 | // in case we need a temporary, trimmed copy of the src pixels |
| 753 | GrAutoSMalloc<128 * 128> trimStorage; |
| 754 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 755 | /* |
| 756 | * check if our srcData has extra bytes past each row. If so, we need |
| 757 | * to trim those off here, since GL doesn't let us pass the rowBytes as |
| 758 | * a parameter to glTexImage2D |
| 759 | */ |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 760 | if (GR_GL_SUPPORT_DESKTOP) { |
| 761 | if (srcData) { |
| 762 | GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, |
| 763 | rowBytes / glDesc.fUploadByteCount)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 764 | } |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 765 | } else { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 766 | size_t trimRowBytes = desc.fWidth * glDesc.fUploadByteCount; |
| 767 | if (srcData && (trimRowBytes < rowBytes)) { |
reed@google.com | 5e76223 | 2011-04-04 18:15:49 +0000 | [diff] [blame] | 768 | // copy the data into our new storage, skipping the trailing bytes |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 769 | size_t trimSize = desc.fHeight * trimRowBytes; |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 770 | const char* src = (const char*)srcData; |
reed@google.com | 5e76223 | 2011-04-04 18:15:49 +0000 | [diff] [blame] | 771 | char* dst = (char*)trimStorage.realloc(trimSize); |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 772 | for (uint32_t y = 0; y < desc.fHeight; y++) { |
| 773 | memcpy(dst, src, trimRowBytes); |
| 774 | src += rowBytes; |
| 775 | dst += trimRowBytes; |
| 776 | } |
| 777 | // now point srcData to our trimmed version |
| 778 | srcData = trimStorage.get(); |
| 779 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 780 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 781 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 782 | if (renderTarget) { |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 783 | if (!this->npotRenderTargetSupport()) { |
| 784 | glDesc.fAllocWidth = GrNextPow2(desc.fWidth); |
| 785 | glDesc.fAllocHeight = GrNextPow2(desc.fHeight); |
| 786 | } |
| 787 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 788 | glDesc.fAllocWidth = GrMax<int>(fMinRenderTargetWidth, |
| 789 | glDesc.fAllocWidth); |
| 790 | glDesc.fAllocHeight = GrMax<int>(fMinRenderTargetHeight, |
| 791 | glDesc.fAllocHeight); |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 792 | } else if (!this->npotTextureSupport()) { |
| 793 | glDesc.fAllocWidth = GrNextPow2(desc.fWidth); |
| 794 | glDesc.fAllocHeight = GrNextPow2(desc.fHeight); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 795 | } |
| 796 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 797 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, glDesc.fTextureID)); |
| 798 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 799 | GR_GL_TEXTURE_MAG_FILTER, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 800 | DEFAULT_PARAMS.fFilter)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 801 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 802 | GR_GL_TEXTURE_MIN_FILTER, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 803 | DEFAULT_PARAMS.fFilter)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 804 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 805 | GR_GL_TEXTURE_WRAP_S, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 806 | DEFAULT_PARAMS.fWrapS)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 807 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 808 | GR_GL_TEXTURE_WRAP_T, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 809 | DEFAULT_PARAMS.fWrapT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 810 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 811 | GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, glDesc.fUploadByteCount)); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 812 | if (kIndex_8_GrPixelConfig == desc.fFormat && |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 813 | supports8BitPalette()) { |
| 814 | // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D |
| 815 | GrAssert(desc.fWidth == glDesc.fAllocWidth); |
| 816 | GrAssert(desc.fHeight == glDesc.fAllocHeight); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 817 | GrGLsizei imageSize = glDesc.fAllocWidth * glDesc.fAllocHeight + |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 818 | kGrColorTableSize; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 819 | GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, glDesc.fUploadFormat, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 820 | glDesc.fAllocWidth, glDesc.fAllocHeight, |
| 821 | 0, imageSize, srcData)); |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 822 | GrGLRestoreResetRowLength(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 823 | } else { |
| 824 | if (NULL != srcData && (glDesc.fAllocWidth != desc.fWidth || |
| 825 | glDesc.fAllocHeight != desc.fHeight)) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 826 | GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 827 | glDesc.fAllocWidth, glDesc.fAllocHeight, |
| 828 | 0, glDesc.fUploadFormat, glDesc.fUploadType, NULL)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 829 | GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fWidth, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 830 | desc.fHeight, glDesc.fUploadFormat, |
| 831 | glDesc.fUploadType, srcData)); |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 832 | GrGLRestoreResetRowLength(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 833 | |
| 834 | uint32_t extraW = glDesc.fAllocWidth - desc.fWidth; |
| 835 | uint32_t extraH = glDesc.fAllocHeight - desc.fHeight; |
| 836 | uint32_t maxTexels = extraW * extraH; |
| 837 | maxTexels = GrMax(extraW * desc.fHeight, maxTexels); |
| 838 | maxTexels = GrMax(desc.fWidth * extraH, maxTexels); |
| 839 | |
| 840 | GrAutoSMalloc<128*128> texels(glDesc.fUploadByteCount * maxTexels); |
| 841 | |
| 842 | uint32_t rowSize = desc.fWidth * glDesc.fUploadByteCount; |
| 843 | if (extraH) { |
| 844 | uint8_t* lastRowStart = (uint8_t*) srcData + |
| 845 | (desc.fHeight - 1) * rowSize; |
| 846 | uint8_t* extraRowStart = (uint8_t*)texels.get(); |
| 847 | |
| 848 | for (uint32_t i = 0; i < extraH; ++i) { |
| 849 | memcpy(extraRowStart, lastRowStart, rowSize); |
| 850 | extraRowStart += rowSize; |
| 851 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 852 | GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fHeight, desc.fWidth, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 853 | extraH, glDesc.fUploadFormat, glDesc.fUploadType, |
| 854 | texels.get())); |
| 855 | } |
| 856 | if (extraW) { |
| 857 | uint8_t* edgeTexel = (uint8_t*)srcData + rowSize - glDesc.fUploadByteCount; |
| 858 | uint8_t* extraTexel = (uint8_t*)texels.get(); |
| 859 | for (uint32_t j = 0; j < desc.fHeight; ++j) { |
| 860 | for (uint32_t i = 0; i < extraW; ++i) { |
| 861 | memcpy(extraTexel, edgeTexel, glDesc.fUploadByteCount); |
| 862 | extraTexel += glDesc.fUploadByteCount; |
| 863 | } |
| 864 | edgeTexel += rowSize; |
| 865 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 866 | GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fWidth, 0, extraW, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 867 | desc.fHeight, glDesc.fUploadFormat, |
| 868 | glDesc.fUploadType, texels.get())); |
| 869 | } |
| 870 | if (extraW && extraH) { |
| 871 | uint8_t* cornerTexel = (uint8_t*)srcData + desc.fHeight * rowSize |
| 872 | - glDesc.fUploadByteCount; |
| 873 | uint8_t* extraTexel = (uint8_t*)texels.get(); |
| 874 | for (uint32_t i = 0; i < extraW*extraH; ++i) { |
| 875 | memcpy(extraTexel, cornerTexel, glDesc.fUploadByteCount); |
| 876 | extraTexel += glDesc.fUploadByteCount; |
| 877 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 878 | GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fWidth, desc.fHeight, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 879 | extraW, extraH, glDesc.fUploadFormat, |
| 880 | glDesc.fUploadType, texels.get())); |
| 881 | } |
| 882 | |
| 883 | } else { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 884 | GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat, glDesc.fAllocWidth, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 885 | glDesc.fAllocHeight, 0, glDesc.fUploadFormat, |
| 886 | glDesc.fUploadType, srcData)); |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 887 | GrGLRestoreResetRowLength(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | glDesc.fOrientation = GrGLTexture::kTopDown_Orientation; |
| 892 | |
| 893 | GrGLRenderTarget::GLRenderTargetIDs rtIDs; |
| 894 | rtIDs.fStencilRenderbufferID = 0; |
| 895 | rtIDs.fMSColorRenderbufferID = 0; |
| 896 | rtIDs.fRTFBOID = 0; |
| 897 | rtIDs.fTexFBOID = 0; |
| 898 | rtIDs.fOwnIDs = true; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 899 | GrGLenum msColorRenderbufferFormat = -1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 900 | |
| 901 | if (renderTarget) { |
| 902 | #if GR_COLLECT_STATS |
| 903 | ++fStats.fRenderTargetCreateCnt; |
| 904 | #endif |
| 905 | bool failed = true; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 906 | GrGLenum status; |
| 907 | GrGLint err; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 908 | |
| 909 | // If need have both RT flag and srcData we have |
| 910 | // to invert the data before uploading because FBO |
| 911 | // will be rendered bottom up |
| 912 | GrAssert(NULL == srcData); |
| 913 | glDesc.fOrientation = GrGLTexture::kBottomUp_Orientation; |
| 914 | |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 915 | GR_GL(GenFramebuffers(1, &rtIDs.fTexFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 916 | GrAssert(rtIDs.fTexFBOID); |
| 917 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 918 | // If we are using multisampling and we will create two FBOS We render |
bsalomon@google.com | d1e43353 | 2011-03-21 21:38:40 +0000 | [diff] [blame] | 919 | // to one and then resolve to the texture bound to the other. |
| 920 | if (samples > 1 && kNone_MSFBO != fMSFBOType) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 921 | GR_GL(GenFramebuffers(1, &rtIDs.fRTFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 922 | GrAssert(0 != rtIDs.fRTFBOID); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 923 | GR_GL(GenRenderbuffers(1, &rtIDs.fMSColorRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 924 | GrAssert(0 != rtIDs.fMSColorRenderbufferID); |
| 925 | if (!fboInternalFormat(desc.fFormat, &msColorRenderbufferFormat)) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 926 | GR_GL(DeleteRenderbuffers(1, &rtIDs.fMSColorRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 927 | GR_GL(DeleteTextures(1, &glDesc.fTextureID)); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 928 | GR_GL(DeleteFramebuffers(1, &rtIDs.fTexFBOID)); |
| 929 | GR_GL(DeleteFramebuffers(1, &rtIDs.fRTFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 930 | return return_null_texture(); |
| 931 | } |
| 932 | } else { |
| 933 | rtIDs.fRTFBOID = rtIDs.fTexFBOID; |
| 934 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 935 | if (!(kNoStencil_GrTextureFlagBit & desc.fFlags)) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 936 | GR_GL(GenRenderbuffers(1, &rtIDs.fStencilRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 937 | GrAssert(0 != rtIDs.fStencilRenderbufferID); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 938 | } |
| 939 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 940 | // someone suggested that some systems might require |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 941 | // unbinding the texture before we call FramebufferTexture2D |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 942 | // (seems unlikely) |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 943 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 944 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 945 | err = ~GR_GL_NO_ERROR; |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 946 | |
| 947 | const StencilFormat* stencilFormats = GrGLStencilFormats(); |
| 948 | for (int i = 0; 0 != stencilFormats[i].fEnum; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 949 | if (rtIDs.fStencilRenderbufferID) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 950 | GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 951 | rtIDs.fStencilRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 952 | if (samples > 1) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 953 | GR_GL_NO_ERR(RenderbufferStorageMultisample( |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 954 | GR_GL_RENDERBUFFER, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 955 | samples, |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 956 | stencilFormats[i].fEnum, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 957 | glDesc.fAllocWidth, |
| 958 | glDesc.fAllocHeight)); |
| 959 | } else { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 960 | GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER, |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 961 | stencilFormats[i].fEnum, |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 962 | glDesc.fAllocWidth, |
| 963 | glDesc.fAllocHeight)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 964 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 965 | err = GrGLGetGLInterface()->fGetError(); |
| 966 | if (err != GR_GL_NO_ERROR) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 967 | continue; |
| 968 | } |
| 969 | } |
| 970 | if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) { |
| 971 | GrAssert(samples > 1); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 972 | GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 973 | rtIDs.fMSColorRenderbufferID)); |
| 974 | GR_GL_NO_ERR(RenderbufferStorageMultisample( |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 975 | GR_GL_RENDERBUFFER, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 976 | samples, |
| 977 | msColorRenderbufferFormat, |
| 978 | glDesc.fAllocWidth, |
| 979 | glDesc.fAllocHeight)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 980 | err = GrGLGetGLInterface()->fGetError(); |
| 981 | if (err != GR_GL_NO_ERROR) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 982 | continue; |
| 983 | } |
| 984 | } |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 985 | GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs.fTexFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 986 | |
| 987 | #if GR_COLLECT_STATS |
| 988 | ++fStats.fRenderTargetChngCnt; |
| 989 | #endif |
bsalomon@google.com | d1e43353 | 2011-03-21 21:38:40 +0000 | [diff] [blame] | 990 | GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 991 | GR_GL_COLOR_ATTACHMENT0, |
| 992 | GR_GL_TEXTURE_2D, |
| 993 | glDesc.fTextureID, 0)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 994 | if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 995 | GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 996 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 997 | GrPrintf("-- glCheckFramebufferStatus %x %d %d\n", |
| 998 | status, desc.fWidth, desc.fHeight); |
| 999 | continue; |
| 1000 | } |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1001 | GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs.fRTFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1002 | #if GR_COLLECT_STATS |
| 1003 | ++fStats.fRenderTargetChngCnt; |
| 1004 | #endif |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1005 | GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1006 | GR_GL_COLOR_ATTACHMENT0, |
| 1007 | GR_GL_RENDERBUFFER, |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1008 | rtIDs.fMSColorRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1009 | |
| 1010 | } |
| 1011 | if (rtIDs.fStencilRenderbufferID) { |
| 1012 | // bind the stencil to rt fbo if present, othewise the tex fbo |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1013 | GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1014 | GR_GL_STENCIL_ATTACHMENT, |
| 1015 | GR_GL_RENDERBUFFER, |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1016 | rtIDs.fStencilRenderbufferID)); |
bsalomon@google.com | 9283b58 | 2011-04-08 19:00:04 +0000 | [diff] [blame] | 1017 | // if it is a packed format bind to depth also, otherwise |
| 1018 | // we may get an unsupported fbo completeness result |
| 1019 | if (stencilFormats[i].fPacked) { |
| 1020 | GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1021 | GR_GL_DEPTH_ATTACHMENT, |
| 1022 | GR_GL_RENDERBUFFER, |
| 1023 | rtIDs.fStencilRenderbufferID)); |
| 1024 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1025 | } |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1026 | status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1027 | |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1028 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 1029 | GrPrintf("-- glCheckFramebufferStatus %x %d %d\n", |
| 1030 | status, desc.fWidth, desc.fHeight); |
bsalomon@google.com | 9283b58 | 2011-04-08 19:00:04 +0000 | [diff] [blame] | 1031 | // undo the depth bind |
| 1032 | if (rtIDs.fStencilRenderbufferID && |
| 1033 | stencilFormats[i].fPacked) { |
| 1034 | GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1035 | GR_GL_DEPTH_ATTACHMENT, |
| 1036 | GR_GL_RENDERBUFFER, |
| 1037 | 0)); |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 1038 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1039 | continue; |
| 1040 | } |
| 1041 | // we're successful! |
| 1042 | failed = false; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1043 | if (rtIDs.fStencilRenderbufferID) { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 1044 | if (UNKNOWN_BITS == stencilFormats[i].fBits) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1045 | GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, (GrGLint*)&glDesc.fStencilBits); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1046 | } else { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 1047 | glDesc.fStencilBits = stencilFormats[i].fBits; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1048 | } |
| 1049 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1050 | break; |
| 1051 | } |
| 1052 | if (failed) { |
| 1053 | if (rtIDs.fStencilRenderbufferID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1054 | GR_GL(DeleteRenderbuffers(1, &rtIDs.fStencilRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1055 | } |
| 1056 | if (rtIDs.fMSColorRenderbufferID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1057 | GR_GL(DeleteRenderbuffers(1, &rtIDs.fMSColorRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1058 | } |
| 1059 | if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1060 | GR_GL(DeleteFramebuffers(1, &rtIDs.fRTFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1061 | } |
| 1062 | if (rtIDs.fTexFBOID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1063 | GR_GL(DeleteFramebuffers(1, &rtIDs.fTexFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1064 | } |
| 1065 | GR_GL(DeleteTextures(1, &glDesc.fTextureID)); |
| 1066 | return return_null_texture(); |
| 1067 | } |
| 1068 | } |
| 1069 | #ifdef TRACE_TEXTURE_CREATION |
| 1070 | GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n", |
| 1071 | tex->fTextureID, width, height, tex->fUploadByteCount); |
| 1072 | #endif |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1073 | GrGLTexture* tex = new GrGLTexture(this, glDesc, rtIDs, DEFAULT_PARAMS); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (0 != rtIDs.fTexFBOID) { |
| 1076 | GrRenderTarget* rt = tex->asRenderTarget(); |
| 1077 | // We've messed with FBO state but may not have set the correct viewport |
| 1078 | // so just dirty the rendertarget state to force a resend. |
| 1079 | fHWDrawState.fRenderTarget = NULL; |
| 1080 | |
| 1081 | // clear the new stencil buffer if we have one |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1082 | if (!(desc.fFlags & kNoStencil_GrTextureFlagBit)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1083 | GrRenderTarget* rtSave = fCurrDrawState.fRenderTarget; |
| 1084 | fCurrDrawState.fRenderTarget = rt; |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 1085 | this->clearStencil(0, ~0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1086 | fCurrDrawState.fRenderTarget = rtSave; |
| 1087 | } |
| 1088 | } |
| 1089 | return tex; |
| 1090 | } |
| 1091 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1092 | GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1093 | GrGLuint id; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1094 | GR_GL(GenBuffers(1, &id)); |
| 1095 | if (id) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1096 | GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id)); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1097 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1098 | GrGLClearErr(); |
| 1099 | // make sure driver can allocate memory for this buffer |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1100 | GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL, |
| 1101 | dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW)); |
| 1102 | if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1103 | GR_GL(DeleteBuffers(1, &id)); |
| 1104 | // deleting bound buffer does implicit bind to 0 |
| 1105 | fHWGeometryState.fVertexBuffer = NULL; |
| 1106 | return NULL; |
| 1107 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1108 | GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1109 | size, dynamic); |
| 1110 | fHWGeometryState.fVertexBuffer = vertexBuffer; |
| 1111 | return vertexBuffer; |
| 1112 | } |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1116 | GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1117 | GrGLuint id; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1118 | GR_GL(GenBuffers(1, &id)); |
| 1119 | if (id) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1120 | GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1121 | GrGLClearErr(); |
| 1122 | // make sure driver can allocate memory for this buffer |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1123 | GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL, |
| 1124 | dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW)); |
| 1125 | if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1126 | GR_GL(DeleteBuffers(1, &id)); |
| 1127 | // deleting bound buffer does implicit bind to 0 |
| 1128 | fHWGeometryState.fIndexBuffer = NULL; |
| 1129 | return NULL; |
| 1130 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1131 | GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1132 | size, dynamic); |
| 1133 | fHWGeometryState.fIndexBuffer = indexBuffer; |
| 1134 | return indexBuffer; |
| 1135 | } |
| 1136 | return NULL; |
| 1137 | } |
| 1138 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1139 | void GrGpuGL::flushScissor(const GrIRect* rect) { |
| 1140 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1141 | const GrGLIRect& vp = |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1142 | ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1143 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1144 | GrGLIRect scissor; |
| 1145 | if (NULL != rect) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1146 | scissor.setRelativeTo(vp, rect->fLeft, rect->fTop, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1147 | rect->width(), rect->height()); |
| 1148 | if (scissor.contains(vp)) { |
| 1149 | rect = NULL; |
| 1150 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if (NULL != rect) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1154 | if (fHWBounds.fScissorRect != scissor) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1155 | scissor.pushToGLScissor(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1156 | fHWBounds.fScissorRect = scissor; |
| 1157 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1158 | if (!fHWBounds.fScissorEnabled) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1159 | GR_GL(Enable(GR_GL_SCISSOR_TEST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1160 | fHWBounds.fScissorEnabled = true; |
| 1161 | } |
| 1162 | } else { |
| 1163 | if (fHWBounds.fScissorEnabled) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1164 | GR_GL(Disable(GR_GL_SCISSOR_TEST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1165 | fHWBounds.fScissorEnabled = false; |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1170 | void GrGpuGL::onClear(const GrIRect* rect, GrColor color) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1171 | if (NULL == fCurrDrawState.fRenderTarget) { |
| 1172 | return; |
| 1173 | } |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1174 | GrIRect r; |
| 1175 | if (NULL != rect) { |
| 1176 | // flushScissor expects rect to be clipped to the target. |
| 1177 | r = *rect; |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame^] | 1178 | GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(), |
| 1179 | fCurrDrawState.fRenderTarget->height()); |
| 1180 | if (r.intersect(rtRect)) { |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1181 | rect = &r; |
| 1182 | } else { |
| 1183 | return; |
| 1184 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1185 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1186 | this->flushRenderTarget(rect); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1187 | this->flushScissor(rect); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1188 | GR_GL(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] | 1189 | fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1190 | GR_GL(ClearColor(GrColorUnpackR(color)/255.f, |
| 1191 | GrColorUnpackG(color)/255.f, |
| 1192 | GrColorUnpackB(color)/255.f, |
| 1193 | GrColorUnpackA(color)/255.f)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1194 | GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 1197 | void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1198 | if (NULL == fCurrDrawState.fRenderTarget) { |
| 1199 | return; |
| 1200 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1201 | |
| 1202 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
| 1203 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1204 | if (fHWBounds.fScissorEnabled) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1205 | GR_GL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1206 | fHWBounds.fScissorEnabled = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1207 | } |
| 1208 | GR_GL(StencilMask(mask)); |
| 1209 | GR_GL(ClearStencil(value)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1210 | GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1211 | fHWDrawState.fStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 1214 | void GrGpuGL::clearStencilClip(const GrIRect& rect) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1215 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1216 | #if 0 |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1217 | GrGLint stencilBitCount = fCurrDrawState.fRenderTarget->stencilBits(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1218 | GrAssert(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1219 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1220 | #else |
| 1221 | // we could just clear the clip bit but when we go through |
| 1222 | // angle a partial stencil mask will cause clears to be |
| 1223 | // turned into draws. Our contract on GrDrawTarget says that |
| 1224 | // changing the clip between stencil passes may or may not |
| 1225 | // 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] | 1226 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1227 | #endif |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1228 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1229 | flushScissor(&rect); |
| 1230 | GR_GL(StencilMask(clipStencilMask)); |
| 1231 | GR_GL(ClearStencil(0)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1232 | GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1233 | fHWDrawState.fStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1236 | void GrGpuGL::onForceRenderTargetFlush() { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1237 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1240 | bool GrGpuGL::onReadPixels(GrRenderTarget* target, |
| 1241 | int left, int top, int width, int height, |
| 1242 | GrPixelConfig config, void* buffer) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1243 | GrGLenum internalFormat; // we don't use this for glReadPixels |
| 1244 | GrGLenum format; |
| 1245 | GrGLenum type; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1246 | if (!this->canBeTexture(config, &internalFormat, &format, &type)) { |
| 1247 | return false; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1248 | } |
| 1249 | GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target); |
| 1250 | GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore; |
| 1251 | switch (tgt->getResolveType()) { |
| 1252 | case GrGLRenderTarget::kCantResolve_ResolveType: |
| 1253 | return false; |
| 1254 | case GrGLRenderTarget::kAutoResolves_ResolveType: |
| 1255 | autoTargetRestore.save(&fCurrDrawState.fRenderTarget); |
| 1256 | fCurrDrawState.fRenderTarget = target; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1257 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | case GrGLRenderTarget::kCanResolve_ResolveType: |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1260 | this->resolveRenderTarget(tgt); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1261 | // we don't track the state of the READ FBO ID. |
| 1262 | GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID())); |
| 1263 | break; |
| 1264 | default: |
| 1265 | GrCrash("Unknown resolve type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1268 | const GrGLIRect& glvp = tgt->getViewport(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1269 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1270 | // the read rect is viewport-relative |
| 1271 | GrGLIRect readRect; |
| 1272 | readRect.setRelativeTo(glvp, left, top, width, height); |
| 1273 | GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1274 | readRect.fWidth, readRect.fHeight, |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 1275 | format, type, buffer)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1276 | |
| 1277 | // now reverse the order of the rows, since GL's are bottom-to-top, but our |
| 1278 | // API presents top-to-bottom |
| 1279 | { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1280 | size_t stride = width * GrBytesPerPixel(config); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1281 | GrAutoMalloc rowStorage(stride); |
| 1282 | void* tmp = rowStorage.get(); |
| 1283 | |
| 1284 | const int halfY = height >> 1; |
| 1285 | char* top = reinterpret_cast<char*>(buffer); |
| 1286 | char* bottom = top + (height - 1) * stride; |
| 1287 | for (int y = 0; y < halfY; y++) { |
| 1288 | memcpy(tmp, top, stride); |
| 1289 | memcpy(top, bottom, stride); |
| 1290 | memcpy(bottom, tmp, stride); |
| 1291 | top += stride; |
| 1292 | bottom -= stride; |
| 1293 | } |
| 1294 | } |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1298 | void GrGpuGL::flushRenderTarget(const GrIRect* bound) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1299 | |
| 1300 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
| 1301 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1302 | GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1303 | if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1304 | GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1305 | #if GR_COLLECT_STATS |
| 1306 | ++fStats.fRenderTargetChngCnt; |
| 1307 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1308 | #if GR_DEBUG |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1309 | GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1310 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1311 | GrPrintf("-- glCheckFramebufferStatus %x\n", status); |
| 1312 | } |
| 1313 | #endif |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1314 | fDirtyFlags.fRenderTargetChanged = true; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1315 | fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1316 | const GrGLIRect& vp = rt->getViewport(); |
bsalomon@google.com | 649a862 | 2011-03-10 14:53:38 +0000 | [diff] [blame] | 1317 | if (fHWBounds.fViewportRect != vp) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1318 | vp.pushToGLViewport(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1319 | fHWBounds.fViewportRect = vp; |
| 1320 | } |
| 1321 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1322 | if (NULL == bound || !bound->isEmpty()) { |
| 1323 | rt->flagAsNeedingResolve(bound); |
| 1324 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1327 | GrGLenum gPrimitiveType2GLMode[] = { |
| 1328 | GR_GL_TRIANGLES, |
| 1329 | GR_GL_TRIANGLE_STRIP, |
| 1330 | GR_GL_TRIANGLE_FAN, |
| 1331 | GR_GL_POINTS, |
| 1332 | GR_GL_LINES, |
| 1333 | GR_GL_LINE_STRIP |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1334 | }; |
| 1335 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1336 | #define SWAP_PER_DRAW 0 |
| 1337 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1338 | #if SWAP_PER_DRAW |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1339 | #if GR_MAC_BUILD |
| 1340 | #include <AGL/agl.h> |
| 1341 | #elif GR_WIN32_BUILD |
| 1342 | void SwapBuf() { |
| 1343 | DWORD procID = GetCurrentProcessId(); |
| 1344 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 1345 | while(hwnd) { |
| 1346 | DWORD wndProcID = 0; |
| 1347 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 1348 | if(wndProcID == procID) { |
| 1349 | SwapBuffers(GetDC(hwnd)); |
| 1350 | } |
| 1351 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 1352 | } |
| 1353 | } |
| 1354 | #endif |
| 1355 | #endif |
| 1356 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1357 | void GrGpuGL::onDrawIndexed(GrPrimitiveType type, |
| 1358 | uint32_t startVertex, |
| 1359 | uint32_t startIndex, |
| 1360 | uint32_t vertexCount, |
| 1361 | uint32_t indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1362 | GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode)); |
| 1363 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1364 | GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1365 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1366 | GrAssert(NULL != fHWGeometryState.fIndexBuffer); |
| 1367 | GrAssert(NULL != fHWGeometryState.fVertexBuffer); |
| 1368 | |
| 1369 | // our setupGeometry better have adjusted this to zero since |
| 1370 | // DrawElements always draws from the begining of the arrays for idx 0. |
| 1371 | GrAssert(0 == startVertex); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1372 | |
| 1373 | GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount, |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1374 | GR_GL_UNSIGNED_SHORT, indices)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1375 | #if SWAP_PER_DRAW |
| 1376 | glFlush(); |
| 1377 | #if GR_MAC_BUILD |
| 1378 | aglSwapBuffers(aglGetCurrentContext()); |
| 1379 | int set_a_break_pt_here = 9; |
| 1380 | aglSwapBuffers(aglGetCurrentContext()); |
| 1381 | #elif GR_WIN32_BUILD |
| 1382 | SwapBuf(); |
| 1383 | int set_a_break_pt_here = 9; |
| 1384 | SwapBuf(); |
| 1385 | #endif |
| 1386 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1389 | void GrGpuGL::onDrawNonIndexed(GrPrimitiveType type, |
| 1390 | uint32_t startVertex, |
| 1391 | uint32_t vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1392 | GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode)); |
| 1393 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1394 | GrAssert(NULL != fHWGeometryState.fVertexBuffer); |
| 1395 | |
| 1396 | // our setupGeometry better have adjusted this to zero. |
| 1397 | // DrawElements doesn't take an offset so we always adjus the startVertex. |
| 1398 | GrAssert(0 == startVertex); |
| 1399 | |
| 1400 | // pass 0 for parameter first. We have to adjust gl*Pointer() to |
| 1401 | // account for startVertex in the DrawElements case. So we always |
| 1402 | // rely on setupGeometry to have accounted for startVertex. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1403 | GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1404 | #if SWAP_PER_DRAW |
| 1405 | glFlush(); |
| 1406 | #if GR_MAC_BUILD |
| 1407 | aglSwapBuffers(aglGetCurrentContext()); |
| 1408 | int set_a_break_pt_here = 9; |
| 1409 | aglSwapBuffers(aglGetCurrentContext()); |
| 1410 | #elif GR_WIN32_BUILD |
| 1411 | SwapBuf(); |
| 1412 | int set_a_break_pt_here = 9; |
| 1413 | SwapBuf(); |
| 1414 | #endif |
| 1415 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1418 | void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1419 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1420 | if (rt->needsResolve()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1421 | GrAssert(kNone_MSFBO != fMSFBOType); |
| 1422 | GrAssert(rt->textureFBOID() != rt->renderFBOID()); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1423 | GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1424 | rt->renderFBOID())); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1425 | GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1426 | rt->textureFBOID())); |
| 1427 | #if GR_COLLECT_STATS |
| 1428 | ++fStats.fRenderTargetChngCnt; |
| 1429 | #endif |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1430 | // make sure we go through flushRenderTarget() since we've modified |
| 1431 | // the bound DRAW FBO ID. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1432 | fHWDrawState.fRenderTarget = NULL; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1433 | const GrGLIRect& vp = rt->getViewport(); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1434 | const GrIRect dirtyRect = rt->getResolveRect(); |
| 1435 | GrGLIRect r; |
| 1436 | r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop, |
| 1437 | dirtyRect.width(), dirtyRect.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1438 | |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1439 | if (kAppleES_MSFBO == fMSFBOType) { |
| 1440 | // Apple's extension uses the scissor as the blit bounds. |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1441 | GR_GL(Enable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1442 | GR_GL(Scissor(r.fLeft, r.fBottom, |
| 1443 | r.fWidth, r.fHeight)); |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 1444 | GR_GL(ResolveMultisampleFramebuffer()); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1445 | fHWBounds.fScissorRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1446 | fHWBounds.fScissorEnabled = true; |
| 1447 | } else { |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1448 | if (kDesktopARB_MSFBO != fMSFBOType) { |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1449 | // this respects the scissor during the blit, so disable it. |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1450 | GrAssert(kDesktopEXT_MSFBO == fMSFBOType); |
| 1451 | flushScissor(NULL); |
| 1452 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1453 | int right = r.fLeft + r.fWidth; |
| 1454 | int top = r.fBottom + r.fHeight; |
| 1455 | GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top, |
| 1456 | r.fLeft, r.fBottom, right, top, |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1457 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1458 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1459 | rt->flagAsResolved(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1463 | static const GrGLenum grToGLStencilFunc[] = { |
| 1464 | GR_GL_ALWAYS, // kAlways_StencilFunc |
| 1465 | GR_GL_NEVER, // kNever_StencilFunc |
| 1466 | GR_GL_GREATER, // kGreater_StencilFunc |
| 1467 | GR_GL_GEQUAL, // kGEqual_StencilFunc |
| 1468 | GR_GL_LESS, // kLess_StencilFunc |
| 1469 | GR_GL_LEQUAL, // kLEqual_StencilFunc, |
| 1470 | GR_GL_EQUAL, // kEqual_StencilFunc, |
| 1471 | GR_GL_NOTEQUAL, // kNotEqual_StencilFunc, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1472 | }; |
| 1473 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount); |
| 1474 | GR_STATIC_ASSERT(0 == kAlways_StencilFunc); |
| 1475 | GR_STATIC_ASSERT(1 == kNever_StencilFunc); |
| 1476 | GR_STATIC_ASSERT(2 == kGreater_StencilFunc); |
| 1477 | GR_STATIC_ASSERT(3 == kGEqual_StencilFunc); |
| 1478 | GR_STATIC_ASSERT(4 == kLess_StencilFunc); |
| 1479 | GR_STATIC_ASSERT(5 == kLEqual_StencilFunc); |
| 1480 | GR_STATIC_ASSERT(6 == kEqual_StencilFunc); |
| 1481 | GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc); |
| 1482 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1483 | static const GrGLenum grToGLStencilOp[] = { |
| 1484 | GR_GL_KEEP, // kKeep_StencilOp |
| 1485 | GR_GL_REPLACE, // kReplace_StencilOp |
| 1486 | GR_GL_INCR_WRAP, // kIncWrap_StencilOp |
| 1487 | GR_GL_INCR, // kIncClamp_StencilOp |
| 1488 | GR_GL_DECR_WRAP, // kDecWrap_StencilOp |
| 1489 | GR_GL_DECR, // kDecClamp_StencilOp |
| 1490 | GR_GL_ZERO, // kZero_StencilOp |
| 1491 | GR_GL_INVERT, // kInvert_StencilOp |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1492 | }; |
| 1493 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount); |
| 1494 | GR_STATIC_ASSERT(0 == kKeep_StencilOp); |
| 1495 | GR_STATIC_ASSERT(1 == kReplace_StencilOp); |
| 1496 | GR_STATIC_ASSERT(2 == kIncWrap_StencilOp); |
| 1497 | GR_STATIC_ASSERT(3 == kIncClamp_StencilOp); |
| 1498 | GR_STATIC_ASSERT(4 == kDecWrap_StencilOp); |
| 1499 | GR_STATIC_ASSERT(5 == kDecClamp_StencilOp); |
| 1500 | GR_STATIC_ASSERT(6 == kZero_StencilOp); |
| 1501 | GR_STATIC_ASSERT(7 == kInvert_StencilOp); |
| 1502 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1503 | void GrGpuGL::flushStencil() { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1504 | const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1505 | |
| 1506 | // use stencil for clipping if clipping is enabled and the clip |
| 1507 | // has been written into the stencil. |
| 1508 | bool stencilClip = fClipState.fClipInStencil && |
| 1509 | (kClip_StateBit & fCurrDrawState.fFlagBits); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1510 | bool stencilChange = fHWStencilClip != stencilClip || |
| 1511 | fHWDrawState.fStencilSettings != *settings || |
| 1512 | ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) != |
| 1513 | (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1514 | |
| 1515 | if (stencilChange) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1516 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1517 | // we can't simultaneously perform stencil-clipping and modify the stencil clip |
| 1518 | GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1519 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1520 | if (settings->isDisabled()) { |
| 1521 | if (stencilClip) { |
| 1522 | settings = &gClipStencilSettings; |
| 1523 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1524 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1525 | |
| 1526 | if (settings->isDisabled()) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1527 | GR_GL(Disable(GR_GL_STENCIL_TEST)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1528 | } else { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1529 | GR_GL(Enable(GR_GL_STENCIL_TEST)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1530 | #if GR_DEBUG |
| 1531 | if (!fStencilWrapOpsSupport) { |
| 1532 | GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp); |
| 1533 | GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp); |
| 1534 | GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp); |
| 1535 | GrAssert(settings->fBackFailOp != kDecWrap_StencilOp); |
| 1536 | GrAssert(settings->fBackPassOp != kIncWrap_StencilOp); |
| 1537 | GrAssert(settings->fBackPassOp != kDecWrap_StencilOp); |
| 1538 | GrAssert(settings->fBackFailOp != kIncWrap_StencilOp); |
| 1539 | GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp); |
| 1540 | } |
| 1541 | #endif |
| 1542 | int stencilBits = fCurrDrawState.fRenderTarget->stencilBits(); |
| 1543 | GrAssert(stencilBits || |
| 1544 | (GrStencilSettings::gDisabled == |
| 1545 | fCurrDrawState.fStencilSettings)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1546 | GrGLuint clipStencilMask = 1 << (stencilBits - 1); |
| 1547 | GrGLuint userStencilMask = clipStencilMask - 1; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1548 | |
| 1549 | unsigned int frontRef = settings->fFrontFuncRef; |
| 1550 | unsigned int frontMask = settings->fFrontFuncMask; |
| 1551 | unsigned int frontWriteMask = settings->fFrontWriteMask; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1552 | GrGLenum frontFunc; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) { |
| 1555 | |
| 1556 | GrAssert(settings->fFrontFunc < kBasicStencilFuncCount); |
| 1557 | frontFunc = grToGLStencilFunc[settings->fFrontFunc]; |
| 1558 | } else { |
| 1559 | frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)]; |
| 1560 | |
| 1561 | ConvertStencilFuncAndMask(settings->fFrontFunc, |
| 1562 | stencilClip, |
| 1563 | clipStencilMask, |
| 1564 | userStencilMask, |
| 1565 | &frontRef, |
| 1566 | &frontMask); |
| 1567 | frontWriteMask &= userStencilMask; |
| 1568 | } |
| 1569 | GrAssert(settings->fFrontFailOp >= 0 && |
bsalomon@google.com | 2022c94 | 2011-03-30 21:43:04 +0000 | [diff] [blame] | 1570 | (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1571 | GrAssert(settings->fFrontPassOp >= 0 && |
bsalomon@google.com | 2022c94 | 2011-03-30 21:43:04 +0000 | [diff] [blame] | 1572 | (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1573 | GrAssert(settings->fBackFailOp >= 0 && |
bsalomon@google.com | 2022c94 | 2011-03-30 21:43:04 +0000 | [diff] [blame] | 1574 | (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1575 | GrAssert(settings->fBackPassOp >= 0 && |
bsalomon@google.com | 2022c94 | 2011-03-30 21:43:04 +0000 | [diff] [blame] | 1576 | (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1577 | if (fTwoSidedStencilSupport) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1578 | GrGLenum backFunc; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1579 | |
| 1580 | unsigned int backRef = settings->fBackFuncRef; |
| 1581 | unsigned int backMask = settings->fBackFuncMask; |
| 1582 | unsigned int backWriteMask = settings->fBackWriteMask; |
| 1583 | |
| 1584 | |
| 1585 | if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) { |
| 1586 | GrAssert(settings->fBackFunc < kBasicStencilFuncCount); |
| 1587 | backFunc = grToGLStencilFunc[settings->fBackFunc]; |
| 1588 | } else { |
| 1589 | backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)]; |
| 1590 | ConvertStencilFuncAndMask(settings->fBackFunc, |
| 1591 | stencilClip, |
| 1592 | clipStencilMask, |
| 1593 | userStencilMask, |
| 1594 | &backRef, |
| 1595 | &backMask); |
| 1596 | backWriteMask &= userStencilMask; |
| 1597 | } |
| 1598 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1599 | GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask)); |
| 1600 | GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask)); |
| 1601 | GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask)); |
| 1602 | GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask)); |
| 1603 | GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp], |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1604 | grToGLStencilOp[settings->fFrontPassOp], |
| 1605 | grToGLStencilOp[settings->fFrontPassOp])); |
| 1606 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1607 | GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp], |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1608 | grToGLStencilOp[settings->fBackPassOp], |
| 1609 | grToGLStencilOp[settings->fBackPassOp])); |
| 1610 | } else { |
| 1611 | GR_GL(StencilFunc(frontFunc, frontRef, frontMask)); |
| 1612 | GR_GL(StencilMask(frontWriteMask)); |
| 1613 | GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp], |
| 1614 | grToGLStencilOp[settings->fFrontPassOp], |
| 1615 | grToGLStencilOp[settings->fFrontPassOp])); |
| 1616 | } |
| 1617 | } |
| 1618 | fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1619 | fHWStencilClip = stencilClip; |
| 1620 | } |
| 1621 | } |
| 1622 | |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1623 | bool GrGpuGL::useSmoothLines() { |
| 1624 | // there is a conflict between using smooth lines and our use of |
| 1625 | // premultiplied alpha. Smooth lines tweak the incoming alpha value |
| 1626 | // but not in a premul-alpha way. So we only use them when our alpha |
| 1627 | // is 0xff. |
| 1628 | |
| 1629 | // TODO: write a smarter line frag shader. |
| 1630 | |
| 1631 | return (kAntialias_StateBit & fCurrDrawState.fFlagBits) && |
| 1632 | canDisableBlend(); |
| 1633 | } |
| 1634 | |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1635 | void GrGpuGL::flushAAState(GrPrimitiveType type) { |
| 1636 | if (GR_GL_SUPPORT_DESKTOP) { |
| 1637 | // ES doesn't support toggling GL_MULTISAMPLE and doesn't have |
| 1638 | // smooth lines. |
| 1639 | |
| 1640 | // we prefer smooth lines over multisampled lines |
| 1641 | // msaa should be disabled if drawing smooth lines. |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1642 | if (GrIsPrimTypeLines(type)) { |
| 1643 | bool smooth = useSmoothLines(); |
| 1644 | if (!fHWAAState.fSmoothLineEnabled && smooth) { |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1645 | GR_GL(Enable(GR_GL_LINE_SMOOTH)); |
| 1646 | fHWAAState.fSmoothLineEnabled = true; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1647 | } else if (fHWAAState.fSmoothLineEnabled && !smooth) { |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1648 | GR_GL(Disable(GR_GL_LINE_SMOOTH)); |
| 1649 | fHWAAState.fSmoothLineEnabled = false; |
| 1650 | } |
| 1651 | if (fCurrDrawState.fRenderTarget->isMultisampled() && |
| 1652 | fHWAAState.fMSAAEnabled) { |
| 1653 | GR_GL(Disable(GR_GL_MULTISAMPLE)); |
| 1654 | fHWAAState.fMSAAEnabled = false; |
| 1655 | } |
| 1656 | } else if (fCurrDrawState.fRenderTarget->isMultisampled() && |
| 1657 | !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) != |
| 1658 | fHWAAState.fMSAAEnabled) { |
| 1659 | if (fHWAAState.fMSAAEnabled) { |
| 1660 | GR_GL(Disable(GR_GL_MULTISAMPLE)); |
| 1661 | fHWAAState.fMSAAEnabled = false; |
| 1662 | } else { |
| 1663 | GR_GL(Enable(GR_GL_MULTISAMPLE)); |
| 1664 | fHWAAState.fMSAAEnabled = true; |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1670 | void GrGpuGL::flushBlend(GrPrimitiveType type) { |
| 1671 | if (GrIsPrimTypeLines(type) && useSmoothLines()) { |
| 1672 | if (fHWBlendDisabled) { |
| 1673 | GR_GL(Enable(GR_GL_BLEND)); |
| 1674 | fHWBlendDisabled = false; |
| 1675 | } |
| 1676 | if (kSA_BlendCoeff != fHWDrawState.fSrcBlend || |
| 1677 | kISA_BlendCoeff != fHWDrawState.fDstBlend) { |
| 1678 | GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff], |
| 1679 | gXfermodeCoeff2Blend[kISA_BlendCoeff])); |
| 1680 | fHWDrawState.fSrcBlend = kSA_BlendCoeff; |
| 1681 | fHWDrawState.fDstBlend = kISA_BlendCoeff; |
| 1682 | } |
| 1683 | } else { |
| 1684 | bool blendOff = canDisableBlend(); |
| 1685 | if (fHWBlendDisabled != blendOff) { |
| 1686 | if (blendOff) { |
| 1687 | GR_GL(Disable(GR_GL_BLEND)); |
| 1688 | } else { |
| 1689 | GR_GL(Enable(GR_GL_BLEND)); |
| 1690 | } |
| 1691 | fHWBlendDisabled = blendOff; |
| 1692 | } |
| 1693 | if (!blendOff) { |
| 1694 | if (fHWDrawState.fSrcBlend != fCurrDrawState.fSrcBlend || |
| 1695 | fHWDrawState.fDstBlend != fCurrDrawState.fDstBlend) { |
| 1696 | GR_GL(BlendFunc(gXfermodeCoeff2Blend[fCurrDrawState.fSrcBlend], |
| 1697 | gXfermodeCoeff2Blend[fCurrDrawState.fDstBlend])); |
| 1698 | fHWDrawState.fSrcBlend = fCurrDrawState.fSrcBlend; |
| 1699 | fHWDrawState.fDstBlend = fCurrDrawState.fDstBlend; |
| 1700 | } |
| 1701 | if ((BlendCoefReferencesConstant(fCurrDrawState.fSrcBlend) || |
| 1702 | BlendCoefReferencesConstant(fCurrDrawState.fDstBlend)) && |
| 1703 | fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) { |
| 1704 | |
| 1705 | float c[] = { |
| 1706 | GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f, |
| 1707 | GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f, |
| 1708 | GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f, |
| 1709 | GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f |
| 1710 | }; |
| 1711 | GR_GL(BlendColor(c[0], c[1], c[2], c[3])); |
| 1712 | fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant; |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1718 | bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1719 | |
| 1720 | // GrGpu::setupClipAndFlushState should have already checked this |
| 1721 | // and bailed if not true. |
| 1722 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1723 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1724 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1725 | // bind texture and set sampler state |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1726 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1727 | GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1728 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1729 | // true for now, but maybe not with GrEffect. |
| 1730 | GrAssert(NULL != nextTexture); |
| 1731 | // if we created a rt/tex and rendered to it without using a |
| 1732 | // texture and now we're texuring from the rt it will still be |
| 1733 | // the last bound texture, but it needs resolving. So keep this |
| 1734 | // out of the "last != next" check. |
| 1735 | GrGLRenderTarget* texRT = |
| 1736 | static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget()); |
| 1737 | if (NULL != texRT) { |
| 1738 | resolveRenderTarget(texRT); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1739 | } |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1740 | |
| 1741 | if (fHWDrawState.fTextures[s] != nextTexture) { |
| 1742 | setTextureUnit(s); |
| 1743 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID())); |
| 1744 | #if GR_COLLECT_STATS |
| 1745 | ++fStats.fTextureChngCnt; |
| 1746 | #endif |
| 1747 | //GrPrintf("---- bindtexture %d\n", nextTexture->textureID()); |
| 1748 | fHWDrawState.fTextures[s] = nextTexture; |
| 1749 | } |
| 1750 | |
| 1751 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
| 1752 | const GrGLTexture::TexParams& oldTexParams = |
| 1753 | nextTexture->getTexParams(); |
| 1754 | GrGLTexture::TexParams newTexParams; |
| 1755 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1756 | if (GrSamplerState::kNearest_Filter == sampler.getFilter()) { |
| 1757 | newTexParams.fFilter = GR_GL_NEAREST; |
| 1758 | } else { |
| 1759 | newTexParams.fFilter = GR_GL_LINEAR; |
| 1760 | } |
| 1761 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1762 | newTexParams.fWrapS = |
| 1763 | GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()]; |
| 1764 | newTexParams.fWrapT = |
| 1765 | GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()]; |
| 1766 | |
| 1767 | if (newTexParams.fFilter != oldTexParams.fFilter) { |
| 1768 | setTextureUnit(s); |
| 1769 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1770 | GR_GL_TEXTURE_MAG_FILTER, |
| 1771 | newTexParams.fFilter)); |
| 1772 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1773 | GR_GL_TEXTURE_MIN_FILTER, |
| 1774 | newTexParams.fFilter)); |
| 1775 | } |
| 1776 | if (newTexParams.fWrapS != oldTexParams.fWrapS) { |
| 1777 | setTextureUnit(s); |
| 1778 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1779 | GR_GL_TEXTURE_WRAP_S, |
| 1780 | newTexParams.fWrapS)); |
| 1781 | } |
| 1782 | if (newTexParams.fWrapT != oldTexParams.fWrapT) { |
| 1783 | setTextureUnit(s); |
| 1784 | GR_GL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1785 | GR_GL_TEXTURE_WRAP_T, |
| 1786 | newTexParams.fWrapT)); |
| 1787 | } |
| 1788 | nextTexture->setTexParams(newTexParams); |
| 1789 | |
| 1790 | // The texture matrix has to compensate for texture width/height |
| 1791 | // and NPOT-embedded-in-POT |
| 1792 | fDirtyFlags.fTextureChangedMask |= (1 << s); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1796 | GrIRect* rect = NULL; |
| 1797 | GrIRect clipBounds; |
| 1798 | if ((fCurrDrawState.fFlagBits & kClip_StateBit) && |
| 1799 | fClip.hasConservativeBounds()) { |
| 1800 | fClip.getConservativeBounds().roundOut(&clipBounds); |
| 1801 | rect = &clipBounds; |
| 1802 | } |
| 1803 | this->flushRenderTarget(rect); |
| 1804 | this->flushAAState(type); |
| 1805 | this->flushBlend(type); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1806 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1807 | if ((fCurrDrawState.fFlagBits & kDither_StateBit) != |
| 1808 | (fHWDrawState.fFlagBits & kDither_StateBit)) { |
| 1809 | if (fCurrDrawState.fFlagBits & kDither_StateBit) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1810 | GR_GL(Enable(GR_GL_DITHER)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1811 | } else { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1812 | GR_GL(Disable(GR_GL_DITHER)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1816 | if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) != |
| 1817 | (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1818 | GrGLenum mask; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1819 | if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1820 | mask = GR_GL_FALSE; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1821 | } else { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1822 | mask = GR_GL_TRUE; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1823 | } |
| 1824 | GR_GL(ColorMask(mask, mask, mask, mask)); |
| 1825 | } |
| 1826 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1827 | if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) { |
| 1828 | switch (fCurrDrawState.fDrawFace) { |
| 1829 | case kCCW_DrawFace: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1830 | GR_GL(Enable(GR_GL_CULL_FACE)); |
| 1831 | GR_GL(CullFace(GR_GL_BACK)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1832 | break; |
| 1833 | case kCW_DrawFace: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1834 | GR_GL(Enable(GR_GL_CULL_FACE)); |
| 1835 | GR_GL(CullFace(GR_GL_FRONT)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1836 | break; |
| 1837 | case kBoth_DrawFace: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1838 | GR_GL(Disable(GR_GL_CULL_FACE)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1839 | break; |
| 1840 | default: |
| 1841 | GrCrash("Unknown draw face."); |
| 1842 | } |
| 1843 | fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace; |
| 1844 | } |
| 1845 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1846 | #if GR_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1847 | // check for circular rendering |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1848 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1849 | GrAssert(!this->isStageEnabled(s) || |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1850 | NULL == fCurrDrawState.fRenderTarget || |
| 1851 | NULL == fCurrDrawState.fTextures[s] || |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 1852 | fCurrDrawState.fTextures[s]->asRenderTarget() != |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1853 | fCurrDrawState.fRenderTarget); |
| 1854 | } |
| 1855 | #endif |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 1856 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1857 | flushStencil(); |
| 1858 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1859 | // 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] | 1860 | fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1861 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1865 | if (fHWGeometryState.fVertexBuffer != buffer) { |
| 1866 | fHWGeometryState.fArrayPtrsDirty = true; |
| 1867 | fHWGeometryState.fVertexBuffer = buffer; |
| 1868 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1872 | if (fHWGeometryState.fVertexBuffer == buffer) { |
| 1873 | // deleting bound buffer does implied bind to 0 |
| 1874 | fHWGeometryState.fVertexBuffer = NULL; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1875 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) { |
| 1880 | fGeometrySrc.fIndexBuffer = buffer; |
| 1881 | } |
| 1882 | |
| 1883 | void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1884 | if (fHWGeometryState.fIndexBuffer == buffer) { |
| 1885 | // deleting bound buffer does implied bind to 0 |
| 1886 | fHWGeometryState.fIndexBuffer = NULL; |
| 1887 | } |
| 1888 | } |
| 1889 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1890 | void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) { |
| 1891 | GrAssert(NULL != renderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1892 | if (fCurrDrawState.fRenderTarget == renderTarget) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1893 | fCurrDrawState.fRenderTarget = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1894 | } |
| 1895 | if (fHWDrawState.fRenderTarget == renderTarget) { |
| 1896 | fHWDrawState.fRenderTarget = NULL; |
| 1897 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1901 | for (int s = 0; s < kNumStages; ++s) { |
| 1902 | if (fCurrDrawState.fTextures[s] == texture) { |
| 1903 | fCurrDrawState.fTextures[s] = NULL; |
| 1904 | } |
| 1905 | if (fHWDrawState.fTextures[s] == texture) { |
| 1906 | // deleting bound texture does implied bind to 0 |
| 1907 | fHWDrawState.fTextures[s] = NULL; |
| 1908 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1909 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1912 | bool GrGpuGL::canBeTexture(GrPixelConfig config, |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1913 | GrGLenum* internalFormat, |
| 1914 | GrGLenum* format, |
| 1915 | GrGLenum* type) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1916 | switch (config) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1917 | case kRGBA_8888_GrPixelConfig: |
| 1918 | case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X? |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 1919 | *format = GR_GL_32BPP_COLOR_FORMAT; |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 1920 | if (GR_GL_SUPPORT_ES) { |
| 1921 | // according to GL_EXT_texture_format_BGRA8888 the *internal* |
| 1922 | // format for a BGRA is BGRA not RGBA (as on desktop) |
| 1923 | *internalFormat = GR_GL_32BPP_COLOR_FORMAT; |
| 1924 | } else { |
| 1925 | *internalFormat = GR_GL_RGBA; |
| 1926 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1927 | *type = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1928 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1929 | case kRGB_565_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1930 | *format = GR_GL_RGB; |
| 1931 | *internalFormat = GR_GL_RGB; |
| 1932 | *type = GR_GL_UNSIGNED_SHORT_5_6_5; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1933 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1934 | case kRGBA_4444_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1935 | *format = GR_GL_RGBA; |
| 1936 | *internalFormat = GR_GL_RGBA; |
| 1937 | *type = GR_GL_UNSIGNED_SHORT_4_4_4_4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1938 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1939 | case kIndex_8_GrPixelConfig: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1940 | if (this->supports8BitPalette()) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1941 | *format = GR_GL_PALETTE8_RGBA8; |
| 1942 | *internalFormat = GR_GL_PALETTE8_RGBA8; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1943 | *type = GR_GL_UNSIGNED_BYTE; // unused I think |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1944 | } else { |
| 1945 | return false; |
| 1946 | } |
| 1947 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1948 | case kAlpha_8_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1949 | *format = GR_GL_ALPHA; |
| 1950 | *internalFormat = GR_GL_ALPHA; |
| 1951 | *type = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | default: |
| 1954 | return false; |
| 1955 | } |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1959 | void GrGpuGL::setTextureUnit(int unit) { |
| 1960 | GrAssert(unit >= 0 && unit < kNumStages); |
| 1961 | if (fActiveTextureUnitIdx != unit) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1962 | GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1963 | fActiveTextureUnitIdx = unit; |
| 1964 | } |
| 1965 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 1966 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1967 | void GrGpuGL::setSpareTextureUnit() { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1968 | if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) { |
| 1969 | GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 1970 | fActiveTextureUnitIdx = SPARE_TEX_UNIT; |
| 1971 | } |
| 1972 | } |
| 1973 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1974 | /* On ES the internalFormat and format must match for TexImage and we use |
| 1975 | GL_RGB, GL_RGBA for color formats. We also generally like having the driver |
| 1976 | decide the internalFormat. However, on ES internalFormat for |
| 1977 | RenderBufferStorage* has to be a specific format (not a base format like |
| 1978 | GL_RGBA). |
| 1979 | */ |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1980 | bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1981 | switch (config) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1982 | case kRGBA_8888_GrPixelConfig: |
| 1983 | case kRGBX_8888_GrPixelConfig: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1984 | if (fRGBA8Renderbuffer) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1985 | *format = GR_GL_RGBA8; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1986 | return true; |
| 1987 | } else { |
| 1988 | return false; |
| 1989 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1990 | case kRGB_565_GrPixelConfig: |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 1991 | GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it |
| 1992 | // with FBO extension desktop GL has |
| 1993 | // no such internal format |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1994 | *format = GR_GL_RGB565; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1995 | return true; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1996 | case kRGBA_4444_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1997 | *format = GR_GL_RGBA4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1998 | return true; |
| 1999 | default: |
| 2000 | return false; |
| 2001 | } |
| 2002 | } |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2003 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 2004 | void GrGpuGL::resetDirtyFlags() { |
| 2005 | Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags)); |
| 2006 | } |
| 2007 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2008 | void GrGpuGL::setBuffers(bool indexed, |
| 2009 | int* extraVertexOffset, |
| 2010 | int* extraIndexOffset) { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2011 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2012 | GrAssert(NULL != extraVertexOffset); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2013 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2014 | GrGLVertexBuffer* vbuf; |
| 2015 | switch (fGeometrySrc.fVertexSrc) { |
| 2016 | case kBuffer_GeometrySrcType: |
| 2017 | *extraVertexOffset = 0; |
| 2018 | vbuf = (GrGLVertexBuffer*) fGeometrySrc.fVertexBuffer; |
| 2019 | break; |
| 2020 | case kArray_GeometrySrcType: |
| 2021 | case kReserved_GeometrySrcType: |
| 2022 | finalizeReservedVertices(); |
| 2023 | *extraVertexOffset = fCurrPoolStartVertex; |
| 2024 | vbuf = (GrGLVertexBuffer*) fCurrPoolVertexBuffer; |
| 2025 | break; |
| 2026 | default: |
| 2027 | vbuf = NULL; // suppress warning |
| 2028 | GrCrash("Unknown geometry src type!"); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2031 | GrAssert(NULL != vbuf); |
| 2032 | GrAssert(!vbuf->isLocked()); |
| 2033 | if (fHWGeometryState.fVertexBuffer != vbuf) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2034 | GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID())); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2035 | fHWGeometryState.fArrayPtrsDirty = true; |
| 2036 | fHWGeometryState.fVertexBuffer = vbuf; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2037 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2038 | |
| 2039 | if (indexed) { |
| 2040 | GrAssert(NULL != extraIndexOffset); |
| 2041 | |
| 2042 | GrGLIndexBuffer* ibuf; |
| 2043 | switch (fGeometrySrc.fIndexSrc) { |
| 2044 | case kBuffer_GeometrySrcType: |
| 2045 | *extraIndexOffset = 0; |
| 2046 | ibuf = (GrGLIndexBuffer*)fGeometrySrc.fIndexBuffer; |
| 2047 | break; |
| 2048 | case kArray_GeometrySrcType: |
| 2049 | case kReserved_GeometrySrcType: |
| 2050 | finalizeReservedIndices(); |
| 2051 | *extraIndexOffset = fCurrPoolStartIndex; |
| 2052 | ibuf = (GrGLIndexBuffer*) fCurrPoolIndexBuffer; |
| 2053 | break; |
| 2054 | default: |
| 2055 | ibuf = NULL; // suppress warning |
| 2056 | GrCrash("Unknown geometry src type!"); |
| 2057 | } |
| 2058 | |
| 2059 | GrAssert(NULL != ibuf); |
| 2060 | GrAssert(!ibuf->isLocked()); |
| 2061 | if (fHWGeometryState.fIndexBuffer != ibuf) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2062 | GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID())); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2063 | fHWGeometryState.fIndexBuffer = ibuf; |
| 2064 | } |
| 2065 | } |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2066 | } |