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 "GrGLContext.h"
13 #include "GrGLProgramDesc.h"
14 #include "GrGLTexture.h"
15 #include "GrGLProgramDataManager.h"
16 #include "glsl/GrGLSLProgramDataManager.h"
17 #include "glsl/GrGLSLUniformHandler.h"
20 #include "SkXfermode.h"
22 #include "builders/GrGLProgramBuilder.h"
24 class GrGLInstalledProcessors;
25 class GrGLProgramBuilder;
29 * This class manages a GPU program and records per-program information.
30 * We can specify the attribute locations so that they are constant
31 * across our shaders. But the driver determines the uniform locations
32 * at link time. We don't need to remember the sampler uniform location
33 * because we will bind a texture slot to it and never change it
34 * Uniforms are program-local so we can't rely on fHWState to hold the
35 * previous uniform state after a program change.
37 class GrGLProgram : public SkRefCnt {
39 typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;
44 * Call to abandon GL objects owned by this program.
48 const GrProgramDesc& getDesc() { return fDesc; }
51 * Gets the GL program ID for this program.
53 GrGLuint programID() const { return fProgramID; }
56 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
57 * space and to make device space positions have the correct origin for processors that require
60 struct RenderTargetState {
61 SkISize fRenderTargetSize;
62 GrSurfaceOrigin fRenderTargetOrigin;
64 RenderTargetState() { this->invalidate(); }
66 fRenderTargetSize.fWidth = -1;
67 fRenderTargetSize.fHeight = -1;
68 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
72 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
73 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
75 * pos.x = dot(v.xy, pos.xz)
76 * pos.y = dot(v.zw, pos.yz)
78 void getRTAdjustmentVec(float* destVec) {
79 destVec[0] = 2.f / fRenderTargetSize.fWidth;
81 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
82 destVec[2] = -2.f / fRenderTargetSize.fHeight;
85 destVec[2] = 2.f / fRenderTargetSize.fHeight;
92 * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the
93 * textures that need to be bound on each unit. It is the caller's responsibility to ensure
94 * the program is bound before calling, and to bind the outgoing textures to their respective
95 * units upon return. (Each index in the array corresponds to its matching GL texture unit.)
97 void setData(const GrPrimitiveProcessor&, const GrPipeline&,
98 SkTArray<const GrTextureAccess*>* textureBindings);
101 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
102 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
103 typedef GrGLProgramDataManager::VaryingInfoArray VaryingInfoArray;
105 GrGLProgram(GrGLGpu*,
106 const GrProgramDesc&,
107 const BuiltinUniformHandles&,
109 const UniformInfoArray&,
110 const VaryingInfoArray&, // used for NVPR only currently
111 GrGLInstalledGeoProc* geometryProcessor,
112 GrGLInstalledXferProc* xferProcessor,
113 GrGLInstalledFragProcs* fragmentProcessors,
114 SkTArray<UniformHandle>* passSamplerUniforms);
116 // A templated helper to loop over effects, set the transforms(via subclass) and bind textures
117 void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&,
118 SkTArray<const GrTextureAccess*>* textureBindings);
119 void setTransformData(const GrPrimitiveProcessor&,
120 const GrFragmentProcessor&,
122 GrGLInstalledFragProc*);
124 // Helper for setData() that sets the view matrix and loads the render target height uniform
125 void setRenderTargetState(const GrPrimitiveProcessor&, const GrPipeline&);
127 // these reflect the current values of uniforms (GL uniform values travel with program)
128 RenderTargetState fRenderTargetState;
129 BuiltinUniformHandles fBuiltinUniformHandles;
132 // the installed effects
133 SkAutoTDelete<GrGLInstalledGeoProc> fGeometryProcessor;
134 SkAutoTDelete<GrGLInstalledXferProc> fXferProcessor;
135 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors;
139 GrGLProgramDataManager fProgramDataManager;
140 SkTArray<UniformHandle> fSamplerUniforms;
142 friend class GrGLProgramBuilder;
144 typedef SkRefCnt INHERITED;