Switch r300_state.c to use r300_fixed_pipelines.h instead of r300_lib.h.
[profile/ivi/mesa.git] / src / mesa / drivers / dri / r300 / r300_state.c
1 /*
2 Copyright (C) The Weather Channel, Inc.  2002.
3 Copyright (C) 2004 Nicolai Haehnle.
4 All Rights Reserved.
5
6 The Weather Channel (TM) funded Tungsten Graphics to develop the
7 initial release of the Radeon 8500 driver under the XFree86 license.
8 This notice must be preserved.
9
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial
20 portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 /*
33  * Authors:
34  *   Nicolai Haehnle <prefect_@gmx.net>
35  */
36
37 #include "glheader.h"
38 #include "state.h"
39 #include "imports.h"
40 #include "enums.h"
41 #include "macros.h"
42 #include "context.h"
43 #include "dd.h"
44 #include "simple_list.h"
45
46 #include "api_arrayelt.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
49 #include "array_cache/acache.h"
50 #include "tnl/tnl.h"
51
52 #include "radeon_ioctl.h"
53 #include "radeon_state.h"
54 #include "r300_context.h"
55 #include "r300_ioctl.h"
56 #include "r300_state.h"
57 #include "r300_reg.h"
58 #include "r300_program.h"
59 #include "r300_fixed_pipelines.h"
60
61 static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
62 {
63         r300ContextPtr rmesa = R300_CONTEXT(ctx);
64         int pp_misc = rmesa->hw.at.cmd[R300_AT_ALPHA_TEST];
65         GLubyte refByte;
66
67         CLAMPED_FLOAT_TO_UBYTE(refByte, ref);
68
69         R300_STATECHANGE(rmesa, at);
70
71         pp_misc &= ~(R300_ALPHA_TEST_OP_MASK | R300_REF_ALPHA_MASK);
72         pp_misc |= (refByte & R300_REF_ALPHA_MASK);
73
74         switch (func) {
75         case GL_NEVER:
76                 pp_misc |= R300_ALPHA_TEST_FAIL;
77                 break;
78         case GL_LESS:
79                 pp_misc |= R300_ALPHA_TEST_LESS;
80                 break;
81         case GL_EQUAL:
82                 pp_misc |= R300_ALPHA_TEST_EQUAL;
83                 break;
84         case GL_LEQUAL:
85                 pp_misc |= R300_ALPHA_TEST_LEQUAL;
86                 break;
87         case GL_GREATER:
88                 pp_misc |= R300_ALPHA_TEST_GREATER;
89                 break;
90         case GL_NOTEQUAL:
91                 pp_misc |= R300_ALPHA_TEST_NEQUAL;
92                 break;
93         case GL_GEQUAL:
94                 pp_misc |= R300_ALPHA_TEST_GEQUAL;
95                 break;
96         case GL_ALWAYS:
97                 pp_misc |= R300_ALPHA_TEST_PASS;
98                 break;
99         }
100
101         rmesa->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;
102 }
103
104 static void r300BlendColor(GLcontext * ctx, const GLfloat cf[4])
105 {
106         GLubyte color[4];
107         r300ContextPtr rmesa = R300_CONTEXT(ctx);
108         fprintf(stderr, "%s:%s is not implemented yet. Fixme !\n", __FILE__, __FUNCTION__);
109         #if 0
110         R200_STATECHANGE(rmesa, ctx);
111         CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]);
112         CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]);
113         CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]);
114         CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]);
115         if (rmesa->radeon.radeonScreen->drmSupportsBlendColor)
116                 rmesa->hw.ctx.cmd[CTX_RB3D_BLENDCOLOR] =
117                     radeonPackColor(4, color[0], color[1], color[2], color[3]);
118         #endif
119 }
120
121 /**
122  * Calculate the hardware blend factor setting.  This same function is used
123  * for source and destination of both alpha and RGB.
124  *
125  * \returns
126  * The hardware register value for the specified blend factor.  This value
127  * will need to be shifted into the correct position for either source or
128  * destination factor.
129  *
130  * \todo
131  * Since the two cases where source and destination are handled differently
132  * are essentially error cases, they should never happen.  Determine if these
133  * cases can be removed.
134  */
135 static int blend_factor(GLenum factor, GLboolean is_src)
136 {
137         int func;
138
139         switch (factor) {
140         case GL_ZERO:
141                 func = R200_BLEND_GL_ZERO;
142                 break;
143         case GL_ONE:
144                 func = R200_BLEND_GL_ONE;
145                 break;
146         case GL_DST_COLOR:
147                 func = R200_BLEND_GL_DST_COLOR;
148                 break;
149         case GL_ONE_MINUS_DST_COLOR:
150                 func = R200_BLEND_GL_ONE_MINUS_DST_COLOR;
151                 break;
152         case GL_SRC_COLOR:
153                 func = R200_BLEND_GL_SRC_COLOR;
154                 break;
155         case GL_ONE_MINUS_SRC_COLOR:
156                 func = R200_BLEND_GL_ONE_MINUS_SRC_COLOR;
157                 break;
158         case GL_SRC_ALPHA:
159                 func = R200_BLEND_GL_SRC_ALPHA;
160                 break;
161         case GL_ONE_MINUS_SRC_ALPHA:
162                 func = R200_BLEND_GL_ONE_MINUS_SRC_ALPHA;
163                 break;
164         case GL_DST_ALPHA:
165                 func = R200_BLEND_GL_DST_ALPHA;
166                 break;
167         case GL_ONE_MINUS_DST_ALPHA:
168                 func = R200_BLEND_GL_ONE_MINUS_DST_ALPHA;
169                 break;
170         case GL_SRC_ALPHA_SATURATE:
171                 func =
172                     (is_src) ? R200_BLEND_GL_SRC_ALPHA_SATURATE :
173                     R200_BLEND_GL_ZERO;
174                 break;
175         case GL_CONSTANT_COLOR:
176                 func = R200_BLEND_GL_CONST_COLOR;
177                 break;
178         case GL_ONE_MINUS_CONSTANT_COLOR:
179                 func = R200_BLEND_GL_ONE_MINUS_CONST_COLOR;
180                 break;
181         case GL_CONSTANT_ALPHA:
182                 func = R200_BLEND_GL_CONST_ALPHA;
183                 break;
184         case GL_ONE_MINUS_CONSTANT_ALPHA:
185                 func = R200_BLEND_GL_ONE_MINUS_CONST_ALPHA;
186                 break;
187         default:
188                 func = (is_src) ? R200_BLEND_GL_ONE : R200_BLEND_GL_ZERO;
189         }
190         return func;
191 }
192
193 /**
194  * Sets both the blend equation and the blend function.
195  * This is done in a single
196  * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX)
197  * change the interpretation of the blend function.
198  * Also, make sure that blend function and blend equation are set to their default
199  * value if color blending is not enabled, since at least blend equations GL_MIN
200  * and GL_FUNC_REVERSE_SUBTRACT will cause wrong results otherwise for
201  * unknown reasons.
202  */
203 static void r300_set_blend_state(GLcontext * ctx)
204 {
205         r300ContextPtr rmesa = R300_CONTEXT(ctx);
206         #if 0
207         GLuint cntl = rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] &
208             ~(R300_ROP_ENABLE | R300_ALPHA_BLEND_ENABLE |
209               R300_SEPARATE_ALPHA_ENABLE);
210         #endif
211
212         int func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
213             (R200_BLEND_GL_ZERO << R200_DST_BLEND_SHIFT);
214         int eqn = R200_COMB_FCN_ADD_CLAMP;
215         int funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
216             (R200_BLEND_GL_ZERO << R200_DST_BLEND_SHIFT);
217         int eqnA = R200_COMB_FCN_ADD_CLAMP;
218
219         R300_STATECHANGE(rmesa, bld);
220
221         if (rmesa->radeon.radeonScreen->drmSupportsBlendColor) {
222                 if (ctx->Color._LogicOpEnabled) {
223                         #if 0
224                         rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] =
225                             cntl | R300_ROP_ENABLE;
226                         #endif
227                         rmesa->hw.bld.cmd[R300_BLD_ABLEND] = eqn | func;
228                         rmesa->hw.bld.cmd[R300_BLD_CBLEND] = eqn | func;
229                         return;
230                 } else if (ctx->Color.BlendEnabled) {
231                         #if 0
232                         rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] =
233                             cntl | R300_ALPHA_BLEND_ENABLE |
234                             R300_SEPARATE_ALPHA_ENABLE;
235                         #endif
236                 } else {
237                         #if 0
238                         rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] = cntl;
239                         #endif
240                         rmesa->hw.bld.cmd[R300_BLD_ABLEND] = eqn | func;
241                         rmesa->hw.bld.cmd[R300_BLD_CBLEND] = eqn | func;
242                         return;
243                 }
244         } else {
245                 if (ctx->Color._LogicOpEnabled) {
246                         #if 0
247                         rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] =
248                             cntl | R300_ROP_ENABLE;
249                         rmesa->hw.ctx.cmd[CTX_RB3D_BLENDCNTL] = eqn | func;
250                         #endif
251                         return;
252                 } else if (ctx->Color.BlendEnabled) {
253                         #if 0
254                         rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] =
255                             cntl | R300_ALPHA_BLEND_ENABLE;
256                         #endif
257                 } else {
258                         #if 0
259                         rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] = cntl;
260                         rmesa->hw.ctx.cmd[CTX_RB3D_BLENDCNTL] = eqn | func;
261                         #endif
262                         return;
263                 }
264         }
265
266         func =
267             (blend_factor(ctx->Color.BlendSrcRGB, GL_TRUE) <<
268              R200_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstRGB,
269                                                    GL_FALSE) <<
270                                       R200_DST_BLEND_SHIFT);
271
272         switch (ctx->Color.BlendEquationRGB) {
273         case GL_FUNC_ADD:
274                 eqn = R300_COMB_FCN_ADD_CLAMP;
275                 break;
276
277         case GL_FUNC_SUBTRACT:
278                 eqn = R300_COMB_FCN_SUB_CLAMP;
279                 break;
280
281         case GL_FUNC_REVERSE_SUBTRACT:
282                 eqn = R200_COMB_FCN_RSUB_CLAMP;
283                 break;
284
285         case GL_MIN:
286                 eqn = R200_COMB_FCN_MIN;
287                 func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
288                     (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
289                 break;
290
291         case GL_MAX:
292                 eqn = R200_COMB_FCN_MAX;
293                 func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
294                     (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
295                 break;
296
297         default:
298                 fprintf(stderr,
299                         "[%s:%u] Invalid RGB blend equation (0x%04x).\n",
300                         __func__, __LINE__, ctx->Color.BlendEquationRGB);
301                 return;
302         }
303
304         if (!rmesa->radeon.radeonScreen->drmSupportsBlendColor) {
305                 #if 0
306                 rmesa->hw.ctx.cmd[CTX_RB3D_BLENDCNTL] = eqn | func;
307                 #endif
308                 return;
309         }
310
311         funcA =
312             (blend_factor(ctx->Color.BlendSrcA, GL_TRUE) <<
313              R200_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstA,
314                                                    GL_FALSE) <<
315                                       R200_DST_BLEND_SHIFT);
316
317         switch (ctx->Color.BlendEquationA) {
318         case GL_FUNC_ADD:
319                 eqnA = R300_COMB_FCN_ADD_CLAMP;
320                 break;
321
322         case GL_FUNC_SUBTRACT:
323                 eqnA = R300_COMB_FCN_SUB_CLAMP;
324                 break;
325
326         case GL_FUNC_REVERSE_SUBTRACT:
327                 eqnA = R200_COMB_FCN_RSUB_CLAMP;
328                 break;
329
330         case GL_MIN:
331                 eqnA = R200_COMB_FCN_MIN;
332                 funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
333                     (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
334                 break;
335
336         case GL_MAX:
337                 eqnA = R200_COMB_FCN_MAX;
338                 funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
339                     (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
340                 break;
341
342         default:
343                 fprintf(stderr, "[%s:%u] Invalid A blend equation (0x%04x).\n",
344                         __func__, __LINE__, ctx->Color.BlendEquationA);
345                 return;
346         }
347
348         rmesa->hw.bld.cmd[R300_BLD_ABLEND] = eqnA | funcA;
349         rmesa->hw.bld.cmd[R300_BLD_CBLEND] = eqn | func ;
350         if(rmesa->hw.bld.cmd[R300_BLD_ABLEND] == rmesa->hw.bld.cmd[R300_BLD_CBLEND]){
351                 rmesa->hw.bld.cmd[R300_BLD_CBLEND] |= R300_BLEND_UNKNOWN | R300_BLEND_ENABLE | R300_BLEND_NO_SEPARATE;
352                 } else {
353                 rmesa->hw.bld.cmd[R300_BLD_CBLEND] |= R300_BLEND_UNKNOWN | R300_BLEND_ENABLE;
354                 }
355
356 }
357
358 static void r300BlendEquationSeparate(GLcontext * ctx,
359                                       GLenum modeRGB, GLenum modeA)
360 {
361         r300_set_blend_state(ctx);
362 }
363
364 static void r300BlendFuncSeparate(GLcontext * ctx,
365                                   GLenum sfactorRGB, GLenum dfactorRGB,
366                                   GLenum sfactorA, GLenum dfactorA)
367 {
368         r300_set_blend_state(ctx);
369 }
370
371 /**
372  * Update our tracked culling state based on Mesa's state.
373  */
374 static void r300UpdateCulling(GLcontext* ctx)
375 {
376         r300ContextPtr r300 = R300_CONTEXT(ctx);
377         uint32_t val = 0;
378
379         R300_STATECHANGE(r300, cul);
380         if (ctx->Polygon.CullFlag) {
381                 if (ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
382                         val = R300_CULL_FRONT|R300_CULL_BACK;
383                 else if (ctx->Polygon.CullFaceMode == GL_FRONT)
384                         val = R300_CULL_FRONT;
385                 else
386                         val = R300_CULL_BACK;
387
388                 if (ctx->Polygon.FrontFace == GL_CW)
389                         val |= R300_FRONT_FACE_CW;
390                 else
391                         val |= R300_FRONT_FACE_CCW;
392         }
393
394         r300->hw.cul.cmd[R300_CUL_CULL] = val;
395 }
396
397
398 /**
399  * Handle glEnable()/glDisable().
400  *
401  * \note Mesa already filters redundant calls to glEnable/glDisable.
402  */
403 static void r300Enable(GLcontext* ctx, GLenum cap, GLboolean state)
404 {
405         r300ContextPtr r300 = R300_CONTEXT(ctx);
406         uint32_t newval;
407
408         if (RADEON_DEBUG & DEBUG_STATE)
409                 fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__,
410                         _mesa_lookup_enum_by_nr(cap),
411                         state ? "GL_TRUE" : "GL_FALSE");
412
413         switch (cap) {
414         case GL_DEPTH_TEST:
415                 R300_STATECHANGE(r300, zc);
416
417                 if (state) {
418                         if (ctx->Depth.Mask)
419                                 newval = R300_RB3D_Z_TEST_AND_WRITE;
420                         else
421                                 newval = R300_RB3D_Z_TEST;
422                 } else
423                         newval = 0;
424
425                 r300->hw.zc.cmd[R300_ZC_CNTL_0] = newval;
426                 break;
427
428         case GL_CULL_FACE:
429                 r300UpdateCulling(ctx);
430                 break;
431
432         default:
433                 radeonEnable(ctx, cap, state);
434                 return;
435         }
436 }
437
438
439 /**
440  * Change the culling mode.
441  *
442  * \note Mesa already filters redundant calls to this function.
443  */
444 static void r300CullFace(GLcontext* ctx, GLenum mode)
445 {
446         (void)mode;
447
448         r300UpdateCulling(ctx);
449 }
450
451
452 /**
453  * Change the polygon orientation.
454  *
455  * \note Mesa already filters redundant calls to this function.
456  */
457 static void r300FrontFace(GLcontext* ctx, GLenum mode)
458 {
459         (void)mode;
460
461         r300UpdateCulling(ctx);
462 }
463
464
465 /**
466  * Change the depth testing function.
467  *
468  * \note Mesa already filters redundant calls to this function.
469  */
470 static void r300DepthFunc(GLcontext* ctx, GLenum func)
471 {
472         r300ContextPtr r300 = R300_CONTEXT(ctx);
473
474         R300_STATECHANGE(r300, zc);
475
476         switch(func) {
477         case GL_NEVER:
478                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_NEVER;
479                 break;
480         case GL_LESS:
481                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_LESS;
482                 break;
483         case GL_EQUAL:
484                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_EQUAL;
485                 break;
486         case GL_LEQUAL:
487                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_LEQUAL;
488                 break;
489         case GL_GREATER:
490                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_GREATER;
491                 break;
492         case GL_NOTEQUAL:
493                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_NEQUAL;
494                 break;
495         case GL_GEQUAL:
496                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_GEQUAL;
497                 break;
498         case GL_ALWAYS:
499                 r300->hw.zc.cmd[R300_ZC_CNTL_1] = R300_Z_TEST_ALWAYS;
500                 break;
501         }
502 }
503
504
505 /**
506  * Enable/Disable depth writing.
507  *
508  * \note Mesa already filters redundant calls to this function.
509  */
510 static void r300DepthMask(GLcontext* ctx, GLboolean mask)
511 {
512         r300ContextPtr r300 = R300_CONTEXT(ctx);
513
514         if (!ctx->Depth.Test)
515                 return;
516
517         R300_STATECHANGE(r300, zc);
518         r300->hw.zc.cmd[R300_ZC_CNTL_0] = mask
519                 ? R300_RB3D_Z_TEST_AND_WRITE : R300_RB3D_Z_TEST;
520 }
521
522
523 /**
524  * Handle glColorMask()
525  */
526 static void r300ColorMask(GLcontext* ctx,
527                           GLboolean r, GLboolean g, GLboolean b, GLboolean a)
528 {
529         r300ContextPtr r300 = R300_CONTEXT(ctx);
530         int mask = (b << 0) | (g << 1) | (r << 2) | (a << 3);
531
532         if (mask != r300->hw.cmk.cmd[R300_CMK_COLORMASK]) {
533                 R300_STATECHANGE(r300, cmk);
534                 r300->hw.cmk.cmd[R300_CMK_COLORMASK] = mask;
535         }
536 }
537
538 /* =============================================================
539  * Point state
540  */
541 static void r300PointSize(GLcontext * ctx, GLfloat size)
542 {
543         r300ContextPtr r300 = R300_CONTEXT(ctx);
544         
545         /* This might need fixing later */
546         R300_STATECHANGE(r300, vps);
547         r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
548 }
549
550 /* =============================================================
551  * Window position and viewport transformation
552  */
553
554 /*
555  * To correctly position primitives:
556  */
557 #define SUBPIXEL_X 0.125
558 #define SUBPIXEL_Y 0.125
559
560 void r300UpdateWindow(GLcontext * ctx)
561 {
562         r300ContextPtr rmesa = R300_CONTEXT(ctx);
563         __DRIdrawablePrivate *dPriv = rmesa->radeon.dri.drawable;
564         GLfloat xoffset = dPriv ? (GLfloat) dPriv->x : 0;
565         GLfloat yoffset = dPriv ? (GLfloat) dPriv->y + dPriv->h : 0;
566         const GLfloat *v = ctx->Viewport._WindowMap.m;
567
568         GLfloat sx = v[MAT_SX];
569         GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
570         GLfloat sy = -v[MAT_SY];
571         GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
572         GLfloat sz = v[MAT_SZ] * rmesa->state.depth.scale;
573         GLfloat tz = v[MAT_TZ] * rmesa->state.depth.scale;
574
575         R300_FIREVERTICES(rmesa);
576         R300_STATECHANGE(rmesa, vpt);
577
578         rmesa->hw.vpt.cmd[R300_VPT_XSCALE]  = r300PackFloat32(sx);
579         rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
580         rmesa->hw.vpt.cmd[R300_VPT_YSCALE]  = r300PackFloat32(sy);
581         rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
582         rmesa->hw.vpt.cmd[R300_VPT_ZSCALE]  = r300PackFloat32(sz);
583         rmesa->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(tz);
584 }
585
586 static void r300Viewport(GLcontext * ctx, GLint x, GLint y,
587                          GLsizei width, GLsizei height)
588 {
589         /* Don't pipeline viewport changes, conflict with window offset
590          * setting below.  Could apply deltas to rescue pipelined viewport
591          * values, or keep the originals hanging around.
592          */
593         R200_FIREVERTICES(R200_CONTEXT(ctx));
594         r300UpdateWindow(ctx);
595 }
596
597 static void r300DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
598 {
599         r300UpdateWindow(ctx);
600 }
601
602 /* Routing and texture-related */
603
604 void r300_setup_routing(GLcontext *ctx, GLboolean immediate)
605 {
606         int i, count=0,reg=0;
607         GLuint dw, mask;
608         TNLcontext *tnl = TNL_CONTEXT(ctx);
609         struct vertex_buffer *VB = &tnl->vb;
610         r300ContextPtr r300 = R300_CONTEXT(ctx);
611         
612         
613         /* Stage 1 - input to VAP */
614         
615         /* Assign register number automatically, retaining it in rmesa->state.reg */
616         
617         /* Note: immediate vertex data includes all coordinates.
618         To save bandwidth use either VBUF or state-based vertex generation */
619         
620         #define CONFIGURE_AOS(v, o, r, f) \
621                 {\
622                 if(immediate){ \
623                         r300->state.aos[count].element_size=4; \
624                         r300->state.aos[count].stride=4; \
625                         r300->state.aos[count].ncomponents=4; \
626                         } else { \
627                         r300->state.aos[count].element_size=v->size; \
628                         r300->state.aos[count].stride=v->size; \
629                         r300->state.aos[count].ncomponents=v->size; \
630                         } \
631                 r300->state.aos[count].offset=o; \
632                 r300->state.aos[count].reg=reg; \
633                 r300->state.aos[count].format=(f); \
634                 r300->state.vap_reg.r=reg; \
635                 count++; \
636                 reg++; \
637                 }
638         
639                 /* All offsets are 0 - for use by immediate mode. 
640                 Should change later to handle vertex buffers */
641         CONFIGURE_AOS(VB->ObjPtr, 0, i_coords, AOS_FORMAT_FLOAT);
642         CONFIGURE_AOS(VB->ColorPtr[0], 0, i_color[0], AOS_FORMAT_FLOAT_COLOR);
643         for(i=0;i < ctx->Const.MaxTextureUnits;i++)
644                 if(ctx->Texture.Unit[i].Enabled)
645                         CONFIGURE_AOS(VB->TexCoordPtr[i], 0, i_tex[i], AOS_FORMAT_FLOAT);
646                         
647         r300->state.aos_count=count;
648         
649         if (RADEON_DEBUG & DEBUG_STATE)
650                 fprintf(stderr, "aos_count=%d\n", count);
651         
652         if(count>R300_MAX_AOS_ARRAYS){
653                 fprintf(stderr, "Aieee ! AOS array count exceeded !\n");
654                 exit(-1);
655                 }
656                         
657         /* Implement AOS */
658         
659         /* setup INPUT_ROUTE */
660         R300_STATECHANGE(r300, vir[0]);
661         for(i=0;i+1<count;i+=2){
662                 dw=(r300->state.aos[i].ncomponents-1) 
663                 | ((r300->state.aos[i].reg)<<8)
664                 | (r300->state.aos[i].format<<14)
665                 | (((r300->state.aos[i+1].ncomponents-1) 
666                 | ((r300->state.aos[i+1].reg)<<8)
667                 | (r300->state.aos[i+1].format<<14))<<16);
668                 
669                 if(i+2==count){
670                         dw|=(1<<(13+16));
671                         }
672                 r300->hw.vir[0].cmd[R300_VIR_CNTL_0+(i>>1)]=dw;
673                 }
674         if(count & 1){
675                 dw=(r300->state.aos[count-1].ncomponents-1)
676                 | (r300->state.aos[count-1].format<<14)
677                 | ((r300->state.aos[count-1].reg)<<8)
678                 | (1<<13);
679                 r300->hw.vir[0].cmd[R300_VIR_CNTL_0+(count>>1)]=dw;
680                 //fprintf(stderr, "vir0 dw=%08x\n", dw);
681                 }
682         /* Set the rest of INPUT_ROUTE_0 to 0 */
683         //for(i=((count+1)>>1); i<8; i++)r300->hw.vir[0].cmd[R300_VIR_CNTL_0+i]=(0x0);
684         ((drm_r300_cmd_header_t*)r300->hw.vir[0].cmd)->unchecked_state.count = (count+1)>>1;
685         
686         
687         /* Mesa assumes that all missing components are from (0, 0, 0, 1) */
688         #define ALL_COMPONENTS ((R300_INPUT_ROUTE_SELECT_X<<R300_INPUT_ROUTE_X_SHIFT) \
689                 | (R300_INPUT_ROUTE_SELECT_Y<<R300_INPUT_ROUTE_Y_SHIFT) \
690                 | (R300_INPUT_ROUTE_SELECT_Z<<R300_INPUT_ROUTE_Z_SHIFT) \
691                 | (R300_INPUT_ROUTE_SELECT_W<<R300_INPUT_ROUTE_W_SHIFT))
692         
693         #define ALL_DEFAULT ((R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_X_SHIFT) \
694                 | (R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_Y_SHIFT) \
695                 | (R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_Z_SHIFT) \
696                 | (R300_INPUT_ROUTE_SELECT_ONE<<R300_INPUT_ROUTE_W_SHIFT))
697         
698         R300_STATECHANGE(r300, vir[1]);
699                 
700         for(i=0;i+1<count;i+=2){
701                 /* do i first.. */
702                 mask=(1<<(r300->state.aos[i].ncomponents*3))-1;
703                 dw=(ALL_COMPONENTS & mask)
704                 | (ALL_DEFAULT & ~mask)
705                 | R300_INPUT_ROUTE_ENABLE;
706                 
707                 /* i+1 */
708                 mask=(1<<(r300->state.aos[i+1].ncomponents*3))-1;
709                 dw|=( 
710                 (ALL_COMPONENTS & mask)
711                 | (ALL_DEFAULT & ~mask)
712                 | R300_INPUT_ROUTE_ENABLE
713                 )<<16;
714         
715                 r300->hw.vir[1].cmd[R300_VIR_CNTL_0+(i>>1)]=dw;
716                 }
717         if(count & 1){
718                 mask=(1<<(r300->state.aos[count-1].ncomponents*3))-1;
719                 dw=(ALL_COMPONENTS & mask)
720                 | (ALL_DEFAULT & ~mask)
721                 | R300_INPUT_ROUTE_ENABLE;
722                 r300->hw.vir[1].cmd[R300_VIR_CNTL_0+(count>>1)]=dw;
723                 //fprintf(stderr, "vir1 dw=%08x\n", dw);
724                 }
725         /* Set the rest of INPUT_ROUTE_1 to 0 */
726         //for(i=((count+1)>>1); i<8; i++)r300->hw.vir[1].cmd[R300_VIR_CNTL_0+i]=0x0;
727         ((drm_r300_cmd_header_t*)r300->hw.vir[1].cmd)->unchecked_state.count = (count+1)>>1;
728         
729         /* Set up input_cntl */
730         
731         R300_STATECHANGE(r300, vic);
732         r300->hw.vic.cmd[R300_VIC_CNTL_0]=0x5555;  /* Hard coded value, no idea what it means */
733         
734         r300->hw.vic.cmd[R300_VIC_CNTL_1]=R300_INPUT_CNTL_POS
735                                         | R300_INPUT_CNTL_COLOR;
736         
737         for(i=0;i < ctx->Const.MaxTextureUnits;i++)
738                 if(ctx->Texture.Unit[i].Enabled)
739                         r300->hw.vic.cmd[R300_VIC_CNTL_1]|=(R300_INPUT_CNTL_TC0<<i);
740         
741         /* Stage 3: VAP output */
742         R300_STATECHANGE(r300, vof);
743         r300->hw.vof.cmd[R300_VOF_CNTL_0]=R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
744                                         | R300_VAP_OUTPUT_VTX_FMT_0__COLOR_PRESENT;
745         
746         r300->hw.vof.cmd[R300_VOF_CNTL_1]=0;
747         for(i=0;i < ctx->Const.MaxTextureUnits;i++)
748                 if(ctx->Texture.Unit[i].Enabled)
749                         r300->hw.vof.cmd[R300_VOF_CNTL_1]|=(4<<(3*i));
750         
751 }
752
753 static r300TexObj default_tex_obj={
754         filter:R300_TX_MAG_FILTER_LINEAR | R300_TX_MIN_FILTER_LINEAR,
755         pitch: 0x8000,
756         size: (0xff << R300_TX_WIDTHMASK_SHIFT) 
757               | (0xff << R300_TX_HEIGHTMASK_SHIFT)
758               | (0x8 << R300_TX_SIZE_SHIFT),
759         format: 0x88a0c,
760         offset: 0x0,
761         unknown4: 0x0,
762         unknown5: 0x0
763         };
764
765         /* there is probably a system to these value, but, for now, 
766            we just try by hand */
767
768 static int inline translate_src(int src)
769 {
770         switch (src) {
771         case GL_TEXTURE:
772                 return 1;
773                 break;
774         case GL_CONSTANT:
775                 return 2;
776                 break;
777         case GL_PRIMARY_COLOR:
778                 return 3;
779                 break;
780         case GL_PREVIOUS:
781                 return 4;
782                 break;
783         case GL_ZERO:
784                 return 5;
785                 break;
786         case GL_ONE:
787                 return 6;
788                 break;
789         default:
790                 return 0;
791         }
792 }
793            
794 /* I think 357 and 457 are prime numbers.. wiggle them if you get coincidences */
795 #define FORMAT_HASH(opRGB, srcRGB, modeRGB, opA, srcA, modeA, format, intFormat)        ( \
796         (\
797         ((opRGB)<<30) | ((opA)<<28) | \
798         ((srcRGB)<< 25) | ((srcA)<<22) | \
799         ((modeRGB)) \
800         ) \
801         ^ ((modeA)*357) \
802         ^ (((format)) *457) \
803         ^ ((intFormat) * 7) \
804         )
805            
806 static GLuint translate_texture_format(GLcontext *ctx, GLint tex_unit, GLuint format, GLint IntFormat)
807 {
808         const struct gl_texture_unit *texUnit= &ctx->Texture.Unit[tex_unit];
809         int i=0; /* number of alpha args .. */
810         
811         /* Size field in format specific first */
812         switch(FORMAT_HASH(                                                     
813                 texUnit->_CurrentCombine->OperandRGB[i] -GL_SRC_COLOR,
814                 translate_src(texUnit->_CurrentCombine->SourceRGB[i]),
815                 texUnit->_CurrentCombine->ModeRGB,
816                 texUnit->_CurrentCombine->OperandA[i] -GL_SRC_ALPHA,
817                 translate_src(texUnit->_CurrentCombine->SourceA[i]),
818                 texUnit->_CurrentCombine->ModeA,
819                 format,
820                 IntFormat
821                 )){
822         case FORMAT_HASH(0, 1, 0x2100, 0, 1, 0x2100, 0x00088047, 0x1908):
823                 /* tested with:
824                         kfiresaver.kss 
825                         */
826                 return 0x7a0c; /* kfiresaver.kss */
827         case FORMAT_HASH(0, 1, 0x2100, 0, 1, 0x2100, 0x00088047, 0x8058):
828                 /* tested with:
829                         Quake3demo 
830                         */
831                 return 0x8a0c; /* Quake3demo -small font on the bottom */
832         case FORMAT_HASH(0, 1, 0x2100, 0, 1, 0x2100, 0x00077047, 4):
833                 /* tested with:
834                         kfiresaver.kss 
835                         */
836                 return 0x4ba0c;
837         case FORMAT_HASH(0, 1, 0x2100, 0, 1, 0x2100, 0x00055047, 4):
838                 /* tested with:
839                         kfiresaver.kss
840                         kfountain.kss
841                         */
842                 return 0x51a0c; 
843         case FORMAT_HASH(0, 1, 0x2100, 0, 4, 0x1e01, 0x00088047, 3):
844                 /* tested with
845                         lesson 06
846                         lesson 07
847                         */
848                 return 0x53a0c;
849         //case FORMAT_HASH(0, 1, 0x2100, 0, 4, 0x1e01, 0x00055047, 0):
850                 /* Can't remember what I tested this with.. 
851                    try putting return 0 of you see broken textures which 
852                    are not being complained about */
853                 return 0x53a0c; 
854         case FORMAT_HASH(0, 1, 0x2100, 0, 4, 0x1e01, 0x00099047, 0x8051):
855         //case FORMAT_HASH(0, 1, 0x2100, 0, 4, 0x1e01, 0x00089047, 0x8058):
856         case FORMAT_HASH(0, 1, 0x2100, 0, 4, 0x1e01, 0x00077047, 0x8051):
857                 /* Tested with:
858                         Quake3demo
859                         */
860                 return 0x53a0c; 
861         case  FORMAT_HASH(0, 1, 0x2100, 0, 1, 0x2100, 0x00055045, 0x00008056):
862         case  FORMAT_HASH(0, 1, 0x2100, 0, 1, 0x2100, 0x00088045, 0x00008056):
863                 return 0x53a23;
864         case FORMAT_HASH(0, 1, 0x2100, 0, 4, 0x1e01, 0x00099004, 0x00008050):
865                 return 0;
866                 return 0x2a0b;
867         }
868         //      return 0x53a0c; 
869         
870         /* Just key on internal format  - useful for quick tryouts*/
871         
872                 return 0;
873         #if 1
874         switch(IntFormat
875                 ){
876         case 0x3:
877                 return 0x53a0c;
878         case 0x8050:
879                 return 0x2a0b;
880         case 0x8056:
881         
882                 return 0x53a23;
883         case 0x8058:
884                 return 0;
885                 return 0x8a0c;
886                 //fprintf(stderr, "%08x\n", format);
887                 /* Tested with:
888                         Quake3demo
889                         */
890                 return 0x53a0c; 
891         default:
892                 return 0x53a0c;
893         }
894         #endif
895         
896         
897         
898         {
899                 static int warn_once=1;
900                 if(warn_once){
901                         fprintf(stderr, "%s:%s Do not know how to translate texture format - help me !\n",
902                                 __FILE__, __FUNCTION__);
903                         warn_once=0;
904                         }
905         }
906                 return 0;
907 }
908         
909 void r300_setup_textures(GLcontext *ctx)
910 {
911         int i, mtu;
912         struct r300_tex_obj *t;
913         r300ContextPtr r300 = R300_CONTEXT(ctx);
914         int max_texture_unit=-1; /* -1 translates into no setup costs for fields */
915         struct gl_texture_unit *texUnit;
916
917         R300_STATECHANGE(r300, txe);
918         R300_STATECHANGE(r300, tex.filter);
919         R300_STATECHANGE(r300, tex.unknown1);
920         R300_STATECHANGE(r300, tex.size);
921         R300_STATECHANGE(r300, tex.format);
922         R300_STATECHANGE(r300, tex.offset);
923         R300_STATECHANGE(r300, tex.unknown4);
924         R300_STATECHANGE(r300, tex.unknown5);
925         
926         r300->state.texture.tc_count=0;
927         
928         r300->hw.txe.cmd[R300_TXE_ENABLE]=0x0;
929         
930         mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
931         if (RADEON_DEBUG & DEBUG_STATE)
932                 fprintf(stderr, "mtu=%d\n", mtu);
933         
934         if(mtu>R300_MAX_TEXTURE_UNITS){
935                 fprintf(stderr, "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n", 
936                         mtu, R300_MAX_TEXTURE_UNITS);
937                 exit(-1);
938                 }
939         for(i=0;i<mtu;i++){
940                 if(ctx->Texture.Unit[i].Enabled){
941                         t=r300->state.texture.unit[i].texobj;
942                         r300->state.texture.tc_count++;
943                         if(t==NULL){
944                                 fprintf(stderr, "Texture unit %d enabled, but corresponding texobj is NULL, using default object.\n", i);
945                                 //exit(-1);
946                                 t=&default_tex_obj;
947                                 }
948                         if (RADEON_DEBUG & DEBUG_STATE)
949                                 fprintf(stderr, "Activating texture unit %d\n", i);
950                         max_texture_unit=i;
951                         r300->hw.txe.cmd[R300_TXE_ENABLE]|=(1<<i);
952                         
953                         r300->hw.tex.filter.cmd[R300_TEX_VALUE_0+i]=t->filter;
954                         r300->hw.tex.unknown1.cmd[R300_TEX_VALUE_0+i]=t->pitch;
955                         r300->hw.tex.size.cmd[R300_TEX_VALUE_0+i]=t->size;
956                         r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=t->format;
957                         r300->hw.tex.offset.cmd[R300_TEX_VALUE_0+i]=r300->radeon.radeonScreen->fbLocation+t->offset;
958                         r300->hw.tex.unknown4.cmd[R300_TEX_VALUE_0+i]=0x0;
959                         r300->hw.tex.unknown5.cmd[R300_TEX_VALUE_0+i]=0x0;
960                         
961                         
962                         
963                         /* We don't know how to set this yet */
964                         //value from r300_lib.c for RGB24
965                         //r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=0x88a0c; 
966                         r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=translate_texture_format(ctx, i, t->format, t->base.tObj->Image[0][0]->IntFormat);
967                         /* Use the code below to quickly find matching texture
968                            formats. Requires an app that displays the same texture
969                            repeatedly  */
970                               #if 1
971                                 if(r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]==0){ 
972                                         static int fmt=0x0;
973                                         static int k=0;
974                                         k++;
975                                         if(k>400){
976                                                 k=0;
977                                                 fmt++;
978                                                 texUnit = &ctx->Texture.Unit[i];
979                                                 fprintf(stderr, "Want to set FORMAT_HASH(%d, %d, 0x%04x, %d, %d, 0x%04x, 0x%08x, 0x%08x)\n",
980                                                         texUnit->_CurrentCombine->OperandRGB[0] -GL_SRC_COLOR,
981                                                         translate_src(texUnit->_CurrentCombine->SourceRGB[0]),
982                                                         texUnit->_CurrentCombine->ModeRGB,
983                                                         texUnit->_CurrentCombine->OperandA[0] -GL_SRC_ALPHA,
984                                                         translate_src(texUnit->_CurrentCombine->SourceA[0]),
985                                                         texUnit->_CurrentCombine->ModeA,
986                                                         t->format,
987                                                         t->base.tObj->Image[0][0]->IntFormat
988                                                         );
989                                                 fprintf(stderr, "Also known: format_x=%08x border_color=%08x cubic_faces=%08x\n", t->format_x, t->pp_border_color, t->pp_cubic_faces);
990                                                 fprintf(stderr, "\t_ReallyEnabled=%08x EnvMode=%08x IntFormat=%08x\n", texUnit->_ReallyEnabled, texUnit->EnvMode,  t->base.tObj->Image[0][0]->IntFormat);
991                                                 if(fmt>0xff){
992                                                         //exit(-1);
993                                                         fmt=0;
994                                                         }
995                                                 //sleep(1);
996                                                 fprintf(stderr, "Now trying format %08x\n", 
997                                                         0x00a0c | (fmt<<12));
998                                                 fprintf(stderr, "size=%08x\n", t->size);
999                                                 }
1000                                         r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=0x00a0b | (fmt<<12);
1001                                         //r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=0x51a00 | (fmt);
1002                                         //r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=0x53a0c | (fmt<<24);
1003                                         }
1004                               #endif
1005                         
1006                         }
1007                         
1008                 }
1009         ((drm_r300_cmd_header_t*)r300->hw.tex.filter.cmd)->unchecked_state.count = max_texture_unit+1;
1010         ((drm_r300_cmd_header_t*)r300->hw.tex.unknown1.cmd)->unchecked_state.count = max_texture_unit+1;
1011         ((drm_r300_cmd_header_t*)r300->hw.tex.size.cmd)->unchecked_state.count = max_texture_unit+1;
1012         ((drm_r300_cmd_header_t*)r300->hw.tex.format.cmd)->unchecked_state.count = max_texture_unit+1;
1013         ((drm_r300_cmd_header_t*)r300->hw.tex.offset.cmd)->unchecked_state.count = max_texture_unit+1;
1014         ((drm_r300_cmd_header_t*)r300->hw.tex.unknown4.cmd)->unchecked_state.count = max_texture_unit+1;
1015         ((drm_r300_cmd_header_t*)r300->hw.tex.unknown5.cmd)->unchecked_state.count = max_texture_unit+1;
1016         
1017         if (RADEON_DEBUG & DEBUG_STATE)
1018                 fprintf(stderr, "TX_ENABLE: %08x  max_texture_unit=%d\n", r300->hw.txe.cmd[R300_TXE_ENABLE], max_texture_unit);
1019 }
1020
1021 void r300_setup_rs_unit(GLcontext *ctx)
1022 {
1023         r300ContextPtr r300 = R300_CONTEXT(ctx);
1024         int i;
1025         
1026         /* This needs to be rewritten - it is a hack at best */
1027         
1028         R300_STATECHANGE(r300, ri);
1029         R300_STATECHANGE(r300, rc);
1030         R300_STATECHANGE(r300, rr);
1031         
1032         for(i = 1; i <= 8; ++i)
1033                 r300->hw.ri.cmd[i] = 0x00d10000;
1034         r300->hw.ri.cmd[R300_RI_INTERP_1] |= R300_RS_INTERP_1_UNKNOWN;
1035         r300->hw.ri.cmd[R300_RI_INTERP_2] |= R300_RS_INTERP_2_UNKNOWN;
1036         r300->hw.ri.cmd[R300_RI_INTERP_3] |= R300_RS_INTERP_3_UNKNOWN;
1037         
1038         for(i = 1; i <= 8; ++i)
1039                 r300->hw.rr.cmd[i] = 0;
1040         /* textures enabled ? */
1041         if(r300->state.texture.tc_count>0){
1042         
1043                 /* This code only really works with one set of texture coordinates */
1044                 
1045                 /* The second constant is needed to get glxgears display anything .. */
1046                 r300->hw.rc.cmd[1] = R300_RS_CNTL_0_UNKNOWN_7 
1047                                 | R300_RS_CNTL_0_UNKNOWN_18 
1048                                 | (r300->state.texture.tc_count<<R300_RS_CNTL_TC_CNT_SHIFT);
1049                 r300->hw.rc.cmd[2] = 0xc0;
1050         
1051         
1052                 ((drm_r300_cmd_header_t*)r300->hw.rr.cmd)->unchecked_state.count = 1;
1053                 r300->hw.rr.cmd[R300_RR_ROUTE_0] = 0x24008;
1054                 
1055                 } else {
1056                 
1057                 /* The second constant is needed to get glxgears display anything .. */
1058                 r300->hw.rc.cmd[1] = R300_RS_CNTL_0_UNKNOWN_7 | R300_RS_CNTL_0_UNKNOWN_18;
1059                 r300->hw.rc.cmd[2] = 0;
1060                 
1061                 ((drm_r300_cmd_header_t*)r300->hw.rr.cmd)->unchecked_state.count = 1;
1062                 r300->hw.rr.cmd[R300_RR_ROUTE_0] = 0x4000;
1063                 
1064                 }
1065 }
1066
1067 #define vpucount(ptr) (((drm_r300_cmd_header_t*)(ptr))->vpu.count)
1068
1069 #define bump_vpu_count(ptr, new_count)   do{\
1070         drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
1071         int _nc=(new_count)/4; \
1072         if(_nc>_p->vpu.count)_p->vpu.count=_nc;\
1073         }while(0)
1074
1075 void static inline setup_vertex_shader_fragment(r300ContextPtr r300, int dest, struct r300_vertex_shader_fragment *vsf)
1076 {
1077         int i;
1078         
1079         if(vsf->length==0)return;
1080         
1081         if(vsf->length & 0x3){
1082                 fprintf(stderr,"VERTEX_SHADER_FRAGMENT must have length divisible by 4\n");
1083                 exit(-1);
1084                 }
1085         
1086         switch((dest>>8) & 0xf){
1087         case 0:
1088                 R300_STATECHANGE(r300, vpi);
1089                 for(i=0;i<vsf->length;i++)
1090                         r300->hw.vpi.cmd[R300_VPI_INSTR_0+i+4*(dest & 0xff)]=(vsf->body.d[i]);
1091                 bump_vpu_count(r300->hw.vpi.cmd, vsf->length+4*(dest & 0xff));
1092                 break;
1093                 
1094         case 2:
1095                 R300_STATECHANGE(r300, vpp);
1096                 for(i=0;i<vsf->length;i++)
1097                         r300->hw.vpp.cmd[R300_VPP_PARAM_0+i+4*(dest & 0xff)]=(vsf->body.d[i]);
1098                 bump_vpu_count(r300->hw.vpp.cmd, vsf->length+4*(dest & 0xff));
1099                 break;
1100         case 4: 
1101                 R300_STATECHANGE(r300, vps);
1102                 for(i=0;i<vsf->length;i++)
1103                         r300->hw.vps.cmd[1+i+4*(dest & 0xff)]=(vsf->body.d[i]);
1104                 bump_vpu_count(r300->hw.vps.cmd, vsf->length+4*(dest & 0xff));
1105                 break;
1106         default:
1107                 fprintf(stderr, "%s:%s don't know how to handle dest %04x\n", __FILE__, __FUNCTION__, dest);
1108                 exit(-1);
1109         }
1110 }
1111
1112
1113 void r300SetupVertexShader(r300ContextPtr rmesa)
1114 {
1115         GLcontext* ctx = rmesa->radeon.glCtx;
1116         
1117         /* Reset state, in case we don't use something */
1118         ((drm_r300_cmd_header_t*)rmesa->hw.vpp.cmd)->vpu.count = 0;
1119         ((drm_r300_cmd_header_t*)rmesa->hw.vpi.cmd)->vpu.count = 0;
1120         ((drm_r300_cmd_header_t*)rmesa->hw.vps.cmd)->vpu.count = 0;
1121
1122
1123 /* This needs to be replaced by vertex shader generation code */
1124
1125         
1126         /* textures enabled ? */
1127         if(rmesa->state.texture.tc_count>0){
1128                 rmesa->state.vertex_shader=SINGLE_TEXTURE_VERTEX_SHADER;
1129                 } else {
1130                 rmesa->state.vertex_shader=FLAT_COLOR_VERTEX_SHADER;
1131                 }
1132
1133
1134         rmesa->state.vertex_shader.matrix[0].length=16;
1135         memcpy(rmesa->state.vertex_shader.matrix[0].body.f, ctx->_ModelProjectMatrix.m, 16*4);
1136         
1137         setup_vertex_shader_fragment(rmesa, VSF_DEST_PROGRAM, &(rmesa->state.vertex_shader.program));
1138         
1139         setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX0, &(rmesa->state.vertex_shader.matrix[0]));
1140         #if 0
1141         setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX1, &(rmesa->state.vertex_shader.matrix[0]));
1142         setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX2, &(rmesa->state.vertex_shader.matrix[0]));
1143         
1144         setup_vertex_shader_fragment(rmesa, VSF_DEST_VECTOR0, &(rmesa->state.vertex_shader.vector[0]));
1145         setup_vertex_shader_fragment(rmesa, VSF_DEST_VECTOR1, &(rmesa->state.vertex_shader.vector[1]));
1146         #endif
1147         
1148         #if 0
1149         setup_vertex_shader_fragment(rmesa, VSF_DEST_UNKNOWN1, &(rmesa->state.vertex_shader.unknown1));
1150         setup_vertex_shader_fragment(rmesa, VSF_DEST_UNKNOWN2, &(rmesa->state.vertex_shader.unknown2));
1151         #endif
1152         
1153         R300_STATECHANGE(rmesa, pvs);
1154         rmesa->hw.pvs.cmd[R300_PVS_CNTL_1]=(rmesa->state.vertex_shader.program_start << R300_PVS_CNTL_1_PROGRAM_START_SHIFT)
1155                 | (rmesa->state.vertex_shader.unknown_ptr1 << R300_PVS_CNTL_1_UNKNOWN_SHIFT)
1156                 | (rmesa->state.vertex_shader.program_end << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
1157         rmesa->hw.pvs.cmd[R300_PVS_CNTL_2]=(rmesa->state.vertex_shader.param_offset << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT)
1158                 | (rmesa->state.vertex_shader.param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
1159         rmesa->hw.pvs.cmd[R300_PVS_CNTL_3]=(rmesa->state.vertex_shader.unknown_ptr2 << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT)
1160         | (rmesa->state.vertex_shader.unknown_ptr3 << 0);
1161         
1162         /* This is done for vertex shader fragments, but also needs to be done for vap_pvs, 
1163         so I leave it as a reminder */
1164         #if 0
1165         reg_start(R300_VAP_PVS_WAITIDLE,0);
1166                 e32(0x00000000);
1167         #endif
1168 }
1169
1170 void r300SetupPixelShader(r300ContextPtr rmesa)
1171 {
1172 int i,k;
1173
1174         /* This needs to be replaced by pixel shader generation code */
1175
1176         /* textures enabled ? */
1177         if(rmesa->state.texture.tc_count>0){
1178                 rmesa->state.pixel_shader=SINGLE_TEXTURE_PIXEL_SHADER;
1179                 } else {
1180                 rmesa->state.pixel_shader=FLAT_COLOR_PIXEL_SHADER;
1181                 }
1182
1183         R300_STATECHANGE(rmesa, fpt);
1184         for(i=0;i<rmesa->state.pixel_shader.program.tex.length;i++)
1185                 rmesa->hw.fpt.cmd[R300_FPT_INSTR_0+i]=rmesa->state.pixel_shader.program.tex.inst[i];
1186         rmesa->hw.fpt.cmd[R300_FPT_CMD_0]=cmducs(R300_PFS_TEXI_0, rmesa->state.pixel_shader.program.tex.length);
1187
1188         #define OUTPUT_FIELD(st, reg, field)  \
1189                 R300_STATECHANGE(rmesa, st); \
1190                 for(i=0;i<rmesa->state.pixel_shader.program.alu.length;i++) \
1191                         rmesa->hw.st.cmd[R300_FPI_INSTR_0+i]=rmesa->state.pixel_shader.program.alu.inst[i].field;\
1192                 rmesa->hw.st.cmd[R300_FPI_CMD_0]=cmducs(reg, rmesa->state.pixel_shader.program.alu.length);
1193         
1194         OUTPUT_FIELD(fpi[0], R300_PFS_INSTR0_0, inst0);
1195         OUTPUT_FIELD(fpi[1], R300_PFS_INSTR1_0, inst1);
1196         OUTPUT_FIELD(fpi[2], R300_PFS_INSTR2_0, inst2);
1197         OUTPUT_FIELD(fpi[3], R300_PFS_INSTR3_0, inst3);
1198         #undef OUTPUT_FIELD
1199
1200         R300_STATECHANGE(rmesa, fp);
1201         for(i=0;i<4;i++){
1202                 rmesa->hw.fp.cmd[R300_FP_NODE0+i]=
1203                 (rmesa->state.pixel_shader.program.node[i].alu_offset << R300_PFS_NODE_ALU_OFFSET_SHIFT)
1204                 | (rmesa->state.pixel_shader.program.node[i].alu_end  << R300_PFS_NODE_ALU_END_SHIFT)
1205                 | (rmesa->state.pixel_shader.program.node[i].tex_offset << R300_PFS_NODE_TEX_OFFSET_SHIFT)
1206                 | (rmesa->state.pixel_shader.program.node[i].tex_end  << R300_PFS_NODE_TEX_END_SHIFT)
1207                 | ( (i==3) ? R300_PFS_NODE_LAST_NODE : 0);
1208                 }
1209
1210                 /*  PFS_CNTL_0 */
1211         rmesa->hw.fp.cmd[R300_FP_CNTL0]=
1212                 (rmesa->state.pixel_shader.program.active_nodes-1) 
1213                 | (rmesa->state.pixel_shader.program.first_node_has_tex<<3);
1214                 /* PFS_CNTL_1 */
1215         rmesa->hw.fp.cmd[R300_FP_CNTL1]=rmesa->state.pixel_shader.program.temp_register_count;
1216                 /* PFS_CNTL_2 */
1217         rmesa->hw.fp.cmd[R300_FP_CNTL2]=
1218                 (rmesa->state.pixel_shader.program.alu_offset << R300_PFS_CNTL_ALU_OFFSET_SHIFT)
1219                 | (rmesa->state.pixel_shader.program.alu_end << R300_PFS_CNTL_ALU_END_SHIFT)
1220                 | (rmesa->state.pixel_shader.program.tex_offset << R300_PFS_CNTL_TEX_OFFSET_SHIFT)
1221                 | (rmesa->state.pixel_shader.program.tex_end << R300_PFS_CNTL_TEX_END_SHIFT);
1222         
1223         R300_STATECHANGE(rmesa, fpp);
1224         for(i=0;i<rmesa->state.pixel_shader.param_length;i++){
1225                 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+0]=r300PackFloat32(rmesa->state.pixel_shader.param[i].x);
1226                 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+1]=r300PackFloat32(rmesa->state.pixel_shader.param[i].y);
1227                 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+2]=r300PackFloat32(rmesa->state.pixel_shader.param[i].z);
1228                 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+3]=r300PackFloat32(rmesa->state.pixel_shader.param[i].w);
1229                 }
1230         rmesa->hw.fpp.cmd[R300_FPP_CMD_0]=cmducs(R300_PFS_PARAM_0_X, rmesa->state.pixel_shader.param_length);
1231
1232 }
1233
1234 /**
1235  * Called by Mesa after an internal state update.
1236  */
1237 static void r300InvalidateState(GLcontext * ctx, GLuint new_state)
1238 {
1239         r300ContextPtr r300 = R300_CONTEXT(ctx);
1240
1241         _swrast_InvalidateState(ctx, new_state);
1242         _swsetup_InvalidateState(ctx, new_state);
1243         _ac_InvalidateState(ctx, new_state);
1244         _tnl_InvalidateState(ctx, new_state);
1245         _ae_invalidate_state(ctx, new_state);
1246
1247         /* Go inefficiency! */
1248         r300ResetHwState(r300);
1249 }
1250
1251
1252 /**
1253  * Completely recalculates hardware state based on the Mesa state.
1254  */
1255 void r300ResetHwState(r300ContextPtr r300)
1256 {
1257         GLcontext* ctx = r300->radeon.glCtx;
1258         int i;
1259
1260         if (RADEON_DEBUG & DEBUG_STATE)
1261                 fprintf(stderr, "%s\n", __FUNCTION__);
1262
1263         r300UpdateWindow(ctx);
1264         
1265         r300ColorMask(ctx,
1266                 ctx->Color.ColorMask[RCOMP],
1267                 ctx->Color.ColorMask[GCOMP],
1268                 ctx->Color.ColorMask[BCOMP],
1269                 ctx->Color.ColorMask[ACOMP]);
1270
1271         r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
1272         r300DepthMask(ctx, ctx->Depth.Mask);
1273         r300DepthFunc(ctx, ctx->Depth.Func);
1274
1275         r300UpdateCulling(ctx);
1276         
1277         r300_setup_routing(ctx, GL_TRUE);
1278         
1279         r300UpdateTextureState(ctx);
1280         r300_setup_textures(ctx);
1281         r300_setup_rs_unit(ctx);
1282         
1283         r300SetupVertexShader(r300);
1284         r300SetupPixelShader(r300);
1285         
1286         r300_set_blend_state(ctx);
1287         r300AlphaFunc(ctx, ctx->Color.AlphaFunc, ctx->Color.AlphaRef);
1288
1289 //BEGIN: TODO
1290         r300->hw.unk2080.cmd[1] = 0x0030045A;
1291
1292         r300->hw.ovf.cmd[R300_OVF_FMT_0] = 0x00000003;
1293         r300->hw.ovf.cmd[R300_OVF_FMT_1] = 0x00000000;
1294
1295         r300->hw.vte.cmd[1] = R300_VPORT_X_SCALE_ENA
1296                                 | R300_VPORT_X_OFFSET_ENA
1297                                 | R300_VPORT_Y_SCALE_ENA
1298                                 | R300_VPORT_Y_OFFSET_ENA
1299                                 | R300_VPORT_Z_SCALE_ENA
1300                                 | R300_VPORT_Z_OFFSET_ENA
1301                                 | R300_VTX_W0_FMT;
1302         r300->hw.vte.cmd[2] = 0x00000008;
1303
1304         r300->hw.unk2134.cmd[1] = 0x00FFFFFF;
1305         r300->hw.unk2134.cmd[2] = 0x00000000;
1306
1307         r300->hw.unk2140.cmd[1] = 0x00000000;
1308
1309         #if 0 /* Done in setup routing */
1310         ((drm_r300_cmd_header_t*)r300->hw.vir[0].cmd)->unchecked_state.count = 1;
1311         r300->hw.vir[0].cmd[1] = 0x21030003;
1312
1313         ((drm_r300_cmd_header_t*)r300->hw.vir[1].cmd)->unchecked_state.count = 1;
1314         r300->hw.vir[1].cmd[1] = 0xF688F688;
1315
1316         r300->hw.vic.cmd[R300_VIR_CNTL_0] = 0x00000001;
1317         r300->hw.vic.cmd[R300_VIR_CNTL_1] = 0x00000405;
1318         #endif
1319         
1320         r300->hw.unk21DC.cmd[1] = 0xAAAAAAAA;
1321
1322         r300->hw.unk221C.cmd[1] = R300_221C_NORMAL;
1323
1324         r300->hw.unk2220.cmd[1] = r300PackFloat32(1.0);
1325         r300->hw.unk2220.cmd[2] = r300PackFloat32(1.0);
1326         r300->hw.unk2220.cmd[3] = r300PackFloat32(1.0);
1327         r300->hw.unk2220.cmd[4] = r300PackFloat32(1.0);
1328
1329         if (GET_CHIP(r300->radeon.radeonScreen) == RADEON_CHIP_R300)
1330                 r300->hw.unk2288.cmd[1] = R300_2288_R300;
1331         else
1332                 r300->hw.unk2288.cmd[1] = R300_2288_RV350;
1333
1334         #if 0
1335         r300->hw.vof.cmd[R300_VOF_CNTL_0] = R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
1336                                 | R300_VAP_OUTPUT_VTX_FMT_0__COLOR_PRESENT;
1337         r300->hw.vof.cmd[R300_VOF_CNTL_1] = 0; /* no textures */
1338         
1339         
1340         r300->hw.pvs.cmd[R300_PVS_CNTL_1] = 0;
1341         r300->hw.pvs.cmd[R300_PVS_CNTL_2] = 0;
1342         r300->hw.pvs.cmd[R300_PVS_CNTL_3] = 0;
1343         #endif  
1344
1345         r300->hw.gb_enable.cmd[1] = R300_GB_POINT_STUFF_ENABLE
1346                 | R300_GB_LINE_STUFF_ENABLE
1347                 | R300_GB_TRIANGLE_STUFF_ENABLE;
1348
1349         r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_0] = 0x66666666;
1350         r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_1] = 0x06666666;
1351         if (GET_CHIP(r300->radeon.radeonScreen) == RADEON_CHIP_R300)
1352                 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] = R300_GB_TILE_ENABLE
1353                                                         | R300_GB_TILE_PIPE_COUNT_R300
1354                                                         | R300_GB_TILE_SIZE_16;
1355         else
1356                 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] = R300_GB_TILE_ENABLE
1357                                                         | R300_GB_TILE_PIPE_COUNT_RV300
1358                                                         | R300_GB_TILE_SIZE_16;
1359         r300->hw.gb_misc.cmd[R300_GB_MISC_SELECT] = 0x00000000;
1360         r300->hw.gb_misc.cmd[R300_GB_MISC_AA_CONFIG] = 0x00000000; /* No antialiasing */
1361
1362         //r300->hw.txe.cmd[R300_TXE_ENABLE] = 0;
1363
1364         r300->hw.unk4200.cmd[1] = r300PackFloat32(0.0);
1365         r300->hw.unk4200.cmd[2] = r300PackFloat32(0.0);
1366         r300->hw.unk4200.cmd[3] = r300PackFloat32(1.0);
1367         r300->hw.unk4200.cmd[4] = r300PackFloat32(1.0);
1368
1369         r300->hw.unk4214.cmd[1] = 0x00050005;
1370
1371         r300->hw.ps.cmd[R300_PS_POINTSIZE] = (6 << R300_POINTSIZE_X_SHIFT) |
1372                                              (6 << R300_POINTSIZE_Y_SHIFT);
1373
1374         r300->hw.unk4230.cmd[1] = 0x01800000;
1375         r300->hw.unk4230.cmd[2] = 0x00020006;
1376         r300->hw.unk4230.cmd[3] = r300PackFloat32(1.0 / 192.0);
1377
1378         r300->hw.unk4260.cmd[1] = 0;
1379         r300->hw.unk4260.cmd[2] = r300PackFloat32(0.0);
1380         r300->hw.unk4260.cmd[3] = r300PackFloat32(1.0);
1381
1382         r300->hw.unk4274.cmd[1] = 0x00000002;
1383         r300->hw.unk4274.cmd[2] = 0x0003AAAA;
1384         r300->hw.unk4274.cmd[3] = 0x00000000;
1385         r300->hw.unk4274.cmd[4] = 0x00000000;
1386
1387         r300->hw.unk4288.cmd[1] = 0x00000000;
1388         r300->hw.unk4288.cmd[2] = 0x00000001;
1389         r300->hw.unk4288.cmd[3] = 0x00000000;
1390         r300->hw.unk4288.cmd[4] = 0x00000000;
1391         r300->hw.unk4288.cmd[5] = 0x00000000;
1392
1393         r300->hw.unk42A0.cmd[1] = 0x00000000;
1394
1395         r300->hw.unk42B4.cmd[1] = 0x00000000;
1396
1397         r300->hw.unk42C0.cmd[1] = 0x4B7FFFFF;
1398         r300->hw.unk42C0.cmd[2] = 0x00000000;
1399
1400
1401         r300->hw.unk43A4.cmd[1] = 0x0000001C;
1402         r300->hw.unk43A4.cmd[2] = 0x2DA49525;
1403
1404         r300->hw.unk43E8.cmd[1] = 0x00FFFFFF;
1405
1406         #if 0
1407         r300->hw.fp.cmd[R300_FP_CNTL0] = 0;
1408         r300->hw.fp.cmd[R300_FP_CNTL1] = 0;
1409         r300->hw.fp.cmd[R300_FP_CNTL2] = 0;
1410         r300->hw.fp.cmd[R300_FP_NODE0] = 0;
1411         r300->hw.fp.cmd[R300_FP_NODE1] = 0;
1412         r300->hw.fp.cmd[R300_FP_NODE2] = 0;
1413         r300->hw.fp.cmd[R300_FP_NODE3] = 0;
1414         #endif
1415         
1416         r300->hw.unk46A4.cmd[1] = 0x00001B01;
1417         r300->hw.unk46A4.cmd[2] = 0x00001B0F;
1418         r300->hw.unk46A4.cmd[3] = 0x00001B0F;
1419         r300->hw.unk46A4.cmd[4] = 0x00001B0F;
1420         r300->hw.unk46A4.cmd[5] = 0x00000001;
1421
1422         #if 0
1423         for(i = 1; i <= 64; ++i) {
1424                 /* create NOP instructions */
1425                 r300->hw.fpi[0].cmd[i] = FP_INSTRC(MAD, FP_ARGC(SRC0C_XYZ), FP_ARGC(ONE), FP_ARGC(ZERO));
1426                 r300->hw.fpi[1].cmd[i] = FP_SELC(0,XYZ,NO,FP_TMP(0),0,0);
1427                 r300->hw.fpi[2].cmd[i] = FP_INSTRA(MAD, FP_ARGA(SRC0A), FP_ARGA(ONE), FP_ARGA(ZERO));
1428                 r300->hw.fpi[3].cmd[i] = FP_SELA(0,W,NO,FP_TMP(0),0,0);
1429         }
1430         #endif
1431         
1432         r300->hw.unk4BC0.cmd[1] = 0;
1433
1434         r300->hw.unk4BC8.cmd[1] = 0;
1435         r300->hw.unk4BC8.cmd[2] = 0;
1436         r300->hw.unk4BC8.cmd[3] = 0;
1437
1438         #if 0
1439         r300->hw.at.cmd[R300_AT_ALPHA_TEST] = 0;
1440         #endif
1441
1442         r300->hw.unk4BD8.cmd[1] = 0;
1443
1444         r300->hw.unk4E00.cmd[1] = 0;
1445
1446         #if 0
1447         r300->hw.bld.cmd[R300_BLD_CBLEND] = 0;
1448         r300->hw.bld.cmd[R300_BLD_ABLEND] = 0;
1449         #endif
1450
1451         r300->hw.unk4E10.cmd[1] = 0;
1452         r300->hw.unk4E10.cmd[2] = 0;
1453         r300->hw.unk4E10.cmd[3] = 0;
1454
1455         r300->hw.cb.cmd[R300_CB_OFFSET] =
1456                 r300->radeon.radeonScreen->backOffset +
1457                 r300->radeon.radeonScreen->fbLocation;
1458         r300->hw.cb.cmd[R300_CB_PITCH] = r300->radeon.radeonScreen->backPitch
1459                 | R300_COLOR_UNKNOWN_22_23;
1460
1461         r300->hw.unk4E50.cmd[1] = 0;
1462         r300->hw.unk4E50.cmd[2] = 0;
1463         r300->hw.unk4E50.cmd[3] = 0;
1464         r300->hw.unk4E50.cmd[4] = 0;
1465         r300->hw.unk4E50.cmd[5] = 0;
1466         r300->hw.unk4E50.cmd[6] = 0;
1467         r300->hw.unk4E50.cmd[7] = 0;
1468         r300->hw.unk4E50.cmd[8] = 0;
1469         r300->hw.unk4E50.cmd[9] = 0;
1470
1471         r300->hw.unk4E88.cmd[1] = 0;
1472
1473         r300->hw.unk4EA0.cmd[1] = 0x00000000;
1474         r300->hw.unk4EA0.cmd[2] = 0xffffffff;
1475
1476         r300->hw.unk4F08.cmd[1] = 0x00FFFF00;
1477
1478         r300->hw.unk4F10.cmd[1] = 0x00000002; // depthbuffer format?
1479         r300->hw.unk4F10.cmd[2] = 0x00000000;
1480         r300->hw.unk4F10.cmd[3] = 0x00000003;
1481         r300->hw.unk4F10.cmd[4] = 0x00000000;
1482
1483         r300->hw.zb.cmd[R300_ZB_OFFSET] =
1484                 r300->radeon.radeonScreen->depthOffset +
1485                 r300->radeon.radeonScreen->fbLocation;
1486         r300->hw.zb.cmd[R300_ZB_PITCH] = r300->radeon.radeonScreen->depthPitch;
1487
1488         r300->hw.unk4F28.cmd[1] = 0;
1489
1490         r300->hw.unk4F30.cmd[1] = 0;
1491         r300->hw.unk4F30.cmd[2] = 0;
1492
1493         r300->hw.unk4F44.cmd[1] = 0;
1494
1495         r300->hw.unk4F54.cmd[1] = 0;
1496
1497         #if 0
1498         ((drm_r300_cmd_header_t*)r300->hw.vpi.cmd)->vpu.count = 0;
1499         for(i = 1; i < R300_VPI_CMDSIZE; i += 4) {
1500                 /* MOV t0, t0 */
1501                 r300->hw.vpi.cmd[i+0] = VP_OUT(ADD,TMP,0,XYZW);
1502                 r300->hw.vpi.cmd[i+1] = VP_IN(TMP,0);
1503                 r300->hw.vpi.cmd[i+2] = VP_ZERO();
1504                 r300->hw.vpi.cmd[i+3] = VP_ZERO();
1505         }
1506
1507         ((drm_r300_cmd_header_t*)r300->hw.vpp.cmd)->vpu.count = 0;
1508         for(i = 1; i < R300_VPP_CMDSIZE; ++i)
1509                 r300->hw.vpp.cmd[i] = 0;
1510         #endif
1511                 
1512         r300->hw.vps.cmd[R300_VPS_ZERO_0] = 0;
1513         r300->hw.vps.cmd[R300_VPS_ZERO_1] = 0;
1514         r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
1515         r300->hw.vps.cmd[R300_VPS_ZERO_3] = 0;
1516         
1517 //END: TODO
1518         
1519         r300->hw.all_dirty = GL_TRUE;
1520 }
1521
1522
1523
1524 /**
1525  * Calculate initial hardware state and register state functions.
1526  * Assumes that the command buffer and state atoms have been
1527  * initialized already.
1528  */
1529 void r300InitState(r300ContextPtr r300)
1530 {
1531         GLcontext *ctx = r300->radeon.glCtx;
1532         GLuint depth_fmt;
1533
1534         radeonInitState(&r300->radeon);
1535         
1536         switch (ctx->Visual.depthBits) {
1537         case 16:
1538                 r300->state.depth.scale = 1.0 / (GLfloat) 0xffff;
1539                 depth_fmt = R200_DEPTH_FORMAT_16BIT_INT_Z;
1540                 //r300->state.stencil.clear = 0x00000000;
1541                 break;
1542         case 24:
1543                 r300->state.depth.scale = 1.0 / (GLfloat) 0xffffff;
1544                 depth_fmt = R200_DEPTH_FORMAT_24BIT_INT_Z;
1545                 //r300->state.stencil.clear = 0xff000000;
1546                 break;
1547         default:
1548                 fprintf(stderr, "Error: Unsupported depth %d... exiting\n",
1549                         ctx->Visual.depthBits);
1550                 exit(-1);
1551         }
1552         
1553         memset(&(r300->state.texture), 0, sizeof(r300->state.texture));
1554
1555         r300ResetHwState(r300);
1556 }
1557
1558
1559
1560 /**
1561  * Initialize driver's state callback functions
1562  */
1563 void r300InitStateFuncs(struct dd_function_table* functions)
1564 {
1565         radeonInitStateFuncs(functions);
1566
1567         functions->UpdateState = r300InvalidateState;
1568         //functions->AlphaFunc = r300AlphaFunc;
1569         functions->BlendColor = r300BlendColor;
1570         functions->BlendEquationSeparate = r300BlendEquationSeparate;
1571         functions->BlendFuncSeparate = r300BlendFuncSeparate;
1572         functions->Enable = r300Enable;
1573         functions->ColorMask = r300ColorMask;
1574         functions->DepthFunc = r300DepthFunc;
1575         functions->DepthMask = r300DepthMask;
1576         functions->CullFace = r300CullFace;
1577         functions->FrontFace = r300FrontFace;
1578
1579         /* Viewport related */
1580         functions->Viewport = r300Viewport;
1581         functions->DepthRange = r300DepthRange;
1582         functions->PointSize = r300PointSize;
1583 }
1584