Add more structure constructor tests.
[piglit/hramrach.git] / tests / fbo / fbo-alpha.c
blob479dabea4519884d971423bda2538fa52c5ff75b
1 /*
2 * Copyright © 2010 Intel Corporation
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:
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, 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.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 /** @file fbo-alpha.c
30 * Tests that rendering to and blending on a GL_ALPHA FBO works with
31 * GL_ARB_framebuffer_object.
34 #include "piglit-util.h"
36 int piglit_width = 128;
37 int piglit_height = 128;
38 int piglit_window_mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE;
40 enum piglit_result
41 piglit_display(void)
43 GLboolean pass = GL_TRUE;
44 GLuint tex, fb;
45 GLenum status;
46 float fbo_white[] = {0.0, 0.0, 0.0, 1.0};
47 float fbo_black[] = {0.0, 0.0, 0.0, 0.0};
48 float fbo_gray[] = {0.0, 0.0, 0.0, 0.5};
49 float white[] = {1.0, 1.0, 1.0, 1.0};
50 float black[] = {0.0, 0.0, 0.0, 0.0};
51 float gray[] = {0.5, 0.5, 0.5, 0.5};
52 int fbo_width = 64;
53 int fbo_height = 64;
55 glGenFramebuffersEXT(1, &fb);
56 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
57 glViewport(0, 0, fbo_width, fbo_height);
59 glGenTextures(1, &tex);
60 glBindTexture(GL_TEXTURE_2D, tex);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
63 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA,
64 fbo_width, fbo_height, 0,
65 GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
67 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
68 GL_COLOR_ATTACHMENT0_EXT,
69 GL_TEXTURE_2D,
70 tex,
71 0);
72 assert(glGetError() == 0);
74 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
75 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
76 fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status);
77 piglit_report_result(PIGLIT_SKIP);
80 /* Clear to no alpha. */
81 glClearColor(0.0, 0.0, 0.0, 0.0);
82 glClear(GL_COLOR_BUFFER_BIT);
84 glColor4f(0.0, 0.0, 0.0, 0.0);
85 piglit_draw_rect(-1.0, -1.0, 0.5, 2.0);
87 glColor4f(0.0, 0.0, 0.0, 1.0);
88 piglit_draw_rect(-0.5, -1.0, 0.5, 2.0);
90 glColor4f(0.0, 0.0, 0.0, 0.5);
91 piglit_draw_rect(0.0, -1.0, 0.5, 2.0);
92 glEnable(GL_BLEND);
93 glBlendFunc(GL_DST_ALPHA, GL_ZERO);
94 glColor4f(0.0, 0.0, 0.0, 1.0);
95 piglit_draw_rect(0.0, -1.0, 0.5, 2.0);
96 glDisable(GL_BLEND);
98 glColor4f(0.0, 0.0, 0.0, 0.5);
99 piglit_draw_rect(0.5, -1.0, 0.5, 2.0);
100 glEnable(GL_BLEND);
101 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
102 glColor4f(0.0, 0.0, 0.0, 1.0);
103 piglit_draw_rect(0.5, -1.0, 0.5, 2.0);
104 glDisable(GL_BLEND);
106 printf("Testing FBO result.\n");
107 pass = piglit_probe_pixel_rgba(fbo_width * 1 / 8, 0, fbo_black) && pass;
108 pass = piglit_probe_pixel_rgba(fbo_width * 3 / 8, 0, fbo_white) && pass;
109 pass = piglit_probe_pixel_rgba(fbo_width * 5 / 8, 0, fbo_gray) && pass;
110 pass = piglit_probe_pixel_rgba(fbo_width * 7 / 8, 0, fbo_gray) && pass;
112 /* Draw the two textures to halves of the window. */
113 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
114 glViewport(0, 0, piglit_width, piglit_height);
116 glEnable(GL_TEXTURE_2D);
117 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
118 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
119 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
120 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_ALPHA);
121 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
122 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
123 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
125 glBindTexture(GL_TEXTURE_2D, tex);
126 piglit_draw_rect_tex(-1, -1, 2, 2,
127 0, 0, 1, 1);
129 glDisable(GL_TEXTURE_2D);
130 glDeleteTextures(1, &tex);
131 glDeleteFramebuffersEXT(1, &fb);
133 printf("Testing window result.\n");
134 pass = piglit_probe_pixel_rgba(piglit_width * 1 / 8, 0, black) && pass;
135 pass = piglit_probe_pixel_rgba(piglit_width * 3 / 8, 0, white) && pass;
136 pass = piglit_probe_pixel_rgba(piglit_width * 5 / 8, 0, gray) && pass;
137 pass = piglit_probe_pixel_rgba(piglit_width * 7 / 8, 0, gray) && pass;
139 glutSwapBuffers();
141 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
144 void
145 piglit_init(int argc, char **argv)
147 piglit_require_extension("GL_ARB_framebuffer_object");
148 piglit_require_extension("GL_ARB_texture_env_combine");