Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gl/builders/GrGLProgramBuilder.h" | 8 #include "gl/builders/GrGLProgramBuilder.h" |
| 9 #include "GrGLProgramDesc.h" | 9 #include "GrGLProgramDesc.h" |
| 10 #include "GrBackendEffectFactory.h" | 10 #include "GrBackendEffectFactory.h" |
| 11 #include "GrEffect.h" | 11 #include "GrEffect.h" |
| 12 #include "GrGpuGL.h" | 12 #include "GrGpuGL.h" |
| 13 #include "GrOptDrawState.h" | 13 #include "GrOptDrawState.h" |
| 14 | 14 |
| 15 #include "SkChecksum.h" | 15 #include "SkChecksum.h" |
| 16 | 16 |
| 17 namespace { | |
|
bsalomon
2014/09/18 18:08:33
Glad to see all this stuff move here.
Gr code use
| |
| 18 /** | |
| 19 * The key for an individual coord transform is made up of a matrix type and a b it that | |
| 20 * indicates the source of the input coords. | |
| 21 */ | |
| 22 enum { | |
| 23 kMatrixTypeKeyBits = 1, | |
| 24 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1, | |
| 25 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits), | |
| 26 kTransformKeyBits = kMatrixTypeKeyBits + 1, | |
| 27 }; | |
| 28 | |
| 29 /** | |
| 30 * We specialize the vertex code for each of these matrix types. | |
| 31 */ | |
| 32 enum MatrixType { | |
| 33 kNoPersp_MatrixType = 0, | |
| 34 kGeneral_MatrixType = 1, | |
| 35 }; | |
| 36 | |
| 37 /** | |
| 38 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates whic h channels are | |
| 39 * present in the texture's config. swizzleComponentMask indicates the channels present in the | |
| 40 * shader swizzle. | |
| 41 */ | |
| 42 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, | |
| 43 uint32_t configComponentMask, | |
| 44 uint32_t swizzleComponentMask) { | |
| 45 if (caps.textureSwizzleSupport()) { | |
| 46 // Any remapping is handled using texture swizzling not shader modificat ions. | |
| 47 return false; | |
| 48 } | |
| 49 // check if the texture is alpha-only | |
| 50 if (kA_GrColorComponentFlag == configComponentMask) { | |
| 51 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleCompon entMask)) { | |
| 52 // we must map the swizzle 'a's to 'r'. | |
| 53 return true; | |
| 54 } | |
| 55 if (kRGB_GrColorComponentFlags & swizzleComponentMask) { | |
| 56 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that | |
| 57 // alpha-only textures smear alpha across all four channels when rea d. | |
| 58 return true; | |
| 59 } | |
| 60 } | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 uint32_t gen_attrib_key(const GrEffect* effect) { | |
| 65 uint32_t key = 0; | |
| 66 | |
| 67 const GrEffect::VertexAttribArray& vars = effect->getVertexAttribs(); | |
| 68 int numAttributes = vars.count(); | |
| 69 SkASSERT(numAttributes <= 2); | |
| 70 for (int a = 0; a < numAttributes; ++a) { | |
| 71 uint32_t value = 1 << a; | |
| 72 key |= value; | |
| 73 } | |
| 74 return key; | |
| 75 } | |
| 76 | |
| 77 uint32_t gen_transform_key(const GrEffectStage& effectStage, | |
| 78 bool useExplicitLocalCoords) { | |
| 79 uint32_t totalKey = 0; | |
| 80 int numTransforms = effectStage.getEffect()->numTransforms(); | |
| 81 for (int t = 0; t < numTransforms; ++t) { | |
| 82 uint32_t key = 0; | |
| 83 if (effectStage.isPerspectiveCoordTransform(t, useExplicitLocalCoords)) { | |
| 84 key |= kGeneral_MatrixType; | |
| 85 } else { | |
| 86 key |= kNoPersp_MatrixType; | |
| 87 } | |
| 88 | |
| 89 const GrCoordTransform& coordTransform = effectStage.getEffect()->coordT ransform(t); | |
| 90 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLoc alCoords) { | |
| 91 key |= kPositionCoords_Flag; | |
| 92 } | |
| 93 key <<= kTransformKeyBits * t; | |
| 94 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap | |
| 95 totalKey |= key; | |
| 96 } | |
| 97 return totalKey; | |
| 98 } | |
| 99 | |
| 100 uint32_t gen_texture_key(const GrEffect* effect, const GrGLCaps& caps) { | |
| 101 uint32_t key = 0; | |
| 102 int numTextures = effect->numTextures(); | |
| 103 for (int t = 0; t < numTextures; ++t) { | |
| 104 const GrTextureAccess& access = effect->textureAccess(t); | |
| 105 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getText ure()->config()); | |
| 106 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.s wizzleMask())) { | |
| 107 key |= 1 << t; | |
| 108 } | |
| 109 } | |
| 110 return key; | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * A function which emits a meta key into the key builder. This is required bec ause shader code may | |
| 115 * be dependent on properties of the effect that the effect itself doesn't use | |
| 116 * in its key (e.g. the pixel format of textures used). So we create a meta-key for | |
| 117 * every effect using this function. It is also responsible for inserting the ef fect's class ID | |
| 118 * which must be different for every GrEffect subclass. It can fail if an effect uses too many | |
| 119 * textures, attributes, etc for the space allotted in the meta-key. | |
| 120 */ | |
| 121 | |
| 122 bool gen_effect_meta_key(const GrEffectStage& effectStage, | |
| 123 bool useExplicitLocalCoords, | |
| 124 const GrGLCaps& caps, | |
| 125 GrEffectKeyBuilder* b) { | |
| 126 | |
| 127 uint32_t textureKey = gen_texture_key(effectStage.getEffect(), caps); | |
| 128 uint32_t transformKey = gen_transform_key(effectStage,useExplicitLocalCoords ); | |
| 129 uint32_t attribKey = gen_attrib_key(effectStage.getEffect()); | |
| 130 uint32_t classID = effectStage.getEffect()->getFactory().effectClassID(); | |
| 131 | |
| 132 // Currently we allow 16 bits for each of the above portions of the meta-key . Fail if they | |
| 133 // don't fit. | |
| 134 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | |
| 135 if ((textureKey | transformKey | attribKey | classID) & kMetaKeyInvalidMask) { | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 uint32_t* key = b->add32n(2); | |
| 140 key[0] = (textureKey << 16 | transformKey); | |
| 141 key[1] = (classID << 16 | attribKey); | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 } | |
| 146 | |
| 17 bool GrGLProgramDesc::GetEffectKey(const GrEffectStage& stage, const GrGLCaps& c aps, | 147 bool GrGLProgramDesc::GetEffectKey(const GrEffectStage& stage, const GrGLCaps& c aps, |
| 18 bool useExplicitLocalCoords, GrEffectKeyBuild er* b, | 148 bool useExplicitLocalCoords, GrEffectKeyBuild er* b, |
| 19 uint16_t* effectKeySize) { | 149 uint16_t* effectKeySize) { |
| 20 const GrBackendEffectFactory& factory = stage.getEffect()->getFactory(); | 150 const GrBackendEffectFactory& factory = stage.getEffect()->getFactory(); |
| 21 const GrEffect& effect = *stage.getEffect(); | 151 const GrEffect& effect = *stage.getEffect(); |
| 22 factory.getGLEffectKey(effect, caps, b); | 152 factory.getGLEffectKey(effect, caps, b); |
| 23 size_t size = b->size(); | 153 size_t size = b->size(); |
| 24 if (size > SK_MaxU16) { | 154 if (size > SK_MaxU16) { |
| 25 *effectKeySize = 0; // suppresses a warning. | 155 *effectKeySize = 0; // suppresses a warning. |
| 26 return false; | 156 return false; |
| 27 } | 157 } |
| 28 *effectKeySize = SkToU16(size); | 158 *effectKeySize = SkToU16(size); |
| 29 if (!GrGLProgramEffects::GenEffectMetaKey(stage, | 159 if (!gen_effect_meta_key(stage, useExplicitLocalCoords, caps, b)) { |
| 30 useExplicitLocalCoords, | |
| 31 caps, | |
| 32 b)) { | |
| 33 return false; | 160 return false; |
| 34 } | 161 } |
| 35 return true; | 162 return true; |
| 36 } | 163 } |
| 37 | 164 |
| 38 bool GrGLProgramDesc::Build(const GrOptDrawState& optState, | 165 bool GrGLProgramDesc::Build(const GrOptDrawState& optState, |
| 39 GrGpu::DrawType drawType, | 166 GrGpu::DrawType drawType, |
| 40 GrBlendCoeff srcCoeff, | 167 GrBlendCoeff srcCoeff, |
| 41 GrBlendCoeff dstCoeff, | 168 GrBlendCoeff dstCoeff, |
| 42 const GrGpuGL* gpu, | 169 const GrGpuGL* gpu, |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 *checksum = 0; | 410 *checksum = 0; |
| 284 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k eyLength); | 411 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k eyLength); |
| 285 } | 412 } |
| 286 | 413 |
| 287 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { | 414 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) { |
| 288 size_t keyLength = other.keyLength(); | 415 size_t keyLength = other.keyLength(); |
| 289 fKey.reset(keyLength); | 416 fKey.reset(keyLength); |
| 290 memcpy(fKey.begin(), other.fKey.begin(), keyLength); | 417 memcpy(fKey.begin(), other.fKey.begin(), keyLength); |
| 291 return *this; | 418 return *this; |
| 292 } | 419 } |
| OLD | NEW |