1 /*
2  * Copyright (c) 2013 Intel Corporation
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 interpolateAtSample.c
26  *
27  * Test ARB_gpu_shader5 interpolateAtSample builtin
28  *
29  * Tests that interpolateAtSample(x) gives the correct result.
30  *
31  * R, 1-G channels are interesting; a correct implementation should produce
32  * (0,1,0) in all pixels.
33  *
34  * We require 3.2, so the following assumptions are made:
35  * - MAX_SAMPLES >= 4 (although we dont require exactly 4 samples; if only an
36  *   8x mode is supported, the test should still work)
37  * - GLSL 1.50 and Multisample textures are supported.
38  */
39 
40 
41 #include "piglit-util-gl.h"
42 
43 PIGLIT_GL_TEST_CONFIG_BEGIN
44 
45 	config.supports_gl_core_version = 32;
46 
47 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
48 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
49 
50 PIGLIT_GL_TEST_CONFIG_END
51 
52 GLuint ms_fbo, vao, bo;
53 GLint draw_prog, test_prog;
54 GLint sample_pos_loc;
55 
56 float green[] = { 0, 1, 0 };
57 float verts[][2] = { { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 1 }, };
58 #define GAIN "5"			/* multiplier for absolute difference; make the error more visible. */
59 
60 
61 enum piglit_result
piglit_display(void)62 piglit_display(void)
63 {
64 	bool pass = true;
65 	GLfloat pos[2];
66 	glGetMultisamplefv(GL_SAMPLE_POSITION, 0, pos);
67 	/* API gives us 0..1 sample position; get it into
68 	 * -0.5..0.5 space.
69 	 */
70 	pos[0] -= 0.5f;
71 	pos[1] -= 0.5f;
72 
73 	glViewport(0, 0, 64, 64);
74 
75 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo);
76 	glUseProgram(draw_prog);
77 	glUniform2fv(sample_pos_loc, 1, pos);
78 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
79 
80 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
81 	glUseProgram(test_prog);
82 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
83 
84 	pass = piglit_probe_rect_rgb(0, 0, 64, 64, green) && pass;
85 
86 	piglit_present_results();
87 
88 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
89 }
90 
91 
92 void
piglit_init(int argc,char ** argv)93 piglit_init(int argc, char**argv)
94 {
95 	GLuint tex;
96 	piglit_require_extension("GL_ARB_gpu_shader5");
97 
98 	glGenFramebuffers(1, &ms_fbo);
99 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo);
100 	glGenTextures(1, &tex);
101 	glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
102 	glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4,
103 				GL_RGBA, 64, 64, GL_TRUE);
104 	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
105 			       GL_TEXTURE_2D_MULTISAMPLE, tex, 0);
106 
107 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
108 		printf("fbo setup failed.\n");
109 		piglit_report_result(PIGLIT_SKIP);
110 	}
111 
112 	/* Test quantity varies from -1 to +1 over 64 pixels --
113 	 * so moving 1px changes its value by 1/32.
114 	 */
115 	draw_prog = piglit_build_simple_program(
116 		"#version 150\n"
117 		"uniform vec2 sample_pos;\n"
118 		"in vec2 p;\n"
119 		"out vec2 test;\n"
120 		"out vec2 ref;\n"
121 		"void main() {\n"
122 		"	gl_Position = vec4(p, 0, 1);\n"
123 		"	test = p;\n"
124 		"	ref = p;\n"
125 		"	ref.xy += sample_pos / 32;\n"
126 		"}\n",
127 
128 		"#version 150\n"
129 		"#extension GL_ARB_gpu_shader5: require\n"
130 		"const int sample_id = 0;\n"
131 		"in vec2 test;\n"
132 		"in vec2 ref;\n"
133 		"void main() {\n"
134 		"	gl_FragColor = vec4(" GAIN " * abs(\n"
135 		"		interpolateAtSample(test, sample_id) - ref), 0, 1);\n"
136 		"}\n");
137 	if (!draw_prog) {
138 		printf("draw_prog compile/link failed\n");
139 		piglit_report_result(PIGLIT_FAIL);
140 	}
141 
142 	test_prog = piglit_build_simple_program(
143 		"#version 150\n"
144 		"in vec2 p;\n"
145 		"void main() {\n"
146 		"	gl_Position = vec4(p, 0, 1);\n"
147 		"}\n",
148 
149 		"#version 150\n"
150 		"uniform sampler2DMS s;\n"
151 		"void main() {\n"
152 		"	vec4 temp = \n"
153 		"		texelFetch(s, ivec2(gl_FragCoord.xy), 0) +\n"
154 		"		texelFetch(s, ivec2(gl_FragCoord.xy), 1) +\n"
155 		"		texelFetch(s, ivec2(gl_FragCoord.xy), 2) +\n"
156 		"		texelFetch(s, ivec2(gl_FragCoord.xy), 3);\n"
157 		"	gl_FragColor = vec4(temp.x, 1-temp.y, temp.z, temp.w);\n"
158 		"}\n");
159 	if (!test_prog) {
160 		printf("test_prog compile/link failed\n");
161 		piglit_report_result(PIGLIT_FAIL);
162 	}
163 
164 	sample_pos_loc = glGetUniformLocation(draw_prog, "sample_pos");
165 
166 	glUseProgram(test_prog);
167 	glUniform1i(glGetUniformLocation(test_prog, "s"), 0);
168 
169 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
170 		printf("shader setup failed\n");
171 		piglit_report_result(PIGLIT_SKIP);
172 	}
173 
174 	glGenVertexArrays(1, &vao);
175 	glBindVertexArray(vao);
176 
177 	glEnableVertexAttribArray(0);
178 	glGenBuffers(1, &bo);
179 	glBindBuffer(GL_ARRAY_BUFFER, bo);
180 	glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
181 	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid const *)0);
182 }
183 
184