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" |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 13 | #include "GrGLGeometryProcessor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 14 | #include "GrGLProcessor.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 15 | #include "GrGpuGL.h" |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 16 | #include "GrGLPathRendering.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 17 | #include "GrGLShaderVar.h" |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 18 | #include "GrGLSL.h" |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 19 | #include "GrOptDrawState.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 20 | #include "SkXfermode.h" |
| 21 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 22 | #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) |
| 23 | #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] | 24 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Retrieves the final matrix that a transform needs to apply to its source coords. |
| 27 | */ |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 28 | static SkMatrix get_transform_matrix(const GrPendingFragmentStage& stage, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 29 | bool useExplicitLocalCoords, |
| 30 | int transformIdx) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 31 | const GrCoordTransform& coordTransform = stage.getProcessor()->coordTransform(transformIdx); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 32 | SkMatrix combined; |
| 33 | |
| 34 | if (kLocal_GrCoordSet == coordTransform.sourceCoords()) { |
| 35 | // If we have explicit local coords then we shouldn't need a coord change. |
| 36 | const SkMatrix& ccm = |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 37 | useExplicitLocalCoords ? SkMatrix::I() : stage.getCoordChangeMatrix(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 38 | combined.setConcat(coordTransform.getMatrix(), ccm); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 39 | } else { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 40 | combined = coordTransform.getMatrix(); |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 41 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 42 | if (coordTransform.reverseY()) { |
| 43 | // combined.postScale(1,-1); |
| 44 | // combined.postTranslate(0,1); |
| 45 | combined.set(SkMatrix::kMSkewY, |
| 46 | combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]); |
| 47 | combined.set(SkMatrix::kMScaleY, |
| 48 | combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]); |
| 49 | combined.set(SkMatrix::kMTransY, |
| 50 | combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 51 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 52 | return combined; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 53 | } |
| 54 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 55 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 56 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 57 | GrGLProgram::GrGLProgram(GrGpuGL* gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 58 | const GrProgramDesc& desc, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 59 | const BuiltinUniformHandles& builtinUniforms, |
| 60 | GrGLuint programID, |
| 61 | const UniformInfoArray& uniforms, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 62 | GrGLInstalledGeoProc* geometryProcessor, |
| 63 | GrGLInstalledFragProcs* fragmentProcessors) |
commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 64 | : fColor(GrColor_ILLEGAL) |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 65 | , fCoverage(0) |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 66 | , fDstCopyTexUnit(-1) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 67 | , fBuiltinUniformHandles(builtinUniforms) |
| 68 | , fProgramID(programID) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 69 | , fGeometryProcessor(geometryProcessor) |
| 70 | , fFragmentProcessors(SkRef(fragmentProcessors)) |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 71 | , fDesc(desc) |
| 72 | , fGpu(gpu) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 73 | , fProgramDataManager(gpu, uniforms) { |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 74 | this->initSamplerUniforms(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | GrGLProgram::~GrGLProgram() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 78 | if (fProgramID) { |
| 79 | GL_CALL(DeleteProgram(fProgramID)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 80 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 83 | void GrGLProgram::abandon() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 84 | fProgramID = 0; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 87 | void GrGLProgram::initSamplerUniforms() { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 88 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 89 | GrGLint texUnitIdx = 0; |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 90 | if (fBuiltinUniformHandles.fDstCopySamplerUni.isValid()) { |
| 91 | fProgramDataManager.setSampler(fBuiltinUniformHandles.fDstCopySamplerUni, texUnitIdx); |
bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 92 | fDstCopyTexUnit = texUnitIdx++; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 93 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 94 | if (fGeometryProcessor.get()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 95 | this->initSamplers(fGeometryProcessor.get(), &texUnitIdx); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 96 | } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 97 | int numProcs = fFragmentProcessors->fProcs.count(); |
| 98 | for (int i = 0; i < numProcs; i++) { |
| 99 | this->initSamplers(fFragmentProcessors->fProcs[i], &texUnitIdx); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 103 | void GrGLProgram::initSamplers(GrGLInstalledProc* ip, int* texUnitIdx) { |
| 104 | SkTArray<GrGLInstalledProc::Sampler, true>& samplers = ip->fSamplers; |
| 105 | int numSamplers = samplers.count(); |
| 106 | for (int s = 0; s < numSamplers; ++s) { |
| 107 | SkASSERT(samplers[s].fUniform.isValid()); |
| 108 | fProgramDataManager.setSampler(samplers[s].fUniform, *texUnitIdx); |
| 109 | samplers[s].fTextureUnit = (*texUnitIdx)++; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void GrGLProgram::bindTextures(const GrGLInstalledProc* ip, const GrProcessor& processor) { |
| 114 | const SkTArray<GrGLInstalledProc::Sampler, true>& samplers = ip->fSamplers; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 115 | int numSamplers = samplers.count(); |
| 116 | SkASSERT(numSamplers == processor.numTextures()); |
| 117 | for (int s = 0; s < numSamplers; ++s) { |
| 118 | SkASSERT(samplers[s].fTextureUnit >= 0); |
| 119 | const GrTextureAccess& textureAccess = processor.textureAccess(s); |
| 120 | fGpu->bindTexture(samplers[s].fTextureUnit, |
| 121 | textureAccess.getParams(), |
| 122 | static_cast<GrGLTexture*>(textureAccess.getTexture())); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 127 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 128 | |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame^] | 129 | void GrGLProgram::setData(const GrOptDrawState& optState, GrGpu::DrawType drawType) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 130 | GrColor color = optState.getColor(); |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 131 | uint8_t coverage = optState.getCoverage(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 132 | |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 133 | this->setColor(optState, color); |
| 134 | this->setCoverage(optState, coverage); |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 135 | this->setMatrixAndRenderTargetHeight(drawType, optState); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 136 | |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame^] | 137 | const GrDeviceCoordTexture* dstCopy = optState.getDstCopy(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 138 | if (dstCopy) { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 139 | if (fBuiltinUniformHandles.fDstCopyTopLeftUni.isValid()) { |
| 140 | fProgramDataManager.set2f(fBuiltinUniformHandles.fDstCopyTopLeftUni, |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 141 | static_cast<GrGLfloat>(dstCopy->offset().fX), |
| 142 | static_cast<GrGLfloat>(dstCopy->offset().fY)); |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 143 | fProgramDataManager.set2f(fBuiltinUniformHandles.fDstCopyScaleUni, |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 144 | 1.f / dstCopy->texture()->width(), |
| 145 | 1.f / dstCopy->texture()->height()); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 146 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()); |
| 147 | static GrTextureParams kParams; // the default is clamp, nearest filtering. |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 148 | fGpu->bindTexture(fDstCopyTexUnit, kParams, texture); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 149 | } else { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 150 | SkASSERT(!fBuiltinUniformHandles.fDstCopyScaleUni.isValid()); |
| 151 | SkASSERT(!fBuiltinUniformHandles.fDstCopySamplerUni.isValid()); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 152 | } |
| 153 | } else { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 154 | SkASSERT(!fBuiltinUniformHandles.fDstCopyTopLeftUni.isValid()); |
| 155 | SkASSERT(!fBuiltinUniformHandles.fDstCopyScaleUni.isValid()); |
| 156 | SkASSERT(!fBuiltinUniformHandles.fDstCopySamplerUni.isValid()); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 157 | } |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 158 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 159 | // we set the textures, and uniforms for installed processors in a generic way, but subclasses |
| 160 | // of GLProgram determine how to set coord transforms |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 161 | if (fGeometryProcessor.get()) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 162 | SkASSERT(optState.hasGeometryProcessor()); |
| 163 | const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); |
| 164 | fGeometryProcessor->fGLProc->setData(fProgramDataManager, gp); |
| 165 | this->bindTextures(fGeometryProcessor, gp); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 166 | } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 167 | this->setFragmentData(optState); |
commit-bot@chromium.org | 2080722 | 2013-11-01 11:54:54 +0000 | [diff] [blame] | 168 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 169 | // Some of GrGLProgram subclasses need to update state here |
| 170 | this->didSetData(drawType); |
| 171 | } |
| 172 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 173 | void GrGLProgram::setFragmentData(const GrOptDrawState& optState) { |
| 174 | int numProcessors = fFragmentProcessors->fProcs.count(); |
| 175 | for (int e = 0; e < numProcessors; ++e) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 176 | const GrPendingFragmentStage& stage = optState.getFragmentStage(e); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 177 | const GrProcessor& processor = *stage.getProcessor(); |
| 178 | fFragmentProcessors->fProcs[e]->fGLProc->setData(fProgramDataManager, processor); |
| 179 | this->setTransformData(stage, fFragmentProcessors->fProcs[e]); |
| 180 | this->bindTextures(fFragmentProcessors->fProcs[e], processor); |
| 181 | } |
| 182 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 183 | void GrGLProgram::setTransformData(const GrPendingFragmentStage& processor, |
| 184 | GrGLInstalledFragProc* ip) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 185 | SkTArray<GrGLInstalledFragProc::Transform, true>& transforms = ip->fTransforms; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 186 | int numTransforms = transforms.count(); |
| 187 | SkASSERT(numTransforms == processor.getProcessor()->numTransforms()); |
| 188 | for (int t = 0; t < numTransforms; ++t) { |
| 189 | SkASSERT(transforms[t].fHandle.isValid()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 190 | const SkMatrix& matrix = get_transform_matrix(processor, ip->fLocalCoordAttrib, t); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 191 | if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) { |
| 192 | fProgramDataManager.setSkMatrix(transforms[t].fHandle.convertToUniformHandle(), matrix); |
| 193 | transforms[t].fCurrentValue = matrix; |
| 194 | } |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 195 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 196 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 197 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 198 | void GrGLProgram::didSetData(GrGpu::DrawType drawType) { |
| 199 | SkASSERT(!GrGpu::IsPathRenderingDrawType(drawType)); |
| 200 | } |
| 201 | |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 202 | void GrGLProgram::setColor(const GrOptDrawState& optState, GrColor color) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 203 | const GrProgramDesc::KeyHeader& header = fDesc.header(); |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 204 | switch (header.fColorInput) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 205 | case GrProgramDesc::kAttribute_ColorInput: |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 206 | // Attribute case is handled in GrGpuGL::setupGeometry |
| 207 | break; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 208 | case GrProgramDesc::kUniform_ColorInput: |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 209 | if (fColor != color && fBuiltinUniformHandles.fColorUni.isValid()) { |
| 210 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 211 | GrGLfloat c[4]; |
| 212 | GrColorToRGBAFloat(color, c); |
| 213 | fProgramDataManager.set4fv(fBuiltinUniformHandles.fColorUni, 1, c); |
| 214 | fColor = color; |
| 215 | } |
| 216 | break; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 217 | case GrProgramDesc::kAllOnes_ColorInput: |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 218 | // Handled by shader creation |
| 219 | break; |
| 220 | default: |
| 221 | SkFAIL("Unexpected color type."); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 225 | void GrGLProgram::setCoverage(const GrOptDrawState& optState, uint8_t coverage) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 226 | const GrProgramDesc::KeyHeader& header = fDesc.header(); |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 227 | switch (header.fCoverageInput) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 228 | case GrProgramDesc::kAttribute_ColorInput: |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 229 | // Attribute case is handled in GrGpuGL::setupGeometry |
| 230 | break; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 231 | case GrProgramDesc::kUniform_ColorInput: |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 232 | if (fCoverage != coverage) { |
| 233 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 234 | GrGLfloat c = GrNormalizeByteToFloat(coverage); |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 235 | fProgramDataManager.set1f(fBuiltinUniformHandles.fCoverageUni, c); |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 236 | fCoverage = coverage; |
| 237 | } |
| 238 | break; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 239 | case GrProgramDesc::kAllOnes_ColorInput: |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 240 | // Handled by shader creation |
| 241 | break; |
| 242 | default: |
| 243 | SkFAIL("Unexpected coverage type."); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 246 | |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 247 | void GrGLProgram::setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 248 | const GrOptDrawState& optState) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 249 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 250 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
| 251 | fMatrixState.fRenderTargetSize.fHeight != optState.getRenderTarget()->height()) { |
| 252 | fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, |
| 253 | SkIntToScalar(optState.getRenderTarget()->height())); |
| 254 | } |
| 255 | |
| 256 | // call subclasses to set the actual view matrix |
| 257 | this->onSetMatrixAndRenderTargetHeight(drawType, optState); |
| 258 | } |
| 259 | |
| 260 | void GrGLProgram::onSetMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, |
| 261 | const GrOptDrawState& optState) { |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 262 | const GrRenderTarget* rt = optState.getRenderTarget(); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 263 | SkISize size; |
| 264 | size.set(rt->width(), rt->height()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 265 | if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 266 | fMatrixState.fRenderTargetSize != size || |
| 267 | !fMatrixState.fViewMatrix.cheapEqualTo(optState.getViewMatrix())) { |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 268 | SkASSERT(fBuiltinUniformHandles.fViewMatrixUni.isValid()); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 269 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 270 | fMatrixState.fViewMatrix = optState.getViewMatrix(); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 271 | fMatrixState.fRenderTargetSize = size; |
| 272 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 273 | |
| 274 | GrGLfloat viewMatrix[3 * 3]; |
| 275 | fMatrixState.getGLMatrix<3>(viewMatrix); |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 276 | fProgramDataManager.setMatrix3f(fBuiltinUniformHandles.fViewMatrixUni, viewMatrix); |
commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 277 | |
| 278 | GrGLfloat rtAdjustmentVec[4]; |
| 279 | fMatrixState.getRTAdjustmentVec(rtAdjustmentVec); |
kkinnunen | dddc18a | 2014-08-03 23:19:46 -0700 | [diff] [blame] | 280 | fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 283 | |
| 284 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 285 | |
| 286 | GrGLNvprProgramBase::GrGLNvprProgramBase(GrGpuGL* gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 287 | const GrProgramDesc& desc, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 288 | const BuiltinUniformHandles& builtinUniforms, |
| 289 | GrGLuint programID, |
| 290 | const UniformInfoArray& uniforms, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 291 | GrGLInstalledFragProcs* fragmentProcessors) |
| 292 | : INHERITED(gpu, desc, builtinUniforms, programID, uniforms, NULL, fragmentProcessors) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void GrGLNvprProgramBase::onSetMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, |
| 296 | const GrOptDrawState& optState) { |
| 297 | SkASSERT(GrGpu::IsPathRenderingDrawType(drawType)); |
| 298 | const GrRenderTarget* rt = optState.getRenderTarget(); |
| 299 | SkISize size; |
| 300 | size.set(rt->width(), rt->height()); |
| 301 | fGpu->glPathRendering()->setProjectionMatrix(optState.getViewMatrix(), size, rt->origin()); |
| 302 | } |
| 303 | |
| 304 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 305 | |
| 306 | GrGLNvprProgram::GrGLNvprProgram(GrGpuGL* gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 307 | const GrProgramDesc& desc, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 308 | const BuiltinUniformHandles& builtinUniforms, |
| 309 | GrGLuint programID, |
| 310 | const UniformInfoArray& uniforms, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 311 | GrGLInstalledFragProcs* fragmentProcessors, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 312 | const SeparableVaryingInfoArray& separableVaryings) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 313 | : INHERITED(gpu, desc, builtinUniforms, programID, uniforms, fragmentProcessors) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 314 | int count = separableVaryings.count(); |
| 315 | fVaryings.push_back_n(count); |
| 316 | for (int i = 0; i < count; i++) { |
| 317 | Varying& varying = fVaryings[i]; |
| 318 | const SeparableVaryingInfo& builderVarying = separableVaryings[i]; |
| 319 | SkASSERT(GrGLShaderVar::kNonArray == builderVarying.fVariable.getArrayCount()); |
| 320 | SkDEBUGCODE( |
| 321 | varying.fType = builderVarying.fVariable.getType(); |
| 322 | ); |
| 323 | varying.fLocation = builderVarying.fLocation; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void GrGLNvprProgram::didSetData(GrGpu::DrawType drawType) { |
| 328 | SkASSERT(GrGpu::IsPathRenderingDrawType(drawType)); |
| 329 | } |
| 330 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 331 | void GrGLNvprProgram::setTransformData(const GrPendingFragmentStage& proc, |
| 332 | GrGLInstalledFragProc* ip) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 333 | SkTArray<GrGLInstalledFragProc::Transform, true>& transforms = ip->fTransforms; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 334 | int numTransforms = transforms.count(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 335 | SkASSERT(numTransforms == proc.getProcessor()->numTransforms()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 336 | for (int t = 0; t < numTransforms; ++t) { |
| 337 | SkASSERT(transforms[t].fHandle.isValid()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 338 | const SkMatrix& transform = get_transform_matrix(proc, false, t); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 339 | if (transforms[t].fCurrentValue.cheapEqualTo(transform)) { |
| 340 | continue; |
| 341 | } |
| 342 | transforms[t].fCurrentValue = transform; |
| 343 | const Varying& fragmentInput = fVaryings[transforms[t].fHandle.handle()]; |
| 344 | SkASSERT(transforms[t].fType == kVec2f_GrSLType || transforms[t].fType == kVec3f_GrSLType); |
| 345 | unsigned components = transforms[t].fType == kVec2f_GrSLType ? 2 : 3; |
| 346 | fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID, |
| 347 | fragmentInput.fLocation, |
| 348 | GR_GL_OBJECT_LINEAR, |
| 349 | components, |
| 350 | transform); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ////////////////////////////////////////////////////////////////////////////////////// |
| 355 | |
| 356 | GrGLLegacyNvprProgram::GrGLLegacyNvprProgram(GrGpuGL* gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 357 | const GrProgramDesc& desc, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 358 | const BuiltinUniformHandles& builtinUniforms, |
| 359 | GrGLuint programID, |
| 360 | const UniformInfoArray& uniforms, |
| 361 | GrGLInstalledFragProcs* fps, |
| 362 | int texCoordSetCnt) |
| 363 | : INHERITED(gpu, desc, builtinUniforms, programID, uniforms, fps) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 364 | , fTexCoordSetCnt(texCoordSetCnt) { |
| 365 | } |
| 366 | |
| 367 | void GrGLLegacyNvprProgram::didSetData(GrGpu::DrawType drawType) { |
| 368 | SkASSERT(GrGpu::IsPathRenderingDrawType(drawType)); |
| 369 | fGpu->glPathRendering()->flushPathTexGenSettings(fTexCoordSetCnt); |
| 370 | } |
| 371 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 372 | void |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 373 | GrGLLegacyNvprProgram::setTransformData(const GrPendingFragmentStage& proc, |
| 374 | GrGLInstalledFragProc* ip) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 375 | // We've hidden the texcoord index in the first entry of the transforms array for each effect |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 376 | int texCoordIndex = ip->fTransforms[0].fHandle.handle(); |
| 377 | int numTransforms = proc.getProcessor()->numTransforms(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 378 | for (int t = 0; t < numTransforms; ++t) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 379 | const SkMatrix& transform = get_transform_matrix(proc, false, t); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 380 | GrGLPathRendering::PathTexGenComponents components = |
| 381 | GrGLPathRendering::kST_PathTexGenComponents; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 382 | if (proc.isPerspectiveCoordTransform(t)) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 383 | components = GrGLPathRendering::kSTR_PathTexGenComponents; |
| 384 | } |
| 385 | fGpu->glPathRendering()->enablePathTexGen(texCoordIndex++, components, transform); |
| 386 | } |
| 387 | } |