aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLShaderBuilder.h
blob: 889219d876bdcf314fa8df0cb0073ef3a2b3ab7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLShaderBuilder_DEFINED
#define GrGLShaderBuilder_DEFINED

#include "GrAllocator.h"
#include "gl/GrGLShaderVar.h"
#include "gl/GrGLSL.h"

typedef GrTAllocator<GrGLShaderVar> VarArray;

/**
  Containts all the incremental state of a shader as it is being built,
  as well as helpers to manipulate that state.
  TODO: migrate CompileShaders() here?
*/

class GrGLShaderBuilder {

public:

    GrGLShaderBuilder();

    void appendVarying(GrSLType type,
                       const char* name,
                       const char** vsOutName = NULL,
                       const char** fsInName = NULL);

    void appendVarying(GrSLType type,
                       const char* name,
                       int stageNum,
                       const char** vsOutName = NULL,
                       const char** fsInName = NULL);

    void computeSwizzle(uint32_t configFlags);
    void computeModulate(const char* fsInColor);

    void emitTextureSetup();

    /** texture2D(samplerName, coordName), with projection
        if necessary; if coordName is not specified,
        uses fSampleCoords. */
    void emitTextureLookup(const char* samplerName,
                           const char* coordName = NULL);

    /** sets outColor to results of texture lookup, with
        swizzle, and/or modulate as necessary */
    void emitDefaultFetch(const char* outColor,
                          const char* samplerName);

    // TODO: needs a better name
    enum SamplerMode {
        kDefault_SamplerMode,
        kProj_SamplerMode,
        kExplicitDivide_SamplerMode  // must do an explicit divide
    };

    // TODO: computing this requires information about fetch mode
    // && coord mapping, as well as StageDesc::fOptFlags - proably
    // need to set up default value and have some custom stages
    // override as necessary?
    void setSamplerMode(SamplerMode samplerMode) {
        fSamplerMode = samplerMode;
    }


    GrStringBuilder fHeader; // VS+FS, GLSL version, etc
    VarArray        fVSUnis;
    VarArray        fVSAttrs;
    VarArray        fVSOutputs;
    VarArray        fGSInputs;
    VarArray        fGSOutputs;
    VarArray        fFSInputs;
    GrStringBuilder fGSHeader; // layout qualifiers specific to GS
    VarArray        fFSUnis;
    VarArray        fFSOutputs;
    GrStringBuilder fFSFunctions;
    GrStringBuilder fVSCode;
    GrStringBuilder fGSCode;
    GrStringBuilder fFSCode;
    bool            fUsesGS;

    /// Per-stage settings - only valid while we're inside
    /// GrGLProgram::genStageCode().
    //@{

    int              fVaryingDims;
    static const int fCoordDims = 2;

protected:

    SamplerMode      fSamplerMode;

public:

    /// True if fSampleCoords is an expression; false if it's a bare
    /// variable name
    bool             fComplexCoord;
    GrStringBuilder  fSampleCoords;

    GrStringBuilder  fSwizzle;
    GrStringBuilder  fModulate;

    //@}

};

#endif