ext_framebuffer_object: setup max mipmap level to make fbo complete
[piglit.git] / tests / spec / arb_viewport_array / render_viewport_2.c
blob3164ff2ee91942f38d015f61ee99fde1bc5c376d
1 /*
2 * Copyright © 2015 Red Hat.
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 DEALINGS
21 * IN THE SOFTWARE.
25 /**
26 * Tests rendering into a single framebuffer surface with multiple viewports
27 * via a geometry shader.
28 * This test is inspired by a CTS test.
29 * For one point, the geom shader emits a triangle strip with a color
30 * pre invocation. The viewports then should get one shade of red lighter.
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config.supports_gl_compat_version = 32;
37 config.supports_gl_core_version = 32;
38 config.supports_gl_es_version = 31;
40 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
41 config.khr_no_error_support = PIGLIT_NO_ERRORS;
43 PIGLIT_GL_TEST_CONFIG_END
45 #ifdef PIGLIT_USE_OPENGL
46 #define GLSL_VERSION "150"
47 #else
48 #define GLSL_VERSION "310 es"
49 #endif
51 const char *vsSource = {
52 "#version " GLSL_VERSION "\n"
53 "void main() {\n"
54 "}\n"
57 const char *gsSource = {
58 "#version " GLSL_VERSION "\n"
59 "#extension GL_ARB_gpu_shader5 : enable\n"
60 "#extension GL_ARB_viewport_array : enable\n"
61 "#extension GL_OES_viewport_array : enable\n"
62 "#extension GL_EXT_geometry_shader : enable\n"
63 "#extension GL_OES_geometry_shader : enable\n"
64 "layout(points, invocations = 16) in;\n"
65 "layout(triangle_strip, max_vertices = 4) out;\n"
66 " flat out int gs_fs_color;\n"
67 "\n"
68 "void main()\n"
69 "{\n"
70 " gs_fs_color = gl_InvocationID;\n"
71 " gl_ViewportIndex = gl_InvocationID;\n"
72 " gl_Position = vec4(-1, -1, 0, 1);\n"
73 " EmitVertex();\n"
74 " gs_fs_color = gl_InvocationID;\n"
75 " gl_ViewportIndex = gl_InvocationID;\n"
76 " gl_Position = vec4(-1, 1, 0, 1);\n"
77 " EmitVertex();\n"
78 " gs_fs_color = gl_InvocationID;\n"
79 " gl_ViewportIndex = gl_InvocationID;\n"
80 " gl_Position = vec4(1, -1, 0, 1);\n"
81 " EmitVertex();\n"
82 " gs_fs_color = gl_InvocationID;\n"
83 " gl_ViewportIndex = gl_InvocationID;\n"
84 " gl_Position = vec4(1, 1, 0, 1);\n"
85 " EmitVertex();\n"
86 "}\n"
89 const char *fsSource = {
90 "#version " GLSL_VERSION "\n"
91 "#ifdef GL_ES\n"
92 "precision highp float;\n"
93 "#endif\n"
94 "flat in int gs_fs_color;\n"
95 "uniform vec3 color;\n"
96 "out vec4 c;\n"
97 "void main() {\n"
98 " c = vec4(1.0 / float(gs_fs_color + 1), 0.0, 0.0, 1.0);\n"
99 "}\n"
102 static bool
103 draw_multi_viewport(void)
105 bool pass = true;
106 int i, j;
107 const int divX=4, divY=4;
108 GLfloat w = (GLfloat) piglit_width / (GLfloat) divX;
109 GLfloat h = (GLfloat) piglit_height / (GLfloat) divY;
110 GLfloat data[16 * 4 /* 4x4 * (x + y + w + h) */];
111 int idx;
112 int p;
114 idx = 0;
115 for (i = 0; i < divX; i++) {
116 for (j = 0; j < divY; j++) {
117 data[idx * 4 + 0] = (GLfloat)(i * w);
118 data[idx * 4 + 1] = (GLfloat)(j * h);
119 data[idx * 4 + 2] = w;
120 data[idx * 4 + 3] = h;
121 idx++;
124 glViewport(0, 0, piglit_width, piglit_height); /* for glClear() */
125 glClear(GL_COLOR_BUFFER_BIT);
126 glViewportArrayv(0, 16, data);
128 glDrawArrays(GL_POINTS, 0, 1);
129 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
131 for (i = 0; i < divX; i++) {
132 for (j = 0; j < divY; j++) {
133 GLfloat expected[4];
135 expected[0] = 1.0 / (1 + i*4 + j);
136 expected[1] = 0.0;
137 expected[2] = 0.0;
138 expected[3] = 1.0;
139 p = piglit_probe_pixel_rgba(i * w + w/2, j * h + h /2,
140 expected);
141 if (!p) {
142 printf("Wrong color for viewport i,j %d %d\n",
143 i, j);
144 pass = false;
149 piglit_present_results();
151 return pass;
154 enum piglit_result
155 piglit_display(void)
157 bool pass = true;
159 pass = draw_multi_viewport();
160 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
161 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
164 void
165 piglit_init(int argc, char **argv)
167 GLuint program;
168 GLuint vao;
170 #ifdef PIGLIT_USE_OPENGL
171 piglit_require_extension("GL_ARB_viewport_array");
172 piglit_require_extension("GL_ARB_gpu_shader5");
173 #else
174 piglit_require_extension("GL_OES_viewport_array");
175 #endif
177 program = piglit_build_simple_program_multiple_shaders(
178 GL_VERTEX_SHADER, vsSource,
179 GL_GEOMETRY_SHADER, gsSource,
180 GL_FRAGMENT_SHADER, fsSource,
182 glUseProgram(program);
184 glGenVertexArrays(1, &vao);
185 glBindVertexArray(vao);