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 "src/gpu/gl/GrGLProgramDataManager.h"
13#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
14#include "src/gpu/glsl/GrGLSLUniformHandler.h"
15
16class GrGLSLFragmentProcessor;
17class GrGLSLPrimitiveProcessor;
18class GrGLSLXferProcessor;
19class GrPipeline;
20class GrPrimitiveProcessor;
21class GrProgramInfo;
22class GrRenderTarget;
23class GrTextureProxy;
24
25/**
26 * This class manages a GPU program and records per-program information. It also records the vertex
27 * and instance attribute layouts that are to be used with the program.
28 */
29class GrGLProgram : public SkRefCnt {
30public:
31 /**
32 * This class has its own Attribute representation as it does not need the name and we don't
33 * want to worry about copying the name string to memory with life time of GrGLProgram.
34 * Additionally, these store the attribute location.
35 */
36 struct Attribute {
37 GrVertexAttribType fCPUType;
38 GrSLType fGPUType;
39 size_t fOffset;
40 GrGLint fLocation;
41 };
42
43 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
44 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
45 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
46
47 /**
48 * The attribute array consists of vertexAttributeCnt + instanceAttributeCnt elements with
49 * the vertex attributes preceding the instance attributes.
50 */
51 static sk_sp<GrGLProgram> Make(GrGLGpu*,
52 const GrGLSLBuiltinUniformHandles&,
53 GrGLuint programID,
54 const UniformInfoArray& uniforms,
55 const UniformInfoArray& textureSamplers,
56 const VaryingInfoArray&, // used for NVPR only currently
57 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
58 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
59 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fps,
60 std::unique_ptr<Attribute[]>,
61 int vertexAttributeCnt,
62 int instanceAttributeCnt,
63 int vertexStride,
64 int instanceStride);
65
66 ~GrGLProgram() override;
67
68 /**
69 * Call to abandon GL objects owned by this program.
70 */
71 void abandon();
72
73 /**
74 * Gets the GL program ID for this program.
75 */
76 GrGLuint programID() const { return fProgramID; }
77
78 /**
79 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
80 * space and to make device space positions have the correct origin for processors that require
81 * them.
82 */
83 struct RenderTargetState {
84 SkISize fRenderTargetSize;
85 GrSurfaceOrigin fRenderTargetOrigin;
86
87 RenderTargetState() { this->invalidate(); }
88 void invalidate() {
89 fRenderTargetSize.fWidth = -1;
90 fRenderTargetSize.fHeight = -1;
91 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
92 }
93
94 /**
95 * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device
96 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
97 * applied as such:
98 * pos.x = dot(v.xy, pos.xz)
99 * pos.y = dot(v.zw, pos.yz)
100 */
101 void getRTAdjustmentVec(float* destVec) {
102 destVec[0] = 2.f / fRenderTargetSize.fWidth;
103 destVec[1] = -1.f;
104 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
105 destVec[2] = -2.f / fRenderTargetSize.fHeight;
106 destVec[3] = 1.f;
107 } else {
108 destVec[2] = 2.f / fRenderTargetSize.fHeight;
109 destVec[3] = -1.f;
110 }
111 }
112 };
113
114 /**
115 * This function uploads uniforms and calls each GrGLSL*Processor's setData.
116 *
117 * It is the caller's responsibility to ensure the program is bound before calling.
118 */
119 void updateUniforms(const GrRenderTarget*, const GrProgramInfo&);
120
121 /**
122 * Binds all primitive processor and fragment processor textures.
123 */
124 void bindTextures(const GrPrimitiveProcessor&, const GrSurfaceProxy* const primProcTextures[],
125 const GrPipeline&);
126
127 int vertexStride() const { return fVertexStride; }
128 int instanceStride() const { return fInstanceStride; }
129
130 int numVertexAttributes() const { return fVertexAttributeCnt; }
131 const Attribute& vertexAttribute(int i) const {
132 SkASSERT(i >= 0 && i < fVertexAttributeCnt);
133 return fAttributes[i];
134 }
135
136 int numInstanceAttributes() const { return fInstanceAttributeCnt; }
137 const Attribute& instanceAttribute(int i) const {
138 SkASSERT(i >= 0 && i < fInstanceAttributeCnt);
139 return fAttributes[i + fVertexAttributeCnt];
140 }
141
142private:
143 GrGLProgram(GrGLGpu*,
144 const GrGLSLBuiltinUniformHandles&,
145 GrGLuint programID,
146 const UniformInfoArray& uniforms,
147 const UniformInfoArray& textureSamplers,
148 const VaryingInfoArray&, // used for NVPR only currently
149 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
150 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
151 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fps,
152 std::unique_ptr<Attribute[]>,
153 int vertexAttributeCnt,
154 int instanceAttributeCnt,
155 int vertexStride,
156 int instanceStride);
157
158 // Helper for setData() that sets the view matrix and loads the render target height uniform
159 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin, const GrPrimitiveProcessor&);
160
161 // these reflect the current values of uniforms (GL uniform values travel with program)
162 RenderTargetState fRenderTargetState;
163 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
164 GrGLuint fProgramID;
165
166 // the installed effects
167 std::unique_ptr<GrGLSLPrimitiveProcessor> fPrimitiveProcessor;
168 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
169 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
170
171 std::unique_ptr<Attribute[]> fAttributes;
172 int fVertexAttributeCnt;
173 int fInstanceAttributeCnt;
174 int fVertexStride;
175 int fInstanceStride;
176
177 GrGLGpu* fGpu;
178 GrGLProgramDataManager fProgramDataManager;
179
180 int fNumTextureSamplers;
181
182 typedef SkRefCnt INHERITED;
183};
184
185#endif
186