blob: 52d53bde959a6312e8f38df21c88e2fae1fa01fb [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
cdalton74b8d322016-04-11 14:47:28 -070075void 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 Salomon662ea4b2018-07-12 14:53:49 -040087 this->bindTextures(primProc, &nextTexSamplerIdx);
cdalton42717652015-06-18 11:54:30 -070088
Brian Salomon662ea4b2018-07-12 14:53:49 -040089 this->setFragmentData(primProc, pipeline, &nextTexSamplerIdx);
joshualitt9b989322014-12-15 14:16:27 -080090
Brian Salomon42c456f2017-03-06 11:29:48 -050091 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040092 SkIPoint offset;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040093 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
94
Brian Salomon18dfa982017-04-03 16:57:43 -040095 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
96 if (dstTexture) {
Brian Osman2b23c4b2018-06-01 12:25:08 -040097 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::ClampNearest(),
Brian Salomon930f9392018-06-20 16:25:26 -040098 static_cast<GrGLTexture*>(dstTexture));
Brian Salomon18dfa982017-04-03 16:57:43 -040099 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400100 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
joshualitt47bb3822014-10-07 16:43:25 -0700101}
102
brianosman33f6b3f2016-06-02 05:49:21 -0700103void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
104 const GrPipeline& pipeline) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400105 this->generateMipmaps(primProc);
brianosman33f6b3f2016-06-02 05:49:21 -0700106
bsalomonb58a2b42016-09-26 06:55:02 -0700107 GrFragmentProcessor::Iter iter(pipeline);
108 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400109 this->generateMipmaps(*fp);
brianosman33f6b3f2016-06-02 05:49:21 -0700110 }
brianosman33f6b3f2016-06-02 05:49:21 -0700111}
112
joshualitt873ad0e2015-01-20 09:08:51 -0800113void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -0700114 const GrPipeline& pipeline,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400115 int* nextTexSamplerIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700116 GrFragmentProcessor::Iter iter(pipeline);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400117 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
bsalomonb58a2b42016-09-26 06:55:02 -0700118 const GrFragmentProcessor* fp = iter.next();
119 GrGLSLFragmentProcessor* glslFP = glslIter.next();
120 while (fp && glslFP) {
121 glslFP->setData(fProgramDataManager, *fp);
Brian Salomon662ea4b2018-07-12 14:53:49 -0400122 this->bindTextures(*fp, nextTexSamplerIdx);
mtklein85552e42016-09-26 08:39:43 -0700123 fp = iter.next();
124 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700125 }
bsalomonb58a2b42016-09-26 06:55:02 -0700126 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700127}
bsalomona624bf32016-09-20 09:12:47 -0700128
bsalomon@google.com91207482013-02-12 21:45:24 +0000129
joshualitt873ad0e2015-01-20 09:08:51 -0800130void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400131 const GrRenderTargetProxy* proxy) {
132 GrRenderTarget* rt = proxy->priv().peekRenderTarget();
joshualitt47bb3822014-10-07 16:43:25 -0700133 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
134 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
Robert Phillips02bb6df2017-03-28 17:11:19 -0400135 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
136 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700137 }
138
joshualittd8dd47b2015-09-11 11:45:01 -0700139 // set RT adjustment
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000140 SkISize size;
141 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700142 if (!primProc.isPathRendering()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400143 if (fRenderTargetState.fRenderTargetOrigin != proxy->origin() ||
joshualittd8dd47b2015-09-11 11:45:01 -0700144 fRenderTargetState.fRenderTargetSize != size) {
145 fRenderTargetState.fRenderTargetSize = size;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400146 fRenderTargetState.fRenderTargetOrigin = proxy->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000147
egdaniel018fb622015-10-28 07:26:40 -0700148 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700149 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
150 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
151 }
152 } else {
153 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
154 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
155 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400156 size, proxy->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000157 }
158}
cdalton74b8d322016-04-11 14:47:28 -0700159
Brian Salomonab015ef2017-04-04 10:15:51 -0400160void GrGLProgram::bindTextures(const GrResourceIOProcessor& processor,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400161 int* nextTexSamplerIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500162 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400163 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Osman2b23c4b2018-06-01 12:25:08 -0400164 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(),
Brian Salomon930f9392018-06-20 16:25:26 -0400165 static_cast<GrGLTexture*>(sampler.peekTexture()));
cdalton74b8d322016-04-11 14:47:28 -0700166 }
cdalton74b8d322016-04-11 14:47:28 -0700167}
brianosman33f6b3f2016-06-02 05:49:21 -0700168
Brian Osman2b23c4b2018-06-01 12:25:08 -0400169void GrGLProgram::generateMipmaps(const GrResourceIOProcessor& processor) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500170 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400171 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomon930f9392018-06-20 16:25:26 -0400172 auto* tex = sampler.peekTexture();
173 if (sampler.samplerState().filter() == GrSamplerState::Filter::kMipMap &&
174 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
175 tex->texturePriv().mipMapsAreDirty()) {
176 SkASSERT(fGpu->caps()->mipMapSupport());
177 fGpu->regenerateMipMapLevels(static_cast<GrGLTexture*>(sampler.peekTexture()));
178 }
brianosman33f6b3f2016-06-02 05:49:21 -0700179 }
180}