blob: 4864343c4b6ab9a1eff337af24e60b02d2d38e66 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
joshualitt23e280d2014-09-18 12:26:38 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Robert Phillips901aff02019-10-08 12:32:56 -040013#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrTexturePriv.h"
15#include "src/gpu/GrXferProcessor.h"
16#include "src/gpu/gl/GrGLBuffer.h"
17#include "src/gpu/gl/GrGLGpu.h"
18#include "src/gpu/gl/GrGLPathRendering.h"
19#include "src/gpu/gl/GrGLProgram.h"
20#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
21#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
22#include "src/gpu/glsl/GrGLSLXferProcessor.h"
Scroggo97c88c22011-05-11 14:05:25 +000023
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000024#define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X)
25#define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X)
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000026
joshualitt47bb3822014-10-07 16:43:25 -070027///////////////////////////////////////////////////////////////////////////////////////////////////
28
Brian Salomon1471df92018-06-08 10:49:00 -040029GrGLProgram::GrGLProgram(
30 GrGLGpu* gpu,
31 const GrGLSLBuiltinUniformHandles& builtinUniforms,
Brian Salomon4d3f5172018-06-07 14:42:52 -040032 GrGLuint programID,
33 const UniformInfoArray& uniforms,
34 const UniformInfoArray& textureSamplers,
Brian Salomon4d3f5172018-06-07 14:42:52 -040035 const VaryingInfoArray& pathProcVaryings,
36 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
37 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
38 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
Brian Salomon802cb312018-06-08 18:05:20 -040039 int fragmentProcessorCnt,
40 std::unique_ptr<Attribute[]> attributes,
Brian Salomon92be2f72018-06-19 14:33:47 -040041 int vertexAttributeCnt,
42 int instanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -040043 int vertexStride,
44 int instanceStride)
Brian Salomon4d3f5172018-06-07 14:42:52 -040045 : fBuiltinUniformHandles(builtinUniforms)
46 , fProgramID(programID)
Brian Salomon802cb312018-06-08 18:05:20 -040047 , fPrimitiveProcessor(std::move(geometryProcessor))
Brian Salomon4d3f5172018-06-07 14:42:52 -040048 , fXferProcessor(std::move(xferProcessor))
49 , fFragmentProcessors(std::move(fragmentProcessors))
50 , fFragmentProcessorCnt(fragmentProcessorCnt)
Brian Salomon802cb312018-06-08 18:05:20 -040051 , fAttributes(std::move(attributes))
Brian Salomon92be2f72018-06-19 14:33:47 -040052 , fVertexAttributeCnt(vertexAttributeCnt)
53 , fInstanceAttributeCnt(instanceAttributeCnt)
Brian Salomon802cb312018-06-08 18:05:20 -040054 , fVertexStride(vertexStride)
55 , fInstanceStride(instanceStride)
Brian Salomon4d3f5172018-06-07 14:42:52 -040056 , fGpu(gpu)
57 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings)
Brian Salomon662ea4b2018-07-12 14:53:49 -040058 , fNumTextureSamplers(textureSamplers.count()) {
cdalton42717652015-06-18 11:54:30 -070059 // Assign texture units to sampler uniforms one time up front.
60 GL_CALL(UseProgram(fProgramID));
Greg Danielbc5d4d72017-05-05 10:28:42 -040061 fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
junov@google.comf93e7172011-03-31 21:26:24 +000062}
63
64GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070065 if (fProgramID) {
66 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000067 }
junov@google.comf93e7172011-03-31 21:26:24 +000068}
69
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000070void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070071 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000072}
73
bsalomon@google.comeb715c82012-07-11 15:03:31 +000074///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000075
Chris Dalton3ffa7af2020-02-20 16:31:47 -070076void GrGLProgram::updateUniforms(const GrRenderTarget* renderTarget,
77 const GrProgramInfo& programInfo) {
Robert Phillips901aff02019-10-08 12:32:56 -040078 this->setRenderTargetState(renderTarget, programInfo.origin(), programInfo.primProc());
bsalomon@google.com91207482013-02-12 21:45:24 +000079
Chris Dalton3ffa7af2020-02-20 16:31:47 -070080 // we set the uniforms for installed processors in a generic way, but subclasses of GLProgram
81 // determine how to set coord transforms
Greg Danielbc5d4d72017-05-05 10:28:42 -040082
83 // We must bind to texture units in the same order in which we set the uniforms in
Brian Salomon662ea4b2018-07-12 14:53:49 -040084 // GrGLProgramDataManager. That is, we bind textures for processors in this order:
85 // primProc, fragProcs, XP.
Brian Salomonc241b582019-11-27 08:57:17 -050086 GrFragmentProcessor::PipelineCoordTransformRange range(programInfo.pipeline());
87 fPrimitiveProcessor->setData(fProgramDataManager, programInfo.primProc(), range);
cdalton42717652015-06-18 11:54:30 -070088
Chris Dalton3ffa7af2020-02-20 16:31:47 -070089 GrFragmentProcessor::CIter fpIter(programInfo.pipeline());
90 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
91 for (; fpIter && glslIter; ++fpIter, ++glslIter) {
92 glslIter->setData(fProgramDataManager, *fpIter);
93 }
94 SkASSERT(!fpIter && !glslIter);
joshualitt9b989322014-12-15 14:16:27 -080095
Robert Phillips901aff02019-10-08 12:32:56 -040096 const GrXferProcessor& xp = programInfo.pipeline().getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040097 SkIPoint offset;
Robert Phillips901aff02019-10-08 12:32:56 -040098 GrTexture* dstTexture = programInfo.pipeline().peekDstTexture(&offset);
Robert Phillipsbb581ce2017-05-29 15:05:15 -040099
Brian Salomon18dfa982017-04-03 16:57:43 -0400100 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
joshualitt47bb3822014-10-07 16:43:25 -0700101}
102
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700103void GrGLProgram::bindTextures(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline,
104 const GrSurfaceProxy* const primProcTextureOverrides[]) {
Brian Salomonf7232642018-09-19 08:58:08 -0400105 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700106 SkASSERT(primProcTextureOverrides[i]->asTextureProxy());
107 auto* overrideTexture = static_cast<GrGLTexture*>(
108 primProcTextureOverrides[i]->peekTexture());
Greg Daniel2c3398d2019-06-19 11:58:01 -0400109 fGpu->bindTexture(i, primProc.textureSampler(i).samplerState(),
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700110 primProc.textureSampler(i).swizzle(), overrideTexture);
Brian Salomonf7232642018-09-19 08:58:08 -0400111 }
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700112 int nextTexSamplerIdx = primProc.numTextureSamplers();
Brian Salomonf7232642018-09-19 08:58:08 -0400113
Brian Salomon7eabfe82019-12-02 14:20:20 -0500114 GrFragmentProcessor::CIter fpIter(pipeline);
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700115 for (; fpIter; ++fpIter) {
Brian Salomonc241b582019-11-27 08:57:17 -0500116 for (int i = 0; i < fpIter->numTextureSamplers(); ++i) {
117 const GrFragmentProcessor::TextureSampler& sampler = fpIter->textureSampler(i);
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700118 fGpu->bindTexture(nextTexSamplerIdx++, sampler.samplerState(), sampler.view().swizzle(),
Brian Salomone782f842018-07-31 13:53:11 -0400119 static_cast<GrGLTexture*>(sampler.peekTexture()));
120 }
joshualitta5305a12014-10-10 17:47:00 -0700121 }
Chris Dalton3ffa7af2020-02-20 16:31:47 -0700122
123 SkIPoint offset;
124 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
125 if (dstTexture) {
126 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::Filter::kNearest,
127 pipeline.dstProxyView().swizzle(), static_cast<GrGLTexture*>(dstTexture));
128 }
129 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
joshualitta5305a12014-10-10 17:47:00 -0700130}
bsalomona624bf32016-09-20 09:12:47 -0700131
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500132void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin,
133 const GrPrimitiveProcessor& primProc) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400134 // Load the RT size uniforms if they are needed
135 if (fBuiltinUniformHandles.fRTWidthUni.isValid() &&
136 fRenderTargetState.fRenderTargetSize.fWidth != rt->width()) {
137 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTWidthUni, SkIntToScalar(rt->width()));
138 }
Greg Daniele6ab9982018-08-22 13:56:32 +0000139 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
140 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
141 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700142 }
143
joshualittd8dd47b2015-09-11 11:45:01 -0700144 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400145 SkISize dimensions = rt->dimensions();
joshualittd8dd47b2015-09-11 11:45:01 -0700146 if (!primProc.isPathRendering()) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500147 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400148 fRenderTargetState.fRenderTargetSize != dimensions) {
149 fRenderTargetState.fRenderTargetSize = dimensions;
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500150 fRenderTargetState.fRenderTargetOrigin = origin;
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000151
egdaniel018fb622015-10-28 07:26:40 -0700152 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700153 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
154 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
155 }
156 } else {
157 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
158 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400159 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(), dimensions, origin);
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000160 }
161}