[Piglit] [PATCH] Add a rendering test for GL_ARB_stencil_texturing.

Kenneth Graunke kenneth at whitecape.org
Tue Feb 11 20:12:27 PST 2014


The GL_ARB_stencil_texturing extension allows sampling from either the
depth or stencil components of combined GL_DEPTH_STENCIL textures.  To
select which component to sample, applications can call TexParameter
with a pname of GL_DEPTH_STENCIL_TEXTURE_MODE and value of either
GL_DEPTH_COMPONENT or GL_STENCIL_INDEX.

This test verifies that both depth and stencil texturing works as
expected.  In particular, it verifies that the shader samples the
correct locations.

I chose a 256x256 texture since it's larger than i965's Y-tiling format
(64) and W-tiling-interpreted-as-Y (128).

Cc: Eric Anholt <eric at anholt.net>
Cc: Topi Pohjolainen <topi.pohjolainen at intel.com>
Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
---
 tests/spec/CMakeLists.txt                          |   1 +
 tests/spec/arb_stencil_texturing/CMakeLists.gl.txt |  12 +
 tests/spec/arb_stencil_texturing/CMakeLists.txt    |   1 +
 tests/spec/arb_stencil_texturing/draw.c            | 278 +++++++++++++++++++++
 4 files changed, 292 insertions(+)
 create mode 100644 tests/spec/arb_stencil_texturing/CMakeLists.gl.txt
 create mode 100644 tests/spec/arb_stencil_texturing/CMakeLists.txt
 create mode 100644 tests/spec/arb_stencil_texturing/draw.c

We should also test that GetTexParameter works.

diff --git a/tests/spec/CMakeLists.txt b/tests/spec/CMakeLists.txt
index c83a64b..1c9e52f 100644
--- a/tests/spec/CMakeLists.txt
+++ b/tests/spec/CMakeLists.txt
@@ -31,6 +31,7 @@ add_subdirectory (arb_separate_shader_objects)
 add_subdirectory (arb_shader_texture_lod/execution)
 add_subdirectory (arb_shader_objects)
 add_subdirectory (arb_shading_language_420pack/execution)
+add_subdirectory (arb_stencil_texturing)
 add_subdirectory (arb_sync)
 add_subdirectory (arb_uniform_buffer_object)
 add_subdirectory (ati_draw_buffers)
diff --git a/tests/spec/arb_stencil_texturing/CMakeLists.gl.txt b/tests/spec/arb_stencil_texturing/CMakeLists.gl.txt
new file mode 100644
index 0000000..d49656a
--- /dev/null
+++ b/tests/spec/arb_stencil_texturing/CMakeLists.gl.txt
@@ -0,0 +1,12 @@
+include_directories(
+	${GLEXT_INCLUDE_DIR}
+	${OPENGL_INCLUDE_PATH}
+)
+
+link_libraries (
+	piglitutil_${piglit_target_api}
+	${OPENGL_gl_LIBRARY}
+	${OPENGL_glu_LIBRARY}
+)
+
+piglit_add_executable (arb_stencil_texturing-draw draw.c)
diff --git a/tests/spec/arb_stencil_texturing/CMakeLists.txt b/tests/spec/arb_stencil_texturing/CMakeLists.txt
new file mode 100644
index 0000000..144a306
--- /dev/null
+++ b/tests/spec/arb_stencil_texturing/CMakeLists.txt
@@ -0,0 +1 @@
+piglit_include_target_api()
diff --git a/tests/spec/arb_stencil_texturing/draw.c b/tests/spec/arb_stencil_texturing/draw.c
new file mode 100644
index 0000000..59c9e65
--- /dev/null
+++ b/tests/spec/arb_stencil_texturing/draw.c
@@ -0,0 +1,278 @@
+/*
+ * Copyright © 2014 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.
+ */
+
+/**
+ * \file draw.c
+ *
+ * A basic drawing test for GL_ARB_stencil_texturing which ensures that
+ * sampling occurs from the right position in the texture.
+ *
+ * It creates two packed depth/stencil textures.  The first has a horizontal
+ * gradient (0.0 -> 1.0 for depth, 0 -> 255 for stencil), and the second a
+ * vertical gradient.
+ *
+ * The expected output is four squares, separated by a blue border.
+ * The left half of the window is generated by stencil texturing, and drawn
+ * in red.  The right half is generated by depth texturing, and drawn in green.
+ *
+ *   Stencil |   Depth
+ *    (red)  |  (green)
+ *           |
+ *   0 --> 1 |  0 --> 1
+ *  --------------------
+ *      1    |     1
+ *      ^    |     ^
+ *      |    |     |
+ *      0    |     0
+ */
+
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+#include "piglit-util-gl-common.h"
+
+PIGLIT_GL_TEST_CONFIG_BEGIN
+
+	config.supports_gl_compat_version = 20;
+	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
+	config.window_width = 256 * 2 + 3;
+	config.window_height = 256 * 2 + 3;
+
+PIGLIT_GL_TEST_CONFIG_END
+
+float stencil_horiz_expected[256 * 256 * 3];
+float stencil_vert_expected[256 * 256 * 3];
+float depth_horiz_expected[256 * 256 * 3];
+float depth_vert_expected[256 * 256 * 3];
+
+GLuint horiz_tex;
+GLuint vert_tex;
+
+/** A program that samples from stencil using usampler2D. */
+GLint stencil_prog;
+
+/** A program that samples from depth using sampler2D. */
+GLint depth_prog;
+
+/******************************************************************************/
+
+/**
+ * Bind a texture and change DEPTH_STENCIL_TEXTURE_MODE (convenience function).
+ */
+static void
+select_tex(GLint handle, GLenum depth_stencil_texture_mode)
+{
+	glBindTexture(GL_TEXTURE_2D, handle);
+	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE,
+			depth_stencil_texture_mode);
+}
+
+enum piglit_result
+piglit_display(void)
+{
+	bool pass = true;
+
+	glClearColor(0.0, 0.0, 1.0, 1.0);
+	glClear(GL_COLOR_BUFFER_BIT);
+
+	glUseProgram(stencil_prog);
+
+	/* Upper left corner: Stencil (black to red, left to right). */
+	glViewport(0, 259, 256, 256);
+	select_tex(horiz_tex, GL_STENCIL_INDEX);
+	piglit_draw_rect(-1, -1, 2, 2);
+	if (!piglit_probe_image_rgb(0, 259, 256, 256, stencil_horiz_expected)) {
+		printf("  FAIL: stencil (horizontal).\n");
+		pass = false;
+	}
+
+
+	/* Lower left corner: Stencil (black to red, upwards). */
+	glViewport(0, 0, 256, 256);
+	select_tex(vert_tex, GL_STENCIL_INDEX);
+	piglit_draw_rect(-1, -1, 2, 2);
+	if (!piglit_probe_image_rgb(0, 0, 256, 256, stencil_vert_expected)) {
+		printf("  FAIL: stencil (vertical).\n");
+		pass = false;
+	}
+
+	glUseProgram(depth_prog);
+
+	/* Upper right corner: Depth (black to green, left to right). */
+	glViewport(259, 259, 256, 256);
+	select_tex(horiz_tex, GL_DEPTH_COMPONENT);
+	piglit_draw_rect(-1, -1, 2, 2);
+	if (!piglit_probe_image_rgb(259, 259, 256, 256, depth_horiz_expected)) {
+		printf("  FAIL: depth (horizontal).\n");
+		pass = false;
+	}
+
+	/* Lower right corner: Depth (black to green, upwards). */
+	glViewport(259, 0, 256, 256);
+	select_tex(vert_tex, GL_DEPTH_COMPONENT);
+	piglit_draw_rect(-1, -1, 2, 2);
+	if (!piglit_probe_image_rgb(259, 0, 256, 256, depth_vert_expected)) {
+		printf("  FAIL: depth (vertical).\n");
+		pass = false;
+	}
+
+	piglit_present_results();
+
+	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
+}
+
+/******************************************************************************/
+
+/**
+ * Pack a floating point depth value and 8-bit stencil value into
+ * GL_UNSIGNED_INT24_S8 data.
+ */
+uint32_t
+pack_z24_s8(float z, uint8_t s)
+{
+	uint32_t z24 = z * (double) 0xffffff;
+	return (z24 << 8) | s;
+}
+
+/**
+ * Generate two textures containing gradients:
+ * - Depth ranges from 0.0 to 1.0.
+ * - Stencil ranges from 0 to 255.
+ *
+ * horiz_tex is left to right (increasing in the +x direction);
+ * vert_tex is bottom to top (increasing in the +y direction).
+ *
+ * Also set up expected values.
+ */
+static void
+setup_textures(void)
+{
+	int i;
+	uint32_t horiz_data[256 * 256];
+	uint32_t vert_data[256 * 256];
+
+	for (i = 0; i < ARRAY_SIZE(horiz_data); i++) {
+		unsigned x = i % 256;
+		unsigned y = i / 256;
+		horiz_data[i] = pack_z24_s8(x / 255.0f, x);
+		vert_data[i] = pack_z24_s8(y / 255.0f, y);
+	}
+
+	glGenTextures(1, &horiz_tex);
+	glBindTexture(GL_TEXTURE_2D, horiz_tex);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
+		     256, 256, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
+		     horiz_data);
+	if (!piglit_check_gl_error(GL_NO_ERROR))
+		piglit_report_result(PIGLIT_FAIL);
+
+	glGenTextures(1, &vert_tex);
+	glBindTexture(GL_TEXTURE_2D, vert_tex);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
+		     256, 256, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
+		     vert_data);
+	if (!piglit_check_gl_error(GL_NO_ERROR))
+		piglit_report_result(PIGLIT_FAIL);
+
+	/* Set up expected values. */
+	memset(stencil_horiz_expected, 0, sizeof(stencil_horiz_expected));
+	memset(stencil_vert_expected, 0, sizeof(stencil_vert_expected));
+	memset(depth_horiz_expected, 0, sizeof(depth_horiz_expected));
+	memset(depth_vert_expected, 0, sizeof(depth_vert_expected));
+
+	for (i = 0; i < 256 * 256; i++) {
+		unsigned x = i % 256;
+		unsigned y = i / 256;
+		stencil_horiz_expected[3*i] = x / 255.0f;
+		stencil_vert_expected[3*i]  = y / 255.0f;
+		depth_horiz_expected[3*i+1] = x / 255.0f;
+		depth_vert_expected[3*i+1]  = y / 255.0f;
+	}
+
+
+}
+
+/**
+ * Compile the shaders used by this program.
+ *
+ * Depth data should be read using a floating point sampler2D; stencil data
+ * should be read using an unsigned integer usampler2D.  So, we need two
+ * separate shaders.
+ */
+static void
+setup_shaders(void)
+{
+	int loc;
+
+	const char *vs =
+	"#version 130\n"
+	"out vec2 texcoords;\n"
+	"void main()\n"
+	"{\n"
+	"    gl_Position = gl_Vertex;\n"
+	"    texcoords = (gl_Vertex.xy + 1.0) / 2.0;\n"
+	"}\n";
+
+	const char *fs_stencil =
+	"#version 130\n"
+	"in vec2 texcoords;\n"
+	"uniform usampler2D tex;\n"
+	"void main()\n"
+	"{\n"
+	"    uint stencil = texture(tex, texcoords).x;\n"
+	"    gl_FragColor = vec4(float(stencil) / 255.0, 0, 0, 1);\n"
+	"}\n";
+
+	const char *fs_depth =
+	"#version 130\n"
+	"in vec2 texcoords;\n"
+	"uniform sampler2D tex;\n"
+	"void main()\n"
+	"{\n"
+	"    float depth = texture(tex, texcoords).x;\n"
+	"    gl_FragColor = vec4(0, depth, 0, 1);\n"
+	"}\n";
+
+	stencil_prog = piglit_build_simple_program(vs, fs_stencil);
+	loc = glGetUniformLocation(stencil_prog, "tex");
+	glUseProgram(stencil_prog);
+	glUniform1i(loc, 0);
+
+	depth_prog = piglit_build_simple_program(vs, fs_depth);
+	loc = glGetUniformLocation(depth_prog, "tex");
+	glUseProgram(depth_prog);
+	glUniform1i(loc, 0);
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+	piglit_require_extension("GL_ARB_stencil_texturing");
+	piglit_require_GLSL_version(130); /* for usampler2D */
+
+	setup_textures();
+	setup_shaders();
+}
-- 
1.8.5.2



More information about the Piglit mailing list