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>
28 ShaderBlitTest::ShaderBlitTest(const std::string& effect, int width, int height,
29 float texW, float texH,
30 float quadW, float quadH):
41 void ShaderBlitTest::teardown()
44 glDisableVertexAttribArray(m_positionAttr);
45 glDisableVertexAttribArray(m_texcoordAttr);
46 glDeleteProgram(m_program);
47 // Disabled until driver segfault is fixed
48 //glDeleteTextures(1, &m_texture);
51 void ShaderBlitTest::operator()(int frame)
55 const GLfloat texcoords[] =
59 m_texW + t, m_texH + t,
63 const GLfloat vertices[] =
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);
77 std::string ShaderBlitTest::name() const
81 s << "blit_shader_" << m_effect;
82 s << "_" << m_width << "x" << m_height;
87 void ShaderBlitTest::prepare()
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"
97 " gl_Position = vec4(in_position, 0.0, 1.0);\n"
98 " texcoord = in_texcoord;\n"
101 const char* fragSourceConst =
102 "precision lowp float;\n"
106 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
109 const char* fragSourceLinGrad =
110 "precision lowp float;\n"
111 "varying vec2 texcoord;\n"
115 " gl_FragColor = vec4(texcoord.x, 0.0, 0.0, 1.0);\n"
118 const char* fragSourceRadGrad =
119 "precision mediump float;\n"
120 "varying vec2 texcoord;\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"
128 const char* fragSourcePalette =
129 "precision mediump float;\n"
130 "varying vec2 texcoord;\n"
131 "uniform sampler2D texture;\n"
132 "uniform sampler2D paletteTexture;\n"
136 " float index = texture2D(texture, texcoord).a;\n"
137 " gl_FragColor = texture2D(paletteTexture, vec2(index, 0.0));\n"
140 const char* fragSourceMask =
141 "precision mediump float;\n"
142 "varying vec2 texcoord;\n"
143 "uniform sampler2D texture;\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"
152 const char* fragSource = 0;
153 if (m_effect == "const")
155 fragSource = fragSourceConst;
157 else if (m_effect == "lingrad")
159 fragSource = fragSourceLinGrad;
161 else if (m_effect == "radgrad")
163 fragSource = fragSourceRadGrad;
165 else if (m_effect == "palette")
167 fragSource = fragSourcePalette;
169 else if (m_effect == "mask")
171 fragSource = fragSourceMask;
175 m_program = createProgram(vertSource, fragSource);
176 glUseProgram(m_program);
178 glClearColor(.2, .4, .6, 1.0);
180 m_positionAttr = glGetAttribLocation(m_program, "in_position");
181 m_texcoordAttr = glGetAttribLocation(m_program, "in_texcoord");
183 glGenTextures(1, &m_texture);
184 glEnableVertexAttribArray(m_positionAttr);
185 glEnableVertexAttribArray(m_texcoordAttr);
187 if (m_effect == "palette")
189 char* texture = new char[800 * 480];
190 char* palette = new char[256 * 4];
191 for (int y = 0; y < 480; y++)
193 for (int x = 0; x < 800; x++)
195 texture[y * 800 + x] = x ^ y;
198 for (int i = 0; i < 256; i++)
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;
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);
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);
219 glUniform1i(glGetUniformLocation(m_program, "texture"), 0);
220 glUniform1i(glGetUniformLocation(m_program, "paletteTexture"), 1);
224 if (m_effect == "mask")
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);