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#ifndef GrGLSLFragmentShaderBuilder_DEFINED 9#define GrGLSLFragmentShaderBuilder_DEFINED 10 11#include "src/gpu/GrBlend.h" 12#include "src/gpu/GrFragmentProcessor.h" 13#include "src/gpu/GrProcessor.h" 14#include "src/gpu/glsl/GrGLSLShaderBuilder.h" 15 16class GrRenderTarget; 17class GrGLSLVarying; 18 19/* 20 * This class is used by fragment processors to build their fragment code. 21 */ 22class GrGLSLFPFragmentBuilder : virtual public GrGLSLShaderBuilder { 23public: 24 /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */ 25 GrGLSLFPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) { 26 // Suppress unused warning error 27 (void) fPadding; 28 } 29 30 enum class ScopeFlags { 31 // Every fragment will always execute this code, and will do it exactly once. 32 kTopLevel = 0, 33 // Either all fragments in a given primitive, or none, will execute this code. 34 kInsidePerPrimitiveBranch = (1 << 0), 35 // Any given fragment may or may not execute this code. 36 kInsidePerPixelBranch = (1 << 1), 37 // This code will be executed more than once. 38 kInsideLoop = (1 << 2) 39 }; 40 41 virtual void forceHighPrecision() = 0; 42 43 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr 44 * if no effect advertised that it will read the destination. */ 45 virtual const char* dstColor() = 0; 46 47private: 48 // WARNING: LIke GrRenderTargetProxy, changes to this can cause issues in ASAN. This is caused 49 // by GrGLSLProgramBuilder's SkTBlockLists requiring 16 byte alignment, but since 50 // GrGLSLFragmentShaderBuilder has a virtual diamond hierarchy, ASAN requires all this pointers 51 // to start aligned, even though clang is already correctly offsetting the individual fields 52 // that require the larger alignment. In the current world, this extra padding is sufficient to 53 // correctly initialize GrGLSLXPFragmentBuilder second. 54 char fPadding[4] = {}; 55}; 56 57GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags) 58 59/* 60 * This class is used by Xfer processors to build their fragment code. 61 */ 62class GrGLSLXPFragmentBuilder : virtual public GrGLSLShaderBuilder { 63public: 64 /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */ 65 GrGLSLXPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {} 66 67 virtual bool hasCustomColorOutput() const = 0; 68 virtual bool hasSecondaryOutput() const = 0; 69 70 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr 71 * if no effect advertised that it will read the destination. */ 72 virtual const char* dstColor() = 0; 73 74 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with 75 this shader. It is only legal to call this method with an advanced blend equation, and only 76 if these equations are supported. */ 77 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; 78}; 79 80/* 81 * This class implements the various fragment builder interfaces. 82 */ 83class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder { 84public: 85 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program); 86 87 // Shared FP/XP interface. 88 const char* dstColor() override; 89 90 // GrGLSLFPFragmentBuilder interface. 91 void forceHighPrecision() override { fForceHighPrecision = true; } 92 93 // GrGLSLXPFragmentBuilder interface. 94 bool hasCustomColorOutput() const override { return SkToBool(fCustomColorOutput); } 95 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; } 96 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; 97 98private: 99 // Private public interface, used by GrGLProgramBuilder to build a fragment shader 100 void enableCustomOutput(); 101 void enableSecondaryOutput(); 102 const char* getPrimaryColorOutputName() const; 103 const char* getSecondaryColorOutputName() const; 104 bool primaryColorOutputIsInOut() const; 105 106#ifdef SK_DEBUG 107 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below 108 // state to track this. The reset call is called per processor emitted. 109 bool fHasReadDstColorThisStage_DebugOnly = false; 110 111 void debugOnly_resetPerStageVerification() { 112 fHasReadDstColorThisStage_DebugOnly = false; 113 } 114#endif 115 116 static const char* DeclaredColorOutputName() { return "sk_FragColor"; } 117 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; } 118 119 GrSurfaceOrigin getSurfaceOrigin() const; 120 121 void onFinalize() override; 122 123 inline static constexpr const char kDstColorName[] = "_dstColor"; 124 125 GrShaderVar* fCustomColorOutput = nullptr; 126 127 bool fSetupFragPosition = false; 128 bool fHasSecondaryOutput = false; 129 bool fHasModifiedSampleMask = false; 130 bool fForceHighPrecision = false; 131 132 friend class GrGLSLProgramBuilder; 133 friend class GrGLProgramBuilder; 134 friend class GrVkPipelineStateBuilder; 135}; 136 137#endif 138