blob: 38d569e734189f552713d4ba07160ba2672ab897 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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 GrGLFragmentShaderBuilder_DEFINED
9#define GrGLFragmentShaderBuilder_DEFINED
10#include "GrGLShaderBuilder.h"
11
joshualittdb0d3ca2014-10-07 12:42:26 -070012class GrGLProgramBuilder;
13
joshualittb0a8a372014-09-23 09:50:21 -070014/*
joshualittdb0d3ca2014-10-07 12:42:26 -070015 * This base class encapsulates the functionality which all GrProcessors are allowed to use in their
16 * fragment shader
joshualittb0a8a372014-09-23 09:50:21 -070017 */
joshualittdb0d3ca2014-10-07 12:42:26 -070018class GrGLProcessorFragmentShaderBuilder : public GrGLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070019public:
joshualittdb0d3ca2014-10-07 12:42:26 -070020 GrGLProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
21 virtual ~GrGLProcessorFragmentShaderBuilder() {}
joshualittb0a8a372014-09-23 09:50:21 -070022 /**
23 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
24 * if code is added that uses one of these features without calling enableFeature()
25 */
26 enum GLSLFeature {
27 kStandardDerivatives_GLSLFeature = 0,
28 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
29 };
30
31 /**
32 * If the feature is supported then true is returned and any necessary #extension declarations
33 * are added to the shaders. If the feature is not supported then false will be returned.
34 */
35 virtual bool enableFeature(GLSLFeature) = 0;
36
37 /**
38 * This returns a variable name to access the 2D, perspective correct version of the coords in
39 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
40 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
41 */
42 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
43 int index) = 0;
44
45
46 /** Returns a variable name that represents the position of the fragment in the FS. The position
47 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
48 virtual const char* fragmentPosition() = 0;
49
50private:
51 typedef GrGLShaderBuilder INHERITED;
52};
53
54/*
55 * Fragment processor's, in addition to all of the above, may need to use dst color so they use
joshualittdb0d3ca2014-10-07 12:42:26 -070056 * this builder to create their shader
joshualittb0a8a372014-09-23 09:50:21 -070057 */
joshualittdb0d3ca2014-10-07 12:42:26 -070058class GrGLFragmentProcessorShaderBuilder : public GrGLProcessorFragmentShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070059public:
joshualittdb0d3ca2014-10-07 12:42:26 -070060 GrGLFragmentProcessorShaderBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
joshualittb0a8a372014-09-23 09:50:21 -070061 /** Returns the variable name that holds the color of the destination pixel. This may be NULL if
62 no effect advertised that it will read the destination. */
63 virtual const char* dstColor() = 0;
64
65private:
joshualittdb0d3ca2014-10-07 12:42:26 -070066 typedef GrGLProcessorFragmentShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070067};
68
joshualittdb0d3ca2014-10-07 12:42:26 -070069class GrGLFragmentShaderBuilder : public GrGLFragmentProcessorShaderBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070070public:
71 typedef uint8_t DstReadKey;
72 typedef uint8_t FragPosKey;
73
74 /** Returns a key for adding code to read the copy-of-dst color in service of effects that
75 require reading the dst. It must not return 0 because 0 indicates that there is no dst
76 copy read at all (in which case this function should not be called). */
77 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
78
79 /** Returns a key for reading the fragment location. This should only be called if there is an
80 effect that will requires the fragment position. If the fragment position is not required,
81 the key is 0. */
82 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
83
84 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc& desc);
85
joshualittfe1233c2014-10-07 12:16:35 -070086 virtual const char* dstColor() SK_OVERRIDE;
joshualitt30ba4362014-08-21 20:18:45 -070087
joshualittdb0d3ca2014-10-07 12:42:26 -070088 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE;
joshualittfe1233c2014-10-07 12:16:35 -070089
joshualittdb0d3ca2014-10-07 12:42:26 -070090 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
91 int index) SK_OVERRIDE;
92
93 virtual const char* fragmentPosition() SK_OVERRIDE;
94
95private:
joshualitt30ba4362014-08-21 20:18:45 -070096 /*
joshualittdb0d3ca2014-10-07 12:42:26 -070097 * An internal call for GrGLFullProgramBuilder to use to add varyings to the vertex shader
joshualitt30ba4362014-08-21 20:18:45 -070098 */
99 void addVarying(GrSLType type,
100 const char* name,
egdaniel6db91282014-09-02 08:02:38 -0700101 const char** fsInName,
102 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefault_Precision);
joshualitt30ba4362014-08-21 20:18:45 -0700103
joshualittdb0d3ca2014-10-07 12:42:26 -0700104 /*
105 * Private functions used by GrGLProgramBuilder for compilation
106 */
107 void bindProgramLocations(GrGLuint programId);
108 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const;
109 void emitCodeBeforeEffects();
110 void emitCodeAfterEffects(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage);
joshualitt30ba4362014-08-21 20:18:45 -0700111
joshualittdb0d3ca2014-10-07 12:42:26 -0700112 /** Enables using the secondary color output and returns the name of the var in which it is
113 to be stored */
114 const char* enableSecondaryOutput();
115
116 /** Gets the name of the primary color output. */
117 const char* getColorOutputName() const;
118
joshualitt30ba4362014-08-21 20:18:45 -0700119 /**
120 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
121 */
122 enum GLSLPrivateFeature {
123 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
124 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature
125 };
126
127 // Interpretation of DstReadKey when generating code
128 enum {
129 kNoDstRead_DstReadKey = 0,
130 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
131 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
132 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
133 };
134
135 enum {
136 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
137 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
138 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
139 };
140
141 bool fHasCustomColorOutput;
142 bool fHasSecondaryOutput;
143 bool fSetupFragPosition;
144 bool fTopLeftFragPosRead;
145
joshualittfe1233c2014-10-07 12:16:35 -0700146 friend class GrGLProgramBuilder;
joshualittdb0d3ca2014-10-07 12:42:26 -0700147 friend class GrGLFullProgramBuilder;
joshualittfe1233c2014-10-07 12:16:35 -0700148
joshualittdb0d3ca2014-10-07 12:42:26 -0700149 typedef GrGLFragmentProcessorShaderBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700150};
151
152#endif