aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/arb_robustness/draw-vbo-bounds.c
blob: 22b045cb98b56eb2eb24fb1d1d03329cc0aeede2 (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
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
/*
 * Copyright (C) 2011 VMware, Inc.
 * Copyright (C) 2010 Marek Olšák <maraeo@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *   Jose Fonseca <jfonseca@vmware.com>
 *   Based on code from Marek Olšák <maraeo@gmail.com>
 */

/* Test whether out-of-bounds vertex buffer object cause termination.
 *
 * Note that the original ARB_vertex_buffer_object extension explicitly states
 * program termination is allowed when out-of-bounds vertex buffer object
 * fetches occur.  The ARB_robustness extension does provides an enbale to
 * guarantee that out-of-bounds buffer object accesses by the GPU will have
 * deterministic behavior and preclude application instability or termination
 * due to an incorrect buffer access.  But regardless of ARB_robustness
 * extension support it is a good idea not to crash.  For example,  viewperf
 * doesn't properly detect NV_primitive_restart and emits 0xffffffff indices
 * which can result in crashes.
 *
 * TODO:
 * - test more vertex/element formats
 * - add test for out-of-bound index buffer object access
 * - add test non-aligned offsets
 * - provide a command line option to actually enable ARB_robustness
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_MAIN(
    320 /*window_width*/,
    320 /*window_height*/,
    PIGLIT_GL_VISUAL_RGB)

void piglit_init(int argc, char **argv)
{
    piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

    piglit_require_gl_version(15);

    glShadeModel(GL_FLAT);
    glClearColor(0.2, 0.2, 0.2, 1.0);
}

static void
random_vertices(GLsizei offset, GLsizei stride, GLsizei count)
{
    GLsizei element_size = 2 * sizeof(GLfloat);
    GLsizei size;
    GLubyte *vertices;
    GLsizei i;

    if (stride == 0) {
	stride = element_size;
    }

    size = offset + (count - 1)*stride + element_size;

    assert(offset % sizeof(GLfloat) == 0);
    assert(stride % sizeof(GLfloat) == 0);

    glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_STATIC_DRAW);
    assert(glGetError() == GL_NO_ERROR);

    vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    assert(vertices);
    if (!vertices) {
        return;
    }

    for (i = 0; i < count; ++i) {
        GLfloat *vertex = (GLfloat *)(vertices + offset + i*stride);
        vertex[0] = (rand() % 1000) * .001;
        vertex[1] = (rand() % 1000) * .001;
    }

    glUnmapBuffer(GL_ARRAY_BUFFER);
}

static void
random_ushort_indices(GLsizei offset, GLsizei count, GLuint min_index, GLuint max_index)
{
    GLushort *indices;
    GLsizei size = offset + count*sizeof(*indices);
    GLsizei i;

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, NULL, GL_STATIC_DRAW);
    assert(glGetError() == GL_NO_ERROR);

    indices = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
    assert(indices);
    if (!indices) {
        return;
    }

    assert(offset % sizeof(*indices) == 0);
    for (i = 0; i < count; ++i) {
        GLushort *index = indices + offset / sizeof *indices + i;
        *index = min_index + rand() % (max_index - min_index + 1);
    }

    glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}

static void test(void)
{
    GLsizei vertex_offset;
    GLsizei vertex_stride;
    GLsizei vertex_count;
    GLuint vertex_buffer;

    GLsizei index_offset;
    GLsizei index_count;
    GLuint max_index;
    GLuint min_index;
    GLuint index_buffer;

    vertex_offset = (rand() % 0xff) * sizeof(GLfloat);
    vertex_stride = (rand() % 0xf) * sizeof(GLfloat);
    vertex_count = 1 + rand() % 0xffff;

    index_offset = (rand() % 0xff) * sizeof(GLushort);
    index_count = 1 + rand() % 0xffff;
    min_index = rand() % vertex_count;
    max_index = min_index + rand() % (vertex_count - min_index);

    if (!piglit_automatic) {
        fprintf(stdout, "vertex_offset = %i\n", vertex_offset);
        fprintf(stdout, "vertex_stride = %i\n", vertex_stride);
        fprintf(stdout, "vertex_count = %i\n", vertex_count);
        fprintf(stdout, "index_offset = %i\n", index_offset);
        fprintf(stdout, "index_count = %i\n", index_count);
        fprintf(stdout, "min_index = %u\n", min_index);
        fprintf(stdout, "max_index = %u\n", max_index);
        fprintf(stdout, "\n");
	fflush(stdout);
    }

    glGenBuffers(1, &vertex_buffer);
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);

    random_vertices(vertex_offset, vertex_stride, vertex_count);

    if (0) {
        /* Generate valid indices only */
        random_ushort_indices(index_offset, index_count, min_index, max_index);
    } else {
        /* Generate out-of-range indices */
        random_ushort_indices(index_offset, index_count, 0, 2*vertex_count - 1);
    }

    glVertexPointer(2, GL_FLOAT, vertex_stride, (const void*)(intptr_t)vertex_offset);
    glDrawRangeElements(GL_TRIANGLES,
                        min_index,
                        max_index,
                        index_count,
                        GL_UNSIGNED_SHORT,
                        (const void*)(intptr_t)index_offset);
    assert(glGetError() == GL_NO_ERROR);

    /* Call glFinish to prevent the draw from being batched, delaying the cpu crash / gpu crash
     * to much later. */
    glFinish();

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDeleteBuffers(1, &index_buffer);
    glDeleteBuffers(1, &vertex_buffer);
}

enum piglit_result
piglit_display(void)
{
    unsigned i;

    glClear(GL_COLOR_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);

    for (i = 0; i < 1000; ++i) {
        test();
        assert(glGetError() == GL_NO_ERROR);
    }

    glFinish();

    return PIGLIT_PASS;
}