blob: d9e73f04e4e5bd9faf80b1e34c64c76912090257 [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) {
egdaniel8dd688b2015-01-22 10:16:09 -080069 this->setRenderTargetState(primProc, pipeline);
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();
81 fXferProcessor->setData(fProgramDataManager, xp);
82 this->bindTextures(xp, pipeline.getAllowSRGBInputs(), &nextSamplerIdx);
joshualitt47bb3822014-10-07 16:43:25 -070083}
84
brianosman33f6b3f2016-06-02 05:49:21 -070085void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
86 const GrPipeline& pipeline) {
87 this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs());
88
bsalomonb58a2b42016-09-26 06:55:02 -070089 GrFragmentProcessor::Iter iter(pipeline);
90 while (const GrFragmentProcessor* fp = iter.next()) {
91 this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs());
brianosman33f6b3f2016-06-02 05:49:21 -070092 }
brianosman33f6b3f2016-06-02 05:49:21 -070093}
94
joshualitt873ad0e2015-01-20 09:08:51 -080095void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -070096 const GrPipeline& pipeline,
cdalton74b8d322016-04-11 14:47:28 -070097 int* nextSamplerIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -070098 GrFragmentProcessor::Iter iter(pipeline);
99 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
100 fFragmentProcessors.count());
101 const GrFragmentProcessor* fp = iter.next();
102 GrGLSLFragmentProcessor* glslFP = glslIter.next();
103 while (fp && glslFP) {
104 glslFP->setData(fProgramDataManager, *fp);
105 this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextSamplerIdx);
mtklein85552e42016-09-26 08:39:43 -0700106 fp = iter.next();
107 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700108 }
bsalomonb58a2b42016-09-26 06:55:02 -0700109 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700110}
bsalomona624bf32016-09-20 09:12:47 -0700111
bsalomon@google.com91207482013-02-12 21:45:24 +0000112
joshualitt873ad0e2015-01-20 09:08:51 -0800113void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800114 const GrPipeline& pipeline) {
joshualitt47bb3822014-10-07 16:43:25 -0700115 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
116 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
egdaniel8dd688b2015-01-22 10:16:09 -0800117 fRenderTargetState.fRenderTargetSize.fHeight != pipeline.getRenderTarget()->height()) {
joshualitt47bb3822014-10-07 16:43:25 -0700118 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni,
egdaniel8dd688b2015-01-22 10:16:09 -0800119 SkIntToScalar(pipeline.getRenderTarget()->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700120 }
121
joshualittd8dd47b2015-09-11 11:45:01 -0700122 // set RT adjustment
egdaniel8dd688b2015-01-22 10:16:09 -0800123 const GrRenderTarget* rt = pipeline.getRenderTarget();
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000124 SkISize size;
125 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700126 if (!primProc.isPathRendering()) {
127 if (fRenderTargetState.fRenderTargetOrigin != rt->origin() ||
128 fRenderTargetState.fRenderTargetSize != size) {
129 fRenderTargetState.fRenderTargetSize = size;
130 fRenderTargetState.fRenderTargetOrigin = rt->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000131
egdaniel018fb622015-10-28 07:26:40 -0700132 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700133 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
134 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
135 }
136 } else {
137 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
138 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
139 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
140 size, rt->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000141 }
142}
cdalton74b8d322016-04-11 14:47:28 -0700143
144void GrGLProgram::bindTextures(const GrProcessor& processor,
145 bool allowSRGBInputs,
146 int* nextSamplerIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500147 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
148 const GrProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500149 fGpu->bindTexture((*nextSamplerIdx)++, sampler.params(),
150 allowSRGBInputs, static_cast<GrGLTexture*>(sampler.texture()));
cdalton74b8d322016-04-11 14:47:28 -0700151 }
152 for (int i = 0; i < processor.numBuffers(); ++i) {
Brian Salomonb014cca2016-11-18 11:39:15 -0500153 const GrProcessor::BufferAccess& access = processor.bufferAccess(i);
csmartdalton1897cfd2016-06-03 08:50:54 -0700154 fGpu->bindTexelBuffer((*nextSamplerIdx)++, access.texelConfig(),
cdalton74b8d322016-04-11 14:47:28 -0700155 static_cast<GrGLBuffer*>(access.buffer()));
156 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500157 for (int i = 0; i < processor.numImageStorages(); ++i) {
158 const GrProcessor::ImageStorageAccess& access = processor.imageStorageAccess(i);
159 fGpu->bindImageStorage((*nextSamplerIdx)++, access.ioType(),
160 static_cast<GrGLTexture *>(access.texture()));
161 }
cdalton74b8d322016-04-11 14:47:28 -0700162}
brianosman33f6b3f2016-06-02 05:49:21 -0700163
164void GrGLProgram::generateMipmaps(const GrProcessor& processor,
165 bool allowSRGBInputs) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500166 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
167 const GrProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500168 fGpu->generateMipmaps(sampler.params(), allowSRGBInputs,
169 static_cast<GrGLTexture*>(sampler.texture()));
brianosman33f6b3f2016-06-02 05:49:21 -0700170 }
171}