1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
//
// Book: OpenGL(R) ES 2.0 Programming Guide
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
// ISBN-10: 0321502795
// ISBN-13: 9780321502797
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780321563835
// http://www.opengles-book.com
//
// Stencil_Test.c
//
// This example shows various stencil buffer
// operations.
//
#define HOOK
#include <stdlib.h>
#include "esUtil.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdarg.h>
#include "esTransform.h"
#include "eglutil.h"
#include "dump_gl_screen.h"
#include "viv_hook.h"
static EGLint const config_attribute_list[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 8,
EGL_NONE
};
static EGLint const pbuffer_attribute_list[] = {
EGL_WIDTH, 400,
EGL_HEIGHT, 240,
EGL_LARGEST_PBUFFER, EGL_TRUE,
EGL_NONE
};
static const EGLint context_attribute_list[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
int main(int argc, char *argv[])
{
EGLDisplay display;
EGLint egl_major, egl_minor;
EGLConfig config;
EGLint num_config;
EGLContext context;
EGLSurface surface;
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
GLint ret;
GLint width, height;
#ifdef HOOK
the_hook("/mnt/sdcard/egl2.fdr");
#endif
const char *vertex_shader_source =
"attribute vec4 a_position; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
"} \n";
const char *fragment_shader_source =
"precision mediump float; \n"
"uniform vec4 u_color; \n"
"void main() \n"
"{ \n"
" gl_FragColor = u_color; \n"
"} \n";
GLfloat vVertices[] = {
-0.75f, 0.25f, 0.50f, // Quad #0
-0.25f, 0.25f, 0.50f,
-0.25f, 0.75f, 0.50f,
-0.75f, 0.75f, 0.50f,
0.25f, 0.25f, 0.90f, // Quad #1
0.75f, 0.25f, 0.90f,
0.75f, 0.75f, 0.90f,
0.25f, 0.75f, 0.90f,
-0.75f, -0.75f, 0.50f, // Quad #2
-0.25f, -0.75f, 0.50f,
-0.25f, -0.25f, 0.50f,
-0.75f, -0.25f, 0.50f,
0.25f, -0.75f, 0.50f, // Quad #3
0.75f, -0.75f, 0.50f,
0.75f, -0.25f, 0.50f,
0.25f, -0.25f, 0.50f,
-1.00f, -1.00f, 0.00f, // Big Quad
1.00f, -1.00f, 0.00f,
1.00f, 1.00f, 0.00f,
-1.00f, 1.00f, 0.00f
};
GLubyte indices[][6] = {
{ 0, 1, 2, 0, 2, 3 }, // Quad #0
{ 4, 5, 6, 4, 6, 7 }, // Quad #1
{ 8, 9, 10, 8, 10, 11 }, // Quad #2
{ 12, 13, 14, 12, 14, 15 }, // Quad #3
{ 16, 17, 18, 16, 18, 19 } // Big Quad
};
#define NumTests 4
GLfloat colors[NumTests][4] = {
{ 1.0f, 0.0f, 0.0f, 1.0f },
{ 0.0f, 1.0f, 0.0f, 1.0f },
{ 0.0f, 0.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f, 0.0f }
};
GLint numStencilBits;
GLuint stencilValues[NumTests] = {
0x7, // Result of test 0
0x0, // Result of test 1
0x2, // Result of test 2
0xff // Result of test 3. We need to fill this
// value in a run-time
};
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
printf("Error: No display found!\n");
return -1;
}
if (!eglInitialize(display, &egl_major, &egl_minor)) {
printf("Error: eglInitialise failed!\n");
return -1;
}
printf("Using display %p with EGL version %d.%d\n",
display, egl_major, egl_minor);
printf("EGL Version \"%s\"\n", eglQueryString(display, EGL_VERSION));
printf("EGL Vendor \"%s\"\n", eglQueryString(display, EGL_VENDOR));
printf("EGL Extensions \"%s\"\n", eglQueryString(display, EGL_EXTENSIONS));
/* get an appropriate EGL frame buffer configuration */
eglChooseConfig(display, config_attribute_list, &config, 1, &num_config);
/* create an EGL rendering context */
context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribute_list);
if (context == EGL_NO_CONTEXT) {
printf("Error: eglCreateContext failed: %d\n", eglGetError());
return -1;
}
surface = eglCreatePbufferSurface(display, config, pbuffer_attribute_list);
if (surface == EGL_NO_SURFACE) {
printf("Error: eglCreatePbufferSurface failed: %d (%s)\n",
eglGetError(), eglStrError(eglGetError()));
return -1;
}
if (!eglQuerySurface(display, surface, EGL_WIDTH, &width) ||
!eglQuerySurface(display, surface, EGL_HEIGHT, &height)) {
printf("Error: eglQuerySurface failed: %d (%s)\n",
eglGetError(), eglStrError(eglGetError()));
return -1;
}
printf("PBuffer: %dx%d\n", width, height);
printf("GL Extensions \"%s\"\n", glGetString(GL_EXTENSIONS));
/* connect the context to the surface */
if (!eglMakeCurrent(display, surface, surface, context)) {
printf("Error: eglMakeCurrent() failed: %d (%s)\n",
eglGetError(), eglStrError(eglGetError()));
return -1;
}
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
if (!vertex_shader) {
printf("Error: glCreateShader(GL_VERTEX_SHADER) failed: %d (%s)\n",
eglGetError(), eglStrError(eglGetError()));
return -1;
}
glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &ret);
if (!ret) {
char *log;
printf("Error: vertex shader compilation failed!:\n");
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &ret);
if (ret > 1) {
log = malloc(ret);
glGetShaderInfoLog(vertex_shader, ret, NULL, log);
printf("%s", log);
}
return -1;
} else
printf("Vertex shader compilation succeeded!\n");
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
if (!fragment_shader) {
printf("Error: glCreateShader(GL_FRAGMENT_SHADER) failed: %d (%s)\n",
eglGetError(), eglStrError(eglGetError()));
return -1;
}
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &ret);
if (!ret) {
char *log;
printf("Error: fragment shader compilation failed!:\n");
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &ret);
if (ret > 1) {
log = malloc(ret);
glGetShaderInfoLog(fragment_shader, ret, NULL, log);
printf("%s", log);
}
return -1;
} else
printf("Fragment shader compilation succeeded!\n");
program = glCreateProgram();
if (!program) {
printf("Error: failed to create program!\n");
return -1;
}
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glBindAttribLocation(program, 0, "in_position");
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &ret);
if (!ret) {
char *log;
printf("Error: program linking failed!:\n");
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &ret);
if (ret > 1) {
log = malloc(ret);
glGetProgramInfoLog(program, ret, NULL, log);
printf("%s", log);
}
return -1;
} else
printf("program linking succeeded!\n");
GLint colorLoc = glGetUniformLocation(program, "u_color");
glUseProgram(program);
glViewport(0, 0, width, height);
// Set the clear color
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
// Set the stencil clear value
glClearStencil ( 0x1 );
// Set the depth clear value
glClearDepthf( 0.75f );
// Enable the depth and stencil tests
glEnable( GL_DEPTH_TEST );
glEnable( GL_STENCIL_TEST );
/* clear the color buffer */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Load the vertex position
glVertexAttribPointer ( 0, 3, GL_FLOAT,
GL_FALSE, 0, vVertices );
glEnableVertexAttribArray ( 0 );
// Test 0:
//
// Initialize upper-left region. In this case, the
// stencil-buffer values will be replaced because the
// stencil test for the rendered pixels will fail the
// stencil test, which is
//
// ref mask stencil mask
// ( 0x7 & 0x3 ) < ( 0x1 & 0x3 )
//
// The value in the stencil buffer for these pixels will
// be 0x7.
//
glStencilFunc( GL_LESS, 0x7, 0x3 );
glStencilOp( GL_REPLACE, GL_DECR, GL_DECR );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[0] );
// Test 1:
//
// Initialize the upper-right region. Here, we'll decrement
// the stencil-buffer values where the stencil test passes
// but the depth test fails. The stencil test is
//
// ref mask stencil mask
// ( 0x3 & 0x3 ) > ( 0x1 & 0x3 )
//
// but where the geometry fails the depth test. The
// stencil values for these pixels will be 0x0.
//
glStencilFunc( GL_GREATER, 0x3, 0x3 );
glStencilOp( GL_KEEP, GL_DECR, GL_KEEP );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[1] );
// Test 2:
//
// Initialize the lower-left region. Here we'll increment
// (with saturation) the stencil value where both the
// stencil and depth tests pass. The stencil test for
// these pixels will be
//
// ref mask stencil mask
// ( 0x1 & 0x3 ) == ( 0x1 & 0x3 )
//
// The stencil values for these pixels will be 0x2.
//
glStencilFunc( GL_EQUAL, 0x1, 0x3 );
glStencilOp( GL_KEEP, GL_INCR, GL_INCR );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[2] );
// Test 3:
//
// Finally, initialize the lower-right region. We'll invert
// the stencil value where the stencil tests fails. The
// stencil test for these pixels will be
//
// ref mask stencil mask
// ( 0x2 & 0x1 ) == ( 0x1 & 0x1 )
//
// The stencil value here will be set to ~((2^s-1) & 0x1),
// (with the 0x1 being from the stencil clear value),
// where 's' is the number of bits in the stencil buffer
//
glStencilFunc( GL_EQUAL, 0x2, 0x1 );
glStencilOp( GL_INVERT, GL_KEEP, GL_KEEP );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[3] );
// Since we don't know at compile time how many stecil bits are present,
// we'll query, and update the value correct value in the
// stencilValues arrays for the fourth tests. We'll use this value
// later in rendering.
glGetIntegerv( GL_STENCIL_BITS, &numStencilBits );
printf("num stencil bits: %i\n", numStencilBits);
stencilValues[3] = ~(((1 << numStencilBits) - 1) & 0x1) & 0xff;
// Use the stencil buffer for controlling where rendering will
// occur. We diable writing to the stencil buffer so we
// can test against them without modifying the values we
// generated.
glStencilMask( 0x0 );
for ( int i = 0; i < NumTests; ++i )
{
glStencilFunc( GL_EQUAL, stencilValues[i], 0xff );
glUniform4fv( colorLoc, 1, colors[i] );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[4] );
}
glFlush();
fflush(stdout);
dump_gl_screen("/sdcard/egl2.bmp", width, height);
#ifdef HOOK
close_hook();
#endif
return 0;
}
|