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" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 14 | #include "GrTextureAccess.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" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 17 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 18 | class GrContext; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 19 | class GrCoordTransform; |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 20 | class GrInvariantOutput; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 21 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Used by processors to build their keys. It incorporates each per-processor key into a larger |
| 24 | * shader key. |
| 25 | */ |
| 26 | class GrProcessorKeyBuilder { |
| 27 | public: |
| 28 | GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) { |
| 29 | SkASSERT(0 == fData->count() % sizeof(uint32_t)); |
| 30 | } |
| 31 | |
| 32 | void add32(uint32_t v) { |
| 33 | ++fCount; |
| 34 | fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); |
| 35 | } |
| 36 | |
| 37 | /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next |
| 38 | add*() call. */ |
| 39 | uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { |
| 40 | SkASSERT(count > 0); |
| 41 | fCount += count; |
| 42 | return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); |
| 43 | } |
| 44 | |
| 45 | size_t size() const { return sizeof(uint32_t) * fCount; } |
| 46 | |
| 47 | private: |
| 48 | SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. |
| 49 | int fCount; // number of uint32_ts added to fData by the processor. |
| 50 | }; |
| 51 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 52 | /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be |
| 53 | immutable: after being constructed, their fields may not change. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 54 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | 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] | 56 | processor must reach 0 before the thread terminates and the pool is destroyed. |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 57 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | class GrProcessor : public GrProgramElement { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 59 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | virtual ~GrProcessor(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 61 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 62 | /** Human-meaningful string to identify this prcoessor; may be embedded |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 63 | in generated shader code. */ |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 64 | virtual const char* name() const = 0; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 65 | |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 66 | // Human-readable dump of all information |
| 67 | virtual SkString dumpInfo() const { |
| 68 | SkString str; |
| 69 | str.appendf("Missing data"); |
| 70 | return str; |
| 71 | } |
| 72 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 73 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 75 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 76 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 77 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 78 | |
| 79 | /** Shortcut for textureAccess(index).texture(); */ |
| 80 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 81 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Platform specific built-in features that a processor can request for the fragment shader. |
| 84 | */ |
| 85 | enum RequiredFeatures { |
| 86 | kNone_RequiredFeatures = 0, |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame^] | 87 | kFragmentPosition_RequiredFeature = 1 << 0, |
| 88 | kSampleLocations_RequiredFeature = 1 << 1 |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures); |
| 92 | |
| 93 | RequiredFeatures requiredFeatures() const { return fRequiredFeatures; } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 94 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 95 | void* operator new(size_t size); |
| 96 | void operator delete(void* target); |
| 97 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 98 | void* operator new(size_t size, void* placement) { |
| 99 | return ::operator new(size, placement); |
| 100 | } |
| 101 | void operator delete(void* target, void* placement) { |
| 102 | ::operator delete(target, placement); |
| 103 | } |
| 104 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 105 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 106 | * Helper for down-casting to a GrProcessor subclass |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 107 | */ |
| 108 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 109 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 110 | uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; } |
| 111 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 112 | protected: |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 113 | GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {} |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 114 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 115 | /** |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 116 | * Subclasses call this from their constructor to register GrTextureAccesses. The processor |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 117 | * subclass manages the lifetime of the accesses (this function only stores a pointer). The |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 118 | * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be |
| 119 | * called from the constructor because GrProcessors are immutable. |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 120 | */ |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 121 | virtual void addTextureAccess(const GrTextureAccess* textureAccess); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 122 | |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 123 | bool hasSameTextureAccesses(const GrProcessor&) const; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame^] | 126 | * If the prcoessor will generate code that uses platform specific built-in features, then it |
| 127 | * must call these methods from its constructor. Otherwise, requests to use these features will |
| 128 | * be denied. |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 129 | */ |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 130 | void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame^] | 131 | void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 132 | |
| 133 | void combineRequiredFeatures(const GrProcessor& other) { |
| 134 | fRequiredFeatures |= other.fRequiredFeatures; |
| 135 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 136 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 137 | template <typename PROC_SUBCLASS> void initClassID() { |
| 138 | static uint32_t kClassID = GenClassID(); |
| 139 | fClassID = kClassID; |
| 140 | } |
| 141 | |
| 142 | uint32_t fClassID; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 143 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 144 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 145 | private: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 146 | static uint32_t GenClassID() { |
| 147 | // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The |
| 148 | // atomic inc returns the old value not the incremented value. So we add |
| 149 | // 1 to the returned value. |
| 150 | uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1; |
| 151 | if (!id) { |
| 152 | SkFAIL("This should never wrap as it should only be called once for each GrProcessor " |
| 153 | "subclass."); |
| 154 | } |
| 155 | return id; |
| 156 | } |
| 157 | |
| 158 | enum { |
| 159 | kIllegalProcessorClassID = 0, |
| 160 | }; |
| 161 | static int32_t gCurrProcessorClassID; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 162 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 163 | RequiredFeatures fRequiredFeatures; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 164 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 165 | typedef GrProgramElement INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 168 | GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures); |
| 169 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 170 | #endif |