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 #include "src/gpu/GrAllocator.h"
9 #include "src/gpu/GrCoordTransform.h"
10 #include "src/gpu/GrPathProcessor.h"
11 #include "src/gpu/GrPipeline.h"
12 #include "src/gpu/GrProcessor.h"
13 #include "src/gpu/GrTexturePriv.h"
14 #include "src/gpu/GrXferProcessor.h"
15 #include "src/gpu/gl/GrGLBuffer.h"
16 #include "src/gpu/gl/GrGLGpu.h"
17 #include "src/gpu/gl/GrGLPathRendering.h"
18 #include "src/gpu/gl/GrGLProgram.h"
19 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
20 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
21 #include "src/gpu/glsl/GrGLSLXferProcessor.h"
22
23 #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X)
24 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X)
25
26 ///////////////////////////////////////////////////////////////////////////////////////////////////
27
GrGLProgram(GrGLGpu * gpu,const GrGLSLBuiltinUniformHandles & builtinUniforms,GrGLuint programID,const UniformInfoArray & uniforms,const UniformInfoArray & textureSamplers,const VaryingInfoArray & pathProcVaryings,std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,std::unique_ptr<GrGLSLXferProcessor> xferProcessor,std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,int fragmentProcessorCnt,std::unique_ptr<Attribute[]> attributes,int vertexAttributeCnt,int instanceAttributeCnt,int vertexStride,int instanceStride)28 GrGLProgram::GrGLProgram(
29 GrGLGpu* gpu,
30 const GrGLSLBuiltinUniformHandles& builtinUniforms,
31 GrGLuint programID,
32 const UniformInfoArray& uniforms,
33 const UniformInfoArray& textureSamplers,
34 const VaryingInfoArray& pathProcVaryings,
35 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
36 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
37 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
38 int fragmentProcessorCnt,
39 std::unique_ptr<Attribute[]> attributes,
40 int vertexAttributeCnt,
41 int instanceAttributeCnt,
42 int vertexStride,
43 int instanceStride)
44 : fBuiltinUniformHandles(builtinUniforms)
45 , fProgramID(programID)
46 , fPrimitiveProcessor(std::move(geometryProcessor))
47 , fXferProcessor(std::move(xferProcessor))
48 , fFragmentProcessors(std::move(fragmentProcessors))
49 , fFragmentProcessorCnt(fragmentProcessorCnt)
50 , fAttributes(std::move(attributes))
51 , fVertexAttributeCnt(vertexAttributeCnt)
52 , fInstanceAttributeCnt(instanceAttributeCnt)
53 , fVertexStride(vertexStride)
54 , fInstanceStride(instanceStride)
55 , fGpu(gpu)
56 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings)
57 , fNumTextureSamplers(textureSamplers.count()) {
58 // Assign texture units to sampler uniforms one time up front.
59 GL_CALL(UseProgram(fProgramID));
60 fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
61 }
62
~GrGLProgram()63 GrGLProgram::~GrGLProgram() {
64 if (fProgramID) {
65 GL_CALL(DeleteProgram(fProgramID));
66 }
67 }
68
abandon()69 void GrGLProgram::abandon() {
70 fProgramID = 0;
71 }
72
73 ///////////////////////////////////////////////////////////////////////////////
74
updateUniformsAndTextureBindings(const GrRenderTarget * renderTarget,GrSurfaceOrigin origin,const GrPrimitiveProcessor & primProc,const GrPipeline & pipeline,const GrTextureProxy * const primProcTextures[])75 void GrGLProgram::updateUniformsAndTextureBindings(const GrRenderTarget* renderTarget,
76 GrSurfaceOrigin origin,
77 const GrPrimitiveProcessor& primProc,
78 const GrPipeline& pipeline,
79 const GrTextureProxy* const primProcTextures[]) {
80 this->setRenderTargetState(renderTarget, origin, primProc);
81
82 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
83 // of GLProgram determine how to set coord transforms
84
85 // We must bind to texture units in the same order in which we set the uniforms in
86 // GrGLProgramDataManager. That is, we bind textures for processors in this order:
87 // primProc, fragProcs, XP.
88 fPrimitiveProcessor->setData(fProgramDataManager, primProc,
89 GrFragmentProcessor::CoordTransformIter(pipeline));
90 if (primProcTextures) {
91 this->updatePrimitiveProcessorTextureBindings(primProc, primProcTextures);
92 }
93 int nextTexSamplerIdx = primProc.numTextureSamplers();
94
95 this->setFragmentData(pipeline, &nextTexSamplerIdx);
96
97 const GrXferProcessor& xp = pipeline.getXferProcessor();
98 SkIPoint offset;
99 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
100
101 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
102 if (dstTexture) {
103 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::ClampNearest(),
104 pipeline.dstTextureProxy()->textureSwizzle(),
105 static_cast<GrGLTexture*>(dstTexture));
106 }
107 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
108 }
109
updatePrimitiveProcessorTextureBindings(const GrPrimitiveProcessor & primProc,const GrTextureProxy * const proxies[])110 void GrGLProgram::updatePrimitiveProcessorTextureBindings(const GrPrimitiveProcessor& primProc,
111 const GrTextureProxy* const proxies[]) {
112 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
113 auto* tex = static_cast<GrGLTexture*>(proxies[i]->peekTexture());
114 fGpu->bindTexture(i, primProc.textureSampler(i).samplerState(),
115 primProc.textureSampler(i).swizzle(), tex);
116 }
117 }
118
setFragmentData(const GrPipeline & pipeline,int * nextTexSamplerIdx)119 void GrGLProgram::setFragmentData(const GrPipeline& pipeline, int* nextTexSamplerIdx) {
120 GrFragmentProcessor::Iter iter(pipeline);
121 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
122 const GrFragmentProcessor* fp = iter.next();
123 GrGLSLFragmentProcessor* glslFP = glslIter.next();
124 while (fp && glslFP) {
125 glslFP->setData(fProgramDataManager, *fp);
126 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
127 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
128 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(), sampler.swizzle(),
129 static_cast<GrGLTexture*>(sampler.peekTexture()));
130 }
131 fp = iter.next();
132 glslFP = glslIter.next();
133 }
134 SkASSERT(!fp && !glslFP);
135 }
136
setRenderTargetState(const GrRenderTarget * rt,GrSurfaceOrigin origin,const GrPrimitiveProcessor & primProc)137 void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin,
138 const GrPrimitiveProcessor& primProc) {
139 // Load the RT size uniforms if they are needed
140 if (fBuiltinUniformHandles.fRTWidthUni.isValid() &&
141 fRenderTargetState.fRenderTargetSize.fWidth != rt->width()) {
142 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTWidthUni, SkIntToScalar(rt->width()));
143 }
144 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
145 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
146 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
147 }
148
149 // set RT adjustment
150 SkISize size;
151 size.set(rt->width(), rt->height());
152 if (!primProc.isPathRendering()) {
153 if (fRenderTargetState.fRenderTargetOrigin != origin ||
154 fRenderTargetState.fRenderTargetSize != size) {
155 fRenderTargetState.fRenderTargetSize = size;
156 fRenderTargetState.fRenderTargetOrigin = origin;
157
158 float rtAdjustmentVec[4];
159 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
160 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
161 }
162 } else {
163 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
164 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
165 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
166 size, origin);
167 }
168 }
169