File: test_webgl_context_attributes_common.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (307 lines) | stat: -rw-r--r-- 11,581 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2013 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>

#include <emscripten.h>
#include <emscripten/console.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

static const int WINDOWS_SIZE = 500;

static GLfloat vertices[] = { 0.0f,  250.f, 0.0f,
                             -250.f, -250.f, 0.0f,
                              250.f, -250.f, 0.0f };

static GLfloat vertices2[] = { 0.0f,  250.f, -1.0f,
                              -250.f, -250.f, -1.0f,
                               250.f, -250.f, -1.0f };

static GLuint shaderProgram = 0;
static GLuint verticesVBO = 0;
static GLuint verticesVBO2 = 0;

static unsigned char backgroundColor[4] = {255, 255, 255, 255};
static unsigned char triangleColor[4] = {255, 0, 0, 255};
static unsigned char triangleColor2[4] = {0, 255, 0, 255};

static char vertexShaderSrc[] =
    "precision highp float;"
    "precision highp int;"

    "uniform mat4 u_mvpMatrix;"
    "uniform vec4 u_color;"

    "attribute vec3 a_position;"

    "varying vec4 v_color;"

    "void main() {"
    "   gl_Position = u_mvpMatrix * vec4(a_position, 1.0);"
    "   v_color = u_color;"
    "}"
    ;

static char fragmentShaderSrc[] =
    "precision highp float;"
    "precision highp int;"

    "varying vec4 v_color;"

    "void main() {"
    "  gl_FragColor = v_color;"
    "}"
    ;

static GLuint createShader(const char *source, int type) {
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, (const GLchar**)(&source), NULL);
    glCompileShader(shader);
    return shader;
}

static GLuint createShaderProgram(const char *vertexShaderSrc, const char *fragmentShaderSrc) {
    GLuint program = glCreateProgram();
    glAttachShader(program, createShader(vertexShaderSrc, GL_VERTEX_SHADER));
    glAttachShader(program, createShader(fragmentShaderSrc, GL_FRAGMENT_SHADER));
    glLinkProgram(program);
    return program;
}

void ortho(float  left,  float  right,  float  bottom,  float  top,  float  nearVal,  float  farVal, GLfloat *projMatrix) {
    float tx = -(right+left)/(right-left);
    float ty = -(top+bottom)/(top-bottom);
    float tz = -(farVal+nearVal)/(farVal-nearVal);
    memset(projMatrix, 0, 16 * sizeof(GLfloat));
    projMatrix[0] = 2.0f / (right-left);
    projMatrix[3] = tx;
    projMatrix[1*4+1] = 2.0f / (top-bottom);
    projMatrix[1*4+3] = ty;
    projMatrix[2*4+2] = -2.0f / (farVal-nearVal);
    projMatrix[2*4+3] = tz;
    projMatrix[3*4+3] = 1.0f;
}

static void initGlObjects() {
    glGenBuffers(1, &verticesVBO);
    glBindBuffer(GL_ARRAY_BUFFER, verticesVBO);
    glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &verticesVBO2);
    glBindBuffer(GL_ARRAY_BUFFER, verticesVBO2);
    glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), vertices2, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    shaderProgram = createShaderProgram(vertexShaderSrc, fragmentShaderSrc);
}

static void drawTriangle(GLuint verticesVBO, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
    glUseProgram(shaderProgram);
    GLuint posLoc = glGetAttribLocation(shaderProgram, "a_position");
    GLuint mvpLoc = glGetUniformLocation(shaderProgram, "u_mvpMatrix");
    GLuint colorLoc = glGetUniformLocation(shaderProgram, "u_color");

    GLfloat mvpMat[16];
    ortho(-WINDOWS_SIZE/2, WINDOWS_SIZE/2, -WINDOWS_SIZE/2, WINDOWS_SIZE/2, -100, 100, mvpMat);

    glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, mvpMat);
    glUniform4f(colorLoc, r/255.f, g/255.f, b/255.f, a/255.f);

    glBindBuffer(GL_ARRAY_BUFFER, verticesVBO);
    glEnableVertexAttribArray(posLoc);
    glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), BUFFER_OFFSET(0));

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glUseProgram(0);
}

// Draw a red triangle on a white background. If antialiasing is disabled, resulting pixels
// will only have white and red colors. If antialiasing is enabled, there will be pixels
// whose color is different from red and white.
static bool testAntiAliasing(bool activated) {
    glViewport(0, 0, WINDOWS_SIZE, WINDOWS_SIZE);
    glClearColor(backgroundColor[0]/255.f, backgroundColor[1]/255.f, backgroundColor[2]/255.f, backgroundColor[3]/255.f);
    glClear(GL_COLOR_BUFFER_BIT);

    drawTriangle(verticesVBO, triangleColor[0], triangleColor[1], triangleColor[2], triangleColor[3]);

    bool antialiased = false;

    unsigned char buffer[(WINDOWS_SIZE*WINDOWS_SIZE)*4];
    glReadPixels(0, 0, WINDOWS_SIZE, WINDOWS_SIZE, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
    glFinish();
    for (unsigned int i = 0 ; i < WINDOWS_SIZE ; ++i) {
      for (unsigned int j = 0 ; j < WINDOWS_SIZE ; ++j) {
        unsigned char r = buffer[4*(i*WINDOWS_SIZE+j)];
        unsigned char g = buffer[4*(i*WINDOWS_SIZE+j)+1];
        unsigned char b = buffer[4*(i*WINDOWS_SIZE+j)+2];
        unsigned char a = buffer[4*(i*WINDOWS_SIZE+j)+3];
        if ((r == backgroundColor[0] && g == backgroundColor[1] && b == backgroundColor[2] && a == backgroundColor[3]) ||
            (r == triangleColor[0] && g == triangleColor[1] && b == triangleColor[2] && a == triangleColor[3])) {
          continue;
        } else {
          antialiased = true;
          break;
        }
      }
    }

    return (activated && antialiased) || (!activated && !antialiased);
}

// Draw a red triangle with depth equals to 0 then a green triangle whose depth equals -1.
// If there is an attached depth buffer, the resulting image will be a red triangle. If not,
// the resulting image will be a green triangle.
static bool testDepth(bool activated) {
    glViewport(0, 0, WINDOWS_SIZE, WINDOWS_SIZE);
    glClearColor(backgroundColor[0]/255.f, backgroundColor[1]/255.f, backgroundColor[2]/255.f, backgroundColor[3]/255.f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    drawTriangle(verticesVBO, triangleColor[0], triangleColor[1], triangleColor[2], triangleColor[3]);
    drawTriangle(verticesVBO2, triangleColor2[0], triangleColor2[1], triangleColor2[2], triangleColor2[3]);

    glDisable(GL_DEPTH_TEST);

    // read the pixel at the center of the resulting image.
    unsigned char buffer[4];
    glReadPixels(WINDOWS_SIZE/2, WINDOWS_SIZE/2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);

    bool frontTriangleColor = (buffer[0] == triangleColor[0] && buffer[1] == triangleColor[1] &&
                               buffer[2] == triangleColor[2] && buffer[3] == triangleColor[3]);

    bool backTriangleColor = (buffer[0] == triangleColor2[0] && buffer[1] == triangleColor2[1] &&
                              buffer[2] == triangleColor2[2] && buffer[3] == triangleColor2[3]);

    return (activated && frontTriangleColor) || (!activated && backTriangleColor);
}

// The stencil function is set to GL_LEQUAL so fragments will be written to the
// back buffer only if the ref value is less or equal than the one in the stencil buffer.
// The content of the stencil buffer is initialized to 0xFF.
// First draw a red triangle whose stencil ref value is 0x1.
// Then draw a green triangle whose stencil ref value is 0xFF.
// If there is an attached stencil buffer, the resulting image will be a red triangle. If not,
// the resulting image will be a green triangle.
static bool testStencil(bool activated) {
    glViewport(0, 0, WINDOWS_SIZE, WINDOWS_SIZE);
    glClearColor(backgroundColor[0]/255.f, backgroundColor[1]/255.f, backgroundColor[2]/255.f, backgroundColor[3]/255.f);
    glClearStencil(0xFF);
    glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    glEnable(GL_STENCIL_TEST);

    glStencilFunc(GL_LEQUAL, 0x1, 0xFF);
    drawTriangle(verticesVBO, triangleColor[0], triangleColor[1], triangleColor[2], triangleColor[3]);

    glStencilFunc(GL_LEQUAL, 0xFF, 0xFF);
    drawTriangle(verticesVBO, triangleColor2[0], triangleColor2[1], triangleColor2[2], triangleColor2[3]);

    glDisable(GL_STENCIL_TEST);

    unsigned char buffer[4];
    glReadPixels(WINDOWS_SIZE/2, WINDOWS_SIZE/2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);

    bool firstTriangleColor = (buffer[0] == triangleColor[0] && buffer[1] == triangleColor[1] &&
                               buffer[2] == triangleColor[2] && buffer[3] == triangleColor[3]);

    bool secondTriangleColor = (buffer[0] == triangleColor2[0] && buffer[1] == triangleColor2[1] &&
                                buffer[2] == triangleColor2[2] && buffer[3] == triangleColor2[3]);

    return (activated && firstTriangleColor) || (!activated && secondTriangleColor);
}

// Clear to a color with alpha = 0. If alpha is enabled then all pixels will have alpha = 0.
// If alpha is disabled then pixels will have alpha of 255
static bool testAlpha(bool activated) {
    glViewport(0, 0, WINDOWS_SIZE, WINDOWS_SIZE);
    glClearColor(backgroundColor[0]/255.f, backgroundColor[1]/255.f, backgroundColor[2]/255.f, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    bool hasAlpha = true;

    unsigned char buffer[(WINDOWS_SIZE*WINDOWS_SIZE)*4];
    glReadPixels(0, 0, WINDOWS_SIZE, WINDOWS_SIZE, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
    glFinish();
    for (unsigned int i = 0 ; i < WINDOWS_SIZE ; ++i) {
      for (unsigned int j = 0 ; j < WINDOWS_SIZE ; ++j) {
        unsigned char r = buffer[4*(i*WINDOWS_SIZE+j)];
        unsigned char g = buffer[4*(i*WINDOWS_SIZE+j)+1];
        unsigned char b = buffer[4*(i*WINDOWS_SIZE+j)+2];
        unsigned char a = buffer[4*(i*WINDOWS_SIZE+j)+3];
        if (r == backgroundColor[0] && g == backgroundColor[1] && b == backgroundColor[2] && a == 0) {
          continue;
        } else {
          hasAlpha = false;
          break;
        }
      }
    }

    return (activated && hasAlpha) || (!activated && !hasAlpha);
}

static bool antiAliasingActivated = false;
static bool depthActivated = false;
static bool stencilActivated = false;
static bool alphaActivated = false;

static bool resultAA = 0;
static bool resultDepth = 0;
static bool resultStencil = 0;
static bool resultAlpha = 0;

static void draw() {
  if (!resultAA) resultAA = testAntiAliasing(antiAliasingActivated);
  assert(resultAA);

  if (!resultDepth) resultDepth = testDepth(depthActivated);
  assert(resultDepth);

  if (!resultStencil) resultStencil = testStencil(stencilActivated);
  assert(resultStencil);

  if (!resultAlpha) resultAlpha = testAlpha(alphaActivated);
  assert(resultAlpha);
}

extern int webglAntialiasSupported(void);
extern int webglDepthSupported(void);
extern int webglStencilSupported(void);
extern int webglAlphaSupported(void);

// Check attributes support in the WebGL implementation (see test_webgl_context_attributes function in test_browser.py)
// Tests will succeed if they are not.
static void checkContextAttributesSupport() {
  if (!webglAntialiasSupported()) {
    resultAA = true;
    emscripten_err("warning: no antialiasing");
  }
  if (!webglDepthSupported()) {
    resultDepth = true;
    emscripten_err("warning: no depth");
  }
  if (!webglStencilSupported()) {
    resultStencil = true;
    emscripten_err("warning: no stencil");
  }
  if (!webglAlphaSupported()) {
    resultAlpha = true;
    emscripten_err("warning: no alpha");
  }
}