summaryrefslogtreecommitdiff
path: root/tests/fbo/fbo-tex-rgbx.c
blob: 61b8a80cde6d98b25ba094827a37f31302fc1b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright © 2014 VMware, Inc.
 *
 * 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 fbo-tex-rgbx.c
 * \author Brian Paul
 *
 * Test texturing from an RGB texture after we've rendered some
 * non-1 alpha values into it.  When we sample from an RGB texture,
 * the alpha values should always be one.
 * Many (most?) GL implementations store RGB textures as RGBx so there
 * really is an alpha channel but it's supposed to be ignored when we
 * sample from it.
 */

#include "piglit-util-gl-common.h"

#define TEX_SIZE 256


PIGLIT_GL_TEST_CONFIG_BEGIN
	config.window_width = TEX_SIZE;
	config.window_height = TEX_SIZE;
	config.supports_gl_compat_version = 10;
	config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
PIGLIT_GL_TEST_CONFIG_END


static const char *fragShaderText =
	"uniform sampler2D tex; \n"
	"void main() \n"
	"{ \n"
	"   gl_FragColor = texture2D(tex, gl_TexCoord[0].xy); \n"
	"} \n";

static GLuint Program;


/* draw green quad with varying alpha values */
static void
draw_quad(void)
{
	static const float verts[4][2] = {
		{ -1.0f, -1.0f },
		{  1.0f, -1.0f },
		{  1.0f,  1.0f },
		{ -1.0f,  1.0f }
	};
	static const float colors[4][4] = {
		{ 0.0f, 1.0f, 0.0f, 0.1f },
		{ 0.0f, 1.0f, 0.0f, 0.2f },
		{ 0.0f, 1.0f, 0.0f, 0.8f },
		{ 0.0f, 1.0f, 0.0f, 0.9f }
	};
	glVertexPointer(2, GL_FLOAT, 0, verts);
	glColorPointer(4, GL_FLOAT, 0, colors);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
}


/**
 * Create texture with given internal format.  Render to it.  Then draw
 * a test quad using that texture.  Check that all alpha values are one.
 */
static bool
test_format(GLenum internalFormat)
{
	GLuint tex, fbo;
	GLenum status;
	bool pass = true;
	GLubyte *results;
	GLuint i;

	/* Create (RGB) tex and FBO to render into it */
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, 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, internalFormat, TEX_SIZE, TEX_SIZE, 0,
		     GL_RGB, GL_UNSIGNED_BYTE, NULL);

	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);

	glFramebufferTexture2D(GL_FRAMEBUFFER,
			       GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_2D, tex, 0);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		return false;

	status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (status != GL_FRAMEBUFFER_COMPLETE) {
		fprintf(stderr, "incomplete fbo (status 0x%x)\n", status);
		return true; /* this isn't necessarily a failure */
	}

	/* Draw into tex, constant green and varying alpha */
	glUseProgram(0);
	draw_quad();

	/* Now draw a textured quad in the window using the texture that we
	 * just rendered to.
	 */
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glUseProgram(Program);

	piglit_draw_rect_tex(-1.0f, -1.0f, 2.0f, 2.0f,
			     0.0f, 0.0f, 1.0f, 1.0f);

	/* Now read back the rendering and check that alpha is always one */
	results = malloc(TEX_SIZE * TEX_SIZE * 4);
	glReadPixels(0, 0, TEX_SIZE, TEX_SIZE,
		     GL_RGBA, GL_UNSIGNED_BYTE, results);

	for (i = 0; i < TEX_SIZE * TEX_SIZE; i++) {
		if (results[i*4+3] != 255) {
			fprintf(stderr,
				"Bad alpha value at texel [%d]: %d."
				"  Should be 255."
				"  Texture format %s\n",
				i, results[i*4+3],
				piglit_get_gl_enum_name(internalFormat));
			pass = false;
			break;
		}
	}

	free(results);

	piglit_present_results();

	glDeleteFramebuffers(1, &fbo);
	glDeleteTextures(1, &tex);

	return pass;
}


void
piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_ARB_framebuffer_object");
	piglit_require_gl_version(20);

	Program = piglit_build_simple_program(NULL, fragShaderText);
	assert(Program);
}


enum piglit_result
piglit_display(void)
{
	bool pass = true;

	pass = test_format(GL_RGB) && pass;
	pass = test_format(GL_RGB4) && pass;
	pass = test_format(GL_RGB5) && pass;
	pass = test_format(GL_RGB8) && pass;
	pass = test_format(GL_RGB10) && pass;
	pass = test_format(GL_RGB12) && pass;
	pass = test_format(GL_RGB16) && pass;

	if (piglit_is_extension_supported("GL_ARB_ES2_compatibility")) {
		pass = test_format(GL_RGB565) && pass;
	}

	if (piglit_is_extension_supported("GL_EXT_texture_sRGB")) {
		pass = test_format(GL_SRGB) && pass;
		pass = test_format(GL_SRGB8) && pass;
	}

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}