GrProgramDesc.cpp 11 KB

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