[Piglit] [PATCH] varying: Checks if driver can handle MAX_VARYING_FLOATS in mixed float, vec*.
Vincent Lejeune
vljn at ovi.com
Wed Feb 8 13:28:45 PST 2012
---
tests/shaders/CMakeLists.gl.txt | 1 +
tests/shaders/glsl-max-varyings-2.c | 452 +++++++++++++++++++++++++++++++++++
2 files changed, 453 insertions(+), 0 deletions(-)
create mode 100644 tests/shaders/glsl-max-varyings-2.c
diff --git a/tests/shaders/CMakeLists.gl.txt b/tests/shaders/CMakeLists.gl.txt
index 9d72260..b00219e 100644
--- a/tests/shaders/CMakeLists.gl.txt
+++ b/tests/shaders/CMakeLists.gl.txt
@@ -143,6 +143,7 @@ add_executable (vp-ignore-input vp-ignore-input.c)
add_executable (glsl-empty-vs-no-fs glsl-empty-vs-no-fs.c)
add_executable (glsl-mat-attribute glsl-mat-attribute.c)
add_executable (glsl-max-varyings glsl-max-varyings.c)
+add_executable (glsl-max-varyings-2 glsl-max-varyings-2.c)
add_executable (glsl-useprogram-displaylist glsl-useprogram-displaylist.c)
add_executable (glsl-routing glsl-routing.c)
add_executable (shader_runner shader_runner.c)
diff --git a/tests/shaders/glsl-max-varyings-2.c b/tests/shaders/glsl-max-varyings-2.c
new file mode 100644
index 0000000..45e7396
--- /dev/null
+++ b/tests/shaders/glsl-max-varyings-2.c
@@ -0,0 +1,452 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric at anholt.net>
+ * Vincent Lejeune <vljn at ovi.com>
+ *
+ */
+
+/** @file glsl-max-varyings-2.c
+ *
+ * Tests whether GL_MAX_VARYING_FLOATS varying components can be used when packed
+ * as vec4, vec3, vec2, or float. Drivers have to pack varyings to pass this test.
+ */
+
+#include "piglit-util.h"
+
+#define MAX_VARYING 128
+
+/* 10x10 rectangles with 2 pixels of pad. Deal with up to 32 varyings. */
+int piglit_width = (2 + MAX_VARYING * 12), piglit_height = (2 + MAX_VARYING * 12);
+int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
+
+
+enum varyings_type {
+ FLT = 0,
+ V2 = 1,
+ V3 = 2,
+ V4 = 3
+};
+
+const char* names[] = { "float", "vec2", "vec3", "vec4" };
+
+char temp[2048];
+
+float ratio[4];
+unsigned amounts[4];
+int max_components;
+
+static void generate_varyings_decl(char* code)
+{
+
+ unsigned index = 0, i;
+
+ static const char * varying_decl[] = {
+ "varying float v%d;\n",
+ "varying vec2 v%d;\n",
+ "varying vec3 v%d;\n",
+ "varying vec4 v%d;\n",
+ };
+
+ for (i = 0; i < amounts[FLT]; i++) {
+ sprintf(temp, varying_decl[FLT], index);
+ strcat(code, temp);
+ index ++;
+ }
+
+ for (i = 0; i < amounts[V2]; i++) {
+ sprintf(temp, varying_decl[V2], index);
+ strcat(code, temp);
+ index ++;
+ }
+
+ for (i = 0; i < amounts[V3]; i++) {
+ sprintf(temp, varying_decl[V3], index);
+ strcat(code, temp);
+ index ++;
+ }
+
+ for (i = 0; i < amounts[V4]; i++) {
+ sprintf(temp, varying_decl[V4], index);
+ strcat(code, temp);
+ index ++;
+ }
+
+ return;
+}
+
+static void
+get_varying_and_component_from_index(unsigned index, unsigned *varying, unsigned *component)
+{
+ // Accessing a varying float
+ if (index < amounts[FLT]) {
+ *varying = index;
+ *component = 4;
+ return;
+ }
+
+ // Accessing a component of a vec2
+ if (index < amounts[FLT] + 2 *amounts[V2]) {
+ unsigned index_from_v2_start = index - amounts[FLT];
+ *varying = index_from_v2_start / 2;
+ *varying += amounts[FLT];
+ *component = index_from_v2_start % 2;
+ return;
+ }
+
+ // Accessing a component of a vec3
+ if (index < amounts[FLT] + 2 *amounts[V2] + 3 * amounts [V3]) {
+ unsigned index_from_v3_start = index - amounts[FLT] - 2 * amounts[V2];
+ *varying = index_from_v3_start / 3;
+ *varying += amounts[FLT] + amounts[V2];
+ *component = index_from_v3_start % 3;
+ return;
+ }
+
+ // Accessing a component of a vec4
+ if (index < amounts[FLT] + 2 * amounts[V2] + 3 *amounts[V3] + 4 * amounts[V4]) {
+ unsigned index_from_v4_start = index - amounts[FLT] - 2 * amounts[V2] - 3 * amounts[V3];
+ *varying = index_from_v4_start / 4;
+ *varying += amounts[FLT] + amounts[V2] + amounts[V4];
+ *component = index_from_v4_start % 4;
+ return;
+ }
+}
+
+static void
+write_zero(char *code, unsigned index, int component) {
+
+ switch (component) {
+ case 0:
+ sprintf(temp,
+ " v%d.x = v_zero;\n", index);
+ break;
+ case 1:
+ sprintf(temp,
+ " v%d.y = v_zero;\n", index);
+ break;
+ case 2:
+ sprintf(temp,
+ " v%d.z = v_zero;\n", index);
+ break;
+ case 3:
+ sprintf(temp,
+ " v%d.w = v_zero;\n", index);
+ break;
+ default:
+ sprintf(temp,
+ " v%d = v_zero;\n", index);
+ break;
+ }
+
+ strcat(code, temp);
+}
+
+static void
+write_green_component(char *code, unsigned index, int component_dst, unsigned component_src) {
+
+ static const char * component_suffix[] = { ".x", ".y", ".z", ".w", ""};
+ sprintf(temp,
+ " v%d%s = green%s;\n", index, component_suffix[component_dst], component_suffix[component_src]);
+ strcat(code, temp);
+}
+
+/* Generate a VS that writes to num_varyings vec4s, and put
+ * interesting data in data_varying with 0.0 everywhere else.
+ */
+static GLint get_vs(int data_varying)
+{
+ GLuint shader;
+ unsigned i, j;
+ char *code;
+
+ code = malloc(2048 * 4);
+ code[0] = 0;
+
+ generate_varyings_decl(code);
+
+ sprintf(temp,
+ "attribute vec4 green;\n"
+ "attribute float v_zero;\n"
+ "void main()\n"
+ "{\n"
+ " gl_Position = (gl_ModelViewProjectionMatrix * \n"
+ " gl_Vertex);\n"
+ );
+ strcat(code, temp);
+
+ for (i = 0; i < data_varying; i++) {
+ unsigned index, component;
+ get_varying_and_component_from_index(i, &index, &component);
+ write_zero(code, index, component);
+ }
+
+ j = 0;
+ for (i = data_varying; i < data_varying + 4; i++) {
+ unsigned index, component;
+ get_varying_and_component_from_index(i, &index, &component);
+ write_green_component(code, index, component, j);
+ j++;
+ }
+
+ for (i = data_varying + 4; i < max_components - 4; i++) {
+ unsigned index, component;
+ get_varying_and_component_from_index(i, &index, &component);
+ write_zero(code, index, component);
+ }
+
+ sprintf(temp,
+ "}\n"
+ );
+ strcat(code, temp);
+
+ shader = piglit_compile_shader_text(GL_VERTEX_SHADER, code);
+ /* printf("%s\n", code); */
+ free(code);
+
+ return shader;
+}
+
+static void
+read_to_val_component(char *code, unsigned index, int component_src, unsigned component_dst, const char *coeff) {
+
+ static const char * component_suffix[] = { ".x", ".y", ".z", ".w", ""};
+ sprintf(temp,
+ " val%s += %s * v%d%s;\n", component_suffix[component_dst], coeff, index, component_suffix[component_src]);
+ strcat(code, temp);
+}
+
+/* Generate a FS that does operations on num_varyings, yet makes only
+ * data_varying contribute to the output.
+ *
+ * We could make a single FS per num_varyings that did this by using a
+ * uniform for data_varying and some multiplication by comparisons
+ * (see glsl-routing for an example), but since we're linking a new
+ * shader each time anyway, this produces a simpler FS to read and
+ * verify.
+ */
+static GLint get_fs(int data_varying)
+{
+ GLuint shader;
+ unsigned i, j;
+ char temp[2048];
+ char *code;
+
+ code = malloc(2048 * 4);
+ code[0] = 0;
+
+ generate_varyings_decl(code);
+
+
+ sprintf(temp,
+ "uniform float zero;\n"
+ "uniform float one;\n"
+ "void main()\n"
+ "{\n"
+ " vec4 val = vec4(0.0);\n"
+ );
+ strcat(code, temp);
+
+ for (i = 0; i < data_varying; i++) {
+ unsigned index, component;
+ get_varying_and_component_from_index(i, &index, &component);
+ read_to_val_component(code, index, component, i % 4, "zero");
+ }
+
+ j = 0;
+ for (i = data_varying; i < data_varying + 4; i++) {
+ unsigned index, component;
+ get_varying_and_component_from_index(i, &index, &component);
+ read_to_val_component(code, index, component, j, "one");
+ j++;
+ }
+
+ for (i = data_varying + 4; i < max_components - 4; i++) {
+ unsigned index, component;
+ get_varying_and_component_from_index(i, &index, &component);
+ read_to_val_component(code, index, component, i % 4, "zero");
+ }
+
+ sprintf(temp,
+ " gl_FragColor = val;\n"
+ "}\n"
+ );
+ strcat(code, temp);
+ /* printf("%s\n", code); */
+ shader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, code);
+ free(code);
+
+ return shader;
+}
+
+static int
+coord_from_index(int index)
+{
+ return 2 + 12 * index;
+}
+
+static void
+draw()
+{
+ int data_varying;
+ float green[4][4] = { {0.0, 1.0, 0.0, 0.0},
+ {0.0, 1.0, 0.0, 0.0},
+ {0.0, 1.0, 0.0, 0.0},
+ {0.0, 1.0, 0.0, 0.0} };
+
+ glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
+ green);
+ glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
+ green);
+ glEnableVertexAttribArray(1);
+ glEnableVertexAttribArray(2);
+
+ for (data_varying = 0; data_varying < max_components - 4; data_varying ++) {
+ GLuint prog, vs, fs;
+ GLint loc;
+
+ vs = get_vs(data_varying);
+ fs = get_fs(data_varying);
+
+ prog = glCreateProgram();
+ glAttachShader(prog, vs);
+ glAttachShader(prog, fs);
+
+ glBindAttribLocation(prog, 1, "green");
+ glBindAttribLocation(prog, 2, "v_zero");
+
+ glLinkProgram(prog);
+ if (!piglit_link_check_status(prog))
+ piglit_report_result(PIGLIT_FAIL);
+
+ glUseProgram(prog);
+
+ loc = glGetUniformLocation(prog, "zero");
+ if (loc != -1) /* not used for num_varyings == 1 */
+ glUniform1f(loc, 0.0);
+
+ loc = glGetUniformLocation(prog, "one");
+ assert(loc != -1); /* should always be used */
+ glUniform1f(loc, 1.0);
+
+
+ piglit_draw_rect(coord_from_index(data_varying),
+ coord_from_index(0),
+ 10,
+ 10);
+
+ glDeleteShader(vs);
+ glDeleteShader(fs);
+ glDeleteProgram(prog);
+ }
+}
+
+enum piglit_result
+piglit_display(void)
+{
+ int col;
+ GLboolean pass = GL_TRUE, warned = GL_FALSE;
+
+ piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
+
+ glGetIntegerv(GL_MAX_VARYING_FLOATS, &max_components);
+
+ printf("GL_MAX_VARYING_FLOATS = %i\n", max_components);
+
+ if (max_components > MAX_VARYING) {
+ printf("test not designed to handle >%d varying vec4s.\n"
+ "(implementation reports %d components)\n",
+ max_components, MAX_VARYING);
+ max_components = MAX_VARYING;
+ warned = GL_TRUE;
+ }
+
+ amounts[FLT] = max_components * ratio[FLT];
+ amounts[V2] = (max_components * ratio[V2]) / 2;
+ amounts[V3] = (max_components * ratio[V3]) / 4;
+ amounts[V4] = (max_components * ratio[V4]) /4;
+
+ max_components = amounts[FLT] + amounts[V2] + amounts[V3] + amounts[V4];
+
+ glClearColor(0.5, 0.5, 0.5, 0.5);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ draw();
+
+ for (col = 0; col < max_components - 4; col ++) {
+ GLboolean ok;
+ float green[3] = {0.0, 1.0, 0.0};
+
+ ok = piglit_probe_rect_rgb(coord_from_index(col),
+ coord_from_index(0),
+ 10, 10,
+ green);
+ if (!ok) {
+ unsigned index, component;
+ get_varying_and_component_from_index(col, &index, &component);
+ printf(" Failure with v%d varyings\n", index);
+ pass = GL_FALSE;
+ break;
+ }
+ }
+
+ glutSwapBuffers();
+
+ if (!pass)
+ return PIGLIT_FAIL;
+ if (warned)
+ return PIGLIT_WARN;
+ else
+ return PIGLIT_PASS;
+}
+
+
+void piglit_init(int argc, char **argv)
+{
+ unsigned i;
+
+ if (!GLEW_VERSION_2_0) {
+ printf("Requires OpenGL 2.0\n");
+ piglit_report_result(PIGLIT_SKIP);
+ }
+
+ if (argc < 5) {
+ fprintf(stderr, "Not enough args.\n");
+ exit(1);
+ }
+
+ for (i = 1; i < argc; i++) {
+ sscanf(argv[i], "%f",&ratio[i - 1]);
+ if (ratio[i - 1] < 0) {
+ fprintf(stderr, "Ratio must be positive.\n");
+ exit(1);
+ }
+ }
+
+ if (ratio[FLT] + ratio[V2] + ratio[V3] + ratio[V4] > 1.0) {
+ fprintf(stderr, "Ratio are not normalised.\n");
+ exit(1);
+ }
+}
+
+
--
1.7.7
More information about the Piglit
mailing list