OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2014 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 | |
8 #include "GrDefaultGeoProcFactory.h" | |
9 | |
10 #include "gl/builders/GrGLProgramBuilder.h" | |
11 #include "gl/GrGLGeometryProcessor.h" | |
12 #include "GrDrawState.h" | |
13 #include "GrTBackendProcessorFactory.h" | |
14 | |
15 /* | |
16 * The default Geometry Processor simply takes position and multiplies it by the
uniform view | |
17 * matrix. It also leaves coverage untouched. Behind the scenes, we may add per
vertex color or | |
18 * local coords. | |
19 */ | |
20 class DefaultGeoProc : public GrGeometryProcessor { | |
21 public: | |
22 static GrGeometryProcessor* Create() { | |
23 GR_CREATE_STATIC_PROCESSOR(gDefaultGeoProc, DefaultGeoProc, ()); | |
24 return SkRef(gDefaultGeoProc); | |
25 } | |
26 | |
27 static const char* Name() { return "DefaultGeometryProcessor"; } | |
28 | |
29 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERR
IDE { | |
30 return GrTBackendGeometryProcessorFactory<DefaultGeoProc>::getInstance()
; | |
31 } | |
32 | |
33 class GLProcessor : public GrGLGeometryProcessor { | |
34 public: | |
35 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&
) | |
36 : INHERITED (factory) {} | |
37 | |
38 virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { | |
39 GrGLVertexBuilder* vs = args.fPB->getVertexShaderBuilder(); | |
40 | |
41 // setup position varying | |
42 vs->codeAppendf("%s = %s * vec3(%s, 1);", vs->glPosition(), vs->uVie
wM(), | |
43 vs->inPosition()); | |
44 | |
45 // output coverage in FS(pass through) | |
46 GrGLGPFragmentBuilder* fs = args.fPB->getFragmentShaderBuilder(); | |
47 fs->codeAppendf("%s = %s;", args.fOutput, GrGLSLExpr4(args.fInput).c
_str()); | |
48 } | |
49 | |
50 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcess
orKeyBuilder*) {} | |
51 | |
52 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&)
SK_OVERRIDE {} | |
53 | |
54 private: | |
55 typedef GrGLGeometryProcessor INHERITED; | |
56 }; | |
57 | |
58 private: | |
59 DefaultGeoProc() {} | |
60 | |
61 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE { | |
62 return true; | |
63 } | |
64 | |
65 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERR
IDE { | |
66 inout->mulByUnknownAlpha(); | |
67 } | |
68 | |
69 GR_DECLARE_GEOMETRY_PROCESSOR_TEST; | |
70 | |
71 typedef GrFragmentProcessor INHERITED; | |
72 }; | |
73 | |
74 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); | |
75 | |
76 GrGeometryProcessor* DefaultGeoProc::TestCreate(SkRandom* random, | |
77 GrContext*, | |
78 const GrDrawTargetCaps& caps, | |
79 GrTexture*[]) { | |
80 return DefaultGeoProc::Create(); | |
81 } | |
82 | |
83 // We use these arrays to customize our default GP. We only need 4 because we o
mit coverage if | |
84 // coverage is not requested in the flags to the create function. | |
85 GrVertexAttrib kDefaultPositionGeoProc[] = { | |
86 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBindi
ng }, | |
87 { kVec4ub_GrVertexAttribType, sizeof(SkPoint), kCoverage_GrVertexAttribBindi
ng }, | |
88 }; | |
89 | |
90 GrVertexAttrib kDefaultPosColorGeoProc[] = { | |
91 { kVec2f_GrVertexAttribType, 0, kPosition_G
rVertexAttribBinding }, | |
92 { kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVe
rtexAttribBinding }, | |
93 { kVec4ub_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kCoverage_G
rVertexAttribBinding }, | |
94 }; | |
95 | |
96 GrVertexAttrib kDefaultPosUVGeoProc[] = { | |
97 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribB
inding }, | |
98 { kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttri
bBinding }, | |
99 { kVec4ub_GrVertexAttribType, 2 * sizeof(SkPoint), kCoverage_GrVertexAttribB
inding }, | |
100 }; | |
101 | |
102 GrVertexAttrib kDefaultPosColUVGeoProc[] = { | |
103 { kVec2f_GrVertexAttribType, 0, kPositi
on_GrVertexAttribBinding }, | |
104 { kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_
GrVertexAttribBinding }, | |
105 { kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kLocalC
oord_GrVertexAttribBinding }, | |
106 { kVec4ub_GrVertexAttribType, 2 * sizeof(SkPoint) + sizeof(GrColor), kCovera
ge_GrVertexAttribBinding }, | |
107 }; | |
108 | |
109 static size_t get_size(GrDefaultGeoProcFactory::GPType flag) { | |
110 switch (flag) { | |
111 case GrDefaultGeoProcFactory::kPosition_GPType: | |
112 return GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); | |
113 case GrDefaultGeoProcFactory::kColor_GPType: | |
114 return GrVertexAttribTypeSize(kVec4ub_GrVertexAttribType); | |
115 case GrDefaultGeoProcFactory::kLocalCoord_GPType: | |
116 return GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); | |
117 case GrDefaultGeoProcFactory::kCoverage_GPType: | |
118 return GrVertexAttribTypeSize(kVec4ub_GrVertexAttribType); | |
119 default: | |
120 SkFAIL("Should never get here"); | |
121 return 0; | |
122 } | |
123 } | |
124 | |
125 const GrGeometryProcessor* | |
126 GrDefaultGeoProcFactory::CreateAndSetAttribs(GrDrawState* ds, uint32_t gpTypeFla
gs) { | |
127 SkASSERT(ds); | |
128 // always atleast position in the GP | |
129 size_t size = get_size(kPosition_GPType); | |
130 int count = 1; | |
131 | |
132 bool hasColor = SkToBool(gpTypeFlags & kColor_GPType); | |
133 bool hasLocalCoord = SkToBool(gpTypeFlags & kLocalCoord_GPType); | |
134 bool hasCoverage = SkToBool(gpTypeFlags & kCoverage_GPType); | |
135 | |
136 if (hasColor) { | |
137 size += get_size(kColor_GPType); | |
138 count++; | |
139 if (hasLocalCoord) { | |
140 size += get_size(kLocalCoord_GPType); | |
141 count++; | |
142 if (hasCoverage) { | |
143 size += get_size(kCoverage_GPType); | |
144 count++; | |
145 ds->setVertexAttribs<kDefaultPosColUVGeoProc>(count, size); | |
146 } else { | |
147 ds->setVertexAttribs<kDefaultPosColUVGeoProc>(count, size); | |
148 | |
149 } | |
150 } else { | |
151 if (hasCoverage) { | |
152 size += get_size(kCoverage_GPType); | |
153 count++; | |
154 ds->setVertexAttribs<kDefaultPosColorGeoProc>(count, size); | |
155 } else { | |
156 ds->setVertexAttribs<kDefaultPosColorGeoProc>(count, size); | |
157 } | |
158 } | |
159 } else if (hasLocalCoord) { | |
160 size += get_size(kLocalCoord_GPType); | |
161 count++; | |
162 if (hasCoverage) { | |
163 size += get_size(kCoverage_GPType); | |
164 count++; | |
165 ds->setVertexAttribs<kDefaultPosUVGeoProc>(count, size); | |
166 } else { | |
167 ds->setVertexAttribs<kDefaultPosUVGeoProc>(count, size); | |
168 } | |
169 } else if (hasCoverage) { | |
170 size += get_size(kCoverage_GPType); | |
171 count++; | |
172 ds->setVertexAttribs<kDefaultPositionGeoProc>(count, size); | |
173 } else { | |
174 // Just position | |
175 ds->setVertexAttribs<kDefaultPositionGeoProc>(count, size); | |
176 } | |
177 return DefaultGeoProc::Create(); | |
178 } | |
179 | |
180 const GrGeometryProcessor* GrDefaultGeoProcFactory::Create() { | |
181 return DefaultGeoProc::Create(); | |
182 } | |
OLD | NEW |