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