blob: 820b55c7feae1ddae08da1b09a77076314452047 [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,
egdaniel0eafe792015-11-20 14:01:22 -080034 const VaryingInfoArray& pathProcVaryings,
egdanielfa896322016-01-13 12:19:30 -080035 GrGLSLPrimitiveProcessor* geometryProcessor,
36 GrGLSLXferProcessor* xferProcessor,
egdaniel09aa1fc2016-04-20 07:09:46 -070037 const GrGLSLFragProcs& fragmentProcessors)
egdanielcdf79db2015-10-19 07:23:02 -070038 : fBuiltinUniformHandles(builtinUniforms)
joshualitt47bb3822014-10-07 16:43:25 -070039 , fProgramID(programID)
joshualitta5305a12014-10-10 17:47:00 -070040 , fGeometryProcessor(geometryProcessor)
egdanielc2304142014-12-11 13:15:13 -080041 , fXferProcessor(xferProcessor)
egdanielfa896322016-01-13 12:19:30 -080042 , fFragmentProcessors(fragmentProcessors)
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +000043 , fDesc(desc)
44 , fGpu(gpu)
egdaniel0eafe792015-11-20 14:01:22 -080045 , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) {
cdalton42717652015-06-18 11:54:30 -070046 // Assign texture units to sampler uniforms one time up front.
47 GL_CALL(UseProgram(fProgramID));
egdaniel09aa1fc2016-04-20 07:09:46 -070048 fProgramDataManager.setSamplers(samplers);
junov@google.comf93e7172011-03-31 21:26:24 +000049}
50
51GrGLProgram::~GrGLProgram() {
kkinnunendddc18a2014-08-03 23:19:46 -070052 if (fProgramID) {
53 GL_CALL(DeleteProgram(fProgramID));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000054 }
egdanielfa896322016-01-13 12:19:30 -080055 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
56 delete fFragmentProcessors[i];
57 }
junov@google.comf93e7172011-03-31 21:26:24 +000058}
59
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000060void GrGLProgram::abandon() {
kkinnunendddc18a2014-08-03 23:19:46 -070061 fProgramID = 0;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000062}
63
bsalomon@google.comeb715c82012-07-11 15:03:31 +000064///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +000065
cdalton74b8d322016-04-11 14:47:28 -070066void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) {
egdaniel8dd688b2015-01-22 10:16:09 -080067 this->setRenderTargetState(primProc, pipeline);
bsalomon@google.com91207482013-02-12 21:45:24 +000068
joshualitt47bb3822014-10-07 16:43:25 -070069 // we set the textures, and uniforms for installed processors in a generic way, but subclasses
70 // of GLProgram determine how to set coord transforms
cdalton74b8d322016-04-11 14:47:28 -070071 int nextSamplerIdx = 0;
bsalomona624bf32016-09-20 09:12:47 -070072 fGeometryProcessor->setData(fProgramDataManager, primProc,
73 GrFragmentProcessor::CoordTransformIter(pipeline));
cdalton74b8d322016-04-11 14:47:28 -070074 this->bindTextures(primProc, pipeline.getAllowSRGBInputs(), &nextSamplerIdx);
cdalton42717652015-06-18 11:54:30 -070075
cdalton74b8d322016-04-11 14:47:28 -070076 this->setFragmentData(primProc, pipeline, &nextSamplerIdx);
joshualitt9b989322014-12-15 14:16:27 -080077
halcanary9d524f22016-03-29 09:03:52 -070078 if (primProc.getPixelLocalStorageState() !=
ethannicholas22793252016-01-30 09:59:10 -080079 GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) {
80 const GrXferProcessor& xp = pipeline.getXferProcessor();
81 fXferProcessor->setData(fProgramDataManager, xp);
cdalton74b8d322016-04-11 14:47:28 -070082 this->bindTextures(xp, pipeline.getAllowSRGBInputs(), &nextSamplerIdx);
ethannicholas22793252016-01-30 09:59:10 -080083 }
joshualitt47bb3822014-10-07 16:43:25 -070084}
85
brianosman33f6b3f2016-06-02 05:49:21 -070086void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc,
87 const GrPipeline& pipeline) {
88 this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs());
89
bsalomonb58a2b42016-09-26 06:55:02 -070090 GrFragmentProcessor::Iter iter(pipeline);
91 while (const GrFragmentProcessor* fp = iter.next()) {
92 this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs());
brianosman33f6b3f2016-06-02 05:49:21 -070093 }
94
95 if (primProc.getPixelLocalStorageState() !=
96 GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) {
97 const GrXferProcessor& xp = pipeline.getXferProcessor();
98 this->generateMipmaps(xp, pipeline.getAllowSRGBInputs());
99 }
100}
101
joshualitt873ad0e2015-01-20 09:08:51 -0800102void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc,
cdalton42717652015-06-18 11:54:30 -0700103 const GrPipeline& pipeline,
cdalton74b8d322016-04-11 14:47:28 -0700104 int* nextSamplerIdx) {
bsalomonb58a2b42016-09-26 06:55:02 -0700105 GrFragmentProcessor::Iter iter(pipeline);
106 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.begin(),
107 fFragmentProcessors.count());
108 const GrFragmentProcessor* fp = iter.next();
109 GrGLSLFragmentProcessor* glslFP = glslIter.next();
110 while (fp && glslFP) {
111 glslFP->setData(fProgramDataManager, *fp);
112 this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextSamplerIdx);
mtklein85552e42016-09-26 08:39:43 -0700113 fp = iter.next();
114 glslFP = glslIter.next();
joshualitta5305a12014-10-10 17:47:00 -0700115 }
bsalomonb58a2b42016-09-26 06:55:02 -0700116 SkASSERT(!fp && !glslFP);
joshualitta5305a12014-10-10 17:47:00 -0700117}
bsalomona624bf32016-09-20 09:12:47 -0700118
bsalomon@google.com91207482013-02-12 21:45:24 +0000119
joshualitt873ad0e2015-01-20 09:08:51 -0800120void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800121 const GrPipeline& pipeline) {
joshualitt47bb3822014-10-07 16:43:25 -0700122 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
123 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
egdaniel8dd688b2015-01-22 10:16:09 -0800124 fRenderTargetState.fRenderTargetSize.fHeight != pipeline.getRenderTarget()->height()) {
joshualitt47bb3822014-10-07 16:43:25 -0700125 fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni,
egdaniel8dd688b2015-01-22 10:16:09 -0800126 SkIntToScalar(pipeline.getRenderTarget()->height()));
joshualitt47bb3822014-10-07 16:43:25 -0700127 }
128
joshualittd8dd47b2015-09-11 11:45:01 -0700129 // set RT adjustment
egdaniel8dd688b2015-01-22 10:16:09 -0800130 const GrRenderTarget* rt = pipeline.getRenderTarget();
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000131 SkISize size;
132 size.set(rt->width(), rt->height());
joshualittd8dd47b2015-09-11 11:45:01 -0700133 if (!primProc.isPathRendering()) {
134 if (fRenderTargetState.fRenderTargetOrigin != rt->origin() ||
135 fRenderTargetState.fRenderTargetSize != size) {
136 fRenderTargetState.fRenderTargetSize = size;
137 fRenderTargetState.fRenderTargetOrigin = rt->origin();
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000138
egdaniel018fb622015-10-28 07:26:40 -0700139 float rtAdjustmentVec[4];
joshualittd8dd47b2015-09-11 11:45:01 -0700140 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
141 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
142 }
143 } else {
144 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
145 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
146 fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
147 size, rt->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000148 }
149}
cdalton74b8d322016-04-11 14:47:28 -0700150
151void GrGLProgram::bindTextures(const GrProcessor& processor,
152 bool allowSRGBInputs,
153 int* nextSamplerIdx) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500154 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
155 const GrProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500156 fGpu->bindTexture((*nextSamplerIdx)++, sampler.params(),
157 allowSRGBInputs, static_cast<GrGLTexture*>(sampler.texture()));
cdalton74b8d322016-04-11 14:47:28 -0700158 }
159 for (int i = 0; i < processor.numBuffers(); ++i) {
Brian Salomonb014cca2016-11-18 11:39:15 -0500160 const GrProcessor::BufferAccess& access = processor.bufferAccess(i);
csmartdalton1897cfd2016-06-03 08:50:54 -0700161 fGpu->bindTexelBuffer((*nextSamplerIdx)++, access.texelConfig(),
cdalton74b8d322016-04-11 14:47:28 -0700162 static_cast<GrGLBuffer*>(access.buffer()));
163 }
164}
brianosman33f6b3f2016-06-02 05:49:21 -0700165
166void GrGLProgram::generateMipmaps(const GrProcessor& processor,
167 bool allowSRGBInputs) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500168 for (int i = 0; i < processor.numTextureSamplers(); ++i) {
169 const GrProcessor::TextureSampler& sampler = processor.textureSampler(i);
Brian Salomondb4183d2016-11-17 12:48:40 -0500170 fGpu->generateMipmaps(sampler.params(), allowSRGBInputs,
171 static_cast<GrGLTexture*>(sampler.texture()));
brianosman33f6b3f2016-06-02 05:49:21 -0700172 }
173}