~glmark2-dev/glmark2/fix-no-mapbuffer

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 © 2010-2011 Linaro Limited
 *
 * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
 *
 * glmark2 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * glmark2.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *  Alexandros Frantzis (glmark2)
 */

#include <string>
#include <sstream>
#include <vector>
#include "vec.h"
#include "mat.h"

/**
 * Helper class for loading and manipulating shader sources.
 */
class ShaderSource
{
public:
    enum ShaderType {
        ShaderTypeVertex,
        ShaderTypeFragment,
        ShaderTypeUnknown
    };

    ShaderSource(ShaderType type = ShaderTypeUnknown) :
        precision_has_been_set_(false), type_(type) {}
    ShaderSource(const std::string &filename, ShaderType type = ShaderTypeUnknown) :
        precision_has_been_set_(false), type_(type) { append_file(filename); }

    void append(const std::string &str);
    void append_file(const std::string &filename);

    void replace(const std::string &remove, const std::string &insert);
    void replace_with_file(const std::string &remove, const std::string &filename);

    void add(const std::string &str, const std::string &function = "");

    void add_const(const std::string &name, float f,
                   const std::string &function = "");
    void add_const(const std::string &name, std::vector<float> &f,
                   const std::string &function = "");
    void add_const(const std::string &name, const LibMatrix::vec2 &v,
                   const std::string &function = "");
    void add_const(const std::string &name, const LibMatrix::vec3 &v,
                   const std::string &function = "");
    void add_const(const std::string &name, const LibMatrix::vec4 &v,
                   const std::string &function = "");
    void add_const(const std::string &name, const LibMatrix::mat3 &m,
                   const std::string &function = "");

    void add_array(const std::string &name, std::vector<float> &array,
                   const std::string &init_function,
                   const std::string &decl_function = "");

    ShaderType type();
    std::string str();

    enum PrecisionValue {
        PrecisionValueLow,
        PrecisionValueMedium,
        PrecisionValueHigh,
        PrecisionValueDefault,
    };

    struct Precision {
        Precision();
        Precision(PrecisionValue int_p, PrecisionValue float_p,
                  PrecisionValue sampler2d_p, PrecisionValue samplercube_p);
        Precision(const std::string& list);

        PrecisionValue int_precision;
        PrecisionValue float_precision;
        PrecisionValue sampler2d_precision;
        PrecisionValue samplercube_precision;
    };

    void precision(const Precision& precision);
    const Precision& precision();

    static void default_precision(const Precision& precision,
                                  ShaderType type = ShaderTypeUnknown);
    static const Precision& default_precision(ShaderType type);

private:
    void add_global(const std::string &str);
    void add_local(const std::string &str, const std::string &function);
    bool load_file(const std::string& filename, std::string& str);
    void emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val,
                        const std::string& type_str);

    std::stringstream source_;
    Precision precision_;
    bool precision_has_been_set_;
    ShaderType type_;

    static std::vector<Precision> default_precision_;
};