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