summaryrefslogtreecommitdiff
path: root/tests/spec/arb_copy_buffer/targets.c
blob: 30cba2cfc11a0444eee2a2444ff27fb75a2e1ddd (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
/*
 * Copyright © 2012 Intel Corporation
 *
 * 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.
 */

/** @file targets.c
 *
 * Tests the following piece of the GL_ARB_copy_buffer spec:
 *
 *     "All or part of one buffer object's data store may be copied to
 *      the data store of another buffer object by calling
 *
 *      void CopyBufferSubData(enum readtarget, enum writetarget,
 *                             intptr readoffset, intptr writeoffset,
 *                             sizeiptr size);
 *
 *      with readtarget and writetarget each set to one of the targets
 *      ARRAY_BUFFER, COPY_READ_BUFFER, COPY_WRITE_BUFFER,
 *      ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER,
 *      TEXTURE_BUFFER, TRANSFORM_FEEDBACK_BUFFER, or
 *      UNIFORM_BUFFER. While any of these targets may be used, the
 *      COPY_READ_BUFFER and COPY_WRITE_BUFFER targets are provided
 *      specifically for copies, so that they can be done without
 *      affecting other buffer binding targets that may be in use."
 *
 * Specifically, it walks over the available targets and makes sure
 * that copies work for them.
 */

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

PIGLIT_GL_TEST_MAIN(
    100 /*window_width*/,
    100 /*window_height*/,
    PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE)

enum piglit_result
piglit_display(void)
{
	/* uncreached */
	return PIGLIT_FAIL;
}

static bool
supported(GLenum target)
{
	switch (target) {
	case GL_ARRAY_BUFFER:
	case GL_ELEMENT_ARRAY_BUFFER:
		return piglit_is_extension_supported("GL_ARB_vertex_buffer_object");

	case GL_COPY_READ_BUFFER:
	case GL_COPY_WRITE_BUFFER:
		return true;

	case GL_PIXEL_PACK_BUFFER:
	case GL_PIXEL_UNPACK_BUFFER:
		return (piglit_is_extension_supported("GL_EXT_pixel_buffer_object") ||
			piglit_is_extension_supported("GL_ARB_pixel_buffer_object"));

	case GL_TEXTURE_BUFFER:
		return (piglit_is_extension_supported("GL_EXT_texture_buffer_object") ||
			piglit_is_extension_supported("GL_ARB_texture_buffer_object"));

	case GL_TRANSFORM_FEEDBACK_BUFFER:
		return piglit_is_extension_supported("GL_EXT_transform_feedback");

	case GL_UNIFORM_BUFFER:
		return (piglit_is_extension_supported("GL_EXT_bindable_uniform") ||
			piglit_is_extension_supported("GL_ARB_uniform_buffer_object"));
	}

	abort();
}

static void
test_copy(GLenum from, GLenum to)
{
	GLuint bufs[2];
	uint8_t data[8];
	uint8_t bad_data[8];
	void *ptr;
	int i;

	glGenBuffers(2, bufs);

	for (i = 0; i < ARRAY_SIZE(data); i++)
		data[i] = i;

	memset(bad_data, 0xd0, sizeof(bad_data));

	glBindBuffer(from, bufs[0]);
	glBufferData(from, sizeof(data), data, GL_DYNAMIC_DRAW);
	glBindBuffer(to, bufs[1]);
	glBufferData(to, sizeof(bad_data), bad_data, GL_DYNAMIC_DRAW);

	glCopyBufferSubData(from, to, 0, 0, sizeof(data));
	ptr = glMapBuffer(to, GL_READ_ONLY);
	if (memcmp(ptr, data, sizeof(data))) {
		fprintf(stderr, "data not copied\n");
		piglit_report_result(PIGLIT_FAIL);
	}
	glUnmapBuffer(to);

	glDeleteBuffers(2, bufs);
}


void
piglit_init(int argc, char **argv)
{
	int i, j;
	GLenum targets[] = {
		GL_ARRAY_BUFFER,
		GL_COPY_READ_BUFFER,
		GL_COPY_WRITE_BUFFER,
		GL_ELEMENT_ARRAY_BUFFER,
		GL_PIXEL_PACK_BUFFER,
		GL_PIXEL_UNPACK_BUFFER,
		GL_TEXTURE_BUFFER,
		GL_TRANSFORM_FEEDBACK_BUFFER,
		GL_UNIFORM_BUFFER,
	};

	piglit_require_extension("GL_ARB_copy_buffer");

	for (i = 0; i < ARRAY_SIZE(targets); i++) {
		GLenum from = targets[i];

		if (!supported(from))
			continue;

		for (j = 0; j < ARRAY_SIZE(targets); j++) {
			GLenum to = targets[j];

			if (from == to)
				continue;
			if (!supported(to))
				continue;

			test_copy(from, to);
		}
	}

	piglit_report_result(PIGLIT_PASS);
}