blob: c1f4aa0256ca0af7a6080c349bf4f0a7d4c18da1 [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"
egdaniel9e4d6d12014-10-15 13:49:02 -070015#include "SkMath.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000016
tomhudson@google.com168e6342012-04-18 17:49:20 +000017class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000018class GrCoordTransform;
egdaniel605dd0f2014-11-12 08:35:25 -080019class GrInvariantOutput;
bsalomon95740982014-09-04 13:12:37 -070020
joshualitteb2a6762014-12-04 11:35:33 -080021/**
22 * Used by processors to build their keys. It incorporates each per-processor key into a larger
23 * shader key.
24 */
25class GrProcessorKeyBuilder {
26public:
27 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
28 SkASSERT(0 == fData->count() % sizeof(uint32_t));
29 }
30
31 void add32(uint32_t v) {
32 ++fCount;
33 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
34 }
35
36 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
37 add*() call. */
38 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
39 SkASSERT(count > 0);
40 fCount += count;
41 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
42 }
43
44 size_t size() const { return sizeof(uint32_t) * fCount; }
45
46private:
47 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
48 int fCount; // number of uint32_ts added to fData by the processor.
49};
50
bsalomon98b33eb2014-10-15 11:05:26 -070051/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
52 immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000053
joshualittb0a8a372014-09-23 09:50:21 -070054 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
bsalomon98b33eb2014-10-15 11:05:26 -070055 processor must reach 0 before the thread terminates and the pool is destroyed. To create a
56 static processor use the helper macro GR_CREATE_STATIC_PROCESSOR declared below.
57 */
joshualittb0a8a372014-09-23 09:50:21 -070058class GrProcessor : public GrProgramElement {
tomhudson@google.com168e6342012-04-18 17:49:20 +000059public:
joshualittb0a8a372014-09-23 09:50:21 -070060 virtual ~GrProcessor();
tomhudson@google.com168e6342012-04-18 17:49:20 +000061
bsalomon98b33eb2014-10-15 11:05:26 -070062 /** Human-meaningful string to identify this prcoessor; may be embedded
twiz@google.coma5e65ec2012-08-02 15:15:16 +000063 in generated shader code. */
joshualitteb2a6762014-12-04 11:35:33 -080064 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000065
bsalomon@google.com50db75c2013-01-11 13:54:30 +000066 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000067
bsalomon@google.com6d003d12012-09-11 15:45:20 +000068 /** Returns the access pattern for the texture at index. index must be valid according to
69 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +000070 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +000071
72 /** Shortcut for textureAccess(index).texture(); */
73 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000074
bsalomon98b33eb2014-10-15 11:05:26 -070075 /** Will this processor read the fragment position? */
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +000076 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000077
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000078 void* operator new(size_t size);
79 void operator delete(void* target);
80
bsalomon@google.comd42aca32013-04-23 15:37:27 +000081 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
joshualitt49586be2014-09-16 08:21:41 -070088 /**
joshualittb0a8a372014-09-23 09:50:21 -070089 * Helper for down-casting to a GrProcessor subclass
joshualitt49586be2014-09-16 08:21:41 -070090 */
91 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
92
joshualitteb2a6762014-12-04 11:35:33 -080093 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
94
bsalomon@google.com50db75c2013-01-11 13:54:30 +000095protected:
joshualitteb2a6762014-12-04 11:35:33 -080096 GrProcessor() : fClassID(kIllegalProcessorClassID), fWillReadFragmentPosition(false) {}
bsalomon420d7e92014-10-16 09:18:09 -070097
bsalomon@google.com50db75c2013-01-11 13:54:30 +000098 /**
bsalomon98b33eb2014-10-15 11:05:26 -070099 * Subclasses call this from their constructor to register GrTextureAccesses. The processor
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000100 * subclass manages the lifetime of the accesses (this function only stores a pointer). The
joshualittb0a8a372014-09-23 09:50:21 -0700101 * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be
102 * called from the constructor because GrProcessors are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000103 */
wangyix93ab2542015-08-19 08:23:12 -0700104 virtual void addTextureAccess(const GrTextureAccess* textureAccess);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000105
bsalomon420d7e92014-10-16 09:18:09 -0700106 bool hasSameTextureAccesses(const GrProcessor&) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000107
108 /**
bsalomon98b33eb2014-10-15 11:05:26 -0700109 * If the prcoessor will generate a backend-specific processor that will read the fragment
110 * position in the FS then it must call this method from its constructor. Otherwise, the
111 * request to access the fragment position will be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000112 */
113 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000114
joshualitteb2a6762014-12-04 11:35:33 -0800115 template <typename PROC_SUBCLASS> void initClassID() {
116 static uint32_t kClassID = GenClassID();
117 fClassID = kClassID;
118 }
119
120 uint32_t fClassID;
wangyix58d890b2015-08-12 09:40:47 -0700121 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
joshualitteb2a6762014-12-04 11:35:33 -0800122
bsalomon0e08fc12014-10-15 08:19:04 -0700123private:
joshualitteb2a6762014-12-04 11:35:33 -0800124 static uint32_t GenClassID() {
125 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
126 // atomic inc returns the old value not the incremented value. So we add
127 // 1 to the returned value.
128 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
129 if (!id) {
130 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
131 "subclass.");
132 }
133 return id;
134 }
135
136 enum {
137 kIllegalProcessorClassID = 0,
138 };
139 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000140
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000141 bool fWillReadFragmentPosition;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000142
bsalomon95740982014-09-04 13:12:37 -0700143 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000144};
145
bsalomon98b33eb2014-10-15 11:05:26 -0700146/**
147 * This creates a processor outside of the memory pool. The processor's destructor will be called
148 * at global destruction time. NAME will be the name of the created instance.
149 */
150#define GR_CREATE_STATIC_PROCESSOR(NAME, PROC_CLASS, ARGS) \
151static SkAlignedSStorage<sizeof(PROC_CLASS)> g_##NAME##_Storage; \
152static PROC_CLASS* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), PROC_CLASS, ARGS); \
153static SkAutoTDestroy<GrProcessor> NAME##_ad(NAME);
154
tomhudson@google.com168e6342012-04-18 17:49:20 +0000155#endif