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