blob: 7257638e1d95355448bbe770c21682ac45ccd6e7 [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"
9
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "GrAllocator.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000012#include "GrCoordTransform.h"
jvanverth39edf762014-12-22 11:44:19 -080013#include "GrGLGpu.h"
cdalton74b8d322016-04-11 14:47:28 -070014#include "GrGLBuffer.h"
kkinnunenec56e452014-08-25 22:21:16 -070015#include "GrGLPathRendering.h"
kkinnunencabe20c2015-06-01 01:37:26 -070016#include "GrPathProcessor.h"
egdaniel8dd688b2015-01-22 10:16:09 -080017#include "GrPipeline.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
bsalomon861e1032014-12-16 07:33:49 -080028GrGLProgram::GrGLProgram(GrGLGpu* gpu,
joshualitt79f8fae2014-10-28 17:59:26 -070029 const GrProgramDesc& desc,
joshualitt47bb3822014-10-07 16:43:25 -070030 const BuiltinUniformHandles& builtinUniforms,
31 GrGLuint programID,
32 const UniformInfoArray& uniforms,
Greg Danielbc5d4d72017-05-05 10:28:42 -040033 const UniformInfoArray& textureSamplers,
34 const UniformInfoArray& texelBuffers,
egdaniel0eafe792015-11-20 14:01:22 -080035 const VaryingInfoArray& pathProcVaryings,
Robert Phillips369e8b72017-08-01 16:13:04 -040036 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
37 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
egdaniel09aa1fc2016-04-20 07:09:46 -070038 const GrGLSLFragProcs& fragmentProcessors)
egdanielcdf79db2015-10-19 07:23:02 -070039 : fBuiltinUniformHandles(builtinUniforms)
joshualitt47bb3822014-10-07 16:43:25 -070040 , fProgramID(programID)
Robert Phillips369e8b72017-08-01 16:13:04 -040041 , fGeometryProcessor(std::move(geometryProcessor))
42 , fXferProcessor(std::move(xferProcessor))
egdanielfa896322016-01-13 12:19:30 -080043 , fFragmentProcessors(fragmentProcessors)
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +000044 , fDesc(desc)
45 , fGpu(gpu)
Greg Danielbc5d4d72017-05-05 10:28:42 -040046 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings)
47 , fNumTextureSamplers(textureSamplers.count())
48 , fNumTexelBuffers(texelBuffers.count()) {
cdalton42717652015-06-18 11:54:30 -070049 // Assign texture units to sampler uniforms one time up front.
50 GL_CALL(UseProgram(fProgramID));
Greg Danielbc5d4d72017-05-05 10:28:42 -040051 fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
52 fProgramDataManager.setSamplerUniforms(texelBuffers, fNumTextureSamplers);
junov@google.comf93e7172011-03-31 21:26:24 +000053}
54
55GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070056 if (fProgramID) {
57 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000058 }
egdanielfa896322016-01-13 12:19:30 -080059 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
60 delete fFragmentProcessors[i];
61 }
junov@google.comf93e7172011-03-31 21:26:24 +000062}
63
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000064void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070065 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000066}
67
bsalomon@google.comeb715c82012-07-11 15:03:31 +000068///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000069
cdalton74b8d322016-04-11 14:47:28 -070070void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -040071 this->setRenderTargetState(primProc, pipeline.proxy());
bsalomon@google.com91207482013-02-12 21:45:24 +000072
joshualitt47bb3822014-10-07 16:43:25 -070073 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
74 // of GLProgram determine how to set coord transforms
Greg Danielbc5d4d72017-05-05 10:28:42 -040075
76 // We must bind to texture units in the same order in which we set the uniforms in
77 // GrGLProgramDataManager. That is first all texture samplers and then texel buffers.
Greg Danielbc5d4d72017-05-05 10:28:42 -040078 // Within each group we will bind them in primProc, fragProcs, XP order.
79 int nextTexSamplerIdx = 0;
80 int nextTexelBufferIdx = fNumTextureSamplers;
bsalomona624bf32016-09-20 09:12:47 -070081 fGeometryProcessor->setData(fProgramDataManager, primProc,
82 GrFragmentProcessor::CoordTransformIter(pipeline));
Greg Danielbc5d4d72017-05-05 10:28:42 -040083 this->bindTextures(primProc, pipeline.getAllowSRGBInputs(), &nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -050084 &nextTexelBufferIdx);
cdalton42717652015-06-18 11:54:30 -070085
Brian Salomon559f5562017-11-15 14:28:33 -050086 this->setFragmentData(primProc, pipeline, &nextTexSamplerIdx, &nextTexelBufferIdx);
joshualitt9b989322014-12-15 14:16:27 -080087
Brian Salomon42c456f2017-03-06 11:29:48 -050088 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040089 SkIPoint offset;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040090 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
91
Brian Salomon18dfa982017-04-03 16:57:43 -040092 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
93 if (dstTexture) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040094 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::ClampNearest(), true,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040095 static_cast<GrGLTexture*>(dstTexture),
96 pipeline.dstTextureProxy()->origin());
Brian Salomon18dfa982017-04-03 16:57:43 -040097 }
Greg Danielbc5d4d72017-05-05 10:28:42 -040098 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
99 SkASSERT(nextTexelBufferIdx == fNumTextureSamplers + fNumTexelBuffers);
joshualitt47bb3822014-10-07 16:43:25 -0700100}
101
brianosman33f6b3f2016-06-02 05:49:21 -0700102void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
103 const GrPipeline& pipeline) {
104 this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs());
105
bsalomonb58a2b42016-09-26 06:55:02 -0700106 GrFragmentProcessor::Iter iter(pipeline);
107 while (const GrFragmentProcessor* fp = iter.next()) {
108 this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs());
brianosman33f6b3f2016-06-02 05:49:21 -0700109 }
brianosman33f6b3f2016-06-02 05:49:21 -0700110}
111
joshualitt873ad0e2015-01-20 09:08:51 -0800112void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -0700113 const GrPipeline& pipeline,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400114 int* nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500115 int* nextTexelBufferIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700116 GrFragmentProcessor::Iter iter(pipeline);
117 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
118 fFragmentProcessors.count());
119 const GrFragmentProcessor* fp = iter.next();
120 GrGLSLFragmentProcessor* glslFP = glslIter.next();
121 while (fp && glslFP) {
122 glslFP->setData(fProgramDataManager, *fp);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400123 this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500124 nextTexelBufferIdx);
mtklein85552e42016-09-26 08:39:43 -0700125 fp = iter.next();
126 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700127 }
bsalomonb58a2b42016-09-26 06:55:02 -0700128 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700129}
bsalomona624bf32016-09-20 09:12:47 -0700130
bsalomon@google.com91207482013-02-12 21:45:24 +0000131
joshualitt873ad0e2015-01-20 09:08:51 -0800132void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400133 const GrRenderTargetProxy* proxy) {
134 GrRenderTarget* rt = proxy->priv().peekRenderTarget();
joshualitt47bb3822014-10-07 16:43:25 -0700135 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
136 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
Robert Phillips02bb6df2017-03-28 17:11:19 -0400137 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
138 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700139 }
140
joshualittd8dd47b2015-09-11 11:45:01 -0700141 // set RT adjustment
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000142 SkISize size;
143 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700144 if (!primProc.isPathRendering()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400145 if (fRenderTargetState.fRenderTargetOrigin != proxy->origin() ||
joshualittd8dd47b2015-09-11 11:45:01 -0700146 fRenderTargetState.fRenderTargetSize != size) {
147 fRenderTargetState.fRenderTargetSize = size;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400148 fRenderTargetState.fRenderTargetOrigin = proxy->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000149
egdaniel018fb622015-10-28 07:26:40 -0700150 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700151 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
152 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
153 }
154 } else {
155 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
156 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
157 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400158 size, proxy->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000159 }
160}
cdalton74b8d322016-04-11 14:47:28 -0700161
Brian Salomonab015ef2017-04-04 10:15:51 -0400162void GrGLProgram::bindTextures(const GrResourceIOProcessor& processor,
cdalton74b8d322016-04-11 14:47:28 -0700163 bool allowSRGBInputs,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400164 int* nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500165 int* nextTexelBufferIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500166 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400167 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400168 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(), allowSRGBInputs,
169 static_cast<GrGLTexture*>(sampler.peekTexture()),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400170 sampler.proxy()->origin());
cdalton74b8d322016-04-11 14:47:28 -0700171 }
172 for (int i = 0; i < processor.numBuffers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400173 const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(i);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400174 fGpu->bindTexelBuffer((*nextTexelBufferIdx)++, access.texelConfig(),
cdalton74b8d322016-04-11 14:47:28 -0700175 static_cast<GrGLBuffer*>(access.buffer()));
176 }
177}
brianosman33f6b3f2016-06-02 05:49:21 -0700178
Brian Salomonab015ef2017-04-04 10:15:51 -0400179void GrGLProgram::generateMipmaps(const GrResourceIOProcessor& processor, bool allowSRGBInputs) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500180 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400181 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400182 fGpu->generateMipmaps(sampler.samplerState(), allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400183 static_cast<GrGLTexture*>(sampler.peekTexture()),
184 sampler.proxy()->origin());
brianosman33f6b3f2016-06-02 05:49:21 -0700185 }
186}