1 | /* |
2 | * Copyright 2015 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 GrGLSLProgramBuilder_DEFINED |
9 | #define GrGLSLProgramBuilder_DEFINED |
10 | |
11 | #include "src/gpu/GrCaps.h" |
12 | #include "src/gpu/GrGeometryProcessor.h" |
13 | #include "src/gpu/GrProgramInfo.h" |
14 | #include "src/gpu/GrRenderTarget.h" |
15 | #include "src/gpu/GrRenderTargetPriv.h" |
16 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
18 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
19 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
20 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
21 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
22 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
23 | |
24 | class GrProgramDesc; |
25 | class GrShaderVar; |
26 | class GrGLSLVaryingHandler; |
27 | class SkString; |
28 | class GrShaderCaps; |
29 | |
30 | class GrGLSLProgramBuilder { |
31 | public: |
32 | using UniformHandle = GrGLSLUniformHandler::UniformHandle; |
33 | using SamplerHandle = GrGLSLUniformHandler::SamplerHandle; |
34 | |
35 | virtual ~GrGLSLProgramBuilder() {} |
36 | |
37 | virtual const GrCaps* caps() const = 0; |
38 | const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); } |
39 | |
40 | GrSurfaceOrigin origin() const { return fProgramInfo.origin(); } |
41 | const GrPipeline& pipeline() const { return fProgramInfo.pipeline(); } |
42 | const GrPrimitiveProcessor& primitiveProcessor() const { return fProgramInfo.primProc(); } |
43 | GrProcessor::CustomFeatures processorFeatures() const { |
44 | return fProgramInfo.requestedFeatures(); |
45 | } |
46 | bool snapVerticesToPixelCenters() const { |
47 | return fProgramInfo.pipeline().snapVerticesToPixelCenters(); |
48 | } |
49 | bool hasPointSize() const { return fProgramInfo.primitiveType() == GrPrimitiveType::kPoints; } |
50 | |
51 | // TODO: stop passing in the renderTarget for just the sampleLocations |
52 | int effectiveSampleCnt() const { |
53 | SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & fProgramInfo.requestedFeatures()); |
54 | return fRenderTarget->renderTargetPriv().getSampleLocations().count(); |
55 | } |
56 | const SkTArray<SkPoint>& getSampleLocations() const { |
57 | return fRenderTarget->renderTargetPriv().getSampleLocations(); |
58 | } |
59 | |
60 | const GrProgramDesc& desc() const { return fDesc; } |
61 | |
62 | void appendUniformDecls(GrShaderFlags visibility, SkString*) const; |
63 | |
64 | const char* samplerVariable(SamplerHandle handle) const { |
65 | return this->uniformHandler()->samplerVariable(handle); |
66 | } |
67 | |
68 | GrSwizzle samplerSwizzle(SamplerHandle handle) const { |
69 | if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) { |
70 | return this->uniformHandler()->samplerSwizzle(handle); |
71 | } |
72 | return GrSwizzle::RGBA(); |
73 | } |
74 | |
75 | // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling |
76 | // the name of the uniform inside of a stage. |
77 | void addRTWidthUniform(const char* name); |
78 | |
79 | // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position) |
80 | // without mangling the name of the uniform inside of a stage. |
81 | void addRTHeightUniform(const char* name); |
82 | |
83 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
84 | // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless |
85 | // explicitly asked not to. |
86 | void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true); |
87 | |
88 | virtual GrGLSLUniformHandler* uniformHandler() = 0; |
89 | virtual const GrGLSLUniformHandler* uniformHandler() const = 0; |
90 | virtual GrGLSLVaryingHandler* varyingHandler() = 0; |
91 | |
92 | // Used for backend customization of the output color and secondary color variables from the |
93 | // fragment processor. Only used if the outputs are explicitly declared in the shaders |
94 | virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {} |
95 | virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {} |
96 | |
97 | // number of each input/output type in a single allocation block, used by many builders |
98 | static const int kVarsPerBlock; |
99 | |
100 | GrGLSLVertexBuilder fVS; |
101 | GrGLSLGeometryBuilder fGS; |
102 | GrGLSLFragmentShaderBuilder fFS; |
103 | |
104 | int fStageIndex; |
105 | |
106 | const GrRenderTarget* fRenderTarget; // TODO: remove this |
107 | const GrProgramDesc& fDesc; |
108 | const GrProgramInfo& fProgramInfo; |
109 | |
110 | GrGLSLBuiltinUniformHandles fUniformHandles; |
111 | |
112 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
113 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
114 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
115 | int fFragmentProcessorCnt; |
116 | |
117 | protected: |
118 | explicit GrGLSLProgramBuilder(GrRenderTarget*, const GrProgramDesc&, const GrProgramInfo&); |
119 | |
120 | void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName); |
121 | |
122 | bool emitAndInstallProcs(); |
123 | |
124 | void finalizeShaders(); |
125 | |
126 | bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); } |
127 | |
128 | private: |
129 | // reset is called by program creator between each processor's emit code. It increments the |
130 | // stage offset for variable name mangling, and also ensures verfication variables in the |
131 | // fragment shader are cleared. |
132 | void reset() { |
133 | this->addStage(); |
134 | SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();) |
135 | } |
136 | void addStage() { fStageIndex++; } |
137 | |
138 | class AutoStageAdvance { |
139 | public: |
140 | AutoStageAdvance(GrGLSLProgramBuilder* pb) |
141 | : fPB(pb) { |
142 | fPB->reset(); |
143 | // Each output to the fragment processor gets its own code section |
144 | fPB->fFS.nextStage(); |
145 | } |
146 | ~AutoStageAdvance() {} |
147 | private: |
148 | GrGLSLProgramBuilder* fPB; |
149 | }; |
150 | |
151 | // Generates a possibly mangled name for a stage variable and writes it to the fragment shader. |
152 | void nameExpression(SkString*, const char* baseName); |
153 | |
154 | void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage); |
155 | void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut); |
156 | SkString emitAndInstallFragProc(const GrFragmentProcessor&, |
157 | int index, |
158 | int transformedCoordVarsIdx, |
159 | const SkString& input, |
160 | SkString output, |
161 | SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*); |
162 | void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn); |
163 | SamplerHandle emitSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&, |
164 | const char* name); |
165 | bool checkSamplerCounts(); |
166 | |
167 | #ifdef SK_DEBUG |
168 | void verify(const GrPrimitiveProcessor&); |
169 | void verify(const GrFragmentProcessor&); |
170 | void verify(const GrXferProcessor&); |
171 | #endif |
172 | |
173 | // These are used to check that we don't excede the allowable number of resources in a shader. |
174 | int fNumFragmentSamplers; |
175 | SkSTArray<4, GrGLSLPrimitiveProcessor::TransformVar> fTransformedCoordVars; |
176 | }; |
177 | |
178 | #endif |
179 | |