2 * Copyright © 2019 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
26 * Test checks that TBO re-init works correctly
29 #include "piglit-util-gl.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
32 config
.supports_gl_core_version
= 31;
33 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGBA
;
34 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
35 PIGLIT_GL_TEST_CONFIG_END
37 #define NUMBER_OF_COLORS 4
38 #define NUMBER_OF_TBO NUMBER_OF_COLORS
40 * Reinit TBO with different data several
41 * times just to make sure
43 #define NUMBER_OF_TBO_REINIT 12
45 static const char *vs_source
=
47 "in vec4 piglit_vertex;\n"
50 " gl_Position = piglit_vertex;\n"
53 static const char *fs_source
=
55 "uniform samplerBuffer s;\n"
56 "uniform int offset;\n"
59 " gl_FragColor = texelFetch(s, offset);\n"
67 struct texbo
create_tbo();
68 void init_tbo_data(struct texbo
*tbo
, const unsigned char *color
);
69 void destroy_tbo(struct texbo
*tbo
);
76 struct texbo tbo_array
[NUMBER_OF_TBO
];
78 const unsigned char pink
[] = { 255, 0, 128, 255 };
79 const unsigned char colors
[NUMBER_OF_COLORS
][4] = {
83 { 255, 255, 255, 255 }
86 for (int i
= 0; i
< NUMBER_OF_TBO
; i
++) {
87 tbo_array
[i
] = create_tbo();
90 prog
= piglit_build_simple_program(vs_source
, fs_source
);
93 glUniform1i(glGetUniformLocation(prog
, "s"), 0);
95 const float line_width
= 2.0f
/ NUMBER_OF_TBO
;
96 //first init using pink color and draw with it.
97 for (int i
= 0; i
< NUMBER_OF_TBO
; i
++) {
98 init_tbo_data(&tbo_array
[i
], pink
);
99 glUniform1i(glGetUniformLocation(prog
, "offset"), i
);
100 glBindTexture(GL_TEXTURE_BUFFER
, tbo_array
[i
].tex
);
101 piglit_draw_rect(i
* line_width
- 1.0f
,
102 -1.0f
, line_width
, 2.0f
);
104 //change colors and draw
105 for (int tbo_reinit
= 0; tbo_reinit
< NUMBER_OF_TBO_REINIT
; tbo_reinit
++) {
106 for (int i
= 0; i
< NUMBER_OF_TBO
; i
++) {
107 const int idx
= (i
+ tbo_reinit
) % NUMBER_OF_COLORS
;
108 init_tbo_data(&tbo_array
[i
], colors
[idx
]);
109 glUniform1i(glGetUniformLocation(prog
, "offset"), i
);
110 glBindTexture(GL_TEXTURE_BUFFER
, tbo_array
[i
].tex
);
111 piglit_draw_rect(i
* line_width
- 1.0f
,
112 -1.0f
, line_width
, 2.0f
);
117 pass
= piglit_check_gl_error(GL_NO_ERROR
);
119 int piglit_line_width
= piglit_width
* (line_width
/ 2);
120 for (int i
= 0; i
< NUMBER_OF_TBO
; i
++) {
121 const int idx
= (i
+ (NUMBER_OF_TBO_REINIT
- 1)) % NUMBER_OF_COLORS
;
122 const unsigned char *expected
= colors
[idx
];
123 const float color
[] = {
124 expected
[0] / 255.0f
,
125 expected
[1] / 255.0f
,
126 expected
[2] / 255.0f
,
129 pass
= pass
&& piglit_probe_rect_rgba(piglit_line_width
* i
,
130 0, piglit_line_width
,
131 piglit_height
, color
);
134 piglit_present_results();
136 for (int i
= 0; i
< NUMBER_OF_TBO
; i
++) {
137 destroy_tbo(&tbo_array
[i
]);
139 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
143 piglit_init(int argc
, char **argv
)
147 struct texbo
create_tbo()
150 glGenBuffers(1, &tbo
.bo
);
151 glBindBuffer(GL_TEXTURE_BUFFER
, tbo
.bo
);
153 glGenTextures(1, &tbo
.tex
);
154 glBindTexture(GL_TEXTURE_BUFFER
, tbo
.tex
);
155 glTexBuffer(GL_TEXTURE_BUFFER
, GL_RGBA8
, tbo
.bo
);
157 glBindBuffer(GL_TEXTURE_BUFFER
, 0);
158 glBindTexture(GL_TEXTURE_BUFFER
, 0);
162 void init_tbo_data(struct texbo
*tbo
, const unsigned char *color
)
164 glBindBuffer(GL_TEXTURE_BUFFER
, tbo
->bo
);
165 const unsigned components
= 4;//RGBA8
166 const unsigned component_size
= 1;//RGBA8
167 const unsigned total_size
= NUMBER_OF_TBO
170 unsigned char *data
=
171 (unsigned char *)malloc(total_size
);
172 for (int i
= 0; i
< NUMBER_OF_TBO
; i
++) {
173 data
[i
* components
+ 0] = color
[0];
174 data
[i
* components
+ 1] = color
[1];
175 data
[i
* components
+ 2] = color
[2];
176 data
[i
* components
+ 3] = color
[3];
178 //invalidate/alloc the buffer and init it by the data
179 glBufferData(GL_TEXTURE_BUFFER
, total_size
, NULL
, GL_STATIC_DRAW
);
180 glBufferSubData(GL_TEXTURE_BUFFER
, 0, total_size
, data
);
181 glBindBuffer(GL_TEXTURE_BUFFER
, 0);
185 void destroy_tbo(struct texbo
*tbo
)
187 glDeleteBuffers(1, &tbo
->bo
);
188 glDeleteTextures(1, &tbo
->tex
);