reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "GrGpuGL.h" |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 10 | #include "GrGLStencilBuffer.h" |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 11 | #include "GrTypes.h" |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 12 | #include "SkTemplates.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 14 | static const GrGLuint GR_MAX_GLUINT = ~0; |
| 15 | static const GrGLint GR_INVAL_GLINT = ~0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 17 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 18 | #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 20 | // we use a spare texture unit to avoid |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 21 | // mucking with the state of any of the stages. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 22 | static const int SPARE_TEX_UNIT = GrDrawState::kNumStages; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 23 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | #define SKIP_CACHE_CHECK true |
| 25 | |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 26 | #if GR_GL_CHECK_ALLOC_WITH_GET_ERROR |
| 27 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface) |
| 28 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call) |
| 29 | #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface) |
| 30 | #else |
| 31 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) |
| 32 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call) |
| 33 | #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR |
| 34 | #endif |
| 35 | |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 39 | static const GrGLenum gXfermodeCoeff2Blend[] = { |
| 40 | GR_GL_ZERO, |
| 41 | GR_GL_ONE, |
| 42 | GR_GL_SRC_COLOR, |
| 43 | GR_GL_ONE_MINUS_SRC_COLOR, |
| 44 | GR_GL_DST_COLOR, |
| 45 | GR_GL_ONE_MINUS_DST_COLOR, |
| 46 | GR_GL_SRC_ALPHA, |
| 47 | GR_GL_ONE_MINUS_SRC_ALPHA, |
| 48 | GR_GL_DST_ALPHA, |
| 49 | GR_GL_ONE_MINUS_DST_ALPHA, |
| 50 | GR_GL_CONSTANT_COLOR, |
| 51 | GR_GL_ONE_MINUS_CONSTANT_COLOR, |
| 52 | GR_GL_CONSTANT_ALPHA, |
| 53 | GR_GL_ONE_MINUS_CONSTANT_ALPHA, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 54 | |
| 55 | // extended blend coeffs |
| 56 | GR_GL_SRC1_COLOR, |
| 57 | GR_GL_ONE_MINUS_SRC1_COLOR, |
| 58 | GR_GL_SRC1_ALPHA, |
| 59 | GR_GL_ONE_MINUS_SRC1_ALPHA, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 62 | bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 63 | static const bool gCoeffReferencesBlendConst[] = { |
| 64 | false, |
| 65 | false, |
| 66 | false, |
| 67 | false, |
| 68 | false, |
| 69 | false, |
| 70 | false, |
| 71 | false, |
| 72 | false, |
| 73 | false, |
| 74 | true, |
| 75 | true, |
| 76 | true, |
| 77 | true, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 78 | |
| 79 | // extended blend coeffs |
| 80 | false, |
| 81 | false, |
| 82 | false, |
| 83 | false, |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 84 | }; |
| 85 | return gCoeffReferencesBlendConst[coeff]; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 86 | GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
| 87 | |
| 88 | GR_STATIC_ASSERT(0 == kZero_BlendCoeff); |
| 89 | GR_STATIC_ASSERT(1 == kOne_BlendCoeff); |
| 90 | GR_STATIC_ASSERT(2 == kSC_BlendCoeff); |
| 91 | GR_STATIC_ASSERT(3 == kISC_BlendCoeff); |
| 92 | GR_STATIC_ASSERT(4 == kDC_BlendCoeff); |
| 93 | GR_STATIC_ASSERT(5 == kIDC_BlendCoeff); |
| 94 | GR_STATIC_ASSERT(6 == kSA_BlendCoeff); |
| 95 | GR_STATIC_ASSERT(7 == kISA_BlendCoeff); |
| 96 | GR_STATIC_ASSERT(8 == kDA_BlendCoeff); |
| 97 | GR_STATIC_ASSERT(9 == kIDA_BlendCoeff); |
| 98 | GR_STATIC_ASSERT(10 == kConstC_BlendCoeff); |
| 99 | GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff); |
| 100 | GR_STATIC_ASSERT(12 == kConstA_BlendCoeff); |
| 101 | GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff); |
| 102 | |
| 103 | GR_STATIC_ASSERT(14 == kS2C_BlendCoeff); |
| 104 | GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff); |
| 105 | GR_STATIC_ASSERT(16 == kS2A_BlendCoeff); |
| 106 | GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff); |
| 107 | |
| 108 | // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope |
| 109 | GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend)); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 114 | static bool gPrintStartupSpew; |
| 115 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 116 | static bool fbo_test(const GrGLInterface* gl, int w, int h) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 118 | GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 119 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 120 | GrGLuint testFBO; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 121 | GR_GL_CALL(gl, GenFramebuffers(1, &testFBO)); |
| 122 | GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO)); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 123 | GrGLuint testRTTex; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 124 | GR_GL_CALL(gl, GenTextures(1, &testRTTex)); |
| 125 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex)); |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 126 | // some implementations require texture to be mip-map complete before |
| 127 | // FBO with level 0 bound as color attachment will be framebuffer complete. |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 128 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 129 | GR_GL_TEXTURE_MIN_FILTER, |
| 130 | GR_GL_NEAREST)); |
| 131 | GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h, |
| 132 | 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL)); |
| 133 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 134 | GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 135 | GR_GL_COLOR_ATTACHMENT0, |
| 136 | GR_GL_TEXTURE_2D, testRTTex, 0)); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 137 | GrGLenum status; |
| 138 | GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 139 | GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO)); |
| 140 | GR_GL_CALL(gl, DeleteTextures(1, &testRTTex)); |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 141 | |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 142 | return status == GR_GL_FRAMEBUFFER_COMPLETE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 145 | GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) { |
| 146 | |
| 147 | GrAssert(ctxInfo.isInitialized()); |
tomhudson@google.com | 747bf29 | 2011-06-14 18:16:52 +0000 | [diff] [blame] | 148 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 149 | fillInConfigRenderableTable(); |
| 150 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 151 | fPrintedCaps = false; |
bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 153 | GrGLClearErr(fGLContextInfo.interface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 155 | if (gPrintStartupSpew) { |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 156 | const GrGLubyte* ext; |
| 157 | GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS)); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 158 | const GrGLubyte* vendor; |
| 159 | const GrGLubyte* renderer; |
| 160 | const GrGLubyte* version; |
| 161 | GL_CALL_RET(vendor, GetString(GR_GL_VENDOR)); |
| 162 | GL_CALL_RET(renderer, GetString(GR_GL_RENDERER)); |
| 163 | GL_CALL_RET(version, GetString(GR_GL_VERSION)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 164 | GrPrintf("------------------------- create GrGpuGL %p --------------\n", |
| 165 | this); |
| 166 | GrPrintf("------ VENDOR %s\n", vendor); |
| 167 | GrPrintf("------ RENDERER %s\n", renderer); |
| 168 | GrPrintf("------ VERSION %s\n", version); |
| 169 | GrPrintf("------ EXTENSIONS\n %s \n", ext); |
| 170 | } |
| 171 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 172 | this->initCaps(); |
tomhudson@google.com | 747bf29 | 2011-06-14 18:16:52 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 174 | fProgramData = NULL; |
| 175 | fProgramCache = new ProgramCache(this->glContextInfo()); |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 176 | |
| 177 | #if 0 |
| 178 | this->programUnitTest(); |
| 179 | #endif |
| 180 | |
bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 181 | fLastSuccessfulStencilFmtIdx = 0; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 182 | fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | GrGpuGL::~GrGpuGL() { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 186 | if (fProgramData && 0 != fHWProgramID) { |
| 187 | // detach the current program so there is no confusion on OpenGL's part |
| 188 | // that we want it to be deleted |
| 189 | GrAssert(fHWProgramID == fProgramData->fProgramID); |
| 190 | GL_CALL(UseProgram(0)); |
| 191 | } |
| 192 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 193 | delete fProgramCache; |
| 194 | fProgramCache = NULL; |
| 195 | fProgramData = NULL; |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 197 | // This must be called by before the GrDrawTarget destructor |
| 198 | this->releaseGeometry(); |
bsalomon@google.com | 15b11df | 2011-09-16 21:18:29 +0000 | [diff] [blame] | 199 | // This subclass must do this before the base class destructor runs |
| 200 | // since we will unref the GrGLInterface. |
| 201 | this->releaseResources(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 202 | } |
| 203 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 204 | /////////////////////////////////////////////////////////////////////////////// |
| 205 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 206 | void GrGpuGL::initCaps() { |
| 207 | GrGLint maxTextureUnits; |
| 208 | // check FS and fixed-function texture unit limits |
| 209 | // we only use textures in the fragment stage currently. |
| 210 | // checks are > to make sure we have a spare unit. |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 211 | const GrGLInterface* gl = this->glInterface(); |
| 212 | GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 213 | GrAssert(maxTextureUnits > GrDrawState::kNumStages); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 214 | |
| 215 | GrGLint numFormats; |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 216 | GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 217 | SkAutoSTMalloc<10, GrGLint> formats(numFormats); |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 218 | GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 219 | for (int i = 0; i < numFormats; ++i) { |
| 220 | if (formats[i] == GR_GL_PALETTE8_RGBA8) { |
| 221 | fCaps.f8BitPaletteSupport = true; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 227 | // we could also look for GL_ATI_separate_stencil extension or |
| 228 | // GL_EXT_stencil_two_side but they use different function signatures |
| 229 | // than GL2.0+ (and than each other). |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 230 | fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0)); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 231 | // supported on GL 1.4 and higher or by extension |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 232 | fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) || |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 233 | this->hasExtension("GL_EXT_stencil_wrap"); |
| 234 | } else { |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 235 | // ES 2 has two sided stencil and stencil wrap |
| 236 | fCaps.fTwoSidedStencilSupport = true; |
| 237 | fCaps.fStencilWrapOpsSupport = true; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 241 | fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO |
| 242 | // extension includes glMapBuffer. |
| 243 | } else { |
| 244 | fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer"); |
| 245 | } |
| 246 | |
| 247 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 248 | if (this->glVersion() >= GR_GL_VER(2,0) || |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 249 | this->hasExtension("GL_ARB_texture_non_power_of_two")) { |
| 250 | fCaps.fNPOTTextureTileSupport = true; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 251 | } else { |
| 252 | fCaps.fNPOTTextureTileSupport = false; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 253 | } |
| 254 | } else { |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 255 | // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 256 | fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot"); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding()); |
| 260 | |
bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 261 | GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize); |
| 262 | GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 263 | // Our render targets are always created with textures as the color |
| 264 | // attachment, hence this min: |
| 265 | fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize); |
| 266 | |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 267 | fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType(); |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 268 | |
| 269 | // Enable supported shader-related caps |
| 270 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 271 | fCaps.fDualSourceBlendingSupport = |
| 272 | this->glVersion() >= GR_GL_VER(3,3) || |
| 273 | this->hasExtension("GL_ARB_blend_func_extended"); |
| 274 | fCaps.fShaderDerivativeSupport = true; |
| 275 | // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS |
| 276 | fCaps.fGeometryShaderSupport = |
| 277 | this->glVersion() >= GR_GL_VER(3,2) && |
| 278 | this->glslGeneration() >= k150_GrGLSLGeneration; |
| 279 | } else { |
| 280 | fCaps.fShaderDerivativeSupport = |
| 281 | this->hasExtension("GL_OES_standard_derivatives"); |
| 282 | } |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 283 | } |
| 284 | |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 285 | void GrGpuGL::fillInConfigRenderableTable() { |
| 286 | |
| 287 | // OpenGL < 3.0 |
| 288 | // no support for render targets unless the GL_ARB_framebuffer_object |
| 289 | // extension is supported (in which case we get ALPHA, RED, RG, RGB, |
| 290 | // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we |
| 291 | // probably don't get R8 in this case. |
| 292 | |
| 293 | // OpenGL 3.0 |
| 294 | // base color renderable: ALPHA, RED, RG, RGB, and RGBA |
| 295 | // sized derivatives: ALPHA8, R8, RGBA4, RGBA8 |
| 296 | |
| 297 | // >= OpenGL 3.1 |
| 298 | // base color renderable: RED, RG, RGB, and RGBA |
| 299 | // sized derivatives: R8, RGBA4, RGBA8 |
| 300 | // if the GL_ARB_compatibility extension is supported then we get back |
| 301 | // support for GL_ALPHA and ALPHA8 |
| 302 | |
| 303 | // GL_EXT_bgra adds BGRA render targets to any version |
| 304 | |
| 305 | // ES 2.0 |
| 306 | // color renderable: RGBA4, RGB5_A1, RGB565 |
| 307 | // GL_EXT_texture_rg adds support for R8 as a color render target |
| 308 | // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8 |
| 309 | // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 |
| 310 | // added BGRA support |
| 311 | |
| 312 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 313 | // Post 3.0 we will get R8 |
| 314 | // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object) |
| 315 | if (this->glVersion() >= GR_GL_VER(3,0) || |
| 316 | this->hasExtension("GL_ARB_framebuffer_object")) { |
| 317 | fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true; |
| 318 | } |
| 319 | } else { |
| 320 | // On ES we can only hope for R8 |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 321 | fConfigRenderSupport[kAlpha_8_GrPixelConfig] = |
| 322 | this->glCaps().textureRedSupport(); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | if (kDesktop_GrGLBinding != this->glBinding()) { |
| 326 | // only available in ES |
| 327 | fConfigRenderSupport[kRGB_565_GrPixelConfig] = true; |
| 328 | } |
| 329 | |
| 330 | // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or |
| 331 | // GL_EXT_framebuffer_object for FBO support. Both of these |
| 332 | // allow RGBA4 render targets so this is always supported. |
| 333 | fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true; |
| 334 | |
| 335 | if (this->glCaps().rgba8RenderbufferSupport()) { |
| 336 | fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true; |
| 337 | } |
| 338 | |
| 339 | if (this->glCaps().bgraFormatSupport()) { |
| 340 | fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true; |
| 341 | } |
| 342 | |
| 343 | // the un-premultiplied formats just inherit the premultiplied setting |
| 344 | fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] = |
| 345 | fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig]; |
| 346 | fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] = |
| 347 | fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig]; |
| 348 | } |
| 349 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 350 | bool GrGpuGL::canPreserveReadWriteUnpremulPixels() { |
| 351 | if (kUnknown_CanPreserveUnpremulRoundtrip == |
| 352 | fCanPreserveUnpremulRoundtrip) { |
| 353 | |
| 354 | SkAutoTMalloc<uint32_t> data(256 * 256 * 3); |
| 355 | uint32_t* srcData = data.get(); |
| 356 | uint32_t* firstRead = data.get() + 256 * 256; |
| 357 | uint32_t* secondRead = data.get() + 2 * 256 * 256; |
| 358 | |
| 359 | for (int y = 0; y < 256; ++y) { |
| 360 | for (int x = 0; x < 256; ++x) { |
| 361 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]); |
| 362 | color[3] = y; |
| 363 | color[2] = x; |
| 364 | color[1] = x; |
| 365 | color[0] = x; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // We have broader support for read/write pixels on render targets |
| 370 | // than on textures. |
| 371 | GrTextureDesc dstDesc; |
| 372 | dstDesc.fFlags = kRenderTarget_GrTextureFlagBit | |
| 373 | kNoStencil_GrTextureFlagBit; |
| 374 | dstDesc.fWidth = 256; |
| 375 | dstDesc.fHeight = 256; |
bsalomon@google.com | b9014f4 | 2012-03-30 14:22:41 +0000 | [diff] [blame] | 376 | dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 377 | dstDesc.fSampleCnt = 0; |
| 378 | |
| 379 | SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0)); |
| 380 | if (!dstTex.get()) { |
| 381 | return false; |
| 382 | } |
| 383 | GrRenderTarget* rt = dstTex.get()->asRenderTarget(); |
| 384 | GrAssert(NULL != rt); |
| 385 | |
| 386 | bool failed = true; |
| 387 | static const UnpremulConversion gMethods[] = { |
| 388 | kUpOnWrite_DownOnRead_UnpremulConversion, |
| 389 | kDownOnWrite_UpOnRead_UnpremulConversion, |
| 390 | }; |
| 391 | |
| 392 | // pretend that we can do the roundtrip to avoid recursive calls to |
| 393 | // this function |
| 394 | fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip; |
| 395 | for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) { |
| 396 | fUnpremulConversion = gMethods[i]; |
| 397 | rt->writePixels(0, 0, |
| 398 | 256, 256, |
| 399 | kRGBA_8888_UPM_GrPixelConfig, srcData, 0); |
| 400 | rt->readPixels(0, 0, |
| 401 | 256, 256, |
| 402 | kRGBA_8888_UPM_GrPixelConfig, firstRead, 0); |
| 403 | rt->writePixels(0, 0, |
| 404 | 256, 256, |
| 405 | kRGBA_8888_UPM_GrPixelConfig, firstRead, 0); |
| 406 | rt->readPixels(0, 0, |
| 407 | 256, 256, |
| 408 | kRGBA_8888_UPM_GrPixelConfig, secondRead, 0); |
| 409 | failed = false; |
| 410 | for (int j = 0; j < 256 * 256; ++j) { |
| 411 | if (firstRead[j] != secondRead[j]) { |
| 412 | failed = true; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | fCanPreserveUnpremulRoundtrip = failed ? |
| 418 | kNo_CanPreserveUnpremulRoundtrip : |
| 419 | kYes_CanPreserveUnpremulRoundtrip; |
| 420 | } |
| 421 | |
| 422 | if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) { |
| 423 | return true; |
| 424 | } else { |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 429 | GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const { |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 430 | if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) { |
| 431 | return GrPixelConfigSwapRAndB(config); |
| 432 | } else { |
| 433 | return config; |
| 434 | } |
| 435 | } |
| 436 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 437 | GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const { |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 438 | if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) { |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 439 | return GrPixelConfigSwapRAndB(config); |
| 440 | } else { |
| 441 | return config; |
| 442 | } |
| 443 | } |
| 444 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 445 | bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const { |
| 446 | return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL); |
| 447 | } |
| 448 | |
bsalomon@google.com | 1bf1c21 | 2011-11-05 12:18:58 +0000 | [diff] [blame] | 449 | void GrGpuGL::onResetContext() { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 450 | if (gPrintStartupSpew && !fPrintedCaps) { |
| 451 | fPrintedCaps = true; |
| 452 | this->getCaps().print(); |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 453 | this->glCaps().print(); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 454 | } |
| 455 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 456 | // we don't use the zb at all |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 457 | GL_CALL(Disable(GR_GL_DEPTH_TEST)); |
| 458 | GL_CALL(DepthMask(GR_GL_FALSE)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 459 | |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 460 | fHWDrawFace = GrDrawState::kInvalid_DrawFace; |
| 461 | fHWDitherEnabled = kUnknown_TriState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 462 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 463 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 469d0dd | 2012-05-21 20:14:29 +0000 | [diff] [blame] | 464 | // Desktop-only state that we never change |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 465 | GL_CALL(Disable(GR_GL_POINT_SMOOTH)); |
bsalomon@google.com | 469d0dd | 2012-05-21 20:14:29 +0000 | [diff] [blame] | 466 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
| 467 | GL_CALL(Disable(GR_GL_POLYGON_SMOOTH)); |
| 468 | GL_CALL(Disable(GR_GL_POLYGON_STIPPLE)); |
| 469 | GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP)); |
| 470 | GL_CALL(Disable(GR_GL_COLOR_TABLE)); |
| 471 | GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP)); |
| 472 | GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL)); |
| 473 | // Since ES doesn't support glPointSize at all we always use the VS to |
| 474 | // set the point size |
| 475 | GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE)); |
| 476 | |
| 477 | // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't |
| 478 | // currently part of our gl interface. There are probably others as |
| 479 | // well. |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 480 | } |
bsalomon@google.com | 4d5f3fe | 2012-05-21 17:11:44 +0000 | [diff] [blame] | 481 | fHWAAState.invalidate(); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 482 | fHWWriteToColor = kUnknown_TriState; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 483 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | // we only ever use lines in hairline mode |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 485 | GL_CALL(LineWidth(1)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 487 | // invalid |
| 488 | fActiveTextureUnitIdx = -1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 489 | |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 490 | fHWBlendState.invalidate(); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 491 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 492 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 493 | fHWBoundTextures[s] = NULL; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 494 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 495 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 496 | fHWBounds.fScissorRect.invalidate(); |
bsalomon@google.com | b72f203 | 2012-04-17 19:29:51 +0000 | [diff] [blame] | 497 | // set to true to force disableScissor to make a GL call. |
| 498 | fHWBounds.fScissorEnabled = true; |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 499 | this->disableScissor(); |
| 500 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 501 | fHWBounds.fViewportRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 502 | |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 503 | fHWStencilSettings.invalidate(); |
| 504 | fHWStencilClipMode = kInvalid_StencilClipMode; |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 505 | |
| 506 | // TODO: I believe this should actually go in GrGpu::onResetContext |
| 507 | // rather than here |
| 508 | fClipMaskManager.resetMask(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 509 | |
| 510 | fHWGeometryState.fIndexBuffer = NULL; |
| 511 | fHWGeometryState.fVertexBuffer = NULL; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 512 | |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 513 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 514 | |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 515 | fHWBoundRenderTarget = NULL; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 516 | |
| 517 | // we assume these values |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 518 | if (this->glCaps().unpackRowLengthSupport()) { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 519 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 520 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 521 | if (this->glCaps().packRowLengthSupport()) { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 522 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 523 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 524 | if (this->glCaps().unpackFlipYSupport()) { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 525 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 526 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 527 | if (this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 528 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE)); |
| 529 | } |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 530 | |
| 531 | fHWGeometryState.fVertexOffset = ~0; |
| 532 | |
| 533 | // Third party GL code may have left vertex attributes enabled. Some GL |
| 534 | // implementations (osmesa) may read vetex attributes that are not required |
| 535 | // by the current shader. Therefore, we have to ensure that only the |
| 536 | // attributes we require for the current draw are enabled or we may cause an |
| 537 | // invalid read. |
| 538 | |
| 539 | // Disable all vertex layout bits so that next flush will assume all |
| 540 | // optional vertex attributes are disabled. |
| 541 | fHWGeometryState.fVertexLayout = 0; |
| 542 | |
| 543 | // We always use the this attribute and assume it is always enabled. |
| 544 | int posAttrIdx = GrGLProgram::PositionAttributeIdx(); |
| 545 | GL_CALL(EnableVertexAttribArray(posAttrIdx)); |
| 546 | // Disable all other vertex attributes. |
bsalomon@google.com | 60da417 | 2012-06-01 19:25:00 +0000 | [diff] [blame^] | 547 | for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 548 | if (va != posAttrIdx) { |
| 549 | GL_CALL(DisableVertexAttribArray(va)); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | fHWProgramID = 0; |
| 554 | fHWConstAttribColor = GrColor_ILLEGAL; |
| 555 | fHWConstAttribCoverage = GrColor_ILLEGAL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 556 | } |
| 557 | |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 558 | GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 559 | GrGLTexture::Desc glTexDesc; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 560 | if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 561 | return NULL; |
| 562 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 563 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 564 | glTexDesc.fWidth = desc.fWidth; |
| 565 | glTexDesc.fHeight = desc.fHeight; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 566 | glTexDesc.fConfig = desc.fConfig; |
| 567 | glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle); |
| 568 | glTexDesc.fOwnsID = false; |
| 569 | glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation; |
| 570 | |
| 571 | GrGLTexture* texture = NULL; |
| 572 | if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) { |
| 573 | GrGLRenderTarget::Desc glRTDesc; |
| 574 | glRTDesc.fRTFBOID = 0; |
| 575 | glRTDesc.fTexFBOID = 0; |
| 576 | glRTDesc.fMSColorRenderbufferID = 0; |
| 577 | glRTDesc.fOwnIDs = true; |
| 578 | glRTDesc.fConfig = desc.fConfig; |
| 579 | glRTDesc.fSampleCnt = desc.fSampleCnt; |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 580 | if (!this->createRenderTargetObjects(glTexDesc.fWidth, |
| 581 | glTexDesc.fHeight, |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 582 | glTexDesc.fTextureID, |
| 583 | &glRTDesc)) { |
| 584 | return NULL; |
| 585 | } |
| 586 | texture = new GrGLTexture(this, glTexDesc, glRTDesc); |
| 587 | } else { |
| 588 | texture = new GrGLTexture(this, glTexDesc); |
| 589 | } |
| 590 | if (NULL == texture) { |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | this->setSpareTextureUnit(); |
| 595 | return texture; |
| 596 | } |
| 597 | |
| 598 | GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) { |
| 599 | GrGLRenderTarget::Desc glDesc; |
| 600 | glDesc.fConfig = desc.fConfig; |
| 601 | glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle); |
| 602 | glDesc.fMSColorRenderbufferID = 0; |
| 603 | glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
| 604 | glDesc.fSampleCnt = desc.fSampleCnt; |
| 605 | glDesc.fOwnIDs = false; |
| 606 | GrGLIRect viewport; |
| 607 | viewport.fLeft = 0; |
| 608 | viewport.fBottom = 0; |
| 609 | viewport.fWidth = desc.fWidth; |
| 610 | viewport.fHeight = desc.fHeight; |
| 611 | |
| 612 | GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport); |
| 613 | if (desc.fStencilBits) { |
| 614 | GrGLStencilBuffer::Format format; |
| 615 | format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat; |
| 616 | format.fPacked = false; |
| 617 | format.fStencilBits = desc.fStencilBits; |
| 618 | format.fTotalBits = desc.fStencilBits; |
| 619 | GrGLStencilBuffer* sb = new GrGLStencilBuffer(this, |
| 620 | 0, |
| 621 | desc.fWidth, |
| 622 | desc.fHeight, |
| 623 | desc.fSampleCnt, |
| 624 | format); |
| 625 | tgt->setStencilBuffer(sb); |
| 626 | sb->unref(); |
| 627 | } |
| 628 | return tgt; |
| 629 | } |
| 630 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 631 | //////////////////////////////////////////////////////////////////////////////// |
| 632 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 633 | void GrGpuGL::onWriteTexturePixels(GrTexture* texture, |
| 634 | int left, int top, int width, int height, |
| 635 | GrPixelConfig config, const void* buffer, |
| 636 | size_t rowBytes) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 637 | if (NULL == buffer) { |
| 638 | return; |
| 639 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 640 | GrGLTexture* glTex = static_cast<GrGLTexture*>(texture); |
| 641 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 642 | this->setSpareTextureUnit(); |
| 643 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID())); |
| 644 | GrGLTexture::Desc desc; |
| 645 | desc.fConfig = glTex->config(); |
| 646 | desc.fWidth = glTex->width(); |
| 647 | desc.fHeight = glTex->height(); |
| 648 | desc.fOrientation = glTex->orientation(); |
| 649 | desc.fTextureID = glTex->textureID(); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 650 | |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 651 | this->uploadTexData(desc, false, |
| 652 | left, top, width, height, |
| 653 | config, buffer, rowBytes); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 654 | } |
| 655 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 656 | namespace { |
| 657 | bool adjust_pixel_ops_params(int surfaceWidth, |
| 658 | int surfaceHeight, |
| 659 | size_t bpp, |
| 660 | int* left, int* top, int* width, int* height, |
| 661 | const void** data, |
| 662 | size_t* rowBytes) { |
| 663 | if (!*rowBytes) { |
| 664 | *rowBytes = *width * bpp; |
| 665 | } |
| 666 | |
| 667 | GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height); |
| 668 | GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 669 | |
| 670 | if (!subRect.intersect(bounds)) { |
| 671 | return false; |
| 672 | } |
| 673 | *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) + |
| 674 | (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp); |
| 675 | |
| 676 | *left = subRect.fLeft; |
| 677 | *top = subRect.fTop; |
| 678 | *width = subRect.width(); |
| 679 | *height = subRect.height(); |
| 680 | return true; |
| 681 | } |
| 682 | } |
| 683 | |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 684 | bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc, |
| 685 | bool isNewTexture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 686 | int left, int top, int width, int height, |
| 687 | GrPixelConfig dataConfig, |
| 688 | const void* data, |
| 689 | size_t rowBytes) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 690 | GrAssert(NULL != data || isNewTexture); |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 691 | |
| 692 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 693 | if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 694 | &width, &height, &data, &rowBytes)) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 695 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 696 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 697 | size_t trimRowBytes = width * bpp; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 698 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 699 | // in case we need a temporary, trimmed copy of the src pixels |
| 700 | SkAutoSMalloc<128 * 128> tempStorage; |
| 701 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 702 | bool useTexStorage = isNewTexture && |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 703 | this->glCaps().texStorageSupport(); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 704 | if (useTexStorage) { |
| 705 | if (kDesktop_GrGLBinding == this->glBinding()) { |
| 706 | // 565 is not a sized internal format on desktop GL. So on desktop |
| 707 | // with 565 we always use an unsized internal format to let the |
| 708 | // system pick the best sized format to convert the 565 data to. |
| 709 | // Since glTexStorage only allows sized internal formats we will |
| 710 | // instead fallback to glTexImage2D. |
| 711 | useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig; |
| 712 | } else { |
| 713 | // ES doesn't allow paletted textures to be used with tex storage |
| 714 | useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | GrGLenum internalFormat; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 719 | GrGLenum externalFormat; |
| 720 | GrGLenum externalType; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 721 | // glTexStorage requires sized internal formats on both desktop and ES. ES |
| 722 | // glTexImage requires an unsized format. |
| 723 | if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat, |
| 724 | &externalFormat, &externalType)) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 725 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 726 | } |
| 727 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 728 | if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) { |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 729 | // paletted textures cannot be updated |
| 730 | return false; |
| 731 | } |
| 732 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 733 | /* |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 734 | * check whether to allocate a temporary buffer for flipping y or |
| 735 | * because our srcData has extra bytes past each row. If so, we need |
| 736 | * to trim those off here, since GL ES may not let us specify |
| 737 | * GL_UNPACK_ROW_LENGTH. |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 738 | */ |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 739 | bool restoreGLRowLength = false; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 740 | bool swFlipY = false; |
| 741 | bool glFlipY = false; |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 742 | if (NULL != data) { |
| 743 | if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 744 | if (this->glCaps().unpackFlipYSupport()) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 745 | glFlipY = true; |
| 746 | } else { |
| 747 | swFlipY = true; |
| 748 | } |
| 749 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 750 | if (this->glCaps().unpackRowLengthSupport() && !swFlipY) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 751 | // can't use this for flipping, only non-neg values allowed. :( |
| 752 | if (rowBytes != trimRowBytes) { |
| 753 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
| 754 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
| 755 | restoreGLRowLength = true; |
| 756 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 757 | } else { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 758 | if (trimRowBytes != rowBytes || swFlipY) { |
| 759 | // copy data into our new storage, skipping the trailing bytes |
| 760 | size_t trimSize = height * trimRowBytes; |
| 761 | const char* src = (const char*)data; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 762 | if (swFlipY) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 763 | src += (height - 1) * rowBytes; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 764 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 765 | char* dst = (char*)tempStorage.reset(trimSize); |
| 766 | for (int y = 0; y < height; y++) { |
| 767 | memcpy(dst, src, trimRowBytes); |
| 768 | if (swFlipY) { |
| 769 | src -= rowBytes; |
| 770 | } else { |
| 771 | src += rowBytes; |
| 772 | } |
| 773 | dst += trimRowBytes; |
| 774 | } |
| 775 | // now point data to our copied version |
| 776 | data = tempStorage.get(); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 777 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 778 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 779 | if (glFlipY) { |
| 780 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE)); |
| 781 | } |
| 782 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp))); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 783 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 784 | bool succeeded = true; |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 785 | if (isNewTexture && |
| 786 | 0 == left && 0 == top && |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 787 | desc.fWidth == width && desc.fHeight == height) { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 788 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 789 | if (useTexStorage) { |
| 790 | // We never resize or change formats of textures. We don't use |
| 791 | // mipmaps currently. |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 792 | GL_ALLOC_CALL(this->glInterface(), |
| 793 | TexStorage2D(GR_GL_TEXTURE_2D, |
| 794 | 1, // levels |
| 795 | internalFormat, |
| 796 | desc.fWidth, desc.fHeight)); |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 797 | } else { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 798 | if (GR_GL_PALETTE8_RGBA8 == internalFormat) { |
| 799 | GrGLsizei imageSize = desc.fWidth * desc.fHeight + |
| 800 | kGrColorTableSize; |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 801 | GL_ALLOC_CALL(this->glInterface(), |
| 802 | CompressedTexImage2D(GR_GL_TEXTURE_2D, |
| 803 | 0, // level |
| 804 | internalFormat, |
| 805 | desc.fWidth, desc.fHeight, |
| 806 | 0, // border |
| 807 | imageSize, |
| 808 | data)); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 809 | } else { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 810 | GL_ALLOC_CALL(this->glInterface(), |
| 811 | TexImage2D(GR_GL_TEXTURE_2D, |
| 812 | 0, // level |
| 813 | internalFormat, |
| 814 | desc.fWidth, desc.fHeight, |
| 815 | 0, // border |
| 816 | externalFormat, externalType, |
| 817 | data)); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 818 | } |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 819 | } |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 820 | GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface()); |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 821 | if (error != GR_GL_NO_ERROR) { |
| 822 | succeeded = false; |
| 823 | } else { |
| 824 | // if we have data and we used TexStorage to create the texture, we |
| 825 | // now upload with TexSubImage. |
| 826 | if (NULL != data && useTexStorage) { |
| 827 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, |
| 828 | 0, // level |
| 829 | left, top, |
| 830 | width, height, |
| 831 | externalFormat, externalType, |
| 832 | data)); |
| 833 | } |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 834 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 835 | } else { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 836 | if (swFlipY || glFlipY) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 837 | top = desc.fHeight - (top + height); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 838 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 839 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, |
| 840 | 0, // level |
| 841 | left, top, |
| 842 | width, height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 843 | externalFormat, externalType, data)); |
| 844 | } |
| 845 | |
| 846 | if (restoreGLRowLength) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 847 | GrAssert(this->glCaps().unpackRowLengthSupport()); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 848 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 849 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 850 | if (glFlipY) { |
| 851 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 852 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 853 | return succeeded; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 854 | } |
| 855 | |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 856 | namespace { |
| 857 | bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo, |
| 858 | int sampleCount, |
| 859 | GrGLenum format, |
| 860 | int width, int height) { |
| 861 | CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface()); |
| 862 | GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType()); |
| 863 | bool created = false; |
| 864 | if (GrGLCaps::kNVDesktop_CoverageAAType == |
| 865 | ctxInfo.caps().coverageAAType()) { |
| 866 | const GrGLCaps::MSAACoverageMode& mode = |
| 867 | ctxInfo.caps().getMSAACoverageMode(sampleCount); |
| 868 | GL_ALLOC_CALL(ctxInfo.interface(), |
| 869 | RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER, |
| 870 | mode.fCoverageSampleCnt, |
| 871 | mode.fColorSampleCnt, |
| 872 | format, |
| 873 | width, height)); |
| 874 | created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface())); |
| 875 | } |
| 876 | if (!created) { |
bsalomon@google.com | f6b070d | 2012-04-27 14:25:44 +0000 | [diff] [blame] | 877 | // glRBMS will fail if requested samples is > max samples. |
| 878 | sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount()); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 879 | GL_ALLOC_CALL(ctxInfo.interface(), |
| 880 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 881 | sampleCount, |
| 882 | format, |
| 883 | width, height)); |
| 884 | created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface())); |
| 885 | } |
| 886 | return created; |
| 887 | } |
| 888 | } |
| 889 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 890 | bool GrGpuGL::createRenderTargetObjects(int width, int height, |
| 891 | GrGLuint texID, |
| 892 | GrGLRenderTarget::Desc* desc) { |
| 893 | desc->fMSColorRenderbufferID = 0; |
| 894 | desc->fRTFBOID = 0; |
| 895 | desc->fTexFBOID = 0; |
| 896 | desc->fOwnIDs = true; |
| 897 | |
| 898 | GrGLenum status; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 899 | |
bsalomon@google.com | ab15d61 | 2011-08-09 12:57:56 +0000 | [diff] [blame] | 900 | GrGLenum msColorFormat = 0; // suppress warning |
| 901 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 902 | GL_CALL(GenFramebuffers(1, &desc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 903 | if (!desc->fTexFBOID) { |
| 904 | goto FAILED; |
| 905 | } |
| 906 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 907 | |
| 908 | // If we are using multisampling we will create two FBOS. We render |
| 909 | // to one and then resolve to the texture bound to the other. |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 910 | if (desc->fSampleCnt > 0) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 911 | if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 912 | goto FAILED; |
| 913 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 914 | GL_CALL(GenFramebuffers(1, &desc->fRTFBOID)); |
| 915 | GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 916 | if (!desc->fRTFBOID || |
| 917 | !desc->fMSColorRenderbufferID || |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 918 | !this->configToGLFormats(desc->fConfig, |
| 919 | // GLES requires sized internal formats |
| 920 | kES2_GrGLBinding == this->glBinding(), |
| 921 | &msColorFormat, NULL, NULL)) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 922 | goto FAILED; |
| 923 | } |
| 924 | } else { |
| 925 | desc->fRTFBOID = desc->fTexFBOID; |
| 926 | } |
| 927 | |
bsalomon@google.com | 0e9b41a | 2012-01-04 22:11:43 +0000 | [diff] [blame] | 928 | // below here we may bind the FBO |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 929 | fHWBoundRenderTarget = NULL; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 930 | if (desc->fRTFBOID != desc->fTexFBOID) { |
| 931 | GrAssert(desc->fSampleCnt > 1); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 932 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 933 | desc->fMSColorRenderbufferID)); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 934 | if (!renderbuffer_storage_msaa(fGLContextInfo, |
| 935 | desc->fSampleCnt, |
| 936 | msColorFormat, |
| 937 | width, height)) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 938 | goto FAILED; |
| 939 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 940 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID)); |
| 941 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 942 | GR_GL_COLOR_ATTACHMENT0, |
| 943 | GR_GL_RENDERBUFFER, |
| 944 | desc->fMSColorRenderbufferID)); |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 945 | if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) { |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 946 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 947 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 948 | goto FAILED; |
| 949 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 950 | fGLContextInfo.caps().markConfigAsValidColorAttachment( |
| 951 | desc->fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 954 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 955 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 956 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 957 | GR_GL_COLOR_ATTACHMENT0, |
| 958 | GR_GL_TEXTURE_2D, |
| 959 | texID, 0)); |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 960 | if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) { |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 961 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 962 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 963 | goto FAILED; |
| 964 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 965 | fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | return true; |
| 969 | |
| 970 | FAILED: |
| 971 | if (desc->fMSColorRenderbufferID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 972 | GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 973 | } |
| 974 | if (desc->fRTFBOID != desc->fTexFBOID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 975 | GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 976 | } |
| 977 | if (desc->fTexFBOID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 978 | GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 979 | } |
| 980 | return false; |
| 981 | } |
| 982 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 983 | // good to set a break-point here to know when createTexture fails |
| 984 | static GrTexture* return_null_texture() { |
| 985 | // GrAssert(!"null texture"); |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | #if GR_DEBUG |
| 990 | static size_t as_size_t(int x) { |
| 991 | return x; |
| 992 | } |
| 993 | #endif |
| 994 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 995 | GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc, |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 996 | const void* srcData, |
| 997 | size_t rowBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 998 | |
| 999 | #if GR_COLLECT_STATS |
| 1000 | ++fStats.fTextureCreateCnt; |
| 1001 | #endif |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 1002 | |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 1003 | GrGLTexture::Desc glTexDesc; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1004 | GrGLRenderTarget::Desc glRTDesc; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1005 | |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 1006 | // Attempt to catch un- or wrongly initialized sample counts; |
| 1007 | GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64); |
| 1008 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 1009 | glTexDesc.fWidth = desc.fWidth; |
| 1010 | glTexDesc.fHeight = desc.fHeight; |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 1011 | glTexDesc.fConfig = desc.fConfig; |
| 1012 | glTexDesc.fOwnsID = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1013 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1014 | glRTDesc.fMSColorRenderbufferID = 0; |
| 1015 | glRTDesc.fRTFBOID = 0; |
| 1016 | glRTDesc.fTexFBOID = 0; |
| 1017 | glRTDesc.fOwnIDs = true; |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 1018 | glRTDesc.fConfig = glTexDesc.fConfig; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1019 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1020 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1021 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1022 | const Caps& caps = this->getCaps(); |
| 1023 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 1024 | // We keep GrRenderTargets in GL's normal orientation so that they |
| 1025 | // can be drawn to by the outside world without the client having |
| 1026 | // to render upside down. |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 1027 | glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation : |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1028 | GrGLTexture::kTopDown_Orientation; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 1029 | |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 1030 | glRTDesc.fSampleCnt = desc.fSampleCnt; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1031 | if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 1032 | desc.fSampleCnt) { |
| 1033 | GrPrintf("MSAA RT requested but not supported on this platform."); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1036 | if (renderTarget) { |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 1037 | if (glTexDesc.fWidth > caps.fMaxRenderTargetSize || |
| 1038 | glTexDesc.fHeight > caps.fMaxRenderTargetSize) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1039 | return return_null_texture(); |
| 1040 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1043 | GL_CALL(GenTextures(1, &glTexDesc.fTextureID)); |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1044 | if (renderTarget && this->glCaps().textureUsageSupport()) { |
bsalomon@google.com | 07dd2bf | 2011-12-09 19:40:36 +0000 | [diff] [blame] | 1045 | // provides a hint about how this texture will be used |
| 1046 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1047 | GR_GL_TEXTURE_USAGE, |
| 1048 | GR_GL_FRAMEBUFFER_ATTACHMENT)); |
| 1049 | } |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 1050 | if (!glTexDesc.fTextureID) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 1051 | return return_null_texture(); |
| 1052 | } |
| 1053 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1054 | this->setSpareTextureUnit(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1055 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID)); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 1056 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1057 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1058 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1059 | // texture that is not mipmap complete (considering the filter in use). |
| 1060 | GrGLTexture::TexParams initialTexParams; |
| 1061 | // we only set a subset here so invalidate first |
| 1062 | initialTexParams.invalidate(); |
| 1063 | initialTexParams.fFilter = GR_GL_NEAREST; |
| 1064 | initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1065 | initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1066 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1067 | GR_GL_TEXTURE_MAG_FILTER, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1068 | initialTexParams.fFilter)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1069 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1070 | GR_GL_TEXTURE_MIN_FILTER, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1071 | initialTexParams.fFilter)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1072 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1073 | GR_GL_TEXTURE_WRAP_S, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1074 | initialTexParams.fWrapS)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1075 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1076 | GR_GL_TEXTURE_WRAP_T, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1077 | initialTexParams.fWrapT)); |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1078 | if (!this->uploadTexData(glTexDesc, true, 0, 0, |
| 1079 | glTexDesc.fWidth, glTexDesc.fHeight, |
| 1080 | desc.fConfig, srcData, rowBytes)) { |
| 1081 | GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID)); |
| 1082 | return return_null_texture(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1083 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1084 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1085 | GrGLTexture* tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1086 | if (renderTarget) { |
| 1087 | #if GR_COLLECT_STATS |
| 1088 | ++fStats.fRenderTargetCreateCnt; |
| 1089 | #endif |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1090 | // unbind the texture from the texture unit before binding it to the frame buffer |
| 1091 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 1092 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 1093 | if (!this->createRenderTargetObjects(glTexDesc.fWidth, |
| 1094 | glTexDesc.fHeight, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1095 | glTexDesc.fTextureID, |
| 1096 | &glRTDesc)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1097 | GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1098 | return return_null_texture(); |
| 1099 | } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 1100 | tex = new GrGLTexture(this, glTexDesc, glRTDesc); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1101 | } else { |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 1102 | tex = new GrGLTexture(this, glTexDesc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1103 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1104 | tex->setCachedTexParams(initialTexParams, this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1105 | #ifdef TRACE_TEXTURE_CREATION |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 1106 | GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n", |
| 1107 | glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1108 | #endif |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1109 | return tex; |
| 1110 | } |
| 1111 | |
| 1112 | namespace { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1113 | |
| 1114 | const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount; |
| 1115 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1116 | void inline get_stencil_rb_sizes(const GrGLInterface* gl, |
| 1117 | GrGLuint rb, |
| 1118 | GrGLStencilBuffer::Format* format) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1119 | // we shouldn't ever know one size and not the other |
| 1120 | GrAssert((kUnknownBitCount == format->fStencilBits) == |
| 1121 | (kUnknownBitCount == format->fTotalBits)); |
| 1122 | if (kUnknownBitCount == format->fStencilBits) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1123 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1124 | GR_GL_RENDERBUFFER_STENCIL_SIZE, |
| 1125 | (GrGLint*)&format->fStencilBits); |
| 1126 | if (format->fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1127 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1128 | GR_GL_RENDERBUFFER_DEPTH_SIZE, |
| 1129 | (GrGLint*)&format->fTotalBits); |
| 1130 | format->fTotalBits += format->fStencilBits; |
| 1131 | } else { |
| 1132 | format->fTotalBits = format->fStencilBits; |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt, |
| 1139 | int width, int height) { |
| 1140 | |
| 1141 | // All internally created RTs are also textures. We don't create |
| 1142 | // SBs for a client's standalone RT (that is RT that isnt also a texture). |
| 1143 | GrAssert(rt->asTexture()); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 1144 | GrAssert(width >= rt->width()); |
| 1145 | GrAssert(height >= rt->height()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1146 | |
| 1147 | int samples = rt->numSamples(); |
| 1148 | GrGLuint sbID; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1149 | GL_CALL(GenRenderbuffers(1, &sbID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1150 | if (!sbID) { |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | GrGLStencilBuffer* sb = NULL; |
| 1155 | |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1156 | int stencilFmtCnt = this->glCaps().stencilFormats().count(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1157 | for (int i = 0; i < stencilFmtCnt; ++i) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1158 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1159 | // we start with the last stencil format that succeeded in hopes |
| 1160 | // that we won't go through this loop more than once after the |
| 1161 | // first (painful) stencil creation. |
| 1162 | int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1163 | const GrGLCaps::StencilFormat& sFmt = |
| 1164 | this->glCaps().stencilFormats()[sIdx]; |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1165 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1166 | // we do this "if" so that we don't call the multisample |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1167 | // version on a GL that doesn't have an MSAA extension. |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1168 | bool created; |
| 1169 | if (samples > 0) { |
| 1170 | created = renderbuffer_storage_msaa(fGLContextInfo, |
| 1171 | samples, |
| 1172 | sFmt.fInternalFormat, |
| 1173 | width, height); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1174 | } else { |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1175 | GL_ALLOC_CALL(this->glInterface(), |
| 1176 | RenderbufferStorage(GR_GL_RENDERBUFFER, |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1177 | sFmt.fInternalFormat, |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1178 | width, height)); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1179 | created = |
| 1180 | (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1181 | } |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1182 | if (created) { |
| 1183 | // After sized formats we attempt an unsized format and take |
| 1184 | // whatever sizes GL gives us. In that case we query for the size. |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1185 | GrGLStencilBuffer::Format format = sFmt; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1186 | get_stencil_rb_sizes(this->glInterface(), sbID, &format); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1187 | sb = new GrGLStencilBuffer(this, sbID, width, height, |
| 1188 | samples, format); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1189 | if (this->attachStencilBufferToRenderTarget(sb, rt)) { |
| 1190 | fLastSuccessfulStencilFmtIdx = sIdx; |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1191 | rt->setStencilBuffer(sb); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1192 | sb->unref(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1193 | return true; |
| 1194 | } |
| 1195 | sb->abandon(); // otherwise we lose sbID |
| 1196 | sb->unref(); |
| 1197 | } |
| 1198 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1199 | GL_CALL(DeleteRenderbuffers(1, &sbID)); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 1200 | return false; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, |
| 1204 | GrRenderTarget* rt) { |
| 1205 | GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt; |
| 1206 | |
| 1207 | GrGLuint fbo = glrt->renderFBOID(); |
| 1208 | |
| 1209 | if (NULL == sb) { |
| 1210 | if (NULL != rt->getStencilBuffer()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1211 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1212 | GR_GL_STENCIL_ATTACHMENT, |
| 1213 | GR_GL_RENDERBUFFER, 0)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1214 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1215 | GR_GL_DEPTH_ATTACHMENT, |
| 1216 | GR_GL_RENDERBUFFER, 0)); |
| 1217 | #if GR_DEBUG |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1218 | GrGLenum status; |
| 1219 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1220 | GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status); |
| 1221 | #endif |
| 1222 | } |
| 1223 | return true; |
| 1224 | } else { |
| 1225 | GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb; |
| 1226 | GrGLuint rb = glsb->renderbufferID(); |
| 1227 | |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 1228 | fHWBoundRenderTarget = NULL; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1229 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo)); |
| 1230 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1231 | GR_GL_STENCIL_ATTACHMENT, |
| 1232 | GR_GL_RENDERBUFFER, rb)); |
| 1233 | if (glsb->format().fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1234 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1235 | GR_GL_DEPTH_ATTACHMENT, |
| 1236 | GR_GL_RENDERBUFFER, rb)); |
| 1237 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1238 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1239 | GR_GL_DEPTH_ATTACHMENT, |
| 1240 | GR_GL_RENDERBUFFER, 0)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1241 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1242 | |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1243 | GrGLenum status; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1244 | if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1245 | glsb->format())) { |
| 1246 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1247 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1248 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1249 | GR_GL_STENCIL_ATTACHMENT, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1250 | GR_GL_RENDERBUFFER, 0)); |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1251 | if (glsb->format().fPacked) { |
| 1252 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1253 | GR_GL_DEPTH_ATTACHMENT, |
| 1254 | GR_GL_RENDERBUFFER, 0)); |
| 1255 | } |
| 1256 | return false; |
| 1257 | } else { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1258 | fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified( |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1259 | rt->config(), |
| 1260 | glsb->format()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1261 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1262 | } |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1263 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1264 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1267 | //////////////////////////////////////////////////////////////////////////////// |
| 1268 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1269 | GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1270 | GrGLuint id; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1271 | GL_CALL(GenBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1272 | if (id) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1273 | GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id)); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1274 | fHWGeometryState.fArrayPtrsDirty = true; |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1275 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1276 | // make sure driver can allocate memory for this buffer |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1277 | GL_ALLOC_CALL(this->glInterface(), |
| 1278 | BufferData(GR_GL_ARRAY_BUFFER, |
| 1279 | size, |
| 1280 | NULL, // data ptr |
| 1281 | dynamic ? GR_GL_DYNAMIC_DRAW : |
| 1282 | GR_GL_STATIC_DRAW)); |
| 1283 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1284 | GL_CALL(DeleteBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1285 | // deleting bound buffer does implicit bind to 0 |
| 1286 | fHWGeometryState.fVertexBuffer = NULL; |
| 1287 | return NULL; |
| 1288 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1289 | GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1290 | size, dynamic); |
| 1291 | fHWGeometryState.fVertexBuffer = vertexBuffer; |
| 1292 | return vertexBuffer; |
| 1293 | } |
| 1294 | return NULL; |
| 1295 | } |
| 1296 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1297 | GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1298 | GrGLuint id; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1299 | GL_CALL(GenBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1300 | if (id) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1301 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id)); |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1302 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1303 | // make sure driver can allocate memory for this buffer |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 1304 | GL_ALLOC_CALL(this->glInterface(), |
| 1305 | BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 1306 | size, |
| 1307 | NULL, // data ptr |
| 1308 | dynamic ? GR_GL_DYNAMIC_DRAW : |
| 1309 | GR_GL_STATIC_DRAW)); |
| 1310 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1311 | GL_CALL(DeleteBuffers(1, &id)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1312 | // deleting bound buffer does implicit bind to 0 |
| 1313 | fHWGeometryState.fIndexBuffer = NULL; |
| 1314 | return NULL; |
| 1315 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1316 | GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1317 | size, dynamic); |
| 1318 | fHWGeometryState.fIndexBuffer = indexBuffer; |
| 1319 | return indexBuffer; |
| 1320 | } |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1324 | void GrGpuGL::enableScissoring(const GrIRect& rect) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1325 | const GrDrawState& drawState = this->getDrawState(); |
| 1326 | const GrGLRenderTarget* rt = |
| 1327 | static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget()); |
| 1328 | |
| 1329 | GrAssert(NULL != rt); |
| 1330 | const GrGLIRect& vp = rt->getViewport(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1331 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1332 | GrGLIRect scissor; |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1333 | scissor.setRelativeTo(vp, rect.fLeft, rect.fTop, |
| 1334 | rect.width(), rect.height()); |
| 1335 | if (scissor.contains(vp)) { |
| 1336 | disableScissor(); |
| 1337 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1340 | if (fHWBounds.fScissorRect != scissor) { |
| 1341 | scissor.pushToGLScissor(this->glInterface()); |
| 1342 | fHWBounds.fScissorRect = scissor; |
| 1343 | } |
| 1344 | if (!fHWBounds.fScissorEnabled) { |
| 1345 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1346 | fHWBounds.fScissorEnabled = true; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | void GrGpuGL::disableScissor() { |
| 1351 | if (fHWBounds.fScissorEnabled) { |
| 1352 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
| 1353 | fHWBounds.fScissorEnabled = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1357 | void GrGpuGL::onClear(const GrIRect* rect, GrColor color) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1358 | const GrDrawState& drawState = this->getDrawState(); |
| 1359 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1360 | // parent class should never let us get here with no RT |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1361 | GrAssert(NULL != rt); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1362 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1363 | GrIRect clippedRect; |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1364 | if (NULL != rect) { |
| 1365 | // flushScissor expects rect to be clipped to the target. |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1366 | clippedRect = *rect; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1367 | GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height()); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1368 | if (clippedRect.intersect(rtRect)) { |
| 1369 | rect = &clippedRect; |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | return; |
| 1372 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1373 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1374 | this->flushRenderTarget(rect); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1375 | if (NULL != rect) |
| 1376 | this->enableScissoring(*rect); |
| 1377 | else |
| 1378 | this->disableScissor(); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1379 | |
| 1380 | GrGLfloat r, g, b, a; |
| 1381 | static const GrGLfloat scale255 = 1.f / 255.f; |
| 1382 | a = GrColorUnpackA(color) * scale255; |
| 1383 | GrGLfloat scaleRGB = scale255; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1384 | if (GrPixelConfigIsUnpremultiplied(rt->config())) { |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1385 | scaleRGB *= a; |
| 1386 | } |
| 1387 | r = GrColorUnpackR(color) * scaleRGB; |
| 1388 | g = GrColorUnpackG(color) * scaleRGB; |
| 1389 | b = GrColorUnpackB(color) * scaleRGB; |
| 1390 | |
| 1391 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 1392 | fHWWriteToColor = kYes_TriState; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1393 | GL_CALL(ClearColor(r, g, b, a)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1394 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 1397 | void GrGpuGL::clearStencil() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1398 | if (NULL == this->getDrawState().getRenderTarget()) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1401 | |
| 1402 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
| 1403 | |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1404 | this->disableScissor(); |
| 1405 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1406 | GL_CALL(StencilMask(0xffffffff)); |
| 1407 | GL_CALL(ClearStencil(0)); |
| 1408 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1409 | fHWStencilSettings.invalidate(); |
| 1410 | fHWStencilClipMode = kInvalid_StencilClipMode; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1413 | void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1414 | const GrDrawState& drawState = this->getDrawState(); |
| 1415 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 1416 | GrAssert(NULL != rt); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1417 | |
| 1418 | // this should only be called internally when we know we have a |
| 1419 | // stencil buffer. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1420 | GrAssert(NULL != rt->getStencilBuffer()); |
| 1421 | GrGLint stencilBitCount = rt->getStencilBuffer()->bits(); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1422 | #if 0 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1423 | GrAssert(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1424 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1425 | #else |
| 1426 | // 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] | 1427 | // ANGLE a partial stencil mask will cause clears to be |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1428 | // turned into draws. Our contract on GrDrawTarget says that |
| 1429 | // changing the clip between stencil passes may or may not |
| 1430 | // 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] | 1431 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1432 | #endif |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1433 | GrGLint value; |
| 1434 | if (insideClip) { |
| 1435 | value = (1 << (stencilBitCount - 1)); |
| 1436 | } else { |
| 1437 | value = 0; |
| 1438 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1439 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1440 | this->enableScissoring(rect); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1441 | GL_CALL(StencilMask(clipStencilMask)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1442 | GL_CALL(ClearStencil(value)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1443 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1444 | fHWStencilSettings.invalidate(); |
| 1445 | fHWStencilClipMode = kInvalid_StencilClipMode; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 1448 | void GrGpuGL::onForceRenderTargetFlush() { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1449 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1452 | bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 1453 | int left, int top, |
| 1454 | int width, int height, |
| 1455 | GrPixelConfig config, |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1456 | size_t rowBytes) const { |
| 1457 | // if GL can do the flip then we'll never pay for it. |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1458 | if (this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1459 | return false; |
| 1460 | } |
| 1461 | |
| 1462 | // If we have to do memcpy to handle non-trim rowBytes then we |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 1463 | // get the flip for free. Otherwise it costs. |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1464 | if (this->glCaps().packRowLengthSupport()) { |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1465 | return true; |
| 1466 | } |
| 1467 | // If we have to do memcpys to handle rowBytes then y-flip is free |
| 1468 | // Note the rowBytes might be tight to the passed in data, but if data |
| 1469 | // gets clipped in x to the target the rowBytes will no longer be tight. |
| 1470 | if (left >= 0 && (left + width) < renderTarget->width()) { |
| 1471 | return 0 == rowBytes || |
| 1472 | GrBytesPerPixel(config) * width == rowBytes; |
| 1473 | } else { |
| 1474 | return false; |
| 1475 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1478 | bool GrGpuGL::onReadPixels(GrRenderTarget* target, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1479 | int left, int top, |
| 1480 | int width, int height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1481 | GrPixelConfig config, |
| 1482 | void* buffer, |
| 1483 | size_t rowBytes, |
| 1484 | bool invertY) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1485 | GrGLenum format; |
| 1486 | GrGLenum type; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 1487 | if (!this->configToGLFormats(config, false, NULL, &format, &type)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1488 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1489 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1490 | size_t bpp = GrBytesPerPixel(config); |
| 1491 | if (!adjust_pixel_ops_params(target->width(), target->height(), bpp, |
| 1492 | &left, &top, &width, &height, |
| 1493 | const_cast<const void**>(&buffer), |
| 1494 | &rowBytes)) { |
| 1495 | return false; |
| 1496 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1497 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1498 | // resolve the render target if necessary |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1499 | GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1500 | GrDrawState::AutoRenderTargetRestore artr; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1501 | switch (tgt->getResolveType()) { |
| 1502 | case GrGLRenderTarget::kCantResolve_ResolveType: |
| 1503 | return false; |
| 1504 | case GrGLRenderTarget::kAutoResolves_ResolveType: |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1505 | artr.set(this->drawState(), target); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1506 | this->flushRenderTarget(&GrIRect::EmptyIRect()); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1507 | break; |
| 1508 | case GrGLRenderTarget::kCanResolve_ResolveType: |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1509 | this->onResolveRenderTarget(tgt); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1510 | // we don't track the state of the READ FBO ID. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1511 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, |
| 1512 | tgt->textureFBOID())); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1513 | break; |
| 1514 | default: |
| 1515 | GrCrash("Unknown resolve type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1518 | const GrGLIRect& glvp = tgt->getViewport(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1519 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1520 | // the read rect is viewport-relative |
| 1521 | GrGLIRect readRect; |
| 1522 | readRect.setRelativeTo(glvp, left, top, width, height); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1523 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1524 | size_t tightRowBytes = bpp * width; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1525 | if (0 == rowBytes) { |
| 1526 | rowBytes = tightRowBytes; |
| 1527 | } |
| 1528 | size_t readDstRowBytes = tightRowBytes; |
| 1529 | void* readDst = buffer; |
| 1530 | |
| 1531 | // determine if GL can read using the passed rowBytes or if we need |
| 1532 | // a scratch buffer. |
| 1533 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
| 1534 | if (rowBytes != tightRowBytes) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1535 | if (this->glCaps().packRowLengthSupport()) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1536 | GrAssert(!(rowBytes % sizeof(GrColor))); |
| 1537 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor))); |
| 1538 | readDstRowBytes = rowBytes; |
| 1539 | } else { |
| 1540 | scratch.reset(tightRowBytes * height); |
| 1541 | readDst = scratch.get(); |
| 1542 | } |
| 1543 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1544 | if (!invertY && this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1545 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1)); |
| 1546 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1547 | GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, |
| 1548 | readRect.fWidth, readRect.fHeight, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1549 | format, type, readDst)); |
| 1550 | if (readDstRowBytes != tightRowBytes) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1551 | GrAssert(this->glCaps().packRowLengthSupport()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1552 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 1553 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1554 | if (!invertY && this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1555 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0)); |
| 1556 | invertY = true; |
| 1557 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1558 | |
| 1559 | // now reverse the order of the rows, since GL's are bottom-to-top, but our |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1560 | // API presents top-to-bottom. We must preserve the padding contents. Note |
| 1561 | // that the above readPixels did not overwrite the padding. |
| 1562 | if (readDst == buffer) { |
| 1563 | GrAssert(rowBytes == readDstRowBytes); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1564 | if (!invertY) { |
| 1565 | scratch.reset(tightRowBytes); |
| 1566 | void* tmpRow = scratch.get(); |
| 1567 | // flip y in-place by rows |
| 1568 | const int halfY = height >> 1; |
| 1569 | char* top = reinterpret_cast<char*>(buffer); |
| 1570 | char* bottom = top + (height - 1) * rowBytes; |
| 1571 | for (int y = 0; y < halfY; y++) { |
| 1572 | memcpy(tmpRow, top, tightRowBytes); |
| 1573 | memcpy(top, bottom, tightRowBytes); |
| 1574 | memcpy(bottom, tmpRow, tightRowBytes); |
| 1575 | top += rowBytes; |
| 1576 | bottom -= rowBytes; |
| 1577 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1578 | } |
| 1579 | } else { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1580 | GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1581 | // copy from readDst to buffer while flipping y |
| 1582 | const int halfY = height >> 1; |
| 1583 | const char* src = reinterpret_cast<const char*>(readDst); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1584 | char* dst = reinterpret_cast<char*>(buffer); |
| 1585 | if (!invertY) { |
| 1586 | dst += (height-1) * rowBytes; |
| 1587 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 1588 | for (int y = 0; y < height; y++) { |
| 1589 | memcpy(dst, src, tightRowBytes); |
| 1590 | src += readDstRowBytes; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1591 | if (invertY) { |
| 1592 | dst += rowBytes; |
| 1593 | } else { |
| 1594 | dst -= rowBytes; |
| 1595 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | return true; |
| 1599 | } |
| 1600 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1601 | void GrGpuGL::flushRenderTarget(const GrIRect* bound) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1602 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1603 | GrGLRenderTarget* rt = |
| 1604 | static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget()); |
| 1605 | GrAssert(NULL != rt); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1606 | |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 1607 | if (fHWBoundRenderTarget != rt) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1608 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1609 | #if GR_COLLECT_STATS |
| 1610 | ++fStats.fRenderTargetChngCnt; |
| 1611 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1612 | #if GR_DEBUG |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1613 | GrGLenum status; |
| 1614 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 1615 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
reed@google.com | b9255d5 | 2011-06-13 18:54:59 +0000 | [diff] [blame] | 1616 | GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1617 | } |
| 1618 | #endif |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 1619 | fHWBoundRenderTarget = rt; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1620 | const GrGLIRect& vp = rt->getViewport(); |
bsalomon@google.com | 649a862 | 2011-03-10 14:53:38 +0000 | [diff] [blame] | 1621 | if (fHWBounds.fViewportRect != vp) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1622 | vp.pushToGLViewport(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1623 | fHWBounds.fViewportRect = vp; |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 1624 | // View matrices pushed to GL depend upon the viewport. So they |
| 1625 | // are now invalid. |
| 1626 | fProgramCache->invalidateViewMatrices(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1627 | } |
| 1628 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1629 | if (NULL == bound || !bound->isEmpty()) { |
| 1630 | rt->flagAsNeedingResolve(bound); |
| 1631 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1634 | GrGLenum gPrimitiveType2GLMode[] = { |
| 1635 | GR_GL_TRIANGLES, |
| 1636 | GR_GL_TRIANGLE_STRIP, |
| 1637 | GR_GL_TRIANGLE_FAN, |
| 1638 | GR_GL_POINTS, |
| 1639 | GR_GL_LINES, |
| 1640 | GR_GL_LINE_STRIP |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1641 | }; |
| 1642 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1643 | #define SWAP_PER_DRAW 0 |
| 1644 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1645 | #if SWAP_PER_DRAW |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1646 | #if GR_MAC_BUILD |
| 1647 | #include <AGL/agl.h> |
| 1648 | #elif GR_WIN32_BUILD |
| 1649 | void SwapBuf() { |
| 1650 | DWORD procID = GetCurrentProcessId(); |
| 1651 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 1652 | while(hwnd) { |
| 1653 | DWORD wndProcID = 0; |
| 1654 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 1655 | if(wndProcID == procID) { |
| 1656 | SwapBuffers(GetDC(hwnd)); |
| 1657 | } |
| 1658 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 1659 | } |
| 1660 | } |
| 1661 | #endif |
| 1662 | #endif |
| 1663 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1664 | void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type, |
| 1665 | uint32_t startVertex, |
| 1666 | uint32_t startIndex, |
| 1667 | uint32_t vertexCount, |
| 1668 | uint32_t indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1669 | GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode)); |
| 1670 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1671 | GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 1672 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1673 | GrAssert(NULL != fHWGeometryState.fIndexBuffer); |
| 1674 | GrAssert(NULL != fHWGeometryState.fVertexBuffer); |
| 1675 | |
| 1676 | // our setupGeometry better have adjusted this to zero since |
| 1677 | // DrawElements always draws from the begining of the arrays for idx 0. |
| 1678 | GrAssert(0 == startVertex); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1679 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1680 | GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount, |
| 1681 | GR_GL_UNSIGNED_SHORT, indices)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1682 | #if SWAP_PER_DRAW |
| 1683 | glFlush(); |
| 1684 | #if GR_MAC_BUILD |
| 1685 | aglSwapBuffers(aglGetCurrentContext()); |
| 1686 | int set_a_break_pt_here = 9; |
| 1687 | aglSwapBuffers(aglGetCurrentContext()); |
| 1688 | #elif GR_WIN32_BUILD |
| 1689 | SwapBuf(); |
| 1690 | int set_a_break_pt_here = 9; |
| 1691 | SwapBuf(); |
| 1692 | #endif |
| 1693 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 1696 | void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type, |
| 1697 | uint32_t startVertex, |
| 1698 | uint32_t vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1699 | GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode)); |
| 1700 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1701 | GrAssert(NULL != fHWGeometryState.fVertexBuffer); |
| 1702 | |
| 1703 | // our setupGeometry better have adjusted this to zero. |
| 1704 | // DrawElements doesn't take an offset so we always adjus the startVertex. |
| 1705 | GrAssert(0 == startVertex); |
| 1706 | |
| 1707 | // pass 0 for parameter first. We have to adjust gl*Pointer() to |
| 1708 | // account for startVertex in the DrawElements case. So we always |
| 1709 | // rely on setupGeometry to have accounted for startVertex. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1710 | GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1711 | #if SWAP_PER_DRAW |
| 1712 | glFlush(); |
| 1713 | #if GR_MAC_BUILD |
| 1714 | aglSwapBuffers(aglGetCurrentContext()); |
| 1715 | int set_a_break_pt_here = 9; |
| 1716 | aglSwapBuffers(aglGetCurrentContext()); |
| 1717 | #elif GR_WIN32_BUILD |
| 1718 | SwapBuf(); |
| 1719 | int set_a_break_pt_here = 9; |
| 1720 | SwapBuf(); |
| 1721 | #endif |
| 1722 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1725 | void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) { |
| 1726 | |
| 1727 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1728 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1729 | if (rt->needsResolve()) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1730 | GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1731 | GrAssert(rt->textureFBOID() != rt->renderFBOID()); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1732 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, |
| 1733 | rt->renderFBOID())); |
| 1734 | GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, |
| 1735 | rt->textureFBOID())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1736 | #if GR_COLLECT_STATS |
| 1737 | ++fStats.fRenderTargetChngCnt; |
| 1738 | #endif |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1739 | // make sure we go through flushRenderTarget() since we've modified |
| 1740 | // the bound DRAW FBO ID. |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 1741 | fHWBoundRenderTarget = NULL; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1742 | const GrGLIRect& vp = rt->getViewport(); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1743 | const GrIRect dirtyRect = rt->getResolveRect(); |
| 1744 | GrGLIRect r; |
| 1745 | r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop, |
| 1746 | dirtyRect.width(), dirtyRect.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1747 | |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1748 | if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1749 | // Apple's extension uses the scissor as the blit bounds. |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1750 | #if 1 |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1751 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1752 | GL_CALL(Scissor(r.fLeft, r.fBottom, |
| 1753 | r.fWidth, r.fHeight)); |
| 1754 | GL_CALL(ResolveMultisampleFramebuffer()); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 1755 | fHWBounds.fScissorRect.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1756 | fHWBounds.fScissorEnabled = true; |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1757 | #else |
| 1758 | this->enableScissoring(dirtyRect); |
| 1759 | GL_CALL(ResolveMultisampleFramebuffer()); |
| 1760 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1761 | } else { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1762 | if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) { |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1763 | // this respects the scissor during the blit, so disable it. |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1764 | GrAssert(GrGLCaps::kDesktopEXT_MSFBOType == |
| 1765 | this->glCaps().msFBOType()); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1766 | this->disableScissor(); |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 1767 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1768 | int right = r.fLeft + r.fWidth; |
| 1769 | int top = r.fBottom + r.fHeight; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1770 | GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top, |
| 1771 | r.fLeft, r.fBottom, right, top, |
| 1772 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1773 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 1774 | rt->flagAsResolved(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1778 | static const GrGLenum grToGLStencilFunc[] = { |
| 1779 | GR_GL_ALWAYS, // kAlways_StencilFunc |
| 1780 | GR_GL_NEVER, // kNever_StencilFunc |
| 1781 | GR_GL_GREATER, // kGreater_StencilFunc |
| 1782 | GR_GL_GEQUAL, // kGEqual_StencilFunc |
| 1783 | GR_GL_LESS, // kLess_StencilFunc |
| 1784 | GR_GL_LEQUAL, // kLEqual_StencilFunc, |
| 1785 | GR_GL_EQUAL, // kEqual_StencilFunc, |
| 1786 | GR_GL_NOTEQUAL, // kNotEqual_StencilFunc, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1787 | }; |
| 1788 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount); |
| 1789 | GR_STATIC_ASSERT(0 == kAlways_StencilFunc); |
| 1790 | GR_STATIC_ASSERT(1 == kNever_StencilFunc); |
| 1791 | GR_STATIC_ASSERT(2 == kGreater_StencilFunc); |
| 1792 | GR_STATIC_ASSERT(3 == kGEqual_StencilFunc); |
| 1793 | GR_STATIC_ASSERT(4 == kLess_StencilFunc); |
| 1794 | GR_STATIC_ASSERT(5 == kLEqual_StencilFunc); |
| 1795 | GR_STATIC_ASSERT(6 == kEqual_StencilFunc); |
| 1796 | GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc); |
| 1797 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1798 | static const GrGLenum grToGLStencilOp[] = { |
| 1799 | GR_GL_KEEP, // kKeep_StencilOp |
| 1800 | GR_GL_REPLACE, // kReplace_StencilOp |
| 1801 | GR_GL_INCR_WRAP, // kIncWrap_StencilOp |
| 1802 | GR_GL_INCR, // kIncClamp_StencilOp |
| 1803 | GR_GL_DECR_WRAP, // kDecWrap_StencilOp |
| 1804 | GR_GL_DECR, // kDecClamp_StencilOp |
| 1805 | GR_GL_ZERO, // kZero_StencilOp |
| 1806 | GR_GL_INVERT, // kInvert_StencilOp |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1807 | }; |
| 1808 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount); |
| 1809 | GR_STATIC_ASSERT(0 == kKeep_StencilOp); |
| 1810 | GR_STATIC_ASSERT(1 == kReplace_StencilOp); |
| 1811 | GR_STATIC_ASSERT(2 == kIncWrap_StencilOp); |
| 1812 | GR_STATIC_ASSERT(3 == kIncClamp_StencilOp); |
| 1813 | GR_STATIC_ASSERT(4 == kDecWrap_StencilOp); |
| 1814 | GR_STATIC_ASSERT(5 == kDecClamp_StencilOp); |
| 1815 | GR_STATIC_ASSERT(6 == kZero_StencilOp); |
| 1816 | GR_STATIC_ASSERT(7 == kInvert_StencilOp); |
| 1817 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1818 | void GrGpuGL::flushStencil() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1819 | const GrDrawState& drawState = this->getDrawState(); |
| 1820 | |
| 1821 | const GrStencilSettings* settings = &drawState.getStencil(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1822 | |
| 1823 | // use stencil for clipping if clipping is enabled and the clip |
| 1824 | // has been written into the stencil. |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1825 | StencilClipMode clipMode; |
| 1826 | if (fClipMaskManager.isClipInStencil() && |
| 1827 | drawState.isClipState()) { |
| 1828 | clipMode = kUseClip_StencilClipMode; |
| 1829 | // We can't be modifying the clip and respecting it at the same time. |
| 1830 | GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)); |
| 1831 | } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) { |
| 1832 | clipMode = kModifyClip_StencilClipMode; |
| 1833 | } else { |
| 1834 | clipMode = kIgnoreClip_StencilClipMode; |
| 1835 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1836 | |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1837 | bool stencilChange = (fHWStencilSettings != *settings) || |
| 1838 | (fHWStencilClipMode != clipMode); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1839 | if (stencilChange) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1840 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1841 | if (settings->isDisabled()) { |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1842 | if (kUseClip_StencilClipMode == clipMode) { |
digit@google.com | 9b482c4 | 2012-02-16 22:03:26 +0000 | [diff] [blame] | 1843 | settings = GetClipStencilSettings(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1844 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1845 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1846 | |
| 1847 | if (settings->isDisabled()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1848 | GL_CALL(Disable(GR_GL_STENCIL_TEST)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1849 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1850 | GL_CALL(Enable(GR_GL_STENCIL_TEST)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1851 | #if GR_DEBUG |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1852 | if (!this->getCaps().fStencilWrapOpsSupport) { |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1853 | GrAssert(settings->frontPassOp() != kIncWrap_StencilOp); |
| 1854 | GrAssert(settings->frontPassOp() != kDecWrap_StencilOp); |
| 1855 | GrAssert(settings->frontFailOp() != kIncWrap_StencilOp); |
| 1856 | GrAssert(settings->backFailOp() != kDecWrap_StencilOp); |
| 1857 | GrAssert(settings->backPassOp() != kIncWrap_StencilOp); |
| 1858 | GrAssert(settings->backPassOp() != kDecWrap_StencilOp); |
| 1859 | GrAssert(settings->backFailOp() != kIncWrap_StencilOp); |
| 1860 | GrAssert(settings->frontFailOp() != kDecWrap_StencilOp); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1861 | } |
| 1862 | #endif |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1863 | int stencilBits = 0; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1864 | GrStencilBuffer* stencilBuffer = |
| 1865 | drawState.getRenderTarget()->getStencilBuffer(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1866 | if (NULL != stencilBuffer) { |
| 1867 | stencilBits = stencilBuffer->bits(); |
| 1868 | } |
| 1869 | // TODO: dynamically attach a stencil buffer |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1870 | GrAssert(stencilBits || settings->isDisabled()); |
bsalomon@google.com | 2cdfade | 2011-11-23 16:53:42 +0000 | [diff] [blame] | 1871 | |
| 1872 | GrGLuint clipStencilMask = 0; |
| 1873 | GrGLuint userStencilMask = ~0; |
| 1874 | if (stencilBits > 0) { |
| 1875 | clipStencilMask = 1 << (stencilBits - 1); |
| 1876 | userStencilMask = clipStencilMask - 1; |
| 1877 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1878 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1879 | unsigned int frontRef = settings->frontFuncRef(); |
| 1880 | unsigned int frontMask = settings->frontFuncMask(); |
| 1881 | unsigned int frontWriteMask = settings->frontWriteMask(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1882 | GrGLenum frontFunc; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1883 | |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1884 | if (kModifyClip_StencilClipMode == clipMode) { |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1885 | GrAssert(settings->frontFunc() < kBasicStencilFuncCount); |
| 1886 | frontFunc = grToGLStencilFunc[settings->frontFunc()]; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1887 | } else { |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1888 | bool useClip = kUseClip_StencilClipMode == clipMode; |
| 1889 | frontFunc = grToGLStencilFunc[ConvertStencilFunc(useClip, |
| 1890 | settings->frontFunc())]; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1891 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1892 | ConvertStencilFuncAndMask(settings->frontFunc(), |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1893 | useClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1894 | clipStencilMask, |
| 1895 | userStencilMask, |
| 1896 | &frontRef, |
| 1897 | &frontMask); |
| 1898 | frontWriteMask &= userStencilMask; |
| 1899 | } |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1900 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1901 | settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1902 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1903 | settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1904 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1905 | settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 1906 | GrAssert((size_t) |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1907 | settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp)); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1908 | if (this->getCaps().fTwoSidedStencilSupport) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1909 | GrGLenum backFunc; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1910 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1911 | unsigned int backRef = settings->backFuncRef(); |
| 1912 | unsigned int backMask = settings->backFuncMask(); |
| 1913 | unsigned int backWriteMask = settings->backWriteMask(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1914 | |
| 1915 | |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1916 | if (kModifyClip_StencilClipMode == clipMode) { |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1917 | GrAssert(settings->backFunc() < kBasicStencilFuncCount); |
| 1918 | backFunc = grToGLStencilFunc[settings->backFunc()]; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1919 | } else { |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1920 | bool useClip = kUseClip_StencilClipMode == clipMode; |
| 1921 | backFunc = grToGLStencilFunc[ConvertStencilFunc(useClip, |
| 1922 | settings->backFunc())]; |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1923 | ConvertStencilFuncAndMask(settings->backFunc(), |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1924 | useClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1925 | clipStencilMask, |
| 1926 | userStencilMask, |
| 1927 | &backRef, |
| 1928 | &backMask); |
| 1929 | backWriteMask &= userStencilMask; |
| 1930 | } |
| 1931 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1932 | GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, |
| 1933 | frontRef, frontMask)); |
| 1934 | GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask)); |
| 1935 | GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc, |
| 1936 | backRef, backMask)); |
| 1937 | GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask)); |
| 1938 | GL_CALL(StencilOpSeparate(GR_GL_FRONT, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1939 | grToGLStencilOp[settings->frontFailOp()], |
| 1940 | grToGLStencilOp[settings->frontPassOp()], |
| 1941 | grToGLStencilOp[settings->frontPassOp()])); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1942 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1943 | GL_CALL(StencilOpSeparate(GR_GL_BACK, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1944 | grToGLStencilOp[settings->backFailOp()], |
| 1945 | grToGLStencilOp[settings->backPassOp()], |
| 1946 | grToGLStencilOp[settings->backPassOp()])); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1947 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1948 | GL_CALL(StencilFunc(frontFunc, frontRef, frontMask)); |
| 1949 | GL_CALL(StencilMask(frontWriteMask)); |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 1950 | GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()], |
| 1951 | grToGLStencilOp[settings->frontPassOp()], |
| 1952 | grToGLStencilOp[settings->frontPassOp()])); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1955 | fHWStencilSettings = *settings; |
| 1956 | fHWStencilClipMode = clipMode; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1960 | void GrGpuGL::flushAAState(GrPrimitiveType type) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1961 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1962 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1963 | // ES doesn't support toggling GL_MULTISAMPLE and doesn't have |
| 1964 | // smooth lines. |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1965 | // we prefer smooth lines over multisampled lines |
bsalomon@google.com | 4d5f3fe | 2012-05-21 17:11:44 +0000 | [diff] [blame] | 1966 | bool smoothLines = false; |
| 1967 | |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 1968 | if (GrIsPrimTypeLines(type)) { |
bsalomon@google.com | 4d5f3fe | 2012-05-21 17:11:44 +0000 | [diff] [blame] | 1969 | smoothLines = this->willUseHWAALines(); |
| 1970 | if (smoothLines) { |
| 1971 | if (kYes_TriState != fHWAAState.fSmoothLineEnabled) { |
| 1972 | GL_CALL(Enable(GR_GL_LINE_SMOOTH)); |
| 1973 | fHWAAState.fSmoothLineEnabled = kYes_TriState; |
| 1974 | // must disable msaa to use line smoothing |
| 1975 | if (rt->isMultisampled() && |
| 1976 | kNo_TriState != fHWAAState.fMSAAEnabled) { |
| 1977 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
| 1978 | fHWAAState.fMSAAEnabled = kNo_TriState; |
| 1979 | } |
| 1980 | } |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1981 | } else { |
bsalomon@google.com | 4d5f3fe | 2012-05-21 17:11:44 +0000 | [diff] [blame] | 1982 | if (kNo_TriState != fHWAAState.fSmoothLineEnabled) { |
| 1983 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
| 1984 | fHWAAState.fSmoothLineEnabled = kNo_TriState; |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | if (!smoothLines && rt->isMultisampled()) { |
| 1989 | if (this->getDrawState().isHWAntialiasState()) { |
| 1990 | if (kYes_TriState != fHWAAState.fMSAAEnabled) { |
| 1991 | GL_CALL(Enable(GR_GL_MULTISAMPLE)); |
| 1992 | fHWAAState.fMSAAEnabled = kYes_TriState; |
| 1993 | } |
| 1994 | } else { |
| 1995 | if (kNo_TriState != fHWAAState.fMSAAEnabled) { |
| 1996 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
| 1997 | fHWAAState.fMSAAEnabled = kNo_TriState; |
| 1998 | } |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 2004 | void GrGpuGL::flushBlend(GrPrimitiveType type, |
| 2005 | GrBlendCoeff srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 2006 | GrBlendCoeff dstCoeff) { |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 2007 | if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) { |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2008 | if (kYes_TriState != fHWBlendState.fEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2009 | GL_CALL(Enable(GR_GL_BLEND)); |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2010 | fHWBlendState.fEnabled = kYes_TriState; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2011 | } |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2012 | if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff || |
| 2013 | kISA_BlendCoeff != fHWBlendState.fDstCoeff) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2014 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff], |
| 2015 | gXfermodeCoeff2Blend[kISA_BlendCoeff])); |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2016 | fHWBlendState.fSrcCoeff = kSA_BlendCoeff; |
| 2017 | fHWBlendState.fDstCoeff = kISA_BlendCoeff; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2018 | } |
| 2019 | } else { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 2020 | // any optimization to disable blending should |
| 2021 | // have already been applied and tweaked the coeffs |
| 2022 | // to (1, 0). |
| 2023 | bool blendOff = kOne_BlendCoeff == srcCoeff && |
| 2024 | kZero_BlendCoeff == dstCoeff; |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2025 | if (blendOff) { |
| 2026 | if (kNo_TriState != fHWBlendState.fEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2027 | GL_CALL(Disable(GR_GL_BLEND)); |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2028 | fHWBlendState.fEnabled = kNo_TriState; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2029 | } |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2030 | } else { |
| 2031 | if (kYes_TriState != fHWBlendState.fEnabled) { |
| 2032 | GL_CALL(Enable(GR_GL_BLEND)); |
| 2033 | fHWBlendState.fEnabled = kYes_TriState; |
| 2034 | } |
| 2035 | if (fHWBlendState.fSrcCoeff != srcCoeff || |
| 2036 | fHWBlendState.fDstCoeff != dstCoeff) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2037 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff], |
| 2038 | gXfermodeCoeff2Blend[dstCoeff])); |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2039 | fHWBlendState.fSrcCoeff = srcCoeff; |
| 2040 | fHWBlendState.fDstCoeff = dstCoeff; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2041 | } |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 2042 | GrColor blendConst = this->getDrawState().getBlendConstant(); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 2043 | if ((BlendCoeffReferencesConstant(srcCoeff) || |
| 2044 | BlendCoeffReferencesConstant(dstCoeff)) && |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2045 | (!fHWBlendState.fConstColorValid || |
| 2046 | fHWBlendState.fConstColor != blendConst)) { |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2047 | |
| 2048 | float c[] = { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2049 | GrColorUnpackR(blendConst) / 255.f, |
| 2050 | GrColorUnpackG(blendConst) / 255.f, |
| 2051 | GrColorUnpackB(blendConst) / 255.f, |
| 2052 | GrColorUnpackA(blendConst) / 255.f |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2053 | }; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2054 | GL_CALL(BlendColor(c[0], c[1], c[2], c[3])); |
bsalomon@google.com | a4d8fc2 | 2012-05-21 13:21:46 +0000 | [diff] [blame] | 2055 | fHWBlendState.fConstColor = blendConst; |
| 2056 | fHWBlendState.fConstColorValid = true; |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2057 | } |
| 2058 | } |
| 2059 | } |
| 2060 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2061 | namespace { |
| 2062 | |
| 2063 | unsigned gr_to_gl_filter(GrSamplerState::Filter filter) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 2064 | switch (filter) { |
| 2065 | case GrSamplerState::kBilinear_Filter: |
| 2066 | case GrSamplerState::k4x4Downsample_Filter: |
| 2067 | return GR_GL_LINEAR; |
| 2068 | case GrSamplerState::kNearest_Filter: |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 2069 | return GR_GL_NEAREST; |
| 2070 | default: |
| 2071 | GrAssert(!"Unknown filter type"); |
| 2072 | return GR_GL_LINEAR; |
| 2073 | } |
| 2074 | } |
| 2075 | |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 2076 | // get_swizzle is only called from this .cpp so it is OK to inline it here |
| 2077 | inline const GrGLenum* get_swizzle(GrPixelConfig config, |
| 2078 | const GrSamplerState& sampler, |
| 2079 | const GrGLCaps& glCaps) { |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2080 | if (GrPixelConfigIsAlphaOnly(config)) { |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 2081 | if (glCaps.textureRedSupport()) { |
| 2082 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, |
| 2083 | GR_GL_RED, GR_GL_RED }; |
| 2084 | return gRedSmear; |
| 2085 | } else { |
| 2086 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 2087 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 2088 | return gAlphaSmear; |
| 2089 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2090 | } else if (sampler.swapsRAndB()) { |
| 2091 | static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN, |
| 2092 | GR_GL_RED, GR_GL_ALPHA }; |
| 2093 | return gRedBlueSwap; |
| 2094 | } else { |
| 2095 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, |
| 2096 | GR_GL_BLUE, GR_GL_ALPHA }; |
| 2097 | return gStraight; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) { |
bsalomon@google.com | 4d063de | 2012-05-31 17:59:23 +0000 | [diff] [blame] | 2102 | GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D, |
| 2103 | GR_GL_TEXTURE_SWIZZLE_RGBA, |
| 2104 | reinterpret_cast<const GrGLint*>(swizzle))); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 2108 | bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) { |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2109 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2110 | GrDrawState* drawState = this->drawState(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2111 | // GrGpu::setupClipAndFlushState should have already checked this |
| 2112 | // and bailed if not true. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2113 | GrAssert(NULL != drawState->getRenderTarget()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2114 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2115 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2116 | // bind texture and set sampler state |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2117 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2118 | GrGLTexture* nextTexture = |
| 2119 | static_cast<GrGLTexture*>(drawState->getTexture(s)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2120 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2121 | // true for now, but maybe not with GrEffect. |
| 2122 | GrAssert(NULL != nextTexture); |
| 2123 | // if we created a rt/tex and rendered to it without using a |
robertphillips@google.com | 1942c05 | 2012-05-03 17:58:27 +0000 | [diff] [blame] | 2124 | // texture and now we're texturing from the rt it will still be |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2125 | // the last bound texture, but it needs resolving. So keep this |
| 2126 | // out of the "last != next" check. |
| 2127 | GrGLRenderTarget* texRT = |
| 2128 | static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget()); |
| 2129 | if (NULL != texRT) { |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 2130 | this->onResolveRenderTarget(texRT); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2131 | } |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2132 | |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2133 | if (fHWBoundTextures[s] != nextTexture) { |
| 2134 | this->setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2135 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID())); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2136 | #if GR_COLLECT_STATS |
| 2137 | ++fStats.fTextureChngCnt; |
| 2138 | #endif |
| 2139 | //GrPrintf("---- bindtexture %d\n", nextTexture->textureID()); |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2140 | fHWBoundTextures[s] = nextTexture; |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2143 | const GrSamplerState& sampler = drawState->getSampler(s); |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 2144 | ResetTimestamp timestamp; |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2145 | const GrGLTexture::TexParams& oldTexParams = |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 2146 | nextTexture->getCachedTexParams(×tamp); |
| 2147 | bool setAll = timestamp < this->getResetTimestamp(); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2148 | GrGLTexture::TexParams newTexParams; |
| 2149 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2150 | newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 2151 | |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 2152 | const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2153 | newTexParams.fWrapS = wraps[sampler.getWrapX()]; |
| 2154 | newTexParams.fWrapT = wraps[sampler.getWrapY()]; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2155 | memcpy(newTexParams.fSwizzleRGBA, |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 2156 | get_swizzle(nextTexture->config(), sampler, this->glCaps()), |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2157 | sizeof(newTexParams.fSwizzleRGBA)); |
| 2158 | if (setAll || newTexParams.fFilter != oldTexParams.fFilter) { |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2159 | this->setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2160 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2161 | GR_GL_TEXTURE_MAG_FILTER, |
| 2162 | newTexParams.fFilter)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2163 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2164 | GR_GL_TEXTURE_MIN_FILTER, |
| 2165 | newTexParams.fFilter)); |
| 2166 | } |
| 2167 | if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) { |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2168 | this->setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2169 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2170 | GR_GL_TEXTURE_WRAP_S, |
| 2171 | newTexParams.fWrapS)); |
| 2172 | } |
| 2173 | if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) { |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2174 | this->setTextureUnit(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2175 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2176 | GR_GL_TEXTURE_WRAP_T, |
| 2177 | newTexParams.fWrapT)); |
| 2178 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 2179 | if (this->glCaps().textureSwizzleSupport() && |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2180 | (setAll || |
| 2181 | memcmp(newTexParams.fSwizzleRGBA, |
| 2182 | oldTexParams.fSwizzleRGBA, |
| 2183 | sizeof(newTexParams.fSwizzleRGBA)))) { |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2184 | this->setTextureUnit(s); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2185 | set_tex_swizzle(newTexParams.fSwizzleRGBA, |
| 2186 | this->glInterface()); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2187 | } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 2188 | nextTexture->setCachedTexParams(newTexParams, |
| 2189 | this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 2193 | GrIRect* rect = NULL; |
| 2194 | GrIRect clipBounds; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2195 | if (drawState->isClipState() && |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 2196 | fClip.hasConservativeBounds()) { |
| 2197 | fClip.getConservativeBounds().roundOut(&clipBounds); |
| 2198 | rect = &clipBounds; |
| 2199 | } |
| 2200 | this->flushRenderTarget(rect); |
| 2201 | this->flushAAState(type); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2202 | |
| 2203 | if (drawState->isDitherState()) { |
| 2204 | if (kYes_TriState != fHWDitherEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2205 | GL_CALL(Enable(GR_GL_DITHER)); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2206 | fHWDitherEnabled = kYes_TriState; |
| 2207 | } |
| 2208 | } else { |
| 2209 | if (kNo_TriState != fHWDitherEnabled) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2210 | GL_CALL(Disable(GR_GL_DITHER)); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2211 | fHWDitherEnabled = kNo_TriState; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2215 | if (drawState->isColorWriteDisabled()) { |
| 2216 | if (kNo_TriState != fHWWriteToColor) { |
| 2217 | GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE, |
| 2218 | GR_GL_FALSE, GR_GL_FALSE)); |
| 2219 | fHWWriteToColor = kNo_TriState; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2220 | } |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2221 | } else { |
| 2222 | if (kYes_TriState != fHWWriteToColor) { |
| 2223 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
| 2224 | fHWWriteToColor = kYes_TriState; |
| 2225 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2228 | if (fHWDrawFace != drawState->getDrawFace()) { |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 2229 | switch (this->getDrawState().getDrawFace()) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2230 | case GrDrawState::kCCW_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2231 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2232 | GL_CALL(CullFace(GR_GL_BACK)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2233 | break; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2234 | case GrDrawState::kCW_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2235 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2236 | GL_CALL(CullFace(GR_GL_FRONT)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2237 | break; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2238 | case GrDrawState::kBoth_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2239 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | default: |
| 2242 | GrCrash("Unknown draw face."); |
| 2243 | } |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2244 | fHWDrawFace = drawState->getDrawFace(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2247 | #if GR_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2248 | // check for circular rendering |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2249 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 2250 | GrAssert(!this->isStageEnabled(s) || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2251 | NULL == drawState->getRenderTarget() || |
| 2252 | NULL == drawState->getTexture(s) || |
| 2253 | drawState->getTexture(s)->asRenderTarget() != |
| 2254 | drawState->getRenderTarget()); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2255 | } |
| 2256 | #endif |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 2257 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2258 | this->flushStencil(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2259 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2260 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2264 | if (fHWGeometryState.fVertexBuffer != buffer) { |
| 2265 | fHWGeometryState.fArrayPtrsDirty = true; |
| 2266 | fHWGeometryState.fVertexBuffer = buffer; |
| 2267 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2271 | if (fHWGeometryState.fVertexBuffer == buffer) { |
| 2272 | // deleting bound buffer does implied bind to 0 |
| 2273 | fHWGeometryState.fVertexBuffer = NULL; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2274 | fHWGeometryState.fArrayPtrsDirty = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2279 | fHWGeometryState.fIndexBuffer = buffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2283 | if (fHWGeometryState.fIndexBuffer == buffer) { |
| 2284 | // deleting bound buffer does implied bind to 0 |
| 2285 | fHWGeometryState.fIndexBuffer = NULL; |
| 2286 | } |
| 2287 | } |
| 2288 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2289 | void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) { |
| 2290 | GrAssert(NULL != renderTarget); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2291 | GrDrawState* drawState = this->drawState(); |
| 2292 | if (drawState->getRenderTarget() == renderTarget) { |
| 2293 | drawState->setRenderTarget(NULL); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2294 | } |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2295 | if (fHWBoundRenderTarget == renderTarget) { |
| 2296 | fHWBoundRenderTarget = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2297 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2301 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 2302 | GrDrawState* drawState = this->drawState(); |
| 2303 | if (drawState->getTexture(s) == texture) { |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 2304 | this->drawState()->setTexture(s, NULL); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2305 | } |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2306 | if (fHWBoundTextures[s] == texture) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2307 | // deleting bound texture does implied bind to 0 |
bsalomon@google.com | c811ea3 | 2012-05-21 15:33:09 +0000 | [diff] [blame] | 2308 | fHWBoundTextures[s] = NULL; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2309 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2310 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2313 | bool GrGpuGL::configToGLFormats(GrPixelConfig config, |
| 2314 | bool getSizedInternalFormat, |
| 2315 | GrGLenum* internalFormat, |
| 2316 | GrGLenum* externalFormat, |
| 2317 | GrGLenum* externalType) { |
| 2318 | GrGLenum dontCare; |
| 2319 | if (NULL == internalFormat) { |
| 2320 | internalFormat = &dontCare; |
| 2321 | } |
| 2322 | if (NULL == externalFormat) { |
| 2323 | externalFormat = &dontCare; |
| 2324 | } |
| 2325 | if (NULL == externalType) { |
| 2326 | externalType = &dontCare; |
| 2327 | } |
| 2328 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2329 | switch (config) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2330 | case kRGBA_8888_PM_GrPixelConfig: |
| 2331 | case kRGBA_8888_UPM_GrPixelConfig: |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2332 | *internalFormat = GR_GL_RGBA; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2333 | *externalFormat = GR_GL_RGBA; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2334 | if (getSizedInternalFormat) { |
| 2335 | *internalFormat = GR_GL_RGBA8; |
| 2336 | } else { |
| 2337 | *internalFormat = GR_GL_RGBA; |
| 2338 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2339 | *externalType = GR_GL_UNSIGNED_BYTE; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | case kBGRA_8888_PM_GrPixelConfig: |
| 2342 | case kBGRA_8888_UPM_GrPixelConfig: |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 2343 | if (!this->glCaps().bgraFormatSupport()) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2344 | return false; |
| 2345 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 2346 | if (this->glCaps().bgraIsInternalFormat()) { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2347 | if (getSizedInternalFormat) { |
| 2348 | *internalFormat = GR_GL_BGRA8; |
| 2349 | } else { |
| 2350 | *internalFormat = GR_GL_BGRA; |
| 2351 | } |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 2352 | } else { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2353 | if (getSizedInternalFormat) { |
| 2354 | *internalFormat = GR_GL_RGBA8; |
| 2355 | } else { |
| 2356 | *internalFormat = GR_GL_RGBA; |
| 2357 | } |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 2358 | } |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2359 | *externalFormat = GR_GL_BGRA; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2360 | *externalType = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2361 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2362 | case kRGB_565_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2363 | *internalFormat = GR_GL_RGB; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2364 | *externalFormat = GR_GL_RGB; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2365 | if (getSizedInternalFormat) { |
| 2366 | if (this->glBinding() == kDesktop_GrGLBinding) { |
| 2367 | return false; |
| 2368 | } else { |
| 2369 | *internalFormat = GR_GL_RGB565; |
| 2370 | } |
| 2371 | } else { |
| 2372 | *internalFormat = GR_GL_RGB; |
| 2373 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2374 | *externalType = GR_GL_UNSIGNED_SHORT_5_6_5; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2375 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2376 | case kRGBA_4444_GrPixelConfig: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2377 | *internalFormat = GR_GL_RGBA; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2378 | *externalFormat = GR_GL_RGBA; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2379 | if (getSizedInternalFormat) { |
| 2380 | *internalFormat = GR_GL_RGBA4; |
| 2381 | } else { |
| 2382 | *internalFormat = GR_GL_RGBA; |
| 2383 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 2384 | *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2385 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2386 | case kIndex_8_GrPixelConfig: |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 2387 | if (this->getCaps().f8BitPaletteSupport) { |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 2388 | *internalFormat = GR_GL_PALETTE8_RGBA8; |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2389 | // glCompressedTexImage doesn't take external params |
| 2390 | *externalFormat = GR_GL_PALETTE8_RGBA8; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2391 | // no sized/unsized internal format distinction here |
| 2392 | *internalFormat = GR_GL_PALETTE8_RGBA8; |
| 2393 | // unused with CompressedTexImage |
bsalomon@google.com | 32e4d2a | 2011-12-09 16:14:25 +0000 | [diff] [blame] | 2394 | *externalType = GR_GL_UNSIGNED_BYTE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2395 | } else { |
| 2396 | return false; |
| 2397 | } |
| 2398 | break; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2399 | case kAlpha_8_GrPixelConfig: |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 2400 | if (this->glCaps().textureRedSupport()) { |
| 2401 | *internalFormat = GR_GL_RED; |
| 2402 | *externalFormat = GR_GL_RED; |
| 2403 | if (getSizedInternalFormat) { |
| 2404 | *internalFormat = GR_GL_R8; |
| 2405 | } else { |
| 2406 | *internalFormat = GR_GL_RED; |
| 2407 | } |
| 2408 | *externalType = GR_GL_UNSIGNED_BYTE; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2409 | } else { |
| 2410 | *internalFormat = GR_GL_ALPHA; |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 2411 | *externalFormat = GR_GL_ALPHA; |
| 2412 | if (getSizedInternalFormat) { |
| 2413 | *internalFormat = GR_GL_ALPHA8; |
| 2414 | } else { |
| 2415 | *internalFormat = GR_GL_ALPHA; |
| 2416 | } |
| 2417 | *externalType = GR_GL_UNSIGNED_BYTE; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 2418 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2419 | break; |
| 2420 | default: |
| 2421 | return false; |
| 2422 | } |
| 2423 | return true; |
| 2424 | } |
| 2425 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2426 | void GrGpuGL::setTextureUnit(int unit) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 2427 | GrAssert(unit >= 0 && unit < GrDrawState::kNumStages); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2428 | if (fActiveTextureUnitIdx != unit) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2429 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2430 | fActiveTextureUnitIdx = unit; |
| 2431 | } |
| 2432 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 2433 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2434 | void GrGpuGL::setSpareTextureUnit() { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2435 | if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2436 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2437 | fActiveTextureUnitIdx = SPARE_TEX_UNIT; |
| 2438 | } |
| 2439 | } |
| 2440 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2441 | void GrGpuGL::setBuffers(bool indexed, |
| 2442 | int* extraVertexOffset, |
| 2443 | int* extraIndexOffset) { |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2444 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2445 | GrAssert(NULL != extraVertexOffset); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2446 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2447 | const GeometryPoolState& geoPoolState = this->getGeomPoolState(); |
| 2448 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2449 | GrGLVertexBuffer* vbuf; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2450 | switch (this->getGeomSrc().fVertexSrc) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2451 | case kBuffer_GeometrySrcType: |
| 2452 | *extraVertexOffset = 0; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2453 | vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2454 | break; |
| 2455 | case kArray_GeometrySrcType: |
| 2456 | case kReserved_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2457 | this->finalizeReservedVertices(); |
| 2458 | *extraVertexOffset = geoPoolState.fPoolStartVertex; |
| 2459 | vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2460 | break; |
| 2461 | default: |
| 2462 | vbuf = NULL; // suppress warning |
| 2463 | GrCrash("Unknown geometry src type!"); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2466 | GrAssert(NULL != vbuf); |
| 2467 | GrAssert(!vbuf->isLocked()); |
| 2468 | if (fHWGeometryState.fVertexBuffer != vbuf) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2469 | GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID())); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2470 | fHWGeometryState.fArrayPtrsDirty = true; |
| 2471 | fHWGeometryState.fVertexBuffer = vbuf; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2472 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2473 | |
| 2474 | if (indexed) { |
| 2475 | GrAssert(NULL != extraIndexOffset); |
| 2476 | |
| 2477 | GrGLIndexBuffer* ibuf; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2478 | switch (this->getGeomSrc().fIndexSrc) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2479 | case kBuffer_GeometrySrcType: |
| 2480 | *extraIndexOffset = 0; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2481 | ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | case kArray_GeometrySrcType: |
| 2484 | case kReserved_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 2485 | this->finalizeReservedIndices(); |
| 2486 | *extraIndexOffset = geoPoolState.fPoolStartIndex; |
| 2487 | ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2488 | break; |
| 2489 | default: |
| 2490 | ibuf = NULL; // suppress warning |
| 2491 | GrCrash("Unknown geometry src type!"); |
| 2492 | } |
| 2493 | |
| 2494 | GrAssert(NULL != ibuf); |
| 2495 | GrAssert(!ibuf->isLocked()); |
| 2496 | if (fHWGeometryState.fIndexBuffer != ibuf) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2497 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID())); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2498 | fHWGeometryState.fIndexBuffer = ibuf; |
| 2499 | } |
| 2500 | } |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 2501 | } |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 2502 | |