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