Revert "wglgears: show stereo-option in usage"
[mesa-demos.git] / src / perf / glslstateschange.c
blob7422b7822eef78720b9acd1abaf974af5492ea04
1 /**
2 * Test states change when using shaders & textures.
4 * Copyright (C) 2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2011 Red Hat All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <assert.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <GL/glew.h>
30 #include "glut_wrap.h"
31 #include "readtex.h"
32 #include "shaderutil.h"
33 #include "glmain.h"
34 #include "common.h"
36 static const char *VertFile1 = "glslstateschange1.vert";
37 static const char *FragFile1 = "glslstateschange1.frag";
38 static const char *VertFile2 = "glslstateschange2.vert";
39 static const char *FragFile2 = "glslstateschange2.frag";
40 static struct uniform_info Uniforms1[] = {
41 { "tex1", 1, GL_SAMPLER_2D, { 0, 0, 0, 0 }, -1 },
42 { "tex2", 1, GL_SAMPLER_2D, { 1, 0, 0, 0 }, -1 },
43 { "UniV1", 1, GL_FLOAT_VEC4, { 0.8, 0.2, 0.2, 0 }, -1 },
44 { "UniV2", 1, GL_FLOAT_VEC4, { 0.6, 0.6, 0.6, 0 }, -1 },
45 END_OF_UNIFORMS
47 static struct uniform_info Uniforms2[] = {
48 { "tex1", 1, GL_SAMPLER_2D, { 0, 0, 0, 0 }, -1 },
49 { "tex2", 1, GL_SAMPLER_2D, { 1, 0, 0, 0 }, -1 },
50 { "UniV1", 1, GL_FLOAT_VEC4, { 0.8, 0.2, 0.2, 0 }, -1 },
51 { "UniV2", 1, GL_FLOAT_VEC4, { 0.6, 0.6, 0.6, 0 }, -1 },
52 END_OF_UNIFORMS
54 static GLuint Program1;
55 static GLuint Program2;
56 static GLint P1VertCoord_attr = -1;
57 static GLint P1TexCoord0_attr = -1, P1TexCoord1_attr = -1;
58 static GLint P2VertCoord_attr = -1;
59 static GLint P2TexCoord0_attr = -1, P2TexCoord1_attr = -1;
61 static const char *TexFiles[4] =
63 DEMOS_DATA_DIR "tile.rgb",
64 DEMOS_DATA_DIR "tree2.rgba",
65 DEMOS_DATA_DIR "tile.rgb",
66 DEMOS_DATA_DIR "tree2.rgba"
68 GLuint texObj[4];
71 int WinWidth = 500, WinHeight = 500;
73 static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
74 static GLfloat EyeDist = 10;
78 static const GLfloat Tex0Coords[4][2] = {
79 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
82 static const GLfloat Tex1Coords[4][2] = {
83 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
86 static const GLfloat VertCoords[4][2] = {
87 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
90 static void
91 DrawPolygonArray(GLint VertCoord_attr,
92 GLint TexCoord0_attr,
93 GLint TexCoord1_attr)
95 void *vertPtr, *tex0Ptr, *tex1Ptr;
97 vertPtr = VertCoords;
98 tex0Ptr = Tex0Coords;
99 tex1Ptr = Tex1Coords;
101 if (VertCoord_attr >= 0) {
102 glVertexAttribPointer(VertCoord_attr, 2, GL_FLOAT, GL_FALSE, 0, vertPtr);
103 glEnableVertexAttribArray(VertCoord_attr);
104 } else {
105 glVertexPointer(2, GL_FLOAT, 0, vertPtr);
106 glEnableClientState(GL_VERTEX_ARRAY);
109 glVertexAttribPointer(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE, 0, tex0Ptr);
110 glEnableVertexAttribArray(TexCoord0_attr);
112 glVertexAttribPointer(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE, 0, tex1Ptr);
113 glEnableVertexAttribArray(TexCoord1_attr);
115 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
118 static void
119 Draw(unsigned count)
121 unsigned i;
123 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124 for (i = 0; i < count; i++) {
125 Yrot = 0.05 * i;
126 glPushMatrix(); /* modelview matrix */
127 glTranslatef(0.0, 0.0, -EyeDist);
128 glRotatef(Zrot, 0, 0, 1);
129 glRotatef(Yrot, 0, 1, 0);
130 glRotatef(Xrot, 1, 0, 0);
132 glUseProgram(Program1);
133 glActiveTexture(GL_TEXTURE0 + 0);
134 glBindTexture(GL_TEXTURE_2D, texObj[0]);
135 glActiveTexture(GL_TEXTURE0 + 1);
136 glBindTexture(GL_TEXTURE_2D, texObj[1]);
137 glUniform4fARB(Uniforms1[2].location, Xrot, Yrot, Zrot, 1.000000);
138 glUniform4fARB(Uniforms1[3].location, Xrot, Yrot, Zrot, 1.000000);
139 DrawPolygonArray(P1VertCoord_attr, P1TexCoord0_attr, P1TexCoord1_attr);
141 glUseProgram(Program2);
142 glActiveTexture(GL_TEXTURE0 + 0);
143 glBindTexture(GL_TEXTURE_2D, texObj[2]);
144 glActiveTexture(GL_TEXTURE0 + 1);
145 glBindTexture(GL_TEXTURE_2D, texObj[3]);
146 glUniform4fARB(Uniforms2[2].location, Xrot, Yrot, Zrot, 1.000000);
147 glUniform4fARB(Uniforms2[3].location, Xrot, Yrot, Zrot, 1.000000);
148 DrawPolygonArray(P2VertCoord_attr, P2TexCoord0_attr, P2TexCoord1_attr);
150 glPopMatrix();
153 glutSwapBuffers();
156 void
157 PerfDraw(void)
159 double rate;
161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 perf_printf("GLSL texture/program change rate\n");
165 rate = PerfMeasureRate(Draw);
166 perf_printf(" Immediate mode: %s change/sec\n", PerfHumanFloat(rate));
168 exit(0);
171 void
172 PerfNextRound(void)
176 static void
177 InitTextures(void)
179 GLenum filter = GL_LINEAR;
180 int i;
182 /* allocate 4 texture objects */
183 glGenTextures(4, texObj);
185 for (i = 0; i < 4; i++) {
186 GLint imgWidth, imgHeight;
187 GLenum imgFormat;
188 GLubyte *image = NULL;
190 image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat);
191 if (!image) {
192 printf("Couldn't read %s\n", TexFiles[i]);
193 exit(0);
196 glActiveTexture(GL_TEXTURE0 + i);
197 glBindTexture(GL_TEXTURE_2D, texObj[i]);
198 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight,
199 imgFormat, GL_UNSIGNED_BYTE, image);
200 free(image);
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
205 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
209 static GLuint
210 CreateAProgram(const char *vertProgFile, const char *fragProgFile,
211 struct uniform_info *uniforms, GLint *VertCoord_attr,
212 GLint *TexCoord0_attr, GLint *TexCoord1_attr)
214 GLuint fragShader, vertShader, program;
216 vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
217 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
218 assert(vertShader);
219 program = LinkShaders(vertShader, fragShader);
221 glUseProgram(program);
223 SetUniformValues(program, uniforms);
225 assert(ValidateShaderProgram(program));
227 *VertCoord_attr = glGetAttribLocation(program, "VertCoord");
228 if (*VertCoord_attr > 0) {
229 /* We want the VertCoord attrib to have position zero so that
230 * the call to glVertexAttrib(0, xyz) triggers vertex processing.
231 * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have
232 * to set that attribute last (which is a PITA to manage).
234 glBindAttribLocation(program, 0, "VertCoord");
235 /* re-link */
236 glLinkProgram(program);
237 /* VertCoord_attr should be zero now */
238 *VertCoord_attr = glGetAttribLocation(program, "VertCoord");
239 assert(*VertCoord_attr == 0);
242 *TexCoord0_attr = glGetAttribLocation(program, "TexCoord0");
243 *TexCoord1_attr = glGetAttribLocation(program, "TexCoord1");
245 return program;
248 static void
249 InitPrograms(void)
251 Program1 = CreateAProgram(VertFile1, FragFile1, Uniforms1,
252 &P1VertCoord_attr,
253 &P1TexCoord0_attr, &P1TexCoord1_attr);
254 Program2 = CreateAProgram(VertFile2, FragFile2, Uniforms2,
255 &P2VertCoord_attr,
256 &P2TexCoord0_attr, &P2TexCoord1_attr);
259 void
260 PerfInit(void)
262 if (!ShadersSupported())
263 exit(1);
265 InitTextures();
266 InitPrograms();
268 glEnable(GL_DEPTH_TEST);
270 glClearColor(.6, .6, .9, 0);
271 glColor3f(1.0, 1.0, 1.0);