| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2013 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. | |
| 6 */ | |
| 7 #include "GrGLProgramDesc.h" | |
| 8 | |
| 9 #include "GrProcessor.h" | |
| 10 #include "GrPipeline.h" | |
| 11 #include "GrRenderTargetPriv.h" | |
| 12 #include "SkChecksum.h" | |
| 13 #include "gl/GrGLDefines.h" | |
| 14 #include "gl/GrGLTexture.h" | |
| 15 #include "gl/GrGLTypes.h" | |
| 16 #include "glsl/GrGLSLFragmentProcessor.h" | |
| 17 #include "glsl/GrGLSLFragmentShaderBuilder.h" | |
| 18 #include "glsl/GrGLSLCaps.h" | |
| 19 | |
| 20 static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShader
Flags visibility, | |
| 21 const GrGLSLCaps& caps) { | |
| 22 enum { | |
| 23 kFirstSamplerType = kTexture2DSampler_GrSLType, | |
| 24 kLastSamplerType = kTextureBufferSampler_GrSLType, | |
| 25 kSamplerTypeKeyBits = 4 | |
| 26 }; | |
| 27 GR_STATIC_ASSERT(kLastSamplerType - kFirstSamplerType < (1 << kSamplerTypeKe
yBits)); | |
| 28 | |
| 29 SkASSERT((int)samplerType >= kFirstSamplerType && (int)samplerType <= kLastS
amplerType); | |
| 30 int samplerTypeKey = samplerType - kFirstSamplerType; | |
| 31 | |
| 32 return SkToU16(caps.configTextureSwizzle(config).asKey() | | |
| 33 (samplerTypeKey << 8) | | |
| 34 (caps.samplerPrecision(config, visibility) << (8 + kSamplerTy
peKeyBits))); | |
| 35 } | |
| 36 | |
| 37 static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrProcessor& proc, | |
| 38 const GrGLSLCaps& caps) { | |
| 39 int numTextures = proc.numTextures(); | |
| 40 int numSamplers = numTextures + proc.numBuffers(); | |
| 41 // Need two bytes per key (swizzle, sampler type, and precision). | |
| 42 int word32Count = (numSamplers + 1) / 2; | |
| 43 if (0 == word32Count) { | |
| 44 return; | |
| 45 } | |
| 46 uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count)); | |
| 47 int i = 0; | |
| 48 for (; i < numTextures; ++i) { | |
| 49 const GrTextureAccess& access = proc.textureAccess(i); | |
| 50 const GrTexture* tex = access.getTexture(); | |
| 51 k16[i] = sampler_key(tex->samplerType(), tex->config(), access.getVisibi
lity(), caps); | |
| 52 } | |
| 53 for (; i < numSamplers; ++i) { | |
| 54 const GrBufferAccess& access = proc.bufferAccess(i - numTextures); | |
| 55 k16[i] = sampler_key(kTextureBufferSampler_GrSLType, access.texelConfig(
), | |
| 56 access.visibility(), caps); | |
| 57 } | |
| 58 // zero the last 16 bits if the number of samplers is odd. | |
| 59 if (numSamplers & 0x1) { | |
| 60 k16[numSamplers] = 0; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * A function which emits a meta key into the key builder. This is required bec
ause shader code may | |
| 66 * be dependent on properties of the effect that the effect itself doesn't use | |
| 67 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for | |
| 68 * every effect using this function. It is also responsible for inserting the ef
fect's class ID | |
| 69 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many | |
| 70 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and
GPs share this | |
| 71 * function because it is hairy, though FPs do not have attribs, and GPs do not
have transforms | |
| 72 */ | |
| 73 static bool gen_meta_key(const GrProcessor& proc, | |
| 74 const GrGLSLCaps& glslCaps, | |
| 75 uint32_t transformKey, | |
| 76 GrProcessorKeyBuilder* b) { | |
| 77 size_t processorKeySize = b->size(); | |
| 78 uint32_t classID = proc.classID(); | |
| 79 | |
| 80 // Currently we allow 16 bits for the class id and the overall processor key
size. | |
| 81 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | |
| 82 if ((processorKeySize | classID) & kMetaKeyInvalidMask) { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 add_sampler_keys(b, proc, glslCaps); | |
| 87 | |
| 88 uint32_t* key = b->add32n(2); | |
| 89 key[0] = (classID << 16) | SkToU32(processorKeySize); | |
| 90 key[1] = transformKey; | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, | |
| 95 const GrFragmentProcessor& fp, | |
| 96 const GrGLSLCaps& glslCaps, | |
| 97 GrProcessorKeyBuilder* b) { | |
| 98 for (int i = 0; i < fp.numChildProcessors(); ++i) { | |
| 99 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCap
s, b)) { | |
| 100 return false; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 fp.getGLSLProcessorKey(glslCaps, b); | |
| 105 | |
| 106 return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransform
s(), | |
| 107 fp.numTransformsE
xclChildren()), b); | |
| 108 } | |
| 109 | |
| 110 bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc, | |
| 111 const GrPrimitiveProcessor& primProc, | |
| 112 const GrPipeline& pipeline, | |
| 113 const GrGLSLCaps& glslCaps) { | |
| 114 // The descriptor is used as a cache key. Thus when a field of the | |
| 115 // descriptor will not affect program generation (because of the attribute | |
| 116 // bindings in use or other descriptor field settings) it should be set | |
| 117 // to a canonical value to avoid duplicate programs with different keys. | |
| 118 | |
| 119 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc; | |
| 120 | |
| 121 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); | |
| 122 // Make room for everything up to the effect keys. | |
| 123 glDesc->key().reset(); | |
| 124 glDesc->key().push_back_n(kProcessorKeysOffset); | |
| 125 | |
| 126 GrProcessorKeyBuilder b(&glDesc->key()); | |
| 127 | |
| 128 primProc.getGLSLProcessorKey(glslCaps, &b); | |
| 129 if (!gen_meta_key(primProc, glslCaps, 0, &b)) { | |
| 130 glDesc->key().reset(); | |
| 131 return false; | |
| 132 } | |
| 133 GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures()
; | |
| 134 | |
| 135 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { | |
| 136 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); | |
| 137 if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) { | |
| 138 glDesc->key().reset(); | |
| 139 return false; | |
| 140 } | |
| 141 requiredFeatures |= fp.requiredFeatures(); | |
| 142 } | |
| 143 | |
| 144 const GrXferProcessor& xp = pipeline.getXferProcessor(); | |
| 145 xp.getGLSLProcessorKey(glslCaps, &b); | |
| 146 if (!gen_meta_key(xp, glslCaps, 0, &b)) { | |
| 147 glDesc->key().reset(); | |
| 148 return false; | |
| 149 } | |
| 150 requiredFeatures |= xp.requiredFeatures(); | |
| 151 | |
| 152 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- | |
| 153 // Because header is a pointer into the dynamic array, we can't push any new
data into the key | |
| 154 // below here. | |
| 155 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); | |
| 156 | |
| 157 // make sure any padding in the header is zeroed. | |
| 158 memset(header, 0, kHeaderSize); | |
| 159 | |
| 160 GrRenderTarget* rt = pipeline.getRenderTarget(); | |
| 161 | |
| 162 if (requiredFeatures & (GrProcessor::kFragmentPosition_RequiredFeature | | |
| 163 GrProcessor::kSampleLocations_RequiredFeature)) { | |
| 164 header->fSurfaceOriginKey = GrGLSLFragmentShaderBuilder::KeyForSurfaceOr
igin(rt->origin()); | |
| 165 } else { | |
| 166 header->fSurfaceOriginKey = 0; | |
| 167 } | |
| 168 | |
| 169 if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) { | |
| 170 SkASSERT(pipeline.isHWAntialiasState()); | |
| 171 header->fSamplePatternKey = | |
| 172 rt->renderTargetPriv().getMultisampleSpecs(pipeline.getStencil()).fU
niqueID; | |
| 173 } else { | |
| 174 header->fSamplePatternKey = 0; | |
| 175 } | |
| 176 | |
| 177 header->fOutputSwizzle = glslCaps.configOutputSwizzle(rt->config()).asKey(); | |
| 178 | |
| 179 if (pipeline.ignoresCoverage()) { | |
| 180 header->fIgnoresCoverage = 1; | |
| 181 } else { | |
| 182 header->fIgnoresCoverage = 0; | |
| 183 } | |
| 184 | |
| 185 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); | |
| 186 header->fColorEffectCnt = pipeline.numColorFragmentProcessors(); | |
| 187 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors(); | |
| 188 glDesc->finalize(); | |
| 189 return true; | |
| 190 } | |
| OLD | NEW |