1 /*
2  * Copyright © 2014 Intel Corporation
3  * Copyright © 2015 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file unaligned.c
27  *
28  * Test R8 copying with non-dword alignment.
29  */
30 
31 #include "piglit-util-gl.h"
32 #include "common.h"
33 
34 PIGLIT_GL_TEST_CONFIG_BEGIN
35 
36 	config.supports_gl_compat_version = 15;
37 	config.supports_gl_core_version = 31;
38 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
39 
40 PIGLIT_GL_TEST_CONFIG_END
41 
42 #define SIZE (1 << 20)
43 
44 static bool debug;
45 
46 static void
clear_buffer(int i,unsigned offset,unsigned size,unsigned char value,char * cpu)47 clear_buffer(int i, unsigned offset, unsigned size, unsigned char value, char *cpu)
48 {
49 	glClearBufferSubData(GL_ARRAY_BUFFER, GL_R8, offset, size,
50 			     GL_RED, GL_UNSIGNED_BYTE, &value);
51 	memset(cpu+offset, value, size);
52 
53 	if (debug && !check_array_buffer_data(SIZE, cpu)) {
54 		printf("Clear %i failed: offset=%i (%%4 = %i), size=%i (%%4 = %i)\n",
55 		       i, offset, offset % 4, size, size % 4);
56 		piglit_report_result(PIGLIT_FAIL);
57 	}
58 }
59 
60 void
piglit_init(int argc,char ** argv)61 piglit_init(int argc, char **argv)
62 {
63 	bool pass = true;
64 	GLuint buffer;
65 	int i;
66 	unsigned offset, size;
67 	char *cpu = malloc(SIZE);
68 
69 	if (!cpu)
70 		piglit_report_result(PIGLIT_FAIL);
71 
72 	for (i = 1; i < argc; i++)
73 		if (!strcmp(argv[i], "-debug"))
74 			debug = true;
75 
76 	piglit_require_extension("GL_ARB_clear_buffer_object");
77 	glGenBuffers(1, &buffer);
78 	glBindBuffer(GL_ARRAY_BUFFER, buffer);
79 	glBufferData(GL_ARRAY_BUFFER, SIZE, NULL, GL_STREAM_READ);
80 
81 	clear_buffer(0, 0, SIZE, 0, cpu);
82 
83 	srand(6487216);
84 	for (i = 1; i <= 200; i++) {
85 		offset = rand() % SIZE;
86 		size = 1 + (rand() % (SIZE - offset));
87 
88 		clear_buffer(i, offset, size, rand(), cpu);
89 	}
90 
91 	/* and some small unaligned copies within one dword */
92 	for (; i < 230; i++) {
93 		offset = (rand() % (SIZE/4)) * 4 + 1 + (rand() % 2);
94 		size = 1;
95 
96 		clear_buffer(i, offset, size, rand(), cpu);
97 	}
98 
99 	pass = piglit_check_gl_error(GL_NO_ERROR) &&
100 	       check_array_buffer_data(SIZE, cpu);
101 	glDeleteBuffers(1, &buffer);
102 	free(cpu);
103 	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
104 }
105 
106 
107 enum piglit_result
piglit_display(void)108 piglit_display(void)
109 {
110 	return PIGLIT_PASS;
111 }
112