blob: 287d7e11845b97edb2cbb96c2ac9a079b2130bca [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
2 * Copyright 2013 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#ifndef GrGeometryProcessor_DEFINED
9#define GrGeometryProcessor_DEFINED
10
11#include "GrProcessor.h"
bsalomon6251d172014-10-15 10:50:36 -070012#include "GrShaderVar.h"
joshualittb0a8a372014-09-23 09:50:21 -070013
14/**
15 * A GrGeomteryProcessor is used to perform computation in the vertex shader and
16 * add support for custom vertex attributes. A GrGemeotryProcessor is typically
17 * tied to the code that does a specific type of high-level primitive rendering
18 * (e.g. anti-aliased circle rendering). The GrGeometryProcessor used for a draw is
19 * specified using GrDrawState. There can only be one geometry processor active for
20 * a draw. The custom vertex attributes required by the geometry processor must be
21 * added to the vertex attribute array specified on the GrDrawState.
22 * GrGeometryProcessor subclasses should be immutable after construction.
23 */
24class GrGeometryProcessor : public GrProcessor {
25public:
joshualitt74077b92014-10-24 11:26:03 -070026 GrGeometryProcessor()
27 : fWillUseGeoShader(false) {}
joshualittb0a8a372014-09-23 09:50:21 -070028
29 virtual const GrBackendGeometryProcessorFactory& getFactory() const = 0;
30
31 /*
32 * This only has a max because GLProgramsTest needs to generate test arrays, and these have to
33 * be static
34 * TODO make this truly dynamic
35 */
36 static const int kMaxVertexAttribs = 2;
37 typedef SkTArray<GrShaderVar, true> VertexAttribArray;
38
39 const VertexAttribArray& getVertexAttribs() const { return fVertexAttribs; }
40
joshualitt74077b92014-10-24 11:26:03 -070041 bool willUseGeoShader() const { return fWillUseGeoShader; }
42
bsalomon98b33eb2014-10-15 11:05:26 -070043 /** Returns true if this and other processor conservatively draw identically. It can only return
44 true when the two prcoessors are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -070045 from getFactory()).
bsalomon98b33eb2014-10-15 11:05:26 -070046 A return value of true from isEqual() should not be used to test whether the prcoessors
47 would generate the same shader code. To test for identical code generation use the
48 processors' keys computed by the GrBackendEffectFactory. */
bsalomon0e08fc12014-10-15 08:19:04 -070049 bool isEqual(const GrGeometryProcessor& that) const {
bsalomon420d7e92014-10-16 09:18:09 -070050 if (&this->getFactory() != &that.getFactory() || !this->hasSameTextureAccesses(that)) {
bsalomon0e08fc12014-10-15 08:19:04 -070051 return false;
52 }
bsalomon420d7e92014-10-16 09:18:09 -070053 return this->onIsEqual(that);
bsalomon0e08fc12014-10-15 08:19:04 -070054 }
55
joshualittb0a8a372014-09-23 09:50:21 -070056protected:
57 /**
58 * Subclasses call this from their constructor to register vertex attributes (at most
59 * kMaxVertexAttribs). This must only be called from the constructor because GrProcessors are
60 * immutable.
61 */
62 const GrShaderVar& addVertexAttrib(const GrShaderVar& var) {
63 SkASSERT(fVertexAttribs.count() < kMaxVertexAttribs);
64 return fVertexAttribs.push_back(var);
65 }
66
joshualitt74077b92014-10-24 11:26:03 -070067 void setWillUseGeoShader() { fWillUseGeoShader = true; }
68
joshualittb0a8a372014-09-23 09:50:21 -070069private:
bsalomon0e08fc12014-10-15 08:19:04 -070070 virtual bool onIsEqual(const GrGeometryProcessor&) const = 0;
71
joshualittb0a8a372014-09-23 09:50:21 -070072 SkSTArray<kMaxVertexAttribs, GrShaderVar, true> fVertexAttribs;
joshualitt74077b92014-10-24 11:26:03 -070073 bool fWillUseGeoShader;
joshualittb0a8a372014-09-23 09:50:21 -070074
75 typedef GrProcessor INHERITED;
76};
77
joshualittb0a8a372014-09-23 09:50:21 -070078#endif