1 /*
2  * (C) Copyright IBM Corporation 2005
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file crossbar.c
27  *
28  * Simple test of GL_ARB_texture_env_crossbar functionality.  Several squares
29  * are drawn with different texture combine modes, but all should be rendered
30  * with the same final color.
31  *
32  * \author Ian Romanick <idr@us.ibm.com>
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "piglit-util-gl.h"
40 
41 static const GLint tests[][8] = {
42    { 1, GL_REPLACE,  GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
43      2, GL_REPLACE,  GL_TEXTURE,       GL_PRIMARY_COLOR },
44    { 3, GL_REPLACE,  GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
45      2, GL_SUBTRACT, GL_TEXTURE0,      GL_TEXTURE1 },
46    { 2, GL_REPLACE,  GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
47      2, GL_REPLACE,  GL_TEXTURE0,      GL_TEXTURE0 },
48    { 2, GL_REPLACE,  GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
49      1, GL_SUBTRACT, GL_TEXTURE0,      GL_TEXTURE1 },
50    { 3, GL_ADD,      GL_TEXTURE1,      GL_TEXTURE1,
51      2, GL_MODULATE, GL_TEXTURE1,      GL_PREVIOUS },
52    { 3, GL_ADD,      GL_TEXTURE1,      GL_TEXTURE1,
53      4, GL_MODULATE, GL_TEXTURE0,      GL_PREVIOUS },
54 };
55 
56 #define NUM_TESTS (sizeof(tests) / sizeof(tests[0]))
57 
58 PIGLIT_GL_TEST_CONFIG_BEGIN
59 
60 	config.supports_gl_compat_version = 10;
61 
62 	config.window_width = 100*(NUM_TESTS+1);
63 	config.window_height = 100;
64 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
65 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
66 
67 PIGLIT_GL_TEST_CONFIG_END
68 
DoFrame(void)69 static void DoFrame( void )
70 {
71    unsigned i;
72 
73    glClearColor(0.0, 0.0, 1.0, 0);
74    glClear( GL_COLOR_BUFFER_BIT );
75 
76    glPushMatrix();
77 
78    /* This is the "reference" square.
79     */
80 
81    glActiveTexture( GL_TEXTURE0 );
82    glDisable( GL_TEXTURE_2D );
83    glActiveTexture( GL_TEXTURE1 );
84    glDisable( GL_TEXTURE_2D );
85 
86    glTranslatef(1.5, 0.0, 0.0);
87    glBegin(GL_QUADS);
88    glColor3f( 0.5, 0.5, 0.5 );
89    glVertex2f(-1, -1);
90    glVertex2f( 1, -1);
91    glVertex2f( 1,  1);
92    glVertex2f(-1,  1);
93    glEnd();
94 
95    for ( i = 0 ; i < NUM_TESTS ; i++ ) {
96       glActiveTexture( GL_TEXTURE0 );
97       glEnable( GL_TEXTURE_2D );
98       glBindTexture( GL_TEXTURE_2D, tests[i][0] );
99       glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
100       glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][1] );
101       glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][2] );
102       glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][3] );
103 
104       glActiveTexture( GL_TEXTURE1 );
105       glEnable( GL_TEXTURE_2D );
106       glBindTexture( GL_TEXTURE_2D, tests[i][4] );
107       glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
108       glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][5] );
109       glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][6] );
110       glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][7] );
111 
112       glCallList(1);
113    }
114 
115    glPopMatrix();
116 
117 }
118 
DoTest(void)119 static bool DoTest( void )
120 {
121    const static float expected[] = {0.5f, 0.5f, 0.5f};
122    bool pass = true;
123    int i;
124 
125    for( i = 0; i <= NUM_TESTS; ++i ) {
126 	   pass = piglit_probe_pixel_rgb(piglit_width*(2*i+1)/((NUM_TESTS+1)*2),
127 			   piglit_height/2,
128 			   expected) && pass;
129    }
130 
131    return pass;
132 }
133 
134 enum piglit_result
piglit_display(void)135 piglit_display(void)
136 {
137    if (piglit_automatic) {
138       DoFrame();
139       piglit_present_results();
140       return PIGLIT_PASS;
141    } else {
142       bool success, retry;
143 
144       printf("\nFirst frame\n-----------\n");
145       DoFrame();
146       success = DoTest();
147       piglit_present_results();
148 
149       printf("\nSecond frame\n------------\n");
150 
151       DoFrame();
152       retry = DoTest();
153       piglit_present_results();
154 
155       if (retry && success)
156           return PIGLIT_PASS;
157       else
158           return PIGLIT_FAIL;
159    }
160 }
161 
piglit_init(int argc,char ** argv)162 void piglit_init(int argc, char **argv)
163 {
164    const char * const ver_string = (const char * const)
165        glGetString( GL_VERSION );
166    float ver = strtod( ver_string, NULL );
167    GLint tex_units;
168    GLint temp[ 256 ];
169 
170    piglit_gen_ortho_projection( 0, 3*(NUM_TESTS+1), -1.5, 1.5, -1, 1, GL_FALSE );
171 
172    if ( (!piglit_is_extension_supported("GL_ARB_multitexture")
173 	 && (ver < 1.3))
174 	|| (!piglit_is_extension_supported("GL_ARB_texture_env_combine")
175 	    && !piglit_is_extension_supported("GL_EXT_texture_env_combine")
176 	    && (ver < 1.3))
177 	|| (!piglit_is_extension_supported("GL_ARB_texture_env_crossbar")
178 	    && !piglit_is_extension_supported("GL_NV_texture_env_combine4")
179 	    && (ver < 1.4)) ) {
180       printf("\nSorry, this program requires GL_ARB_multitexture and either\n"
181 	     "GL_ARB_texture_env_combine or GL_EXT_texture_env_combine (or OpenGL 1.3).\n"
182 	     "Either GL_ARB_texture_env_crossbar or GL_NV_texture_env_combine4 (or\n"
183 	     "OpenGL 1.4) are also required.\n");
184       if (!piglit_automatic)
185          printf("PIGLIT: {'result': 'fail' }\n");
186       exit(1);
187    }
188 
189    glGetIntegerv( GL_MAX_TEXTURE_UNITS, & tex_units );
190    if ( tex_units < 2 ) {
191       printf("\nSorry, this program requires at least 2 texture units.\n");
192       if (!piglit_automatic)
193          printf("PIGLIT: {'result': 'fail' }\n");
194       exit(1);
195    }
196 
197    if (piglit_automatic)
198       printf("\nAll %lu squares should be the same color.\n", (unsigned long) NUM_TESTS + 1);
199 
200    (void) memset( temp, 0x00, sizeof( temp ) );
201    glBindTexture( GL_TEXTURE_2D, 1 );
202    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
203    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
204    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
205 		 GL_RGBA, GL_UNSIGNED_BYTE, temp );
206 
207    (void) memset( temp, 0x7f, sizeof( temp ) );
208    glBindTexture( GL_TEXTURE_2D, 2 );
209    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
210    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
211    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
212 		 GL_RGBA, GL_UNSIGNED_BYTE, temp );
213 
214    (void) memset( temp, 0xff, sizeof( temp ) );
215    glBindTexture( GL_TEXTURE_2D, 3 );
216    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
217    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
218    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
219 		 GL_RGBA, GL_UNSIGNED_BYTE, temp );
220 
221    (void) memset( temp, 0x3f, sizeof( temp ) );
222    glBindTexture( GL_TEXTURE_2D, 4 );
223    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
224    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
225    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
226 		 GL_RGBA, GL_UNSIGNED_BYTE, temp );
227 
228 
229    glNewList( 1, GL_COMPILE );
230    glTranslatef(3.0, 0, 0);
231    glBegin(GL_QUADS);
232    glColor3f( 0.9, 0.0, 0.0 );
233    glMultiTexCoord2f( GL_TEXTURE0, 0.5, 0.5 );
234    glMultiTexCoord2f( GL_TEXTURE1, 0.5, 0.5 );
235    glVertex2f(-1, -1);
236    glVertex2f( 1, -1);
237    glVertex2f( 1,  1);
238    glVertex2f(-1,  1);
239    glEnd();
240    glEndList();
241 }
242