1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrDefaultGeoProcFactory.h"
#include "GrDrawState.h"
#include "GrInvariantOutput.h"
#include "GrTBackendProcessorFactory.h"
#include "gl/GrGLGeometryProcessor.h"
#include "gl/builders/GrGLProgramBuilder.h"
/*
* The default Geometry Processor simply takes position and multiplies it by the uniform view
* matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or
* local coords.
*/
typedef GrDefaultGeoProcFactory Flag;
class DefaultGeoProc : public GrGeometryProcessor {
public:
static GrGeometryProcessor* Create(uint32_t gpTypeFlags) {
switch (gpTypeFlags) {
case Flag::kColor_GPType | Flag::kCoverage_GPType | Flag::kLocalCoord_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcColLocCov, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcColLocCov);
}
case Flag::kColor_GPType | Flag::kLocalCoord_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcColLoc, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcColLoc);
}
case Flag::kColor_GPType | Flag::kCoverage_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcColCov, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcColCov);
}
case Flag::kColor_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcCol, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcCol);
}
case Flag::kLocalCoord_GPType | Flag::kCoverage_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcLocCov, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcLocCov);
}
case Flag::kLocalCoord_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcLoc, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcLoc);
}
case Flag::kCoverage_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProcCov, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProcCov);
}
case Flag::kPosition_GPType: {
GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProc, DefaultGeoProc, (gpTypeFlags));
return SkRef(gDefaultGeoProc);
}
default:
SkFAIL("Incomplete Switch");
return NULL;
}
}
static const char* Name() { return "DefaultGeometryProcessor"; }
virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE {
return GrTBackendGeometryProcessorFactory<DefaultGeoProc>::getInstance();
}
const GrAttribute* inPosition() const { return fInPosition; }
const GrAttribute* inColor() const { return fInColor; }
const GrAttribute* inLocalCoords() const { return fInLocalCoords; }
const GrAttribute* inCoverage() const { return fInCoverage; }
class GLProcessor : public GrGLGeometryProcessor {
public:
GLProcessor(const GrBackendProcessorFactory& factory,
const GrGeometryProcessor&,
const GrBatchTracker&)
: INHERITED (factory) {}
virtual void emitCode(const EmitArgs& args) SK_OVERRIDE {
const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>();
GrGLVertexBuilder* vs = args.fPB->getVertexShaderBuilder();
vs->codeAppendf("%s = %s;", vs->positionCoords(), gp.inPosition()->fName);
// Setup pass through color
if (gp.inColor()) {
args.fPB->addPassThroughAttribute(gp.inColor(), args.fOutputColor);
}
// Setup local coords if needed
if (gp.inLocalCoords()) {
vs->codeAppendf("%s = %s;", vs->localCoords(), gp.inLocalCoords()->fName);
} else {
vs->codeAppendf("%s = %s;", vs->localCoords(), gp.inPosition()->fName);
}
// setup position varying
vs->codeAppendf("%s = %s * vec3(%s, 1);", vs->glPosition(), vs->uViewM(),
gp.inPosition()->fName);
// Setup coverage as pass through
GrGLGPFragmentBuilder* fs = args.fPB->getFragmentShaderBuilder();
fs->codeAppendf("float alpha = 1.0;");
if (gp.inCoverage()) {
args.fPB->addPassThroughAttribute(gp.inCoverage(), "alpha");
}
fs->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
}
static inline void GenKey(const GrGeometryProcessor& gp,
const GrBatchTracker&,
const GrGLCaps&,
GrProcessorKeyBuilder* b) {
const DefaultGeoProc& def = gp.cast<DefaultGeoProc>();
b->add32(def.fFlags);
}
virtual void setData(const GrGLProgramDataManager&,
const GrGeometryProcessor&,
const GrBatchTracker&) SK_OVERRIDE {}
private:
typedef GrGLGeometryProcessor INHERITED;
};
private:
DefaultGeoProc(uint32_t gpTypeFlags)
: fInPosition(NULL)
, fInColor(NULL)
, fInLocalCoords(NULL)
, fInCoverage(NULL)
, fFlags(gpTypeFlags) {
bool hasColor = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kColor_GPType);
bool hasLocalCoord = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kLocalCoord_GPType);
bool hasCoverage = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kCoverage_GPType);
fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
if (hasColor) {
fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType));
this->setHasVertexColor();
}
if (hasLocalCoord) {
fInLocalCoords = &this->addVertexAttrib(GrAttribute("inLocalCoord",
kVec2f_GrVertexAttribType));
this->setHasLocalCoords();
}
if (hasCoverage) {
fInCoverage = &this->addVertexAttrib(GrAttribute("inCoverage",
kFloat_GrVertexAttribType));
this->setHasVertexCoverage();
}
}
virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE {
const DefaultGeoProc& gp = other.cast<DefaultGeoProc>();
return gp.fFlags == this->fFlags;
}
virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
if (fInCoverage) {
inout->mulByUnknownAlpha();
} else {
inout->mulByKnownAlpha(255);
}
}
const GrAttribute* fInPosition;
const GrAttribute* fInColor;
const GrAttribute* fInLocalCoords;
const GrAttribute* fInCoverage;
uint32_t fFlags;
GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
typedef GrFragmentProcessor INHERITED;
};
GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc);
GrGeometryProcessor* DefaultGeoProc::TestCreate(SkRandom* random,
GrContext*,
const GrDrawTargetCaps& caps,
GrTexture*[]) {
uint32_t flags = 0;
if (random->nextBool()) {
flags |= GrDefaultGeoProcFactory::kColor_GPType;
}
if (random->nextBool()) {
flags |= GrDefaultGeoProcFactory::kCoverage_GPType;
}
if (random->nextBool()) {
flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
}
return DefaultGeoProc::Create(flags);
}
const GrGeometryProcessor* GrDefaultGeoProcFactory::Create(uint32_t gpTypeFlags) {
return DefaultGeoProc::Create(gpTypeFlags);
}
|