2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #ifndef GrGLProgram_DEFINED
10 #define GrGLProgram_DEFINED
12 #include "GrDrawState.h"
13 #include "GrGLContext.h"
14 #include "GrGLProgramDesc.h"
15 #include "GrGLShaderBuilder.h"
17 #include "GrGLTexture.h"
18 #include "GrGLProgramDataManager.h"
21 #include "SkXfermode.h"
24 class GrGLProgramEffects;
25 class GrGLShaderBuilder;
28 * This class manages a GPU program and records per-program information.
29 * We can specify the attribute locations so that they are constant
30 * across our shaders. But the driver determines the uniform locations
31 * at link time. We don't need to remember the sampler uniform location
32 * because we will bind a texture slot to it and never change it
33 * Uniforms are program-local so we can't rely on fHWState to hold the
34 * previous uniform state after a program change.
36 class GrGLProgram : public SkRefCnt {
38 SK_DECLARE_INST_COUNT(GrGLProgram)
40 typedef GrGLShaderBuilder::BuiltinUniformHandles BuiltinUniformHandles;
42 static GrGLProgram* Create(GrGpuGL* gpu,
43 const GrGLProgramDesc& desc,
44 const GrEffectStage* colorStages[],
45 const GrEffectStage* coverageStages[]);
47 virtual ~GrGLProgram();
50 * Call to abandon GL objects owned by this program.
55 * The shader may modify the blend coefficients. Params are in/out.
57 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
59 const GrGLProgramDesc& getDesc() { return fDesc; }
62 * Gets the GL program ID for this program.
64 GrGLuint programID() const { return fProgramID; }
66 bool hasVertexShader() const { return fHasVertexShader; }
69 * Some GL state that is relevant to programs is not stored per-program. In particular color
70 * and coverage attributes can be global state. This struct is read and updated by
71 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
74 struct SharedGLState {
75 GrColor fConstAttribColor;
76 int fConstAttribColorIndex;
77 GrColor fConstAttribCoverage;
78 int fConstAttribCoverageIndex;
80 SharedGLState() { this->invalidate(); }
82 fConstAttribColor = GrColor_ILLEGAL;
83 fConstAttribColorIndex = -1;
84 fConstAttribCoverage = GrColor_ILLEGAL;
85 fConstAttribCoverageIndex = -1;
90 * The GrDrawState's view matrix along with the aspects of the render target determine the
91 * matrix sent to GL. The size of the render target affects the GL matrix because we must
92 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
93 * target may require us to perform a mirror-flip.
97 SkISize fRenderTargetSize;
98 GrSurfaceOrigin fRenderTargetOrigin;
100 MatrixState() { this->invalidate(); }
102 fViewMatrix = SkMatrix::InvalidMatrix();
103 fRenderTargetSize.fWidth = -1;
104 fRenderTargetSize.fHeight = -1;
105 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
109 * Gets a matrix that goes from local coords to Skia's device coordinates.
111 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
112 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
116 * Gets a matrix that goes from local coordinates to GL normalized device coords.
118 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
120 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
121 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
122 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
125 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
126 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
129 combined.preConcat(fViewMatrix);
130 GrGLGetMatrix<Size>(destMatrix, combined);
134 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
135 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
137 * pos.x = dot(v.xy, pos.xz)
138 * pos.y = dot(v.zq, pos.yz)
140 void getRTAdjustmentVec(GrGLfloat* destVec) {
141 destVec[0] = 2.f / fRenderTargetSize.fWidth;
143 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
144 destVec[2] = -2.f / fRenderTargetSize.fHeight;
147 destVec[2] = 2.f / fRenderTargetSize.fHeight;
154 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
155 * draw occurs using the program after the program has already been bound. It also uses the
156 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
157 * stages come from GrGLProgramDesc::Build().
159 void setData(GrDrawState::BlendOptFlags,
160 const GrEffectStage* colorStages[],
161 const GrEffectStage* coverageStages[],
162 const GrDeviceCoordTexture* dstCopy, // can be NULL
166 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
168 GrGLProgram(GrGpuGL*,
169 const GrGLProgramDesc&,
170 const GrGLShaderBuilder&);
172 // Sets the texture units for samplers.
173 void initSamplerUniforms();
175 // Helper for setData(). Makes GL calls to specify the initial color when there is not
176 // per-vertex colors.
177 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
179 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
180 // per-vertex coverages.
181 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
183 // Helper for setData() that sets the view matrix and loads the render target height uniform
184 void setMatrixAndRenderTargetHeight(const GrDrawState&);
186 // these reflect the current values of uniforms (GL uniform values travel with program)
187 MatrixState fMatrixState;
192 BuiltinUniformHandles fBuiltinUniformHandles;
193 SkAutoTUnref<GrGLProgramEffects> fColorEffects;
194 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects;
196 bool fHasVertexShader;
199 GrGLProgramDesc fDesc;
202 GrGLProgramDataManager fProgramDataManager;
204 typedef SkRefCnt INHERITED;