xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gl/GrGLProgram.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2011 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/gl/GrGLProgram.h"
8*c8dee2aaSAndroid Build Coastguard Worker 
9*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkSamplingOptions.h"
10*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/ganesh/GrTypes.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/ganesh/gl/GrGLFunctions.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/ganesh/gl/GrGLInterface.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/Swizzle.h"
14*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrCaps.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrFragmentProcessor.h"
16*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrGeometryProcessor.h"
17*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrPipeline.h"
18*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrProgramInfo.h"
19*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrRenderTarget.h"
20*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrSamplerState.h"
21*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrSurfaceProxy.h"
22*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrSurfaceProxyView.h"
23*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/GrXferProcessor.h"
24*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/effects/GrTextureEffect.h"
25*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/gl/GrGLGpu.h"
26*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/gl/GrGLTexture.h"
27*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/ganesh/gl/GrGLUtil.h"
28*c8dee2aaSAndroid Build Coastguard Worker #include "src/sksl/SkSLCompiler.h"
29*c8dee2aaSAndroid Build Coastguard Worker 
30*c8dee2aaSAndroid Build Coastguard Worker #include <array>
31*c8dee2aaSAndroid Build Coastguard Worker #include <functional>
32*c8dee2aaSAndroid Build Coastguard Worker #include <utility>
33*c8dee2aaSAndroid Build Coastguard Worker 
34*c8dee2aaSAndroid Build Coastguard Worker class GrTexture;
35*c8dee2aaSAndroid Build Coastguard Worker 
36*c8dee2aaSAndroid Build Coastguard Worker #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X)
37*c8dee2aaSAndroid Build Coastguard Worker #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X)
38*c8dee2aaSAndroid Build Coastguard Worker 
39*c8dee2aaSAndroid Build Coastguard Worker ///////////////////////////////////////////////////////////////////////////////////////////////////
40*c8dee2aaSAndroid Build Coastguard Worker 
Make(GrGLGpu * gpu,const GrGLSLBuiltinUniformHandles & builtinUniforms,GrGLuint programID,const UniformInfoArray & uniforms,const UniformInfoArray & textureSamplers,std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl,std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl,std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,std::unique_ptr<Attribute[]> attributes,int vertexAttributeCnt,int instanceAttributeCnt,int vertexStride,int instanceStride)41*c8dee2aaSAndroid Build Coastguard Worker sk_sp<GrGLProgram> GrGLProgram::Make(
42*c8dee2aaSAndroid Build Coastguard Worker         GrGLGpu* gpu,
43*c8dee2aaSAndroid Build Coastguard Worker         const GrGLSLBuiltinUniformHandles& builtinUniforms,
44*c8dee2aaSAndroid Build Coastguard Worker         GrGLuint programID,
45*c8dee2aaSAndroid Build Coastguard Worker         const UniformInfoArray& uniforms,
46*c8dee2aaSAndroid Build Coastguard Worker         const UniformInfoArray& textureSamplers,
47*c8dee2aaSAndroid Build Coastguard Worker         std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl,
48*c8dee2aaSAndroid Build Coastguard Worker         std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl,
49*c8dee2aaSAndroid Build Coastguard Worker         std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,
50*c8dee2aaSAndroid Build Coastguard Worker         std::unique_ptr<Attribute[]> attributes,
51*c8dee2aaSAndroid Build Coastguard Worker         int vertexAttributeCnt,
52*c8dee2aaSAndroid Build Coastguard Worker         int instanceAttributeCnt,
53*c8dee2aaSAndroid Build Coastguard Worker         int vertexStride,
54*c8dee2aaSAndroid Build Coastguard Worker         int instanceStride) {
55*c8dee2aaSAndroid Build Coastguard Worker     sk_sp<GrGLProgram> program(new GrGLProgram(gpu,
56*c8dee2aaSAndroid Build Coastguard Worker                                                builtinUniforms,
57*c8dee2aaSAndroid Build Coastguard Worker                                                programID,
58*c8dee2aaSAndroid Build Coastguard Worker                                                uniforms,
59*c8dee2aaSAndroid Build Coastguard Worker                                                textureSamplers,
60*c8dee2aaSAndroid Build Coastguard Worker                                                std::move(gpImpl),
61*c8dee2aaSAndroid Build Coastguard Worker                                                std::move(xpImpl),
62*c8dee2aaSAndroid Build Coastguard Worker                                                std::move(fpImpls),
63*c8dee2aaSAndroid Build Coastguard Worker                                                std::move(attributes),
64*c8dee2aaSAndroid Build Coastguard Worker                                                vertexAttributeCnt,
65*c8dee2aaSAndroid Build Coastguard Worker                                                instanceAttributeCnt,
66*c8dee2aaSAndroid Build Coastguard Worker                                                vertexStride,
67*c8dee2aaSAndroid Build Coastguard Worker                                                instanceStride));
68*c8dee2aaSAndroid Build Coastguard Worker     // Assign texture units to sampler uniforms one time up front.
69*c8dee2aaSAndroid Build Coastguard Worker     gpu->flushProgram(program);
70*c8dee2aaSAndroid Build Coastguard Worker     program->fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
71*c8dee2aaSAndroid Build Coastguard Worker     return program;
72*c8dee2aaSAndroid Build Coastguard Worker }
73*c8dee2aaSAndroid Build Coastguard Worker 
GrGLProgram(GrGLGpu * gpu,const GrGLSLBuiltinUniformHandles & builtinUniforms,GrGLuint programID,const UniformInfoArray & uniforms,const UniformInfoArray & textureSamplers,std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl,std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl,std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,std::unique_ptr<Attribute[]> attributes,int vertexAttributeCnt,int instanceAttributeCnt,int vertexStride,int instanceStride)74*c8dee2aaSAndroid Build Coastguard Worker GrGLProgram::GrGLProgram(GrGLGpu* gpu,
75*c8dee2aaSAndroid Build Coastguard Worker                          const GrGLSLBuiltinUniformHandles& builtinUniforms,
76*c8dee2aaSAndroid Build Coastguard Worker                          GrGLuint programID,
77*c8dee2aaSAndroid Build Coastguard Worker                          const UniformInfoArray& uniforms,
78*c8dee2aaSAndroid Build Coastguard Worker                          const UniformInfoArray& textureSamplers,
79*c8dee2aaSAndroid Build Coastguard Worker                          std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl,
80*c8dee2aaSAndroid Build Coastguard Worker                          std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl,
81*c8dee2aaSAndroid Build Coastguard Worker                          std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,
82*c8dee2aaSAndroid Build Coastguard Worker                          std::unique_ptr<Attribute[]> attributes,
83*c8dee2aaSAndroid Build Coastguard Worker                          int vertexAttributeCnt,
84*c8dee2aaSAndroid Build Coastguard Worker                          int instanceAttributeCnt,
85*c8dee2aaSAndroid Build Coastguard Worker                          int vertexStride,
86*c8dee2aaSAndroid Build Coastguard Worker                          int instanceStride)
87*c8dee2aaSAndroid Build Coastguard Worker         : fBuiltinUniformHandles(builtinUniforms)
88*c8dee2aaSAndroid Build Coastguard Worker         , fProgramID(programID)
89*c8dee2aaSAndroid Build Coastguard Worker         , fGPImpl(std::move(gpImpl))
90*c8dee2aaSAndroid Build Coastguard Worker         , fXPImpl(std::move(xpImpl))
91*c8dee2aaSAndroid Build Coastguard Worker         , fFPImpls(std::move(fpImpls))
92*c8dee2aaSAndroid Build Coastguard Worker         , fAttributes(std::move(attributes))
93*c8dee2aaSAndroid Build Coastguard Worker         , fVertexAttributeCnt(vertexAttributeCnt)
94*c8dee2aaSAndroid Build Coastguard Worker         , fInstanceAttributeCnt(instanceAttributeCnt)
95*c8dee2aaSAndroid Build Coastguard Worker         , fVertexStride(vertexStride)
96*c8dee2aaSAndroid Build Coastguard Worker         , fInstanceStride(instanceStride)
97*c8dee2aaSAndroid Build Coastguard Worker         , fGpu(gpu)
98*c8dee2aaSAndroid Build Coastguard Worker         , fProgramDataManager(gpu, uniforms)
99*c8dee2aaSAndroid Build Coastguard Worker         , fNumTextureSamplers(textureSamplers.count()) {}
100*c8dee2aaSAndroid Build Coastguard Worker 
~GrGLProgram()101*c8dee2aaSAndroid Build Coastguard Worker GrGLProgram::~GrGLProgram() {
102*c8dee2aaSAndroid Build Coastguard Worker     if (fProgramID) {
103*c8dee2aaSAndroid Build Coastguard Worker         GL_CALL(DeleteProgram(fProgramID));
104*c8dee2aaSAndroid Build Coastguard Worker     }
105*c8dee2aaSAndroid Build Coastguard Worker }
106*c8dee2aaSAndroid Build Coastguard Worker 
abandon()107*c8dee2aaSAndroid Build Coastguard Worker void GrGLProgram::abandon() {
108*c8dee2aaSAndroid Build Coastguard Worker     fProgramID = 0;
109*c8dee2aaSAndroid Build Coastguard Worker }
110*c8dee2aaSAndroid Build Coastguard Worker 
111*c8dee2aaSAndroid Build Coastguard Worker ///////////////////////////////////////////////////////////////////////////////
112*c8dee2aaSAndroid Build Coastguard Worker 
updateUniforms(const GrRenderTarget * renderTarget,const GrProgramInfo & programInfo)113*c8dee2aaSAndroid Build Coastguard Worker void GrGLProgram::updateUniforms(const GrRenderTarget* renderTarget,
114*c8dee2aaSAndroid Build Coastguard Worker                                  const GrProgramInfo& programInfo) {
115*c8dee2aaSAndroid Build Coastguard Worker     this->setRenderTargetState(renderTarget, programInfo.origin(), programInfo.geomProc());
116*c8dee2aaSAndroid Build Coastguard Worker 
117*c8dee2aaSAndroid Build Coastguard Worker     // we set the uniforms for installed processors in a generic way, but subclasses of GLProgram
118*c8dee2aaSAndroid Build Coastguard Worker     // determine how to set coord transforms
119*c8dee2aaSAndroid Build Coastguard Worker 
120*c8dee2aaSAndroid Build Coastguard Worker     // We must bind to texture units in the same order in which we set the uniforms in
121*c8dee2aaSAndroid Build Coastguard Worker     // GrGLProgramDataManager. That is, we bind textures for processors in this order:
122*c8dee2aaSAndroid Build Coastguard Worker     // primProc, fragProcs, XP.
123*c8dee2aaSAndroid Build Coastguard Worker     fGPImpl->setData(fProgramDataManager, *fGpu->caps()->shaderCaps(), programInfo.geomProc());
124*c8dee2aaSAndroid Build Coastguard Worker 
125*c8dee2aaSAndroid Build Coastguard Worker     for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) {
126*c8dee2aaSAndroid Build Coastguard Worker         const auto& fp = programInfo.pipeline().getFragmentProcessor(i);
127*c8dee2aaSAndroid Build Coastguard Worker         fp.visitWithImpls([&](const GrFragmentProcessor& fp,
128*c8dee2aaSAndroid Build Coastguard Worker                               GrFragmentProcessor::ProgramImpl& impl) {
129*c8dee2aaSAndroid Build Coastguard Worker             impl.setData(fProgramDataManager, fp);
130*c8dee2aaSAndroid Build Coastguard Worker         }, *fFPImpls[i]);
131*c8dee2aaSAndroid Build Coastguard Worker     }
132*c8dee2aaSAndroid Build Coastguard Worker 
133*c8dee2aaSAndroid Build Coastguard Worker     programInfo.pipeline().setDstTextureUniforms(fProgramDataManager, &fBuiltinUniformHandles);
134*c8dee2aaSAndroid Build Coastguard Worker     fXPImpl->setData(fProgramDataManager, programInfo.pipeline().getXferProcessor());
135*c8dee2aaSAndroid Build Coastguard Worker }
136*c8dee2aaSAndroid Build Coastguard Worker 
bindTextures(const GrGeometryProcessor & geomProc,const GrSurfaceProxy * const geomProcTextures[],const GrPipeline & pipeline)137*c8dee2aaSAndroid Build Coastguard Worker void GrGLProgram::bindTextures(const GrGeometryProcessor& geomProc,
138*c8dee2aaSAndroid Build Coastguard Worker                                const GrSurfaceProxy* const geomProcTextures[],
139*c8dee2aaSAndroid Build Coastguard Worker                                const GrPipeline& pipeline) {
140*c8dee2aaSAndroid Build Coastguard Worker     // Bind textures from the geometry processor.
141*c8dee2aaSAndroid Build Coastguard Worker     for (int i = 0; i < geomProc.numTextureSamplers(); ++i) {
142*c8dee2aaSAndroid Build Coastguard Worker         SkASSERT(geomProcTextures[i]->asTextureProxy());
143*c8dee2aaSAndroid Build Coastguard Worker         auto* overrideTexture = static_cast<GrGLTexture*>(geomProcTextures[i]->peekTexture());
144*c8dee2aaSAndroid Build Coastguard Worker         fGpu->bindTexture(i, geomProc.textureSampler(i).samplerState(),
145*c8dee2aaSAndroid Build Coastguard Worker                           geomProc.textureSampler(i).swizzle(), overrideTexture);
146*c8dee2aaSAndroid Build Coastguard Worker     }
147*c8dee2aaSAndroid Build Coastguard Worker     int nextTexSamplerIdx = geomProc.numTextureSamplers();
148*c8dee2aaSAndroid Build Coastguard Worker     // Bind texture from the destination proxy view.
149*c8dee2aaSAndroid Build Coastguard Worker     GrTexture* dstTexture = pipeline.peekDstTexture();
150*c8dee2aaSAndroid Build Coastguard Worker     if (dstTexture) {
151*c8dee2aaSAndroid Build Coastguard Worker         fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::Filter::kNearest,
152*c8dee2aaSAndroid Build Coastguard Worker                           pipeline.dstProxyView().swizzle(), static_cast<GrGLTexture*>(dstTexture));
153*c8dee2aaSAndroid Build Coastguard Worker     }
154*c8dee2aaSAndroid Build Coastguard Worker     // Bind textures from all of the fragment processors.
155*c8dee2aaSAndroid Build Coastguard Worker     pipeline.visitTextureEffects([&](const GrTextureEffect& te) {
156*c8dee2aaSAndroid Build Coastguard Worker         GrSamplerState samplerState = te.samplerState();
157*c8dee2aaSAndroid Build Coastguard Worker         skgpu::Swizzle swizzle = te.view().swizzle();
158*c8dee2aaSAndroid Build Coastguard Worker         auto* texture = static_cast<GrGLTexture*>(te.texture());
159*c8dee2aaSAndroid Build Coastguard Worker         fGpu->bindTexture(nextTexSamplerIdx++, samplerState, swizzle, texture);
160*c8dee2aaSAndroid Build Coastguard Worker     });
161*c8dee2aaSAndroid Build Coastguard Worker 
162*c8dee2aaSAndroid Build Coastguard Worker     SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
163*c8dee2aaSAndroid Build Coastguard Worker }
164*c8dee2aaSAndroid Build Coastguard Worker 
setRenderTargetState(const GrRenderTarget * rt,GrSurfaceOrigin origin,const GrGeometryProcessor & geomProc)165*c8dee2aaSAndroid Build Coastguard Worker void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt,
166*c8dee2aaSAndroid Build Coastguard Worker                                        GrSurfaceOrigin origin,
167*c8dee2aaSAndroid Build Coastguard Worker                                        const GrGeometryProcessor& geomProc) {
168*c8dee2aaSAndroid Build Coastguard Worker     // Set RT adjustment and RT flip
169*c8dee2aaSAndroid Build Coastguard Worker     SkISize dimensions = rt->dimensions();
170*c8dee2aaSAndroid Build Coastguard Worker     if (fRenderTargetState.fRenderTargetOrigin != origin ||
171*c8dee2aaSAndroid Build Coastguard Worker         fRenderTargetState.fRenderTargetSize != dimensions) {
172*c8dee2aaSAndroid Build Coastguard Worker         fRenderTargetState.fRenderTargetSize = dimensions;
173*c8dee2aaSAndroid Build Coastguard Worker         fRenderTargetState.fRenderTargetOrigin = origin;
174*c8dee2aaSAndroid Build Coastguard Worker 
175*c8dee2aaSAndroid Build Coastguard Worker         // The client will mark a swap buffer as kBottomLeft when making a SkSurface because
176*c8dee2aaSAndroid Build Coastguard Worker         // GL's framebuffer space has (0, 0) at the bottom left. In NDC (-1, -1) is also the
177*c8dee2aaSAndroid Build Coastguard Worker         // bottom left. However, Skia's device coords has (0, 0) at the top left, so a flip is
178*c8dee2aaSAndroid Build Coastguard Worker         // required when the origin is kBottomLeft.
179*c8dee2aaSAndroid Build Coastguard Worker         bool flip = (origin == kBottomLeft_GrSurfaceOrigin);
180*c8dee2aaSAndroid Build Coastguard Worker         std::array<float, 4> v = SkSL::Compiler::GetRTAdjustVector(dimensions, flip);
181*c8dee2aaSAndroid Build Coastguard Worker         fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, v.data());
182*c8dee2aaSAndroid Build Coastguard Worker         if (fBuiltinUniformHandles.fRTFlipUni.isValid()) {
183*c8dee2aaSAndroid Build Coastguard Worker             std::array<float, 2> d = SkSL::Compiler::GetRTFlipVector(dimensions.height(), flip);
184*c8dee2aaSAndroid Build Coastguard Worker             fProgramDataManager.set2fv(fBuiltinUniformHandles.fRTFlipUni, 1, d.data());
185*c8dee2aaSAndroid Build Coastguard Worker         }
186*c8dee2aaSAndroid Build Coastguard Worker     }
187*c8dee2aaSAndroid Build Coastguard Worker }
188