Don't abort just because libosso failed to initialize
[glmemperf] / shaderblittest.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  * Shader blit test
22  */
23 #include "shaderblittest.h"
24 #include <sstream>
25 #include <GLES2/gl2ext.h>
26 #include <stdlib.h>
27
28 ShaderBlitTest::ShaderBlitTest(const std::string& effect, int width, int height, 
29                                float texW, float texH,
30                                float quadW, float quadH):
31     m_width(width),
32     m_height(height),
33     m_texW(texW),
34     m_texH(texH),
35     m_quadW(quadW),
36     m_quadH(quadH),
37     m_effect(effect)
38 {
39 }
40
41 void ShaderBlitTest::teardown()
42 {
43     glUseProgram(0);
44     glDisableVertexAttribArray(m_positionAttr);
45     glDisableVertexAttribArray(m_texcoordAttr);
46     glDeleteProgram(m_program);
47     // Disabled until driver segfault is fixed
48     //glDeleteTextures(1, &m_texture);
49 }
50
51 void ShaderBlitTest::operator()(int frame)
52 {
53     float t = frame * 0;
54
55     const GLfloat texcoords[] =
56     {
57          0 + t,       m_texH + t,
58          0 + t,       0 + t,
59          m_texW + t,  m_texH + t,
60          m_texW + t,  0 + t
61     };
62
63     const GLfloat vertices[] =
64     {
65         -m_quadW, -m_quadH,
66         -m_quadW,  m_quadH,
67          m_quadW, -m_quadH,
68          m_quadW,  m_quadH
69     };
70
71     glVertexAttribPointer(m_positionAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
72     glVertexAttribPointer(m_texcoordAttr, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
73     glClear(GL_COLOR_BUFFER_BIT);
74     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
75 }
76
77 std::string ShaderBlitTest::name() const
78 {
79     std::stringstream s;
80
81     s << "blit_shader_" << m_effect;
82     s << "_" << m_width << "x" << m_height;
83
84     return s.str();
85 }
86
87 void ShaderBlitTest::prepare()
88 {
89     const char* vertSource = 
90         "precision mediump float;\n"
91         "attribute vec2 in_position;\n"
92         "attribute vec2 in_texcoord;\n"
93         "varying vec2 texcoord;\n"
94         "\n"
95         "void main()\n"
96         "{\n"
97         "       gl_Position = vec4(in_position, 0.0, 1.0);\n"
98         "       texcoord = in_texcoord;\n"
99         "}\n";
100
101     const char* fragSourceConst = 
102         "precision lowp float;\n"
103         "\n"
104         "void main()\n"
105         "{\n"
106         "       gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
107         "}\n";
108
109     const char* fragSourceLinGrad = 
110         "precision lowp float;\n"
111         "varying vec2 texcoord;\n"
112         "\n"
113         "void main()\n"
114         "{\n"
115         "       gl_FragColor = vec4(texcoord.x, 0.0, 0.0, 1.0);\n"
116         "}\n";
117
118     const char* fragSourceRadGrad = 
119         "precision mediump float;\n"
120         "varying vec2 texcoord;\n"
121         "\n"
122         "void main()\n"
123         "{\n"
124         "   float t = 1.0 - distance(texcoord, vec2(0.5, 0.5)) * 2.0;\n"
125         "       gl_FragColor = vec4(0.0, t, 0.0, 1.0);\n"
126         "}\n";
127
128     const char* fragSourcePalette = 
129         "precision mediump float;\n"
130         "varying vec2 texcoord;\n"
131         "uniform sampler2D texture;\n"
132         "uniform sampler2D paletteTexture;\n"
133         "\n"
134         "void main()\n"
135         "{\n"
136         "       float index = texture2D(texture, texcoord).a;\n"
137         "       gl_FragColor = texture2D(paletteTexture, vec2(index, 0.0));\n"
138         "}\n";
139
140     const char* fragSourceMask = 
141         "precision mediump float;\n"
142         "varying vec2 texcoord;\n"
143         "uniform sampler2D texture;\n"
144         "\n"
145         "void main()\n"
146         "{\n"
147         "       float mask   = texture2D(texture, texcoord).g;\n"
148         "       vec4 color   = texture2D(texture, texcoord + vec2(0, 64.0 / 128.0));\n"
149         "       gl_FragColor = vec4(color.rgb, mask);\n"
150         "}\n";
151
152     const char* fragSource = 0;
153     if (m_effect == "const")
154     {
155         fragSource = fragSourceConst;
156     }
157     else if (m_effect == "lingrad")
158     {
159         fragSource = fragSourceLinGrad;
160     }
161     else if (m_effect == "radgrad")
162     {
163         fragSource = fragSourceRadGrad;
164     }
165     else if (m_effect == "palette")
166     {
167         fragSource = fragSourcePalette;
168     }
169     else if (m_effect == "mask")
170     {
171         fragSource = fragSourceMask;
172     }
173     assert(fragSource);
174
175     m_program = createProgram(vertSource, fragSource);
176     glUseProgram(m_program);
177
178     glClearColor(.2, .4, .6, 1.0);
179
180     m_positionAttr = glGetAttribLocation(m_program, "in_position");
181     m_texcoordAttr = glGetAttribLocation(m_program, "in_texcoord");
182
183     glGenTextures(1, &m_texture);
184     glEnableVertexAttribArray(m_positionAttr);
185     glEnableVertexAttribArray(m_texcoordAttr);
186
187     if (m_effect == "palette")
188     {
189         char* texture = new char[800 * 480];
190         char* palette = new char[256 * 4];
191         for (int y = 0; y < 480; y++)
192         {
193             for (int x = 0; x < 800; x++)
194             {
195                 texture[y * 800 + x] = x ^ y;
196             }
197         }
198         for (int i = 0; i < 256; i++)
199         {
200             palette[i * 4 + 0] = i;
201             palette[i * 4 + 1] = abs(i - 0x7f);
202             palette[i * 4 + 2] = 0xff - i;
203             palette[i * 4 + 3] = 0xff;
204         }
205         glActiveTexture(GL_TEXTURE1);
206         glBindTexture(GL_TEXTURE_2D, 3);
207         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, palette);
208         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
209         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
210         ASSERT_GL();
211         glActiveTexture(GL_TEXTURE0);
212         glBindTexture(GL_TEXTURE_2D, 4);
213         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 800, 480, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture);
214         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
215         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
216         ASSERT_GL();
217         delete[] texture;
218         delete[] palette;
219         glUniform1i(glGetUniformLocation(m_program, "texture"), 0);
220         glUniform1i(glGetUniformLocation(m_program, "paletteTexture"), 1);
221         ASSERT_GL();
222     }
223
224     if (m_effect == "mask")
225     {
226         glBindTexture(GL_TEXTURE_2D, m_texture);
227         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
228         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
229         loadCompressedTexture(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, 128, 256, "data/xorg-colormask_128x256_etc1.raw");
230         glUniform1i(glGetUniformLocation(m_program, "texture"), 0);
231         ASSERT_GL();
232     }
233
234     ASSERT_GL();
235 }