From 3ac239163d6d259bfb529e952fc90e537a80b984 Mon Sep 17 00:00:00 2001 From: Julian Adams Date: Thu, 31 Mar 2011 21:00:06 +0100 Subject: [PATCH] add test to test vertex and fragment shaders can both sample from textures in the same program --- tests/all.tests | 1 + tests/texturing/CMakeLists.gl.txt | 1 + tests/texturing/fragment-and-vertex-texturing.c | 194 +++++++++++++++++++++++ 3 files changed, 196 insertions(+), 0 deletions(-) create mode 100644 tests/texturing/fragment-and-vertex-texturing.c diff --git a/tests/all.tests b/tests/all.tests index eb86ae7..7e1dac1 100644 --- a/tests/all.tests +++ b/tests/all.tests @@ -531,6 +531,7 @@ add_plain_test(texturing, 'depth-tex-modes') add_plain_test(texturing, 'depth-tex-modes-glsl') add_plain_test(texturing, 'depth-tex-modes-rg') add_plain_test(texturing, 'depth-tex-compare') +add_plain_test(texturing, 'fragment-and-vertex-texturing') add_plain_test(texturing, 'fxt1-teximage') add_plain_test(texturing, 'gen-teximage') add_plain_test(texturing, 'gen-compressed-teximage') diff --git a/tests/texturing/CMakeLists.gl.txt b/tests/texturing/CMakeLists.gl.txt index fd738a8..4583e57 100644 --- a/tests/texturing/CMakeLists.gl.txt +++ b/tests/texturing/CMakeLists.gl.txt @@ -26,6 +26,7 @@ add_executable (gen-nonzero-unit gen-nonzero-unit.c) add_executable (gen-teximage gen-teximage.c) add_executable (gen-texsubimage gen-texsubimage.c) add_executable (getteximage-simple getteximage-simple.c) +add_executable (fragment-and-vertex-texturing fragment-and-vertex-texturing.c) add_executable (levelclamp levelclamp.c) add_executable (lodbias lodbias.c) add_executable (lodclamp lodclamp.c) diff --git a/tests/texturing/fragment-and-vertex-texturing.c b/tests/texturing/fragment-and-vertex-texturing.c new file mode 100644 index 0000000..48fb8b7 --- /dev/null +++ b/tests/texturing/fragment-and-vertex-texturing.c @@ -0,0 +1,194 @@ +/* + * 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 + * Marek Olšák + * + */ + +/** @file fbo-drawbuffers-blend-add.c + * + * Tests that sampling from both vertex and fragment textures each read + * from the correct texture. + */ + +#include +#include "piglit-util.h" + +int piglit_width = 128; +int piglit_height = 128; +int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE; + +static const char *vs_source = + "uniform sampler2D vertex_tex; \n" + "varying vec3 vertex_tex_color; \n" +/* "uniform sampler2D fragment_tex; \n" + "varying vec3 fragment_tex_color; \n" */ + "void main() \n" + "{ \n" + " gl_Position = gl_Vertex;\n" + " vertex_tex_color = texture2DLod(vertex_tex, vec2(0.5), 0.0).xyz; \n" +/* " fragment_tex_color = texture2DLod(fragment_tex, vec2(0.5), 0.0).xyz; \n" */ + "} \n"; + +static const char *fs_source = + "uniform sampler2D fragment_tex; \n" + "varying vec3 vertex_tex_color; \n" + "void main() \n" + "{ \n" + " vec3 fragment_tex_color = texture2D(fragment_tex, vec2(0.5), 0.0).xyz; \n" + " gl_FragColor = vec4(fragment_tex_color + vertex_tex_color, 1.0); \n" + "} \n"; + +static const char *prog = "fragment-and-vertex-texturing"; + +/* debug aid */ +static void +check_error(int line) +{ + GLenum err = glGetError(); + if (err) { + printf("%s: GL error 0x%x at line %d\n", prog, err, line); + } +} + +/* debug aid */ +static void +check_bad_location(GLint location, int line) +{ + if (location == -1) { + printf("%s: bad GL location at line %d\n", prog, line); + } +} + +static GLuint +make_texture(int texture_unit, unsigned char r, unsigned char g, unsigned char b) +{ + #define texSide 8 + GLuint tex; + unsigned char texImage[texSide * texSide * 4]; + GLuint i, j; + + for (i = 0; i < texSide; i++) { + for (j = 0; j < texSide; j++) { + int k = (i * texSide + j) * 3; + texImage[k] = r; + texImage[k + 1] = g; + texImage[k + 2] = b; + } + } + + glGenTextures(1, &tex); + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(GL_TEXTURE_2D, tex); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texSide, texSide, 0, + GL_RGB, GL_UNSIGNED_BYTE, texImage); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + check_error(__LINE__); + + return tex; +} + +static void +display() +{ + GLuint fs, vs, prog; + + /* Clear all to blue so we see if the shader rendering happens. */ + glClearColor(0.0, 0.0, 1.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + /* Build the shader that spams green to all outputs. */ + vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_source); + + fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_source); + + prog = piglit_link_simple_program(vs, fs); + glUseProgram(prog); + + check_error(__LINE__); + + const int vertex_tex_unit = 0; + const int fragment_tex_unit = 1; + GLuint red = make_texture(vertex_tex_unit, 127, 0, 0); + GLuint green = make_texture(fragment_tex_unit, 0, 127, 0); + + // -1 is bad + GLint vertex_tex_loc = glGetUniformLocation(prog, "vertex_tex"); + check_bad_location(vertex_tex_loc, __LINE__); + + GLint fragment_tex_loc = glGetUniformLocation(prog, "fragment_tex"); + check_bad_location(fragment_tex_loc, __LINE__); + + glUniform1i(vertex_tex_loc, vertex_tex_unit); + glUniform1i(fragment_tex_loc, fragment_tex_unit); + + piglit_draw_rect(-1, -1, 2, 2); + + check_error(__LINE__); + + glDeleteTextures(1, &red); + glDeleteTextures(1, &green); + + check_error(__LINE__); +} + +enum piglit_result +piglit_display(void) +{ + int pass; + + display(); + + float expected[] = {0.5, 0.5, 0.0}; + + pass = piglit_probe_pixel_rgb(1, 1, expected); + + glutSwapBuffers(); + + return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE; +} + +void +piglit_init(int argc, char **argv) +{ + int m; + + printf("The result should be a solid block of half-bright yellow color\n"); + + piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE); + + if (!GLEW_VERSION_2_0) { + printf("Requires OpenGL 2.0\n"); + piglit_report_result(PIGLIT_SKIP); + } + + glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &m); + if (m < 1) { + printf("No vertex shader texture units supported.\n"); + piglit_report_result(PIGLIT_SKIP); + } +} + -- 1.7.1