2 * OpenGL ES 2.0 memory performance estimator
3 * Copyright (C) 2009 Nokia
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.
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.
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.
19 * \author Sami Kyöstilä <sami.kyostila@nokia.com>
23 #include "shaderblittest.h"
25 #include <GLES2/gl2ext.h>
27 ShaderBlitTest::ShaderBlitTest(const std::string& effect, int width, int height,
28 float texW, float texH,
29 float quadW, float quadH):
40 void ShaderBlitTest::teardown()
43 glDisableVertexAttribArray(m_positionAttr);
44 glDisableVertexAttribArray(m_texcoordAttr);
45 glDeleteProgram(m_program);
46 // Disabled until driver segfault is fixed
47 //glDeleteTextures(1, &m_texture);
50 void ShaderBlitTest::operator()(int frame)
54 const GLfloat texcoords[] =
58 m_texW + t, m_texH + t,
62 const GLfloat vertices[] =
70 glVertexAttribPointer(m_positionAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
71 glVertexAttribPointer(m_texcoordAttr, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
72 glClear(GL_COLOR_BUFFER_BIT);
73 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
76 std::string ShaderBlitTest::name() const
80 s << "blit_shader_" << m_effect;
81 s << "_" << m_width << "x" << m_height;
86 void ShaderBlitTest::prepare()
88 const char* vertSource =
89 "precision mediump float;\n"
90 "attribute vec2 in_position;\n"
91 "attribute vec2 in_texcoord;\n"
92 "varying vec2 texcoord;\n"
96 " gl_Position = vec4(in_position, 0.0, 1.0);\n"
97 " texcoord = in_texcoord;\n"
100 const char* fragSourceConst =
101 "precision lowp float;\n"
105 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
108 const char* fragSourceLinGrad =
109 "precision lowp float;\n"
110 "varying vec2 texcoord;\n"
114 " gl_FragColor = vec4(texcoord.x, 0.0, 0.0, 1.0);\n"
117 const char* fragSourceRadGrad =
118 "precision mediump float;\n"
119 "varying vec2 texcoord;\n"
123 " float t = 1.0 - distance(texcoord, vec2(0.5, 0.5)) * 2.0;\n"
124 " gl_FragColor = vec4(0.0, t, 0.0, 1.0);\n"
127 const char* fragSourcePalette =
128 "precision mediump float;\n"
129 "varying vec2 texcoord;\n"
130 "uniform sampler2D texture;\n"
131 "uniform sampler2D paletteTexture;\n"
135 " float index = texture2D(texture, texcoord).a;\n"
136 " gl_FragColor = texture2D(paletteTexture, vec2(index, 0.0));\n"
139 const char* fragSourceMask =
140 "precision mediump float;\n"
141 "varying vec2 texcoord;\n"
142 "uniform sampler2D texture;\n"
146 " float mask = texture2D(texture, texcoord).g;\n"
147 " vec4 color = texture2D(texture, texcoord + vec2(0, 64.0 / 128.0));\n"
148 " gl_FragColor = vec4(color.rgb, mask);\n"
151 const char* fragSource = 0;
152 if (m_effect == "const")
154 fragSource = fragSourceConst;
156 else if (m_effect == "lingrad")
158 fragSource = fragSourceLinGrad;
160 else if (m_effect == "radgrad")
162 fragSource = fragSourceRadGrad;
164 else if (m_effect == "palette")
166 fragSource = fragSourcePalette;
168 else if (m_effect == "mask")
170 fragSource = fragSourceMask;
174 m_program = createProgram(vertSource, fragSource);
175 glUseProgram(m_program);
177 glClearColor(.2, .4, .6, 1.0);
179 m_positionAttr = glGetAttribLocation(m_program, "in_position");
180 m_texcoordAttr = glGetAttribLocation(m_program, "in_texcoord");
182 glGenTextures(1, &m_texture);
183 glEnableVertexAttribArray(m_positionAttr);
184 glEnableVertexAttribArray(m_texcoordAttr);
186 if (m_effect == "palette")
188 char* texture = new char[800 * 480];
189 char* palette = new char[256 * 4];
190 for (int y = 0; y < 480; y++)
192 for (int x = 0; x < 800; x++)
194 texture[y * 800 + x] = x ^ y;
197 for (int i = 0; i < 256; i++)
199 palette[i * 4 + 0] = i;
200 palette[i * 4 + 1] = abs(i - 0x7f);
201 palette[i * 4 + 2] = 0xff - i;
202 palette[i * 4 + 3] = 0xff;
204 glActiveTexture(GL_TEXTURE1);
205 glBindTexture(GL_TEXTURE_2D, 3);
206 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, palette);
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
210 glActiveTexture(GL_TEXTURE0);
211 glBindTexture(GL_TEXTURE_2D, 4);
212 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 800, 480, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture);
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
214 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
218 glUniform1i(glGetUniformLocation(m_program, "texture"), 0);
219 glUniform1i(glGetUniformLocation(m_program, "paletteTexture"), 1);
223 if (m_effect == "mask")
225 glBindTexture(GL_TEXTURE_2D, m_texture);
226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
228 loadCompressedTexture(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, 128, 256, "data/xorg-colormask_128x256_etc1.raw");
229 glUniform1i(glGetUniformLocation(m_program, "texture"), 0);