framework/replay: update renderdoc against v1.20
[piglit.git] / tests / spec / arb_viewport_array / render_viewport.c
blobbdc8b6f00bcc3e14e0f77a01bedb8beb298a20c8
1 /*
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
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.
23 * Author: Jon Ashburn <jon@lunarg.com>
26 /**
27 * Tests rendering into a single framebuffer surface with multiple viewports
28 * via a geometry shader. Confirm that each area of the surface delineated by
29 * a viewport renders the correct color. Use multiple draw
30 * calls to replicate geometry rather than the geometry shader.
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
37 * surface.
39 #include "piglit-util-gl.h"
41 PIGLIT_GL_TEST_CONFIG_BEGIN
43 config.supports_gl_compat_version = 32;
44 config.supports_gl_core_version = 32;
45 config.supports_gl_es_version = 31;
47 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
48 config.khr_no_error_support = PIGLIT_NO_ERRORS;
50 PIGLIT_GL_TEST_CONFIG_END
52 #ifdef PIGLIT_USE_OPENGL
53 #define GLSL_VERSION "150"
54 #else
55 #define GLSL_VERSION "310 es"
56 #endif
58 const char *vsSource = {
59 "#version " GLSL_VERSION "\n"
60 "in vec4 piglit_vertex;\n"
61 "void main() {\n"
62 " gl_Position = piglit_vertex;\n"
63 "}\n"
66 const char *gsSource = {
67 "#version " GLSL_VERSION "\n"
68 "#extension GL_ARB_viewport_array : enable\n"
69 "#extension GL_OES_viewport_array : enable\n"
70 "#extension GL_EXT_geometry_shader : enable\n"
71 "#extension GL_OES_geometry_shader : enable\n"
72 "layout(triangles) in;\n"
73 "layout(triangle_strip, max_vertices = 3) out;\n"
74 "uniform int idx;\n"
75 "\n"
76 "void main()\n"
77 "{\n"
78 " gl_ViewportIndex = idx;\n"
79 " for(int i = 0; i < gl_in.length(); i++) {\n"
80 " gl_Position = gl_in[i].gl_Position;\n"
81 " EmitVertex();\n"
82 " }\n"
83 " EndPrimitive();\n"
84 "}\n"
87 const char *fsSource = {
88 "#version " GLSL_VERSION "\n"
89 "#ifdef GL_ES\n"
90 "precision highp float;\n"
91 "#endif\n"
92 "uniform vec3 color;\n"
93 "out vec4 c;\n"
94 "void main() {\n"
95 " c = vec4(color.xyz, 1.0);\n"
96 "}\n"
99 static GLint colorLoc;
100 static GLint vpIndexLoc;
103 * Draws a single quad into multiple viewport each with a different
104 * color. Reads back the expected color to test if the drawing was correct.
105 * @param changeVPloc; if true then the geometry shader viewport location
106 * is changed with each loop, otherwise viewport location is fixed.
108 static bool
109 draw_multi_viewport(const bool changeVPLoc)
111 bool pass = true;
112 int i, j;
113 const int divX=2, divY=4;
114 GLfloat w = (GLfloat) piglit_width / (GLfloat) divX;
115 GLfloat h = (GLfloat) piglit_height / (GLfloat) divY;
116 const GLfloat colors[][4] = {{0.0, 0.0, 1.0, 1.0},
117 {0.0, 1.0, 0.0, 1.0},
118 {1.0, 0.0, 0.0, 1.0},
119 {1.0, 1.0, 0.0, 1.0},
120 {0.0, 1.0, 1.0, 1.0},
121 {1.0, 0.0, 1.0, 1.0},
122 {1.0, 1.0, 1.0, 1.0},
123 {0.0, 0.0, 0.5, 1.0},
124 {0.0, 0.0, 0.0, 0.0}};
126 assert(ARRAY_SIZE(colors) == divX*divY + 1);
128 glViewport(0, 0, piglit_width, piglit_height); /* for glClear() */
129 glClear(GL_COLOR_BUFFER_BIT);
130 glUniform1i(vpIndexLoc, divX * divY);
131 glViewportIndexedf(divX * divY, -10.0, -30.0, piglit_width, 20.0);
132 for (i = 0; i < divX; i++) {
133 for (j = 0; j < divY; j++) {
134 int p;
135 GLfloat *expected;
136 glUniform3fv(colorLoc, 1, &colors[j + i*divY][0]);
137 if (changeVPLoc) {
138 glUniform1i(vpIndexLoc, j + i*divY);
139 expected = (GLfloat *) &colors[j + i*divY][0];
140 } else {
141 expected = (GLfloat *) &colors[divX * divY][0];
143 glViewportIndexedf(j + i*divY, i * w, j * h, w, h);
144 piglit_draw_rect(-1, -1, 2, 2);
145 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
146 p = piglit_probe_pixel_rgba(i * w + w/2, j * h + h/2,
147 expected);
148 piglit_present_results();
149 if (!p) {
150 printf("Wrong color for viewport i,j %d %d changeVP=%d\n",
151 i, j, changeVPLoc);
152 pass = false;
156 return pass;
159 enum piglit_result
160 piglit_display(void)
162 bool pass = true;
164 pass = draw_multi_viewport(true);
165 pass = draw_multi_viewport(false) && pass;
166 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
167 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
170 void
171 piglit_init(int argc, char **argv)
173 GLuint program;
175 #ifdef PIGLIT_USE_OPENGL
176 piglit_require_extension("GL_ARB_viewport_array");
177 #else
178 piglit_require_extension("GL_OES_viewport_array");
179 #endif
181 program = piglit_build_simple_program_multiple_shaders(
182 GL_VERTEX_SHADER, vsSource,
183 GL_GEOMETRY_SHADER, gsSource,
184 GL_FRAGMENT_SHADER, fsSource,
186 glUseProgram(program);
187 colorLoc = glGetUniformLocation(program, "color");
188 vpIndexLoc = glGetUniformLocation(program, "idx");