2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #include "GrGLProgramDesc.h"
9 #include "GrGLProcessor.h"
10 #include "GrBackendProcessorFactory.h"
11 #include "GrProcessor.h"
13 #include "GrOptDrawState.h"
14 #include "SkChecksum.h"
15 #include "gl/builders/GrGLFragmentShaderBuilder.h"
18 * The key for an individual coord transform is made up of a matrix type and a bit that
19 * indicates the source of the input coords.
22 kMatrixTypeKeyBits = 1,
23 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
24 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
25 kTransformKeyBits = kMatrixTypeKeyBits + 1,
29 * We specialize the vertex code for each of these matrix types.
32 kNoPersp_MatrixType = 0,
33 kGeneral_MatrixType = 1,
37 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
38 * present in the texture's config. swizzleComponentMask indicates the channels present in the
41 static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
42 uint32_t configComponentMask,
43 uint32_t swizzleComponentMask) {
44 if (caps.textureSwizzleSupport()) {
45 // Any remapping is handled using texture swizzling not shader modifications.
48 // check if the texture is alpha-only
49 if (kA_GrColorComponentFlag == configComponentMask) {
50 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
51 // we must map the swizzle 'a's to 'r'.
54 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
55 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
56 // alpha-only textures smear alpha across all four channels when read.
63 static uint32_t gen_attrib_key(const GrGeometryProcessor& proc) {
66 const GrGeometryProcessor::VertexAttribArray& vars = proc.getVertexAttribs();
67 int numAttributes = vars.count();
68 SkASSERT(numAttributes <= 2);
69 for (int a = 0; a < numAttributes; ++a) {
70 uint32_t value = 1 << a;
76 static uint32_t gen_transform_key(const GrFragmentStage& effectStage,
77 bool useExplicitLocalCoords) {
78 uint32_t totalKey = 0;
79 int numTransforms = effectStage.getProcessor()->numTransforms();
80 for (int t = 0; t < numTransforms; ++t) {
82 if (effectStage.isPerspectiveCoordTransform(t, useExplicitLocalCoords)) {
83 key |= kGeneral_MatrixType;
85 key |= kNoPersp_MatrixType;
88 const GrCoordTransform& coordTransform = effectStage.getProcessor()->coordTransform(t);
89 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLocalCoords) {
90 key |= kPositionCoords_Flag;
92 key <<= kTransformKeyBits * t;
93 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
99 static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) {
101 int numTextures = proc.numTextures();
102 for (int t = 0; t < numTextures; ++t) {
103 const GrTextureAccess& access = proc.textureAccess(t);
104 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
105 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
113 * A function which emits a meta key into the key builder. This is required because shader code may
114 * be dependent on properties of the effect that the effect itself doesn't use
115 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
116 * every effect using this function. It is also responsible for inserting the effect's class ID
117 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
118 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share
119 * this function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
121 static bool get_meta_key(const GrProcessor& proc,
122 const GrGLCaps& caps,
123 uint32_t transformKey,
125 GrProcessorKeyBuilder* b,
126 uint16_t* processorKeySize) {
127 const GrBackendProcessorFactory& factory = proc.getFactory();
128 factory.getGLProcessorKey(proc, caps, b);
129 size_t size = b->size();
130 if (size > SK_MaxU16) {
131 *processorKeySize = 0; // suppresses a warning.
134 *processorKeySize = SkToU16(size);
135 uint32_t textureKey = gen_texture_key(proc, caps);
136 uint32_t classID = proc.getFactory().classID();
138 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
140 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
141 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
145 uint32_t* key = b->add32n(2);
146 key[0] = (textureKey << 16 | transformKey);
147 key[1] = (classID << 16);
151 struct GeometryProcessorKeyBuilder {
152 typedef GrGeometryProcessor StagedProcessor;
153 static bool GetProcessorKey(const GrGeometryProcessor& gp,
154 const GrGLCaps& caps,
156 GrProcessorKeyBuilder* b,
158 /* 0 because no transforms on a GP */
159 return get_meta_key(gp, caps, 0, gen_attrib_key(gp), b, keySize);
163 struct FragmentProcessorKeyBuilder {
164 typedef GrFragmentStage StagedProcessor;
165 static bool GetProcessorKey(const GrFragmentStage& fps,
166 const GrGLCaps& caps,
168 GrProcessorKeyBuilder* b,
170 /* 0 because no attribs on a fP */
171 return get_meta_key(*fps.getProcessor(), caps, gen_transform_key(fps, useLocalCoords), 0,
177 template <class ProcessorKeyBuilder>
179 GrGLProgramDescBuilder::BuildStagedProcessorKey(const typename ProcessorKeyBuilder::StagedProcessor& stage,
180 const GrGLCaps& caps,
181 bool requiresLocalCoordAttrib,
183 int* offsetAndSizeIndex) {
184 GrProcessorKeyBuilder b(&desc->fKey);
185 uint16_t processorKeySize;
186 uint32_t processorOffset = desc->fKey.count();
187 if (processorOffset > SK_MaxU16 ||
188 !ProcessorKeyBuilder::GetProcessorKey(stage, caps, requiresLocalCoordAttrib, &b,
194 uint16_t* offsetAndSize =
195 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kProcessorKeyOffsetsAndLengthOffset +
196 *offsetAndSizeIndex * 2 * sizeof(uint16_t));
197 offsetAndSize[0] = SkToU16(processorOffset);
198 offsetAndSize[1] = processorKeySize;
199 ++(*offsetAndSizeIndex);
203 bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState,
204 const GrProgramDesc::DescInfo& descInfo,
205 GrGpu::DrawType drawType,
207 const GrDeviceCoordTexture* dstCopy,
208 GrProgramDesc* desc) {
209 bool inputColorIsUsed = descInfo.fInputColorIsUsed;
210 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed;
212 // The descriptor is used as a cache key. Thus when a field of the
213 // descriptor will not affect program generation (because of the attribute
214 // bindings in use or other descriptor field settings) it should be set
215 // to a canonical value to avoid duplicate programs with different keys.
217 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib;
219 int numStages = optState.numTotalStages();
221 GR_STATIC_ASSERT(0 == kProcessorKeyOffsetsAndLengthOffset % sizeof(uint32_t));
222 // Make room for everything up to and including the array of offsets to effect keys.
224 desc->fKey.push_back_n(kProcessorKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
226 int offsetAndSizeIndex = 0;
228 // We can only have one effect which touches the vertex shader
229 if (optState.hasGeometryProcessor()) {
230 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getGeometryProcessor(),
234 &offsetAndSizeIndex)) {
239 for (int s = 0; s < optState.numFragmentStages(); ++s) {
240 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFragmentStage(s),
242 requiresLocalCoordAttrib,
244 &offsetAndSizeIndex)) {
249 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
250 // Because header is a pointer into the dynamic array, we can't push any new data into the key
252 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>();
254 // make sure any padding in the header is zeroed.
255 memset(header, 0, kHeaderSize);
257 header->fHasGeometryProcessor = optState.hasGeometryProcessor();
259 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
261 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
262 if (gpu->caps()->pathRenderingSupport() && isPathRendering) {
263 header->fUseNvpr = true;
264 SkASSERT(!optState.hasGeometryProcessor());
266 header->fUseNvpr = false;
269 bool hasUniformColor = inputColorIsUsed &&
270 (isPathRendering || !descInfo.hasColorVertexAttribute());
272 bool hasUniformCoverage = inputCoverageIsUsed &&
273 (isPathRendering || !descInfo.hasCoverageVertexAttribute());
275 if (!inputColorIsUsed) {
276 header->fColorInput = GrProgramDesc::kAllOnes_ColorInput;
277 } else if (hasUniformColor) {
278 header->fColorInput = GrProgramDesc::kUniform_ColorInput;
280 header->fColorInput = GrProgramDesc::kAttribute_ColorInput;
281 SkASSERT(!header->fUseNvpr);
284 bool covIsSolidWhite = !descInfo.hasCoverageVertexAttribute() &&
285 0xffffffff == optState.getCoverageColor();
287 if (covIsSolidWhite || !inputCoverageIsUsed) {
288 header->fCoverageInput = GrProgramDesc::kAllOnes_ColorInput;
289 } else if (hasUniformCoverage) {
290 header->fCoverageInput = GrProgramDesc::kUniform_ColorInput;
292 header->fCoverageInput = GrProgramDesc::kAttribute_ColorInput;
293 SkASSERT(!header->fUseNvpr);
296 if (descInfo.fReadsDst) {
297 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
298 const GrTexture* dstCopyTexture = NULL;
300 dstCopyTexture = dstCopy->texture();
302 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
304 SkASSERT(0 != header->fDstReadKey);
306 header->fDstReadKey = 0;
309 if (descInfo.fReadsFragPosition) {
310 header->fFragPosKey =
311 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(),
314 header->fFragPosKey = 0;
317 // Record attribute indices
318 header->fPositionAttributeIndex = descInfo.positionAttributeIndex();
319 header->fLocalCoordAttributeIndex = descInfo.localCoordAttributeIndex();
321 // For constant color and coverage we need an attribute with an index beyond those already set
322 int availableAttributeIndex = optState.getVertexAttribCount();
323 if (descInfo.hasColorVertexAttribute()) {
324 header->fColorAttributeIndex = descInfo.colorVertexAttributeIndex();
325 } else if (GrProgramDesc::kAttribute_ColorInput == header->fColorInput) {
326 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
327 header->fColorAttributeIndex = availableAttributeIndex;
328 availableAttributeIndex++;
330 header->fColorAttributeIndex = -1;
333 if (descInfo.hasCoverageVertexAttribute()) {
334 header->fCoverageAttributeIndex = descInfo.coverageVertexAttributeIndex();
335 } else if (GrProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
336 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
337 header->fCoverageAttributeIndex = availableAttributeIndex;
339 header->fCoverageAttributeIndex = -1;
342 header->fPrimaryOutputType = descInfo.fPrimaryOutputType;
343 header->fSecondaryOutputType = descInfo.fSecondaryOutputType;
345 header->fColorEffectCnt = optState.numColorStages();
346 header->fCoverageEffectCnt = optState.numCoverageStages();