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