fbo-srgb.c 5.5 KB
Newer Older
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
/*
 * Copyright © 2010 Red Hat
 *
 * 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:
 *    Dave Airlie
 *
 */

/** @file fbo-srgb
 *
 * This test check that a SRGB8_A8 texture bound as an FBO doesn't
 * do blending and updating to the bound FBO in SRGB mode.
 * this is specified by the EXT_texture_sRGB specification,
 * To get SRGB blending EXT_framebuffer_sRGB is required.
 */

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

38 39 40 41
PIGLIT_GL_TEST_MAIN(
    128 /*window_width*/,
    128 /*window_height*/,
    GLUT_RGB | GLUT_DOUBLE)
42 43 44 45 46

/* size of texture/renderbuffer (power of two) */
#define FBO_SIZE 128

static GLuint
Dave Airlie's avatar
Dave Airlie committed
47
make_fbo(int w, int h, int srgb_format, GLuint *tex_p)
48 49 50 51
{
	GLuint tex;
	GLuint fb;
 	GLenum status;
52 53
	int formats[2][2] =  { { GL_SRGB8_ALPHA8_EXT, GL_RGBA },
			       { GL_SRGB8_EXT, GL_RGB } };
54 55 56 57 58 59

	glGenFramebuffersEXT(1, &fb);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
60
	glTexImage2D(GL_TEXTURE_2D, 0, formats[srgb_format][0],
61
		     w, h, 0,
62
		     formats[srgb_format][1], GL_UNSIGNED_BYTE, NULL);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
				  GL_COLOR_ATTACHMENT0_EXT,
				  GL_TEXTURE_2D,
				  tex,
				  0);
	assert(glGetError() == 0);

	status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
	if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
		fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status);
		piglit_report_result(PIGLIT_SKIP);
	}

80
	*tex_p = tex;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	return fb;
}

static void
draw_fbo(int x, int y)
{
	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

	glEnable(GL_TEXTURE_2D);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

	piglit_draw_rect_tex(x, y, FBO_SIZE, FBO_SIZE,
			     0, 0, 1, 1);
102
	glDisable(GL_TEXTURE_2D);
103 104 105
}

static GLboolean
106
framebuffer_srgb_fbo(int srgb_format)
107 108 109 110 111 112 113 114
{
	GLboolean pass = GL_TRUE;
	int fbo_width = FBO_SIZE;
	int fbo_height = FBO_SIZE;
	GLuint fbo;
	float green[] = {0, 0.3, 0.0, 0};
	float expected_green[4];
	float expected_blend[4];
Dave Airlie's avatar
Dave Airlie committed
115
	GLuint tex;
116 117 118 119

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

120 121
	fbo = make_fbo(fbo_width, fbo_height, srgb_format, &tex);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
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

	assert(glGetError() == 0);

	glViewport(0, 0, fbo_width, fbo_height);
	piglit_ortho_projection(fbo_width, fbo_height, GL_FALSE);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glColor4fv(green);
	piglit_draw_rect(0, 0, 20, 20);

	piglit_draw_rect(30, 30, 20, 20);

	/* enable blending and draw a green square with sRGB enabled on top of the previous green square */
	piglit_draw_rect(20, 0, 20, 20);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE);
	piglit_draw_rect(20, 0, 20, 20);

	glDisable(GL_BLEND);

	if (!piglit_probe_rect_rgb(0, 0, 20, 20, green))
		pass = GL_FALSE;

	memcpy(expected_green, green, sizeof(float) * 4);

	if (!piglit_probe_rect_rgb(30, 30, 20, 20, expected_green))
		pass = GL_FALSE;
	
	memcpy(expected_blend, green, sizeof(float) * 4);
	expected_blend[1] = green[1] * 2.0;

	if (!piglit_probe_rect_rgb(20, 0, 20, 20, expected_blend))
		pass = GL_FALSE;

	glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
	glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
159
	
160
	draw_fbo(0, 0);
161

162
	glDeleteFramebuffersEXT(1, &fbo);
163
	glDeleteTextures(1, &tex);
164
	piglit_present_results();
165

166 167 168 169 170 171 172 173
	return pass;
}

enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;

174 175 176 177 178 179 180
	pass = framebuffer_srgb_fbo(0);
	if (pass == GL_TRUE) {
		pass = framebuffer_srgb_fbo(1);
		if (pass == GL_FALSE)
			printf("Failed on format SRGB8\n");
	} else
		printf("Failed on format SRGB8_ALPHA8\n");
181

182
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
}


static void reshape(int width, int height)
{
	piglit_width = width;
	piglit_height = height;

	piglit_ortho_projection(width, height, GL_FALSE);
}

void
piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_EXT_texture_sRGB");
	piglit_require_extension("GL_EXT_framebuffer_object");
	reshape(piglit_width, piglit_height);
}