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 "src/gpu/ganesh/GrFragmentProcessor.h"
13 #include "src/gpu/ganesh/GrGeometryProcessor.h"
14 #include "src/gpu/ganesh/GrXferProcessor.h"
15 #include "src/gpu/ganesh/gl/GrGLProgramDataManager.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
17 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
22 class GrGeometryProcessor;
28 * This class manages a GPU program and records per-program information. It also records the vertex
29 * and instance attribute layouts that are to be used with the program.
31 class GrGLProgram : public SkRefCnt {
34 * This class has its own Attribute representation as it does not need the name and we don't
35 * want to worry about copying the name string to memory with life time of GrGLProgram.
36 * Additionally, these store the attribute location.
39 GrVertexAttribType fCPUType;
45 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
46 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
47 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
50 * The attribute array consists of vertexAttributeCnt + instanceAttributeCnt elements with
51 * the vertex attributes preceding the instance attributes.
53 static sk_sp<GrGLProgram> Make(
55 const GrGLSLBuiltinUniformHandles&,
57 const UniformInfoArray& uniforms,
58 const UniformInfoArray& textureSamplers,
59 std::unique_ptr<GrGeometryProcessor::ProgramImpl>,
60 std::unique_ptr<GrXferProcessor::ProgramImpl>,
61 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fps,
62 std::unique_ptr<Attribute[]>,
63 int vertexAttributeCnt,
64 int instanceAttributeCnt,
68 ~GrGLProgram() override;
71 * Call to abandon GL objects owned by this program.
76 * Gets the GL program ID for this program.
78 GrGLuint programID() const { return fProgramID; }
81 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
82 * space and to make device space positions have the correct origin for processors that require
85 struct RenderTargetState {
86 SkISize fRenderTargetSize;
87 GrSurfaceOrigin fRenderTargetOrigin;
89 RenderTargetState() { this->invalidate(); }
91 fRenderTargetSize.fWidth = -1;
92 fRenderTargetSize.fHeight = -1;
93 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
98 * This function uploads uniforms and calls each GrGLSL*Processor's setData.
100 * It is the caller's responsibility to ensure the program is bound before calling.
102 void updateUniforms(const GrRenderTarget*, const GrProgramInfo&);
105 * Binds all geometry processor and fragment processor textures.
107 void bindTextures(const GrGeometryProcessor&,
108 const GrSurfaceProxy* const geomProcTextures[],
111 int vertexStride() const { return fVertexStride; }
112 int instanceStride() const { return fInstanceStride; }
114 int numVertexAttributes() const { return fVertexAttributeCnt; }
115 const Attribute& vertexAttribute(int i) const {
116 SkASSERT(i >= 0 && i < fVertexAttributeCnt);
117 return fAttributes[i];
120 int numInstanceAttributes() const { return fInstanceAttributeCnt; }
121 const Attribute& instanceAttribute(int i) const {
122 SkASSERT(i >= 0 && i < fInstanceAttributeCnt);
123 return fAttributes[i + fVertexAttributeCnt];
127 GrGLProgram(GrGLGpu*,
128 const GrGLSLBuiltinUniformHandles&,
130 const UniformInfoArray& uniforms,
131 const UniformInfoArray& textureSamplers,
132 std::unique_ptr<GrGeometryProcessor::ProgramImpl>,
133 std::unique_ptr<GrXferProcessor::ProgramImpl>,
134 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,
135 std::unique_ptr<Attribute[]>,
136 int vertexAttributeCnt,
137 int instanceAttributeCnt,
141 // Helper for setData() that sets the view matrix and loads the render target height uniform
142 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin, const GrGeometryProcessor&);
144 // these reflect the current values of uniforms (GL uniform values travel with program)
145 RenderTargetState fRenderTargetState;
146 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
149 // the installed effects
150 std::unique_ptr<GrGeometryProcessor::ProgramImpl> fGPImpl;
151 std::unique_ptr<GrXferProcessor::ProgramImpl> fXPImpl;
152 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fFPImpls;
154 std::unique_ptr<Attribute[]> fAttributes;
155 int fVertexAttributeCnt;
156 int fInstanceAttributeCnt;
161 GrGLProgramDataManager fProgramDataManager;
163 int fNumTextureSamplers;
165 using INHERITED = SkRefCnt;