blob: b5f83d46db8da880ffa7ab942e5d67aca10e1678 [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
joshualitt47bb3822014-10-07 16:43:25 -070010
joshualitt30ba4362014-08-21 20:18:45 -070011#include "GrGLShaderBuilder.h"
12
joshualitt74077b92014-10-24 11:26:03 -070013class GrGLVarying;
14
joshualittb0a8a372014-09-23 09:50:21 -070015/*
joshualitt47bb3822014-10-07 16:43:25 -070016 * This base class encapsulates the functionality which the GP uses to build fragment shaders
joshualittb0a8a372014-09-23 09:50:21 -070017 */
joshualitt47bb3822014-10-07 16:43:25 -070018class GrGLGPFragmentBuilder : public GrGLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070019public:
joshualitt47bb3822014-10-07 16:43:25 -070020 GrGLGPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
21 virtual ~GrGLGPFragmentBuilder() {}
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
joshualitt47bb3822014-10-07 16:43:25 -070056 * this builder to create their shader. Because this is the only shader builder the FP sees, we
57 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -070058 */
joshualitt47bb3822014-10-07 16:43:25 -070059class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070060public:
joshualitt47bb3822014-10-07 16:43:25 -070061 GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
62
joshualittb0a8a372014-09-23 09:50:21 -070063 /** Returns the variable name that holds the color of the destination pixel. This may be NULL if
64 no effect advertised that it will read the destination. */
65 virtual const char* dstColor() = 0;
66
67private:
joshualitt47bb3822014-10-07 16:43:25 -070068 typedef GrGLGPFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070069};
70
joshualitt47bb3822014-10-07 16:43:25 -070071// TODO rename to Fragment Builder
72class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070073public:
74 typedef uint8_t DstReadKey;
75 typedef uint8_t FragPosKey;
76
77 /** Returns a key for adding code to read the copy-of-dst color in service of effects that
78 require reading the dst. It must not return 0 because 0 indicates that there is no dst
79 copy read at all (in which case this function should not be called). */
80 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
81
82 /** Returns a key for reading the fragment location. This should only be called if there is an
83 effect that will requires the fragment position. If the fragment position is not required,
84 the key is 0. */
85 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
86
87 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc& desc);
88
joshualitt47bb3822014-10-07 16:43:25 -070089 // true public interface, defined explicitly in the abstract interfaces above
joshualittdb0d3ca2014-10-07 12:42:26 -070090 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE;
joshualittdb0d3ca2014-10-07 12:42:26 -070091 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
92 int index) SK_OVERRIDE;
joshualittdb0d3ca2014-10-07 12:42:26 -070093 virtual const char* fragmentPosition() SK_OVERRIDE;
joshualitt47bb3822014-10-07 16:43:25 -070094 virtual const char* dstColor() SK_OVERRIDE;
joshualittdb0d3ca2014-10-07 12:42:26 -070095
joshualitt74077b92014-10-24 11:26:03 -070096private:
joshualitt47bb3822014-10-07 16:43:25 -070097 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
98 void emitCodeToReadDstTexture();
99 void enableCustomOutput();
100 void enableSecondaryOutput();
101 const char* getPrimaryColorOutputName() const;
102 const char* getSecondaryColorOutputName() const;
103 void enableSecondaryOutput(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage);
104 void combineColorAndCoverage(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage);
105 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const;
106 void bindFragmentShaderLocations(GrGLuint programID);
107
joshualitt47bb3822014-10-07 16:43:25 -0700108 // As GLProcessors emit code, there are some conditions we need to verify. We use the below
109 // state to track this. The reset call is called per processor emitted.
110 bool hasReadDstColor() const { return fHasReadDstColor; }
111 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
112 void reset() {
113 fHasReadDstColor = false;
114 fHasReadFragmentPosition = false;
115 }
joshualitt30ba4362014-08-21 20:18:45 -0700116
joshualitt74077b92014-10-24 11:26:03 -0700117 /*
118 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
119 */
120 void addVarying(GrGLVarying*, GrGLShaderVar::Precision);
121
joshualitt30ba4362014-08-21 20:18:45 -0700122 /**
123 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
124 */
125 enum GLSLPrivateFeature {
126 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
127 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature
128 };
129
130 // Interpretation of DstReadKey when generating code
131 enum {
132 kNoDstRead_DstReadKey = 0,
133 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
134 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
135 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
136 };
137
joshualitt47bb3822014-10-07 16:43:25 -0700138 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700139 enum {
140 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
141 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
142 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
143 };
144
joshualitt47bb3822014-10-07 16:43:25 -0700145 static const char* kDstCopyColorName;
146
joshualitt30ba4362014-08-21 20:18:45 -0700147 bool fHasCustomColorOutput;
148 bool fHasSecondaryOutput;
149 bool fSetupFragPosition;
150 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700151 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700152
joshualitt47bb3822014-10-07 16:43:25 -0700153 // some state to verify shaders and effects are consistent, this is reset between effects by
154 // the program creator
155 bool fHasReadDstColor;
156 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700157
joshualitt47bb3822014-10-07 16:43:25 -0700158 friend class GrGLNvprProgramBuilder;
159 friend class GrGLProgramBuilder;
160
161 typedef GrGLFPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700162};
163
164#endif