~thomas-voss/glmark2/build-for-mir

« back to all changes in this revision

Viewing changes to src/scene-function.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jani Monoses
  • Date: 2011-08-05 00:05:19 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20110805000519-zit4dpe1wwzhnbg0
Tags: upstream-2011.07
Import upstream version 2011.07

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2010-2011 Linaro Limited
 
3
 *
 
4
 * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
 
5
 *
 
6
 * glmark2 is free software: you can redistribute it and/or modify it under the
 
7
 * terms of the GNU General Public License as published by the Free Software
 
8
 * Foundation, either version 3 of the License, or (at your option) any later
 
9
 * version.
 
10
 *
 
11
 * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
 
12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
 * details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License along with
 
17
 * glmark2.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 * Authors:
 
20
 *  Alexandros Frantzis (glmark2)
 
21
 */
 
22
#include "scene.h"
 
23
#include "mat.h"
 
24
#include "stack.h"
 
25
#include "vec.h"
 
26
#include "log.h"
 
27
 
 
28
#include <sstream>
 
29
 
 
30
static const std::string shader_file_base(GLMARK_DATA_PATH"/shaders/function");
 
31
 
 
32
static const std::string vtx_file(shader_file_base + ".vert");
 
33
static const std::string frg_file(shader_file_base + ".frag");
 
34
static const std::string call_file(shader_file_base + "-call.all");
 
35
static const std::string step_low_file(shader_file_base + "-step-low.all");
 
36
static const std::string step_medium_file(shader_file_base + "-step-medium.all");
 
37
 
 
38
SceneFunction::SceneFunction(Canvas &pCanvas) :
 
39
    SceneGrid(pCanvas, "function")
 
40
{
 
41
    mOptions["fragment-steps"] = Scene::Option("fragment-steps", "1",
 
42
            "The number of computational steps in the fragment shader");
 
43
    mOptions["fragment-function"] = Scene::Option("fragment-function", "true",
 
44
            "Whether each computational step includes a function call");
 
45
    mOptions["vertex-steps"] = Scene::Option("vertex-steps", "1",
 
46
            "The number of computational steps in the vertex shader");
 
47
    mOptions["vertex-function"] = Scene::Option("vertex-function", "true",
 
48
            "Whether each computational step includes an if-else clause");
 
49
    mOptions["vertex-complexity"] = Scene::Option("vertex-complexity", "low",
 
50
            "The complexity of each computational step in the vertex shader");
 
51
    mOptions["fragment-complexity"] = Scene::Option("fragment-complexity", "low",
 
52
            "The complexity of each computational step in the fragment shader");
 
53
}
 
54
 
 
55
SceneFunction::~SceneFunction()
 
56
{
 
57
}
 
58
 
 
59
static std::string &
 
60
replace_string(std::string &str, const std::string &remove, const std::string &insert)
 
61
{
 
62
    std::string::size_type pos = 0;
 
63
 
 
64
    while ((pos = str.find(remove, pos)) != std::string::npos) {
 
65
        str.replace(pos, remove.size(), insert);
 
66
        pos++;
 
67
    }
 
68
 
 
69
    return str;
 
70
}
 
71
 
 
72
static std::string
 
73
get_vertex_shader_source(int steps, bool function, std::string &complexity)
 
74
{
 
75
    std::string vtx_string, step_low_string, step_medium_string, call_string;
 
76
 
 
77
    if (!gotSource(vtx_file, vtx_string) ||
 
78
        !gotSource(step_low_file, step_low_string) ||
 
79
        !gotSource(step_medium_file, step_medium_string) ||
 
80
        !gotSource(call_file, call_string))
 
81
    {
 
82
        return "";
 
83
    }
 
84
 
 
85
    std::stringstream ss_main;
 
86
    std::string process_string;
 
87
 
 
88
    if (complexity == "low")
 
89
        process_string = step_low_string;
 
90
    else if (complexity == "medium")
 
91
        process_string = step_medium_string;
 
92
 
 
93
    for (int i = 0; i < steps; i++) {
 
94
        if (function)
 
95
            ss_main << call_string;
 
96
        else
 
97
            ss_main << process_string;
 
98
    }
 
99
 
 
100
    replace_string(vtx_string, "$PROCESS$", function ? process_string : "");
 
101
    replace_string(vtx_string, "$MAIN$", ss_main.str());
 
102
 
 
103
    return vtx_string;
 
104
}
 
105
 
 
106
static std::string
 
107
get_fragment_shader_source(int steps, bool function, std::string &complexity)
 
108
{
 
109
    std::string frg_string, step_low_string, step_medium_string, call_string;
 
110
 
 
111
    if (!gotSource(frg_file, frg_string) ||
 
112
        !gotSource(step_low_file, step_low_string) ||
 
113
        !gotSource(step_medium_file, step_medium_string) ||
 
114
        !gotSource(call_file, call_string))
 
115
    {
 
116
        return "";
 
117
    }
 
118
 
 
119
    std::stringstream ss_main;
 
120
    std::string process_string;
 
121
 
 
122
    if (complexity == "low")
 
123
        process_string = step_low_string;
 
124
    else if (complexity == "medium")
 
125
        process_string = step_medium_string;
 
126
 
 
127
    for (int i = 0; i < steps; i++) {
 
128
        if (function)
 
129
            ss_main << call_string;
 
130
        else
 
131
            ss_main << process_string;
 
132
    }
 
133
 
 
134
    replace_string(frg_string, "$PROCESS$", function ? process_string : "");
 
135
    replace_string(frg_string, "$MAIN$", ss_main.str());
 
136
 
 
137
    return frg_string;
 
138
}
 
139
 
 
140
void SceneFunction::setup()
 
141
{
 
142
    SceneGrid::setup();
 
143
 
 
144
    /* Parse options */
 
145
    bool vtx_function = mOptions["vertex-function"].value == "true";
 
146
    bool frg_function = mOptions["fragment-function"].value == "true";
 
147
    std::string vtx_complexity = mOptions["vertex-complexity"].value;
 
148
    std::string frg_complexity = mOptions["fragment-complexity"].value;
 
149
    int vtx_steps = 0;
 
150
    int frg_steps = 0;
 
151
 
 
152
    std::stringstream ss;
 
153
 
 
154
    ss << mOptions["vertex-steps"].value;
 
155
    ss >> vtx_steps;
 
156
    ss.clear();
 
157
    ss << mOptions["fragment-steps"].value;
 
158
    ss >> frg_steps;
 
159
 
 
160
    /* Load shaders */
 
161
    std::string vtx_shader(get_vertex_shader_source(vtx_steps, vtx_function,
 
162
                                                    vtx_complexity));
 
163
    std::string frg_shader(get_fragment_shader_source(frg_steps, frg_function,
 
164
                                                      frg_complexity));
 
165
 
 
166
    if (!Scene::load_shaders_from_strings(mProgram, vtx_shader, frg_shader))
 
167
        return;
 
168
 
 
169
    mProgram.start();
 
170
 
 
171
    std::vector<GLint> attrib_locations;
 
172
    attrib_locations.push_back(mProgram.getAttribIndex("position"));
 
173
    mMesh.set_attrib_locations(attrib_locations);
 
174
 
 
175
    mRunning = true;
 
176
    mStartTime = Scene::get_timestamp_us() / 1000000.0;
 
177
    mLastUpdateTime = mStartTime;
 
178
}