1 /*
2  * Copyright © 2011 Marek Olšák
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 /** @file blending-in-shader.c
26  *
27  * Test programmable blending with GL_NV_texture_barrier.
28  */
29 
30 #include "piglit-util-gl.h"
31 
32 PIGLIT_GL_TEST_CONFIG_BEGIN
33 
34 	config.supports_gl_compat_version = 10;
35 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
36 
37 PIGLIT_GL_TEST_CONFIG_END
38 
39 static GLuint tex, fbo, prog, texloc;
40 static float tex_data[16*16*4], res_data[16*16*4];
41 
42 static const char *fstext = {
43 	"uniform sampler2D fb;"
44 	"void main() {"
45 	"  gl_FragColor = sqrt(texture2D(fb, gl_FragCoord.xy / 16.0));"
46 	"}"
47 };
48 
49 #define PASSES 3
50 
piglit_display(void)51 enum piglit_result piglit_display(void)
52 {
53 	GLboolean pass = GL_TRUE;
54 	int i;
55 
56 	glBindTexture(GL_TEXTURE_2D, tex);
57 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 16, 16, 0, GL_RGBA, GL_FLOAT, tex_data);
58 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
59 	glViewport(0, 0, 16, 16);
60 
61 	glUseProgram(prog);
62 	glUniform1i(texloc, 0);
63 
64 	for (i = 0; i < PASSES; i++) {
65 		if (i != 0)
66 			glTextureBarrierNV();
67 		piglit_draw_rect_tex(-1, -1, 2, 2, 0, 0, 1, 1);
68 	}
69 
70 	pass = piglit_probe_image_rgba(0, 0, 16, 16, res_data);
71 
72 	glUseProgram(0);
73 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);
74 	glViewport(0, 0, piglit_width, piglit_height);
75 
76 	piglit_draw_rect_tex(-1, -1, 2, 2, 0, 0, 1, 1);
77 
78 	piglit_present_results();
79 
80 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
81 }
82 
piglit_init(int argc,char ** argv)83 void piglit_init(int argc, char **argv)
84 {
85 	unsigned int i, j;
86 
87 	piglit_require_extension("GL_EXT_framebuffer_object");
88 	piglit_require_extension("GL_NV_texture_barrier");
89         piglit_require_GLSL();
90 
91 	srand(0);
92 	for (i = 0; i < 16 * 16 * 4; ++i) {
93 		tex_data[i] = (rand() % 256) / 255.f;
94 		res_data[i] = tex_data[i];
95 		for (j = 0; j < PASSES; j++)
96 			res_data[i] = sqrt(res_data[i]);
97 	}
98 
99 	glEnable(GL_TEXTURE_2D);
100 	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
101 
102 	glGenTextures(1, &tex);
103 	glBindTexture(GL_TEXTURE_2D, tex);
104 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
105 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
106 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
107 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
108 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 16, 16, 0, GL_RGBA, GL_FLOAT, NULL);
109 
110 	glGenFramebuffersEXT(1, &fbo);
111 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
112 	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
113 	assert(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT);
114 
115 	prog = piglit_build_simple_program(NULL, fstext);
116 
117 	texloc = glGetUniformLocation(prog, "fb");
118 }
119