VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.c@ 68474

Last change on this file since 68474 was 63199, checked in by vboxsync, 8 years ago

GuestHost/OpenGL: warnings (gcc).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.2 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_mem.h"
8#include "state.h"
9#include "state_internals.h"
10
11/*
12 * Note: regardless of GL_NV_vertex_program, we store all per-vertex
13 * attributes in an array now, instead of specially named attributes
14 * like color, normal, texcoord, etc.
15 */
16
17
18void crStateCurrentInit( CRContext *ctx )
19{
20 CRCurrentState *c = &ctx->current;
21 CRStateBits *sb = GetCurrentBits();
22 CRCurrentBits *cb = &(sb->current);
23 static const GLfloat default_normal[4] = {0.0f, 0.0f, 1.0f, 1.0f};
24 static const GLfloat default_color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
25 static const GLfloat default_secondaryColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
26 static const GLfloat default_attrib[4] = {0.0f, 0.0f, 0.0f, 1.0f};
27 unsigned int i;
28
29 /*
30 * initialize all vertex attributes to <0,0,0,1> for starters
31 */
32 for (i = 0; i < CR_MAX_VERTEX_ATTRIBS; i++) {
33 COPY_4V(c->vertexAttrib[i], default_attrib);
34 COPY_4V(c->vertexAttribPre[i], default_attrib);
35 }
36 /* now re-do the exceptions */
37 COPY_4V(c->vertexAttrib[VERT_ATTRIB_COLOR0], default_color);
38 COPY_4V(c->vertexAttrib[VERT_ATTRIB_COLOR1], default_secondaryColor);
39 COPY_4V(c->vertexAttrib[VERT_ATTRIB_NORMAL], default_normal);
40
41 c->rasterIndex = 1.0f;
42 c->colorIndex = c->colorIndexPre = 1.0;
43 c->edgeFlag = c->edgeFlagPre = GL_TRUE;
44
45 /* Set the "pre" values and raster position attributes */
46 for (i = 0; i < CR_MAX_VERTEX_ATTRIBS; i++) {
47 COPY_4V(c->vertexAttribPre[i], c->vertexAttrib[i]);
48 COPY_4V(c->rasterAttrib[i], c->vertexAttrib[i]);
49 COPY_4V(c->rasterAttribPre[i], c->vertexAttrib[i]);
50 }
51
52 c->rasterValid = GL_TRUE;
53
54 c->inBeginEnd = GL_FALSE;
55 c->beginEndNum = 0;
56 /*c->beginEndMax = cfg->beginend_max;*/
57 c->mode = 0x10; /* Undefined Mode */
58 c->flushOnEnd = 0;
59
60 c->current = 0; /* picked up by crStateSetCurrentPointers() */
61
62 /* init dirty bits */
63 RESET(cb->dirty, ctx->bitid);
64 for (i = 0; i < CR_MAX_VERTEX_ATTRIBS; i++) {
65 RESET(cb->vertexAttrib[i], ctx->bitid);
66 }
67 RESET(cb->edgeFlag, ctx->bitid);
68 RESET(cb->colorIndex, ctx->bitid);
69 RESET(cb->rasterPos, ctx->bitid);
70}
71
72void STATE_APIENTRY crStateColor3f( GLfloat r, GLfloat g, GLfloat b )
73{
74 crStateColor4f(r, g, b, 1.0F);
75}
76
77void STATE_APIENTRY crStateColor3fv( const GLfloat *color )
78{
79 crStateColor4f( color[0], color[1], color[2], 1.0F );
80}
81
82void STATE_APIENTRY crStateColor4f( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
83{
84 CRContext *g = GetCurrentContext();
85 CRCurrentState *c = &(g->current);
86 CRStateBits *sb = GetCurrentBits();
87 CRCurrentBits *cb = &(sb->current);
88
89 FLUSH();
90
91 c->vertexAttrib[VERT_ATTRIB_COLOR0][0] = red;
92 c->vertexAttrib[VERT_ATTRIB_COLOR0][1] = green;
93 c->vertexAttrib[VERT_ATTRIB_COLOR0][2] = blue;
94 c->vertexAttrib[VERT_ATTRIB_COLOR0][3] = alpha;
95
96 DIRTY(cb->dirty, g->neg_bitid);
97 DIRTY(cb->vertexAttrib[VERT_ATTRIB_COLOR0], g->neg_bitid);
98}
99
100void STATE_APIENTRY crStateColor4fv( const GLfloat *color )
101{
102 crStateColor4f( color[0], color[1], color[2], color[3] );
103}
104
105void crStateSetCurrentPointers( CRContext *ctx, CRCurrentStatePointers *current )
106{
107 CRCurrentState *c = &(ctx->current);
108 c->current = current;
109}
110
111void crStateResetCurrentPointers( CRCurrentStatePointers *current )
112{
113 uint32_t attribsUsedMask = current->attribsUsedMask;
114
115 crMemset(current, 0, sizeof (*current));
116
117 current->attribsUsedMask = attribsUsedMask;
118}
119
120void STATE_APIENTRY crStateBegin( GLenum mode )
121{
122 CRContext *g = GetCurrentContext();
123 CRCurrentState *c = &(g->current);
124
125 if (mode > GL_POLYGON)
126 {
127 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Begin called with invalid mode: %d", mode);
128 return;
129 }
130
131 if (c->inBeginEnd)
132 {
133 crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "glBegin called inside Begin/End");
134 return;
135 }
136
137 c->attribsUsedMask = 0;
138 c->inBeginEnd = GL_TRUE;
139 c->mode = mode;
140 c->beginEndNum++;
141}
142
143void STATE_APIENTRY crStateEnd( void )
144{
145 CRContext *g = GetCurrentContext();
146 CRCurrentState *c = &(g->current);
147
148 if (!c->inBeginEnd)
149 {
150 crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "glEnd called outside Begin/End" );
151 return;
152 }
153
154 c->inBeginEnd = GL_FALSE;
155}
156
157void crStateCurrentSwitch( CRCurrentBits *c, CRbitvalue *bitID,
158 CRContext *fromCtx, CRContext *toCtx )
159{
160 const CRCurrentState *from = &(fromCtx->current);
161 const CRCurrentState *to = &(toCtx->current);
162 const GLuint maxTextureUnits = fromCtx->limits.maxTextureUnits;
163 unsigned int i, j;
164 CRbitvalue nbitID[CR_MAX_BITARRAY];
165
166 for (j=0;j<CR_MAX_BITARRAY;j++)
167 nbitID[j] = ~bitID[j];
168
169 if (CHECKDIRTY(c->rasterPos, bitID)) {
170 if (to->rasterValid) {
171 const GLfloat fromX = from->rasterAttrib[VERT_ATTRIB_POS][0];
172 const GLfloat fromY = from->rasterAttrib[VERT_ATTRIB_POS][1];
173 const GLfloat fromZ = from->rasterAttrib[VERT_ATTRIB_POS][2];
174 const GLfloat toX = to->rasterAttrib[VERT_ATTRIB_POS][0];
175 const GLfloat toY = to->rasterAttrib[VERT_ATTRIB_POS][1];
176 const GLfloat toZ = to->rasterAttrib[VERT_ATTRIB_POS][2];
177 if (toX != fromX || toY != fromY || toZ != fromZ) {
178 /* Use glWindowPos (which updates raster color) */
179 diff_api.WindowPos3fvARB(to->rasterAttrib[VERT_ATTRIB_POS]);
180 FILLDIRTY(c->rasterPos);
181 FILLDIRTY(c->dirty);
182 }
183 }
184 CLEARDIRTY(c->rasterPos, nbitID);
185 }
186
187 /* Vertex Current State Switch Code */
188
189 /* Its important that we don't do a value check here because
190 ** current may not actually have the correct values, I think...
191 ** We also need to restore the current state tracking pointer
192 ** since the packing functions will set it.
193 */
194
195 if (CHECKDIRTY(c->colorIndex, bitID)) {
196 if (to->colorIndex != from->colorIndex) {
197 diff_api.Indexf(to->colorIndex);
198 FILLDIRTY(c->colorIndex);
199 FILLDIRTY(c->dirty);
200 }
201 CLEARDIRTY(c->colorIndex, nbitID);
202 }
203
204 if (CHECKDIRTY(c->edgeFlag, bitID)) {
205 if (to->edgeFlag != from->edgeFlag) {
206 diff_api.EdgeFlag(to->edgeFlag);
207 FILLDIRTY(c->edgeFlag);
208 FILLDIRTY(c->dirty);
209 }
210 CLEARDIRTY(c->edgeFlag, nbitID);
211 }
212
213 /* If using a vertex program, update the generic vertex attributes,
214 * which may or may not be aliased with conventional attributes.
215 */
216#if defined(CR_ARB_vertex_program) || defined(CR_NV_vertex_progra)
217 if (toCtx->program.vpEnabled &&
218 (toCtx->extensions.ARB_vertex_program ||
219 (toCtx->extensions.NV_vertex_program))) {
220 const unsigned attribsUsedMask = toCtx->current.attribsUsedMask;
221 for (i = 1; i < CR_MAX_VERTEX_ATTRIBS; i++) { /* skip zero */
222 if ((attribsUsedMask & (1 << i))
223 && CHECKDIRTY(c->vertexAttrib[i], bitID)) {
224 if (COMPARE_VECTOR (from->vertexAttrib[i], to->vertexAttribPre[i])) {
225 diff_api.VertexAttrib4fvARB(i, &(to->vertexAttrib[i][0]));
226 FILLDIRTY(c->vertexAttrib[i]);
227 FILLDIRTY(c->dirty);
228 }
229 CLEARDIRTY(c->vertexAttrib[i], nbitID);
230 }
231 }
232 }
233 /* Fall-through so that attributes which don't have their bit set in the
234 * attribsUsedMask get handled via the conventional attribute functions.
235 */
236#endif
237
238 {
239 /* use conventional attribute functions */
240
241 /* NEED TO FIX THIS!!!!!! */
242 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0], bitID)) {
243 if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR0],to->vertexAttrib[VERT_ATTRIB_COLOR0])) {
244 diff_api.Color4fv ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_COLOR0]));
245 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0]);
246 FILLDIRTY(c->dirty);
247 }
248 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0], nbitID);
249 }
250
251 /* NEED TO FIX THIS, ALSO?!!!!! */
252#ifdef CR_EXT_secondary_color
253 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1], bitID)) {
254 if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR1],to->vertexAttrib[VERT_ATTRIB_COLOR1])) {
255 diff_api.SecondaryColor3fvEXT ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_COLOR1]));
256 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1]);
257 FILLDIRTY(c->dirty);
258 }
259 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1], nbitID);
260 }
261#endif
262
263 /* NEED TO FIX THIS, ALSO?!!!!! */
264#ifdef CR_EXT_fog_coord
265 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG], bitID)) {
266 if (from->vertexAttrib[VERT_ATTRIB_FOG][0] != to->vertexAttrib[VERT_ATTRIB_FOG][0] ) {
267 diff_api.FogCoordfvEXT ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_FOG][0] ));
268 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG]);
269 FILLDIRTY(c->dirty);
270 }
271 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG], nbitID);
272 }
273#endif
274
275 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL], bitID)) {
276 if (COMPARE_VECTOR (from->vertexAttrib[VERT_ATTRIB_NORMAL], to->vertexAttrib[VERT_ATTRIB_NORMAL])) {
277 diff_api.Normal3fv ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_NORMAL][0]));
278 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL]);
279 FILLDIRTY(c->dirty);
280 }
281 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL], nbitID);
282 }
283
284 for (i = 0; i < maxTextureUnits; i++) {
285 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i], bitID)) {
286 if (COMPARE_TEXCOORD (from->vertexAttrib[VERT_ATTRIB_TEX0 + i], to->vertexAttribPre[VERT_ATTRIB_TEX0 + i])) {
287 diff_api.MultiTexCoord4fvARB (i+GL_TEXTURE0_ARB, (GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_TEX0+ i][0]));
288 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i]);
289 FILLDIRTY(c->dirty);
290 }
291 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i], nbitID);
292 }
293 }
294 }
295
296 CLEARDIRTY(c->dirty, nbitID);
297}
298
299void
300crStateCurrentDiff( CRCurrentBits *c, CRbitvalue *bitID,
301 CRContext *fromCtx, CRContext *toCtx )
302{
303 CRCurrentState *from = &(fromCtx->current);
304 const CRCurrentState *to = &(toCtx->current);
305 unsigned int i, j;
306 CRbitvalue nbitID[CR_MAX_BITARRAY];
307
308 for (j=0;j<CR_MAX_BITARRAY;j++)
309 nbitID[j] = ~bitID[j];
310
311 if (CHECKDIRTY(c->rasterPos, bitID)) {
312 from->rasterValid = to->rasterValid;
313 if (to->rasterValid) {
314 const GLfloat fromX = from->rasterAttrib[VERT_ATTRIB_POS][0];
315 const GLfloat fromY = from->rasterAttrib[VERT_ATTRIB_POS][1];
316 const GLfloat fromZ = from->rasterAttrib[VERT_ATTRIB_POS][2];
317 const GLfloat toX = to->rasterAttrib[VERT_ATTRIB_POS][0];
318 const GLfloat toY = to->rasterAttrib[VERT_ATTRIB_POS][1];
319 const GLfloat toZ = to->rasterAttrib[VERT_ATTRIB_POS][2];
320 if (toX != fromX || toY != fromY || toZ != fromZ) {
321 /* Use glWindowPos (which updates raster color) */
322 diff_api.WindowPos3fvARB(to->rasterAttrib[VERT_ATTRIB_POS]);
323 from->rasterAttrib[VERT_ATTRIB_POS][0] = toX;
324 from->rasterAttrib[VERT_ATTRIB_POS][1] = toY;
325 from->rasterAttrib[VERT_ATTRIB_POS][2] = toZ;
326 }
327 }
328 CLEARDIRTY(c->rasterPos, nbitID);
329 }
330
331 /* Vertex Current State Sync Code */
332 /* Some things to note here:
333 ** 1) Compare is done against the pre value since the
334 ** current value includes the geometry info.
335 ** 2) Update is done with the current value since
336 ** the server will be getting the geometry block
337 ** 3) Copy is done outside of the compare to ensure
338 ** that it happens.
339 */
340
341 /* edge flag */
342 if (CHECKDIRTY(c->edgeFlag, bitID)) {
343 if (from->edgeFlag != to->edgeFlagPre) {
344 diff_api.EdgeFlag (to->edgeFlagPre);
345 }
346 from->edgeFlag = to->edgeFlag;
347 CLEARDIRTY(c->edgeFlag, nbitID);
348 }
349
350 /* color index */
351 if (CHECKDIRTY(c->colorIndex, bitID)) {
352 if (from->colorIndex != to->colorIndexPre) {
353 diff_api.Indexf (to->colorIndex);
354 }
355 from->colorIndex = to->colorIndex;
356 CLEARDIRTY(c->colorIndex, nbitID);
357 }
358
359
360 /* If using a vertex program, update the generic vertex attributes,
361 * which may or may not be aliased with conventional attributes.
362 */
363#if defined(CR_ARB_vertex_program) || defined(CR_NV_vertex_progra)
364 if (toCtx->program.vpEnabled &&
365 (toCtx->extensions.ARB_vertex_program ||
366 (toCtx->extensions.NV_vertex_program))) {
367 const unsigned attribsUsedMask = toCtx->current.attribsUsedMask;
368 for (i = 1; i < CR_MAX_VERTEX_ATTRIBS; i++) { /* skip zero */
369 if ((attribsUsedMask & (1 << i))
370 && CHECKDIRTY(c->vertexAttrib[i], bitID)) {
371 if (COMPARE_VECTOR (from->vertexAttrib[i], to->vertexAttribPre[i])) {
372 diff_api.VertexAttrib4fvARB(i, &(to->vertexAttribPre[i][0]));
373 }
374 COPY_4V(from->vertexAttrib[i] , to->vertexAttrib[i]);
375 CLEARDIRTY(c->vertexAttrib[i], nbitID);
376 }
377 }
378 }
379 /* Fall-through so that attributes which don't have their bit set in the
380 * attribsUsedMask get handled via the conventional attribute functions.
381 */
382#endif
383
384 {
385 /* use conventional attribute functions */
386 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0], bitID)) {
387 if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR0],to->vertexAttribPre[VERT_ATTRIB_COLOR0])) {
388 diff_api.Color4fv ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_COLOR0]));
389 }
390 COPY_4V(from->vertexAttrib[VERT_ATTRIB_COLOR0] , to->vertexAttrib[VERT_ATTRIB_COLOR0]);
391 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0], nbitID);
392 }
393
394#ifdef CR_EXT_secondary_color
395 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1], bitID)) {
396 if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR1],to->vertexAttribPre[VERT_ATTRIB_COLOR1])) {
397 diff_api.SecondaryColor3fvEXT ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_COLOR1]));
398 }
399 COPY_4V(from->vertexAttrib[VERT_ATTRIB_COLOR1] , to->vertexAttrib[VERT_ATTRIB_COLOR1]);
400 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1], nbitID);
401 }
402#endif
403
404#ifdef CR_EXT_fog_coord
405 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG], bitID)) {
406 if (from->vertexAttrib[VERT_ATTRIB_FOG] != to->vertexAttribPre[VERT_ATTRIB_FOG]) {
407 diff_api.FogCoordfvEXT ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_FOG]));
408 }
409 COPY_4V(from->vertexAttrib[VERT_ATTRIB_FOG] , to->vertexAttrib[VERT_ATTRIB_FOG]);
410 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG], nbitID);
411 }
412#endif
413
414 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL], bitID)) {
415 if (COMPARE_VECTOR (from->vertexAttrib[VERT_ATTRIB_NORMAL], to->vertexAttribPre[VERT_ATTRIB_NORMAL])) {
416 diff_api.Normal3fv ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_NORMAL]));
417 }
418 COPY_4V(from->vertexAttrib[VERT_ATTRIB_NORMAL] , to->vertexAttrib[VERT_ATTRIB_NORMAL]);
419 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL], nbitID);
420 }
421
422 for ( i = 0 ; i < fromCtx->limits.maxTextureUnits ; i++)
423 {
424 if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i], bitID)) {
425 if (COMPARE_TEXCOORD (from->vertexAttrib[VERT_ATTRIB_TEX0 + i], to->vertexAttribPre[VERT_ATTRIB_TEX0 + i])) {
426 diff_api.MultiTexCoord4fvARB (GL_TEXTURE0_ARB + i, (GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_TEX0 + i]));
427 }
428 COPY_4V(from->vertexAttrib[VERT_ATTRIB_TEX0 + i] , to->vertexAttrib[VERT_ATTRIB_TEX0 + i]);
429 CLEARDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i], nbitID);
430 }
431 }
432 }
433
434 CLEARDIRTY(c->dirty, nbitID);
435}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette