blob: 2a876d0851a0ef56287b795bba9dc4595641a96a [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,
34 const UniformInfoArray& texelBuffers,
35 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)
58 , fNumTextureSamplers(textureSamplers.count())
59 , fNumTexelBuffers(texelBuffers.count()) {
cdalton42717652015-06-18 11:54:30 -070060 // Assign texture units to sampler uniforms one time up front.
61 GL_CALL(UseProgram(fProgramID));
Greg Danielbc5d4d72017-05-05 10:28:42 -040062 fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
63 fProgramDataManager.setSamplerUniforms(texelBuffers, fNumTextureSamplers);
junov@google.comf93e7172011-03-31 21:26:24 +000064}
65
66GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070067 if (fProgramID) {
68 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000069 }
junov@google.comf93e7172011-03-31 21:26:24 +000070}
71
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000072void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070073 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000074}
75
bsalomon@google.comeb715c82012-07-11 15:03:31 +000076///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000077
cdalton74b8d322016-04-11 14:47:28 -070078void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -040079 this->setRenderTargetState(primProc, pipeline.proxy());
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
85 // GrGLProgramDataManager. That is first all texture samplers and then texel buffers.
Greg Danielbc5d4d72017-05-05 10:28:42 -040086 // Within each group we will bind them in primProc, fragProcs, XP order.
87 int nextTexSamplerIdx = 0;
88 int nextTexelBufferIdx = fNumTextureSamplers;
Brian Salomon802cb312018-06-08 18:05:20 -040089 fPrimitiveProcessor->setData(fProgramDataManager, primProc,
90 GrFragmentProcessor::CoordTransformIter(pipeline));
Brian Osman2b23c4b2018-06-01 12:25:08 -040091 this->bindTextures(primProc, &nextTexSamplerIdx, &nextTexelBufferIdx);
cdalton42717652015-06-18 11:54:30 -070092
Brian Salomon559f5562017-11-15 14:28:33 -050093 this->setFragmentData(primProc, pipeline, &nextTexSamplerIdx, &nextTexelBufferIdx);
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);
105 SkASSERT(nextTexelBufferIdx == fNumTextureSamplers + fNumTexelBuffers);
joshualitt47bb3822014-10-07 16:43:25 -0700106}
107
brianosman33f6b3f2016-06-02 05:49:21 -0700108void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
109 const GrPipeline& pipeline) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400110 this->generateMipmaps(primProc);
brianosman33f6b3f2016-06-02 05:49:21 -0700111
bsalomonb58a2b42016-09-26 06:55:02 -0700112 GrFragmentProcessor::Iter iter(pipeline);
113 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400114 this->generateMipmaps(*fp);
brianosman33f6b3f2016-06-02 05:49:21 -0700115 }
brianosman33f6b3f2016-06-02 05:49:21 -0700116}
117
joshualitt873ad0e2015-01-20 09:08:51 -0800118void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -0700119 const GrPipeline& pipeline,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400120 int* nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500121 int* nextTexelBufferIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700122 GrFragmentProcessor::Iter iter(pipeline);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400123 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
bsalomonb58a2b42016-09-26 06:55:02 -0700124 const GrFragmentProcessor* fp = iter.next();
125 GrGLSLFragmentProcessor* glslFP = glslIter.next();
126 while (fp && glslFP) {
127 glslFP->setData(fProgramDataManager, *fp);
Brian Osman2b23c4b2018-06-01 12:25:08 -0400128 this->bindTextures(*fp, nextTexSamplerIdx, nextTexelBufferIdx);
mtklein85552e42016-09-26 08:39:43 -0700129 fp = iter.next();
130 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700131 }
bsalomonb58a2b42016-09-26 06:55:02 -0700132 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700133}
bsalomona624bf32016-09-20 09:12:47 -0700134
bsalomon@google.com91207482013-02-12 21:45:24 +0000135
joshualitt873ad0e2015-01-20 09:08:51 -0800136void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400137 const GrRenderTargetProxy* proxy) {
138 GrRenderTarget* rt = proxy->priv().peekRenderTarget();
joshualitt47bb3822014-10-07 16:43:25 -0700139 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
140 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
Robert Phillips02bb6df2017-03-28 17:11:19 -0400141 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
142 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700143 }
144
joshualittd8dd47b2015-09-11 11:45:01 -0700145 // set RT adjustment
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000146 SkISize size;
147 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700148 if (!primProc.isPathRendering()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400149 if (fRenderTargetState.fRenderTargetOrigin != proxy->origin() ||
joshualittd8dd47b2015-09-11 11:45:01 -0700150 fRenderTargetState.fRenderTargetSize != size) {
151 fRenderTargetState.fRenderTargetSize = size;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400152 fRenderTargetState.fRenderTargetOrigin = proxy->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000153
egdaniel018fb622015-10-28 07:26:40 -0700154 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700155 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
156 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
157 }
158 } else {
159 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
160 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
161 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400162 size, proxy->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000163 }
164}
cdalton74b8d322016-04-11 14:47:28 -0700165
Brian Salomonab015ef2017-04-04 10:15:51 -0400166void GrGLProgram::bindTextures(const GrResourceIOProcessor& processor,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400167 int* nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500168 int* nextTexelBufferIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500169 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400170 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Osman2b23c4b2018-06-01 12:25:08 -0400171 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(),
Brian Salomon930f9392018-06-20 16:25:26 -0400172 static_cast<GrGLTexture*>(sampler.peekTexture()));
cdalton74b8d322016-04-11 14:47:28 -0700173 }
174 for (int i = 0; i < processor.numBuffers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400175 const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(i);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400176 fGpu->bindTexelBuffer((*nextTexelBufferIdx)++, access.texelConfig(),
cdalton74b8d322016-04-11 14:47:28 -0700177 static_cast<GrGLBuffer*>(access.buffer()));
178 }
179}
brianosman33f6b3f2016-06-02 05:49:21 -0700180
Brian Osman2b23c4b2018-06-01 12:25:08 -0400181void GrGLProgram::generateMipmaps(const GrResourceIOProcessor& processor) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500182 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400183 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomon930f9392018-06-20 16:25:26 -0400184 auto* tex = sampler.peekTexture();
185 if (sampler.samplerState().filter() == GrSamplerState::Filter::kMipMap &&
186 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
187 tex->texturePriv().mipMapsAreDirty()) {
188 SkASSERT(fGpu->caps()->mipMapSupport());
189 fGpu->regenerateMipMapLevels(static_cast<GrGLTexture*>(sampler.peekTexture()));
190 }
brianosman33f6b3f2016-06-02 05:49:21 -0700191 }
192}