2 * Copyright © 2013 LunarG, 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
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
23 * Author: Jon Ashburn <jon@lunarg.com>
24 * Adapted for vertex shader: Ilia Mirkin <imirkin@alum.mit.edu>
28 * Tests rendering into a single framebuffer surface with multiple viewports
29 * via a vertex shader. Confirm that each area of the surface delineated by
30 * a viewport renders the correct color.
31 * From the extension registry for ARB_viewport_array:
32 * "This extension enhances OpenGL by providing a mechanism to expose
33 * multiple viewports. Each viewport is specified as a rectangle. The
34 * destination viewport may be selected per-primitive by the geometry
35 * shader. This allows the Geometry Shader to produce different versions
36 * of primitives destined for separate viewport rectangles on the same
38 * This extension allows specifying the viewport from the vertex shader.
40 #include "piglit-util-gl.h"
42 PIGLIT_GL_TEST_CONFIG_BEGIN
44 config
.supports_gl_core_version
= 31;
46 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
;
47 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
49 PIGLIT_GL_TEST_CONFIG_END
51 const char *vsSource
= {
53 "#extension GL_AMD_vertex_shader_viewport_index : enable\n"
54 "in vec4 piglit_vertex;\n"
57 " gl_ViewportIndex = idx;\n"
58 " gl_Position = piglit_vertex;\n"
62 const char *fsSource
= {
64 "uniform vec3 color;\n"
66 " gl_FragColor = vec4(color.xyz, 1.0);\n"
70 static GLint colorLoc
;
71 static GLint vpIndexLoc
;
74 * Draws a single quad into multiple viewport each with a different
75 * color. Reads back the expected color to test if the drawing was correct.
76 * @param changeVPloc; if true then the vertex shader viewport location
77 * is changed with each loop, otherwise viewport location is fixed.
80 draw_multi_viewport(const bool changeVPLoc
)
84 const int divX
=2, divY
=4;
85 GLfloat w
= (GLfloat
) piglit_width
/ (GLfloat
) divX
;
86 GLfloat h
= (GLfloat
) piglit_height
/ (GLfloat
) divY
;
87 const GLfloat colors
[][3] = {{0.0, 0.0, 1.0},
97 assert(ARRAY_SIZE(colors
) == divX
*divY
+ 1);
99 glViewport(0, 0, piglit_width
, piglit_height
); /* for glClear() */
100 glClear(GL_COLOR_BUFFER_BIT
);
101 glUniform1i(vpIndexLoc
, divX
* divY
);
102 glViewportIndexedf(divX
* divY
, -10.0, -30.0, piglit_width
, 20.0);
103 for (i
= 0; i
< divX
; i
++) {
104 for (j
= 0; j
< divY
; j
++) {
107 glUniform3fv(colorLoc
, 1, &colors
[j
+ i
*divY
][0]);
109 glUniform1i(vpIndexLoc
, j
+ i
*divY
);
110 expected
= (GLfloat
*) &colors
[j
+ i
*divY
][0];
112 expected
= (GLfloat
*) &colors
[divX
* divY
][0];
114 glViewportIndexedf(j
+ i
*divY
, i
* w
, j
* h
, w
, h
);
115 piglit_draw_rect(-1, -1, 2, 2);
116 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
117 p
= piglit_probe_pixel_rgb(i
* w
+ w
/2, j
* h
+ h
/2,
119 piglit_present_results();
121 printf("Wrong color for viewport i,j %d %d changeVP=%d\n",
135 pass
= draw_multi_viewport(true);
136 pass
= draw_multi_viewport(false) && pass
;
137 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
138 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
142 piglit_init(int argc
, char **argv
)
146 piglit_require_extension("GL_AMD_vertex_shader_viewport_index");
148 program
= piglit_build_simple_program(vsSource
, fsSource
);
149 glUseProgram(program
);
150 colorLoc
= glGetUniformLocation(program
, "color");
151 vpIndexLoc
= glGetUniformLocation(program
, "idx");