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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrPathProcessor.h" |
| 9 | #include "src/gpu/GrPipeline.h" |
| 10 | #include "src/gpu/GrProcessor.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrProgramInfo.h" |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrTAllocator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrTexturePriv.h" |
| 14 | #include "src/gpu/GrXferProcessor.h" |
| 15 | #include "src/gpu/gl/GrGLBuffer.h" |
| 16 | #include "src/gpu/gl/GrGLGpu.h" |
| 17 | #include "src/gpu/gl/GrGLPathRendering.h" |
| 18 | #include "src/gpu/gl/GrGLProgram.h" |
| 19 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 20 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 21 | #include "src/gpu/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 | |
Robert Phillips | df546db | 2020-05-29 09:42:54 -0400 | [diff] [blame] | 28 | sk_sp<GrGLProgram> GrGLProgram::Make( |
| 29 | GrGLGpu* gpu, |
| 30 | const GrGLSLBuiltinUniformHandles& builtinUniforms, |
| 31 | GrGLuint programID, |
| 32 | const UniformInfoArray& uniforms, |
| 33 | const UniformInfoArray& textureSamplers, |
| 34 | const VaryingInfoArray& pathProcVaryings, |
| 35 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 36 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
| 37 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fps, |
Robert Phillips | df546db | 2020-05-29 09:42:54 -0400 | [diff] [blame] | 38 | std::unique_ptr<Attribute[]> attributes, |
| 39 | int vertexAttributeCnt, |
| 40 | int instanceAttributeCnt, |
| 41 | int vertexStride, |
| 42 | int instanceStride) { |
| 43 | sk_sp<GrGLProgram> program(new GrGLProgram(gpu, |
| 44 | builtinUniforms, |
| 45 | programID, |
| 46 | uniforms, |
| 47 | textureSamplers, |
| 48 | pathProcVaryings, |
| 49 | std::move(geometryProcessor), |
| 50 | std::move(xferProcessor), |
| 51 | std::move(fps), |
Robert Phillips | df546db | 2020-05-29 09:42:54 -0400 | [diff] [blame] | 52 | std::move(attributes), |
| 53 | vertexAttributeCnt, |
| 54 | instanceAttributeCnt, |
| 55 | vertexStride, |
| 56 | instanceStride)); |
| 57 | // Assign texture units to sampler uniforms one time up front. |
| 58 | gpu->flushProgram(program); |
| 59 | program->fProgramDataManager.setSamplerUniforms(textureSamplers, 0); |
| 60 | return program; |
| 61 | } |
| 62 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 63 | GrGLProgram::GrGLProgram( |
| 64 | GrGLGpu* gpu, |
| 65 | const GrGLSLBuiltinUniformHandles& builtinUniforms, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 66 | GrGLuint programID, |
| 67 | const UniformInfoArray& uniforms, |
| 68 | const UniformInfoArray& textureSamplers, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 69 | const VaryingInfoArray& pathProcVaryings, |
| 70 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 71 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
Robert Phillips | df546db | 2020-05-29 09:42:54 -0400 | [diff] [blame] | 72 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fps, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 73 | std::unique_ptr<Attribute[]> attributes, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 74 | int vertexAttributeCnt, |
| 75 | int instanceAttributeCnt, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 76 | int vertexStride, |
| 77 | int instanceStride) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 78 | : fBuiltinUniformHandles(builtinUniforms) |
| 79 | , fProgramID(programID) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 80 | , fPrimitiveProcessor(std::move(geometryProcessor)) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 81 | , fXferProcessor(std::move(xferProcessor)) |
Robert Phillips | df546db | 2020-05-29 09:42:54 -0400 | [diff] [blame] | 82 | , fFragmentProcessors(std::move(fps)) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 83 | , fAttributes(std::move(attributes)) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 84 | , fVertexAttributeCnt(vertexAttributeCnt) |
| 85 | , fInstanceAttributeCnt(instanceAttributeCnt) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 86 | , fVertexStride(vertexStride) |
| 87 | , fInstanceStride(instanceStride) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 88 | , fGpu(gpu) |
| 89 | , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 90 | , fNumTextureSamplers(textureSamplers.count()) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | GrGLProgram::~GrGLProgram() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 94 | if (fProgramID) { |
| 95 | GL_CALL(DeleteProgram(fProgramID)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 96 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 99 | void GrGLProgram::abandon() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 100 | fProgramID = 0; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 103 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 104 | |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 105 | void GrGLProgram::updateUniforms(const GrRenderTarget* renderTarget, |
| 106 | const GrProgramInfo& programInfo) { |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 107 | this->setRenderTargetState(renderTarget, programInfo.origin(), programInfo.primProc()); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 108 | |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 109 | // we set the uniforms for installed processors in a generic way, but subclasses of GLProgram |
| 110 | // determine how to set coord transforms |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 111 | |
| 112 | // We must bind to texture units in the same order in which we set the uniforms in |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 113 | // GrGLProgramDataManager. That is, we bind textures for processors in this order: |
| 114 | // primProc, fragProcs, XP. |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 115 | fPrimitiveProcessor->setData(fProgramDataManager, programInfo.primProc()); |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 116 | |
Brian Salomon | 61a70fb | 2020-07-08 19:02:54 -0400 | [diff] [blame] | 117 | for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) { |
| 118 | auto& pipelineFP = programInfo.pipeline().getFragmentProcessor(i); |
| 119 | auto& baseGLSLFP = *fFragmentProcessors[i]; |
| 120 | for (auto [fp, glslFP] : GrGLSLFragmentProcessor::ParallelRange(pipelineFP, baseGLSLFP)) { |
| 121 | glslFP.setData(fProgramDataManager, fp); |
| 122 | } |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 123 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 124 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 125 | const GrXferProcessor& xp = programInfo.pipeline().getXferProcessor(); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 126 | SkIPoint offset; |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 127 | GrTexture* dstTexture = programInfo.pipeline().peekDstTexture(&offset); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 128 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 129 | fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Chris Dalton | db20afc | 2020-03-05 12:13:53 -0700 | [diff] [blame] | 132 | void GrGLProgram::bindTextures(const GrPrimitiveProcessor& primProc, |
| 133 | const GrSurfaceProxy* const primProcTextures[], |
| 134 | const GrPipeline& pipeline) { |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 135 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 136 | SkASSERT(primProcTextures[i]->asTextureProxy()); |
| 137 | auto* overrideTexture = static_cast<GrGLTexture*>(primProcTextures[i]->peekTexture()); |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 138 | fGpu->bindTexture(i, primProc.textureSampler(i).samplerState(), |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 139 | primProc.textureSampler(i).swizzle(), overrideTexture); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 140 | } |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 141 | int nextTexSamplerIdx = primProc.numTextureSamplers(); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 142 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 143 | GrFragmentProcessor::CIter fpIter(pipeline); |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 144 | for (; fpIter; ++fpIter) { |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 145 | for (int i = 0; i < fpIter->numTextureSamplers(); ++i) { |
| 146 | const GrFragmentProcessor::TextureSampler& sampler = fpIter->textureSampler(i); |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 147 | fGpu->bindTexture(nextTexSamplerIdx++, sampler.samplerState(), sampler.view().swizzle(), |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 148 | static_cast<GrGLTexture*>(sampler.peekTexture())); |
| 149 | } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 150 | } |
Chris Dalton | 3ffa7af | 2020-02-20 16:31:47 -0700 | [diff] [blame] | 151 | |
| 152 | SkIPoint offset; |
| 153 | GrTexture* dstTexture = pipeline.peekDstTexture(&offset); |
| 154 | if (dstTexture) { |
| 155 | fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::Filter::kNearest, |
| 156 | pipeline.dstProxyView().swizzle(), static_cast<GrGLTexture*>(dstTexture)); |
| 157 | } |
| 158 | SkASSERT(nextTexSamplerIdx == fNumTextureSamplers); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 159 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 160 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 161 | void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin, |
| 162 | const GrPrimitiveProcessor& primProc) { |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 163 | // Load the RT size uniforms if they are needed |
| 164 | if (fBuiltinUniformHandles.fRTWidthUni.isValid() && |
| 165 | fRenderTargetState.fRenderTargetSize.fWidth != rt->width()) { |
| 166 | fProgramDataManager.set1f(fBuiltinUniformHandles.fRTWidthUni, SkIntToScalar(rt->width())); |
| 167 | } |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 168 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
| 169 | fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) { |
| 170 | fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height())); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 171 | } |
| 172 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 173 | // set RT adjustment |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 174 | SkISize dimensions = rt->dimensions(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 175 | if (!primProc.isPathRendering()) { |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 176 | if (fRenderTargetState.fRenderTargetOrigin != origin || |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 177 | fRenderTargetState.fRenderTargetSize != dimensions) { |
| 178 | fRenderTargetState.fRenderTargetSize = dimensions; |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 179 | fRenderTargetState.fRenderTargetOrigin = origin; |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 180 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 181 | float rtAdjustmentVec[4]; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 182 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
| 183 | fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
| 184 | } |
| 185 | } else { |
| 186 | SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport()); |
| 187 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 188 | fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(), dimensions, origin); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 189 | } |
| 190 | } |