OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #include <GLES2/gl2.h> | |
5 #include <GLES2/gl2ext.h> | |
6 #include <GLES2/gl2extchromium.h> | |
7 #include <math.h> | |
8 #include <stdio.h> | |
9 #include <string> | |
10 | |
11 // Some tests for compressed textures. | |
12 #include "gpu/demos/framework/demo_factory.h" | |
13 #include "gpu/demos/gles2_book/example.h" | |
14 | |
15 namespace gpu { | |
16 namespace demos { | |
17 | |
18 namespace { | |
19 | |
20 int g_frameCount = 0; | |
21 int g_textureIndex = 0; | |
22 int g_numTextures = 1; | |
23 GLuint g_textures[5]; | |
24 int g_textureLoc = -1; | |
25 GLuint g_programObject = 0; | |
26 GLuint g_vbo = 0; | |
27 | |
28 void CheckGLError(const char* func_name, int line_no) { | |
29 #ifndef NDEBUG | |
30 GLenum error = GL_NO_ERROR; | |
31 while ((error = glGetError()) != GL_NO_ERROR) { | |
32 fprintf(stderr, "GL ERROR in %s at line %d : 0x%04x\n", | |
33 func_name, line_no, error); | |
34 } | |
35 #endif | |
36 } | |
37 | |
38 GLuint LoadShader(GLenum type, const char* shaderSrc) { | |
39 CheckGLError("LoadShader", __LINE__); | |
40 GLuint shader = glCreateShader(type); | |
41 if (shader == 0) { | |
42 return 0; | |
43 } | |
44 // Load the shader source | |
45 glShaderSource(shader, 1, &shaderSrc, NULL); | |
46 // Compile the shader | |
47 glCompileShader(shader); | |
48 // Check the compile status | |
49 GLint value = 0; | |
50 glGetShaderiv(shader, GL_COMPILE_STATUS, &value); | |
51 if (value == 0) { | |
52 char buffer[1024]; | |
53 GLsizei length = 0; | |
54 glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer); | |
55 std::string log(buffer, length); | |
56 fprintf(stderr, "Error compiling shader: %s\n", log.c_str()); | |
57 glDeleteShader(shader); | |
58 return 0; | |
59 } | |
60 return shader; | |
61 } | |
62 | |
63 void InitShaders() { | |
64 static const char* vShaderStr = | |
65 "attribute vec2 g_Position;\n" | |
66 "varying vec2 texCoord;\n" | |
67 "void main()\n" | |
68 "{\n" | |
69 " gl_Position = vec4(g_Position.x, g_Position.y, 0.0, 1.0);\n" | |
70 " texCoord = g_Position * vec2(0.5) + vec2(0.5);\n" | |
71 "}\n"; | |
72 static const char* fShaderStr = | |
73 "precision mediump float;" | |
74 "uniform sampler2D tex;\n" | |
75 "varying vec2 texCoord;\n" | |
76 "void main()\n" | |
77 "{\n" | |
78 " gl_FragColor = texture2D(tex, texCoord);\n" | |
79 "}\n"; | |
80 | |
81 CheckGLError("InitShaders", __LINE__); | |
82 GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr); | |
83 GLuint fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr); | |
84 // Create the program object | |
85 GLuint programObject = glCreateProgram(); | |
86 if (programObject == 0) { | |
87 fprintf(stderr, "Creating program failed\n"); | |
88 return; | |
89 } | |
90 glAttachShader(programObject, vertexShader); | |
91 glAttachShader(programObject, fragmentShader); | |
92 // Bind g_Position to attribute 0 | |
93 glBindAttribLocation(programObject, 0, "g_Position"); | |
94 // Link the program | |
95 glLinkProgram(programObject); | |
96 // Check the link status | |
97 GLint linked = 0; | |
98 glGetProgramiv(programObject, GL_LINK_STATUS, &linked); | |
99 if (linked == 0) { | |
100 char buffer[1024]; | |
101 GLsizei length = 0; | |
102 glGetProgramInfoLog(programObject, sizeof(buffer), &length, buffer); | |
103 std::string log(buffer, length); | |
104 fprintf(stderr, "Error linking program: %s\n", log.c_str()); | |
105 glDeleteProgram(programObject); | |
106 return; | |
107 } | |
108 g_programObject = programObject; | |
109 g_textureLoc = glGetUniformLocation(g_programObject, "tex"); | |
110 glGenBuffers(1, &g_vbo); | |
111 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); | |
112 static float vertices[] = { | |
113 1.0f, 1.0f, | |
114 -1.0f, 1.0f, | |
115 -1.0f, -1.0f, | |
116 1.0f, 1.0f, | |
117 -1.0f, -1.0f, | |
118 1.0f, -1.0f | |
119 }; | |
120 glBufferData(GL_ARRAY_BUFFER, | |
121 sizeof(vertices), | |
122 vertices, | |
123 GL_STATIC_DRAW); | |
124 CheckGLError("InitShaders", __LINE__); | |
125 } | |
126 | |
127 GLuint CreateCheckerboardTexture() { | |
128 CheckGLError("CreateCheckerboardTexture", __LINE__); | |
129 static unsigned char pixels[] = { | |
130 255, 255, 255, | |
131 0, 0, 0, | |
132 0, 0, 0, | |
133 255, 255, 255, | |
134 }; | |
135 GLuint texture; | |
136 glGenTextures(1, &texture); | |
137 glBindTexture(GL_TEXTURE_2D, texture); | |
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
142 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
143 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, | |
144 pixels); | |
145 CheckGLError("CreateCheckerboardTexture", __LINE__); | |
146 return texture; | |
147 } | |
148 | |
149 GLuint CreateCompressedTexture( | |
150 const void* data, | |
151 GLsizeiptr size, | |
152 GLenum format, | |
153 GLint width, | |
154 GLint height) { | |
155 GLuint texture; | |
156 glGenTextures(1, &texture); | |
157 glBindTexture(GL_TEXTURE_2D, texture); | |
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
162 glCompressedTexImage2D( | |
163 GL_TEXTURE_2D, 0, format, width, height, 0, size, data); | |
164 CheckGLError("CreateCompressedTxture", __LINE__); | |
165 return texture; | |
166 } | |
167 | |
168 GLuint LoadDXT1RGBTexture() { | |
169 static unsigned char data[] = { | |
170 0x00, 0xd0, 0x00, 0xef, 0x00, 0xaa, 0x95, 0xd5, | |
171 0x00, 0x90, 0x57, 0xff, 0x00, 0xaa, 0xff, 0x55, | |
172 0x00, 0x88, 0x99, 0xff, 0x00, 0xaa, 0xff, 0x55, | |
173 0x20, 0xb8, 0xe4, 0xf6, 0x00, 0xaa, 0x5b, 0x5d, | |
174 0x21, 0x09, 0xe6, 0x27, 0x15, 0x15, 0x15, 0x15, | |
175 0xd7, 0xbd, 0xff, 0xff, 0x56, 0x16, 0x16, 0x56, | |
176 0x00, 0x00, 0xff, 0xff, 0x55, 0x00, 0x00, 0x55, | |
177 0xe0, 0x08, 0xa9, 0x2f, 0x51, 0x58, 0x56, 0x54, | |
178 0xff, 0x07, 0x15, 0x09, 0x40, 0x6a, 0xd5, 0xd5, | |
179 0xd7, 0xbd, 0xff, 0xff, 0x56, 0x16, 0x16, 0x16, | |
180 0x91, 0x08, 0xff, 0xff, 0x55, 0xff, 0x00, 0x03, | |
181 0xfe, 0x07, 0x5b, 0x09, 0x01, 0xa9, 0x55, 0xff, | |
182 0x0d, 0x10, 0x18, 0xe8, 0xea, 0x15, 0x95, 0x55, | |
183 0x08, 0x88, 0x3c, 0xe7, 0x55, 0x55, 0xff, 0x00, | |
184 0x10, 0x20, 0x18, 0xe8, 0xa8, 0x54, 0x56, 0x55, | |
185 0x1f, 0x78, 0x15, 0xf8, 0x00, 0xaa, 0x55, 0x55, | |
186 }; | |
187 return CreateCompressedTexture( | |
188 data, sizeof(data), GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 16, 16); | |
189 } | |
190 | |
191 GLuint LoadDXT1RGBATexture() { | |
192 static unsigned char data[] = { | |
193 0xff, 0xff, 0x90, 0xee, 0x00, 0xaa, 0xff, 0x55, | |
194 0x53, 0xde, 0xdd, 0xff, 0x55, 0x55, 0x00, 0xff, | |
195 0xc9, 0xb4, 0xdd, 0xff, 0x55, 0x55, 0xaa, 0xff, | |
196 0x6f, 0xee, 0xdd, 0xff, 0x55, 0x55, 0xa8, 0x03, | |
197 0x60, 0xa3, 0xa5, 0xe5, 0x55, 0x55, 0xaa, 0x00, | |
198 0x00, 0x00, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, | |
199 0x80, 0xab, 0xc2, 0xc4, 0xff, 0x55, 0xaa, 0xff, | |
200 0x60, 0x9b, 0xa5, 0xe5, 0x57, 0x56, 0xaa, 0x00, | |
201 0x1f, 0xbf, 0xff, 0xf7, 0x55, 0x55, 0xaa, 0x00, | |
202 0x00, 0x00, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, | |
203 0xfe, 0xbe, 0x7e, 0xdf, 0xff, 0xaa, 0x55, 0x00, | |
204 0xfe, 0xbe, 0xff, 0xf7, 0x54, 0x56, 0xaa, 0x00, | |
205 0xfa, 0x4c, 0x7e, 0x9e, 0x55, 0xaa, 0x00, 0x00, | |
206 0xbb, 0x2c, 0x98, 0x54, 0xff, 0xff, 0x55, 0xaa, | |
207 0xfa, 0x4c, 0x7e, 0x9e, 0x55, 0xaa, 0x00, 0x00, | |
208 0xfa, 0x4c, 0x7e, 0x9e, 0x55, 0xaa, 0x00, 0x00, | |
209 }; | |
210 return CreateCompressedTexture( | |
211 data, sizeof(data), GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 16, 16); | |
212 } | |
213 | |
214 GLuint LoadDXT3RGBATexture() { | |
215 static unsigned char data[] = { | |
216 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
217 0xa3, 0x22, 0x03, 0x03, 0x55, 0xff, 0xaa, 0x00, | |
218 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, | |
219 0xcf, 0x7c, 0xc3, 0x12, 0x55, 0x55, 0x55, 0x54, | |
220 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, | |
221 0x83, 0x1a, 0x03, 0x03, 0x55, 0xff, 0x00, 0x00, | |
222 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, | |
223 0xcf, 0x7c, 0xc3, 0x12, 0x55, 0x55, 0x55, 0x54, | |
224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
225 0xc3, 0x59, 0x63, 0x32, 0x55, 0xff, 0xaa, 0x00, | |
226 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, | |
227 0x8f, 0x94, 0x03, 0x4a, 0x54, 0x94, 0x94, 0x54, | |
228 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, | |
229 0xa3, 0x59, 0x43, 0x2a, 0x55, 0xff, 0xaa, 0x00, | |
230 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, | |
231 0xaf, 0x84, 0x03, 0x42, 0x54, 0x55, 0x55, 0x55, | |
232 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
233 0xc3, 0xa0, 0x83, 0x71, 0x55, 0xff, 0xaa, 0x00, | |
234 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, | |
235 0x0f, 0xb4, 0x43, 0x81, 0x54, 0x94, 0x94, 0x94, | |
236 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
237 0xc3, 0xa0, 0x83, 0x69, 0x55, 0xff, 0xaa, 0x00, | |
238 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
239 0xc3, 0xa0, 0x83, 0x69, 0x55, 0xfd, 0xaa, 0x00, | |
240 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
241 0x23, 0xd0, 0xa3, 0xb0, 0x55, 0xff, 0xaa, 0x00, | |
242 0x00, 0x40, 0x00, 0x40, 0xff, 0xff, 0xff, 0xff, | |
243 0xf0, 0xc3, 0x43, 0xc0, 0x94, 0x94, 0x55, 0x55, | |
244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
245 0x23, 0xd0, 0xa3, 0xb0, 0x55, 0xff, 0xaa, 0x00, | |
246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
247 0x23, 0xd0, 0xa3, 0xb0, 0x55, 0xff, 0xaa, 0x00, | |
248 }; | |
249 return CreateCompressedTexture( | |
250 data, sizeof(data), GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 16, 16); | |
251 } | |
252 | |
253 GLuint LoadDXT5RGBATexture() { | |
254 static unsigned char data[] = { | |
255 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
256 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, | |
257 0x04, 0xfb, 0xff, 0xff, 0xff, 0x49, 0x02, 0xdb, | |
258 0x97, 0xf6, 0x32, 0xcd, 0xc0, 0xc0, 0x7b, 0xc0, | |
259 0xfb, 0xff, 0x49, 0x92, 0x24, 0x00, 0x60, 0xdb, | |
260 0x11, 0xcd, 0x67, 0x82, 0x78, 0x78, 0x5e, 0x78, | |
261 0x04, 0xfb, 0xff, 0xff, 0xff, 0xf9, 0x8f, 0xff, | |
262 0x2e, 0xac, 0xc4, 0x71, 0x15, 0x15, 0x15, 0x14, | |
263 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
264 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, | |
265 0x04, 0x43, 0xb0, 0x0d, 0x3b, 0xb0, 0x03, 0xdb, | |
266 0xb8, 0xf6, 0xd5, 0xd5, 0x60, 0x60, 0x60, 0x60, | |
267 0xfb, 0x00, 0x49, 0x02, 0x00, 0x00, 0x90, 0x24, | |
268 0xb0, 0xbc, 0x26, 0x7a, 0x78, 0x78, 0x78, 0x78, | |
269 0x04, 0xf7, 0xf8, 0x9f, 0xff, 0xf9, 0x9f, 0xff, | |
270 0x0e, 0xac, 0x83, 0x69, 0x34, 0x35, 0x35, 0x35, | |
271 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
272 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, | |
273 0x04, 0x43, 0xb0, 0x0d, 0x3b, 0xb0, 0x03, 0x3b, | |
274 0xb8, 0xf6, 0xd5, 0xd5, 0x60, 0x60, 0x60, 0x60, | |
275 0xfb, 0xff, 0xb6, 0x0d, 0x00, 0x49, 0x92, 0x24, | |
276 0x11, 0xcd, 0x67, 0x82, 0x78, 0x5e, 0x78, 0x78, | |
277 0xea, 0xff, 0x4a, 0xd2, 0x24, 0x49, 0x92, 0x24, | |
278 0x0e, 0xac, 0xc4, 0x71, 0x15, 0x15, 0x15, 0x15, | |
279 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
280 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, | |
281 0xfd, 0x00, 0x49, 0x9c, 0xc4, 0x00, 0x00, 0x00, | |
282 0xb8, 0xf6, 0x53, 0xcd, 0xe0, 0xe0, 0x7f, 0xe0, | |
283 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
284 0xf1, 0xcc, 0x46, 0x82, 0x78, 0x78, 0x78, 0x78, | |
285 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
286 0x0e, 0xac, 0xc4, 0x71, 0x15, 0x15, 0x15, 0x15, | |
287 }; | |
288 return CreateCompressedTexture( | |
289 data, sizeof(data), GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 16, 16); | |
290 } | |
291 | |
292 } // anonymous namespace. | |
293 | |
294 static int stInit(ESContext *esContext) { | |
295 CheckGLError("GLFromCPPInit", __LINE__); | |
296 g_textures[0] = CreateCheckerboardTexture(); | |
297 glClearColor(0.f, 0.f, .7f, 1.f); | |
298 const char* extensions = | |
299 reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); | |
300 if (strstr(extensions, "GL_EXT_texture_compression_dxt1")) { | |
301 g_textures[g_numTextures++] = LoadDXT1RGBTexture(); | |
302 g_textures[g_numTextures++] = LoadDXT1RGBATexture(); | |
303 } | |
304 if (strstr(extensions, "GL_CHROMIUM_texture_compression_dxt3")) { | |
305 g_textures[g_numTextures++] = LoadDXT3RGBATexture(); | |
306 } | |
307 if (strstr(extensions, "GL_CHROMIUM_texture_compression_dxt5")) { | |
308 g_textures[g_numTextures++] = LoadDXT5RGBATexture(); | |
309 } | |
310 InitShaders(); | |
311 CheckGLError("GLFromCPPInit", __LINE__); | |
312 return 1; | |
313 } | |
314 | |
315 static void stDraw (ESContext *esContext) { | |
316 CheckGLError("GLFromCPPDraw", __LINE__); | |
317 | |
318 g_frameCount++; | |
319 if (g_frameCount > 60) | |
320 { | |
321 g_frameCount = 0; | |
322 g_textureIndex = (g_textureIndex + 1) % g_numTextures; | |
323 } | |
324 | |
325 glViewport(0, 0, esContext->width, esContext->height); | |
326 // Clear the color buffer | |
327 glClear(GL_COLOR_BUFFER_BIT); | |
328 glEnable(GL_BLEND); | |
329 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
330 CheckGLError("GLFromCPPDraw", __LINE__); | |
331 // Use the program object | |
332 glUseProgram(g_programObject); | |
333 CheckGLError("GLFromCPPDraw", __LINE__); | |
334 | |
335 // Load the vertex data | |
336 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); | |
337 glEnableVertexAttribArray(0); | |
338 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
339 CheckGLError("GLFromCPPDraw", __LINE__); | |
340 // Bind the texture to texture unit 0 | |
341 glBindTexture(GL_TEXTURE_2D, g_textures[g_textureIndex]); | |
342 CheckGLError("GLFromCPPDraw", __LINE__); | |
343 // Point the uniform sampler to texture unit 0 | |
344 glUniform1i(g_textureLoc, 0); | |
345 CheckGLError("GLFromCPPDraw", __LINE__); | |
346 glDrawArrays(GL_TRIANGLES, 0, 6); | |
347 CheckGLError("GLFromCPPDraw", __LINE__); | |
348 glFlush(); | |
349 } | |
350 | |
351 static void stShutDown (ESContext *esContext) { | |
352 } | |
353 | |
354 struct STUserData { | |
355 int dummy; | |
356 }; | |
357 | |
358 class CompressedTextureTest : public gles2_book::Example<STUserData> { | |
359 public: | |
360 CompressedTextureTest() { | |
361 RegisterCallbacks(stInit, NULL, stDraw, stShutDown); | |
362 } | |
363 | |
364 const wchar_t* Title() const { | |
365 return L"Compressed Texture Test"; | |
366 } | |
367 | |
368 bool IsAnimated() { | |
369 return true; | |
370 } | |
371 }; | |
372 | |
373 Demo* CreateDemo() { | |
374 return new CompressedTextureTest(); | |
375 } | |
376 | |
377 } // namespace demos | |
378 } // namespace gpu | |
379 | |
380 | |
OLD | NEW |