blob: 84a8f8b9636caf9b66f34172762703b5ea878d6f [file] [log] [blame]
commit-bot@chromium.org261dc562013-10-04 15:42:56 +00001/*
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
egdaniele659a582015-11-13 09:55:43 -08008#ifndef GrGLSLGeometryProcessor_DEFINED
9#define GrGLSLGeometryProcessor_DEFINED
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000010
egdaniele659a582015-11-13 09:55:43 -080011#include "GrGLSLPrimitiveProcessor.h"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000012
egdaniel8dcdedc2015-11-11 06:27:20 -080013class GrGLSLGPBuilder;
joshualitt8072caa2015-02-12 14:20:52 -080014
joshualittabb52a12015-01-13 15:02:10 -080015/**
16 * If a GL effect needs a GrGLFullShaderBuilder* object to emit vertex code, then it must inherit
17 * from this class. Since paths don't have vertices, this class is only meant to be used internally
18 * by skia, for special cases.
19 */
egdaniele659a582015-11-13 09:55:43 -080020class GrGLSLGeometryProcessor : public GrGLSLPrimitiveProcessor {
joshualittabb52a12015-01-13 15:02:10 -080021public:
22 /* Any general emit code goes in the base class emitCode. Subclasses override onEmitCode */
mtklein36352bf2015-03-25 18:17:31 -070023 void emitCode(EmitArgs&) override;
joshualittabb52a12015-01-13 15:02:10 -080024
joshualitte3ababe2015-05-15 07:56:07 -070025 // By default we use the identity matrix
joshualittd8dd47b2015-09-11 11:45:01 -070026 void setTransformData(const GrPrimitiveProcessor&,
egdaniel018fb622015-10-28 07:26:40 -070027 const GrGLSLProgramDataManager& pdman,
joshualittd8dd47b2015-09-11 11:45:01 -070028 int index,
29 const SkTArray<const GrCoordTransform*, true>& transforms) override {
joshualitte3ababe2015-05-15 07:56:07 -070030 this->setTransformDataMatrix(SkMatrix::I(), pdman, index, transforms);
31 }
32
33 // A helper which subclasses can use if needed
34 template <class GeometryProcessor>
35 void setTransformDataHelper(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -070036 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -070037 int index,
38 const SkTArray<const GrCoordTransform*, true>& transforms) {
39 const GeometryProcessor& gp = primProc.cast<GeometryProcessor>();
40 this->setTransformDataMatrix(gp.localMatrix(), pdman, index, transforms);
41 }
joshualittabb52a12015-01-13 15:02:10 -080042
43protected:
joshualittb2aa7cb2015-08-05 11:05:22 -070044 // Emit a uniform matrix for each coord transform.
egdaniel7ea439b2015-12-03 09:20:44 -080045 void emitTransforms(GrGLSLVertexBuilder* vb,
egdaniel0eafe792015-11-20 14:01:22 -080046 GrGLSLVaryingHandler* varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080047 GrGLSLUniformHandler* uniformHandler,
robertphillips46d36f02015-01-18 08:14:14 -080048 const GrShaderVar& posVar,
joshualitte3ababe2015-05-15 07:56:07 -070049 const char* localCoords,
joshualittabb52a12015-01-13 15:02:10 -080050 const TransformsIn& tin,
51 TransformsOut* tout) {
egdaniel7ea439b2015-12-03 09:20:44 -080052 this->emitTransforms(vb, varyingHandler, uniformHandler,
53 posVar, localCoords, SkMatrix::I(), tin, tout);
joshualittabb52a12015-01-13 15:02:10 -080054 }
55
joshualittb2aa7cb2015-08-05 11:05:22 -070056 // Emit pre-transformed coords as a vertex attribute per coord-transform.
egdaniel7ea439b2015-12-03 09:20:44 -080057 void emitTransforms(GrGLSLVertexBuilder*,
egdaniel0eafe792015-11-20 14:01:22 -080058 GrGLSLVaryingHandler*,
egdaniel7ea439b2015-12-03 09:20:44 -080059 GrGLSLUniformHandler*,
robertphillips46d36f02015-01-18 08:14:14 -080060 const GrShaderVar& posVar,
joshualittabb52a12015-01-13 15:02:10 -080061 const char* localCoords,
62 const SkMatrix& localMatrix,
63 const TransformsIn&,
64 TransformsOut*);
65
joshualittb2aa7cb2015-08-05 11:05:22 -070066 // caller has emitted transforms via attributes
egdaniel7ea439b2015-12-03 09:20:44 -080067 void emitTransforms(GrGLSLVertexBuilder*,
egdaniel0eafe792015-11-20 14:01:22 -080068 GrGLSLVaryingHandler*,
joshualittb2aa7cb2015-08-05 11:05:22 -070069 const char* localCoords,
70 const TransformsIn& tin,
71 TransformsOut* tout);
72
robertphillips46d36f02015-01-18 08:14:14 -080073 struct GrGPArgs {
74 // The variable used by a GP to store its position. It can be
75 // either a vec2 or a vec3 depending on the presence of perspective.
76 GrShaderVar fPositionVar;
77 };
78
79 // Create the correct type of position variable given the CTM
egdaniel7ea439b2015-12-03 09:20:44 -080080 void setupPosition(GrGLSLVertexBuilder*, GrGPArgs*, const char* posName);
81 void setupPosition(GrGLSLVertexBuilder*,
82 GrGLSLUniformHandler* uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080083 GrGPArgs*,
84 const char* posName,
85 const SkMatrix& mat,
joshualitt5559ca22015-05-21 15:50:36 -070086 UniformHandle* viewMatrixUniform);
robertphillips46d36f02015-01-18 08:14:14 -080087
88 static uint32_t ComputePosKey(const SkMatrix& mat) {
89 if (mat.isIdentity()) {
90 return 0x0;
91 } else if (!mat.hasPerspective()) {
92 return 0x01;
93 } else {
94 return 0x02;
95 }
96 }
97
joshualittabb52a12015-01-13 15:02:10 -080098private:
joshualitte3ababe2015-05-15 07:56:07 -070099 void setTransformDataMatrix(const SkMatrix& localMatrix,
egdaniel018fb622015-10-28 07:26:40 -0700100 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700101 int index,
102 const SkTArray<const GrCoordTransform*, true>& transforms) {
103 SkSTArray<2, Transform, true>& procTransforms = fInstalledTransforms[index];
104 int numTransforms = transforms.count();
105 for (int t = 0; t < numTransforms; ++t) {
106 SkASSERT(procTransforms[t].fHandle.isValid());
107 const SkMatrix& transform = GetTransformMatrix(localMatrix, *transforms[t]);
108 if (!procTransforms[t].fCurrentValue.cheapEqualTo(transform)) {
joshualitte7afc2d2015-09-11 10:44:13 -0700109 pdman.setSkMatrix(procTransforms[t].fHandle.toIndex(), transform);
joshualitte3ababe2015-05-15 07:56:07 -0700110 procTransforms[t].fCurrentValue = transform;
111 }
112 }
113 }
114
robertphillips46d36f02015-01-18 08:14:14 -0800115 virtual void onEmitCode(EmitArgs&, GrGPArgs*) = 0;
joshualittabb52a12015-01-13 15:02:10 -0800116
egdaniele659a582015-11-13 09:55:43 -0800117 typedef GrGLSLPrimitiveProcessor INHERITED;
joshualittabb52a12015-01-13 15:02:10 -0800118};
119
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000120#endif