blob: bc7dd60c7cb08ddf8f2f963c9031508ec7b619f3 [file] [log] [blame]
wangyixb1daa862015-08-18 11:29:31 -07001/*
2 * Copyright 2015 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.
6 */
7
Ethan Nicholasd4efe682019-08-29 16:10:13 -04008#include "src/gpu/GrCoordTransform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrFragmentProcessor.h"
10#include "src/gpu/GrProcessor.h"
11#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
12#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13#include "src/gpu/glsl/GrGLSLUniformHandler.h"
wangyixb1daa862015-08-18 11:29:31 -070014
egdaniel64c47282015-11-13 06:54:19 -080015void GrGLSLFragmentProcessor::setData(const GrGLSLProgramDataManager& pdman,
16 const GrFragmentProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -070017 this->onSetData(pdman, processor);
wangyixb1daa862015-08-18 11:29:31 -070018}
wangyix2a378432015-08-18 12:00:12 -070019
Brian Osman978693c2020-01-24 14:52:10 -050020SkString GrGLSLFragmentProcessor::invokeChild(int childIndex, const char* inputColor,
21 EmitArgs& args, SkSL::String skslCoords) {
cdalton85285412016-02-18 12:37:07 -080022 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040023 while (childIndex >= (int) fFunctionNames.size()) {
24 fFunctionNames.emplace_back();
25 }
Brian Osman978693c2020-01-24 14:52:10 -050026
27 // Subtle bug workaround: If an FP (this) has a child, and wishes to sample it, but does not
28 // want to *force* explicit coord sampling, then the obvious solution is to call it with
29 // invokeChild and no coords. However, if this FP is then adopted as a child of another FP that
30 // does want to sample with explicit coords, that property is propagated (recursively) to all
31 // children, and we need to supply explicit coords. So we propagate our own "_coords" (this is
32 // the name of our explicit coords parameter generated in the helper function).
Brian Salomon7d8b3972019-11-26 22:34:44 -050033 if (!args.fFp.coordTransformsApplyToLocalCoords() && skslCoords.length() == 0) {
Ethan Nicholasd4efe682019-08-29 16:10:13 -040034 skslCoords = "_coords";
35 }
Brian Osman978693c2020-01-24 14:52:10 -050036
37 const GrFragmentProcessor& childProc = args.fFp.childProcessor(childIndex);
38
39 // Emit the child's helper function if this is the first time we've seen a call
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040040 if (fFunctionNames[childIndex].size() == 0) {
Brian Osman978693c2020-01-24 14:52:10 -050041 fragBuilder->onBeforeChildProcEmitCode(); // call first so mangleString is updated
Ethan Nicholasd4efe682019-08-29 16:10:13 -040042
43 TransformedCoordVars coordVars = args.fTransformedCoords.childInputs(childIndex);
44 TextureSamplers textureSamplers = args.fTexSamplers.childInputs(childIndex);
Brian Osman978693c2020-01-24 14:52:10 -050045
Ethan Nicholasd4efe682019-08-29 16:10:13 -040046 EmitArgs childArgs(fragBuilder,
47 args.fUniformHandler,
48 args.fShaderCaps,
49 childProc,
Brian Osman978693c2020-01-24 14:52:10 -050050 "_output",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040051 "_input",
52 coordVars,
53 textureSamplers);
Brian Osman978693c2020-01-24 14:52:10 -050054 fFunctionNames[childIndex] =
55 fragBuilder->writeProcessorFunction(this->childProcessor(childIndex), childArgs);
Ethan Nicholas6ad52892019-05-03 13:13:42 +000056
Brian Osman978693c2020-01-24 14:52:10 -050057 fragBuilder->onAfterChildProcEmitCode();
Ethan Nicholas6ad52892019-05-03 13:13:42 +000058 }
bsalomon38ddbad2015-09-24 06:00:00 -070059
Brian Osman978693c2020-01-24 14:52:10 -050060 // If the fragment processor is invoked with overridden coordinates, it must *always* be invoked
61 // with overridden coords.
62 SkASSERT(childProc.coordTransformsApplyToLocalCoords() == (skslCoords.length() == 0));
Michael Ludwig231de032018-08-30 14:33:01 -040063
Brian Osman978693c2020-01-24 14:52:10 -050064 // Produce a string containing the call to the helper function
65 SkString result = SkStringPrintf("%s(%s", fFunctionNames[childIndex].c_str(),
66 inputColor ? inputColor : "half4(1)");
67 if (skslCoords.length()) {
68 result.appendf(", %s", skslCoords.c_str());
69 }
70 result.append(")");
71 return result;
wangyix2a378432015-08-18 12:00:12 -070072}
bsalomona624bf32016-09-20 09:12:47 -070073
74//////////////////////////////////////////////////////////////////////////////
75
Brian Salomonc241b582019-11-27 08:57:17 -050076GrGLSLFragmentProcessor::Iter::Iter(std::unique_ptr<GrGLSLFragmentProcessor> fps[], int cnt) {
77 for (int i = cnt - 1; i >= 0; --i) {
78 fFPStack.push_back(fps[i].get());
bsalomona624bf32016-09-20 09:12:47 -070079 }
Brian Salomonc241b582019-11-27 08:57:17 -050080}
81
82GrGLSLFragmentProcessor& GrGLSLFragmentProcessor::Iter::operator*() const {
83 return *fFPStack.back();
84}
85
86GrGLSLFragmentProcessor* GrGLSLFragmentProcessor::Iter::operator->() const {
87 return fFPStack.back();
88}
89
90GrGLSLFragmentProcessor::Iter& GrGLSLFragmentProcessor::Iter::operator++() {
91 SkASSERT(!fFPStack.empty());
92 const GrGLSLFragmentProcessor* back = fFPStack.back();
bsalomonb58a2b42016-09-26 06:55:02 -070093 fFPStack.pop_back();
94 for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
95 fFPStack.push_back(back->childProcessor(i));
96 }
Brian Salomonc241b582019-11-27 08:57:17 -050097 return *this;
bsalomona624bf32016-09-20 09:12:47 -070098}