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