1 /*
2  * Copyright © 2014 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 /**
25  * \file common.c
26  *
27  * Common routines to fill or check array buffer data.
28  */
29 
30 #include "piglit-util-gl.h"
31 
32 /* Check that the range of ARRAY_BUFFER specified with <ofs> and <length>
33  * is filled with chunks of data of length <expected_data_size>.
34  */
35 bool
check_array_buffer_sub_data(const int ofs,const int length,const int expected_data_size,const void * const expected_data)36 check_array_buffer_sub_data(const int ofs, const int length,
37 		const int expected_data_size, const void *const expected_data)
38 {
39 	bool pass = true;
40 	int i;
41 	int buffer_size;
42 	char *buffer_data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
43 	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &buffer_size);
44 
45 	assert(length % expected_data_size == 0);
46 
47 	assert(ofs >= 0);
48 	assert(length >= 0);
49 	assert(ofs + length <= buffer_size);
50 
51 	for (i = ofs; i < ofs + length; i += expected_data_size) {
52 		pass = memcmp(buffer_data + i, expected_data,
53 				expected_data_size) == 0 && pass;
54 	}
55 
56 	glUnmapBuffer(GL_ARRAY_BUFFER);
57 
58 	return pass;
59 }
60 
61 
62 /* As above, but for entire buffer. */
63 bool
check_array_buffer_data(const int expected_data_size,const void * const expected_data)64 check_array_buffer_data(const int expected_data_size,
65 		const void *const expected_data)
66 {
67 	int buffer_size;
68 	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &buffer_size);
69 
70 	return check_array_buffer_sub_data(0, buffer_size, expected_data_size,
71 			expected_data);
72 }
73 
74 /* Fill the entire ARRAY_BUFFER with chunks of data of <data_size> length.
75  */
76 void
fill_array_buffer(const int data_size,const void * const data)77 fill_array_buffer(const int data_size, const void *const data)
78 {
79 	int i;
80 	int buffer_size;
81 	char *buffer_data = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
82 	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &buffer_size);
83 
84 	assert (buffer_size % data_size == 0);
85 
86 	for (i = 0; i < buffer_size; i += data_size) {
87 		memcpy(buffer_data + i, data, data_size);
88 	}
89 
90 	glUnmapBuffer(GL_ARRAY_BUFFER);
91 }
92 
93 
94 
95