blob: e0eccf27757a96d9011011fa68560ba310d6b8db [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
junov@google.comf93e7172011-03-31 21:26:24 +00008#include "GrGLProgram.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00009#include "GrAllocator.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000010#include "GrCoordTransform.h"
cdalton74b8d322016-04-11 14:47:28 -070011#include "GrGLBuffer.h"
Brian Salomon930f9392018-06-20 16:25:26 -040012#include "GrGLGpu.h"
kkinnunenec56e452014-08-25 22:21:16 -070013#include "GrGLPathRendering.h"
kkinnunencabe20c2015-06-01 01:37:26 -070014#include "GrPathProcessor.h"
egdaniel8dd688b2015-01-22 10:16:09 -080015#include "GrPipeline.h"
Brian Salomon930f9392018-06-20 16:25:26 -040016#include "GrProcessor.h"
17#include "GrTexturePriv.h"
egdanielc2304142014-12-11 13:15:13 -080018#include "GrXferProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080019#include "glsl/GrGLSLFragmentProcessor.h"
egdaniele659a582015-11-13 09:55:43 -080020#include "glsl/GrGLSLGeometryProcessor.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080021#include "glsl/GrGLSLXferProcessor.h"
Scroggo97c88c22011-05-11 14:05:25 +000022
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000023#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)
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000025
joshualitt47bb3822014-10-07 16:43:25 -070026///////////////////////////////////////////////////////////////////////////////////////////////////
27
Brian Salomon1471df92018-06-08 10:49:00 -040028GrGLProgram::GrGLProgram(
29 GrGLGpu* gpu,
30 const GrGLSLBuiltinUniformHandles& builtinUniforms,
Brian Salomon4d3f5172018-06-07 14:42:52 -040031 GrGLuint programID,
32 const UniformInfoArray& uniforms,
33 const UniformInfoArray& textureSamplers,
Brian Salomon4d3f5172018-06-07 14:42:52 -040034 const VaryingInfoArray& pathProcVaryings,
35 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
36 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
37 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
Brian Salomon802cb312018-06-08 18:05:20 -040038 int fragmentProcessorCnt,
39 std::unique_ptr<Attribute[]> attributes,
Brian Salomon92be2f72018-06-19 14:33:47 -040040 int vertexAttributeCnt,
41 int instanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -040042 int vertexStride,
43 int instanceStride)
Brian Salomon4d3f5172018-06-07 14:42:52 -040044 : fBuiltinUniformHandles(builtinUniforms)
45 , fProgramID(programID)
Brian Salomon802cb312018-06-08 18:05:20 -040046 , fPrimitiveProcessor(std::move(geometryProcessor))
Brian Salomon4d3f5172018-06-07 14:42:52 -040047 , fXferProcessor(std::move(xferProcessor))
48 , fFragmentProcessors(std::move(fragmentProcessors))
49 , fFragmentProcessorCnt(fragmentProcessorCnt)
Brian Salomon802cb312018-06-08 18:05:20 -040050 , fAttributes(std::move(attributes))
Brian Salomon92be2f72018-06-19 14:33:47 -040051 , fVertexAttributeCnt(vertexAttributeCnt)
52 , fInstanceAttributeCnt(instanceAttributeCnt)
Brian Salomon802cb312018-06-08 18:05:20 -040053 , fVertexStride(vertexStride)
54 , fInstanceStride(instanceStride)
Brian Salomon4d3f5172018-06-07 14:42:52 -040055 , fGpu(gpu)
56 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings)
Brian Salomon662ea4b2018-07-12 14:53:49 -040057 , fNumTextureSamplers(textureSamplers.count()) {
cdalton42717652015-06-18 11:54:30 -070058 // Assign texture units to sampler uniforms one time up front.
59 GL_CALL(UseProgram(fProgramID));
Greg Danielbc5d4d72017-05-05 10:28:42 -040060 fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
junov@google.comf93e7172011-03-31 21:26:24 +000061}
62
63GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070064 if (fProgramID) {
65 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000066 }
junov@google.comf93e7172011-03-31 21:26:24 +000067}
68
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000069void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070070 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000071}
72
bsalomon@google.comeb715c82012-07-11 15:03:31 +000073///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000074
Brian Salomonfdf05f42018-08-06 17:51:35 -040075void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -040076 this->setRenderTargetState(primProc, pipeline.proxy());
bsalomon@google.com91207482013-02-12 21:45:24 +000077
joshualitt47bb3822014-10-07 16:43:25 -070078 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
79 // of GLProgram determine how to set coord transforms
Greg Danielbc5d4d72017-05-05 10:28:42 -040080
81 // We must bind to texture units in the same order in which we set the uniforms in
Brian Salomon662ea4b2018-07-12 14:53:49 -040082 // GrGLProgramDataManager. That is, we bind textures for processors in this order:
83 // primProc, fragProcs, XP.
Greg Danielbc5d4d72017-05-05 10:28:42 -040084 int nextTexSamplerIdx = 0;
Brian Salomon802cb312018-06-08 18:05:20 -040085 fPrimitiveProcessor->setData(fProgramDataManager, primProc,
86 GrFragmentProcessor::CoordTransformIter(pipeline));
Brian Salomone782f842018-07-31 13:53:11 -040087 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Brian Salomonfdf05f42018-08-06 17:51:35 -040088 const GrPrimitiveProcessor::TextureSampler& sampler = primProc.textureSampler(i);
89 fGpu->bindTexture(nextTexSamplerIdx++, sampler.samplerState(),
90 static_cast<GrGLTexture*>(sampler.peekTexture()));
Brian Salomone782f842018-07-31 13:53:11 -040091 }
cdalton42717652015-06-18 11:54:30 -070092
Brian Salomone782f842018-07-31 13:53:11 -040093 this->setFragmentData(pipeline, &nextTexSamplerIdx);
joshualitt9b989322014-12-15 14:16:27 -080094
Brian Salomon42c456f2017-03-06 11:29:48 -050095 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040096 SkIPoint offset;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040097 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
98
Brian Salomon18dfa982017-04-03 16:57:43 -040099 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
100 if (dstTexture) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400101 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::ClampNearest(),
Brian Salomon930f9392018-06-20 16:25:26 -0400102 static_cast<GrGLTexture*>(dstTexture));
Brian Salomon18dfa982017-04-03 16:57:43 -0400103 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400104 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
joshualitt47bb3822014-10-07 16:43:25 -0700105}
106
Brian Salomone782f842018-07-31 13:53:11 -0400107void GrGLProgram::setFragmentData(const GrPipeline& pipeline, int* nextTexSamplerIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700108 GrFragmentProcessor::Iter iter(pipeline);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400109 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
bsalomonb58a2b42016-09-26 06:55:02 -0700110 const GrFragmentProcessor* fp = iter.next();
111 GrGLSLFragmentProcessor* glslFP = glslIter.next();
112 while (fp && glslFP) {
113 glslFP->setData(fProgramDataManager, *fp);
Brian Salomone782f842018-07-31 13:53:11 -0400114 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
115 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
116 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(),
117 static_cast<GrGLTexture*>(sampler.peekTexture()));
118 }
mtklein85552e42016-09-26 08:39:43 -0700119 fp = iter.next();
120 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700121 }
bsalomonb58a2b42016-09-26 06:55:02 -0700122 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700123}
bsalomona624bf32016-09-20 09:12:47 -0700124
joshualitt873ad0e2015-01-20 09:08:51 -0800125void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400126 const GrRenderTargetProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400127 GrRenderTarget* rt = proxy->peekRenderTarget();
joshualitt47bb3822014-10-07 16:43:25 -0700128 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
129 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
Robert Phillips02bb6df2017-03-28 17:11:19 -0400130 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
131 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700132 }
133
joshualittd8dd47b2015-09-11 11:45:01 -0700134 // set RT adjustment
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000135 SkISize size;
136 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700137 if (!primProc.isPathRendering()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400138 if (fRenderTargetState.fRenderTargetOrigin != proxy->origin() ||
joshualittd8dd47b2015-09-11 11:45:01 -0700139 fRenderTargetState.fRenderTargetSize != size) {
140 fRenderTargetState.fRenderTargetSize = size;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400141 fRenderTargetState.fRenderTargetOrigin = proxy->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000142
egdaniel018fb622015-10-28 07:26:40 -0700143 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700144 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
145 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
146 }
147 } else {
148 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
149 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
150 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400151 size, proxy->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000152 }
153}