junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 7 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 8 | #include "GrGLProgram.h" |
| 9 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "GrAllocator.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrProcessor.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 12 | #include "GrCoordTransform.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 13 | #include "GrGLGpu.h" |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 14 | #include "GrGLBuffer.h" |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 15 | #include "GrGLPathRendering.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 16 | #include "GrPathProcessor.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 17 | #include "GrPipeline.h" |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 18 | #include "GrXferProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 20 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLXferProcessor.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 23 | #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.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 25 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 26 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 27 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 28 | GrGLProgram::GrGLProgram(GrGLGpu* gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 29 | const GrProgramDesc& desc, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 30 | const BuiltinUniformHandles& builtinUniforms, |
| 31 | GrGLuint programID, |
| 32 | const UniformInfoArray& uniforms, |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 33 | const UniformInfoArray& textureSamplers, |
| 34 | const UniformInfoArray& texelBuffers, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 35 | const UniformInfoArray& imageStorages, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 36 | const VaryingInfoArray& pathProcVaryings, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 37 | GrGLSLPrimitiveProcessor* geometryProcessor, |
| 38 | GrGLSLXferProcessor* xferProcessor, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 39 | const GrGLSLFragProcs& fragmentProcessors) |
egdaniel | cdf79db | 2015-10-19 07:23:02 -0700 | [diff] [blame] | 40 | : fBuiltinUniformHandles(builtinUniforms) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 41 | , fProgramID(programID) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 42 | , fGeometryProcessor(geometryProcessor) |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 43 | , fXferProcessor(xferProcessor) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 44 | , fFragmentProcessors(fragmentProcessors) |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 45 | , fDesc(desc) |
| 46 | , fGpu(gpu) |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 47 | , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) |
| 48 | , fNumTextureSamplers(textureSamplers.count()) |
| 49 | , fNumTexelBuffers(texelBuffers.count()) { |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 50 | // Assign texture units to sampler uniforms one time up front. |
| 51 | GL_CALL(UseProgram(fProgramID)); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 52 | fProgramDataManager.setSamplerUniforms(textureSamplers, 0); |
| 53 | fProgramDataManager.setSamplerUniforms(texelBuffers, fNumTextureSamplers); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 54 | fProgramDataManager.setImageStorages(imageStorages); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | GrGLProgram::~GrGLProgram() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 58 | if (fProgramID) { |
| 59 | GL_CALL(DeleteProgram(fProgramID)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 60 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 61 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 62 | delete fFragmentProcessors[i]; |
| 63 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 66 | void GrGLProgram::abandon() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 67 | fProgramID = 0; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 68 | } |
| 69 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 71 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 72 | void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) { |
Robert Phillips | 02bb6df | 2017-03-28 17:11:19 -0400 | [diff] [blame] | 73 | this->setRenderTargetState(primProc, pipeline.getRenderTarget()); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 74 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 75 | // 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 Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 77 | |
| 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; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 85 | fGeometryProcessor->setData(fProgramDataManager, primProc, |
| 86 | GrFragmentProcessor::CoordTransformIter(pipeline)); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 87 | this->bindTextures(primProc, pipeline.getAllowSRGBInputs(), &nextTexSamplerIdx, |
| 88 | &nextTexelBufferIdx, &nextImageStorageIdx); |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 89 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 90 | this->setFragmentData(primProc, pipeline, &nextTexSamplerIdx, &nextTexelBufferIdx, |
| 91 | &nextImageStorageIdx); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 92 | |
Brian Salomon | 42c456f | 2017-03-06 11:29:48 -0500 | [diff] [blame] | 93 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 94 | SkIPoint offset; |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame^] | 95 | GrTexture* dstTexture = pipeline.peekDstTexture(&offset); |
| 96 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 97 | fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset); |
| 98 | if (dstTexture) { |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 99 | fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerParams::ClampNoFilter(), true, |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 100 | static_cast<GrGLTexture*>(dstTexture)); |
| 101 | } |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 102 | SkASSERT(nextTexSamplerIdx == fNumTextureSamplers); |
| 103 | SkASSERT(nextTexelBufferIdx == fNumTextureSamplers + fNumTexelBuffers); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 106 | void GrGLProgram::generateMipmaps(const GrPrimitiveProcessor& primProc, |
| 107 | const GrPipeline& pipeline) { |
| 108 | this->generateMipmaps(primProc, pipeline.getAllowSRGBInputs()); |
| 109 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 110 | GrFragmentProcessor::Iter iter(pipeline); |
| 111 | while (const GrFragmentProcessor* fp = iter.next()) { |
| 112 | this->generateMipmaps(*fp, pipeline.getAllowSRGBInputs()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 113 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 114 | } |
| 115 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 116 | void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc, |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 117 | const GrPipeline& pipeline, |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 118 | int* nextTexSamplerIdx, |
| 119 | int* nextTexelBufferIdx, |
| 120 | int* nextImageStorageIdx) { |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 121 | 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 Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 128 | this->bindTextures(*fp, pipeline.getAllowSRGBInputs(), nextTexSamplerIdx, |
| 129 | nextTexelBufferIdx, nextImageStorageIdx); |
mtklein | 85552e4 | 2016-09-26 08:39:43 -0700 | [diff] [blame] | 130 | fp = iter.next(); |
| 131 | glslFP = glslIter.next(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 132 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 133 | SkASSERT(!fp && !glslFP); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 134 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 135 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 136 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 137 | void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc, |
Robert Phillips | 02bb6df | 2017-03-28 17:11:19 -0400 | [diff] [blame] | 138 | const GrRenderTarget* rt) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 139 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 140 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
Robert Phillips | 02bb6df | 2017-03-28 17:11:19 -0400 | [diff] [blame] | 141 | fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) { |
| 142 | fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height())); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 143 | } |
| 144 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 145 | // set RT adjustment |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 146 | SkISize size; |
| 147 | size.set(rt->width(), rt->height()); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 148 | if (!primProc.isPathRendering()) { |
| 149 | if (fRenderTargetState.fRenderTargetOrigin != rt->origin() || |
| 150 | fRenderTargetState.fRenderTargetSize != size) { |
| 151 | fRenderTargetState.fRenderTargetSize = size; |
| 152 | fRenderTargetState.fRenderTargetOrigin = rt->origin(); |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 153 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 154 | float rtAdjustmentVec[4]; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 155 | 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(), |
| 162 | size, rt->origin()); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 165 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 166 | void GrGLProgram::bindTextures(const GrResourceIOProcessor& processor, |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 167 | bool allowSRGBInputs, |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 168 | int* nextTexSamplerIdx, |
| 169 | int* nextTexelBufferIdx, |
| 170 | int* nextImageStorageIdx) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 171 | for (int i = 0; i < processor.numTextureSamplers(); ++i) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 172 | const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 173 | fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.params(), |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 174 | allowSRGBInputs, static_cast<GrGLTexture*>(sampler.peekTexture())); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 175 | } |
| 176 | for (int i = 0; i < processor.numBuffers(); ++i) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 177 | const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(i); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 178 | fGpu->bindTexelBuffer((*nextTexelBufferIdx)++, access.texelConfig(), |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 179 | static_cast<GrGLBuffer*>(access.buffer())); |
| 180 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 181 | for (int i = 0; i < processor.numImageStorages(); ++i) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 182 | const GrResourceIOProcessor::ImageStorageAccess& access = processor.imageStorageAccess(i); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 183 | fGpu->bindImageStorage((*nextImageStorageIdx)++, access.ioType(), |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 184 | static_cast<GrGLTexture *>(access.peekTexture())); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 185 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 186 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 187 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 188 | void GrGLProgram::generateMipmaps(const GrResourceIOProcessor& processor, bool allowSRGBInputs) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 189 | for (int i = 0; i < processor.numTextureSamplers(); ++i) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 190 | const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 191 | fGpu->generateMipmaps(sampler.params(), allowSRGBInputs, |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 192 | static_cast<GrGLTexture*>(sampler.peekTexture())); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 193 | } |
| 194 | } |