1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
// BEGIN_COPYRIGHT -*- glean -*-
//
// Copyright (C) 2009 VMware, Inc. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL VMWARE BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// END_COPYRIGHT
// Test GL_NV_texture_env_combine4
// Generate some random combiner state and colors, compute the expected
// color, then render with the combiner state and compare the results.
// Only one texture unit is tested and not all possible combiner terms
// are exercised.
//
// Brian Paul
// 23 Jan 2009
#define GL_GLEXT_PROTOTYPES
#include <cassert>
#include <cmath>
#include <cstring>
#include "ttexcombine4.h"
namespace GLEAN {
TexCombine4Result::TexCombine4Result()
{
pass = true;
}
// generate random combiner state
void
TexCombine4Test::generate_state(struct combine_state &state)
{
int i;
if (rand.next() > 0.5f)
state.CombineMode = GL_ADD;
else
state.CombineMode = GL_ADD_SIGNED_EXT;
for (i = 0; i < 4; i++) {
int src = int(rand.next() * 4.0);
switch (src) {
case 0:
state.Source[i] = GL_ZERO;
break;
case 1:
state.Source[i] = GL_TEXTURE;
break;
case 2:
state.Source[i] = GL_CONSTANT_EXT;
break;
default:
state.Source[i] = GL_PRIMARY_COLOR_EXT;
break;
}
if (rand.next() > 0.5f) {
state.OperandRGB[i] = GL_SRC_COLOR;
state.OperandA[i] = GL_SRC_ALPHA;
}
else {
state.OperandRGB[i] = GL_ONE_MINUS_SRC_COLOR;
state.OperandA[i] = GL_ONE_MINUS_SRC_ALPHA;
}
}
state.PrimaryColor[0] = rand.next();
state.PrimaryColor[1] = rand.next();
state.PrimaryColor[2] = rand.next();
state.PrimaryColor[3] = rand.next();
state.ConstantColor[0] = rand.next();
state.ConstantColor[1] = rand.next();
state.ConstantColor[2] = rand.next();
state.ConstantColor[3] = rand.next();
state.TextureColor[0] = rand.next();
state.TextureColor[1] = rand.next();
state.TextureColor[2] = rand.next();
state.TextureColor[3] = rand.next();
}
// compute expected final color
void
TexCombine4Test::evaluate_state(const struct combine_state &state,
GLfloat result[4])
{
GLfloat arg[4][4];
int i;
// setup terms
for (i = 0; i < 4; i++) {
switch (state.Source[i]) {
case GL_ZERO:
arg[i][0] = arg[i][1] = arg[i][2] = arg[i][3] = 0.0f;
break;
case GL_PRIMARY_COLOR_EXT:
arg[i][0] = state.PrimaryColor[0];
arg[i][1] = state.PrimaryColor[1];
arg[i][2] = state.PrimaryColor[2];
arg[i][3] = state.PrimaryColor[3];
break;
case GL_CONSTANT_EXT:
arg[i][0] = state.ConstantColor[0];
arg[i][1] = state.ConstantColor[1];
arg[i][2] = state.ConstantColor[2];
arg[i][3] = state.ConstantColor[3];
break;
case GL_TEXTURE:
arg[i][0] = state.TextureColor[0];
arg[i][1] = state.TextureColor[1];
arg[i][2] = state.TextureColor[2];
arg[i][3] = state.TextureColor[3];
break;
default:
assert(0);
}
switch (state.OperandRGB[i]) {
case GL_SRC_COLOR:
// nop
break;
case GL_ONE_MINUS_SRC_COLOR:
arg[i][0] = 1.0f - arg[i][0];
arg[i][1] = 1.0f - arg[i][1];
arg[i][2] = 1.0f - arg[i][2];
arg[i][3] = 1.0f - arg[i][3];
break;
default:
assert(0);
}
}
// combine terms, loop over color channels
for (i = 0; i < 4; i++) {
result[i] = arg[0][i] * arg[1][i] + arg[2][i] * arg[3][i];
if (state.CombineMode == GL_ADD_SIGNED_EXT)
result[i] -= 0.5f;
if (result[i] < 0.0f)
result[i] = 0.0f;
else if (result[i] > 1.0f)
result[i] = 1.0f;
}
}
// render quad with given combiner state and return resulting color
// return false if GL error is detected, true otherwise.
bool
TexCombine4Test::render_state(const struct combine_state &state,
GLfloat result[4])
{
if (glGetError()) {
reportError("GL error detected before setting combiner state.");
return false;
}
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, state.CombineMode);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, state.CombineMode);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, state.Source[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, state.Source[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, state.Source[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, state.Source[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, state.Source[2]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, state.Source[2]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE3_RGB_NV, state.Source[3]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE3_ALPHA_NV, state.Source[3]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, state.OperandRGB[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, state.OperandA[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, state.OperandRGB[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, state.OperandA[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, state.OperandRGB[2]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, state.OperandA[2]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND3_RGB_NV, state.OperandRGB[3]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND3_ALPHA_NV, state.OperandA[3]);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, state.ConstantColor);
if (glGetError()) {
reportError("GL error generated by combiner state.");
return false;
}
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
GL_RGBA, GL_FLOAT, state.TextureColor);
glColor4fv(state.PrimaryColor);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0);
glVertex2f(-1.0, -1.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(1.0, -1.0);
glTexCoord2f(1.0, 1.0);
glVertex2f( 1.0, 1.0);
glTexCoord2f(0.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
glReadPixels(WINDOW_SIZE / 2, WINDOW_SIZE / 2, 1, 1,
GL_RGBA, GL_FLOAT, result);
return true;
}
void
TexCombine4Test::report_state(const struct combine_state &state)
{
env->log << "\tCurrent GL state:\n";
if (state.CombineMode == GL_ADD)
env->log << "\t\tCOMBINE = GL_ADD\n";
else
env->log << "\t\tCOMBINE = GL_ADD_SIGNED_EXT\n";
for (int i = 0; i < 4; i++) {
env->log << "\t\t" << "SOURCE" << i << ": ";
switch (state.Source[i]) {
case GL_ZERO:
env->log << "GL_ZERO\n";
break;
case GL_TEXTURE:
env->log << "GL_TEXTURE\n";
break;
case GL_CONSTANT_EXT:
env->log << "GL_CONSTANT\n";
break;
case GL_PRIMARY_COLOR_EXT:
env->log << "GL_PRIMARY_COLOR\n";
break;
default:
assert(0);
}
env->log << "\t\t" << "OPERAND" << i << "_RGB: ";
switch (state.OperandRGB[i]) {
case GL_SRC_COLOR:
env->log << "GL_SRC_COLOR\n";
break;
case GL_ONE_MINUS_SRC_COLOR:
env->log << "GL_ONE_MINUS_SRC_COLOR\n";
break;
default:
assert(0);
}
env->log << "\t\t" << "OPERAND" << i << "_ALPHA: ";
switch (state.OperandRGB[i]) {
case GL_SRC_COLOR:
env->log << "GL_SRC_ALPHA\n";
break;
case GL_ONE_MINUS_SRC_COLOR:
env->log << "GL_ONE_MINUS_SRC_ALPHA\n";
break;
default:
assert(0);
}
}
char str[100];
sprintf(str, "%.3f, %.3f, %.3f, %.3f",
state.PrimaryColor[0], state.PrimaryColor[1],
state.PrimaryColor[2], state.PrimaryColor[3]);
env->log << "\t\tPrimary Color: " << str << "\n";
sprintf(str, "%.3f, %.3f, %.3f, %.3f",
state.ConstantColor[0], state.ConstantColor[1],
state.ConstantColor[2], state.ConstantColor[3]);
env->log << "\t\tConstant Color: " << str << "\n";
sprintf(str, "%.3f, %.3f, %.3f, %.3f",
state.TextureColor[0], state.TextureColor[1],
state.TextureColor[2], state.TextureColor[3]);
env->log << "\t\tTexture Color: " << str << "\n";
}
void
TexCombine4Test::reportError(const char *msg)
{
env->log << name << ": Error: " << msg << "\n";
}
void
TexCombine4Test::runOne(TexCombine4Result &r, Window &w)
{
(void) w; // silence warning
rand = RandomDouble(42); // init random number generator
const float err = 0.05; // xxx compute something better
for (int i = 0; i < NUM_TESTS; i++) {
combine_state state;
GLfloat expected[4], actual[4];
//env->log << "\t iteration " << i << "\n";
generate_state(state);
evaluate_state(state, expected);
if (!render_state(state, actual)) {
r.pass = false;
return;
}
if (fabs(expected[0] - actual[0]) > err ||
fabs(expected[1] - actual[1]) > err ||
fabs(expected[2] - actual[2]) > err) {
char str[100];
env->log << name << ": Error: GL_NV_texure_env_combine4 failed\n";
report_state(state);
env->log << "\tResults:\n";
sprintf(str, "%.3f, %.3f, %.3f, %.3f",
expected[0], expected[1],
expected[2], expected[3]);
env->log << "\t\tExpected color: " << str << "\n";
sprintf(str, "%.3f, %.3f, %.3f, %.3f",
actual[0], actual[1],
actual[2], actual[3]);
env->log << "\t\tRendered color: " << str << "\n";
r.pass = false;
return;
}
}
r.pass = true;
}
void
TexCombine4Test::logOne(TexCombine4Result &r)
{
logPassFail(r);
logConcise(r);
}
void
TexCombine4Result::putresults(ostream &s) const
{
if (pass) {
s << "PASS\n";
}
else {
s << "FAIL\n";
}
}
bool
TexCombine4Result::getresults(istream &s)
{
char result[1000];
s >> result;
if (strcmp(result, "FAIL") == 0) {
pass = false;
}
else {
pass = true;
}
return s.good();
}
// The test object itself:
TexCombine4Test texCombine4Test("texCombine4", "window, rgb",
"GL_NV_texture_env_combine4, GL_EXT_texture_env_combine",
"Test the GL_NV_texture_env_combine4 extension.\n");
} // namespace GLEAN
|