blob: 26c723ecc48fd5a540fe15bcde6aa97279f3daca [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:
joshualittabb52a12015-01-13 15:02:10 -080051 friend class GrGLNormalPathProcessor;
52
joshualittb0a8a372014-09-23 09:50:21 -070053 typedef GrGLShaderBuilder INHERITED;
54};
55
56/*
57 * 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 -070058 * this builder to create their shader. Because this is the only shader builder the FP sees, we
59 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -070060 */
joshualitt47bb3822014-10-07 16:43:25 -070061class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070062public:
joshualitt47bb3822014-10-07 16:43:25 -070063 GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
64
joshualittb0a8a372014-09-23 09:50:21 -070065 /** Returns the variable name that holds the color of the destination pixel. This may be NULL if
66 no effect advertised that it will read the destination. */
67 virtual const char* dstColor() = 0;
68
69private:
joshualitt47bb3822014-10-07 16:43:25 -070070 typedef GrGLGPFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070071};
72
joshualitt47bb3822014-10-07 16:43:25 -070073// TODO rename to Fragment Builder
74class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070075public:
76 typedef uint8_t DstReadKey;
77 typedef uint8_t FragPosKey;
78
79 /** Returns a key for adding code to read the copy-of-dst color in service of effects that
80 require reading the dst. It must not return 0 because 0 indicates that there is no dst
81 copy read at all (in which case this function should not be called). */
82 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
83
84 /** Returns a key for reading the fragment location. This should only be called if there is an
85 effect that will requires the fragment position. If the fragment position is not required,
86 the key is 0. */
87 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
88
joshualitt79f8fae2014-10-28 17:59:26 -070089 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey);
joshualitt30ba4362014-08-21 20:18:45 -070090
joshualitt47bb3822014-10-07 16:43:25 -070091 // true public interface, defined explicitly in the abstract interfaces above
mtklein36352bf2015-03-25 18:17:31 -070092 bool enableFeature(GLSLFeature) override;
joshualittdb0d3ca2014-10-07 12:42:26 -070093 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -070094 int index) override;
95 const char* fragmentPosition() override;
96 const char* dstColor() override;
joshualittdb0d3ca2014-10-07 12:42:26 -070097
joshualitt74077b92014-10-24 11:26:03 -070098private:
joshualitt47bb3822014-10-07 16:43:25 -070099 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700100 void enableCustomOutput();
101 void enableSecondaryOutput();
102 const char* getPrimaryColorOutputName() const;
103 const char* getSecondaryColorOutputName() const;
joshualitt43466a12015-02-13 17:18:27 -0800104 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds);
joshualitt47bb3822014-10-07 16:43:25 -0700105 void bindFragmentShaderLocations(GrGLuint programID);
106
joshualitt47bb3822014-10-07 16:43:25 -0700107 // As GLProcessors 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 hasReadDstColor() const { return fHasReadDstColor; }
110 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
111 void reset() {
112 fHasReadDstColor = false;
113 fHasReadFragmentPosition = false;
114 }
joshualitt30ba4362014-08-21 20:18:45 -0700115
joshualitt74077b92014-10-24 11:26:03 -0700116 /*
117 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
118 */
bsalomonc0bd6482014-12-09 10:04:14 -0800119 void addVarying(GrGLVarying*, GrSLPrecision);
joshualitt74077b92014-10-24 11:26:03 -0700120
joshualitt30ba4362014-08-21 20:18:45 -0700121 /**
122 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
123 */
124 enum GLSLPrivateFeature {
125 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
126 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature
127 };
128
129 // Interpretation of DstReadKey when generating code
130 enum {
131 kNoDstRead_DstReadKey = 0,
132 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
133 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
134 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
135 };
136
joshualitt47bb3822014-10-07 16:43:25 -0700137 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700138 enum {
139 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
140 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
141 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
142 };
143
joshualitt47bb3822014-10-07 16:43:25 -0700144 static const char* kDstCopyColorName;
145
joshualitt30ba4362014-08-21 20:18:45 -0700146 bool fHasCustomColorOutput;
147 bool fHasSecondaryOutput;
148 bool fSetupFragPosition;
149 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700150 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700151
joshualitt47bb3822014-10-07 16:43:25 -0700152 // some state to verify shaders and effects are consistent, this is reset between effects by
153 // the program creator
154 bool fHasReadDstColor;
155 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700156
joshualitt47bb3822014-10-07 16:43:25 -0700157 friend class GrGLProgramBuilder;
158
159 typedef GrGLFPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700160};
161
162#endif