2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Ben Widawsky <ben@bwidawsk.net>
28 #include "piglit-util-gl.h"
30 PIGLIT_GL_TEST_CONFIG_BEGIN
32 config
.supports_gl_compat_version
= 10;
34 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
36 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
38 PIGLIT_GL_TEST_CONFIG_END
40 uint8_t data
[1 << 20];
49 verify_buffer(GLenum target
, int offset
, int length
, void const *compare
) {
51 const void *ptr
= glMapBufferRange(target
, offset
, length
, GL_MAP_READ_BIT
);
52 ret
= memcmp(ptr
, compare
, length
);
53 glUnmapBuffer(target
);
59 clear_buffer(GLenum target
)
61 if (piglit_is_extension_supported("GL_ARB_clear_buffer_object")) {
62 /* Clear the buffer just to make the device busy, so that
63 * the driver can't optimize MapBufferRange to unsychronized
64 * without explicit_flush.
66 glClearBufferData(target
, GL_R32I
, GL_RED_INTEGER
, GL_INT
, NULL
);
70 /* This test relies on simple patterns, so using offets which are multiples of
74 piglit_init(int argc
, char *argv
[])
77 uint8_t temp_data
[100];
78 GLenum target
= GL_ARRAY_BUFFER
;
79 GLenum verify
= GL_COPY_WRITE_BUFFER
;
84 piglit_require_gl_version(15);
86 piglit_require_extension("GL_ARB_map_buffer_range");
88 for (i
= 0; i
< sizeof(data
); i
++) {
92 for (i
= 0; i
< 100; i
++) {
96 glGenBuffersARB(2, handles
);
97 glBindBufferARB(target
, handles
[0]);
98 glBindBufferARB(verify
, handles
[1]);
99 glBufferData(target
, sizeof(data
), data
, GL_STATIC_DRAW
);
100 glBufferData(verify
, 0x1000, NULL
, GL_STREAM_READ
);
104 /* Validate that reads work, this is required for remaining ops */
105 ret
= verify_buffer(target
, 0x201, 100, &data
[0x201]);
107 piglit_report_result(PIGLIT_FAIL
);
109 /* Test 1: test the invalidate range */
110 ptr
= glMapBufferRange(target
, 0x10004, 100, GL_MAP_WRITE_BIT
|
111 GL_MAP_INVALIDATE_RANGE_BIT
);
112 memcpy(ptr
, temp_data
, 100);
113 glUnmapBuffer(target
);
114 ret
= verify_buffer(target
, 0x10004, 100, temp_data
);
116 piglit_report_result(PIGLIT_FAIL
);
118 /* Test 2: test unsynchronized writes */
119 ptr
= glMapBufferRange(target
, 0x50f, 100, GL_MAP_WRITE_BIT
|
120 GL_MAP_UNSYNCHRONIZED_BIT
);
121 memcpy(ptr
, temp_data
, 100);
122 glUnmapBuffer(target
);
123 ret
= verify_buffer(target
, 0x50f, 100, temp_data
);
125 piglit_report_result(PIGLIT_FAIL
);
127 /* Test 3: test explicitly flushed unsynchronized writes
128 * 3a: Check if things are magically coherent (unmap doing more than it
130 ptr
= glMapBufferRange(target
, 0xa000, 100, GL_MAP_WRITE_BIT
|
131 GL_MAP_FLUSH_EXPLICIT_BIT
|
132 GL_MAP_UNSYNCHRONIZED_BIT
);
133 memcpy(ptr
, temp_data
, 100);
134 glUnmapBuffer(target
);
135 glCopyBufferSubData(target
, verify
, 0xa002, 0, 100);
136 ret
= verify_buffer(verify
, 0, 100, temp_data
);
138 fprintf(stderr
, "Coherent without flush\n");
140 /* 3b: test with flushed range */
141 ptr
= glMapBufferRange(target
, 0xa002, 100, GL_MAP_WRITE_BIT
|
142 GL_MAP_FLUSH_EXPLICIT_BIT
|
143 GL_MAP_UNSYNCHRONIZED_BIT
);
144 memcpy(ptr
, temp_data
, 100);
145 glFlushMappedBufferRange(target
, 0x0, 100);
146 glUnmapBuffer(target
);
147 glCopyBufferSubData(target
, verify
, 0xa002, 100, 100);
148 ret
= verify_buffer(verify
, 100, 100, temp_data
);
150 piglit_report_result(PIGLIT_FAIL
);
152 /* 3c: test with flushed range, invalidate range */
153 clear_buffer(target
);
154 ptr
= glMapBufferRange(target
, 0xa002, 100, GL_MAP_WRITE_BIT
|
155 GL_MAP_FLUSH_EXPLICIT_BIT
|
156 GL_MAP_INVALIDATE_RANGE_BIT
);
157 memcpy(ptr
, temp_data
, 100);
158 glFlushMappedBufferRange(target
, 0x0, 100);
159 glUnmapBuffer(target
);
160 glCopyBufferSubData(target
, verify
, 0xa002, 100, 100);
161 ret
= verify_buffer(verify
, 100, 100, temp_data
);
163 piglit_report_result(PIGLIT_FAIL
);
165 /* 3d: test with flushed range, invalidate range, non-zero flush offset */
166 clear_buffer(target
);
167 ptr
= glMapBufferRange(target
, 0xa002 - 4, 104, GL_MAP_WRITE_BIT
|
168 GL_MAP_FLUSH_EXPLICIT_BIT
|
169 GL_MAP_INVALIDATE_RANGE_BIT
);
170 memcpy(ptr
+ 4, temp_data
, 100);
171 glFlushMappedBufferRange(target
, 0x4, 100);
172 glUnmapBuffer(target
);
173 glCopyBufferSubData(target
, verify
, 0xa002, 100, 100);
174 ret
= verify_buffer(verify
, 100, 100, temp_data
);
176 piglit_report_result(PIGLIT_FAIL
);
178 piglit_report_result(PIGLIT_PASS
);