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