blob: 1dbf1539a22bbb86d31bd45ad964d47ab87b15fa [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
joshualitt8072caa2015-02-12 14:20:52 -080011#include "GrPrimitiveProcessor.h"
joshualitt9b989322014-12-15 14:16:27 -080012
13/**
14 * A GrGeometryProcessor is a flexible method for rendering a primitive. The GrGeometryProcessor
15 * has complete control over vertex attributes and uniforms(aside from the render target) but it
16 * must obey the same contract as any GrPrimitiveProcessor, specifically it must emit a color and
17 * coverage into the fragment shader. Where this color and coverage come from is completely the
18 * responsibility of the GrGeometryProcessor.
19 */
20class GrGeometryProcessor : public GrPrimitiveProcessor {
21public:
joshualitte3ababe2015-05-15 07:56:07 -070022 GrGeometryProcessor()
ethannicholas22793252016-01-30 09:59:10 -080023 : fWillUseGeoShader(false)
ethannicholas28ef4452016-03-25 09:26:03 -070024 , fLocalCoordsType(kUnused_LocalCoordsType)
25 , fSampleShading(0.0) {}
joshualitt9b989322014-12-15 14:16:27 -080026
mtkleinf0599002015-07-13 06:18:39 -070027 bool willUseGeoShader() const override { return fWillUseGeoShader; }
joshualitt74077b92014-10-24 11:26:03 -070028
joshualittb2aa7cb2015-08-05 11:05:22 -070029 bool hasTransformedLocalCoords() const override {
30 return kHasTransformed_LocalCoordsType == fLocalCoordsType;
31 }
32
33 bool hasExplicitLocalCoords() const override {
34 return kHasExplicit_LocalCoordsType == fLocalCoordsType;
35 }
36
ethannicholas28ef4452016-03-25 09:26:03 -070037 /**
38 * Returns the minimum fraction of samples for which the fragment shader will be run. For
39 * instance, if sampleShading is 0.5 in MSAA16 mode, the fragment shader will run a minimum of
40 * 8 times per pixel. The default value is zero.
41 */
42 float getSampleShading() const override {
43 return fSampleShading;
44 }
45
joshualittb0a8a372014-09-23 09:50:21 -070046protected:
47 /**
joshualitt2dd1ae02014-12-03 06:24:10 -080048 * Subclasses call this from their constructor to register vertex attributes. Attributes
49 * will be padded to the nearest 4 bytes for performance reasons.
50 * TODO After deferred geometry, we should do all of this inline in GenerateGeometry alongside
joshualitt9b989322014-12-15 14:16:27 -080051 * the struct used to actually populate the attributes. This is all extremely fragile, vertex
52 * attributes have to be added in the order they will appear in the struct which maps memory.
53 * The processor key should reflect the vertex attributes, or there lack thereof in the
54 * GrGeometryProcessor.
joshualittb0a8a372014-09-23 09:50:21 -070055 */
joshualitt71c92602015-01-14 08:12:47 -080056 const Attribute& addVertexAttrib(const Attribute& attribute) {
joshualitt2dd1ae02014-12-03 06:24:10 -080057 fVertexStride += attribute.fOffset;
bsalomon7dbd45d2016-03-23 10:40:53 -070058 fAttribs.push_back(attribute);
59 return fAttribs.back();
joshualittb0a8a372014-09-23 09:50:21 -070060 }
61
joshualitt74077b92014-10-24 11:26:03 -070062 void setWillUseGeoShader() { fWillUseGeoShader = true; }
63
joshualittb2aa7cb2015-08-05 11:05:22 -070064 /**
65 * If a GrFragmentProcessor in the GrPipeline needs localCoods, we will provide them in one of
66 * three ways
67 * 1) LocalCoordTransform * Position - in Shader
68 * 2) LocalCoordTransform * ExplicitLocalCoords- in Shader
69 * 3) A transformation on the CPU uploaded via vertex attribute
70 * TODO make this GrBatches responsibility
71 */
72 enum LocalCoordsType {
73 kUnused_LocalCoordsType,
74 kHasExplicit_LocalCoordsType,
75 kHasTransformed_LocalCoordsType
76 };
77
78 void setHasExplicitLocalCoords() {
79 SkASSERT(kUnused_LocalCoordsType == fLocalCoordsType);
80 fLocalCoordsType = kHasExplicit_LocalCoordsType;
81 }
82 void setHasTransformedLocalCoords() {
83 SkASSERT(kUnused_LocalCoordsType == fLocalCoordsType);
84 fLocalCoordsType = kHasTransformed_LocalCoordsType;
85 }
joshualitt2dd1ae02014-12-03 06:24:10 -080086
ethannicholas28ef4452016-03-25 09:26:03 -070087 void setSampleShading(float sampleShading) {
88 fSampleShading = sampleShading;
89 }
90
joshualittb0a8a372014-09-23 09:50:21 -070091private:
joshualitt74077b92014-10-24 11:26:03 -070092 bool fWillUseGeoShader;
joshualittb2aa7cb2015-08-05 11:05:22 -070093 LocalCoordsType fLocalCoordsType;
ethannicholas28ef4452016-03-25 09:26:03 -070094 float fSampleShading;
joshualittb0a8a372014-09-23 09:50:21 -070095
joshualitt290c09b2014-12-19 13:45:20 -080096 typedef GrPrimitiveProcessor INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070097};
joshualitt56995b52014-12-11 15:44:02 -080098
joshualittb0a8a372014-09-23 09:50:21 -070099#endif