| 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 |
#include "src/gpu/ganesh/GrProgramDesc.h" |
| 8 |
|
| 9 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 10 |
#include "include/private/base/SkAssert.h" |
| 11 |
#include "include/private/base/SkTo.h" |
| 12 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 13 |
#include "src/gpu/KeyBuilder.h" |
| 14 |
#include "src/gpu/Swizzle.h" |
| 15 |
#include "src/gpu/ganesh/GrCaps.h" |
| 16 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 17 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 18 |
#include "src/gpu/ganesh/GrPipeline.h" |
| 19 |
#include "src/gpu/ganesh/GrProcessor.h" |
| 20 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 21 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 22 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 23 |
#include "src/gpu/ganesh/GrXferProcessor.h" |
| 24 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 25 |
|
| 26 |
enum GrSurfaceOrigin : int; |
| 27 |
|
| 28 |
// Currently we allow 8 bits for the class id |
| 29 |
static constexpr uint32_t kClassIDBits = 8; |
| 30 |
static constexpr uint32_t kSamplerOrImageTypeKeyBits = 4; |
| 31 |
|
| 32 |
static inline uint16_t texture_type_key(GrTextureType type) { |
| 33 |
int value = UINT16_MAX; |
| 34 |
switch (type) { |
| 35 |
case GrTextureType::k2D: |
| 36 |
value = 0; |
| 37 |
break; |
| 38 |
case GrTextureType::kExternal: |
| 39 |
value = 1; |
| 40 |
break; |
| 41 |
case GrTextureType::kRectangle: |
| 42 |
value = 2; |
| 43 |
break; |
| 44 |
default: |
| 45 |
SK_ABORT("Unexpected texture type"); |
| 46 |
} |
| 47 |
SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value); |
| 48 |
return SkToU16(value); |
| 49 |
} |
| 50 |
|
| 51 |
static uint32_t sampler_key(GrTextureType textureType, const skgpu::Swizzle& swizzle, |
| 52 |
const GrCaps& caps) { |
| 53 |
int samplerTypeKey = texture_type_key(textureType); |
| 54 |
|
| 55 |
static_assert(2 == sizeof(swizzle.asKey())); |
| 56 |
uint16_t swizzleKey = swizzle.asKey(); |
| 57 |
return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits); |
| 58 |
} |
| 59 |
|
| 60 |
static void add_geomproc_sampler_keys(skgpu::KeyBuilder* b, |
| 61 |
const GrGeometryProcessor& geomProc, |
| 62 |
const GrCaps& caps) { |
| 63 |
int numTextureSamplers = geomProc.numTextureSamplers(); |
| 64 |
b->add32(numTextureSamplers, "ppNumSamplers"); |
| 65 |
for (int i = 0; i < numTextureSamplers; ++i) { |
| 66 |
const GrGeometryProcessor::TextureSampler& sampler = geomProc.textureSampler(i); |
| 67 |
const GrBackendFormat& backendFormat = sampler.backendFormat(); |
| 68 |
|
| 69 |
uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps); |
| 70 |
b->add32(samplerKey); |
| 71 |
|
| 72 |
caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Functions which emit processor key info into the key builder. |
| 78 |
* For every effect, we include the effect's class ID (different for every GrProcessor subclass), |
| 79 |
* any information generated by the effect itself (addToKey), and some meta-information. |
| 80 |
* Shader code may be dependent on properties of the effect not placed in the key by the effect |
| 81 |
* (e.g. pixel format of textures used). |
| 82 |
*/ |
| 83 |
static void gen_geomproc_key(const GrGeometryProcessor& geomProc, |
| 84 |
const GrCaps& caps, |
| 85 |
skgpu::KeyBuilder* b) { |
| 86 |
b->appendComment(geomProc.name()); |
| 87 |
b->addBits(kClassIDBits, geomProc.classID(), "geomProcClassID"); |
| 88 |
|
| 89 |
geomProc.addToKey(*caps.shaderCaps(), b); |
| 90 |
geomProc.getAttributeKey(b); |
| 91 |
|
| 92 |
add_geomproc_sampler_keys(b, geomProc, caps); |
| 93 |
} |
| 94 |
|
| 95 |
// This corresponds to GrGLSLProgramBuilder::emitAndInstallXferProc |
| 96 |
static void gen_xp_key(const GrXferProcessor& xp, |
| 97 |
const GrCaps& caps, |
| 98 |
const GrPipeline& pipeline, |
| 99 |
skgpu::KeyBuilder* b) { |
| 100 |
b->appendComment(xp.name()); |
| 101 |
b->addBits(kClassIDBits, xp.classID(), "xpClassID"); |
| 102 |
|
| 103 |
xp.addToKey(*caps.shaderCaps(), b); |
| 104 |
} |
| 105 |
|
| 106 |
static void gen_fp_key(const GrFragmentProcessor& fp, |
| 107 |
const GrCaps& caps, |
| 108 |
skgpu::KeyBuilder* b) { |
| 109 |
b->appendComment(fp.name()); |
| 110 |
b->addBits(kClassIDBits, fp.classID(), "fpClassID"); |
| 111 |
b->addBits(GrGeometryProcessor::kCoordTransformKeyBits, |
| 112 |
GrGeometryProcessor::ComputeCoordTransformsKey(fp), "fpTransforms"); |
| 113 |
|
| 114 |
if (auto* te = fp.asTextureEffect()) { |
| 115 |
const GrBackendFormat& backendFormat = te->view().proxy()->backendFormat(); |
| 116 |
uint32_t samplerKey = sampler_key(backendFormat.textureType(), te->view().swizzle(), caps); |
| 117 |
b->add32(samplerKey, "fpSamplerKey"); |
| 118 |
caps.addExtraSamplerKey(b, te->samplerState(), backendFormat); |
| 119 |
} |
| 120 |
|
| 121 |
fp.addToKey(*caps.shaderCaps(), b); |
| 122 |
b->add32(fp.numChildProcessors(), "fpNumChildren"); |
| 123 |
|
| 124 |
for (int i = 0; i < fp.numChildProcessors(); ++i) { |
| 125 |
if (auto child = fp.childProcessor(i)) { |
| 126 |
gen_fp_key(*child, caps, b); |
| 127 |
} else { |
| 128 |
// Fold in a sentinel value as the "class ID" for any null children |
| 129 |
b->appendComment("Null"); |
| 130 |
b->addBits(kClassIDBits, GrProcessor::ClassID::kNull_ClassID, "fpClassID"); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// This corresponds to GrGLSLProgramBuilder::emitAndInstallDstTexture |
| 136 |
static void gen_dstTexture_key(const GrCaps& caps, |
| 137 |
const GrPipeline& pipeline, |
| 138 |
skgpu::KeyBuilder* b) { |
| 139 |
|
| 140 |
const GrSurfaceProxyView& dstView = pipeline.dstProxyView(); |
| 141 |
if (dstView.proxy()) { |
| 142 |
uint32_t samplerKey = sampler_key(dstView.proxy()->backendFormat().textureType(), |
| 143 |
dstView.swizzle(), caps); |
| 144 |
b->add32(samplerKey); |
| 145 |
|
| 146 |
b->addBool(kTopLeft_GrSurfaceOrigin == dstView.origin(), "surfaceOrigin"); |
| 147 |
b->addBool(pipeline.usesDstInputAttachment(), "usesDstInputAttachment"); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
static void gen_key(skgpu::KeyBuilder* b, |
| 152 |
const GrProgramInfo& programInfo, |
| 153 |
const GrCaps& caps) { |
| 154 |
gen_geomproc_key(programInfo.geomProc(), caps, b); |
| 155 |
|
| 156 |
const GrPipeline& pipeline = programInfo.pipeline(); |
| 157 |
|
| 158 |
gen_dstTexture_key(caps, pipeline, b); |
| 159 |
|
| 160 |
// This corresponds to GrGLSLProgramBuilder::emitAndInstallFragProcs |
| 161 |
b->addBits(2, pipeline.numFragmentProcessors(), "numFPs"); |
| 162 |
b->addBits(1, pipeline.numColorFragmentProcessors(), "numColorFPs"); |
| 163 |
for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
| 164 |
gen_fp_key(pipeline.getFragmentProcessor(i), caps, b); |
| 165 |
} |
| 166 |
|
| 167 |
gen_xp_key(pipeline.getXferProcessor(), caps, pipeline, b); |
| 168 |
|
| 169 |
b->addBits(16, pipeline.writeSwizzle().asKey(), "writeSwizzle"); |
| 170 |
b->addBool(pipeline.snapVerticesToPixelCenters(), "snapVertices"); |
| 171 |
// The base descriptor only stores whether or not the primitiveType is kPoints. Backend- |
| 172 |
// specific versions (e.g., Vulkan) require more detail |
| 173 |
b->addBool((programInfo.primitiveType() == GrPrimitiveType::kPoints), "isPoints"); |
| 174 |
|
| 175 |
// Put a clean break between the "common" data written by this function, and any backend data |
| 176 |
// appended later. The initial key length will just be this portion (rounded to 4 bytes). |
| 177 |
b->flush(); |
| 178 |
} |
| 179 |
|
| 180 |
void GrProgramDesc::Build(GrProgramDesc* desc, |
| 181 |
const GrProgramInfo& programInfo, |
| 182 |
const GrCaps& caps) { |
| 183 |
desc->reset(); |
| 184 |
skgpu::KeyBuilder b(desc->key()); |
| 185 |
gen_key(&b, programInfo, caps); |
| 186 |
desc->fInitialKeyLength = desc->keyLength(); |
| 187 |
} |
| 188 |
|
| 189 |
SkString GrProgramDesc::Describe(const GrProgramInfo& programInfo, |
| 190 |
const GrCaps& caps) { |
| 191 |
GrProgramDesc desc; |
| 192 |
skgpu::StringKeyBuilder b(desc.key()); |
| 193 |
gen_key(&b, programInfo, caps); |
| 194 |
b.flush(); |
| 195 |
return b.description(); |
| 196 |
} |