blob: 23442c55186ebc7f06c3c8d5ab67b4c6b535401f [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
Robert Phillipsd0fe8752019-01-31 14:13:59 -050076void GrGLProgram::updateUniformsAndTextureBindings(const GrRenderTarget* renderTarget,
Robert Phillips901aff02019-10-08 12:32:56 -040077 const GrProgramInfo& programInfo) {
78
79 this->setRenderTargetState(renderTarget, programInfo.origin(), programInfo.primProc());
bsalomon@google.com91207482013-02-12 21:45:24 +000080
joshualitt47bb3822014-10-07 16:43:25 -070081 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
82 // of GLProgram determine how to set coord transforms
Greg Danielbc5d4d72017-05-05 10:28:42 -040083
84 // We must bind to texture units in the same order in which we set the uniforms in
Brian Salomon662ea4b2018-07-12 14:53:49 -040085 // GrGLProgramDataManager. That is, we bind textures for processors in this order:
86 // primProc, fragProcs, XP.
Brian Salomonc241b582019-11-27 08:57:17 -050087 GrFragmentProcessor::PipelineCoordTransformRange range(programInfo.pipeline());
88 fPrimitiveProcessor->setData(fProgramDataManager, programInfo.primProc(), range);
Robert Phillips901aff02019-10-08 12:32:56 -040089 if (programInfo.hasFixedPrimProcTextures()) {
90 this->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
91 programInfo.fixedPrimProcTextures());
Brian Salomone782f842018-07-31 13:53:11 -040092 }
Robert Phillips901aff02019-10-08 12:32:56 -040093 int nextTexSamplerIdx = programInfo.primProc().numTextureSamplers();
cdalton42717652015-06-18 11:54:30 -070094
Robert Phillips901aff02019-10-08 12:32:56 -040095 this->setFragmentData(programInfo.pipeline(), &nextTexSamplerIdx);
joshualitt9b989322014-12-15 14:16:27 -080096
Robert Phillips901aff02019-10-08 12:32:56 -040097 const GrXferProcessor& xp = programInfo.pipeline().getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040098 SkIPoint offset;
Robert Phillips901aff02019-10-08 12:32:56 -040099 GrTexture* dstTexture = programInfo.pipeline().peekDstTexture(&offset);
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400100
Brian Salomon18dfa982017-04-03 16:57:43 -0400101 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
102 if (dstTexture) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400103 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::ClampNearest(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400104 programInfo.pipeline().dstProxyView().swizzle(),
Brian Salomon930f9392018-06-20 16:25:26 -0400105 static_cast<GrGLTexture*>(dstTexture));
Brian Salomon18dfa982017-04-03 16:57:43 -0400106 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400107 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
joshualitt47bb3822014-10-07 16:43:25 -0700108}
109
Brian Salomonf7232642018-09-19 08:58:08 -0400110void GrGLProgram::updatePrimitiveProcessorTextureBindings(const GrPrimitiveProcessor& primProc,
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500111 const GrSurfaceProxy* const proxies[]) {
Brian Salomonf7232642018-09-19 08:58:08 -0400112 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500113 SkASSERT(proxies[i]->asTextureProxy());
Brian Salomonf7232642018-09-19 08:58:08 -0400114 auto* tex = static_cast<GrGLTexture*>(proxies[i]->peekTexture());
Greg Daniel2c3398d2019-06-19 11:58:01 -0400115 fGpu->bindTexture(i, primProc.textureSampler(i).samplerState(),
116 primProc.textureSampler(i).swizzle(), tex);
Brian Salomonf7232642018-09-19 08:58:08 -0400117 }
118}
119
Brian Salomone782f842018-07-31 13:53:11 -0400120void GrGLProgram::setFragmentData(const GrPipeline& pipeline, int* nextTexSamplerIdx) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500121 GrFragmentProcessor::CIter fpIter(pipeline);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400122 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
Brian Salomonc241b582019-11-27 08:57:17 -0500123 for (; fpIter && glslIter; ++fpIter, ++glslIter) {
124 glslIter->setData(fProgramDataManager, *fpIter);
125 for (int i = 0; i < fpIter->numTextureSamplers(); ++i) {
126 const GrFragmentProcessor::TextureSampler& sampler = fpIter->textureSampler(i);
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000127 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(),
128 sampler.view().swizzle(),
Brian Salomone782f842018-07-31 13:53:11 -0400129 static_cast<GrGLTexture*>(sampler.peekTexture()));
130 }
joshualitta5305a12014-10-10 17:47:00 -0700131 }
Brian Salomonc241b582019-11-27 08:57:17 -0500132 SkASSERT(!fpIter && !glslIter);
joshualitta5305a12014-10-10 17:47:00 -0700133}
bsalomona624bf32016-09-20 09:12:47 -0700134
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500135void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin,
136 const GrPrimitiveProcessor& primProc) {
Ethan Nicholascd700e92018-08-24 16:43:57 -0400137 // Load the RT size uniforms if they are needed
138 if (fBuiltinUniformHandles.fRTWidthUni.isValid() &&
139 fRenderTargetState.fRenderTargetSize.fWidth != rt->width()) {
140 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTWidthUni, SkIntToScalar(rt->width()));
141 }
Greg Daniele6ab9982018-08-22 13:56:32 +0000142 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
143 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
144 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700145 }
146
joshualittd8dd47b2015-09-11 11:45:01 -0700147 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400148 SkISize dimensions = rt->dimensions();
joshualittd8dd47b2015-09-11 11:45:01 -0700149 if (!primProc.isPathRendering()) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500150 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400151 fRenderTargetState.fRenderTargetSize != dimensions) {
152 fRenderTargetState.fRenderTargetSize = dimensions;
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500153 fRenderTargetState.fRenderTargetOrigin = origin;
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000154
egdaniel018fb622015-10-28 07:26:40 -0700155 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700156 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
157 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
158 }
159 } else {
160 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
161 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400162 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(), dimensions, origin);
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000163 }
164}