~glcompbench-dev/glcompbench/trunk

« back to all changes in this revision

Viewing changes to src/composite-test.cc

  • Committer: Alexandros Frantzis
  • Date: 2011-01-17 18:15:21 UTC
  • Revision ID: alexandros.frantzis@linaro.org-20110117181521-rng36csayyf0obtv
Initial commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2011 Linaro Limited
 
3
 *
 
4
 * This file is part of glcompbench.
 
5
 *
 
6
 * glcompbench is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * glcompbench is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with glcompbench.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 * Authors:
 
20
 *  Alexandros Frantzis <alexandros.frantzis@linaro.org>
 
21
 *  Jesse Barker <jesse.barker@linaro.org>
 
22
 */
 
23
 
 
24
#include "composite-test.h"
 
25
#include "log.h"
 
26
 
 
27
using std::string;
 
28
using std::cerr;
 
29
using std::endl;
 
30
 
 
31
const string CompositeTest::model_view_matrix_name_("modelview");
 
32
const string CompositeTest::projection_matrix_name_("projection");
 
33
const string CompositeTest::texture0_name_("Texture0");
 
34
const string CompositeTest::texcoord_name_("texcoord");
 
35
const string CompositeTest::position_name_("position");
 
36
 
 
37
/**
 
38
 * Gets the number of window that are visible.
 
39
 *
 
40
 * @param window_list the window list to search in
 
41
 *
 
42
 * @return the number of visible windows
 
43
 */
 
44
static int
 
45
num_visible_windows(std::list<CompositeWindow *> &window_list)
 
46
{
 
47
    int count = 0;
 
48
 
 
49
    for(std::list<CompositeWindow*>::iterator iter = window_list.begin();
 
50
        iter != window_list.end(); iter++)
 
51
    {
 
52
        if ((*iter)->get_texture())
 
53
            count++;
 
54
    }
 
55
 
 
56
    return count;
 
57
}
 
58
 
 
59
void
 
60
CompositeTest::init()
 
61
{
 
62
    // Initialize shader sources from input files
 
63
    string vsFilename(GLCOMPBENCH_DATA_PATH"/composite-vertex-shader");
 
64
    if (!gotSource(vsFilename, vertex_shader_))
 
65
    {
 
66
        cerr << "Failed to get vertex shader source for logo" << endl;
 
67
        return;
 
68
    }
 
69
    string fsFilename(GLCOMPBENCH_DATA_PATH"/composite-fragment-shader");
 
70
    if (!gotSource(fsFilename, fragment_shader_))
 
71
    {
 
72
        cerr << "Failed to get fragment shader source for logo" << endl;
 
73
        return;
 
74
    }
 
75
 
 
76
    program_.init();
 
77
    if (!program_.valid())
 
78
    {
 
79
        cerr << "No valid program for compositing (reason: ";
 
80
        cerr << program_.errorMessage() << ")" << endl;
 
81
        return;
 
82
    }
 
83
    program_.addShader(GL_VERTEX_SHADER, vertex_shader_);
 
84
    if (!program_.valid())
 
85
    {
 
86
        cerr << "Failed to add vertex shader to compositing program (reason: ";
 
87
        cerr << program_.errorMessage() << ")" << endl;
 
88
        return;
 
89
    }
 
90
    program_.addShader(GL_FRAGMENT_SHADER, fragment_shader_);
 
91
    if (!program_.valid())
 
92
    {
 
93
        cerr << "Failed to add fragment shader to compositing program (reason: ";
 
94
        cerr << program_.errorMessage() << ")" << endl;
 
95
        return;
 
96
    }
 
97
    program_.build();
 
98
    if (!program_.ready())
 
99
    {
 
100
        cerr << "Failed to build compositing program (reason: ";
 
101
        cerr << program_.errorMessage() << ")" << endl;
 
102
        return;
 
103
    }
 
104
    vertexIndex_ = program_.getAttribIndex(position_name_);
 
105
    texcoordIndex_ = program_.getAttribIndex(texcoord_name_);
 
106
}
 
107
 
 
108
/**
 
109
 * Prepares the test for a test run.
 
110
 */
 
111
void
 
112
CompositeTest::prepare_for_run()
 
113
{
 
114
    program_.start();
 
115
    int texUnit(0);
 
116
    program_.loadUniformScalar(texUnit, texture0_name_);
 
117
 
 
118
    glClearColor(0.1, 0.1, 0.3, 1.0);
 
119
 
 
120
    glEnable(GL_BLEND);
 
121
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
122
}
 
123
 
 
124
void
 
125
CompositeTest::reshape(int width, int height)
 
126
{
 
127
    float ar = static_cast<float>(width) / static_cast<float>(height);
 
128
 
 
129
    /* Update the projection matrix */
 
130
    projection_matrix.loadIdentity();
 
131
    projection_matrix.perspective(22.6, ar, 5.0, 60.0);
 
132
 
 
133
    /* Update the modelview matrix */
 
134
    model_view_matrix.loadIdentity();
 
135
    model_view_matrix.translate(0.0, 0.0, -10.0);
 
136
 
 
137
    /* Set the viewport */
 
138
    glViewport(0, 0, width, height);
 
139
}
 
140
 
 
141
void
 
142
CompositeTest::draw(std::list<CompositeWindow *> &window_list)
 
143
{
 
144
    const GLfloat verts[4][3] = {
 
145
        { -1, -1, 0 },
 
146
        {  1, -1, 0 },
 
147
        {  1,  1, 0 },
 
148
        { -1,  1, 0 }
 
149
    };
 
150
    const GLfloat texcoords[4][2] = {
 
151
        { 0, 1 },
 
152
        { 1, 1 },
 
153
        { 1, 0 },
 
154
        { 0, 0 }
 
155
    };
 
156
 
 
157
    glActiveTexture(GL_TEXTURE0);
 
158
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
159
 
 
160
    /* Set up the position of the attributes in the vertex array */
 
161
    glVertexAttribPointer(vertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, verts);
 
162
    glVertexAttribPointer(texcoordIndex_, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
 
163
 
 
164
    /* Enable the attributes */
 
165
    glEnableVertexAttribArray(vertexIndex_);
 
166
    glEnableVertexAttribArray(texcoordIndex_);
 
167
 
 
168
    program_.loadUniformMatrix(projection_matrix.getCurrent(),
 
169
                               projection_matrix_name_);
 
170
 
 
171
    /* Find out how many windows are visible and calculate the angular step */
 
172
    GLuint visible_windows(num_visible_windows(window_list));
 
173
    GLfloat angular_step(2 * M_PI / visible_windows);
 
174
 
 
175
    /* Draw the windows in a circle using the calculated angular step */
 
176
    GLint i(0);
 
177
    for(std::list<CompositeWindow*>::iterator iter = window_list.begin();
 
178
        iter != window_list.end(); ++iter)
 
179
    {
 
180
        CompositeWindow *comp_win = *iter;
 
181
        GLuint tex = comp_win->get_texture();
 
182
        if (tex) {
 
183
            model_view_matrix.push();
 
184
            model_view_matrix.translate(cos(angular_step * i),
 
185
                                        sin(angular_step * i), 0);
 
186
 
 
187
            /* Load shader ModelView uniform */
 
188
            program_.loadUniformMatrix(model_view_matrix.getCurrent(),
 
189
                                       model_view_matrix_name_);
 
190
 
 
191
            Log::debug("Drawing Win: 0x%x Tex: 0x%x\n",
 
192
                       comp_win->get_xwindow(), comp_win->get_texture());
 
193
 
 
194
            glBindTexture(GL_TEXTURE_2D, tex);
 
195
            glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
196
            model_view_matrix.pop();
 
197
            ++i;
 
198
        }
 
199
    }
 
200
 
 
201
    /* Disable the attributes */
 
202
    glDisableVertexAttribArray(vertexIndex_);
 
203
    glDisableVertexAttribArray(texcoordIndex_);
 
204
}
 
205
 
 
206
void
 
207
CompositeTest::cleanup()
 
208
{
 
209
    program_.stop();
 
210
}