• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GrGLContext.h"
13 #include "GrGLProgramDesc.h"
14 #include "GrGLTexture.h"
15 #include "GrGLProgramDataManager.h"
16 #include "glsl/GrGLSLProgramDataManager.h"
17 #include "glsl/GrGLSLUniformHandler.h"
18 
19 #include "SkString.h"
20 #include "SkXfermode.h"
21 
22 #include "builders/GrGLProgramBuilder.h"
23 
24 class GrGLInstalledProcessors;
25 class GrGLProgramBuilder;
26 class GrPipeline;
27 
28 /**
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.
36  */
37 class GrGLProgram : public SkRefCnt {
38 public:
39     typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;
40 
41     ~GrGLProgram();
42 
43     /**
44      * Call to abandon GL objects owned by this program.
45      */
46     void abandon();
47 
getDesc()48     const GrProgramDesc& getDesc() { return fDesc; }
49 
50     /**
51      * Gets the GL program ID for this program.
52      */
programID()53     GrGLuint programID() const { return fProgramID; }
54 
55     /**
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
58      * them.
59      */
60     struct RenderTargetState {
61         SkISize         fRenderTargetSize;
62         GrSurfaceOrigin fRenderTargetOrigin;
63 
RenderTargetStateRenderTargetState64         RenderTargetState() { this->invalidate(); }
invalidateRenderTargetState65         void invalidate() {
66             fRenderTargetSize.fWidth = -1;
67             fRenderTargetSize.fHeight = -1;
68             fRenderTargetOrigin = (GrSurfaceOrigin) -1;
69         }
70 
71         /**
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
74          * applied as such:
75          * pos.x = dot(v.xy, pos.xz)
76          * pos.y = dot(v.zw, pos.yz)
77          */
getRTAdjustmentVecRenderTargetState78         void getRTAdjustmentVec(float* destVec) {
79             destVec[0] = 2.f / fRenderTargetSize.fWidth;
80             destVec[1] = -1.f;
81             if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
82                 destVec[2] = -2.f / fRenderTargetSize.fHeight;
83                 destVec[3] = 1.f;
84             } else {
85                 destVec[2] = 2.f / fRenderTargetSize.fHeight;
86                 destVec[3] = -1.f;
87             }
88         }
89     };
90 
91     /**
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.)
96      */
97     void setData(const GrPrimitiveProcessor&, const GrPipeline&,
98                  SkTArray<const GrTextureAccess*>* textureBindings);
99 
100 protected:
101     typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
102     typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
103     typedef GrGLProgramDataManager::VaryingInfoArray VaryingInfoArray;
104 
105     GrGLProgram(GrGLGpu*,
106                 const GrProgramDesc&,
107                 const BuiltinUniformHandles&,
108                 GrGLuint programID,
109                 const UniformInfoArray&,
110                 const VaryingInfoArray&, // used for NVPR only currently
111                 GrGLSLPrimitiveProcessor* geometryProcessor,
112                 GrGLSLXferProcessor* xferProcessor,
113                 const GrGLSLFragProcs& fragmentProcessors,
114                 SkTArray<UniformHandle>* passSamplerUniforms);
115 
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&,
121                           int index);
122 
123     // Helper for setData() that sets the view matrix and loads the render target height uniform
124     void setRenderTargetState(const GrPrimitiveProcessor&, const GrPipeline&);
125 
126     // these reflect the current values of uniforms (GL uniform values travel with program)
127     RenderTargetState fRenderTargetState;
128     BuiltinUniformHandles fBuiltinUniformHandles;
129     GrGLuint fProgramID;
130 
131     // the installed effects
132     SkAutoTDelete<GrGLSLPrimitiveProcessor> fGeometryProcessor;
133     SkAutoTDelete<GrGLSLXferProcessor> fXferProcessor;
134     GrGLSLFragProcs fFragmentProcessors;
135 
136     GrProgramDesc fDesc;
137     GrGLGpu* fGpu;
138     GrGLProgramDataManager fProgramDataManager;
139     SkTArray<UniformHandle> fSamplerUniforms;
140 
141     friend class GrGLProgramBuilder;
142 
143     typedef SkRefCnt INHERITED;
144 };
145 
146 #endif
147