fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / spec / arb_sparse_buffer / basic.c
blobe181f24c6f3f3546d0328ea7cb7b5fc8a9afbda6
1 /*
2 * Copyright (c) 2017 Advanced Micro Devices, Inc.
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
13 * Software.
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
21 * DEALINGS IN THE SOFTWARE.
24 /** \file
26 * Draw a colored quad from a vertex buffer residing in a sparse buffer.
29 #include "piglit-util-gl.h"
31 #include <inttypes.h>
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_compat_version = 33;
36 config.supports_gl_core_version = 33;
38 PIGLIT_GL_TEST_CONFIG_END
40 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
42 static const char vs_source[] =
43 "#version 130\n"
44 "\n"
45 "in vec4 pos;\n"
46 "in vec4 color;\n"
47 "\n"
48 "out vec4 fs_color;\n"
49 "\n"
50 "void main() {\n"
51 " gl_Position = pos;\n"
52 " fs_color = color;\n"
53 "}\n";
55 static const char fs_source[] =
56 "#version 130\n"
57 "\n"
58 "in vec4 fs_color;\n"
59 "\n"
60 "out vec4 out_color;\n"
61 "\n"
62 "void main() {\n"
63 " out_color = fs_color;\n"
64 "}\n";
66 /* Interleaved position and color data. */
67 static const float vb_data[] = {
68 -1.0, -1.0, 0.0, 1.0, 0.5, 1.0, 0.0, 1.0,
69 -1.0, 1.0, 0.0, 1.0, 0.5, 1.0, 0.0, 1.0,
70 1.0, -1.0, 0.0, 1.0, 0.5, 1.0, 0.0, 1.0,
71 1.0, 1.0, 0.0, 1.0, 0.5, 1.0, 0.0, 1.0,
74 static int sparse_buffer_page_size;
75 static GLuint program;
78 static bool
79 do_run_one(uint64_t buffer_size, uint64_t commit_offset, uint64_t commit_size,
80 uint64_t vbuf_offset, bool vbuf_committed)
82 GLuint buf;
83 GLuint vao;
85 /* Setup buffer commitment and data. */
86 glGenBuffers(1, &buf);
87 glBindBuffer(GL_ARRAY_BUFFER, buf);
89 glBufferStorage(GL_ARRAY_BUFFER, buffer_size, NULL,
90 GL_DYNAMIC_STORAGE_BIT | GL_SPARSE_STORAGE_BIT_ARB);
92 glBufferPageCommitmentARB(GL_ARRAY_BUFFER, commit_offset, commit_size, true);
94 glBufferSubData(GL_ARRAY_BUFFER, vbuf_offset, sizeof(vb_data), vb_data);
96 if (!piglit_check_gl_error(GL_NO_ERROR))
97 return false;
99 /* Clear to red. */
100 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
101 glClear(GL_COLOR_BUFFER_BIT);
103 /* Draw. */
104 glUseProgram(program);
106 glGenVertexArrays(1, &vao);
107 glBindVertexArray(vao);
109 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8,
110 BUFFER_OFFSET(vbuf_offset));
111 glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8,
112 BUFFER_OFFSET(vbuf_offset + sizeof(float) * 4));
113 glEnableVertexAttribArray(0);
114 glEnableVertexAttribArray(1);
116 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
118 glDeleteVertexArrays(1, &vao);
119 glDeleteBuffers(1, &buf);
121 if (!piglit_check_gl_error(GL_NO_ERROR))
122 return false;
124 /* Check that the result looks good. There's not really anything we can
125 * check if the right memory wasn't committed.
127 if (vbuf_committed &&
128 !piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height,
129 &vb_data[4]))
130 return false;
132 return true;
135 static bool
136 run_one(uint64_t buffer_size, uint64_t commit_offset, uint64_t commit_size,
137 uint64_t vbuf_offset, bool vbuf_committed)
139 bool pass = do_run_one(buffer_size, commit_offset, commit_size,
140 vbuf_offset, vbuf_committed);
142 if (!pass) {
143 printf("Previous error with:\n"
144 " buffer_size = %"PRIu64"\n"
145 " commit_offset = %"PRIu64"\n"
146 " commit_size = %"PRIu64"\n"
147 " vbuf_offset = %"PRIu64"\n",
148 buffer_size, commit_offset, commit_size, vbuf_offset);
151 return pass;
154 enum piglit_result
155 piglit_display(void)
157 bool pass = true;
158 uint64_t buffer_size;
159 uint64_t commit_offset;
161 /* The spec doesn't require this, but in practice it'd be surprising
162 * to see tiny page sizes, so let's not worry about the possibility.
164 assert(sparse_buffer_page_size / 2 > sizeof(vb_data));
166 buffer_size = sparse_buffer_page_size / 2;
167 pass = run_one(buffer_size, 0, buffer_size, 0, true) && pass;
169 buffer_size = 75 * sparse_buffer_page_size;
170 pass = run_one(buffer_size, 12 * sparse_buffer_page_size,
171 sparse_buffer_page_size, 12 * sparse_buffer_page_size, true) && pass;
173 commit_offset = 1llu * 1024 * 1024 * 1024;
174 buffer_size = commit_offset + sparse_buffer_page_size / 2;
175 pass = run_one(buffer_size, commit_offset, sparse_buffer_page_size / 2,
176 buffer_size - sizeof(vb_data), true) && pass;
178 buffer_size = 10 * sparse_buffer_page_size;
179 pass = run_one(buffer_size, sparse_buffer_page_size, 9 * sparse_buffer_page_size,
180 0, false) && pass;
182 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
185 void
186 piglit_init(int argc, char **argv)
188 piglit_require_extension("GL_ARB_sparse_buffer");
190 glGetIntegerv(GL_SPARSE_BUFFER_PAGE_SIZE_ARB, &sparse_buffer_page_size);
192 program = piglit_build_simple_program_unlinked(vs_source, fs_source);
193 glBindAttribLocation(program, 0, "pos");
194 glBindAttribLocation(program, 1, "color");
195 glLinkProgram(program);
197 if (!piglit_link_check_status(program))
198 piglit_report_result(PIGLIT_FAIL);