gl-3.1: use glsl 1.40 for mixed-int-float-fbo
[piglit.git] / tests / spec / gl-3.1 / mixed-int-float-fbo.c
blobd97e23d7048273c05d892871c46ca3758cd5eae7
1 /*
2 * Copyright (c) 2016 VMware, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
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.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
25 /**
26 * Test mixed integer/float FBO.
28 * If the argument 'int_second' is given the 0th color attachment will
29 * be a unorm texture and the 1st color attachment will be an integer texture.
30 * Otherwise, the 0th color attachment will be integer and the 1st color
31 * attachment will be unorm.
34 #include "piglit-util-gl.h"
36 static bool ext_gpu_shader4 = false;
38 PIGLIT_GL_TEST_CONFIG_BEGIN
39 for (int i = 1; i < argc; i++) {
40 if (!strcmp(argv[i], "ext_gpu_shader4")) {
41 ext_gpu_shader4 = true;
42 puts("Testing GL_EXT_gpu_shader4.");
43 break;
47 if (ext_gpu_shader4)
48 config.supports_gl_compat_version = 10;
49 else
50 config.supports_gl_core_version = 31;
52 config.window_visual = PIGLIT_GL_VISUAL_RGBA;
53 config.khr_no_error_support = PIGLIT_NO_ERRORS;
54 PIGLIT_GL_TEST_CONFIG_END
56 static const char *vs_text =
57 "#version 140\n"
58 "in vec4 vertex;\n"
59 "void main() \n"
60 "{ \n"
61 " gl_Position = vertex; \n"
62 "} \n";
64 static const char *fs_text =
65 "#version 140\n"
66 "out ivec4 outputInt;\n"
67 "out vec4 outputFloat;\n"
68 "void main() \n"
69 "{ \n"
70 " outputInt = ivec4(1, 2, 3, 4); \n"
71 " outputFloat = vec4(0.25, 0.5, 0.75, 1.0); \n"
72 "} \n";
74 static const char *vs_text_gpu_shader4 =
75 "#version 110\n"
76 "attribute vec4 vertex;\n"
77 "void main() \n"
78 "{ \n"
79 " gl_Position = vertex; \n"
80 "} \n";
82 static const char *fs_text_gpu_shader4 =
83 "#version 110\n"
84 "#extension GL_EXT_gpu_shader4 : enable\n"
85 "varying out ivec4 outputInt;\n"
86 "varying out vec4 outputFloat;\n"
87 "void main() \n"
88 "{ \n"
89 " outputInt = ivec4(1, 2, 3, 4); \n"
90 " outputFloat = vec4(0.25, 0.5, 0.75, 1.0); \n"
91 "} \n";
93 const int width = 128, height = 128;
94 bool int_output_first = true;
97 static GLuint
98 create_program(void)
100 GLuint program;
102 if (ext_gpu_shader4) {
103 program = piglit_build_simple_program(vs_text_gpu_shader4,
104 fs_text_gpu_shader4);
106 if (int_output_first) {
107 glBindFragDataLocationEXT(program, 0, "outputInt");
108 glBindFragDataLocationEXT(program, 1, "outputFloat");
110 else {
111 glBindFragDataLocationEXT(program, 0, "outputFloat");
112 glBindFragDataLocationEXT(program, 1, "outputInt");
114 } else {
115 program = piglit_build_simple_program(vs_text, fs_text);
117 if (int_output_first) {
118 glBindFragDataLocation(program, 0, "outputInt");
119 glBindFragDataLocation(program, 1, "outputFloat");
121 else {
122 glBindFragDataLocation(program, 0, "outputFloat");
123 glBindFragDataLocation(program, 1, "outputInt");
127 glLinkProgram(program);
128 if (!piglit_link_check_status(program))
129 piglit_report_result(PIGLIT_FAIL);
131 piglit_check_gl_error(GL_NO_ERROR);
133 return program;
137 static GLuint
138 create_fbo(void)
140 GLuint intTex, unormTex, fbo;
142 glGenTextures(1, &intTex);
143 glBindTexture(GL_TEXTURE_2D, intTex);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, width, height, 0,
147 GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, NULL);
149 glGenTextures(1, &unormTex);
150 glBindTexture(GL_TEXTURE_2D, unormTex);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
153 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
154 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
156 glGenFramebuffers(1, &fbo);
157 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
158 if (int_output_first) {
159 glFramebufferTexture2D(GL_FRAMEBUFFER,
160 GL_COLOR_ATTACHMENT0_EXT,
161 GL_TEXTURE_2D, intTex, 0);
162 glFramebufferTexture2D(GL_FRAMEBUFFER,
163 GL_COLOR_ATTACHMENT1_EXT,
164 GL_TEXTURE_2D, unormTex, 0);
166 else {
167 glFramebufferTexture2D(GL_FRAMEBUFFER,
168 GL_COLOR_ATTACHMENT0_EXT,
169 GL_TEXTURE_2D, unormTex, 0);
170 glFramebufferTexture2D(GL_FRAMEBUFFER,
171 GL_COLOR_ATTACHMENT1_EXT,
172 GL_TEXTURE_2D, intTex, 0);
175 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
176 if (status != GL_FRAMEBUFFER_COMPLETE) {
177 printf("Mixed int/float FBO is incomplete. Skipping test.\n");
178 piglit_report_result(PIGLIT_SKIP);
181 static const GLenum draw_bufs[2] = {
182 GL_COLOR_ATTACHMENT0,
183 GL_COLOR_ATTACHMENT1
185 glDrawBuffers(2, draw_bufs);
187 if (!piglit_check_gl_error(GL_NO_ERROR)) {
188 printf("Test setup failed\n");
189 piglit_report_result(PIGLIT_SKIP);
192 return fbo;
196 enum piglit_result
197 piglit_display(void)
199 if (ext_gpu_shader4) {
200 glClearColor(0, 0, 0, 0);
201 glClear(GL_COLOR_BUFFER_BIT);
202 } else {
203 const int int_clear[4] = { 99, 99, 99, 99 };
204 const float float_clear[4] = { 0.33, 0.33, 0.33, 0.33 };
206 if (int_output_first) {
207 glClearBufferiv(GL_COLOR, 0, int_clear);
208 glClearBufferfv(GL_COLOR, 1, float_clear);
210 else {
211 glClearBufferfv(GL_COLOR, 0, float_clear);
212 glClearBufferiv(GL_COLOR, 1, int_clear);
216 piglit_draw_rect(-1, -1, 2, 2);
218 bool pass = true;
220 /* check the int target */
221 if (int_output_first) {
222 glReadBuffer(GL_COLOR_ATTACHMENT0);
224 else {
225 glReadBuffer(GL_COLOR_ATTACHMENT1);
227 const int expected_int[4] = {1, 2, 3, 4};
228 if (!piglit_probe_rect_rgba_int(0, 0, width, height, expected_int)) {
229 printf("Failed probing integer color buffer on"
230 " GL_COLOR_ATTACHMENT%d.\n",
231 (int) !int_output_first);
232 pass = false;
235 /* check the unorm target */
236 if (int_output_first) {
237 glReadBuffer(GL_COLOR_ATTACHMENT1);
239 else {
240 glReadBuffer(GL_COLOR_ATTACHMENT0);
242 const float expected_unorm[4] = {0.25, 0.5, 0.75, 1.0};
243 if (!piglit_probe_rect_rgba(0, 0, width, height, expected_unorm)) {
244 printf("Failed probing unorm color buffer on"
245 " GL_COLOR_ATTACHMENT%d.\n",
246 (int) int_output_first);
247 pass = false;
250 if (piglit_is_extension_supported("GL_EXT_texture_integer")) {
251 /* This query is only part of the extension, not core GL */
252 GLboolean intMode = 0;
253 glGetBooleanv(GL_RGBA_INTEGER_MODE_EXT, &intMode);
254 if (!intMode) {
255 printf("GL_RGBA_INTEGER_MODE_EXT incorrectly"
256 " returned false.\n");
257 pass = false;
261 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
263 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
267 void
268 piglit_init(int argc, char **argv)
270 if (ext_gpu_shader4) {
271 piglit_require_gl_version(20);
272 piglit_require_extension("GL_ARB_framebuffer_object");
273 piglit_require_extension("GL_EXT_gpu_shader4");
274 piglit_require_extension("GL_EXT_texture_integer");
277 if (argc > 1 && strcmp(argv[1], "int_second") == 0) {
278 int_output_first = false;
281 GLuint fbo = create_fbo();
282 GLuint program = create_program();
284 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
285 glUseProgram(program);