blob: 27b6f802f56cdfc9e28a7babf1a7d23926e95b28 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
junov@google.comf93e7172011-03-31 21:26:24 +00009#ifndef GrGLProgram_DEFINED
10#define GrGLProgram_DEFINED
11
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrDrawState.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +000013#include "GrGLContext.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000014#include "GrGLProgramDesc.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000015#include "GrGLSL.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000016#include "GrGLTexture.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000017#include "GrGLUniformManager.h"
junov@google.comf93e7172011-03-31 21:26:24 +000018
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000019#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000020#include "SkXfermode.h"
21
junov@google.comf93e7172011-03-31 21:26:24 +000022class GrBinHashKeyBuilder;
bsalomon@google.comd698f772012-10-25 13:22:00 +000023class GrGLEffect;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000024class GrGLShaderBuilder;
junov@google.comf93e7172011-03-31 21:26:24 +000025
26/**
27 * This class manages a GPU program and records per-program information.
28 * We can specify the attribute locations so that they are constant
29 * across our shaders. But the driver determines the uniform locations
30 * at link time. We don't need to remember the sampler uniform location
31 * because we will bind a texture slot to it and never change it
32 * Uniforms are program-local so we can't rely on fHWState to hold the
33 * previous uniform state after a program change.
34 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000035class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000036public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000037 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000038
robertphillips@google.com6177e692013-02-28 20:16:25 +000039 static GrGLProgram* Create(const GrGLContext& gl,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000040 const GrGLProgramDesc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000041 const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000042
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000043 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000044
bsalomon@google.com34cccde2013-01-04 18:34:30 +000045 /**
46 * Call to abandon GL objects owned by this program.
47 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000048 void abandon();
49
junov@google.comf93e7172011-03-31 21:26:24 +000050 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +000051 * The shader may modify the blend coefficients. Params are in/out
junov@google.comf93e7172011-03-31 21:26:24 +000052 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000053 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000054
bsalomon@google.com31ec7982013-03-27 18:14:57 +000055 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000056
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000058 * Gets the GL program ID for this program.
59 */
60 GrGLuint programID() const { return fProgramID; }
61
62 /**
bsalomon@google.com91207482013-02-12 21:45:24 +000063 * Some GL state that is relevant to programs is not stored per-program. In particular vertex
64 * attributes are global state. This struct is read and updated by GrGLProgram::setData to
65 * allow us to avoid setting this state redundantly.
66 */
67 struct SharedGLState {
68 GrColor fConstAttribColor;
69 GrColor fConstAttribCoverage;
70
71 SharedGLState() { this->invalidate(); }
72 void invalidate() {
73 fConstAttribColor = GrColor_ILLEGAL;
74 fConstAttribCoverage = GrColor_ILLEGAL;
75 }
76 };
77
78 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000079 * The GrDrawState's view matrix along with the aspects of the render target determine the
80 * matrix sent to GL. The size of the render target affects the GL matrix because we must
81 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
82 * target may require us to perform a mirror-flip.
83 */
84 struct MatrixState {
85 SkMatrix fViewMatrix;
86 SkISize fRenderTargetSize;
87 GrSurfaceOrigin fRenderTargetOrigin;
88
89 MatrixState() { this->invalidate(); }
90 void invalidate() {
91 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +000092 fRenderTargetSize.fWidth = -1;
93 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000094 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
95 }
96 };
97
98 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +000099 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
100 * draw occurs using the program after the program has already been bound. It also uses the
101 * GrGpuGL object to bind the textures required by the GrGLEffects.
bsalomon@google.com91207482013-02-12 21:45:24 +0000102 *
103 * The color and coverage params override the GrDrawState's getColor() and getCoverage() values.
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000104 */
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000105 void setData(GrGpuGL*,
106 GrColor color,
107 GrColor coverage,
108 const GrDeviceCoordTexture* dstCopy, // can be NULL
109 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000110
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000111private:
robertphillips@google.com6177e692013-02-28 20:16:25 +0000112 GrGLProgram(const GrGLContext& gl,
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000113 const GrGLProgramDesc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000114 const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000115
116 bool succeeded() const { return 0 != fProgramID; }
117
118 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000119 * This is the heavy initialization routine for building a GLProgram.
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000120 */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000121 bool genProgram(const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000122
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000123 void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000124
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000125 void genGeometryShader(GrGLShaderBuilder* segments) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000126
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000127 typedef GrGLUniformManager::UniformHandle UniformHandle;
128
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000129 void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage);
130
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000131 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
bsalomon@google.com17504f52012-10-30 12:34:25 +0000132 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000133 bool bindColorOut,
134 bool bindDualSrcOut);
135
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000136 // Sets the texture units for samplers
137 void initSamplerUniforms();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000138
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000139 bool compileShaders(const GrGLShaderBuilder& builder);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000140
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000141 const char* adjustInColor(const SkString& inColor) const;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000142
bsalomon@google.com91207482013-02-12 21:45:24 +0000143 // Helper for setData(). Makes GL calls to specify the initial color when there is not
144 // per-vertex colors.
145 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
146
147 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
148 // per-vertex coverages.
149 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
150
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000151 // Helper for setData() that sets the view matrix and loads the render target height uniform
152 void setMatrixAndRenderTargetHeight(const GrDrawState&);
153
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000154 typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000155
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000156 struct UniformHandles {
157 UniformHandle fViewMatrixUni;
158 UniformHandle fColorUni;
159 UniformHandle fCoverageUni;
160 UniformHandle fColorFilterUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000161
bsalomon@google.com706f6682012-10-23 14:53:55 +0000162 // We use the render target height to provide a y-down frag coord when specifying
163 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000164 UniformHandle fRTHeightUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000165
166 // Uniforms for computing texture coords to do the dst-copy lookup
167 UniformHandle fDstCopyTopLeftUni;
168 UniformHandle fDstCopyScaleUni;
169 UniformHandle fDstCopySamplerUni;
170
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000171 // An array of sampler uniform handles for each effect.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000172 SamplerUniSArray fEffectSamplerUnis[GrDrawState::kNumStages];
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000173
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000174 UniformHandles() {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000175 fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle;
176 fColorUni = GrGLUniformManager::kInvalidUniformHandle;
177 fCoverageUni = GrGLUniformManager::kInvalidUniformHandle;
178 fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000179 fRTHeightUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000180 fDstCopyTopLeftUni = GrGLUniformManager::kInvalidUniformHandle;
181 fDstCopyScaleUni = GrGLUniformManager::kInvalidUniformHandle;
182 fDstCopySamplerUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com91961302011-05-09 18:39:58 +0000183 }
junov@google.comf93e7172011-03-31 21:26:24 +0000184 };
185
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000186 // GL IDs
187 GrGLuint fVShaderID;
188 GrGLuint fGShaderID;
189 GrGLuint fFShaderID;
190 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000191
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000192 // these reflect the current values of uniforms (GL uniform values travel with program)
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000193 MatrixState fMatrixState;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000194 GrColor fColor;
195 GrColor fCoverage;
196 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000197
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000198 GrGLEffect* fEffects[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000199
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000200 GrGLProgramDesc fDesc;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000201 const GrGLContext& fContext;
junov@google.comf93e7172011-03-31 21:26:24 +0000202
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000203 GrGLUniformManager fUniformManager;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000204 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000205
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000206 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000207};
208
209#endif