blob: 27fce2e26764e0cc5a1594f1fa206389169eb99c [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
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
joshualittb0a8a372014-09-23 09:50:21 -07008#ifndef GrProcessor_DEFINED
9#define GrProcessor_DEFINED
tomhudson@google.com168e6342012-04-18 17:49:20 +000010
bsalomon@google.com371e1052013-01-11 21:08:55 +000011#include "GrColor.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "GrProcessorUnitTest.h"
bsalomon95740982014-09-04 13:12:37 -070013#include "GrProgramElement.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000014#include "GrTextureAccess.h"
cdalton74b8d322016-04-11 14:47:28 -070015#include "GrBufferAccess.h"
egdaniel9e4d6d12014-10-15 13:49:02 -070016#include "SkMath.h"
robertphillipse004bfc2015-11-16 09:06:59 -080017#include "SkString.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000018
tomhudson@google.com168e6342012-04-18 17:49:20 +000019class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000020class GrCoordTransform;
egdaniel605dd0f2014-11-12 08:35:25 -080021class GrInvariantOutput;
bsalomon95740982014-09-04 13:12:37 -070022
joshualitteb2a6762014-12-04 11:35:33 -080023/**
24 * Used by processors to build their keys. It incorporates each per-processor key into a larger
25 * shader key.
26 */
27class GrProcessorKeyBuilder {
28public:
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
48private:
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
bsalomon98b33eb2014-10-15 11:05:26 -070053/** 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.com0ac6af42013-01-16 15:16:18 +000055
joshualittb0a8a372014-09-23 09:50:21 -070056 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
mdempsky38f1f6f2015-08-27 12:57:01 -070057 processor must reach 0 before the thread terminates and the pool is destroyed.
bsalomon98b33eb2014-10-15 11:05:26 -070058 */
joshualittb0a8a372014-09-23 09:50:21 -070059class GrProcessor : public GrProgramElement {
tomhudson@google.com168e6342012-04-18 17:49:20 +000060public:
joshualittb0a8a372014-09-23 09:50:21 -070061 virtual ~GrProcessor();
tomhudson@google.com168e6342012-04-18 17:49:20 +000062
bsalomon98b33eb2014-10-15 11:05:26 -070063 /** Human-meaningful string to identify this prcoessor; may be embedded
twiz@google.coma5e65ec2012-08-02 15:15:16 +000064 in generated shader code. */
joshualitteb2a6762014-12-04 11:35:33 -080065 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000066
robertphillipse004bfc2015-11-16 09:06:59 -080067 // Human-readable dump of all information
68 virtual SkString dumpInfo() const {
69 SkString str;
70 str.appendf("Missing data");
71 return str;
72 }
73
bsalomon@google.com50db75c2013-01-11 13:54:30 +000074 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000075
bsalomon@google.com6d003d12012-09-11 15:45:20 +000076 /** Returns the access pattern for the texture at index. index must be valid according to
77 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +000078 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000079
80 /** Shortcut for textureAccess(index).texture(); */
81 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000082
cdalton74b8d322016-04-11 14:47:28 -070083 int numBuffers() const { return fBufferAccesses.count(); }
84
85 /** Returns the access pattern for the buffer at index. index must be valid according to
86 numBuffers(). */
87 const GrBufferAccess& bufferAccess(int index) const {
88 return *fBufferAccesses[index];
89 }
90
cdalton87332102016-02-26 12:22:02 -080091 /**
92 * Platform specific built-in features that a processor can request for the fragment shader.
93 */
94 enum RequiredFeatures {
95 kNone_RequiredFeatures = 0,
cdalton28f45b92016-03-07 13:58:26 -080096 kFragmentPosition_RequiredFeature = 1 << 0,
97 kSampleLocations_RequiredFeature = 1 << 1
cdalton87332102016-02-26 12:22:02 -080098 };
99
100 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
101
102 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000103
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000104 void* operator new(size_t size);
105 void operator delete(void* target);
106
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000107 void* operator new(size_t size, void* placement) {
108 return ::operator new(size, placement);
109 }
110 void operator delete(void* target, void* placement) {
111 ::operator delete(target, placement);
112 }
113
joshualitt49586be2014-09-16 08:21:41 -0700114 /**
joshualittb0a8a372014-09-23 09:50:21 -0700115 * Helper for down-casting to a GrProcessor subclass
joshualitt49586be2014-09-16 08:21:41 -0700116 */
117 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
118
joshualitteb2a6762014-12-04 11:35:33 -0800119 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
120
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000121protected:
cdalton87332102016-02-26 12:22:02 -0800122 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700123
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000124 /**
cdalton74b8d322016-04-11 14:47:28 -0700125 * Subclasses call these from their constructor to register sampler sources. The processor
126 * subclass manages the lifetime of the objects (these functions only store pointers). The
127 * GrTextureAccess and/or GrBufferAccess instances are typically member fields of the
128 * GrProcessor subclass. These must only be called from the constructor because GrProcessors
129 * are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000130 */
wangyix93ab2542015-08-19 08:23:12 -0700131 virtual void addTextureAccess(const GrTextureAccess* textureAccess);
cdalton74b8d322016-04-11 14:47:28 -0700132 virtual void addBufferAccess(const GrBufferAccess* bufferAccess);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000133
cdalton74b8d322016-04-11 14:47:28 -0700134 bool hasSameSamplers(const GrProcessor&) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000135
136 /**
cdalton28f45b92016-03-07 13:58:26 -0800137 * If the prcoessor will generate code that uses platform specific built-in features, then it
138 * must call these methods from its constructor. Otherwise, requests to use these features will
139 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000140 */
cdalton87332102016-02-26 12:22:02 -0800141 void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; }
cdalton28f45b92016-03-07 13:58:26 -0800142 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800143
144 void combineRequiredFeatures(const GrProcessor& other) {
145 fRequiredFeatures |= other.fRequiredFeatures;
146 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000147
joshualitteb2a6762014-12-04 11:35:33 -0800148 template <typename PROC_SUBCLASS> void initClassID() {
149 static uint32_t kClassID = GenClassID();
150 fClassID = kClassID;
151 }
152
153 uint32_t fClassID;
wangyix58d890b2015-08-12 09:40:47 -0700154 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
cdalton74b8d322016-04-11 14:47:28 -0700155 SkSTArray<2, const GrBufferAccess*, true> fBufferAccesses;
joshualitteb2a6762014-12-04 11:35:33 -0800156
bsalomon0e08fc12014-10-15 08:19:04 -0700157private:
joshualitteb2a6762014-12-04 11:35:33 -0800158 static uint32_t GenClassID() {
159 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
160 // atomic inc returns the old value not the incremented value. So we add
161 // 1 to the returned value.
162 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
163 if (!id) {
164 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
165 "subclass.");
166 }
167 return id;
168 }
169
170 enum {
171 kIllegalProcessorClassID = 0,
172 };
173 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000174
cdalton87332102016-02-26 12:22:02 -0800175 RequiredFeatures fRequiredFeatures;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000176
bsalomon95740982014-09-04 13:12:37 -0700177 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000178};
179
cdalton87332102016-02-26 12:22:02 -0800180GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
181
tomhudson@google.com168e6342012-04-18 17:49:20 +0000182#endif