blob: 29fe35c111991cd9a27d92d7a67d0e6fb89c3ea9 [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,
Brian Salomon101b8442016-11-18 11:58:54 -050033 const UniformInfoArray& samplers,
Brian Salomonf9f45122016-11-29 11:59:17 -050034 const UniformInfoArray& imageStorages,
egdaniel0eafe792015-11-20 14:01:22 -080035 const VaryingInfoArray& pathProcVaryings,
egdanielfa896322016-01-13 12:19:30 -080036 GrGLSLPrimitiveProcessor* geometryProcessor,
37 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)
joshualitta5305a12014-10-10 17:47:00 -070041 , fGeometryProcessor(geometryProcessor)
egdanielc2304142014-12-11 13:15:13 -080042 , fXferProcessor(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)
egdaniel0eafe792015-11-20 14:01:22 -080046 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) {
cdalton42717652015-06-18 11:54:30 -070047 // Assign texture units to sampler uniforms one time up front.
48 GL_CALL(UseProgram(fProgramID));
egdaniel09aa1fc2016-04-20 07:09:46 -070049 fProgramDataManager.setSamplers(samplers);
Brian Salomonf9f45122016-11-29 11:59:17 -050050 fProgramDataManager.setImageStorages(imageStorages);
junov@google.comf93e7172011-03-31 21:26:24 +000051}
52
53GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070054 if (fProgramID) {
55 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000056 }
egdanielfa896322016-01-13 12:19:30 -080057 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
58 delete fFragmentProcessors[i];
59 }
junov@google.comf93e7172011-03-31 21:26:24 +000060}
61
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000062void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070063 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000064}
65
bsalomon@google.comeb715c82012-07-11 15:03:31 +000066///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000067
cdalton74b8d322016-04-11 14:47:28 -070068void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) {
Robert Phillips02bb6df2017-03-28 17:11:19 -040069 this->setRenderTargetState(primProc, pipeline.getRenderTarget());
bsalomon@google.com91207482013-02-12 21:45:24 +000070
joshualitt47bb3822014-10-07 16:43:25 -070071 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
72 // of GLProgram determine how to set coord transforms
cdalton74b8d322016-04-11 14:47:28 -070073 int nextSamplerIdx = 0;
bsalomona624bf32016-09-20 09:12:47 -070074 fGeometryProcessor->setData(fProgramDataManager, primProc,
75 GrFragmentProcessor::CoordTransformIter(pipeline));
cdalton74b8d322016-04-11 14:47:28 -070076 this->bindTextures(primProc, pipeline.getAllowSRGBInputs(), &nextSamplerIdx);
cdalton42717652015-06-18 11:54:30 -070077
cdalton74b8d322016-04-11 14:47:28 -070078 this->setFragmentData(primProc, pipeline, &nextSamplerIdx);
joshualitt9b989322014-12-15 14:16:27 -080079
Brian Salomon42c456f2017-03-06 11:29:48 -050080 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -040081 SkIPoint offset;
82 GrTexture* dstTexture = pipeline.dstTexture(&offset);
83 fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
84 if (dstTexture) {
85 fGpu->bindTexture(nextSamplerIdx++, GrSamplerParams::ClampNoFilter(), true,
86 static_cast<GrGLTexture*>(dstTexture));
87 }
joshualitt47bb3822014-10-07 16:43:25 -070088}
89
brianosman33f6b3f2016-06-02 05:49:21 -070090void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
91 const GrPipeline& pipeline) {
92 this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs());
93
bsalomonb58a2b42016-09-26 06:55:02 -070094 GrFragmentProcessor::Iter iter(pipeline);
95 while (const GrFragmentProcessor* fp = iter.next()) {
96 this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs());
brianosman33f6b3f2016-06-02 05:49:21 -070097 }
brianosman33f6b3f2016-06-02 05:49:21 -070098}
99
joshualitt873ad0e2015-01-20 09:08:51 -0800100void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -0700101 const GrPipeline& pipeline,
cdalton74b8d322016-04-11 14:47:28 -0700102 int* nextSamplerIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700103 GrFragmentProcessor::Iter iter(pipeline);
104 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
105 fFragmentProcessors.count());
106 const GrFragmentProcessor* fp = iter.next();
107 GrGLSLFragmentProcessor* glslFP = glslIter.next();
108 while (fp && glslFP) {
109 glslFP->setData(fProgramDataManager, *fp);
110 this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextSamplerIdx);
mtklein85552e42016-09-26 08:39:43 -0700111 fp = iter.next();
112 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700113 }
bsalomonb58a2b42016-09-26 06:55:02 -0700114 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700115}
bsalomona624bf32016-09-20 09:12:47 -0700116
bsalomon@google.com91207482013-02-12 21:45:24 +0000117
joshualitt873ad0e2015-01-20 09:08:51 -0800118void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
Robert Phillips02bb6df2017-03-28 17:11:19 -0400119 const GrRenderTarget* rt) {
joshualitt47bb3822014-10-07 16:43:25 -0700120 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
121 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
Robert Phillips02bb6df2017-03-28 17:11:19 -0400122 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
123 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700124 }
125
joshualittd8dd47b2015-09-11 11:45:01 -0700126 // set RT adjustment
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000127 SkISize size;
128 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700129 if (!primProc.isPathRendering()) {
130 if (fRenderTargetState.fRenderTargetOrigin != rt->origin() ||
131 fRenderTargetState.fRenderTargetSize != size) {
132 fRenderTargetState.fRenderTargetSize = size;
133 fRenderTargetState.fRenderTargetOrigin = rt->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000134
egdaniel018fb622015-10-28 07:26:40 -0700135 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700136 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
137 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
138 }
139 } else {
140 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
141 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
142 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
143 size, rt->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000144 }
145}
cdalton74b8d322016-04-11 14:47:28 -0700146
147void GrGLProgram::bindTextures(const GrProcessor& processor,
148 bool allowSRGBInputs,
149 int* nextSamplerIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500150 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
151 const GrProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500152 fGpu->bindTexture((*nextSamplerIdx)++, sampler.params(),
153 allowSRGBInputs, static_cast<GrGLTexture*>(sampler.texture()));
cdalton74b8d322016-04-11 14:47:28 -0700154 }
155 for (int i = 0; i < processor.numBuffers(); ++i) {
Brian Salomonb014cca2016-11-18 11:39:15 -0500156 const GrProcessor::BufferAccess& access = processor.bufferAccess(i);
csmartdalton1897cfd2016-06-03 08:50:54 -0700157 fGpu->bindTexelBuffer((*nextSamplerIdx)++, access.texelConfig(),
cdalton74b8d322016-04-11 14:47:28 -0700158 static_cast<GrGLBuffer*>(access.buffer()));
159 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500160 for (int i = 0; i < processor.numImageStorages(); ++i) {
161 const GrProcessor::ImageStorageAccess& access = processor.imageStorageAccess(i);
162 fGpu->bindImageStorage((*nextSamplerIdx)++, access.ioType(),
163 static_cast<GrGLTexture *>(access.texture()));
164 }
cdalton74b8d322016-04-11 14:47:28 -0700165}
brianosman33f6b3f2016-06-02 05:49:21 -0700166
167void GrGLProgram::generateMipmaps(const GrProcessor& processor,
168 bool allowSRGBInputs) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500169 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
170 const GrProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500171 fGpu->generateMipmaps(sampler.params(), allowSRGBInputs,
172 static_cast<GrGLTexture*>(sampler.texture()));
brianosman33f6b3f2016-06-02 05:49:21 -0700173 }
174}