1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
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
21  * DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include "piglit-util-gl.h"
26 
27 /* File: stencil-clear.c
28  *
29  * Tests whether clearing a multisample stencil texture, followed by a blit
30  * and subsequent rendering works correctly.
31  */
32 
33 #define TEX_WIDTH 256
34 #define TEX_HEIGHT 256
35 
36 PIGLIT_GL_TEST_CONFIG_BEGIN
37 
38     config.supports_gl_compat_version = 30;
39     config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
40     config.window_width = TEX_WIDTH;
41     config.window_height = TEX_HEIGHT;
42     config.khr_no_error_support = PIGLIT_NO_ERRORS;
43 
44 PIGLIT_GL_TEST_CONFIG_END
45 
46 static GLuint fbo, fbo_copy;
47 
48 static void
usage(void)49 usage(void)
50 {
51 	fprintf(stderr, "usage: arb_texture_multisample-stencil-clear [samples N]\n");
52 	exit(1);
53 }
54 
55 enum piglit_result
piglit_display(void)56 piglit_display(void)
57 {
58 	static const float black[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
59 	bool pass = true;
60 
61 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
62 	glViewport(0, 0, TEX_WIDTH, TEX_HEIGHT);
63 
64 	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
65 	glClearDepth(0.0f);
66 	glClearStencil(0);
67 	glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
68 
69 	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
70 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_copy);
71 
72 	/* This blit is there on purpose to trigger a bug in stencil decompress
73 	 * on Radeon. The blit destination is not used.
74 	 */
75 	glBlitFramebuffer(0, 0, TEX_WIDTH, TEX_WIDTH, 0, 0, TEX_WIDTH, TEX_WIDTH, GL_STENCIL_BUFFER_BIT, GL_NEAREST);
76 	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
77 
78 	glEnable(GL_STENCIL_TEST);
79 	glStencilMask(255);
80 	glStencilFunc(GL_LEQUAL, 1, 255);
81 	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
82 
83 	glColor4f(1.0, 1.0, 1.0, 1.0);
84 	glBegin(GL_TRIANGLE_FAN);
85 		glVertex3f( 0.0174f, -0.00413f, 1.0f);
86 		glVertex3f(-1.0f, -1.0f,  1.0f);
87 		glVertex3f( 1.0f, -1.0f, -1.0f);
88 		glVertex3f( 1.0f,  1.0f,  1.0f);
89 		glVertex3f(-1.0f,  1.0f, -1.0f);
90 		glVertex3f(-1.0f, -1.0f, -1.0f);
91 	glEnd();
92 
93 	glDisable(GL_STENCIL_TEST);
94 
95 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
96 	glClearColor(1.0, 0.0, 1.0, 1.0);
97 	glClear(GL_COLOR_BUFFER_BIT);
98 
99 	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
100 	glBlitFramebuffer(0, 0, TEX_WIDTH, TEX_HEIGHT, 0, 0, TEX_WIDTH, TEX_HEIGHT,
101 			  GL_COLOR_BUFFER_BIT, GL_NEAREST);
102 	glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);
103 
104 	pass = piglit_probe_rect_rgb(0, 0, TEX_WIDTH, TEX_HEIGHT, black) && pass;
105 
106 	piglit_present_results();
107 
108 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
109 }
110 
create_fbo(unsigned num_color_samples,unsigned num_depth_samples)111 GLuint create_fbo(unsigned num_color_samples, unsigned num_depth_samples)
112 {
113 	GLenum tex_target;
114 	GLuint texColor;
115 	GLuint texZS;
116 	GLuint fbo;
117 
118 	/* setup an fbo with (optionally multisample) textures */
119 	glGenTextures(1, &texColor);
120 	glGenTextures(1, &texZS);
121 
122 	if (num_color_samples != 0) {
123 		tex_target = GL_TEXTURE_2D_MULTISAMPLE;
124 
125 		glBindTexture(tex_target, texZS);
126 		glTexImage2DMultisample(
127 			tex_target, num_depth_samples, GL_DEPTH32F_STENCIL8,
128 			TEX_WIDTH, TEX_HEIGHT, GL_TRUE);
129 
130 		glBindTexture(tex_target, texColor);
131 		glTexImage2DMultisample(
132 			tex_target, num_color_samples, GL_RGBA8,
133 			TEX_WIDTH, TEX_HEIGHT, GL_TRUE);
134 	} else {
135 		tex_target = GL_TEXTURE_2D;
136 
137 		glBindTexture(tex_target, texZS);
138 		glTexParameteri(tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139 		glTexParameteri(tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
140 		glTexImage2D(tex_target, 0, GL_DEPTH32F_STENCIL8,
141 			     TEX_WIDTH, TEX_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
142 
143 		glBindTexture(tex_target, texColor);
144 		glTexParameteri(tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
145 		glTexParameteri(tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
146 		glTexImage2D(tex_target, 0, GL_RGBA8, TEX_WIDTH, TEX_HEIGHT, 0,
147 			     GL_RGBA, GL_FLOAT, NULL);
148 	}
149 
150 	glGenFramebuffers(1, &fbo);
151 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
152 	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
153 			GL_DEPTH_STENCIL_ATTACHMENT,
154 			tex_target, texZS, 0);
155 	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
156 			GL_COLOR_ATTACHMENT0,
157 			tex_target, texColor, 0);
158 
159 	if (!piglit_check_gl_error(GL_NO_ERROR)) {
160 		printf("Error during tex/fbo setup; no point continuing.\n");
161 		piglit_report_result(PIGLIT_FAIL);
162 	}
163 
164 	return fbo;
165 }
166 
167 void
piglit_init(int argc,char ** argv)168 piglit_init(int argc, char **argv)
169 {
170 	GLint num_color_samples, num_depth_samples;
171 
172 	piglit_require_extension("GL_ARB_texture_multisample");
173 
174 	/* By default, se the max number of samples for testing */
175 	glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, &num_color_samples);
176 	glGetIntegerv(GL_MAX_DEPTH_TEXTURE_SAMPLES, &num_depth_samples);
177 
178 	for (int i = 1; i < argc; ++i) {
179 		if (!strcmp(argv[i], "samples")) {
180 			++i;
181 			if (i >= argc)
182 				usage();
183 			num_color_samples = num_depth_samples = atoi(argv[i]);
184 		} else
185 			usage();
186 	}
187 
188 	printf("Number of color samples: %u  depth samples: %d\n",
189 	       num_color_samples, num_depth_samples);
190 
191 	fbo = create_fbo(num_color_samples, num_depth_samples);
192 	fbo_copy = create_fbo(0, 0);
193 }
194