tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 8 | #ifndef GrProcessor_DEFINED |
| 9 | #define GrProcessor_DEFINED |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 11 | #include "GrColor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "GrProcessorUnitTest.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 13 | #include "GrProgramElement.h" |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 14 | #include "GrBufferAccess.h" |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 15 | #include "SkMath.h" |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 16 | #include "SkString.h" |
mtklein | 59c12e3 | 2016-05-02 07:19:41 -0700 | [diff] [blame] | 17 | #include "../private/SkAtomics.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 18 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 19 | class GrContext; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 20 | class GrCoordTransform; |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 21 | class GrInvariantOutput; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 22 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Used by processors to build their keys. It incorporates each per-processor key into a larger |
| 25 | * shader key. |
| 26 | */ |
| 27 | class GrProcessorKeyBuilder { |
| 28 | public: |
| 29 | GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) { |
| 30 | SkASSERT(0 == fData->count() % sizeof(uint32_t)); |
| 31 | } |
| 32 | |
| 33 | void add32(uint32_t v) { |
| 34 | ++fCount; |
| 35 | fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); |
| 36 | } |
| 37 | |
| 38 | /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next |
| 39 | add*() call. */ |
| 40 | uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { |
| 41 | SkASSERT(count > 0); |
| 42 | fCount += count; |
| 43 | return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); |
| 44 | } |
| 45 | |
| 46 | size_t size() const { return sizeof(uint32_t) * fCount; } |
| 47 | |
| 48 | private: |
| 49 | SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. |
| 50 | int fCount; // number of uint32_ts added to fData by the processor. |
| 51 | }; |
| 52 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 53 | /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be |
| 54 | immutable: after being constructed, their fields may not change. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 55 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 56 | Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an |
mdempsky | 38f1f6f | 2015-08-27 12:57:01 -0700 | [diff] [blame] | 57 | processor must reach 0 before the thread terminates and the pool is destroyed. |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 58 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 59 | class GrProcessor : public GrProgramElement { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 60 | public: |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 61 | class TextureSampler; |
| 62 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 63 | virtual ~GrProcessor(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 64 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 65 | /** Human-meaningful string to identify this prcoessor; may be embedded |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 66 | in generated shader code. */ |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 67 | virtual const char* name() const = 0; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 68 | |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 69 | // Human-readable dump of all information |
| 70 | virtual SkString dumpInfo() const { |
| 71 | SkString str; |
| 72 | str.appendf("Missing data"); |
| 73 | return str; |
| 74 | } |
| 75 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 76 | int numTextureSamplers() const { return fTextureSamplers.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 77 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 78 | /** Returns the access pattern for the texture at index. index must be valid according to |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 79 | numTextureSamplers(). */ |
| 80 | const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 81 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 82 | int numBuffers() const { return fBufferAccesses.count(); } |
| 83 | |
| 84 | /** Returns the access pattern for the buffer at index. index must be valid according to |
| 85 | numBuffers(). */ |
| 86 | const GrBufferAccess& bufferAccess(int index) const { |
| 87 | return *fBufferAccesses[index]; |
| 88 | } |
| 89 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Platform specific built-in features that a processor can request for the fragment shader. |
| 92 | */ |
| 93 | enum RequiredFeatures { |
| 94 | kNone_RequiredFeatures = 0, |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 95 | kFragmentPosition_RequiredFeature = 1 << 0, |
| 96 | kSampleLocations_RequiredFeature = 1 << 1 |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures); |
| 100 | |
| 101 | RequiredFeatures requiredFeatures() const { return fRequiredFeatures; } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 102 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 103 | void* operator new(size_t size); |
| 104 | void operator delete(void* target); |
| 105 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 106 | void* operator new(size_t size, void* placement) { |
| 107 | return ::operator new(size, placement); |
| 108 | } |
| 109 | void operator delete(void* target, void* placement) { |
| 110 | ::operator delete(target, placement); |
| 111 | } |
| 112 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 113 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 114 | * Helper for down-casting to a GrProcessor subclass |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 115 | */ |
| 116 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 117 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 118 | uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; } |
| 119 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 120 | protected: |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 121 | GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {} |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 122 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 123 | /** |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 124 | * Subclasses call these from their constructor to register sampler sources. The processor |
| 125 | * subclass manages the lifetime of the objects (these functions only store pointers). The |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 126 | * TextureSampler and/or GrBufferAccess instances are typically member fields of the |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 127 | * GrProcessor subclass. These must only be called from the constructor because GrProcessors |
| 128 | * are immutable. |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 129 | */ |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 130 | void addTextureSampler(const TextureSampler*); |
Brian Salomon | 55e0346 | 2016-10-28 11:25:52 -0400 | [diff] [blame] | 131 | void addBufferAccess(const GrBufferAccess* bufferAccess); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 132 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 133 | bool hasSameSamplers(const GrProcessor&) const; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 136 | * If the prcoessor will generate code that uses platform specific built-in features, then it |
| 137 | * must call these methods from its constructor. Otherwise, requests to use these features will |
| 138 | * be denied. |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 139 | */ |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 140 | void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 141 | void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 142 | |
| 143 | void combineRequiredFeatures(const GrProcessor& other) { |
| 144 | fRequiredFeatures |= other.fRequiredFeatures; |
| 145 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 146 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 147 | template <typename PROC_SUBCLASS> void initClassID() { |
| 148 | static uint32_t kClassID = GenClassID(); |
| 149 | fClassID = kClassID; |
| 150 | } |
| 151 | |
| 152 | uint32_t fClassID; |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 153 | private: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 154 | static uint32_t GenClassID() { |
| 155 | // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The |
| 156 | // atomic inc returns the old value not the incremented value. So we add |
| 157 | // 1 to the returned value. |
| 158 | uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1; |
| 159 | if (!id) { |
| 160 | SkFAIL("This should never wrap as it should only be called once for each GrProcessor " |
| 161 | "subclass."); |
| 162 | } |
| 163 | return id; |
| 164 | } |
| 165 | |
| 166 | enum { |
| 167 | kIllegalProcessorClassID = 0, |
| 168 | }; |
| 169 | static int32_t gCurrProcessorClassID; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 170 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 171 | RequiredFeatures fRequiredFeatures; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 172 | SkSTArray<4, const TextureSampler*, true> fTextureSamplers; |
| 173 | SkSTArray<2, const GrBufferAccess*, true> fBufferAccesses; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 174 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 175 | typedef GrProgramElement INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 178 | GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures); |
| 179 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 180 | /** |
| 181 | * Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with |
| 182 | * an associated GrTextureParams |
| 183 | */ |
| 184 | class GrProcessor::TextureSampler : public SkNoncopyable { |
| 185 | public: |
| 186 | /** |
| 187 | * Must be initialized before adding to a GrProcessor's texture access list. |
| 188 | */ |
| 189 | TextureSampler(); |
| 190 | |
| 191 | TextureSampler(GrTexture*, const GrTextureParams&); |
| 192 | |
| 193 | explicit TextureSampler(GrTexture*, |
| 194 | GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode, |
| 195 | SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode, |
| 196 | GrShaderFlags visibility = kFragment_GrShaderFlag); |
| 197 | |
| 198 | void reset(GrTexture*, const GrTextureParams&, |
| 199 | GrShaderFlags visibility = kFragment_GrShaderFlag); |
| 200 | void reset(GrTexture*, |
| 201 | GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode, |
| 202 | SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode, |
| 203 | GrShaderFlags visibility = kFragment_GrShaderFlag); |
| 204 | |
| 205 | bool operator==(const TextureSampler& that) const { |
| 206 | return this->getTexture() == that.getTexture() && |
| 207 | fParams == that.fParams && |
| 208 | fVisibility == that.fVisibility; |
| 209 | } |
| 210 | |
| 211 | bool operator!=(const TextureSampler& other) const { return !(*this == other); } |
| 212 | |
| 213 | GrTexture* getTexture() const { return fTexture.get(); } |
| 214 | GrShaderFlags getVisibility() const { return fVisibility; } |
| 215 | |
| 216 | /** |
| 217 | * For internal use by GrProcessor. |
| 218 | */ |
| 219 | const GrGpuResourceRef* getProgramTexture() const { return &fTexture; } |
| 220 | |
| 221 | const GrTextureParams& getParams() const { return fParams; } |
| 222 | |
| 223 | private: |
| 224 | |
| 225 | typedef GrTGpuResourceRef<GrTexture> ProgramTexture; |
| 226 | |
| 227 | ProgramTexture fTexture; |
| 228 | GrTextureParams fParams; |
| 229 | GrShaderFlags fVisibility; |
| 230 | |
| 231 | typedef SkNoncopyable INHERITED; |
| 232 | }; |
| 233 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 234 | #endif |