fbo-deriv.c 4.56 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 36 37 38 39 40 41 42 43 44 45
/*
 * Copyright © 2012 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 fbo-deriv.c
 *
 * Verify that the implementation produces correct values for the GLSL
 * dFdx() and dFdy() functions, both in fbos and in the default
 * framebuffer.
 *
 * Note: the reason that we test both fbos and the default framebuffer
 * in the same test is that some implementations (e.g. Mesa/i965) need
 * to compile the dFdy() function differently depending whether we are
 * rendering to an FBO or to the default framebuffer; testing both in
 * the same test allows us to verify that the implementation
 * recompiles the shader if necessary.
 *
 * This test draws a pair of squares in which dFdx and dFdy are
 * expected to both be 1.0.  It colors the rectangles red=0.5*dFdx and
 * green=0.5*dFdy, so the expected color is (0.5, 0.5, 0, 0).  The
 * left rectangle is drawn in the default framebuffer; the right
 * rectangle is drawn in an FBO and then blitted to the default
 * framebuffer.
 */

46
#include "piglit-util-gl-common.h"
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

PIGLIT_GL_TEST_MAIN(
    256 /*window_width*/,
    128 /*window_height*/,
    GLUT_DOUBLE | GLUT_RGBA)

static const int fbo_width = 128, fbo_height = 128;

static GLuint fbo;
static GLint prog;

static const char *vert =
	"void main()\n"
	"{\n"
	"  gl_Position = gl_Vertex;\n"
	"}\n";

static const char *frag =
	"void main()\n"
	"{\n"
	"  gl_FragColor = vec4(0.5*dFdx(gl_FragCoord.x),\n"
	"                      0.5*dFdy(gl_FragCoord.y), 0.0, 0.0);\n"
	"}\n";

static void
create_fbo()
{
	GLenum status;
	GLuint rb;

	glGenRenderbuffers(1, &rb);
	glBindRenderbuffer(GL_RENDERBUFFER, rb);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, fbo_width, fbo_height);
	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				  GL_RENDERBUFFER, rb);
	status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	switch (status) {
	case GL_FRAMEBUFFER_UNSUPPORTED:
		printf("Framebuffer unsupported\n");
		piglit_report_result(PIGLIT_SKIP);
		break;
	case GL_FRAMEBUFFER_COMPLETE:
		break;
	default:
		printf("Framebuffer incomplete\n");
		piglit_report_result(PIGLIT_FAIL);
		break;
	}
}

void
piglit_init(int argc, char **argv)
{
	GLint vs, fs;

	piglit_require_gl_version(20);

	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vert);
	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag);
	prog = piglit_link_simple_program(vs, fs);

	create_fbo();
}

enum piglit_result
piglit_display(void)
{
	float expected[3] = { 0.5, 0.5, 0.0 };
	GLboolean pass = GL_TRUE;

	glUseProgram(prog);

	/* Draw a square to the left half of the window */
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	glViewport(0, 0, piglit_width, piglit_height);
	piglit_draw_rect(-1, -1, 1, 2);

	/* Draw a square to the FBO */
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glViewport(0, 0, fbo_width, fbo_height);
	piglit_draw_rect(-1, -1, 2, 2);

	/* Blit the square from the FBO to the right half of the window */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	glBlitFramebuffer(0, 0, fbo_width, fbo_height,
			  piglit_width/2, 0, piglit_width, piglit_height,
			  GL_COLOR_BUFFER_BIT, GL_NEAREST);

	/* Check that both squares have the correct color */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
	pass = piglit_probe_rect_rgb(0, 0, piglit_width, piglit_height,
				     expected) && pass;

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}