blob: aa67bed65c6bcd28b362d9fe7a24f4293499cda9 [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,
Brian Salomonf9f45122016-11-29 11:59:17 -050035 const UniformInfoArray& imageStorages,
egdaniel0eafe792015-11-20 14:01:22 -080036 const VaryingInfoArray& pathProcVaryings,
egdanielfa896322016-01-13 12:19:30 -080037 GrGLSLPrimitiveProcessor* geometryProcessor,
38 GrGLSLXferProcessor* xferProcessor,
egdaniel09aa1fc2016-04-20 07:09:46 -070039 const GrGLSLFragProcs& fragmentProcessors)
egdanielcdf79db2015-10-19 07:23:02 -070040 : fBuiltinUniformHandles(builtinUniforms)
joshualitt47bb3822014-10-07 16:43:25 -070041 , fProgramID(programID)
joshualitta5305a12014-10-10 17:47:00 -070042 , fGeometryProcessor(geometryProcessor)
egdanielc2304142014-12-11 13:15:13 -080043 , fXferProcessor(xferProcessor)
egdanielfa896322016-01-13 12:19:30 -080044 , fFragmentProcessors(fragmentProcessors)
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +000045 , fDesc(desc)
46 , fGpu(gpu)
Greg Danielbc5d4d72017-05-05 10:28:42 -040047 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings)
48 , fNumTextureSamplers(textureSamplers.count())
49 , fNumTexelBuffers(texelBuffers.count()) {
cdalton42717652015-06-18 11:54:30 -070050 // Assign texture units to sampler uniforms one time up front.
51 GL_CALL(UseProgram(fProgramID));
Greg Danielbc5d4d72017-05-05 10:28:42 -040052 fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
53 fProgramDataManager.setSamplerUniforms(texelBuffers, fNumTextureSamplers);
Brian Salomonf9f45122016-11-29 11:59:17 -050054 fProgramDataManager.setImageStorages(imageStorages);
junov@google.comf93e7172011-03-31 21:26:24 +000055}
56
57GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070058 if (fProgramID) {
59 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000060 }
egdanielfa896322016-01-13 12:19:30 -080061 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
62 delete fFragmentProcessors[i];
63 }
junov@google.comf93e7172011-03-31 21:26:24 +000064}
65
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000066void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070067 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000068}
69
bsalomon@google.comeb715c82012-07-11 15:03:31 +000070///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000071
cdalton74b8d322016-04-11 14:47:28 -070072void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) {
Robert Phillips7294b852017-08-01 13:51:44 +000073 this->setRenderTargetState(primProc, pipeline.renderTarget());
bsalomon@google.com91207482013-02-12 21:45:24 +000074
joshualitt47bb3822014-10-07 16:43:25 -070075 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
76 // of GLProgram determine how to set coord transforms
Greg Danielbc5d4d72017-05-05 10:28:42 -040077
78 // We must bind to texture units in the same order in which we set the uniforms in
79 // GrGLProgramDataManager. That is first all texture samplers and then texel buffers.
80 // ImageStorages are bound to their own image units so they are tracked separately.
81 // Within each group we will bind them in primProc, fragProcs, XP order.
82 int nextTexSamplerIdx = 0;
83 int nextTexelBufferIdx = fNumTextureSamplers;
84 int nextImageStorageIdx = 0;
bsalomona624bf32016-09-20 09:12:47 -070085 fGeometryProcessor->setData(fProgramDataManager, primProc,
86 GrFragmentProcessor::CoordTransformIter(pipeline));
Greg Danielbc5d4d72017-05-05 10:28:42 -040087 this->bindTextures(primProc, pipeline.getAllowSRGBInputs(), &nextTexSamplerIdx,
88 &nextTexelBufferIdx, &nextImageStorageIdx);
cdalton42717652015-06-18 11:54:30 -070089
Greg Danielbc5d4d72017-05-05 10:28:42 -040090 this->setFragmentData(primProc, pipeline, &nextTexSamplerIdx, &nextTexelBufferIdx,
91 &nextImageStorageIdx);
joshualitt9b989322014-12-15 14:16:27 -080092
Brian Salomon42c456f2017-03-06 11:29:48 -050093 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040094 SkIPoint offset;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040095 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
96
Brian Salomon18dfa982017-04-03 16:57:43 -040097 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
98 if (dstTexture) {
Greg Danielbc5d4d72017-05-05 10:28:42 -040099 fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerParams::ClampNoFilter(), true,
Robert Phillips7294b852017-08-01 13:51:44 +0000100 static_cast<GrGLTexture*>(dstTexture));
Brian Salomon18dfa982017-04-03 16:57:43 -0400101 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400102 SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
103 SkASSERT(nextTexelBufferIdx == fNumTextureSamplers + fNumTexelBuffers);
joshualitt47bb3822014-10-07 16:43:25 -0700104}
105
brianosman33f6b3f2016-06-02 05:49:21 -0700106void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
107 const GrPipeline& pipeline) {
108 this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs());
109
bsalomonb58a2b42016-09-26 06:55:02 -0700110 GrFragmentProcessor::Iter iter(pipeline);
111 while (const GrFragmentProcessor* fp = iter.next()) {
112 this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs());
brianosman33f6b3f2016-06-02 05:49:21 -0700113 }
brianosman33f6b3f2016-06-02 05:49:21 -0700114}
115
joshualitt873ad0e2015-01-20 09:08:51 -0800116void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -0700117 const GrPipeline& pipeline,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400118 int* nextTexSamplerIdx,
119 int* nextTexelBufferIdx,
120 int* nextImageStorageIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700121 GrFragmentProcessor::Iter iter(pipeline);
122 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
123 fFragmentProcessors.count());
124 const GrFragmentProcessor* fp = iter.next();
125 GrGLSLFragmentProcessor* glslFP = glslIter.next();
126 while (fp && glslFP) {
127 glslFP->setData(fProgramDataManager, *fp);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400128 this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextTexSamplerIdx,
129 nextTexelBufferIdx, nextImageStorageIdx);
mtklein85552e42016-09-26 08:39:43 -0700130 fp = iter.next();
131 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700132 }
bsalomonb58a2b42016-09-26 06:55:02 -0700133 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700134}
bsalomona624bf32016-09-20 09:12:47 -0700135
bsalomon@google.com91207482013-02-12 21:45:24 +0000136
joshualitt873ad0e2015-01-20 09:08:51 -0800137void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
Robert Phillips7294b852017-08-01 13:51:44 +0000138 const GrRenderTarget* rt) {
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 Phillips7294b852017-08-01 13:51:44 +0000149 if (fRenderTargetState.fRenderTargetOrigin != rt->origin() ||
joshualittd8dd47b2015-09-11 11:45:01 -0700150 fRenderTargetState.fRenderTargetSize != size) {
151 fRenderTargetState.fRenderTargetSize = size;
Robert Phillips7294b852017-08-01 13:51:44 +0000152 fRenderTargetState.fRenderTargetOrigin = rt->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 Phillips7294b852017-08-01 13:51:44 +0000162 size, rt->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,
cdalton74b8d322016-04-11 14:47:28 -0700167 bool allowSRGBInputs,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400168 int* nextTexSamplerIdx,
169 int* nextTexelBufferIdx,
170 int* nextImageStorageIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500171 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400172 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400173 fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.params(),
Robert Phillips7294b852017-08-01 13:51:44 +0000174 allowSRGBInputs, static_cast<GrGLTexture*>(sampler.peekTexture()));
cdalton74b8d322016-04-11 14:47:28 -0700175 }
176 for (int i = 0; i < processor.numBuffers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400177 const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(i);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400178 fGpu->bindTexelBuffer((*nextTexelBufferIdx)++, access.texelConfig(),
cdalton74b8d322016-04-11 14:47:28 -0700179 static_cast<GrGLBuffer*>(access.buffer()));
180 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500181 for (int i = 0; i < processor.numImageStorages(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400182 const GrResourceIOProcessor::ImageStorageAccess& access = processor.imageStorageAccess(i);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400183 fGpu->bindImageStorage((*nextImageStorageIdx)++, access.ioType(),
Robert Phillips9bee2e52017-05-29 12:37:20 -0400184 static_cast<GrGLTexture *>(access.peekTexture()));
Brian Salomonf9f45122016-11-29 11:59:17 -0500185 }
cdalton74b8d322016-04-11 14:47:28 -0700186}
brianosman33f6b3f2016-06-02 05:49:21 -0700187
Brian Salomonab015ef2017-04-04 10:15:51 -0400188void GrGLProgram::generateMipmaps(const GrResourceIOProcessor& processor, bool allowSRGBInputs) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500189 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400190 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500191 fGpu->generateMipmaps(sampler.params(), allowSRGBInputs,
Robert Phillips7294b852017-08-01 13:51:44 +0000192 static_cast<GrGLTexture*>(sampler.peekTexture()));
brianosman33f6b3f2016-06-02 05:49:21 -0700193 }
194}