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
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.
26 * Contains some common functionality for writing arb_direct_state_access
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
[] =
40 "#if __VERSION__ < 130\n"
41 "attribute vec4 piglit_vertex;\n"
42 "attribute vec2 piglit_texcoord;\n"
43 "varying vec2 tex_coord;\n"
45 "in vec4 piglit_vertex;\n"
46 "in vec2 piglit_texcoord;\n"
47 "out vec2 tex_coord;\n"
49 "uniform mat3 xform;\n"
53 " gl_Position = vec4((xform * piglit_vertex.xyw).xy, 0, 1);\n"
54 " tex_coord = piglit_texcoord;\n"
58 static const char fs_1d_template
[] =
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"
65 "out vec4 piglit_color;\n"
66 "in vec2 tex_coord;\n"
68 "uniform sampler1D tex;\n"
72 " piglit_color = texture(tex, tex_coord.x);\n"
76 static const char fs_2d_template
[] =
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"
83 "out vec4 piglit_color;\n"
84 "in vec2 tex_coord;\n"
86 "uniform sampler2D tex;\n"
90 " gl_FragColor = texture(tex, tex_coord);\n"
94 static const char fs_3d_template
[] =
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"
101 "out vec4 piglit_color;\n"
102 "in vec2 tex_coord;\n"
104 "uniform sampler3D tex;\n"
108 " piglit_color = texture(tex, vec3(tex_coord, 0));\n"
112 static const char fs_rect_template
[] =
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"
119 "out vec4 piglit_color;\n"
120 "in vec2 tex_coord;\n"
122 "uniform sampler2DRect tex;\n"
126 " piglit_color = texture(tex, tex_coord);\n"
131 dsa_create_program(GLenum target
)
143 piglit_get_glsl_version(&es
, &major
, &minor
);
144 ver
= ((major
* 100 + minor
) >= 140) ? "140" : "110";
146 (void)!asprintf(&vs_source
, vs_template
, ver
);
149 (void)!asprintf(&fs_source
, fs_1d_template
, ver
);
152 (void)!asprintf(&fs_source
, fs_2d_template
, ver
);
155 (void)!asprintf(&fs_source
, fs_3d_template
, ver
);
157 case GL_TEXTURE_RECTANGLE_ARB
:
158 (void)!asprintf(&fs_source
, fs_rect_template
, ver
);
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
);
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");
187 glProgramUniformMatrix3fv(prog
, loc
, 1, GL_FALSE
, xform
);
193 dsa_texture_with_unit(GLuint prog
, GLuint unit
)
195 const GLuint loc
= glGetUniformLocation(prog
, "tex");
196 glProgramUniform1i(prog
, loc
, unit
);
200 dsa_set_xform(GLuint prog
, int width
, int height
)
202 const GLint loc
= glGetUniformLocation(prog
, "xform");
205 xform
[0] = 2.0 / width
;
210 xform
[4] = 2.0 / height
;
217 glProgramUniformMatrix3fv(prog
, loc
, 1, GL_FALSE
, xform
);