GrProgramDesc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2014 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. #ifndef GrProgramDesc_DEFINED
  8. #define GrProgramDesc_DEFINED
  9. #include "include/private/GrTypesPriv.h"
  10. #include "include/private/SkTArray.h"
  11. #include "include/private/SkTo.h"
  12. #include "src/core/SkOpts.h"
  13. #include "src/gpu/GrColor.h"
  14. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  15. class GrShaderCaps;
  16. class GrPipeline;
  17. class GrPrimitiveProcessor;
  18. /** This class describes a program to generate. It also serves as a program cache key */
  19. class GrProgramDesc {
  20. public:
  21. // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
  22. GrProgramDesc() {}
  23. /**
  24. * Builds a program descriptor. Before the descriptor can be used, the client must call finalize
  25. * on the returned GrProgramDesc.
  26. *
  27. * @param GrPrimitiveProcessor The geometry
  28. * @param hasPointSize Controls whether the shader will output a point size.
  29. * @param GrPipeline The optimized drawstate. The descriptor will represent a program
  30. * which this optstate can use to draw with. The optstate contains
  31. * general draw information, as well as the specific color, geometry,
  32. * and coverage stages which will be used to generate the GL Program for
  33. * this optstate.
  34. * @param GrGpu Ptr to the GrGpu object the program will be used with.
  35. * @param GrProgramDesc The built and finalized descriptor
  36. **/
  37. static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrPrimitiveProcessor&,
  38. bool hasPointSize, const GrPipeline&, GrGpu*);
  39. // Returns this as a uint32_t array to be used as a key in the program cache.
  40. const uint32_t* asKey() const {
  41. return reinterpret_cast<const uint32_t*>(fKey.begin());
  42. }
  43. // Gets the number of bytes in asKey(). It will be a 4-byte aligned value.
  44. uint32_t keyLength() const {
  45. SkASSERT(0 == (fKey.count() % 4));
  46. return fKey.count();
  47. }
  48. GrProgramDesc& operator= (const GrProgramDesc& other) {
  49. uint32_t keyLength = other.keyLength();
  50. fKey.reset(SkToInt(keyLength));
  51. memcpy(fKey.begin(), other.fKey.begin(), keyLength);
  52. return *this;
  53. }
  54. bool operator== (const GrProgramDesc& that) const {
  55. if (this->keyLength() != that.keyLength()) {
  56. return false;
  57. }
  58. SkASSERT(SkIsAlign4(this->keyLength()));
  59. int l = this->keyLength() >> 2;
  60. const uint32_t* aKey = this->asKey();
  61. const uint32_t* bKey = that.asKey();
  62. for (int i = 0; i < l; ++i) {
  63. if (aKey[i] != bKey[i]) {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. bool operator!= (const GrProgramDesc& other) const {
  70. return !(*this == other);
  71. }
  72. void setSurfaceOriginKey(int key) {
  73. KeyHeader* header = this->atOffset<KeyHeader, kHeaderOffset>();
  74. header->fSurfaceOriginKey = key;
  75. }
  76. struct KeyHeader {
  77. bool hasSurfaceOriginKey() const {
  78. return SkToBool(fSurfaceOriginKey);
  79. }
  80. GrProcessor::CustomFeatures processorFeatures() const {
  81. return (GrProcessor::CustomFeatures)fProcessorFeatures;
  82. }
  83. // Set to uniquely idenitify any swizzling of the shader's output color(s).
  84. uint16_t fOutputSwizzle;
  85. uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required.
  86. uint8_t fCoverageFragmentProcessorCnt;
  87. // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info.
  88. uint8_t fSurfaceOriginKey : 2;
  89. uint8_t fProcessorFeatures : 1;
  90. bool fSnapVerticesToPixelCenters : 1;
  91. bool fHasPointSize : 1;
  92. bool fClampBlendInput : 1;
  93. uint8_t fPad : 2;
  94. };
  95. GR_STATIC_ASSERT(sizeof(KeyHeader) == 6);
  96. // This should really only be used internally, base classes should return their own headers
  97. const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
  98. protected:
  99. template<typename T, size_t OFFSET> T* atOffset() {
  100. return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
  101. }
  102. template<typename T, size_t OFFSET> const T* atOffset() const {
  103. return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
  104. }
  105. // The key, stored in fKey, is composed of two parts:
  106. // 1. Header struct defined above.
  107. // 2. A Backend specific payload which includes the per-processor keys.
  108. enum KeyOffsets {
  109. kHeaderOffset = 0,
  110. kHeaderSize = SkAlign4(sizeof(KeyHeader)),
  111. // Part 4.
  112. // This is the offset into the backenend specific part of the key, which includes
  113. // per-processor keys.
  114. kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
  115. };
  116. enum {
  117. kMaxPreallocProcessors = 8,
  118. kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
  119. kPreAllocSize = kHeaderOffset + kHeaderSize +
  120. kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
  121. };
  122. SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
  123. const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
  124. private:
  125. SkSTArray<kPreAllocSize, uint8_t, true> fKey;
  126. };
  127. #endif