Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2016 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 
      8 #include "GrProgramDesc.h"
      9 
     10 #include "GrPipeline.h"
     11 #include "GrPrimitiveProcessor.h"
     12 #include "GrProcessor.h"
     13 #include "GrRenderTargetPriv.h"
     14 #include "GrShaderCaps.h"
     15 #include "GrTexturePriv.h"
     16 #include "SkChecksum.h"
     17 #include "SkTo.h"
     18 #include "glsl/GrGLSLFragmentProcessor.h"
     19 #include "glsl/GrGLSLFragmentShaderBuilder.h"
     20 
     21 enum {
     22     kSamplerOrImageTypeKeyBits = 4
     23 };
     24 
     25 static inline uint16_t texture_type_key(GrTextureType type) {
     26     int value = UINT16_MAX;
     27     switch (type) {
     28         case GrTextureType::k2D:
     29             value = 0;
     30             break;
     31         case GrTextureType::kExternal:
     32             value = 1;
     33             break;
     34         case GrTextureType::kRectangle:
     35             value = 2;
     36             break;
     37     }
     38     SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
     39     return SkToU16(value);
     40 }
     41 
     42 static uint32_t sampler_key(GrTextureType textureType, GrPixelConfig config,
     43                             const GrShaderCaps& caps) {
     44     int samplerTypeKey = texture_type_key(textureType);
     45 
     46     GR_STATIC_ASSERT(2 == sizeof(caps.configTextureSwizzle(config).asKey()));
     47     return SkToU32(samplerTypeKey |
     48                    caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits |
     49                    (GrSLSamplerPrecision(config) << (16 + kSamplerOrImageTypeKeyBits)));
     50 }
     51 
     52 static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
     53                              GrGpu* gpu, const GrShaderCaps& caps) {
     54     int numTextureSamplers = fp.numTextureSamplers();
     55     if (!numTextureSamplers) {
     56         return;
     57     }
     58     uint32_t* k32 = b->add32n(numTextureSamplers);
     59     for (int i = 0; i < numTextureSamplers; ++i) {
     60         const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
     61         const GrTexture* tex = sampler.peekTexture();
     62         k32[i] = sampler_key(tex->texturePriv().textureType(), tex->config(), caps);
     63         uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
     64                 sampler.samplerState(), sampler.proxy()->backendFormat());
     65         if (extraSamplerKey) {
     66             SkASSERT(sampler.proxy()->textureType() == GrTextureType::kExternal);
     67             // We first mark the normal sampler key with last bit to flag that it has an extra
     68             // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
     69             SkASSERT((k32[i] & (1 << 31)) == 0);
     70             k32[i] = k32[i] | (1 << 31);
     71             b->add32(extraSamplerKey);
     72         }
     73     }
     74 }
     75 
     76 static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
     77                              const GrShaderCaps& caps) {
     78     int numTextureSamplers = pp.numTextureSamplers();
     79     if (!numTextureSamplers) {
     80         return;
     81     }
     82     uint32_t* k32 = b->add32n(numTextureSamplers);
     83     for (int i = 0; i < numTextureSamplers; ++i) {
     84         const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
     85         k32[i] = sampler_key(sampler.textureType(), sampler.config(), caps);
     86         uint32_t extraSamplerKey = sampler.extraSamplerKey();
     87         if (extraSamplerKey) {
     88             SkASSERT(sampler.textureType() == GrTextureType::kExternal);
     89             // We first mark the normal sampler key with last bit to flag that it has an extra
     90             // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
     91             SkASSERT((k32[i] & (1 << 15)) == 0);
     92             k32[i] = k32[i] | (1 << 15);
     93             b->add32(extraSamplerKey);
     94         }
     95     }
     96 }
     97 
     98 /**
     99  * A function which emits a meta key into the key builder.  This is required because shader code may
    100  * be dependent on properties of the effect that the effect itself doesn't use
    101  * in its key (e.g. the pixel format of textures used). So we create a meta-key for
    102  * every effect using this function. It is also responsible for inserting the effect's class ID
    103  * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
    104  * transforms, etc, for the space allotted in the meta-key.  NOTE, both FPs and GPs share this
    105  * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
    106  */
    107 static bool gen_meta_key(const GrFragmentProcessor& fp,
    108                          GrGpu* gpu,
    109                          const GrShaderCaps& shaderCaps,
    110                          uint32_t transformKey,
    111                          GrProcessorKeyBuilder* b) {
    112     size_t processorKeySize = b->size();
    113     uint32_t classID = fp.classID();
    114 
    115     // Currently we allow 16 bits for the class id and the overall processor key size.
    116     static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
    117     if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
    118         return false;
    119     }
    120 
    121     add_sampler_keys(b, fp, gpu, shaderCaps);
    122 
    123     uint32_t* key = b->add32n(2);
    124     key[0] = (classID << 16) | SkToU32(processorKeySize);
    125     key[1] = transformKey;
    126     return true;
    127 }
    128 
    129 static bool gen_meta_key(const GrPrimitiveProcessor& pp,
    130                          const GrShaderCaps& shaderCaps,
    131                          uint32_t transformKey,
    132                          GrProcessorKeyBuilder* b) {
    133     size_t processorKeySize = b->size();
    134     uint32_t classID = pp.classID();
    135 
    136     // Currently we allow 16 bits for the class id and the overall processor key size.
    137     static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
    138     if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
    139         return false;
    140     }
    141 
    142     add_sampler_keys(b, pp, shaderCaps);
    143 
    144     uint32_t* key = b->add32n(2);
    145     key[0] = (classID << 16) | SkToU32(processorKeySize);
    146     key[1] = transformKey;
    147     return true;
    148 }
    149 
    150 static bool gen_meta_key(const GrXferProcessor& xp,
    151                          const GrShaderCaps& shaderCaps,
    152                          GrProcessorKeyBuilder* b) {
    153     size_t processorKeySize = b->size();
    154     uint32_t classID = xp.classID();
    155 
    156     // Currently we allow 16 bits for the class id and the overall processor key size.
    157     static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
    158     if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
    159         return false;
    160     }
    161 
    162     b->add32((classID << 16) | SkToU32(processorKeySize));
    163     return true;
    164 }
    165 
    166 static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
    167                                         const GrFragmentProcessor& fp,
    168                                         GrGpu* gpu,
    169                                         const GrShaderCaps& shaderCaps,
    170                                         GrProcessorKeyBuilder* b) {
    171     for (int i = 0; i < fp.numChildProcessors(); ++i) {
    172         if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
    173             return false;
    174         }
    175     }
    176 
    177     fp.getGLSLProcessorKey(shaderCaps, b);
    178 
    179     return gen_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
    180                                                                       fp.numCoordTransforms()), b);
    181 }
    182 
    183 bool GrProgramDesc::Build(
    184         GrProgramDesc* desc, const GrRenderTarget* renderTarget,
    185         const GrPrimitiveProcessor& primProc, bool hasPointSize, const GrPipeline& pipeline,
    186         GrGpu* gpu) {
    187     // The descriptor is used as a cache key. Thus when a field of the
    188     // descriptor will not affect program generation (because of the attribute
    189     // bindings in use or other descriptor field settings) it should be set
    190     // to a canonical value to avoid duplicate programs with different keys.
    191 
    192     const GrShaderCaps& shaderCaps = *gpu->caps()->shaderCaps();
    193 
    194     GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
    195     // Make room for everything up to the effect keys.
    196     desc->key().reset();
    197     desc->key().push_back_n(kProcessorKeysOffset);
    198 
    199     GrProcessorKeyBuilder b(&desc->key());
    200 
    201     primProc.getGLSLProcessorKey(shaderCaps, &b);
    202     primProc.getAttributeKey(&b);
    203     if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
    204         desc->key().reset();
    205         return false;
    206     }
    207     GrProcessor::CustomFeatures processorFeatures = primProc.requestedFeatures();
    208 
    209     for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
    210         const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
    211         if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu, shaderCaps, &b)) {
    212             desc->key().reset();
    213             return false;
    214         }
    215         processorFeatures |= fp.requestedFeatures();
    216     }
    217 
    218     const GrXferProcessor& xp = pipeline.getXferProcessor();
    219     const GrSurfaceOrigin* originIfDstTexture = nullptr;
    220     GrSurfaceOrigin origin;
    221     if (pipeline.dstTextureProxy()) {
    222         origin = pipeline.dstTextureProxy()->origin();
    223         originIfDstTexture = &origin;
    224     }
    225     xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
    226     if (!gen_meta_key(xp, shaderCaps, &b)) {
    227         desc->key().reset();
    228         return false;
    229     }
    230     processorFeatures |= xp.requestedFeatures();
    231 
    232     if (processorFeatures & GrProcessor::CustomFeatures::kSampleLocations) {
    233         SkASSERT(pipeline.isHWAntialiasState());
    234         b.add32(renderTarget->renderTargetPriv().getSamplePatternKey(pipeline));
    235     }
    236 
    237     // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
    238     // Because header is a pointer into the dynamic array, we can't push any new data into the key
    239     // below here.
    240     KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
    241 
    242     // make sure any padding in the header is zeroed.
    243     memset(header, 0, kHeaderSize);
    244     header->fOutputSwizzle = shaderCaps.configOutputSwizzle(renderTarget->config()).asKey();
    245     header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
    246     header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
    247     // Fail if the client requested more processors than the key can fit.
    248     if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
    249         header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
    250         return false;
    251     }
    252     header->fProcessorFeatures = (uint8_t)processorFeatures;
    253     SkASSERT(header->processorFeatures() == processorFeatures);  // Ensure enough bits.
    254     header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
    255     header->fHasPointSize = hasPointSize ? 1 : 0;
    256     header->fClampBlendInput =
    257             GrClampType::kManual == GrPixelConfigClampType(renderTarget->config()) ? 1 : 0;
    258     return true;
    259 }
    260