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