blob: 7c9662e585a16d685daa6795df66ec7fd7972975 [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"
mtklein59c12e32016-05-02 07:19:41 -070018#include "../private/SkAtomics.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000019
tomhudson@google.com168e6342012-04-18 17:49:20 +000020class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000021class GrCoordTransform;
egdaniel605dd0f2014-11-12 08:35:25 -080022class GrInvariantOutput;
bsalomon95740982014-09-04 13:12:37 -070023
joshualitteb2a6762014-12-04 11:35:33 -080024/**
25 * Used by processors to build their keys. It incorporates each per-processor key into a larger
26 * shader key.
27 */
28class GrProcessorKeyBuilder {
29public:
30 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
31 SkASSERT(0 == fData->count() % sizeof(uint32_t));
32 }
33
34 void add32(uint32_t v) {
35 ++fCount;
36 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
37 }
38
39 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
40 add*() call. */
41 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
42 SkASSERT(count > 0);
43 fCount += count;
44 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
45 }
46
47 size_t size() const { return sizeof(uint32_t) * fCount; }
48
49private:
50 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
51 int fCount; // number of uint32_ts added to fData by the processor.
52};
53
bsalomon98b33eb2014-10-15 11:05:26 -070054/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
55 immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000056
joshualittb0a8a372014-09-23 09:50:21 -070057 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
mdempsky38f1f6f2015-08-27 12:57:01 -070058 processor must reach 0 before the thread terminates and the pool is destroyed.
bsalomon98b33eb2014-10-15 11:05:26 -070059 */
joshualittb0a8a372014-09-23 09:50:21 -070060class GrProcessor : public GrProgramElement {
tomhudson@google.com168e6342012-04-18 17:49:20 +000061public:
joshualittb0a8a372014-09-23 09:50:21 -070062 virtual ~GrProcessor();
tomhudson@google.com168e6342012-04-18 17:49:20 +000063
bsalomon98b33eb2014-10-15 11:05:26 -070064 /** Human-meaningful string to identify this prcoessor; may be embedded
twiz@google.coma5e65ec2012-08-02 15:15:16 +000065 in generated shader code. */
joshualitteb2a6762014-12-04 11:35:33 -080066 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000067
robertphillipse004bfc2015-11-16 09:06:59 -080068 // Human-readable dump of all information
69 virtual SkString dumpInfo() const {
70 SkString str;
71 str.appendf("Missing data");
72 return str;
73 }
74
bsalomon@google.com50db75c2013-01-11 13:54:30 +000075 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000076
bsalomon@google.com6d003d12012-09-11 15:45:20 +000077 /** Returns the access pattern for the texture at index. index must be valid according to
78 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +000079 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000080
81 /** Shortcut for textureAccess(index).texture(); */
82 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000083
cdalton74b8d322016-04-11 14:47:28 -070084 int numBuffers() const { return fBufferAccesses.count(); }
85
86 /** Returns the access pattern for the buffer at index. index must be valid according to
87 numBuffers(). */
88 const GrBufferAccess& bufferAccess(int index) const {
89 return *fBufferAccesses[index];
90 }
91
cdalton87332102016-02-26 12:22:02 -080092 /**
93 * Platform specific built-in features that a processor can request for the fragment shader.
94 */
95 enum RequiredFeatures {
96 kNone_RequiredFeatures = 0,
cdalton28f45b92016-03-07 13:58:26 -080097 kFragmentPosition_RequiredFeature = 1 << 0,
98 kSampleLocations_RequiredFeature = 1 << 1
cdalton87332102016-02-26 12:22:02 -080099 };
100
101 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
102
103 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000104
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000105 void* operator new(size_t size);
106 void operator delete(void* target);
107
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000108 void* operator new(size_t size, void* placement) {
109 return ::operator new(size, placement);
110 }
111 void operator delete(void* target, void* placement) {
112 ::operator delete(target, placement);
113 }
114
joshualitt49586be2014-09-16 08:21:41 -0700115 /**
joshualittb0a8a372014-09-23 09:50:21 -0700116 * Helper for down-casting to a GrProcessor subclass
joshualitt49586be2014-09-16 08:21:41 -0700117 */
118 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
119
joshualitteb2a6762014-12-04 11:35:33 -0800120 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
121
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000122protected:
cdalton87332102016-02-26 12:22:02 -0800123 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700124
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000125 /**
cdalton74b8d322016-04-11 14:47:28 -0700126 * Subclasses call these from their constructor to register sampler sources. The processor
127 * subclass manages the lifetime of the objects (these functions only store pointers). The
128 * GrTextureAccess and/or GrBufferAccess instances are typically member fields of the
129 * GrProcessor subclass. These must only be called from the constructor because GrProcessors
130 * are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000131 */
Brian Salomon55e03462016-10-28 11:25:52 -0400132 void addTextureAccess(const GrTextureAccess* textureAccess);
133 void addBufferAccess(const GrBufferAccess* bufferAccess);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000134
cdalton74b8d322016-04-11 14:47:28 -0700135 bool hasSameSamplers(const GrProcessor&) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000136
137 /**
cdalton28f45b92016-03-07 13:58:26 -0800138 * If the prcoessor will generate code that uses platform specific built-in features, then it
139 * must call these methods from its constructor. Otherwise, requests to use these features will
140 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000141 */
cdalton87332102016-02-26 12:22:02 -0800142 void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; }
cdalton28f45b92016-03-07 13:58:26 -0800143 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800144
145 void combineRequiredFeatures(const GrProcessor& other) {
146 fRequiredFeatures |= other.fRequiredFeatures;
147 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000148
joshualitteb2a6762014-12-04 11:35:33 -0800149 template <typename PROC_SUBCLASS> void initClassID() {
150 static uint32_t kClassID = GenClassID();
151 fClassID = kClassID;
152 }
153
154 uint32_t fClassID;
bsalomon0e08fc12014-10-15 08:19:04 -0700155private:
joshualitteb2a6762014-12-04 11:35:33 -0800156 static uint32_t GenClassID() {
157 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
158 // atomic inc returns the old value not the incremented value. So we add
159 // 1 to the returned value.
160 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
161 if (!id) {
162 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
163 "subclass.");
164 }
165 return id;
166 }
167
168 enum {
169 kIllegalProcessorClassID = 0,
170 };
171 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000172
cdalton87332102016-02-26 12:22:02 -0800173 RequiredFeatures fRequiredFeatures;
Brian Salomon55e03462016-10-28 11:25:52 -0400174 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
175 SkSTArray<2, const GrBufferAccess*, true> fBufferAccesses;
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