Removed invalid EGL config attribute
[glmemperf] / blittest.cpp
1 /**
2  * OpenGL ES 2.0 memory performance estimator
3  * Copyright (C) 2009 Nokia
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * \author Sami Kyöstilä <sami.kyostila@nokia.com>
20  *
21  * Generic blitter
22  */
23 #include "blittest.h"
24 #include "util.h"
25 #include <sstream>
26
27 const char *defaultVertSource = 
28     "precision mediump float;\n"
29     "attribute vec2 in_position;\n"
30     "attribute vec2 in_texcoord;\n"
31     "varying vec2 texcoord;\n"
32     "\n"
33     "void main()\n"
34     "{\n"
35     "   gl_Position = vec4(in_position, 0.0, 1.0);\n"
36     "   texcoord = in_texcoord;\n"
37     "}\n";
38
39 const char *defaultFragSource = 
40     "precision mediump float;\n"
41     "varying vec2 texcoord;\n"
42     "uniform sampler2D texture;\n"
43     "\n"
44     "void main()\n"
45     "{\n"
46     "   gl_FragColor = texture2D(texture, texcoord);\n"
47     "}\n";
48
49 BlitTest::BlitTest(int width, int height, 
50                    bool rotate, float texW, float texH,
51                    float quadW, float quadH):
52     m_width(width),
53     m_height(height),
54     m_texW(texW),
55     m_texH(texH),
56     m_quadW(quadW),
57     m_quadH(quadH),
58     m_rotate(rotate),
59     m_vertSource(defaultVertSource),
60     m_fragSource(defaultFragSource)
61 {
62 }
63
64 BlitTest::BlitTest(GLenum format, GLenum type, int width, int height, const std::string& fileName,
65                    bool rotate, float texW, float texH,
66                    float quadW, float quadH):
67     m_format(format),
68     m_type(type),
69     m_width(width),
70     m_height(height),
71     m_texW(texW),
72     m_texH(texH),
73     m_quadW(quadW),
74     m_quadH(quadH),
75     m_rotate(rotate),
76     m_fileName(fileName),
77     m_vertSource(defaultVertSource),
78     m_fragSource(defaultFragSource)
79 {
80     if (m_format >= 0x8c00)
81     {
82         m_type = m_format;
83     }
84 }
85
86 void BlitTest::teardown()
87 {
88     glUseProgram(0);
89     glDisableVertexAttribArray(m_positionAttr);
90     glDisableVertexAttribArray(m_texcoordAttr);
91     glDeleteProgram(m_program);
92     // Disabled until driver segfault is fixed
93     //glDeleteTextures(1, &m_texture);
94 }
95
96 void BlitTest::prepare()
97 {
98     initializeBlitter();
99     if (m_fileName.size())
100     {
101         if (m_format >= 0x8c00)
102         {
103             loadCompressedTexture(GL_TEXTURE_2D, 0, m_format, m_width, m_height, m_fileName);
104         }
105         else
106         {
107             loadRawTexture(GL_TEXTURE_2D, 0, m_format, m_width, m_height, m_format, m_type, m_fileName);
108         }
109     }
110     ASSERT_GL();
111 }
112
113 void BlitTest::operator()(int frame)
114 {
115     float t = frame / 400.0f;
116
117     // Disable animation for now, since it interferes with the results due
118     // to lack of wrapping for NPOT textures
119     t = 0;
120
121     const GLfloat texcoords[] =
122     {
123          0 + t,       m_texH + t,
124          0 + t,       0 + t,
125          m_texW + t,  m_texH + t,
126          m_texW + t,  0 + t
127     };
128
129     const GLfloat texcoordsRotated[] =
130     {
131          m_texW + t,  0 + t,
132          0 + t,       0 + t,
133          m_texW + t,  m_texH + t,
134          0 + t,       m_texH + t
135     };
136
137     const GLfloat vertices[] =
138     {
139         -m_quadW, -m_quadH,
140         -m_quadW,  m_quadH,
141          m_quadW, -m_quadH,
142          m_quadW,  m_quadH
143     };
144
145     glVertexAttribPointer(m_positionAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
146     glVertexAttribPointer(m_texcoordAttr, 2, GL_FLOAT, GL_FALSE, 0, m_rotate ? texcoordsRotated : texcoords);
147     glClear(GL_COLOR_BUFFER_BIT);
148     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
149 }
150
151 std::string BlitTest::name() const
152 {
153     std::stringstream s;
154
155     if (m_rotate)
156     {
157         s << "blit_tex_rot90_";
158     }
159     else
160     {
161         s << "blit_tex_";
162     }
163
164     s << textureFormatName(m_format, m_type);
165     s << "_" << m_width << "x" << m_height;
166
167     return s.str();
168 }
169
170 void BlitTest::initializeBlitter()
171 {
172     m_program = createProgram(m_vertSource, m_fragSource);
173     glUseProgram(m_program);
174
175     glClearColor(.2, .4, .6, 1.0);
176
177     m_positionAttr = glGetAttribLocation(m_program, "in_position");
178     m_texcoordAttr = glGetAttribLocation(m_program, "in_texcoord");
179     assert(m_positionAttr >= 0);
180     assert(m_texcoordAttr >= 0);
181
182     glEnableVertexAttribArray(m_positionAttr);
183     glEnableVertexAttribArray(m_texcoordAttr);
184
185     m_textureUnif = glGetUniformLocation(m_program, "texture");
186     assert(m_textureUnif >= 0);
187     glUniform1i(m_textureUnif, 0);
188
189     glGenTextures(1, &m_texture);
190     glBindTexture(GL_TEXTURE_2D, m_texture);
191     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
192     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
193     ASSERT_GL();
194 }