aboutsummaryrefslogtreecommitdiff
path: root/tests/gl_teximage.c
blob: 9cafce9c7076ebd8f180dc3363b8da61e7231f6f (plain)
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
/*
 * GLES2 test for glTexImage2D parameters
 *
 * Original author: Jason Green <jason@transgaming.com>
 *
 */
#include "GLES2/gl2.h"
#include "SDL/SDL.h"

#include <stdio.h>
#include <stdlib.h>
#include <emscripten.h>
#include <unistd.h>

typedef enum {
    TEST_STATUS_SUCCESS = 0,
    TEST_STATUS_FAILURE = 1
} TestStatus;

/* Report success or failure (1 or 0) to Emscripten's test harness. Also, exit
 * with the given error code. */
static void exit_with_status(TestStatus code)
{
#ifdef REPORT_RESULT
    int result = (code == TEST_STATUS_SUCCESS) ? 1 : 0;
    REPORT_RESULT();
#endif

    exit(code);
}

/* Loop over all glGetError() results until GL reports GL_NO_ERROR */
static void clear_gl_errors()
{
    GLenum err;
    do {
        err = glGetError();
    } while (err != GL_NO_ERROR);
}

int main(int argc, char *argv[])
{
    TestStatus passed = TEST_STATUS_SUCCESS;
    SDL_Surface *screen;

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("SDL_Init failed with %s\n", SDL_GetError());
        exit_with_status(TEST_STATUS_FAILURE);
    }

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL);
    if (!screen) {
        printf("SDL_SetVideoMode failed with %s\n", SDL_GetError());
        exit_with_status(TEST_STATUS_FAILURE);
    }

    GLuint texture;
    glGenTextures(1, &texture);

    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // Allocate space for a 32x32 image with 4 bytes per pixel.
    // No need to fill it with any useful information, as these tests are
    // only designed to make sure glTexImage2D doesn't crash on unsupported
    // formats.
    void* pixels = malloc(4 * 32 * 32);
    if (pixels == NULL) {
        printf("Unable to allocate pixel data\n");
        exit_with_status(TEST_STATUS_FAILURE);
    }

    // First, try 0xffff for the internal format - should fail
    glTexImage2D(GL_TEXTURE_2D, 0, 0xffff, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    GLenum err = glGetError();
    if (err == GL_NO_ERROR) {
        printf("internal format == 0xffff succeeded, but should have failed\n");
        passed = TEST_STATUS_FAILURE;
    }
    clear_gl_errors();

    // Try 0xffff for the format - should fail
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, 0xffff, GL_UNSIGNED_BYTE, pixels);
    err = glGetError();
    if (err == GL_NO_ERROR) {
        printf("format == 0xffff succeeded, but should have failed\n");
        passed = TEST_STATUS_FAILURE;
    }
    clear_gl_errors();

    // Try 0xffff for the type - should fail
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, 0xffff, pixels);
    err = glGetError();
    if (err == GL_NO_ERROR) {
        printf("type == 0xffff succeeded, but should have failed\n");
        passed = TEST_STATUS_FAILURE;
    }
    clear_gl_errors();

    // Try GL_RGBA/GL_UNSIGNED_BYTE - should succeed
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    err = glGetError();
    if (err != GL_NO_ERROR) {
        printf("GL_RGBA/GL_UNSIGNED_BYTE failed with %x, but should have succeeded\n", err);
        passed = TEST_STATUS_FAILURE;
    }
    clear_gl_errors();

    // Clean up objects
    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(1, &texture);
    free(pixels);

    // 'screen' is freed implicitly by SDL_Quit()
    SDL_Quit();

    exit_with_status(passed);
}