GrGLProgram.h revision 05a2ee052c9ef4c781b7b590b00b3d2da3b3449a
1/* 2 * 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. 6 */ 7 8 9#ifndef GrGLProgram_DEFINED 10#define GrGLProgram_DEFINED 11 12#include "GrDrawState.h" 13#include "GrGLContext.h" 14#include "GrGLProgramDesc.h" 15#include "GrGLSL.h" 16#include "GrGLTexture.h" 17#include "GrGLUniformManager.h" 18 19#include "SkString.h" 20#include "SkXfermode.h" 21 22class GrBinHashKeyBuilder; 23class GrGLEffect; 24class GrGLShaderBuilder; 25 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 */ 35class GrGLProgram : public GrRefCnt { 36public: 37 SK_DECLARE_INST_COUNT(GrGLProgram) 38 39 static GrGLProgram* Create(const GrGLContext& gl, 40 const GrGLProgramDesc& desc, 41 const GrEffectStage* stages[]); 42 43 virtual ~GrGLProgram(); 44 45 /** 46 * Call to abandon GL objects owned by this program. 47 */ 48 void abandon(); 49 50 /** 51 * The shader may modify the blend coefficients. Params are in/out 52 */ 53 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; 54 55 const GrGLProgramDesc& getDesc() { return fDesc; } 56 57 /** 58 * Gets the GL program ID for this program. 59 */ 60 GrGLuint programID() const { return fProgramID; } 61 62 /** 63 * Some GL state that is relevant to programs is not stored per-program. In particular color 64 * and coverage attributes can be global state. This struct is read and updated by 65 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state 66 * redundantly. 67 */ 68 struct SharedGLState { 69 GrColor fConstAttribColor; 70 int fConstAttribColorIndex; 71 GrColor fConstAttribCoverage; 72 int fConstAttribCoverageIndex; 73 74 SharedGLState() { this->invalidate(); } 75 void invalidate() { 76 fConstAttribColor = GrColor_ILLEGAL; 77 fConstAttribColorIndex = -1; 78 fConstAttribCoverage = GrColor_ILLEGAL; 79 fConstAttribCoverageIndex = -1; 80 } 81 }; 82 83 /** 84 * The GrDrawState's view matrix along with the aspects of the render target determine the 85 * matrix sent to GL. The size of the render target affects the GL matrix because we must 86 * convert from Skia device coords to GL's normalized coords. Also the origin of the render 87 * target may require us to perform a mirror-flip. 88 */ 89 struct MatrixState { 90 SkMatrix fViewMatrix; 91 SkISize fRenderTargetSize; 92 GrSurfaceOrigin fRenderTargetOrigin; 93 94 MatrixState() { this->invalidate(); } 95 void invalidate() { 96 fViewMatrix = SkMatrix::InvalidMatrix(); 97 fRenderTargetSize.fWidth = -1; 98 fRenderTargetSize.fHeight = -1; 99 fRenderTargetOrigin = (GrSurfaceOrigin) -1; 100 } 101 }; 102 103 /** 104 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a 105 * draw occurs using the program after the program has already been bound. It also uses the 106 * GrGpuGL object to bind the textures required by the GrGLEffects. 107 * 108 * The color and coverage params override the GrDrawState's getColor() and getCoverage() values. 109 */ 110 void setData(GrGpuGL*, 111 GrColor color, 112 GrColor coverage, 113 const GrDeviceCoordTexture* dstCopy, // can be NULL 114 SharedGLState*); 115 116private: 117 GrGLProgram(const GrGLContext& gl, 118 const GrGLProgramDesc& desc, 119 const GrEffectStage* stages[]); 120 121 bool succeeded() const { return 0 != fProgramID; } 122 123 /** 124 * This is the heavy initialization routine for building a GLProgram. 125 */ 126 bool genProgram(const GrEffectStage* stages[]); 127 128 void genInputColor(GrGLShaderBuilder* builder, SkString* inColor); 129 130 void genGeometryShader(GrGLShaderBuilder* segments) const; 131 132 typedef GrGLUniformManager::UniformHandle UniformHandle; 133 134 void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage); 135 136 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program 137 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, 138 bool bindColorOut, 139 bool bindDualSrcOut); 140 141 // Sets the texture units for samplers 142 void initSamplerUniforms(); 143 144 bool compileShaders(const GrGLShaderBuilder& builder); 145 146 const char* adjustInColor(const SkString& inColor) const; 147 148 // Helper for setData(). Makes GL calls to specify the initial color when there is not 149 // per-vertex colors. 150 void setColor(const GrDrawState&, GrColor color, SharedGLState*); 151 152 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not 153 // per-vertex coverages. 154 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*); 155 156 // Helper for setData() that sets the view matrix and loads the render target height uniform 157 void setMatrixAndRenderTargetHeight(const GrDrawState&); 158 159 typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray; 160 161 struct UniformHandles { 162 UniformHandle fViewMatrixUni; 163 UniformHandle fColorUni; 164 UniformHandle fCoverageUni; 165 UniformHandle fColorFilterUni; 166 167 // We use the render target height to provide a y-down frag coord when specifying 168 // origin_upper_left is not supported. 169 UniformHandle fRTHeightUni; 170 171 // Uniforms for computing texture coords to do the dst-copy lookup 172 UniformHandle fDstCopyTopLeftUni; 173 UniformHandle fDstCopyScaleUni; 174 UniformHandle fDstCopySamplerUni; 175 176 // An array of sampler uniform handles for each effect. 177 SamplerUniSArray fEffectSamplerUnis[GrDrawState::kNumStages]; 178 179 UniformHandles() { 180 fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle; 181 fColorUni = GrGLUniformManager::kInvalidUniformHandle; 182 fCoverageUni = GrGLUniformManager::kInvalidUniformHandle; 183 fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle; 184 fRTHeightUni = GrGLUniformManager::kInvalidUniformHandle; 185 fDstCopyTopLeftUni = GrGLUniformManager::kInvalidUniformHandle; 186 fDstCopyScaleUni = GrGLUniformManager::kInvalidUniformHandle; 187 fDstCopySamplerUni = GrGLUniformManager::kInvalidUniformHandle; 188 } 189 }; 190 191 // GL IDs 192 GrGLuint fVShaderID; 193 GrGLuint fGShaderID; 194 GrGLuint fFShaderID; 195 GrGLuint fProgramID; 196 197 // these reflect the current values of uniforms (GL uniform values travel with program) 198 MatrixState fMatrixState; 199 GrColor fColor; 200 GrColor fCoverage; 201 GrColor fColorFilterColor; 202 203 GrGLEffect* fEffects[GrDrawState::kNumStages]; 204 205 GrGLProgramDesc fDesc; 206 const GrGLContext& fContext; 207 208 GrGLUniformManager fUniformManager; 209 UniformHandles fUniformHandles; 210 211 typedef GrRefCnt INHERITED; 212}; 213 214#endif 215