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 | #include "SkXfermode.h" |
| 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 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 29 | GrGLProgram::GrGLProgram(GrGLGpu* gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 30 | const GrProgramDesc& desc, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 31 | const BuiltinUniformHandles& builtinUniforms, |
| 32 | GrGLuint programID, |
| 33 | const UniformInfoArray& uniforms, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame^] | 34 | const SkTArray<GrGLSampler>& samplers, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 35 | const VaryingInfoArray& pathProcVaryings, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 36 | GrGLSLPrimitiveProcessor* geometryProcessor, |
| 37 | GrGLSLXferProcessor* xferProcessor, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame^] | 38 | const GrGLSLFragProcs& fragmentProcessors) |
egdaniel | cdf79db | 2015-10-19 07:23:02 -0700 | [diff] [blame] | 39 | : fBuiltinUniformHandles(builtinUniforms) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 40 | , fProgramID(programID) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 41 | , fGeometryProcessor(geometryProcessor) |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 42 | , fXferProcessor(xferProcessor) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 43 | , fFragmentProcessors(fragmentProcessors) |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 44 | , fDesc(desc) |
| 45 | , fGpu(gpu) |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 46 | , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) { |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 47 | // Assign texture units to sampler uniforms one time up front. |
| 48 | GL_CALL(UseProgram(fProgramID)); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame^] | 49 | fProgramDataManager.setSamplers(samplers); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | GrGLProgram::~GrGLProgram() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 53 | if (fProgramID) { |
| 54 | GL_CALL(DeleteProgram(fProgramID)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 55 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 56 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 57 | delete fFragmentProcessors[i]; |
| 58 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 59 | } |
| 60 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 61 | void GrGLProgram::abandon() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 62 | fProgramID = 0; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 65 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 66 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 67 | void GrGLProgram::setData(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 68 | this->setRenderTargetState(primProc, pipeline); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 69 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 70 | // we set the textures, and uniforms for installed processors in a generic way, but subclasses |
| 71 | // of GLProgram determine how to set coord transforms |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 72 | int nextSamplerIdx = 0; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 73 | fGeometryProcessor->setData(fProgramDataManager, primProc); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 74 | this->bindTextures(primProc, pipeline.getAllowSRGBInputs(), &nextSamplerIdx); |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 75 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 76 | this->setFragmentData(primProc, pipeline, &nextSamplerIdx); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 77 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 78 | if (primProc.getPixelLocalStorageState() != |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 79 | GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) { |
| 80 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
| 81 | fXferProcessor->setData(fProgramDataManager, xp); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 82 | this->bindTextures(xp, pipeline.getAllowSRGBInputs(), &nextSamplerIdx); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 83 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 84 | } |
| 85 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 86 | void GrGLProgram::setFragmentData(const GrPrimitiveProcessor& primProc, |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 87 | const GrPipeline& pipeline, |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 88 | int* nextSamplerIdx) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 89 | int numProcessors = fFragmentProcessors.count(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 90 | for (int i = 0; i < numProcessors; ++i) { |
| 91 | const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 92 | fFragmentProcessors[i]->setData(fProgramDataManager, processor); |
| 93 | this->setTransformData(primProc, processor, i); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 94 | this->bindTextures(processor, pipeline.getAllowSRGBInputs(), nextSamplerIdx); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 97 | void GrGLProgram::setTransformData(const GrPrimitiveProcessor& primProc, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 98 | const GrFragmentProcessor& processor, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 99 | int index) { |
| 100 | fGeometryProcessor->setTransformData(primProc, fProgramDataManager, index, |
| 101 | processor.coordTransforms()); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 102 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 103 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 104 | void GrGLProgram::setRenderTargetState(const GrPrimitiveProcessor& primProc, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 105 | const GrPipeline& pipeline) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 106 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 107 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 108 | fRenderTargetState.fRenderTargetSize.fHeight != pipeline.getRenderTarget()->height()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 109 | fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 110 | SkIntToScalar(pipeline.getRenderTarget()->height())); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 113 | // set RT adjustment |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 114 | const GrRenderTarget* rt = pipeline.getRenderTarget(); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 115 | SkISize size; |
| 116 | size.set(rt->width(), rt->height()); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 117 | if (!primProc.isPathRendering()) { |
| 118 | if (fRenderTargetState.fRenderTargetOrigin != rt->origin() || |
| 119 | fRenderTargetState.fRenderTargetSize != size) { |
| 120 | fRenderTargetState.fRenderTargetSize = size; |
| 121 | fRenderTargetState.fRenderTargetOrigin = rt->origin(); |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 122 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 123 | float rtAdjustmentVec[4]; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 124 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
| 125 | fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
| 126 | } |
| 127 | } else { |
| 128 | SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport()); |
| 129 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
| 130 | fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(), |
| 131 | size, rt->origin()); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 134 | |
| 135 | void GrGLProgram::bindTextures(const GrProcessor& processor, |
| 136 | bool allowSRGBInputs, |
| 137 | int* nextSamplerIdx) { |
| 138 | for (int i = 0; i < processor.numTextures(); ++i) { |
| 139 | const GrTextureAccess& access = processor.textureAccess(i); |
| 140 | fGpu->bindTexture((*nextSamplerIdx)++, access.getParams(), |
| 141 | allowSRGBInputs, static_cast<GrGLTexture*>(access.getTexture())); |
| 142 | } |
| 143 | for (int i = 0; i < processor.numBuffers(); ++i) { |
| 144 | const GrBufferAccess& access = processor.bufferAccess(i); |
| 145 | fGpu->bindTexelBuffer((*nextSamplerIdx)++, access.offsetInBytes(), access.texelConfig(), |
| 146 | static_cast<GrGLBuffer*>(access.buffer())); |
| 147 | } |
| 148 | } |