Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/gpu/effects/GrMatrixEffect.h" |
| 9 | |
| 10 | #include "src/gpu/GrTexture.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 11 | #include "src/gpu/effects/GrTextureEffect.h" |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLUtil.h" |
| 16 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 17 | class GrGLSLMatrixEffect : public GrFragmentProcessor::ProgramImpl { |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 18 | public: |
| 19 | GrGLSLMatrixEffect() {} |
| 20 | |
| 21 | void emitCode(EmitArgs& args) override { |
Brian Osman | b1e9cb0 | 2021-05-21 12:03:51 -0400 | [diff] [blame] | 22 | fMatrixVar = args.fUniformHandler->addUniform(&args.fFp, |
| 23 | kFragment_GrShaderFlag, |
| 24 | kFloat3x3_GrSLType, |
| 25 | SkSL::SampleUsage::MatrixUniformName()); |
John Stiles | 52e9a7a | 2020-09-30 14:20:36 -0400 | [diff] [blame] | 26 | args.fFragBuilder->codeAppendf("return %s;\n", |
| 27 | this->invokeChildWithMatrix(0, args).c_str()); |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | private: |
| 31 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 32 | const GrFragmentProcessor& proc) override { |
| 33 | const GrMatrixEffect& mtx = proc.cast<GrMatrixEffect>(); |
Brian Salomon | 816db16 | 2021-04-16 08:39:35 -0400 | [diff] [blame] | 34 | if (auto te = mtx.childProcessor(0)->asTextureEffect()) { |
| 35 | SkMatrix m = te->coordAdjustmentMatrix(); |
| 36 | m.preConcat(mtx.matrix()); |
| 37 | pdman.setSkMatrix(fMatrixVar, m); |
| 38 | } else { |
| 39 | pdman.setSkMatrix(fMatrixVar, mtx.matrix()); |
| 40 | } |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | UniformHandle fMatrixVar; |
| 44 | }; |
| 45 | |
Brian Salomon | 4bbb290 | 2021-04-13 10:04:20 -0400 | [diff] [blame] | 46 | std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::Make( |
| 47 | const SkMatrix& matrix, std::unique_ptr<GrFragmentProcessor> child) { |
Brian Salomon | 4bbb290 | 2021-04-13 10:04:20 -0400 | [diff] [blame] | 48 | if (child->classID() == kGrMatrixEffect_ClassID) { |
| 49 | auto me = static_cast<GrMatrixEffect*>(child.get()); |
| 50 | // registerChild's sample usage records whether the matrix used has perspective or not, |
| 51 | // so we can't add perspective to 'me' if it doesn't already have it. |
| 52 | if (me->matrix().hasPerspective() || !matrix.hasPerspective()) { |
| 53 | me->fMatrix.preConcat(matrix); |
| 54 | return child; |
| 55 | } |
| 56 | } |
| 57 | return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(matrix, std::move(child))); |
| 58 | } |
| 59 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 60 | std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrMatrixEffect::onMakeProgramImpl() const { |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 61 | return std::make_unique<GrGLSLMatrixEffect>(); |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 64 | void GrMatrixEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {} |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 65 | |
| 66 | bool GrMatrixEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 67 | const GrMatrixEffect& that = other.cast<GrMatrixEffect>(); |
| 68 | if (fMatrix != that.fMatrix) return false; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | GrMatrixEffect::GrMatrixEffect(const GrMatrixEffect& src) |
John Stiles | 307f8f5 | 2021-08-09 15:36:59 -0400 | [diff] [blame^] | 73 | : INHERITED(src) |
| 74 | , fMatrix(src.fMatrix) {} |
Brian Osman | 273944a | 2020-05-29 16:44:26 -0400 | [diff] [blame] | 75 | |
| 76 | std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::clone() const { |
| 77 | return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(*this)); |
| 78 | } |