1 /*
2 * Copyright 2014 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
8 #include "effects/GrDisableColorXP.h"
9 #include "GrPipeline.h"
10 #include "GrProcessor.h"
11 #include "glsl/GrGLSLFragmentShaderBuilder.h"
12 #include "glsl/GrGLSLProgramDataManager.h"
13 #include "glsl/GrGLSLXferProcessor.h"
14
15 /**
16 * This xfer processor disables color writing. Thus color and coverage and ignored and no blending
17 * occurs. This XP is usful for things like stenciling.
18 */
19 class DisableColorXP : public GrXferProcessor {
20 public:
Create()21 static GrXferProcessor* Create() { return new DisableColorXP; }
22
~DisableColorXP()23 ~DisableColorXP() override {}
24
name() const25 const char* name() const override { return "Disable Color"; }
26
27 GrGLSLXferProcessor* createGLSLInstance() const override;
28
29 private:
30 DisableColorXP();
31
onGetOptimizations(const FragmentProcessorAnalysis &) const32 GrXferProcessor::OptFlags onGetOptimizations(const FragmentProcessorAnalysis&) const override {
33 return GrXferProcessor::kIgnoreColor_OptFlag;
34 }
35
36 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
37
38 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override;
39
onIsEqual(const GrXferProcessor & xpBase) const40 bool onIsEqual(const GrXferProcessor& xpBase) const override {
41 return true;
42 }
43
44 typedef GrXferProcessor INHERITED;
45 };
46
47 ///////////////////////////////////////////////////////////////////////////////
48
49 class GLDisableColorXP : public GrGLSLXferProcessor {
50 public:
GLDisableColorXP(const GrProcessor &)51 GLDisableColorXP(const GrProcessor&) {}
52
~GLDisableColorXP()53 ~GLDisableColorXP() override {}
54
GenKey(const GrProcessor &,const GrShaderCaps &,GrProcessorKeyBuilder *)55 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
56
57 private:
emitOutputsForBlendState(const EmitArgs & args)58 void emitOutputsForBlendState(const EmitArgs& args) override {
59 // This emit code should be empty. However, on the nexus 6 there is a driver bug where if
60 // you do not give gl_FragColor a value, the gl context is lost and we end up drawing
61 // nothing. So this fix just sets the gl_FragColor arbitrarily to 0.
62 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
63 fragBuilder->codeAppendf("%s = vec4(0);", args.fOutputPrimary);
64 }
65
onSetData(const GrGLSLProgramDataManager &,const GrXferProcessor &)66 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
67
68 typedef GrGLSLXferProcessor INHERITED;
69 };
70
71 ///////////////////////////////////////////////////////////////////////////////
72
DisableColorXP()73 DisableColorXP::DisableColorXP() {
74 this->initClassID<DisableColorXP>();
75 }
76
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const77 void DisableColorXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
78 GLDisableColorXP::GenKey(*this, caps, b);
79 }
80
createGLSLInstance() const81 GrGLSLXferProcessor* DisableColorXP::createGLSLInstance() const { return new GLDisableColorXP(*this); }
82
onGetBlendInfo(GrXferProcessor::BlendInfo * blendInfo) const83 void DisableColorXP::onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const {
84 blendInfo->fWriteColor = false;
85 }
86
87 ///////////////////////////////////////////////////////////////////////////////
onCreateXferProcessor(const GrCaps & caps,const FragmentProcessorAnalysis & analysis,bool hasMixedSamples,const DstTexture * dst) const88 GrXferProcessor* GrDisableColorXPFactory::onCreateXferProcessor(
89 const GrCaps& caps,
90 const FragmentProcessorAnalysis& analysis,
91 bool hasMixedSamples,
92 const DstTexture* dst) const {
93 return DisableColorXP::Create();
94 }
95
96 GR_DEFINE_XP_FACTORY_TEST(GrDisableColorXPFactory);
97
98 #if GR_TEST_UTILS
TestGet(GrProcessorTestData *)99 const GrXPFactory* GrDisableColorXPFactory::TestGet(GrProcessorTestData*) {
100 return GrDisableColorXPFactory::Get();
101 }
102 #endif
103