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

« back to all changes in this revision

Viewing changes to src/main.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:
21
21
 *  Ben Smith (original glmark benchmark)
22
22
 *  Alexandros Frantzis (glmark2)
23
23
 */
24
 
#include "oglsdl.h"
 
24
#include "gl-headers.h"
25
25
#include "scene.h"
26
 
 
27
 
#if USE_GL
28
 
#include "screen-sdl-gl.h"
29
 
#elif USE_GLESv2
30
 
#include "screen-sdl-glesv2.h"
31
 
#endif
32
 
 
33
 
#define UNUSED_PARAM(x) (void)(x)
34
 
 
35
 
int main(int argc, char *argv[])
36
 
{
37
 
    UNUSED_PARAM(argc);
38
 
    UNUSED_PARAM(argv);
39
 
 
40
 
    SDL_Event event;
41
 
    int running = 1;
42
 
    unsigned current_scene = 0;
43
 
 
44
 
    // Create the screen
45
 
#if USE_GL
46
 
    ScreenSDLGL screen(800, 600, 24, 0);
47
 
#elif USE_GLESv2
48
 
    ScreenSDLGLESv2 screen(800, 600, 24, 0);
49
 
#endif
50
 
 
51
 
    if (!screen.mInitSuccess) {
52
 
        printf("Error: %s: Could not initialize screen\n", __FUNCTION__);
53
 
        return 1;
 
26
#include "benchmark.h"
 
27
#include "options.h"
 
28
#include "log.h"
 
29
 
 
30
#include <iostream>
 
31
 
 
32
#if USE_GL
 
33
#include "canvas-x11-glx.h"
 
34
#elif USE_GLESv2
 
35
#include "canvas-x11-egl.h"
 
36
#endif
 
37
 
 
38
using std::vector;
 
39
using std::map;
 
40
using std::string;
 
41
 
 
42
static const char *default_benchmarks[] = {
 
43
    "build:use-vbo=false",
 
44
    "build:use-vbo=true",
 
45
    "texture:texture-filter=nearest",
 
46
    "texture:texture-filter=linear",
 
47
    "texture:texture-filter=mipmap",
 
48
    "shading:shading=gouraud",
 
49
    "shading:shading=blinn-phong-inf",
 
50
    "shading:shading=phong",
 
51
    "bump:bump-render=high-poly",
 
52
    "bump:bump-render=normals",
 
53
    "conditionals:vertex-steps=0:fragment-steps=0",
 
54
    "conditionals:vertex-steps=0:fragment-steps=5",
 
55
    "conditionals:vertex-steps=5:fragment-steps=0",
 
56
    "function:fragment-steps=5:fragment-complexity=low",
 
57
    "function:fragment-steps=5:fragment-complexity=medium",
 
58
    "loop:vertex-steps=5:fragment-steps=5:fragment-loop=false",
 
59
    "loop:vertex-steps=5:fragment-steps=5:fragment-uniform=false",
 
60
    "loop:vertex-steps=5:fragment-steps=5:fragment-uniform=true",
 
61
    NULL
 
62
};
 
63
 
 
64
void
 
65
add_default_benchmarks(vector<Benchmark *> &benchmarks)
 
66
{
 
67
    for (const char **s = default_benchmarks; *s != NULL; s++)
 
68
        benchmarks.push_back(new Benchmark(*s));
 
69
}
 
70
 
 
71
void
 
72
add_custom_benchmarks(vector<Benchmark *> &benchmarks)
 
73
{
 
74
    for (vector<string>::const_iterator iter = Options::benchmarks.begin();
 
75
         iter != Options::benchmarks.end();
 
76
         iter++)
 
77
    {
 
78
        benchmarks.push_back(new Benchmark(*iter));
54
79
    }
55
 
 
56
 
    printf("=======================================================\n");
57
 
    printf("    glmark2 %s\n", GLMARK_VERSION);
58
 
    printf("=======================================================\n");
59
 
    screen.print_info();
60
 
    printf("=======================================================\n");
61
 
 
62
 
    // Create the scenes.
63
 
    Scene *scene[] = {
64
 
        new SceneBuild(screen),
65
 
        new SceneTexture(screen),
66
 
        new SceneShading(screen),
67
 
    };
68
 
 
69
 
    unsigned num_scenes = sizeof(scene) / sizeof(*scene);
70
 
 
71
 
    // Load the first scene
72
 
    if (!scene[current_scene]->load())
73
 
        return 1;
74
 
    scene[current_scene]->start();
75
 
 
76
 
    while(running)
 
80
}
 
81
 
 
82
static void
 
83
list_scenes()
 
84
{
 
85
    const map<string, Scene *> &scenes = Benchmark::scenes();
 
86
 
 
87
    for (map<string, Scene *>::const_iterator scene_iter = scenes.begin();
 
88
         scene_iter != scenes.end();
 
89
         scene_iter++)
77
90
    {
78
 
        while(SDL_PollEvent(&event))
 
91
        Scene *scene = scene_iter->second;
 
92
        if (scene->name().empty())
 
93
            continue;
 
94
        Log::info("[Scene] %s\n", scene->name().c_str());
 
95
 
 
96
        const map<string, Scene::Option> &options = scene->options();
 
97
 
 
98
        for (map<string, Scene::Option>::const_iterator opt_iter = options.begin();
 
99
             opt_iter != options.end();
 
100
             opt_iter++)
79
101
        {
80
 
            switch(event.type)
 
102
            const Scene::Option &opt = opt_iter->second;
 
103
            Log::info("  [Option] %s\n"
 
104
                      "    Description  : %s\n"
 
105
                      "    Default Value: %s\n",
 
106
                      opt.name.c_str(), 
 
107
                      opt.description.c_str(),
 
108
                      opt.default_value.c_str());
 
109
        }
 
110
    }
 
111
}
 
112
 
 
113
void
 
114
do_benchmark(Canvas &canvas, vector<Benchmark *> &benchmarks)
 
115
{
 
116
    unsigned score = 0;
 
117
 
 
118
    for (vector<Benchmark *>::iterator bench_iter = benchmarks.begin();
 
119
         bench_iter != benchmarks.end();
 
120
         bench_iter++)
 
121
    {
 
122
        bool keep_running = true;
 
123
        Benchmark *bench = *bench_iter;
 
124
        Scene &scene = bench->setup_scene();
 
125
 
 
126
        if (!scene.name().empty()) {
 
127
            Log::info("%s", scene.info_string().c_str());
 
128
            Log::flush();
 
129
 
 
130
            while (scene.is_running() &&
 
131
                   (keep_running = !canvas.should_quit()))
81
132
            {
82
 
            case SDL_QUIT:
83
 
                running = 0;
84
 
                break;
85
 
            case SDL_KEYDOWN:
86
 
                if(event.key.keysym.sym == SDLK_ESCAPE)
87
 
                    running = 0;
88
 
                break;
89
 
            }
90
 
        }
91
 
 
92
 
        screen.clear();
93
 
 
94
 
        // Update the state of the current scene
95
 
        scene[current_scene]->update();
96
 
 
97
 
        // If the current scene is still running then draw it,
98
 
        // otherwise move to the next scene
99
 
        if (scene[current_scene]->is_running()) {
100
 
            scene[current_scene]->draw();
101
 
        }
102
 
        else {
103
 
            // Unload the current scene
104
 
            scene[current_scene]->unload();
105
 
 
106
 
            current_scene++;
107
 
 
108
 
            // Do we have another scene?
109
 
            if (current_scene < num_scenes) {
110
 
                // Load and start next scene
111
 
                if (!scene[current_scene]->load())
112
 
                    return 1;
113
 
                scene[current_scene]->start();
114
 
            }
115
 
            else
116
 
                running = false;
117
 
        }
118
 
 
119
 
 
120
 
        screen.update();
121
 
    }
122
 
 
123
 
    unsigned score = 0;
124
 
    for (unsigned i = 0; i < num_scenes; i++)
125
 
        score += scene[i]->calculate_score();
126
 
 
127
 
    printf("=======================================================\n");
128
 
    printf("                                  glmark2 Score: %u \n", score);
129
 
        printf("=======================================================\n");
 
133
                canvas.clear();
 
134
 
 
135
                scene.draw();
 
136
                scene.update();
 
137
 
 
138
                canvas.update();
 
139
            }
 
140
 
 
141
            Log::info(" FPS: %u\n", scene.average_fps());
 
142
            score += scene.average_fps();
 
143
        }
 
144
 
 
145
        bench->teardown_scene();
 
146
 
 
147
        if (!keep_running)
 
148
            break;
 
149
    }
 
150
 
 
151
    Log::info("=======================================================\n");
 
152
    Log::info("                                  glmark2 Score: %u \n", score);
 
153
    Log::info("=======================================================\n");
 
154
 
 
155
}
 
156
 
 
157
void
 
158
do_validation(Canvas &canvas, vector<Benchmark *> &benchmarks)
 
159
{
 
160
    for (vector<Benchmark *>::iterator bench_iter = benchmarks.begin();
 
161
         bench_iter != benchmarks.end();
 
162
         bench_iter++)
 
163
    {
 
164
        Benchmark *bench = *bench_iter;
 
165
        Scene &scene = bench->setup_scene();
 
166
 
 
167
        if (!scene.name().empty()) {
 
168
            Log::info("%s", scene.info_string().c_str());
 
169
            Log::flush();
 
170
 
 
171
            canvas.clear();
 
172
            scene.draw();
 
173
            canvas.update();
 
174
 
 
175
            string result;
 
176
            switch(scene.validate()) {
 
177
                case Scene::ValidationSuccess:
 
178
                    result = "Success";
 
179
                    break;
 
180
                case Scene::ValidationFailure:
 
181
                    result = "Failure";
 
182
                    break;
 
183
                case Scene::ValidationUnknown:
 
184
                    result = "Unknown";
 
185
                    break;
 
186
                default:
 
187
                    break;
 
188
            }
 
189
 
 
190
            Log::info(" Validation: %s\n", result.c_str());
 
191
        }
 
192
 
 
193
        bench->teardown_scene();
 
194
    }
 
195
}
 
196
 
 
197
int main(int argc, char *argv[])
 
198
{
 
199
 
 
200
    if (!Options::parse_args(argc, argv))
 
201
        return 1;
 
202
 
 
203
    if (Options::show_help) {
 
204
        Options::print_help();
 
205
        return 0;
 
206
    }
 
207
 
 
208
    // Create the canvas
 
209
#if USE_GL
 
210
    CanvasX11GLX canvas(800, 600);
 
211
#elif USE_GLESv2
 
212
    CanvasX11EGL canvas(800, 600);
 
213
#endif
 
214
 
 
215
    // Register the scenes, so they can be looked-up by name
 
216
    Benchmark::register_scene(*new SceneDefaultOptions(canvas));
 
217
    Benchmark::register_scene(*new SceneBuild(canvas));
 
218
    Benchmark::register_scene(*new SceneTexture(canvas));
 
219
    Benchmark::register_scene(*new SceneShading(canvas));
 
220
    Benchmark::register_scene(*new SceneConditionals(canvas));
 
221
    Benchmark::register_scene(*new SceneFunction(canvas));
 
222
    Benchmark::register_scene(*new SceneLoop(canvas));
 
223
    Benchmark::register_scene(*new SceneBump(canvas));
 
224
 
 
225
    if (Options::list_scenes) {
 
226
        list_scenes();
 
227
        return 0;
 
228
    }
 
229
 
 
230
    if (!canvas.init()) {
 
231
        Log::error("Error: %s: Could not initialize canvas\n", __FUNCTION__);
 
232
        return 1;
 
233
    }
 
234
 
 
235
    // Add the benchmarks to run
 
236
    vector<Benchmark *> benchmarks;
 
237
 
 
238
    if (Options::benchmarks.empty())
 
239
        add_default_benchmarks(benchmarks);
 
240
    else
 
241
        add_custom_benchmarks(benchmarks);
 
242
 
 
243
    Log::info("=======================================================\n");
 
244
    Log::info("    glmark2 %s\n", GLMARK_VERSION);
 
245
    Log::info("=======================================================\n");
 
246
    canvas.print_info();
 
247
    Log::info("=======================================================\n");
 
248
 
 
249
    canvas.visible(true);
 
250
 
 
251
    if (Options::validate)
 
252
        do_validation(canvas, benchmarks);
 
253
    else
 
254
        do_benchmark(canvas, benchmarks);
130
255
 
131
256
    return 0;
132
257
}