2012-04-18 17:49:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
#ifndef GrProcessor_DEFINED
|
|
|
|
#define GrProcessor_DEFINED
|
2012-04-18 17:49:20 +00:00
|
|
|
|
2013-01-11 21:08:55 +00:00
|
|
|
#include "GrColor.h"
|
2014-09-23 16:50:21 +00:00
|
|
|
#include "GrProcessorUnitTest.h"
|
2014-09-04 20:12:37 +00:00
|
|
|
#include "GrProgramElement.h"
|
2016-04-11 21:47:28 +00:00
|
|
|
#include "GrBufferAccess.h"
|
2014-10-15 20:49:02 +00:00
|
|
|
#include "SkMath.h"
|
2015-11-16 17:06:59 +00:00
|
|
|
#include "SkString.h"
|
2016-05-02 14:19:41 +00:00
|
|
|
#include "../private/SkAtomics.h"
|
2012-04-20 18:35:38 +00:00
|
|
|
|
2012-04-18 17:49:20 +00:00
|
|
|
class GrContext;
|
2013-10-02 13:04:56 +00:00
|
|
|
class GrCoordTransform;
|
2014-11-12 16:35:25 +00:00
|
|
|
class GrInvariantOutput;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
/**
|
|
|
|
* Used by processors to build their keys. It incorporates each per-processor key into a larger
|
|
|
|
* shader key.
|
|
|
|
*/
|
|
|
|
class GrProcessorKeyBuilder {
|
|
|
|
public:
|
|
|
|
GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
|
|
|
|
SkASSERT(0 == fData->count() % sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void add32(uint32_t v) {
|
|
|
|
++fCount;
|
|
|
|
fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
|
|
|
|
add*() call. */
|
|
|
|
uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
|
|
|
|
SkASSERT(count > 0);
|
|
|
|
fCount += count;
|
|
|
|
return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return sizeof(uint32_t) * fCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
|
|
|
|
int fCount; // number of uint32_ts added to fData by the processor.
|
|
|
|
};
|
|
|
|
|
2014-10-15 18:05:26 +00:00
|
|
|
/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
|
|
|
|
immutable: after being constructed, their fields may not change.
|
2013-01-16 15:16:18 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
|
2015-08-27 19:57:01 +00:00
|
|
|
processor must reach 0 before the thread terminates and the pool is destroyed.
|
2014-10-15 18:05:26 +00:00
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrProcessor : public GrProgramElement {
|
2012-04-18 17:49:20 +00:00
|
|
|
public:
|
2016-11-17 16:38:22 +00:00
|
|
|
class TextureSampler;
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
virtual ~GrProcessor();
|
2012-04-18 17:49:20 +00:00
|
|
|
|
2014-10-15 18:05:26 +00:00
|
|
|
/** Human-meaningful string to identify this prcoessor; may be embedded
|
2012-08-02 15:15:16 +00:00
|
|
|
in generated shader code. */
|
2014-12-04 19:35:33 +00:00
|
|
|
virtual const char* name() const = 0;
|
2012-05-21 20:57:59 +00:00
|
|
|
|
2015-11-16 17:06:59 +00:00
|
|
|
// Human-readable dump of all information
|
|
|
|
virtual SkString dumpInfo() const {
|
|
|
|
SkString str;
|
|
|
|
str.appendf("Missing data");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-11-17 16:38:22 +00:00
|
|
|
int numTextureSamplers() const { return fTextureSamplers.count(); }
|
2012-04-18 17:49:20 +00:00
|
|
|
|
2012-09-11 15:45:20 +00:00
|
|
|
/** Returns the access pattern for the texture at index. index must be valid according to
|
2016-11-17 16:38:22 +00:00
|
|
|
numTextureSamplers(). */
|
|
|
|
const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; }
|
2012-08-02 15:15:16 +00:00
|
|
|
|
2016-04-11 21:47:28 +00:00
|
|
|
int numBuffers() const { return fBufferAccesses.count(); }
|
|
|
|
|
|
|
|
/** Returns the access pattern for the buffer at index. index must be valid according to
|
|
|
|
numBuffers(). */
|
|
|
|
const GrBufferAccess& bufferAccess(int index) const {
|
|
|
|
return *fBufferAccesses[index];
|
|
|
|
}
|
|
|
|
|
2016-02-26 20:22:02 +00:00
|
|
|
/**
|
|
|
|
* Platform specific built-in features that a processor can request for the fragment shader.
|
|
|
|
*/
|
|
|
|
enum RequiredFeatures {
|
|
|
|
kNone_RequiredFeatures = 0,
|
2016-03-07 21:58:26 +00:00
|
|
|
kFragmentPosition_RequiredFeature = 1 << 0,
|
|
|
|
kSampleLocations_RequiredFeature = 1 << 1
|
2016-02-26 20:22:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
|
|
|
|
|
|
|
|
RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
|
2013-03-12 12:26:08 +00:00
|
|
|
|
2012-07-24 21:36:16 +00:00
|
|
|
void* operator new(size_t size);
|
|
|
|
void operator delete(void* target);
|
|
|
|
|
2013-04-23 15:37:27 +00:00
|
|
|
void* operator new(size_t size, void* placement) {
|
|
|
|
return ::operator new(size, placement);
|
|
|
|
}
|
|
|
|
void operator delete(void* target, void* placement) {
|
|
|
|
::operator delete(target, placement);
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:21:41 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Helper for down-casting to a GrProcessor subclass
|
2014-09-16 15:21:41 +00:00
|
|
|
*/
|
|
|
|
template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
|
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
|
|
|
|
|
2013-01-11 13:54:30 +00:00
|
|
|
protected:
|
2016-02-26 20:22:02 +00:00
|
|
|
GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
|
2014-10-16 16:18:09 +00:00
|
|
|
|
2013-01-11 13:54:30 +00:00
|
|
|
/**
|
2016-04-11 21:47:28 +00:00
|
|
|
* Subclasses call these from their constructor to register sampler sources. The processor
|
|
|
|
* subclass manages the lifetime of the objects (these functions only store pointers). The
|
2016-11-17 16:38:22 +00:00
|
|
|
* TextureSampler and/or GrBufferAccess instances are typically member fields of the
|
2016-04-11 21:47:28 +00:00
|
|
|
* GrProcessor subclass. These must only be called from the constructor because GrProcessors
|
|
|
|
* are immutable.
|
2013-01-11 13:54:30 +00:00
|
|
|
*/
|
2016-11-17 16:38:22 +00:00
|
|
|
void addTextureSampler(const TextureSampler*);
|
2016-10-28 15:25:52 +00:00
|
|
|
void addBufferAccess(const GrBufferAccess* bufferAccess);
|
2013-01-11 13:54:30 +00:00
|
|
|
|
2016-04-11 21:47:28 +00:00
|
|
|
bool hasSameSamplers(const GrProcessor&) const;
|
2013-05-09 14:55:46 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-07 21:58:26 +00:00
|
|
|
* If the prcoessor will generate code that uses platform specific built-in features, then it
|
|
|
|
* must call these methods from its constructor. Otherwise, requests to use these features will
|
|
|
|
* be denied.
|
2013-05-09 14:55:46 +00:00
|
|
|
*/
|
2016-02-26 20:22:02 +00:00
|
|
|
void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; }
|
2016-03-07 21:58:26 +00:00
|
|
|
void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
|
2016-02-26 20:22:02 +00:00
|
|
|
|
|
|
|
void combineRequiredFeatures(const GrProcessor& other) {
|
|
|
|
fRequiredFeatures |= other.fRequiredFeatures;
|
|
|
|
}
|
2013-03-29 19:22:36 +00:00
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
template <typename PROC_SUBCLASS> void initClassID() {
|
|
|
|
static uint32_t kClassID = GenClassID();
|
|
|
|
fClassID = kClassID;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t fClassID;
|
2014-10-15 15:19:04 +00:00
|
|
|
private:
|
2014-12-04 19:35:33 +00:00
|
|
|
static uint32_t GenClassID() {
|
|
|
|
// fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
|
|
|
|
// atomic inc returns the old value not the incremented value. So we add
|
|
|
|
// 1 to the returned value.
|
|
|
|
uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
|
|
|
|
if (!id) {
|
|
|
|
SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
|
|
|
|
"subclass.");
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kIllegalProcessorClassID = 0,
|
|
|
|
};
|
|
|
|
static int32_t gCurrProcessorClassID;
|
2013-01-16 15:16:18 +00:00
|
|
|
|
2016-02-26 20:22:02 +00:00
|
|
|
RequiredFeatures fRequiredFeatures;
|
2016-11-17 16:38:22 +00:00
|
|
|
SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
|
|
|
|
SkSTArray<2, const GrBufferAccess*, true> fBufferAccesses;
|
2013-01-16 15:16:18 +00:00
|
|
|
|
2014-09-04 20:12:37 +00:00
|
|
|
typedef GrProgramElement INHERITED;
|
2012-04-18 17:49:20 +00:00
|
|
|
};
|
|
|
|
|
2016-02-26 20:22:02 +00:00
|
|
|
GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
|
|
|
|
|
2016-11-17 16:38:22 +00:00
|
|
|
/**
|
|
|
|
* Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with
|
|
|
|
* an associated GrTextureParams
|
|
|
|
*/
|
|
|
|
class GrProcessor::TextureSampler : public SkNoncopyable {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Must be initialized before adding to a GrProcessor's texture access list.
|
|
|
|
*/
|
|
|
|
TextureSampler();
|
|
|
|
|
|
|
|
TextureSampler(GrTexture*, const GrTextureParams&);
|
|
|
|
|
|
|
|
explicit TextureSampler(GrTexture*,
|
|
|
|
GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
|
|
|
|
SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
|
|
|
|
GrShaderFlags visibility = kFragment_GrShaderFlag);
|
|
|
|
|
|
|
|
void reset(GrTexture*, const GrTextureParams&,
|
|
|
|
GrShaderFlags visibility = kFragment_GrShaderFlag);
|
|
|
|
void reset(GrTexture*,
|
|
|
|
GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
|
|
|
|
SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
|
|
|
|
GrShaderFlags visibility = kFragment_GrShaderFlag);
|
|
|
|
|
|
|
|
bool operator==(const TextureSampler& that) const {
|
2016-11-17 17:48:40 +00:00
|
|
|
return this->texture() == that.texture() &&
|
2016-11-17 16:38:22 +00:00
|
|
|
fParams == that.fParams &&
|
|
|
|
fVisibility == that.fVisibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const TextureSampler& other) const { return !(*this == other); }
|
|
|
|
|
2016-11-17 17:48:40 +00:00
|
|
|
GrTexture* texture() const { return fTexture.get(); }
|
|
|
|
GrShaderFlags visibility() const { return fVisibility; }
|
|
|
|
const GrTextureParams& params() const { return fParams; }
|
2016-11-17 16:38:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For internal use by GrProcessor.
|
|
|
|
*/
|
2016-11-17 17:48:40 +00:00
|
|
|
const GrGpuResourceRef* programTexture() const { return &fTexture; }
|
2016-11-17 16:38:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
|
|
|
|
|
|
|
|
ProgramTexture fTexture;
|
|
|
|
GrTextureParams fParams;
|
|
|
|
GrShaderFlags fVisibility;
|
|
|
|
|
|
|
|
typedef SkNoncopyable INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-04-18 17:49:20 +00:00
|
|
|
#endif
|