GrFragmentProcessor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  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. #include "src/gpu/GrCoordTransform.h"
  8. #include "src/gpu/GrFragmentProcessor.h"
  9. #include "src/gpu/GrPipeline.h"
  10. #include "src/gpu/GrProcessorAnalysis.h"
  11. #include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
  12. #include "src/gpu/effects/generated/GrConstColorProcessor.h"
  13. #include "src/gpu/effects/generated/GrOverrideInputFragmentProcessor.h"
  14. #include "src/gpu/effects/generated/GrPremulInputFragmentProcessor.h"
  15. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  16. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  17. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  18. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  19. bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
  20. if (this->classID() != that.classID()) {
  21. return false;
  22. }
  23. if (this->numTextureSamplers() != that.numTextureSamplers()) {
  24. return false;
  25. }
  26. for (int i = 0; i < this->numTextureSamplers(); ++i) {
  27. if (this->textureSampler(i) != that.textureSampler(i)) {
  28. return false;
  29. }
  30. }
  31. if (!this->hasSameTransforms(that)) {
  32. return false;
  33. }
  34. if (!this->onIsEqual(that)) {
  35. return false;
  36. }
  37. if (this->numChildProcessors() != that.numChildProcessors()) {
  38. return false;
  39. }
  40. for (int i = 0; i < this->numChildProcessors(); ++i) {
  41. if (!this->childProcessor(i).isEqual(that.childProcessor(i))) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. void GrFragmentProcessor::visitProxies(const GrOp::VisitProxyFunc& func) {
  48. GrFragmentProcessor::TextureAccessIter iter(this);
  49. while (const TextureSampler* sampler = iter.next()) {
  50. bool mipped = (GrSamplerState::Filter::kMipMap == sampler->samplerState().filter());
  51. func(sampler->proxy(), GrMipMapped(mipped));
  52. }
  53. }
  54. GrGLSLFragmentProcessor* GrFragmentProcessor::createGLSLInstance() const {
  55. GrGLSLFragmentProcessor* glFragProc = this->onCreateGLSLInstance();
  56. glFragProc->fChildProcessors.push_back_n(fChildProcessors.count());
  57. for (int i = 0; i < fChildProcessors.count(); ++i) {
  58. glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLSLInstance();
  59. }
  60. return glFragProc;
  61. }
  62. const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::textureSampler(int i) const {
  63. SkASSERT(i >= 0 && i < fTextureSamplerCnt);
  64. return this->onTextureSampler(i);
  65. }
  66. void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
  67. fCoordTransforms.push_back(transform);
  68. fFlags |= kUsesLocalCoords_Flag;
  69. SkDEBUGCODE(transform->setInProcessor();)
  70. }
  71. #ifdef SK_DEBUG
  72. bool GrFragmentProcessor::isInstantiated() const {
  73. for (int i = 0; i < fTextureSamplerCnt; ++i) {
  74. if (!this->textureSampler(i).isInstantiated()) {
  75. return false;
  76. }
  77. }
  78. for (int i = 0; i < this->numChildProcessors(); ++i) {
  79. if (!this->childProcessor(i).isInstantiated()) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. #endif
  86. int GrFragmentProcessor::registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child) {
  87. if (child->usesLocalCoords()) {
  88. fFlags |= kUsesLocalCoords_Flag;
  89. }
  90. fRequestedFeatures |= child->fRequestedFeatures;
  91. int index = fChildProcessors.count();
  92. fChildProcessors.push_back(std::move(child));
  93. return index;
  94. }
  95. bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
  96. if (this->numCoordTransforms() != that.numCoordTransforms()) {
  97. return false;
  98. }
  99. int count = this->numCoordTransforms();
  100. for (int i = 0; i < count; ++i) {
  101. if (!this->coordTransform(i).hasSameEffectAs(that.coordTransform(i))) {
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulChildByInputAlpha(
  108. std::unique_ptr<GrFragmentProcessor> fp) {
  109. if (!fp) {
  110. return nullptr;
  111. }
  112. return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kDstIn);
  113. }
  114. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
  115. std::unique_ptr<GrFragmentProcessor> fp) {
  116. if (!fp) {
  117. return nullptr;
  118. }
  119. return GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(fp), SkBlendMode::kSrcIn);
  120. }
  121. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::PremulInput(
  122. std::unique_ptr<GrFragmentProcessor> fp) {
  123. if (!fp) {
  124. return nullptr;
  125. }
  126. std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { GrPremulInputFragmentProcessor::Make(),
  127. std::move(fp) };
  128. return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
  129. }
  130. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
  131. std::unique_ptr<GrFragmentProcessor> fp, const GrSwizzle& swizzle) {
  132. class SwizzleFragmentProcessor : public GrFragmentProcessor {
  133. public:
  134. static std::unique_ptr<GrFragmentProcessor> Make(const GrSwizzle& swizzle) {
  135. return std::unique_ptr<GrFragmentProcessor>(new SwizzleFragmentProcessor(swizzle));
  136. }
  137. const char* name() const override { return "Swizzle"; }
  138. const GrSwizzle& swizzle() const { return fSwizzle; }
  139. std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(fSwizzle); }
  140. private:
  141. SwizzleFragmentProcessor(const GrSwizzle& swizzle)
  142. : INHERITED(kSwizzleFragmentProcessor_ClassID, kAll_OptimizationFlags)
  143. , fSwizzle(swizzle) {}
  144. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
  145. class GLFP : public GrGLSLFragmentProcessor {
  146. public:
  147. void emitCode(EmitArgs& args) override {
  148. const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
  149. const GrSwizzle& swizzle = sfp.swizzle();
  150. GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
  151. fragBuilder->codeAppendf("%s = %s.%s;",
  152. args.fOutputColor, args.fInputColor, swizzle.c_str());
  153. }
  154. };
  155. return new GLFP;
  156. }
  157. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
  158. b->add32(fSwizzle.asKey());
  159. }
  160. bool onIsEqual(const GrFragmentProcessor& other) const override {
  161. const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
  162. return fSwizzle == sfp.fSwizzle;
  163. }
  164. SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
  165. return fSwizzle.applyTo(input);
  166. }
  167. GrSwizzle fSwizzle;
  168. typedef GrFragmentProcessor INHERITED;
  169. };
  170. if (!fp) {
  171. return nullptr;
  172. }
  173. if (GrSwizzle::RGBA() == swizzle) {
  174. return fp;
  175. }
  176. std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp),
  177. SwizzleFragmentProcessor::Make(swizzle) };
  178. return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
  179. }
  180. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeInputPremulAndMulByOutput(
  181. std::unique_ptr<GrFragmentProcessor> fp) {
  182. class PremulFragmentProcessor : public GrFragmentProcessor {
  183. public:
  184. static std::unique_ptr<GrFragmentProcessor> Make(
  185. std::unique_ptr<GrFragmentProcessor> processor) {
  186. return std::unique_ptr<GrFragmentProcessor>(
  187. new PremulFragmentProcessor(std::move(processor)));
  188. }
  189. const char* name() const override { return "Premultiply"; }
  190. std::unique_ptr<GrFragmentProcessor> clone() const override {
  191. return Make(this->childProcessor(0).clone());
  192. }
  193. private:
  194. PremulFragmentProcessor(std::unique_ptr<GrFragmentProcessor> processor)
  195. : INHERITED(kPremulFragmentProcessor_ClassID, OptFlags(processor.get())) {
  196. this->registerChildProcessor(std::move(processor));
  197. }
  198. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
  199. class GLFP : public GrGLSLFragmentProcessor {
  200. public:
  201. void emitCode(EmitArgs& args) override {
  202. GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
  203. this->emitChild(0, args);
  204. fragBuilder->codeAppendf("%s.rgb *= %s.rgb;", args.fOutputColor,
  205. args.fInputColor);
  206. fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
  207. }
  208. };
  209. return new GLFP;
  210. }
  211. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
  212. bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
  213. static OptimizationFlags OptFlags(const GrFragmentProcessor* inner) {
  214. OptimizationFlags flags = kNone_OptimizationFlags;
  215. if (inner->preservesOpaqueInput()) {
  216. flags |= kPreservesOpaqueInput_OptimizationFlag;
  217. }
  218. if (inner->hasConstantOutputForConstantInput()) {
  219. flags |= kConstantOutputForConstantInput_OptimizationFlag;
  220. }
  221. return flags;
  222. }
  223. SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
  224. SkPMColor4f childColor = ConstantOutputForConstantInput(this->childProcessor(0),
  225. SK_PMColor4fWHITE);
  226. SkPMColor4f premulInput = SkColor4f{ input.fR, input.fG, input.fB, input.fA }.premul();
  227. return premulInput * childColor;
  228. }
  229. typedef GrFragmentProcessor INHERITED;
  230. };
  231. if (!fp) {
  232. return nullptr;
  233. }
  234. return PremulFragmentProcessor::Make(std::move(fp));
  235. }
  236. //////////////////////////////////////////////////////////////////////////////
  237. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
  238. std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color, bool useUniform) {
  239. if (!fp) {
  240. return nullptr;
  241. }
  242. return GrOverrideInputFragmentProcessor::Make(std::move(fp), color, useUniform);
  243. }
  244. std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::RunInSeries(
  245. std::unique_ptr<GrFragmentProcessor>* series, int cnt) {
  246. class SeriesFragmentProcessor : public GrFragmentProcessor {
  247. public:
  248. static std::unique_ptr<GrFragmentProcessor> Make(
  249. std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
  250. return std::unique_ptr<GrFragmentProcessor>(new SeriesFragmentProcessor(children, cnt));
  251. }
  252. const char* name() const override { return "Series"; }
  253. std::unique_ptr<GrFragmentProcessor> clone() const override {
  254. SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> children(this->numChildProcessors());
  255. for (int i = 0; i < this->numChildProcessors(); ++i) {
  256. if (!children.push_back(this->childProcessor(i).clone())) {
  257. return nullptr;
  258. }
  259. }
  260. return Make(children.begin(), this->numChildProcessors());
  261. }
  262. private:
  263. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
  264. class GLFP : public GrGLSLFragmentProcessor {
  265. public:
  266. void emitCode(EmitArgs& args) override {
  267. // First guy's input might be nil.
  268. SkString temp("out0");
  269. this->emitChild(0, args.fInputColor, &temp, args);
  270. SkString input = temp;
  271. for (int i = 1; i < this->numChildProcessors() - 1; ++i) {
  272. temp.printf("out%d", i);
  273. this->emitChild(i, input.c_str(), &temp, args);
  274. input = temp;
  275. }
  276. // Last guy writes to our output variable.
  277. this->emitChild(this->numChildProcessors() - 1, input.c_str(), args);
  278. }
  279. };
  280. return new GLFP;
  281. }
  282. SeriesFragmentProcessor(std::unique_ptr<GrFragmentProcessor>* children, int cnt)
  283. : INHERITED(kSeriesFragmentProcessor_ClassID, OptFlags(children, cnt)) {
  284. SkASSERT(cnt > 1);
  285. for (int i = 0; i < cnt; ++i) {
  286. this->registerChildProcessor(std::move(children[i]));
  287. }
  288. }
  289. static OptimizationFlags OptFlags(std::unique_ptr<GrFragmentProcessor>* children, int cnt) {
  290. OptimizationFlags flags = kAll_OptimizationFlags;
  291. for (int i = 0; i < cnt && flags != kNone_OptimizationFlags; ++i) {
  292. flags &= children[i]->optimizationFlags();
  293. }
  294. return flags;
  295. }
  296. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
  297. bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
  298. SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
  299. SkPMColor4f color = inColor;
  300. int childCnt = this->numChildProcessors();
  301. for (int i = 0; i < childCnt; ++i) {
  302. color = ConstantOutputForConstantInput(this->childProcessor(i), color);
  303. }
  304. return color;
  305. }
  306. typedef GrFragmentProcessor INHERITED;
  307. };
  308. if (!cnt) {
  309. return nullptr;
  310. }
  311. if (1 == cnt) {
  312. return std::move(series[0]);
  313. }
  314. // Run the through the series, do the invariant output processing, and look for eliminations.
  315. GrProcessorAnalysisColor inputColor;
  316. inputColor.setToUnknown();
  317. GrColorFragmentProcessorAnalysis info(inputColor, unique_ptr_address_as_pointer_address(series),
  318. cnt);
  319. SkTArray<std::unique_ptr<GrFragmentProcessor>> replacementSeries;
  320. SkPMColor4f knownColor;
  321. int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
  322. if (leadingFPsToEliminate) {
  323. std::unique_ptr<GrFragmentProcessor> colorFP(
  324. GrConstColorProcessor::Make(knownColor, GrConstColorProcessor::InputMode::kIgnore));
  325. if (leadingFPsToEliminate == cnt) {
  326. return colorFP;
  327. }
  328. cnt = cnt - leadingFPsToEliminate + 1;
  329. replacementSeries.reserve(cnt);
  330. replacementSeries.emplace_back(std::move(colorFP));
  331. for (int i = 0; i < cnt - 1; ++i) {
  332. replacementSeries.emplace_back(std::move(series[leadingFPsToEliminate + i]));
  333. }
  334. series = replacementSeries.begin();
  335. }
  336. return SeriesFragmentProcessor::Make(series, cnt);
  337. }
  338. //////////////////////////////////////////////////////////////////////////////
  339. GrFragmentProcessor::Iter::Iter(const GrPipeline& pipeline) {
  340. for (int i = pipeline.numFragmentProcessors() - 1; i >= 0; --i) {
  341. fFPStack.push_back(&pipeline.getFragmentProcessor(i));
  342. }
  343. }
  344. GrFragmentProcessor::Iter::Iter(const GrPaint& paint) {
  345. for (int i = paint.numCoverageFragmentProcessors() - 1; i >= 0; --i) {
  346. fFPStack.push_back(paint.getCoverageFragmentProcessor(i));
  347. }
  348. for (int i = paint.numColorFragmentProcessors() - 1; i >= 0; --i) {
  349. fFPStack.push_back(paint.getColorFragmentProcessor(i));
  350. }
  351. }
  352. const GrFragmentProcessor* GrFragmentProcessor::Iter::next() {
  353. if (fFPStack.empty()) {
  354. return nullptr;
  355. }
  356. const GrFragmentProcessor* back = fFPStack.back();
  357. fFPStack.pop_back();
  358. for (int i = back->numChildProcessors() - 1; i >= 0; --i) {
  359. fFPStack.push_back(&back->childProcessor(i));
  360. }
  361. return back;
  362. }
  363. ///////////////////////////////////////////////////////////////////////////////////////////////////
  364. GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
  365. const GrSamplerState& samplerState) {
  366. this->reset(std::move(proxy), samplerState);
  367. }
  368. GrFragmentProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
  369. GrSamplerState::Filter filterMode,
  370. GrSamplerState::WrapMode wrapXAndY) {
  371. this->reset(std::move(proxy), filterMode, wrapXAndY);
  372. }
  373. void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
  374. const GrSamplerState& samplerState) {
  375. fProxy = std::move(proxy);
  376. fSamplerState = samplerState;
  377. fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
  378. }
  379. void GrFragmentProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
  380. GrSamplerState::Filter filterMode,
  381. GrSamplerState::WrapMode wrapXAndY) {
  382. fProxy = std::move(proxy);
  383. filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
  384. fSamplerState = GrSamplerState(wrapXAndY, filterMode);
  385. }