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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrBackendProcessorFactory.h" |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 12 | #include "GrColor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | #include "GrProcessorUnitTest.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 14 | #include "GrProgramElement.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 15 | #include "GrTextureAccess.h" |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 16 | #include "SkMath.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 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 22 | /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be |
| 23 | immutable: after being constructed, their fields may not change. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 24 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 25 | Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 26 | processor must reach 0 before the thread terminates and the pool is destroyed. To create a |
| 27 | static processor use the helper macro GR_CREATE_STATIC_PROCESSOR declared below. |
| 28 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 29 | class GrProcessor : public GrProgramElement { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 30 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 31 | SK_DECLARE_INST_COUNT(GrProcessor) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 32 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 33 | virtual ~GrProcessor(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 35 | /** |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 36 | * This function is used to perform optimizations. When called the invarientOuput param |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 37 | * indicate whether the input components to this processor in the FS will have known values. |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 38 | * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent |
| 39 | * member indicates whether the input will be 1 or 4 bytes. The function updates the members of |
| 40 | * inout to indicate known values of its output. A component of the color member only has |
| 41 | * meaning if the corresponding bit in validFlags is set. |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 42 | */ |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame^] | 43 | void computeInvariantOutput(GrInvariantOutput* inout) const; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 45 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 46 | identification. The factory should be an instance of templated class, |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 47 | GrTBackendProcessorFactory. It is templated on the subclass of GrProcessor. The subclass |
| 48 | must have a nested type (or typedef) named GLProcessor which will be the subclass of |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 49 | GrGLProcessor created by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 50 | |
| 51 | Example: |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 52 | class MyCustomProcessor : public GrProcessor { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 53 | ... |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 54 | virtual const GrBackendProcessorFactory& getFactory() const SK_OVERRIDE { |
| 55 | return GrTBackendProcessorFactory<MyCustomProcessor>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 56 | } |
| 57 | ... |
| 58 | }; |
| 59 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | virtual const GrBackendProcessorFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +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. */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 64 | const char* name() const; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 66 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 67 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 68 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 69 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 70 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 71 | |
| 72 | /** Shortcut for textureAccess(index).texture(); */ |
| 73 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 74 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 75 | /** Will this processor read the fragment position? */ |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 76 | bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 77 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 78 | void* operator new(size_t size); |
| 79 | void operator delete(void* target); |
| 80 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 81 | void* operator new(size_t size, void* placement) { |
| 82 | return ::operator new(size, placement); |
| 83 | } |
| 84 | void operator delete(void* target, void* placement) { |
| 85 | ::operator delete(target, placement); |
| 86 | } |
| 87 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 88 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 89 | * Helper for down-casting to a GrProcessor subclass |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 90 | */ |
| 91 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 92 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 93 | protected: |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 94 | GrProcessor() : fWillReadFragmentPosition(false) {} |
| 95 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 96 | /** |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 97 | * 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] | 98 | * 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] | 99 | * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be |
| 100 | * called from the constructor because GrProcessors are immutable. |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 101 | */ |
| 102 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 103 | |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 104 | bool hasSameTextureAccesses(const GrProcessor&) const; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 107 | * If the prcoessor will generate a backend-specific processor that will read the fragment |
| 108 | * position in the FS then it must call this method from its constructor. Otherwise, the |
| 109 | * request to access the fragment position will be denied. |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 110 | */ |
| 111 | void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 112 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 113 | private: |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Subclass implements this to support getConstantColorComponents(...). |
| 116 | */ |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame^] | 117 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 118 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 119 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 120 | bool fWillReadFragmentPosition; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 121 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 122 | typedef GrProgramElement INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * This creates a processor outside of the memory pool. The processor's destructor will be called |
| 128 | * at global destruction time. NAME will be the name of the created instance. |
| 129 | */ |
| 130 | #define GR_CREATE_STATIC_PROCESSOR(NAME, PROC_CLASS, ARGS) \ |
| 131 | static SkAlignedSStorage<sizeof(PROC_CLASS)> g_##NAME##_Storage; \ |
| 132 | static PROC_CLASS* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), PROC_CLASS, ARGS); \ |
| 133 | static SkAutoTDestroy<GrProcessor> NAME##_ad(NAME); |
| 134 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 135 | #endif |