/*
 * 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.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 30;
	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");

	setup_textures();
	setup_shaders();
}
