glsl-array-bounds: set out-of-bounds array index inside shader
[piglit.git] / tests / shaders / glsl-fs-color-matrix.c
blob3b149932b3a0433703301891224249bb609b767b
1 /*
2 * Copyright © 2011 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 glsl-fs-color-matrix.c
26 * Transform the color value read from a texture by a matrix, keeping its alpha
28 * The shader in this test is fairly terrible (calling texture2D twice with
29 * the same texture coordinate), but it reproduces a bug in the Mesa i915
30 * driver. See Meego bug #13005 (https://bugs.meego.com/show_bug.cgi?id=13005).
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config.supports_gl_compat_version = 10;
38 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
40 PIGLIT_GL_TEST_CONFIG_END
42 const char *vs_text =
43 "attribute vec4 vertex;\n"
44 "attribute vec2 textureCoord;\n"
45 "varying vec2 coord;\n"
46 "void main(void)\n"
47 "{\n"
48 " gl_Position = vertex;\n"
49 " coord = textureCoord;\n"
50 "}\n";
52 const char *fs_text =
53 "uniform sampler2D texture;\n"
54 "uniform mat4 colorMatrix;\n"
55 "varying vec2 coord;\n"
56 "void main(void)\n"
57 "{\n"
58 " vec4 color = vec4(texture2D(texture, coord.st).rgb, 1.0);\n"
59 " color = colorMatrix * color;\n"
60 " gl_FragColor = vec4(color.rgb, texture2D(texture, coord.st).a);\n"
61 "}\n";
63 static const GLfloat identity_matrix[] = {
64 0.0, 0.0, 1.0, 0.0,
65 1.0, 0.0, 0.0, 0.0,
66 0.0, 1.0, 0.0, 0.0,
67 0.0, 0.0, 0.0, 1.0,
70 static const GLfloat vertex[] = {
71 -1.0, -1.0,
72 +1.0, -1.0,
73 +1.0, 1.0,
74 -1.0, 1.0,
77 static const GLfloat tex_coord[] = {
78 0.0, 0.0,
79 1.0, 0.0,
80 1.0, 1.0,
81 0.0, 1.0,
84 static const GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
85 static const GLfloat red[4] = { 1.0, 0.0, 0.0, 1.0 };
86 static const GLfloat green[4] = { 0.0, 1.0, 0.0, 1.0 };
87 static const GLfloat blue[4] = { 0.0, 0.0, 1.0, 1.0 };
89 static GLint prog;
91 enum piglit_result
92 piglit_display(void)
94 GLboolean pass = GL_TRUE;
95 GLuint tex;
97 tex = piglit_rgbw_texture(GL_RGBA8, 64, 64, GL_FALSE, GL_TRUE,
98 GL_UNSIGNED_NORMALIZED);
100 glClear(GL_COLOR_BUFFER_BIT);
101 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
103 pass = piglit_probe_pixel_rgb(1 * piglit_width / 3,
104 1 * piglit_width / 3,
105 blue);
106 pass = piglit_probe_pixel_rgb(2 * piglit_width / 3,
107 1 * piglit_width / 3,
108 red)
109 && pass;
110 pass = piglit_probe_pixel_rgb(1 * piglit_width / 3,
111 2 * piglit_width / 3,
112 green)
113 && pass;
114 pass = piglit_probe_pixel_rgb(2 * piglit_width / 3,
115 2 * piglit_width / 3,
116 white)
117 && pass;
119 piglit_present_results();
121 glDeleteTextures(1, &tex);
123 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
126 void
127 piglit_init(int argc, char **argv)
129 GLuint vs;
130 GLuint fs;
131 GLint loc;
132 GLboolean ok;
134 piglit_require_GLSL();
136 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text);
137 fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_text);
138 prog = piglit_link_simple_program(vs, fs);
140 glBindAttribLocation(prog, 0, "vertex");
141 glBindAttribLocation(prog, 1, "textureCoord");
143 glLinkProgram(prog);
144 ok = piglit_link_check_status(prog);
145 if (!ok)
146 piglit_report_result(PIGLIT_FAIL);
148 glUseProgram(prog);
150 loc = glGetUniformLocation(prog, "colorMatrix");
151 glUniformMatrix4fv(loc, 1, GL_FALSE, identity_matrix);
153 loc = glGetUniformLocation(prog, "texture");
154 glUniform1i(loc, 0);
156 glClearColor(0.2, 0.2, 0.2, 1.0);
158 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
159 2 * sizeof(GLfloat), vertex);
160 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
161 2 * sizeof(GLfloat), tex_coord);
163 glEnableVertexAttribArray(0);
164 glEnableVertexAttribArray(1);