glsl-array-bounds: set out-of-bounds array index inside shader
[piglit.git] / tests / fbo / fbo-deriv.c
blob2cb61e0a7dc8619ad31c37795429976f308851f0
1 /*
2 * Copyright © 2012 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
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.
24 /**
25 * \file fbo-deriv.c
27 * Verify that the implementation produces correct values for the GLSL
28 * dFdx() and dFdy() functions, both in fbos and in the default
29 * framebuffer.
31 * Note: the reason that we test both fbos and the default framebuffer
32 * in the same test is that some implementations (e.g. Mesa/i965) need
33 * to compile the dFdy() function differently depending whether we are
34 * rendering to an FBO or to the default framebuffer; testing both in
35 * the same test allows us to verify that the implementation
36 * recompiles the shader if necessary.
38 * This test draws a pair of squares in which dFdx and dFdy are
39 * expected to both be 1.0. It colors the rectangles red=0.5*dFdx and
40 * green=0.5*dFdy, so the expected color is (0.5, 0.5, 0, 0). The
41 * left rectangle is drawn in the default framebuffer; the right
42 * rectangle is drawn in an FBO and then blitted to the default
43 * framebuffer.
46 #include "piglit-util-gl.h"
48 PIGLIT_GL_TEST_CONFIG_BEGIN
50 config.supports_gl_compat_version = 10;
52 config.window_width = 256;
53 config.window_height = 128;
54 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
55 config.khr_no_error_support = PIGLIT_NO_ERRORS;
57 PIGLIT_GL_TEST_CONFIG_END
59 static const int fbo_width = 128, fbo_height = 128;
61 static GLuint fbo;
62 static GLint prog;
64 static const char *vert =
65 "void main()\n"
66 "{\n"
67 " gl_Position = gl_Vertex;\n"
68 "}\n";
70 static const char *frag =
71 "void main()\n"
72 "{\n"
73 " gl_FragColor = vec4(0.5*dFdx(gl_FragCoord.x),\n"
74 " 0.5*dFdy(gl_FragCoord.y), 0.0, 0.0);\n"
75 "}\n";
77 static void
78 create_fbo()
80 GLenum status;
81 GLuint rb;
83 glGenRenderbuffers(1, &rb);
84 glBindRenderbuffer(GL_RENDERBUFFER, rb);
85 glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, fbo_width, fbo_height);
86 glGenFramebuffers(1, &fbo);
87 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
88 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
89 GL_RENDERBUFFER, rb);
90 status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
92 if (!piglit_check_gl_error(GL_NO_ERROR))
93 piglit_report_result(PIGLIT_FAIL);
95 switch (status) {
96 case GL_FRAMEBUFFER_UNSUPPORTED:
97 printf("Framebuffer unsupported\n");
98 piglit_report_result(PIGLIT_SKIP);
99 break;
100 case GL_FRAMEBUFFER_COMPLETE:
101 break;
102 default:
103 printf("Framebuffer incomplete\n");
104 piglit_report_result(PIGLIT_FAIL);
105 break;
109 void
110 piglit_init(int argc, char **argv)
112 GLint vs, fs;
114 piglit_require_gl_version(20);
115 piglit_require_extension("GL_ARB_framebuffer_object");
117 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vert);
118 fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag);
119 prog = piglit_link_simple_program(vs, fs);
121 create_fbo();
124 enum piglit_result
125 piglit_display(void)
127 float expected[3] = { 0.5, 0.5, 0.0 };
128 GLboolean pass = GL_TRUE;
130 glUseProgram(prog);
132 /* Draw a square to the left half of the window */
133 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
134 glViewport(0, 0, piglit_width, piglit_height);
135 piglit_draw_rect(-1, -1, 1, 2);
137 /* Draw a square to the FBO */
138 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
139 glViewport(0, 0, fbo_width, fbo_height);
140 piglit_draw_rect(-1, -1, 2, 2);
142 /* Blit the square from the FBO to the right half of the window */
143 glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
144 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
145 glBlitFramebuffer(0, 0, fbo_width, fbo_height,
146 piglit_width/2, 0, piglit_width, piglit_height,
147 GL_COLOR_BUFFER_BIT, GL_NEAREST);
149 /* Check that both squares have the correct color */
150 glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);
151 pass = piglit_probe_rect_rgb(0, 0, piglit_width, piglit_height,
152 expected) && pass;
154 piglit_present_results();
156 return pass ? PIGLIT_PASS : PIGLIT_FAIL;