ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_direct_state_access / dsa-utils.c
blob972d737e37953271565ffe93bff4b44b9f96b851
1 /*
2 * Copyright 2014 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
21 * DEALINGS IN THE SOFTWARE.
24 /** @file dsa-utils.c
26 * Contains some common functionality for writing arb_direct_state_access
27 * Piglit tests.
30 #include "dsa-utils.h"
31 #include "piglit-shader.h"
34 * You must use shaders in order to use different texture units.
35 * These duplicate fixed-function gl 1.0 pipeline shading.
36 * Adapted from arb_clear_texture/3d.c.
38 static const char vs_template[] =
39 "#version %s\n"
40 "#if __VERSION__ < 130\n"
41 "attribute vec4 piglit_vertex;\n"
42 "attribute vec2 piglit_texcoord;\n"
43 "varying vec2 tex_coord;\n"
44 "#else\n"
45 "in vec4 piglit_vertex;\n"
46 "in vec2 piglit_texcoord;\n"
47 "out vec2 tex_coord;\n"
48 "#endif\n"
49 "uniform mat3 xform;\n"
50 "\n"
51 "void main()\n"
52 "{\n"
53 " gl_Position = vec4((xform * piglit_vertex.xyw).xy, 0, 1);\n"
54 " tex_coord = piglit_texcoord;\n"
55 "}\n"
58 static const char fs_1d_template[] =
59 "#version %s\n"
60 "#if __VERSION__ < 130\n"
61 "#define piglit_color gl_FragColor\n"
62 "#define texture(s,t) texture1D(s,t)\n"
63 "varying vec2 tex_coord;\n"
64 "#else\n"
65 "out vec4 piglit_color;\n"
66 "in vec2 tex_coord;\n"
67 "#endif\n"
68 "uniform sampler1D tex;\n"
69 "\n"
70 "void main()\n"
71 "{\n"
72 " piglit_color = texture(tex, tex_coord.x);\n"
73 "}\n"
76 static const char fs_2d_template[] =
77 "#version %s\n"
78 "#if __VERSION__ < 130\n"
79 "#define piglit_color gl_FragColor\n"
80 "#define texture(s,t) texture2D(s,t)\n"
81 "varying vec2 tex_coord;\n"
82 "#else\n"
83 "out vec4 piglit_color;\n"
84 "in vec2 tex_coord;\n"
85 "#endif\n"
86 "uniform sampler2D tex;\n"
87 "\n"
88 "void main()\n"
89 "{\n"
90 " gl_FragColor = texture(tex, tex_coord);\n"
91 "}\n"
94 static const char fs_3d_template[] =
95 "#version %s\n"
96 "#if __VERSION__ < 130\n"
97 "#define piglit_color gl_FragColor\n"
98 "#define texture(s,t) texture3D(s,t)\n"
99 "varying vec2 tex_coord;\n"
100 "#else\n"
101 "out vec4 piglit_color;\n"
102 "in vec2 tex_coord;\n"
103 "#endif\n"
104 "uniform sampler3D tex;\n"
105 "\n"
106 "void main()\n"
107 "{\n"
108 " piglit_color = texture(tex, vec3(tex_coord, 0));\n"
109 "}\n"
112 static const char fs_rect_template[] =
113 "#version %s\n"
114 "#if __VERSION__ < 130\n"
115 "#define piglit_color gl_FragColor\n"
116 "#define texture(s,t) texture2DRect(s,t)\n"
117 "varying vec2 tex_coord;\n"
118 "#else\n"
119 "out vec4 piglit_color;\n"
120 "in vec2 tex_coord;\n"
121 "#endif\n"
122 "uniform sampler2DRect tex;\n"
123 "\n"
124 "void main()\n"
125 "{\n"
126 " piglit_color = texture(tex, tex_coord);\n"
127 "}\n"
130 GLuint
131 dsa_create_program(GLenum target)
133 char *fs_source;
134 char *vs_source;
135 GLuint prog;
136 bool es;
137 int major;
138 int minor;
139 const char * ver;
140 GLint loc;
141 GLfloat xform[9];
143 piglit_get_glsl_version(&es, &major, &minor);
144 ver = ((major * 100 + minor) >= 140) ? "140" : "110";
146 (void)!asprintf(&vs_source, vs_template, ver);
147 switch (target) {
148 case GL_TEXTURE_1D:
149 (void)!asprintf(&fs_source, fs_1d_template, ver);
150 break;
151 case GL_TEXTURE_2D:
152 (void)!asprintf(&fs_source, fs_2d_template, ver);
153 break;
154 case GL_TEXTURE_3D:
155 (void)!asprintf(&fs_source, fs_3d_template, ver);
156 break;
157 case GL_TEXTURE_RECTANGLE_ARB:
158 (void)!asprintf(&fs_source, fs_rect_template, ver);
159 break;
160 default:
161 fprintf(stderr, "Invalid texture target in %s\n", __func__);
162 piglit_report_result(PIGLIT_FAIL);
165 prog = piglit_build_simple_program(vs_source, fs_source);
166 free(vs_source);
167 free(fs_source);
169 /* Note: the default value for all uniforms after linking is zero, so
170 * there is no need to explicitly set it here. However, the xform
171 * matrix needs to be set to the identity matrix.
173 loc = glGetUniformLocation(prog, "xform");
175 xform[0] = 1.0;
176 xform[1] = 0.0;
177 xform[2] = 0.0;
179 xform[3] = 0.0;
180 xform[4] = 1.0;
181 xform[5] = 0.0;
183 xform[6] = 0.0;
184 xform[7] = 0.0;
185 xform[8] = 1.0;
187 glProgramUniformMatrix3fv(prog, loc, 1, GL_FALSE, xform);
189 return prog;
192 void
193 dsa_texture_with_unit(GLuint prog, GLuint unit)
195 const GLuint loc = glGetUniformLocation(prog, "tex");
196 glProgramUniform1i(prog, loc, unit);
199 void
200 dsa_set_xform(GLuint prog, int width, int height)
202 const GLint loc = glGetUniformLocation(prog, "xform");
203 GLfloat xform[9];
205 xform[0] = 2.0 / width;
206 xform[1] = 0.0;
207 xform[2] = 0.0;
209 xform[3] = 0.0;
210 xform[4] = 2.0 / height;
211 xform[5] = 0.0;
213 xform[6] = -1.0;
214 xform[7] = -1.0;
215 xform[8] = 1.0;
217 glProgramUniformMatrix3fv(prog, loc, 1, GL_FALSE, xform);