Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_teximage.c
7085 views
1
/*
2
* Copyright 2014 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
/*
9
* GLES2 test for glTexImage2D parameters
10
*
11
* Original author: Jason Green <[email protected]>
12
*
13
*/
14
#include "GLES2/gl2.h"
15
#include "SDL/SDL.h"
16
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <emscripten.h>
20
#include <unistd.h>
21
22
typedef enum {
23
TEST_STATUS_SUCCESS = 0,
24
TEST_STATUS_FAILURE = 1
25
} TestStatus;
26
27
/* Report success or failure (1 or 0) to Emscripten's test harness. Also, exit
28
* with the given error code. */
29
static void exit_with_status(TestStatus code) {
30
#ifdef REPORT_RESULT
31
int result = (code == TEST_STATUS_SUCCESS) ? 1 : 0;
32
REPORT_RESULT(result);
33
#endif
34
35
exit(code);
36
}
37
38
/* Loop over all glGetError() results until GL reports GL_NO_ERROR */
39
static void clear_gl_errors() {
40
GLenum err;
41
do {
42
err = glGetError();
43
} while (err != GL_NO_ERROR);
44
}
45
46
int main(int argc, char *argv[]) {
47
TestStatus passed = TEST_STATUS_SUCCESS;
48
SDL_Surface *screen;
49
50
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
51
printf("SDL_Init failed with %s\n", SDL_GetError());
52
exit_with_status(TEST_STATUS_FAILURE);
53
}
54
55
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
56
screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL);
57
if (!screen) {
58
printf("SDL_SetVideoMode failed with %s\n", SDL_GetError());
59
exit_with_status(TEST_STATUS_FAILURE);
60
}
61
62
GLuint texture;
63
glGenTextures(1, &texture);
64
65
glBindTexture(GL_TEXTURE_2D, texture);
66
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
68
69
// Allocate space for a 32x32 image with 4 bytes per pixel.
70
// No need to fill it with any useful information, as these tests are
71
// only designed to make sure glTexImage2D doesn't crash on unsupported
72
// formats.
73
void* pixels = malloc(4 * 32 * 32);
74
if (pixels == NULL) {
75
printf("Unable to allocate pixel data\n");
76
exit_with_status(TEST_STATUS_FAILURE);
77
}
78
79
// First, try 0xffff for the internal format - should fail
80
glTexImage2D(GL_TEXTURE_2D, 0, 0xffff, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
81
GLenum err = glGetError();
82
if (err == GL_NO_ERROR) {
83
printf("internal format == 0xffff succeeded, but should have failed\n");
84
passed = TEST_STATUS_FAILURE;
85
}
86
clear_gl_errors();
87
88
// Try 0xffff for the format - should fail
89
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, 0xffff, GL_UNSIGNED_BYTE, pixels);
90
err = glGetError();
91
if (err == GL_NO_ERROR) {
92
printf("format == 0xffff succeeded, but should have failed\n");
93
passed = TEST_STATUS_FAILURE;
94
}
95
clear_gl_errors();
96
97
// Try 0xffff for the type - should fail
98
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, 0xffff, pixels);
99
err = glGetError();
100
if (err == GL_NO_ERROR) {
101
printf("type == 0xffff succeeded, but should have failed\n");
102
passed = TEST_STATUS_FAILURE;
103
}
104
clear_gl_errors();
105
106
// Try GL_RGBA/GL_UNSIGNED_BYTE - should succeed
107
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
108
err = glGetError();
109
if (err != GL_NO_ERROR) {
110
printf("GL_RGBA/GL_UNSIGNED_BYTE failed with %x, but should have succeeded\n", err);
111
passed = TEST_STATUS_FAILURE;
112
}
113
clear_gl_errors();
114
115
// Clean up objects
116
glBindTexture(GL_TEXTURE_2D, 0);
117
glDeleteTextures(1, &texture);
118
free(pixels);
119
120
// 'screen' is freed implicitly by SDL_Quit()
121
SDL_Quit();
122
123
exit_with_status(passed);
124
}
125
126