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