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