~glcompbench-dev/glcompbench/blur-update

« back to all changes in this revision

Viewing changes to src/libmatrix/shader-source.h

  • Committer: Jesse Barker
  • Date: 2012-01-27 22:26:02 UTC
  • mfrom: (74.1.5 libmatrix-util)
  • Revision ID: jesse.barker@linaro.org-20120127222602-osainr4na7opoiij
Merge of lp:~glcompbench-dev/glcompbench/libmatrix-util.

Updates glcompbench to reflect the latest lp:libmatrix, which, in and of itself,
conslidates code previously duplicated between glcompbench and glmark2 into
libmatrix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (c) 2010-2012 Linaro Limited
 
3
//
 
4
// All rights reserved. This program and the accompanying materials
 
5
// are made available under the terms of the MIT License which accompanies
 
6
// this distribution, and is available at
 
7
// http://www.opensource.org/licenses/mit-license.php
 
8
//
 
9
// Contributors:
 
10
//     Alexandros Frantzis <alexandros.frantzis@linaro.org>
 
11
//     Jesse Barker <jesse.barker@linaro.org>
 
12
//
 
13
#include <string>
 
14
#include <sstream>
 
15
#include <vector>
 
16
#include "vec.h"
 
17
#include "mat.h"
 
18
 
 
19
/**
 
20
 * Helper class for loading and manipulating shader sources.
 
21
 */
 
22
class ShaderSource
 
23
{
 
24
public:
 
25
    enum ShaderType {
 
26
        ShaderTypeVertex,
 
27
        ShaderTypeFragment,
 
28
        ShaderTypeUnknown
 
29
    };
 
30
 
 
31
    ShaderSource(ShaderType type = ShaderTypeUnknown) :
 
32
        precision_has_been_set_(false), type_(type) {}
 
33
    ShaderSource(const std::string &filename, ShaderType type = ShaderTypeUnknown) :
 
34
        precision_has_been_set_(false), type_(type) { append_file(filename); }
 
35
 
 
36
    void append(const std::string &str);
 
37
    void append_file(const std::string &filename);
 
38
 
 
39
    void replace(const std::string &remove, const std::string &insert);
 
40
    void replace_with_file(const std::string &remove, const std::string &filename);
 
41
 
 
42
    void add(const std::string &str, const std::string &function = "");
 
43
 
 
44
    void add_const(const std::string &name, float f,
 
45
                   const std::string &function = "");
 
46
    void add_const(const std::string &name, std::vector<float> &f,
 
47
                   const std::string &function = "");
 
48
    void add_const(const std::string &name, const LibMatrix::vec2 &v,
 
49
                   const std::string &function = "");
 
50
    void add_const(const std::string &name, const LibMatrix::vec3 &v,
 
51
                   const std::string &function = "");
 
52
    void add_const(const std::string &name, const LibMatrix::vec4 &v,
 
53
                   const std::string &function = "");
 
54
    void add_const(const std::string &name, const LibMatrix::mat3 &m,
 
55
                   const std::string &function = "");
 
56
 
 
57
    void add_array(const std::string &name, std::vector<float> &array,
 
58
                   const std::string &init_function,
 
59
                   const std::string &decl_function = "");
 
60
 
 
61
    ShaderType type();
 
62
    std::string str();
 
63
 
 
64
    enum PrecisionValue {
 
65
        PrecisionValueLow,
 
66
        PrecisionValueMedium,
 
67
        PrecisionValueHigh,
 
68
        PrecisionValueDefault
 
69
    };
 
70
 
 
71
    struct Precision {
 
72
        Precision();
 
73
        Precision(PrecisionValue int_p, PrecisionValue float_p,
 
74
                  PrecisionValue sampler2d_p, PrecisionValue samplercube_p);
 
75
        Precision(const std::string& list);
 
76
 
 
77
        PrecisionValue int_precision;
 
78
        PrecisionValue float_precision;
 
79
        PrecisionValue sampler2d_precision;
 
80
        PrecisionValue samplercube_precision;
 
81
    };
 
82
 
 
83
    void precision(const Precision& precision);
 
84
    const Precision& precision();
 
85
 
 
86
    static void default_precision(const Precision& precision,
 
87
                                  ShaderType type = ShaderTypeUnknown);
 
88
    static const Precision& default_precision(ShaderType type);
 
89
 
 
90
private:
 
91
    void add_global(const std::string &str);
 
92
    void add_local(const std::string &str, const std::string &function);
 
93
    bool load_file(const std::string& filename, std::string& str);
 
94
    void emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val,
 
95
                        const std::string& type_str);
 
96
 
 
97
    std::stringstream source_;
 
98
    Precision precision_;
 
99
    bool precision_has_been_set_;
 
100
    ShaderType type_;
 
101
 
 
102
    static std::vector<Precision> default_precision_;
 
103
};