Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / radeon / radeon_state_init.c
1 /*
2  * Copyright 2000, 2001 VA Linux Systems Inc., Fremont, California.
3  *
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * on the rights to use, copy, modify, merge, publish, distribute, sub
10  * license, and/or sell copies of the Software, and to permit persons to whom
11  * the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Gareth Hughes <gareth@valinux.com>
27  *    Keith Whitwell <keith@tungstengraphics.com>
28  */
29
30 #include "main/glheader.h"
31 #include "main/imports.h"
32 #include "main/api_arrayelt.h"
33
34 #include "swrast/swrast.h"
35 #include "vbo/vbo.h"
36 #include "tnl/t_pipeline.h"
37 #include "swrast_setup/swrast_setup.h"
38
39 #include "radeon_context.h"
40 #include "radeon_mipmap_tree.h"
41 #include "radeon_ioctl.h"
42 #include "radeon_state.h"
43 #include "radeon_queryobj.h"
44
45 #include "../r200/r200_reg.h"
46
47 #include "xmlpool.h"
48
49 /* New (1.3) state mechanism.  3 commands (packet, scalar, vector) in
50  * 1.3 cmdbuffers allow all previous state to be updated as well as
51  * the tcl scalar and vector areas.
52  */
53 static struct {
54         int start;
55         int len;
56         const char *name;
57 } packet[RADEON_MAX_STATE_PACKETS] = {
58         {RADEON_PP_MISC, 7, "RADEON_PP_MISC"},
59         {RADEON_PP_CNTL, 3, "RADEON_PP_CNTL"},
60         {RADEON_RB3D_COLORPITCH, 1, "RADEON_RB3D_COLORPITCH"},
61         {RADEON_RE_LINE_PATTERN, 2, "RADEON_RE_LINE_PATTERN"},
62         {RADEON_SE_LINE_WIDTH, 1, "RADEON_SE_LINE_WIDTH"},
63         {RADEON_PP_LUM_MATRIX, 1, "RADEON_PP_LUM_MATRIX"},
64         {RADEON_PP_ROT_MATRIX_0, 2, "RADEON_PP_ROT_MATRIX_0"},
65         {RADEON_RB3D_STENCILREFMASK, 3, "RADEON_RB3D_STENCILREFMASK"},
66         {RADEON_SE_VPORT_XSCALE, 6, "RADEON_SE_VPORT_XSCALE"},
67         {RADEON_SE_CNTL, 2, "RADEON_SE_CNTL"},
68         {RADEON_SE_CNTL_STATUS, 1, "RADEON_SE_CNTL_STATUS"},
69         {RADEON_RE_MISC, 1, "RADEON_RE_MISC"},
70         {RADEON_PP_TXFILTER_0, 6, "RADEON_PP_TXFILTER_0"},
71         {RADEON_PP_BORDER_COLOR_0, 1, "RADEON_PP_BORDER_COLOR_0"},
72         {RADEON_PP_TXFILTER_1, 6, "RADEON_PP_TXFILTER_1"},
73         {RADEON_PP_BORDER_COLOR_1, 1, "RADEON_PP_BORDER_COLOR_1"},
74         {RADEON_PP_TXFILTER_2, 6, "RADEON_PP_TXFILTER_2"},
75         {RADEON_PP_BORDER_COLOR_2, 1, "RADEON_PP_BORDER_COLOR_2"},
76         {RADEON_SE_ZBIAS_FACTOR, 2, "RADEON_SE_ZBIAS_FACTOR"},
77         {RADEON_SE_TCL_OUTPUT_VTX_FMT, 11, "RADEON_SE_TCL_OUTPUT_VTX_FMT"},
78         {RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED, 17,
79                     "RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED"},
80         {R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0"},
81         {R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1"},
82         {R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2"},
83         {R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3"},
84         {R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4"},
85         {R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5"},
86         {R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6"},
87         {R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7"},
88         {R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0"},
89         {R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0"},
90         {R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0"},
91         {R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL"},
92         {R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0"},
93         {R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2"},
94         {R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL"},
95         {R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0"},
96         {R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1"},
97         {R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2"},
98         {R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3"},
99         {R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4"},
100         {R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5"},
101         {R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0"},
102         {R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1"},
103         {R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2"},
104         {R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3"},
105         {R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4"},
106         {R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5"},
107         {R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL"},
108         {R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1,
109          "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
110         {R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3"},
111         {R200_PP_CNTL_X, 1, "R200_PP_CNTL_X"},
112         {R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET"},
113         {R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL"},
114         {R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0"},
115         {R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1"},
116         {R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2"},
117         {R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS"},
118         {R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL"},
119         {R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE"},
120         {R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4,
121                     "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0"},
122         {R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0"},    /* 61 */
123         {R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"}, /* 62 */
124         {R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1"},
125         {R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1"},
126         {R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2"},
127         {R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2"},
128         {R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3"},
129         {R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3"},
130         {R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4"},
131         {R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4"},
132         {R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5"},
133         {R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5"},
134         {RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0"},
135         {RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1"},
136         {RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2"},
137         {R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR"},
138         {R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL"},
139         {RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
140         {RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
141         {RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
142         {RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
143         {RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
144         {RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
145         {R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
146         {R200_PP_TXCBLEND_8, 32, "R200_PP_AFS_0"},     /* 85 */
147         {R200_PP_TXCBLEND_0, 32, "R200_PP_AFS_1"},
148         {R200_PP_TFACTOR_0, 8, "R200_ATF_TFACTOR"},
149         {R200_PP_TXFILTER_0, 8, "R200_PP_TXCTLALL_0"},
150         {R200_PP_TXFILTER_1, 8, "R200_PP_TXCTLALL_1"},
151         {R200_PP_TXFILTER_2, 8, "R200_PP_TXCTLALL_2"},
152         {R200_PP_TXFILTER_3, 8, "R200_PP_TXCTLALL_3"},
153         {R200_PP_TXFILTER_4, 8, "R200_PP_TXCTLALL_4"},
154         {R200_PP_TXFILTER_5, 8, "R200_PP_TXCTLALL_5"},
155         {R200_VAP_PVS_CNTL_1, 2, "R200_VAP_PVS_CNTL"},
156 };
157
158 /* =============================================================
159  * State initialization
160  */
161 static int cmdpkt( r100ContextPtr rmesa, int id ) 
162 {
163    drm_radeon_cmd_header_t h;
164
165    if (rmesa->radeon.radeonScreen->kernel_mm) {
166      return CP_PACKET0(packet[id].start, packet[id].len - 1);
167    } else {
168      h.i = 0;
169      h.packet.cmd_type = RADEON_CMD_PACKET;
170      h.packet.packet_id = id;
171    }
172    return h.i;
173 }
174
175 static int cmdvec( int offset, int stride, int count ) 
176 {
177    drm_radeon_cmd_header_t h;
178    h.i = 0;
179    h.vectors.cmd_type = RADEON_CMD_VECTORS;
180    h.vectors.offset = offset;
181    h.vectors.stride = stride;
182    h.vectors.count = count;
183    return h.i;
184 }
185
186 static int cmdscl( int offset, int stride, int count ) 
187 {
188    drm_radeon_cmd_header_t h;
189    h.i = 0;
190    h.scalars.cmd_type = RADEON_CMD_SCALARS;
191    h.scalars.offset = offset;
192    h.scalars.stride = stride;
193    h.scalars.count = count;
194    return h.i;
195 }
196
197 #define CHECK( NM, FLAG, ADD )                          \
198 static int check_##NM( struct gl_context *ctx, struct radeon_state_atom *atom ) \
199 {                                                       \
200    return FLAG ? atom->cmd_size + (ADD) : 0;                    \
201 }
202
203 #define TCL_CHECK( NM, FLAG, ADD )                              \
204 static int check_##NM( struct gl_context *ctx, struct radeon_state_atom *atom ) \
205 {                                                       \
206    r100ContextPtr rmesa = R100_CONTEXT(ctx);    \
207    return (!rmesa->radeon.TclFallback && (FLAG)) ? atom->cmd_size + (ADD) : 0;  \
208 }
209
210
211 CHECK( always, GL_TRUE, 0 )
212 CHECK( always_add2, GL_TRUE, 2 )
213 CHECK( always_add4, GL_TRUE, 4 )
214 CHECK( never, GL_FALSE, 0 )
215 CHECK( tex0_mm, ctx->Texture.Unit[0]._ReallyEnabled, 3 )
216 CHECK( tex1_mm, ctx->Texture.Unit[1]._ReallyEnabled, 3 )
217 /* need this for the cubic_map on disabled unit 2 bug, maybe r100 only? */
218 CHECK( tex2_mm, ctx->Texture._EnabledUnits, 3 )
219 CHECK( tex0, ctx->Texture.Unit[0]._ReallyEnabled, 2 )
220 CHECK( tex1, ctx->Texture.Unit[1]._ReallyEnabled, 2 )
221 CHECK( tex2, ctx->Texture._EnabledUnits, 2 )
222 CHECK( cube0, (ctx->Texture.Unit[0]._ReallyEnabled & TEXTURE_CUBE_BIT), 3 + 3*5 - CUBE_STATE_SIZE )
223 CHECK( cube1, (ctx->Texture.Unit[1]._ReallyEnabled & TEXTURE_CUBE_BIT), 3 + 3*5 - CUBE_STATE_SIZE )
224 CHECK( cube2, (ctx->Texture.Unit[2]._ReallyEnabled & TEXTURE_CUBE_BIT), 3 + 3*5 - CUBE_STATE_SIZE )
225 CHECK( cube0_mm, (ctx->Texture.Unit[0]._ReallyEnabled & TEXTURE_CUBE_BIT), 2 + 4*5 - CUBE_STATE_SIZE )
226 CHECK( cube1_mm, (ctx->Texture.Unit[1]._ReallyEnabled & TEXTURE_CUBE_BIT), 2 + 4*5 - CUBE_STATE_SIZE )
227 CHECK( cube2_mm, (ctx->Texture.Unit[2]._ReallyEnabled & TEXTURE_CUBE_BIT), 2 + 4*5 - CUBE_STATE_SIZE )
228 CHECK( fog, ctx->Fog.Enabled, 0 )
229 CHECK( fog_add4, ctx->Fog.Enabled, 4 )
230 TCL_CHECK( tcl, GL_TRUE, 0 )
231 TCL_CHECK( tcl_add4, GL_TRUE, 4 )
232 TCL_CHECK( tcl_tex0, ctx->Texture.Unit[0]._ReallyEnabled, 0 )
233 TCL_CHECK( tcl_tex1, ctx->Texture.Unit[1]._ReallyEnabled, 0 )
234 TCL_CHECK( tcl_tex2, ctx->Texture.Unit[2]._ReallyEnabled, 0 )
235 TCL_CHECK( tcl_tex0_add4, ctx->Texture.Unit[0]._ReallyEnabled, 4 )
236 TCL_CHECK( tcl_tex1_add4, ctx->Texture.Unit[1]._ReallyEnabled, 4 )
237 TCL_CHECK( tcl_tex2_add4, ctx->Texture.Unit[2]._ReallyEnabled, 4 )
238 TCL_CHECK( tcl_lighting, ctx->Light.Enabled, 0 )
239 TCL_CHECK( tcl_lighting_add4, ctx->Light.Enabled, 4 )
240 TCL_CHECK( tcl_eyespace_or_lighting, ctx->_NeedEyeCoords || ctx->Light.Enabled, 0 )
241 TCL_CHECK( tcl_eyespace_or_lighting_add4, ctx->_NeedEyeCoords || ctx->Light.Enabled, 4 )
242 TCL_CHECK( tcl_lit0, ctx->Light.Enabled && ctx->Light.Light[0].Enabled, 0 )
243 TCL_CHECK( tcl_lit1, ctx->Light.Enabled && ctx->Light.Light[1].Enabled, 0 )
244 TCL_CHECK( tcl_lit2, ctx->Light.Enabled && ctx->Light.Light[2].Enabled, 0 )
245 TCL_CHECK( tcl_lit3, ctx->Light.Enabled && ctx->Light.Light[3].Enabled, 0 )
246 TCL_CHECK( tcl_lit4, ctx->Light.Enabled && ctx->Light.Light[4].Enabled, 0 )
247 TCL_CHECK( tcl_lit5, ctx->Light.Enabled && ctx->Light.Light[5].Enabled, 0 )
248 TCL_CHECK( tcl_lit6, ctx->Light.Enabled && ctx->Light.Light[6].Enabled, 0 )
249 TCL_CHECK( tcl_lit7, ctx->Light.Enabled && ctx->Light.Light[7].Enabled, 0 )
250 TCL_CHECK( tcl_lit0_add6, ctx->Light.Enabled && ctx->Light.Light[0].Enabled, 6 )
251 TCL_CHECK( tcl_lit1_add6, ctx->Light.Enabled && ctx->Light.Light[1].Enabled, 6 )
252 TCL_CHECK( tcl_lit2_add6, ctx->Light.Enabled && ctx->Light.Light[2].Enabled, 6 )
253 TCL_CHECK( tcl_lit3_add6, ctx->Light.Enabled && ctx->Light.Light[3].Enabled, 6 )
254 TCL_CHECK( tcl_lit4_add6, ctx->Light.Enabled && ctx->Light.Light[4].Enabled, 6 )
255 TCL_CHECK( tcl_lit5_add6, ctx->Light.Enabled && ctx->Light.Light[5].Enabled, 6 )
256 TCL_CHECK( tcl_lit6_add6, ctx->Light.Enabled && ctx->Light.Light[6].Enabled, 6 )
257 TCL_CHECK( tcl_lit7_add6, ctx->Light.Enabled && ctx->Light.Light[7].Enabled, 6 )
258 TCL_CHECK( tcl_ucp0, (ctx->Transform.ClipPlanesEnabled & 0x1), 0 )
259 TCL_CHECK( tcl_ucp1, (ctx->Transform.ClipPlanesEnabled & 0x2), 0 )
260 TCL_CHECK( tcl_ucp2, (ctx->Transform.ClipPlanesEnabled & 0x4), 0 )
261 TCL_CHECK( tcl_ucp3, (ctx->Transform.ClipPlanesEnabled & 0x8), 0 )
262 TCL_CHECK( tcl_ucp4, (ctx->Transform.ClipPlanesEnabled & 0x10), 0 )
263 TCL_CHECK( tcl_ucp5, (ctx->Transform.ClipPlanesEnabled & 0x20), 0 )
264 TCL_CHECK( tcl_ucp0_add4, (ctx->Transform.ClipPlanesEnabled & 0x1), 4 )
265 TCL_CHECK( tcl_ucp1_add4, (ctx->Transform.ClipPlanesEnabled & 0x2), 4 )
266 TCL_CHECK( tcl_ucp2_add4, (ctx->Transform.ClipPlanesEnabled & 0x4), 4 )
267 TCL_CHECK( tcl_ucp3_add4, (ctx->Transform.ClipPlanesEnabled & 0x8), 4 )
268 TCL_CHECK( tcl_ucp4_add4, (ctx->Transform.ClipPlanesEnabled & 0x10), 4 )
269 TCL_CHECK( tcl_ucp5_add4, (ctx->Transform.ClipPlanesEnabled & 0x20), 4 )
270 TCL_CHECK( tcl_eyespace_or_fog, ctx->_NeedEyeCoords || ctx->Fog.Enabled, 0 )
271 TCL_CHECK( tcl_eyespace_or_fog_add4, ctx->_NeedEyeCoords || ctx->Fog.Enabled, 4 )
272
273 CHECK( txr0, (ctx->Texture.Unit[0]._ReallyEnabled & TEXTURE_RECT_BIT), 0 )
274 CHECK( txr1, (ctx->Texture.Unit[1]._ReallyEnabled & TEXTURE_RECT_BIT), 0 )
275 CHECK( txr2, (ctx->Texture.Unit[2]._ReallyEnabled & TEXTURE_RECT_BIT), 0 )
276
277 #define OUT_VEC(hdr, data) do {                 \
278     drm_radeon_cmd_header_t h;                                  \
279     h.i = hdr;                                                          \
280     OUT_BATCH(CP_PACKET0(RADEON_SE_TCL_STATE_FLUSH, 0));                \
281     OUT_BATCH(0);                                                       \
282     OUT_BATCH(CP_PACKET0(R200_SE_TCL_VECTOR_INDX_REG, 0));              \
283     OUT_BATCH(h.vectors.offset | (h.vectors.stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT)); \
284     OUT_BATCH(CP_PACKET0_ONE(R200_SE_TCL_VECTOR_DATA_REG, h.vectors.count - 1));        \
285     OUT_BATCH_TABLE((data), h.vectors.count);                           \
286   } while(0)
287
288 #define OUT_SCL(hdr, data) do {                                 \
289     drm_radeon_cmd_header_t h;                                          \
290     h.i = hdr;                                                          \
291     OUT_BATCH(CP_PACKET0(R200_SE_TCL_SCALAR_INDX_REG, 0));              \
292     OUT_BATCH((h.scalars.offset) | (h.scalars.stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT)); \
293     OUT_BATCH(CP_PACKET0_ONE(R200_SE_TCL_SCALAR_DATA_REG, h.scalars.count - 1));        \
294     OUT_BATCH_TABLE((data), h.scalars.count);                           \
295   } while(0)
296
297 static void scl_emit(struct gl_context *ctx, struct radeon_state_atom *atom)
298 {
299    r100ContextPtr r100 = R100_CONTEXT(ctx);
300    BATCH_LOCALS(&r100->radeon);
301    uint32_t dwords = atom->check(ctx, atom);
302    
303    BEGIN_BATCH_NO_AUTOSTATE(dwords);
304    OUT_SCL(atom->cmd[0], atom->cmd+1);
305    END_BATCH();
306 }
307
308
309 static void vec_emit(struct gl_context *ctx, struct radeon_state_atom *atom)
310 {
311    r100ContextPtr r100 = R100_CONTEXT(ctx);
312    BATCH_LOCALS(&r100->radeon);
313    uint32_t dwords = atom->check(ctx, atom);
314
315    BEGIN_BATCH_NO_AUTOSTATE(dwords);
316    OUT_VEC(atom->cmd[0], atom->cmd+1);
317    END_BATCH();
318 }
319
320
321 static void lit_emit(struct gl_context *ctx, struct radeon_state_atom *atom)
322 {
323    r100ContextPtr r100 = R100_CONTEXT(ctx);
324    BATCH_LOCALS(&r100->radeon);
325    uint32_t dwords = atom->check(ctx, atom);
326
327    BEGIN_BATCH_NO_AUTOSTATE(dwords);
328    OUT_VEC(atom->cmd[LIT_CMD_0], atom->cmd+1);
329    OUT_SCL(atom->cmd[LIT_CMD_1], atom->cmd+LIT_CMD_1+1);
330    END_BATCH();
331 }
332
333 static void ctx_emit(struct gl_context *ctx, struct radeon_state_atom *atom)
334 {
335    r100ContextPtr r100 = R100_CONTEXT(ctx);
336    BATCH_LOCALS(&r100->radeon);
337    struct radeon_renderbuffer *rrb;
338    uint32_t cbpitch;
339    uint32_t zbpitch, depth_fmt;
340    uint32_t dwords = atom->check(ctx, atom);
341
342    /* output the first 7 bytes of context */
343    BEGIN_BATCH_NO_AUTOSTATE(dwords);
344    OUT_BATCH_TABLE(atom->cmd, 5);
345
346    rrb = radeon_get_depthbuffer(&r100->radeon);
347    if (!rrb) {
348      OUT_BATCH(0);
349      OUT_BATCH(0);
350    } else {
351      zbpitch = (rrb->pitch / rrb->cpp);
352      if (r100->using_hyperz)
353        zbpitch |= RADEON_DEPTH_HYPERZ;
354
355      OUT_BATCH_RELOC(0, rrb->bo, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
356      OUT_BATCH(zbpitch);
357      if (rrb->cpp == 4)
358         depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
359      else
360         depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
361      atom->cmd[CTX_RB3D_ZSTENCILCNTL] &= ~RADEON_DEPTH_FORMAT_MASK;
362      atom->cmd[CTX_RB3D_ZSTENCILCNTL] |= depth_fmt;
363    }
364      
365    OUT_BATCH(atom->cmd[CTX_RB3D_ZSTENCILCNTL]);
366    OUT_BATCH(atom->cmd[CTX_CMD_1]);
367    OUT_BATCH(atom->cmd[CTX_PP_CNTL]);
368
369    rrb = radeon_get_colorbuffer(&r100->radeon);
370    if (!rrb || !rrb->bo) {
371       OUT_BATCH(atom->cmd[CTX_RB3D_CNTL]);
372       OUT_BATCH(atom->cmd[CTX_RB3D_COLOROFFSET]);
373    } else {
374       atom->cmd[CTX_RB3D_CNTL] &= ~(0xf << 10);
375       if (rrb->cpp == 4)
376          atom->cmd[CTX_RB3D_CNTL] |= RADEON_COLOR_FORMAT_ARGB8888;
377       else
378          atom->cmd[CTX_RB3D_CNTL] |= RADEON_COLOR_FORMAT_RGB565;
379
380       OUT_BATCH(atom->cmd[CTX_RB3D_CNTL]);
381       OUT_BATCH_RELOC(0, rrb->bo, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
382    }
383
384    OUT_BATCH(atom->cmd[CTX_CMD_2]);
385
386    if (!rrb || !rrb->bo) {
387      OUT_BATCH(atom->cmd[CTX_RB3D_COLORPITCH]);
388    } else {
389      cbpitch = (rrb->pitch / rrb->cpp);
390      if (rrb->bo->flags & RADEON_BO_FLAGS_MACRO_TILE)
391        cbpitch |= RADEON_COLOR_TILE_ENABLE;
392      OUT_BATCH(cbpitch);
393    }
394
395    END_BATCH();
396 }
397
398 static int check_always_ctx( struct gl_context *ctx, struct radeon_state_atom *atom)
399 {
400    r100ContextPtr r100 = R100_CONTEXT(ctx);
401    struct radeon_renderbuffer *rrb, *drb;
402    uint32_t dwords;
403
404    rrb = radeon_get_colorbuffer(&r100->radeon);
405    if (!rrb || !rrb->bo) {
406       return 0;
407    }
408
409    drb = radeon_get_depthbuffer(&r100->radeon);
410
411    dwords = 10;
412    if (drb)
413      dwords += 6;
414    if (rrb)
415      dwords += 8;
416
417    return dwords;
418 }
419
420 static void ctx_emit_cs(struct gl_context *ctx, struct radeon_state_atom *atom)
421 {
422    r100ContextPtr r100 = R100_CONTEXT(ctx);
423    BATCH_LOCALS(&r100->radeon);
424    struct radeon_renderbuffer *rrb, *drb;
425    uint32_t cbpitch = 0;
426    uint32_t zbpitch = 0;
427    uint32_t dwords = atom->check(ctx, atom);
428    uint32_t depth_fmt;
429
430    rrb = radeon_get_colorbuffer(&r100->radeon);
431    if (!rrb || !rrb->bo) {
432       fprintf(stderr, "no rrb\n");
433       return;
434    }
435
436    atom->cmd[CTX_RB3D_CNTL] &= ~(0xf << 10);
437    if (rrb->cpp == 4)
438         atom->cmd[CTX_RB3D_CNTL] |= RADEON_COLOR_FORMAT_ARGB8888;
439    else switch (rrb->base.Format) {
440    case MESA_FORMAT_RGB565:
441         atom->cmd[CTX_RB3D_CNTL] |= RADEON_COLOR_FORMAT_RGB565;
442         break;
443    case MESA_FORMAT_ARGB4444:
444         atom->cmd[CTX_RB3D_CNTL] |= RADEON_COLOR_FORMAT_ARGB4444;
445         break;
446    case MESA_FORMAT_ARGB1555:
447         atom->cmd[CTX_RB3D_CNTL] |= RADEON_COLOR_FORMAT_ARGB1555;
448         break;
449    default:
450         _mesa_problem(ctx, "unexpected format in ctx_emit_cs()");
451    }
452
453    cbpitch = (rrb->pitch / rrb->cpp);
454    if (rrb->bo->flags & RADEON_BO_FLAGS_MACRO_TILE)
455        cbpitch |= R200_COLOR_TILE_ENABLE;
456
457    drb = radeon_get_depthbuffer(&r100->radeon);
458    if (drb) {
459      zbpitch = (drb->pitch / drb->cpp);
460      if (drb->cpp == 4)
461         depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
462      else
463         depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
464      atom->cmd[CTX_RB3D_ZSTENCILCNTL] &= ~RADEON_DEPTH_FORMAT_MASK;
465      atom->cmd[CTX_RB3D_ZSTENCILCNTL] |= depth_fmt;
466      
467    }
468
469    BEGIN_BATCH_NO_AUTOSTATE(dwords);
470
471    /* In the CS case we need to split this up */
472    OUT_BATCH(CP_PACKET0(packet[0].start, 3));
473    OUT_BATCH_TABLE((atom->cmd + 1), 4);
474
475    if (drb) {
476      OUT_BATCH(CP_PACKET0(RADEON_RB3D_DEPTHOFFSET, 0));
477      OUT_BATCH_RELOC(0, drb->bo, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
478
479      OUT_BATCH(CP_PACKET0(RADEON_RB3D_DEPTHPITCH, 0));
480      OUT_BATCH(zbpitch);
481    }
482
483    OUT_BATCH(CP_PACKET0(RADEON_RB3D_ZSTENCILCNTL, 0));
484    OUT_BATCH(atom->cmd[CTX_RB3D_ZSTENCILCNTL]);
485    OUT_BATCH(CP_PACKET0(RADEON_PP_CNTL, 1));
486    OUT_BATCH(atom->cmd[CTX_PP_CNTL]);
487    OUT_BATCH(atom->cmd[CTX_RB3D_CNTL]);
488
489    if (rrb) {
490      OUT_BATCH(CP_PACKET0(RADEON_RB3D_COLOROFFSET, 0));
491      OUT_BATCH_RELOC(rrb->draw_offset, rrb->bo, rrb->draw_offset, 0, RADEON_GEM_DOMAIN_VRAM, 0);
492
493      OUT_BATCH(CP_PACKET0(RADEON_RB3D_COLORPITCH, 0));
494      OUT_BATCH_RELOC(cbpitch, rrb->bo, cbpitch, 0, RADEON_GEM_DOMAIN_VRAM, 0);
495    }
496
497    // if (atom->cmd_size == CTX_STATE_SIZE_NEWDRM) {
498    //   OUT_BATCH_TABLE((atom->cmd + 14), 4);
499    // }
500
501    END_BATCH();
502    BEGIN_BATCH_NO_AUTOSTATE(4);
503    OUT_BATCH(CP_PACKET0(RADEON_RE_TOP_LEFT, 0));
504    OUT_BATCH(0);
505    OUT_BATCH(CP_PACKET0(RADEON_RE_WIDTH_HEIGHT, 0));
506    if (rrb) {
507        OUT_BATCH(((rrb->base.Width - 1) << RADEON_RE_WIDTH_SHIFT) |
508                  ((rrb->base.Height - 1) << RADEON_RE_HEIGHT_SHIFT));
509    } else {
510        OUT_BATCH(0);
511    }
512    END_BATCH();
513 }
514
515 static void cube_emit(struct gl_context *ctx, struct radeon_state_atom *atom)
516 {
517    r100ContextPtr r100 = R100_CONTEXT(ctx);
518    BATCH_LOCALS(&r100->radeon);
519    uint32_t dwords = atom->check(ctx, atom);
520    int i = atom->idx, j;
521    radeonTexObj *t = r100->state.texture.unit[i].texobj;
522    radeon_mipmap_level *lvl;
523
524    if (!(ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_CUBE_BIT))
525         return;
526
527    if (!t)
528         return;
529
530    if (!t->mt)
531         return;
532
533    BEGIN_BATCH_NO_AUTOSTATE(dwords);
534    OUT_BATCH_TABLE(atom->cmd, 3);
535    lvl = &t->mt->levels[0];
536    for (j = 0; j < 5; j++) {
537         OUT_BATCH_RELOC(lvl->faces[j].offset, t->mt->bo, lvl->faces[j].offset,
538                         RADEON_GEM_DOMAIN_VRAM, 0, 0);
539    }
540    END_BATCH();
541 }
542
543 static void cube_emit_cs(struct gl_context *ctx, struct radeon_state_atom *atom)
544 {
545    r100ContextPtr r100 = R100_CONTEXT(ctx);
546    BATCH_LOCALS(&r100->radeon);
547    uint32_t dwords = atom->check(ctx, atom);
548    int i = atom->idx, j;
549    radeonTexObj *t = r100->state.texture.unit[i].texobj;
550    radeon_mipmap_level *lvl;
551    uint32_t base_reg;
552
553    if (!(ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_CUBE_BIT))
554         return;
555
556    if (!t)
557         return;
558
559    if (!t->mt)
560         return;
561
562    switch(i) {
563         case 1: base_reg = RADEON_PP_CUBIC_OFFSET_T1_0; break;
564         case 2: base_reg = RADEON_PP_CUBIC_OFFSET_T2_0; break;
565         default:
566         case 0: base_reg = RADEON_PP_CUBIC_OFFSET_T0_0; break;
567    };
568    BEGIN_BATCH_NO_AUTOSTATE(dwords);
569    OUT_BATCH_TABLE(atom->cmd, 2);
570    lvl = &t->mt->levels[0];
571    for (j = 0; j < 5; j++) {
572         OUT_BATCH(CP_PACKET0(base_reg + (4 * j), 0));
573         OUT_BATCH_RELOC(lvl->faces[j].offset, t->mt->bo, lvl->faces[j].offset,
574                         RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
575    }
576    END_BATCH();
577 }
578
579 static void tex_emit(struct gl_context *ctx, struct radeon_state_atom *atom)
580 {
581    r100ContextPtr r100 = R100_CONTEXT(ctx);
582    BATCH_LOCALS(&r100->radeon);
583    uint32_t dwords = atom->cmd_size;
584    int i = atom->idx;
585    radeonTexObj *t = r100->state.texture.unit[i].texobj;
586    radeon_mipmap_level *lvl;
587
588    if (t && t->mt && !t->image_override)
589      dwords += 2;
590    BEGIN_BATCH_NO_AUTOSTATE(dwords);
591
592    OUT_BATCH_TABLE(atom->cmd, 3);
593    if (t && t->mt && !t->image_override) {
594      if ((ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_CUBE_BIT)) {
595         lvl = &t->mt->levels[0];
596         OUT_BATCH_RELOC(lvl->faces[5].offset, t->mt->bo, lvl->faces[5].offset,
597                         RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
598      } else {
599         OUT_BATCH_RELOC(t->tile_bits, t->mt->bo, 0,
600                      RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
601      }
602    } else if (!t) {
603      /* workaround for old CS mechanism */
604      OUT_BATCH(r100->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP]);
605      //     OUT_BATCH(r100->radeon.radeonScreen);
606    } else {
607      OUT_BATCH(t->override_offset);
608    }
609
610    OUT_BATCH_TABLE((atom->cmd+4), 5);
611    END_BATCH();
612 }
613
614 static void tex_emit_cs(struct gl_context *ctx, struct radeon_state_atom *atom)
615 {
616    r100ContextPtr r100 = R100_CONTEXT(ctx);
617    BATCH_LOCALS(&r100->radeon);
618    uint32_t dwords = atom->cmd_size;
619    int i = atom->idx;
620    radeonTexObj *t = r100->state.texture.unit[i].texobj;
621    radeon_mipmap_level *lvl;
622    int hastexture = 1;
623
624    if (!t)
625         hastexture = 0;
626    else {
627         if (!t->mt && !t->bo)
628                 hastexture = 0;
629    }
630    dwords += 1;
631    if (hastexture)
632      dwords += 2;
633    else
634      dwords -= 2;
635    BEGIN_BATCH_NO_AUTOSTATE(dwords);
636
637    OUT_BATCH(CP_PACKET0(RADEON_PP_TXFILTER_0 + (24 * i), 1));
638    OUT_BATCH_TABLE((atom->cmd + 1), 2);
639
640    if (hastexture) {
641      OUT_BATCH(CP_PACKET0(RADEON_PP_TXOFFSET_0 + (24 * i), 0));
642      if (t->mt && !t->image_override) {
643         if ((ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_CUBE_BIT)) {
644             lvl = &t->mt->levels[t->minLod];
645             OUT_BATCH_RELOC(lvl->faces[5].offset, t->mt->bo, lvl->faces[5].offset,
646                         RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
647         } else {
648            OUT_BATCH_RELOC(t->tile_bits, t->mt->bo, get_base_teximage_offset(t),
649                      RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
650         }
651       } else {
652         if (t->bo)
653             OUT_BATCH_RELOC(t->tile_bits, t->bo, 0,
654                             RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
655       }
656    }
657
658    OUT_BATCH(CP_PACKET0(RADEON_PP_TXCBLEND_0 + (i * 24), 1));
659    OUT_BATCH_TABLE((atom->cmd+4), 2);
660    OUT_BATCH(CP_PACKET0(RADEON_PP_BORDER_COLOR_0 + (i * 4), 0));
661    OUT_BATCH((atom->cmd[TEX_PP_BORDER_COLOR]));
662    END_BATCH();
663 }
664
665 /* Initialize the context's hardware state.
666  */
667 void radeonInitState( r100ContextPtr rmesa )
668 {
669    struct gl_context *ctx = rmesa->radeon.glCtx;
670    GLuint i;
671
672    rmesa->radeon.state.color.clear = 0x00000000;
673
674    switch ( ctx->Visual.depthBits ) {
675    case 16:
676       rmesa->radeon.state.depth.clear = 0x0000ffff;
677       rmesa->radeon.state.stencil.clear = 0x00000000;
678       break;
679    case 24:
680       rmesa->radeon.state.depth.clear = 0x00ffffff;
681       rmesa->radeon.state.stencil.clear = 0xffff0000;
682       break;
683    default:
684       break;
685    }
686
687    rmesa->radeon.Fallback = 0;
688
689
690    rmesa->radeon.hw.max_state_size = 0;
691
692 #define ALLOC_STATE_IDX( ATOM, CHK, SZ, NM, FLAG, IDX )         \
693    do {                                                         \
694       rmesa->hw.ATOM.cmd_size = SZ;                             \
695       rmesa->hw.ATOM.cmd = (GLuint *)CALLOC(SZ * sizeof(int));  \
696       rmesa->hw.ATOM.lastcmd = (GLuint *)CALLOC(SZ * sizeof(int)); \
697       rmesa->hw.ATOM.name = NM;                                         \
698       rmesa->hw.ATOM.is_tcl = FLAG;                                     \
699       rmesa->hw.ATOM.check = check_##CHK;                               \
700       rmesa->hw.ATOM.dirty = GL_TRUE;                                   \
701       rmesa->hw.ATOM.idx = IDX;                                 \
702       rmesa->radeon.hw.max_state_size += SZ * sizeof(int);              \
703    } while (0)
704
705 #define ALLOC_STATE( ATOM, CHK, SZ, NM, FLAG )          \
706    ALLOC_STATE_IDX(ATOM, CHK, SZ, NM, FLAG, 0)
707
708    /* Allocate state buffers:
709     */
710    ALLOC_STATE( ctx, always_add4, CTX_STATE_SIZE, "CTX/context", 0 );
711    if (rmesa->radeon.radeonScreen->kernel_mm) {
712      rmesa->hw.ctx.emit = ctx_emit_cs;
713      rmesa->hw.ctx.check = check_always_ctx;
714    } else
715      rmesa->hw.ctx.emit = ctx_emit;
716    ALLOC_STATE( lin, always, LIN_STATE_SIZE, "LIN/line", 0 );
717    ALLOC_STATE( msk, always, MSK_STATE_SIZE, "MSK/mask", 0 );
718    ALLOC_STATE( vpt, always, VPT_STATE_SIZE, "VPT/viewport", 0 );
719    ALLOC_STATE( set, always, SET_STATE_SIZE, "SET/setup", 0 );
720    ALLOC_STATE( msc, always, MSC_STATE_SIZE, "MSC/misc", 0 );
721    ALLOC_STATE( zbs, always, ZBS_STATE_SIZE, "ZBS/zbias", 0 );
722    ALLOC_STATE( tcl, always, TCL_STATE_SIZE, "TCL/tcl", 1 );
723    ALLOC_STATE( mtl, tcl_lighting, MTL_STATE_SIZE, "MTL/material", 1 );
724    if (rmesa->radeon.radeonScreen->kernel_mm) {
725       ALLOC_STATE( grd, always_add2, GRD_STATE_SIZE, "GRD/guard-band", 1 );
726       ALLOC_STATE( fog, fog_add4, FOG_STATE_SIZE, "FOG/fog", 1 );
727       ALLOC_STATE( glt, tcl_lighting_add4, GLT_STATE_SIZE, "GLT/light-global", 1 );
728       ALLOC_STATE( eye, tcl_lighting_add4, EYE_STATE_SIZE, "EYE/eye-vector", 1 );
729       ALLOC_STATE_IDX( tex[0], tex0_mm, TEX_STATE_SIZE, "TEX/tex-0", 0, 0);
730       ALLOC_STATE_IDX( tex[1], tex1_mm, TEX_STATE_SIZE, "TEX/tex-1", 0, 1);
731       ALLOC_STATE_IDX( tex[2], tex2_mm, TEX_STATE_SIZE, "TEX/tex-2", 0, 2);
732       ALLOC_STATE( mat[0], tcl_add4, MAT_STATE_SIZE, "MAT/modelproject", 1 );
733       ALLOC_STATE( mat[1], tcl_eyespace_or_fog_add4, MAT_STATE_SIZE, "MAT/modelview", 1 );
734       ALLOC_STATE( mat[2], tcl_eyespace_or_lighting_add4, MAT_STATE_SIZE, "MAT/it-modelview", 1 );
735       ALLOC_STATE( mat[3], tcl_tex0_add4, MAT_STATE_SIZE, "MAT/texmat0", 1 );
736       ALLOC_STATE( mat[4], tcl_tex1_add4, MAT_STATE_SIZE, "MAT/texmat1", 1 );
737       ALLOC_STATE( mat[5], tcl_tex2_add4, MAT_STATE_SIZE, "MAT/texmat2", 1 );
738       ALLOC_STATE( lit[0], tcl_lit0_add6, LIT_STATE_SIZE, "LIT/light-0", 1 );
739       ALLOC_STATE( lit[1], tcl_lit1_add6, LIT_STATE_SIZE, "LIT/light-1", 1 );
740       ALLOC_STATE( lit[2], tcl_lit2_add6, LIT_STATE_SIZE, "LIT/light-2", 1 );
741       ALLOC_STATE( lit[3], tcl_lit3_add6, LIT_STATE_SIZE, "LIT/light-3", 1 );
742       ALLOC_STATE( lit[4], tcl_lit4_add6, LIT_STATE_SIZE, "LIT/light-4", 1 );
743       ALLOC_STATE( lit[5], tcl_lit5_add6, LIT_STATE_SIZE, "LIT/light-5", 1 );
744       ALLOC_STATE( lit[6], tcl_lit6_add6, LIT_STATE_SIZE, "LIT/light-6", 1 );
745       ALLOC_STATE( lit[7], tcl_lit7_add6, LIT_STATE_SIZE, "LIT/light-7", 1 );
746       ALLOC_STATE( ucp[0], tcl_ucp0_add4, UCP_STATE_SIZE, "UCP/userclip-0", 1 );
747       ALLOC_STATE( ucp[1], tcl_ucp1_add4, UCP_STATE_SIZE, "UCP/userclip-1", 1 );
748       ALLOC_STATE( ucp[2], tcl_ucp2_add4, UCP_STATE_SIZE, "UCP/userclip-2", 1 );
749       ALLOC_STATE( ucp[3], tcl_ucp3_add4, UCP_STATE_SIZE, "UCP/userclip-3", 1 );
750       ALLOC_STATE( ucp[4], tcl_ucp4_add4, UCP_STATE_SIZE, "UCP/userclip-4", 1 );
751       ALLOC_STATE( ucp[5], tcl_ucp5_add4, UCP_STATE_SIZE, "UCP/userclip-5", 1 );
752    } else {
753       ALLOC_STATE( grd, always, GRD_STATE_SIZE, "GRD/guard-band", 1 );
754       ALLOC_STATE( fog, fog, FOG_STATE_SIZE, "FOG/fog", 1 );
755       ALLOC_STATE( glt, tcl_lighting, GLT_STATE_SIZE, "GLT/light-global", 1 );
756       ALLOC_STATE( eye, tcl_lighting, EYE_STATE_SIZE, "EYE/eye-vector", 1 );
757       ALLOC_STATE_IDX( tex[0], tex0, TEX_STATE_SIZE, "TEX/tex-0", 0, 0);
758       ALLOC_STATE_IDX( tex[1], tex1, TEX_STATE_SIZE, "TEX/tex-1", 0, 1);
759       ALLOC_STATE_IDX( tex[2], tex2, TEX_STATE_SIZE, "TEX/tex-2", 0, 2);
760       ALLOC_STATE( mat[0], tcl, MAT_STATE_SIZE, "MAT/modelproject", 1 );
761       ALLOC_STATE( mat[1], tcl_eyespace_or_fog, MAT_STATE_SIZE, "MAT/modelview", 1 );
762       ALLOC_STATE( mat[2], tcl_eyespace_or_lighting, MAT_STATE_SIZE, "MAT/it-modelview", 1 );
763       ALLOC_STATE( mat[3], tcl_tex0, MAT_STATE_SIZE, "MAT/texmat0", 1 );
764       ALLOC_STATE( mat[4], tcl_tex1, MAT_STATE_SIZE, "MAT/texmat1", 1 );
765       ALLOC_STATE( mat[5], tcl_tex2, MAT_STATE_SIZE, "MAT/texmat2", 1 );
766       ALLOC_STATE( lit[0], tcl_lit0, LIT_STATE_SIZE, "LIT/light-0", 1 );
767       ALLOC_STATE( lit[1], tcl_lit1, LIT_STATE_SIZE, "LIT/light-1", 1 );
768       ALLOC_STATE( lit[2], tcl_lit2, LIT_STATE_SIZE, "LIT/light-2", 1 );
769       ALLOC_STATE( lit[3], tcl_lit3, LIT_STATE_SIZE, "LIT/light-3", 1 );
770       ALLOC_STATE( lit[4], tcl_lit4, LIT_STATE_SIZE, "LIT/light-4", 1 );
771       ALLOC_STATE( lit[5], tcl_lit5, LIT_STATE_SIZE, "LIT/light-5", 1 );
772       ALLOC_STATE( lit[6], tcl_lit6, LIT_STATE_SIZE, "LIT/light-6", 1 );
773       ALLOC_STATE( lit[7], tcl_lit7, LIT_STATE_SIZE, "LIT/light-7", 1 );
774       ALLOC_STATE( ucp[0], tcl_ucp0, UCP_STATE_SIZE, "UCP/userclip-0", 1 );
775       ALLOC_STATE( ucp[1], tcl_ucp1, UCP_STATE_SIZE, "UCP/userclip-1", 1 );
776       ALLOC_STATE( ucp[2], tcl_ucp2, UCP_STATE_SIZE, "UCP/userclip-2", 1 );
777       ALLOC_STATE( ucp[3], tcl_ucp3, UCP_STATE_SIZE, "UCP/userclip-3", 1 );
778       ALLOC_STATE( ucp[4], tcl_ucp4, UCP_STATE_SIZE, "UCP/userclip-4", 1 );
779       ALLOC_STATE( ucp[5], tcl_ucp5, UCP_STATE_SIZE, "UCP/userclip-5", 1 );
780    }
781
782    if (rmesa->radeon.radeonScreen->kernel_mm) {
783        ALLOC_STATE( stp, always, STP_STATE_SIZE, "STP/stp", 0 );
784    }
785    
786    for (i = 0; i < 3; i++) {
787       if (rmesa->radeon.radeonScreen->kernel_mm)
788           rmesa->hw.tex[i].emit = tex_emit_cs;
789       else
790           rmesa->hw.tex[i].emit = tex_emit;
791    }
792    if (rmesa->radeon.radeonScreen->drmSupportsCubeMapsR100)
793    {
794       if (rmesa->radeon.radeonScreen->kernel_mm) {
795          ALLOC_STATE_IDX( cube[0], cube0_mm, CUBE_STATE_SIZE, "CUBE/cube-0", 0, 0 );
796          ALLOC_STATE_IDX( cube[1], cube1_mm, CUBE_STATE_SIZE, "CUBE/cube-1", 0, 1 );
797          ALLOC_STATE_IDX( cube[2], cube2_mm, CUBE_STATE_SIZE, "CUBE/cube-2", 0, 2 );
798          for (i = 0; i < 3; i++)
799             rmesa->hw.cube[i].emit = cube_emit_cs;
800       } else {
801          ALLOC_STATE_IDX( cube[0], cube0, CUBE_STATE_SIZE, "CUBE/cube-0", 0, 0 );
802          ALLOC_STATE_IDX( cube[1], cube1, CUBE_STATE_SIZE, "CUBE/cube-1", 0, 1 );
803          ALLOC_STATE_IDX( cube[2], cube2, CUBE_STATE_SIZE, "CUBE/cube-2", 0, 2 );
804          for (i = 0; i < 3; i++)
805             rmesa->hw.cube[i].emit = cube_emit;
806       }
807    }
808    else
809    {
810       ALLOC_STATE_IDX( cube[0], never, CUBE_STATE_SIZE, "CUBE/cube-0", 0, 0 );
811       ALLOC_STATE_IDX( cube[1], never, CUBE_STATE_SIZE, "CUBE/cube-1", 0, 1 );
812       ALLOC_STATE_IDX( cube[2], never, CUBE_STATE_SIZE, "CUBE/cube-2", 0, 2 );
813    }
814    ALLOC_STATE_IDX( txr[0], txr0, TXR_STATE_SIZE, "TXR/txr-0", 0, 0 );
815    ALLOC_STATE_IDX( txr[1], txr1, TXR_STATE_SIZE, "TXR/txr-1", 0, 1 );
816    ALLOC_STATE_IDX( txr[2], txr2, TXR_STATE_SIZE, "TXR/txr-2", 0, 2 );
817
818    radeonSetUpAtomList( rmesa );
819
820    /* Fill in the packet headers:
821     */
822    rmesa->hw.ctx.cmd[CTX_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_MISC);
823    rmesa->hw.ctx.cmd[CTX_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_CNTL);
824    rmesa->hw.ctx.cmd[CTX_CMD_2] = cmdpkt(rmesa, RADEON_EMIT_RB3D_COLORPITCH);
825    rmesa->hw.lin.cmd[LIN_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_RE_LINE_PATTERN);
826    rmesa->hw.lin.cmd[LIN_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_SE_LINE_WIDTH);
827    rmesa->hw.msk.cmd[MSK_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_RB3D_STENCILREFMASK);
828    rmesa->hw.vpt.cmd[VPT_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_SE_VPORT_XSCALE);
829    rmesa->hw.set.cmd[SET_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_SE_CNTL);
830    rmesa->hw.set.cmd[SET_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_SE_CNTL_STATUS);
831    rmesa->hw.msc.cmd[MSC_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_RE_MISC);
832    rmesa->hw.tex[0].cmd[TEX_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_TXFILTER_0);
833    rmesa->hw.tex[0].cmd[TEX_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_BORDER_COLOR_0);
834    rmesa->hw.tex[1].cmd[TEX_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_TXFILTER_1);
835    rmesa->hw.tex[1].cmd[TEX_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_BORDER_COLOR_1);
836    rmesa->hw.tex[2].cmd[TEX_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_TXFILTER_2);
837    rmesa->hw.tex[2].cmd[TEX_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_BORDER_COLOR_2);
838    rmesa->hw.cube[0].cmd[CUBE_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_CUBIC_FACES_0);
839    rmesa->hw.cube[0].cmd[CUBE_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_CUBIC_OFFSETS_T0);
840    rmesa->hw.cube[1].cmd[CUBE_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_CUBIC_FACES_1);
841    rmesa->hw.cube[1].cmd[CUBE_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_CUBIC_OFFSETS_T1);
842    rmesa->hw.cube[2].cmd[CUBE_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_CUBIC_FACES_2);
843    rmesa->hw.cube[2].cmd[CUBE_CMD_1] = cmdpkt(rmesa, RADEON_EMIT_PP_CUBIC_OFFSETS_T2);
844    rmesa->hw.zbs.cmd[ZBS_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_SE_ZBIAS_FACTOR);
845    rmesa->hw.tcl.cmd[TCL_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT);
846    rmesa->hw.mtl.cmd[MTL_CMD_0] = 
847       cmdpkt(rmesa, RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED);
848    rmesa->hw.txr[0].cmd[TXR_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_TEX_SIZE_0);
849    rmesa->hw.txr[1].cmd[TXR_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_TEX_SIZE_1);
850    rmesa->hw.txr[2].cmd[TXR_CMD_0] = cmdpkt(rmesa, RADEON_EMIT_PP_TEX_SIZE_2);
851    rmesa->hw.grd.cmd[GRD_CMD_0] = 
852       cmdscl( RADEON_SS_VERT_GUARD_CLIP_ADJ_ADDR, 1, 4 );
853    rmesa->hw.fog.cmd[FOG_CMD_0] = 
854       cmdvec( RADEON_VS_FOG_PARAM_ADDR, 1, 4 );
855    rmesa->hw.glt.cmd[GLT_CMD_0] = 
856       cmdvec( RADEON_VS_GLOBAL_AMBIENT_ADDR, 1, 4 );
857    rmesa->hw.eye.cmd[EYE_CMD_0] = 
858       cmdvec( RADEON_VS_EYE_VECTOR_ADDR, 1, 4 );
859
860    for (i = 0 ; i < 6; i++) {
861       rmesa->hw.mat[i].cmd[MAT_CMD_0] = 
862          cmdvec( RADEON_VS_MATRIX_0_ADDR + i*4, 1, 16);
863    }
864
865    for (i = 0 ; i < 8; i++) {
866       rmesa->hw.lit[i].cmd[LIT_CMD_0] = 
867          cmdvec( RADEON_VS_LIGHT_AMBIENT_ADDR + i, 8, 24 );
868       rmesa->hw.lit[i].cmd[LIT_CMD_1] = 
869          cmdscl( RADEON_SS_LIGHT_DCD_ADDR + i, 8, 6 );
870    }
871
872    for (i = 0 ; i < 6; i++) {
873       rmesa->hw.ucp[i].cmd[UCP_CMD_0] = 
874          cmdvec( RADEON_VS_UCP_ADDR + i, 1, 4 );
875    }
876
877    if (rmesa->radeon.radeonScreen->kernel_mm) {
878       rmesa->hw.stp.cmd[STP_CMD_0] = CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0);
879       rmesa->hw.stp.cmd[STP_DATA_0] = 0;
880       rmesa->hw.stp.cmd[STP_CMD_1] = CP_PACKET0_ONE(RADEON_RE_STIPPLE_DATA, 31);
881
882       rmesa->hw.grd.emit = scl_emit;
883       rmesa->hw.fog.emit = vec_emit;
884       rmesa->hw.glt.emit = vec_emit;
885       rmesa->hw.eye.emit = vec_emit;
886       
887       for (i = 0; i < 6; i++)
888          rmesa->hw.mat[i].emit = vec_emit;
889
890       for (i = 0; i < 8; i++)
891          rmesa->hw.lit[i].emit = lit_emit;
892
893       for (i = 0; i < 6; i++)
894          rmesa->hw.ucp[i].emit = vec_emit;
895    }
896
897    rmesa->last_ReallyEnabled = -1;
898
899    /* Initial Harware state:
900     */
901    rmesa->hw.ctx.cmd[CTX_PP_MISC] = (RADEON_ALPHA_TEST_PASS |
902                                      RADEON_CHROMA_FUNC_FAIL |
903                                      RADEON_CHROMA_KEY_NEAREST |
904                                      RADEON_SHADOW_FUNC_EQUAL |
905                                      RADEON_SHADOW_PASS_1 /*|
906                                      RADEON_RIGHT_HAND_CUBE_OGL */);
907
908    rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] = (RADEON_FOG_VERTEX |
909                                           /* this bit unused for vertex fog */
910                                           RADEON_FOG_USE_DEPTH);
911
912    rmesa->hw.ctx.cmd[CTX_RE_SOLID_COLOR] = 0x00000000;
913
914    rmesa->hw.ctx.cmd[CTX_RB3D_BLENDCNTL] = (RADEON_COMB_FCN_ADD_CLAMP |
915                                             RADEON_SRC_BLEND_GL_ONE |
916                                             RADEON_DST_BLEND_GL_ZERO );
917
918    rmesa->hw.ctx.cmd[CTX_RB3D_ZSTENCILCNTL] = (RADEON_Z_TEST_LESS |
919                                                RADEON_STENCIL_TEST_ALWAYS |
920                                                RADEON_STENCIL_FAIL_KEEP |
921                                                RADEON_STENCIL_ZPASS_KEEP |
922                                                RADEON_STENCIL_ZFAIL_KEEP |
923                                                RADEON_Z_WRITE_ENABLE);
924
925    if (rmesa->using_hyperz) {
926        rmesa->hw.ctx.cmd[CTX_RB3D_ZSTENCILCNTL] |= RADEON_Z_COMPRESSION_ENABLE |
927                                                    RADEON_Z_DECOMPRESSION_ENABLE;
928       if (rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
929          /* works for q3, but slight rendering errors with glxgears ? */
930 /*       rmesa->hw.ctx.cmd[CTX_RB3D_ZSTENCILCNTL] |= RADEON_Z_HIERARCHY_ENABLE;*/
931          /* need this otherwise get lots of lockups with q3 ??? */
932          rmesa->hw.ctx.cmd[CTX_RB3D_ZSTENCILCNTL] |= RADEON_FORCE_Z_DIRTY;
933       } 
934    }
935
936    rmesa->hw.ctx.cmd[CTX_PP_CNTL] = (RADEON_SCISSOR_ENABLE |
937                                      RADEON_ANTI_ALIAS_NONE);
938
939    rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] = (RADEON_PLANE_MASK_ENABLE |
940                                        RADEON_ZBLOCK16);
941
942    switch ( driQueryOptioni( &rmesa->radeon.optionCache, "dither_mode" ) ) {
943    case DRI_CONF_DITHER_XERRORDIFFRESET:
944       rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] |= RADEON_DITHER_INIT;
945       break;
946    case DRI_CONF_DITHER_ORDERED:
947       rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] |= RADEON_SCALE_DITHER_ENABLE;
948       break;
949    }
950    if ( driQueryOptioni( &rmesa->radeon.optionCache, "round_mode" ) ==
951         DRI_CONF_ROUND_ROUND )
952       rmesa->radeon.state.color.roundEnable = RADEON_ROUND_ENABLE;
953    else
954       rmesa->radeon.state.color.roundEnable = 0;
955    if ( driQueryOptioni (&rmesa->radeon.optionCache, "color_reduction" ) ==
956         DRI_CONF_COLOR_REDUCTION_DITHER )
957       rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] |= RADEON_DITHER_ENABLE;
958    else
959       rmesa->hw.ctx.cmd[CTX_RB3D_CNTL] |= rmesa->radeon.state.color.roundEnable;
960
961
962    rmesa->hw.set.cmd[SET_SE_CNTL] = (RADEON_FFACE_CULL_CCW |
963                                      RADEON_BFACE_SOLID |
964                                      RADEON_FFACE_SOLID |
965 /*                           RADEON_BADVTX_CULL_DISABLE | */
966                                      RADEON_FLAT_SHADE_VTX_LAST |
967                                      RADEON_DIFFUSE_SHADE_GOURAUD |
968                                      RADEON_ALPHA_SHADE_GOURAUD |
969                                      RADEON_SPECULAR_SHADE_GOURAUD |
970                                      RADEON_FOG_SHADE_GOURAUD |
971                                      RADEON_VPORT_XY_XFORM_ENABLE |
972                                      RADEON_VPORT_Z_XFORM_ENABLE |
973                                      RADEON_VTX_PIX_CENTER_OGL |
974                                      RADEON_ROUND_MODE_TRUNC |
975                                      RADEON_ROUND_PREC_8TH_PIX);
976
977    rmesa->hw.set.cmd[SET_SE_CNTL_STATUS] =
978 #ifdef MESA_BIG_ENDIAN
979                                             RADEON_VC_32BIT_SWAP;
980 #else
981                                             RADEON_VC_NO_SWAP;
982 #endif
983
984    if (!(rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL)) {
985      rmesa->hw.set.cmd[SET_SE_CNTL_STATUS] |= RADEON_TCL_BYPASS;
986    }
987
988    rmesa->hw.set.cmd[SET_SE_COORDFMT] = (
989       RADEON_VTX_W0_IS_NOT_1_OVER_W0 |
990       RADEON_TEX1_W_ROUTING_USE_Q1);
991
992
993    rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] = ((1 << 16) | 0xffff);
994
995    rmesa->hw.lin.cmd[LIN_RE_LINE_STATE] = 
996       ((0 << RADEON_LINE_CURRENT_PTR_SHIFT) |
997        (1 << RADEON_LINE_CURRENT_COUNT_SHIFT));
998
999    rmesa->hw.lin.cmd[LIN_SE_LINE_WIDTH] = (1 << 4);
1000
1001    rmesa->hw.msk.cmd[MSK_RB3D_STENCILREFMASK] = 
1002       ((0x00 << RADEON_STENCIL_REF_SHIFT) |
1003        (0xff << RADEON_STENCIL_MASK_SHIFT) |
1004        (0xff << RADEON_STENCIL_WRITEMASK_SHIFT));
1005
1006    rmesa->hw.msk.cmd[MSK_RB3D_ROPCNTL] = RADEON_ROP_COPY;
1007    rmesa->hw.msk.cmd[MSK_RB3D_PLANEMASK] = 0xffffffff;
1008
1009    rmesa->hw.msc.cmd[MSC_RE_MISC] = 
1010       ((0 << RADEON_STIPPLE_X_OFFSET_SHIFT) |
1011        (0 << RADEON_STIPPLE_Y_OFFSET_SHIFT) |
1012        RADEON_STIPPLE_BIG_BIT_ORDER);
1013
1014    rmesa->hw.vpt.cmd[VPT_SE_VPORT_XSCALE]  = 0x00000000;
1015    rmesa->hw.vpt.cmd[VPT_SE_VPORT_XOFFSET] = 0x00000000;
1016    rmesa->hw.vpt.cmd[VPT_SE_VPORT_YSCALE]  = 0x00000000;
1017    rmesa->hw.vpt.cmd[VPT_SE_VPORT_YOFFSET] = 0x00000000;
1018    rmesa->hw.vpt.cmd[VPT_SE_VPORT_ZSCALE]  = 0x00000000;
1019    rmesa->hw.vpt.cmd[VPT_SE_VPORT_ZOFFSET] = 0x00000000;
1020
1021    for ( i = 0 ; i < ctx->Const.MaxTextureUnits ; i++ ) {
1022       rmesa->hw.tex[i].cmd[TEX_PP_TXFILTER] = RADEON_BORDER_MODE_OGL;
1023       rmesa->hw.tex[i].cmd[TEX_PP_TXFORMAT] = 
1024           (RADEON_TXFORMAT_ENDIAN_NO_SWAP |
1025            RADEON_TXFORMAT_PERSPECTIVE_ENABLE |
1026            (i << 24) | /* This is one of RADEON_TXFORMAT_ST_ROUTE_STQ[012] */
1027            (2 << RADEON_TXFORMAT_WIDTH_SHIFT) |
1028            (2 << RADEON_TXFORMAT_HEIGHT_SHIFT));
1029
1030       /* Initialize the texture offset to the start of the card texture heap */
1031       //      rmesa->hw.tex[i].cmd[TEX_PP_TXOFFSET] =
1032       //          rmesa->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP];
1033
1034       rmesa->hw.tex[i].cmd[TEX_PP_BORDER_COLOR] = 0;
1035       rmesa->hw.tex[i].cmd[TEX_PP_TXCBLEND] =  
1036           (RADEON_COLOR_ARG_A_ZERO |
1037            RADEON_COLOR_ARG_B_ZERO |
1038            RADEON_COLOR_ARG_C_CURRENT_COLOR |
1039            RADEON_BLEND_CTL_ADD |
1040            RADEON_SCALE_1X |
1041            RADEON_CLAMP_TX);
1042       rmesa->hw.tex[i].cmd[TEX_PP_TXABLEND] = 
1043           (RADEON_ALPHA_ARG_A_ZERO |
1044            RADEON_ALPHA_ARG_B_ZERO |
1045            RADEON_ALPHA_ARG_C_CURRENT_ALPHA |
1046            RADEON_BLEND_CTL_ADD |
1047            RADEON_SCALE_1X |
1048            RADEON_CLAMP_TX);
1049       rmesa->hw.tex[i].cmd[TEX_PP_TFACTOR] = 0;
1050
1051       rmesa->hw.cube[i].cmd[CUBE_PP_CUBIC_FACES] = 0;
1052       rmesa->hw.cube[i].cmd[CUBE_PP_CUBIC_OFFSET_0] =
1053           rmesa->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP];
1054       rmesa->hw.cube[i].cmd[CUBE_PP_CUBIC_OFFSET_1] =
1055           rmesa->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP];
1056       rmesa->hw.cube[i].cmd[CUBE_PP_CUBIC_OFFSET_2] =
1057           rmesa->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP];
1058       rmesa->hw.cube[i].cmd[CUBE_PP_CUBIC_OFFSET_3] =
1059           rmesa->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP];
1060       rmesa->hw.cube[i].cmd[CUBE_PP_CUBIC_OFFSET_4] =
1061           rmesa->radeon.radeonScreen->texOffset[RADEON_LOCAL_TEX_HEAP];
1062    }
1063
1064    /* Can only add ST1 at the time of doing some multitex but can keep
1065     * it after that.  Errors if DIFFUSE is missing.
1066     */
1067    rmesa->hw.tcl.cmd[TCL_OUTPUT_VTXFMT] = 
1068       (RADEON_TCL_VTX_Z0 |
1069        RADEON_TCL_VTX_W0 |
1070        RADEON_TCL_VTX_PK_DIFFUSE
1071          );     /* need to keep this uptodate */
1072                                                    
1073    rmesa->hw.tcl.cmd[TCL_OUTPUT_VTXSEL] =
1074       ( RADEON_TCL_COMPUTE_XYZW         |
1075         (RADEON_TCL_TEX_INPUT_TEX_0 << RADEON_TCL_TEX_0_OUTPUT_SHIFT) |
1076         (RADEON_TCL_TEX_INPUT_TEX_1 << RADEON_TCL_TEX_1_OUTPUT_SHIFT) |
1077         (RADEON_TCL_TEX_INPUT_TEX_2 << RADEON_TCL_TEX_2_OUTPUT_SHIFT));
1078
1079
1080    /* XXX */
1081    rmesa->hw.tcl.cmd[TCL_MATRIX_SELECT_0] = 
1082       ((MODEL << RADEON_MODELVIEW_0_SHIFT) |
1083        (MODEL_IT << RADEON_IT_MODELVIEW_0_SHIFT));
1084
1085    rmesa->hw.tcl.cmd[TCL_MATRIX_SELECT_1] = 
1086       ((MODEL_PROJ << RADEON_MODELPROJECT_0_SHIFT) |
1087        (TEXMAT_0 << RADEON_TEXMAT_0_SHIFT) |
1088        (TEXMAT_1 << RADEON_TEXMAT_1_SHIFT) |
1089        (TEXMAT_2 << RADEON_TEXMAT_2_SHIFT));
1090
1091    rmesa->hw.tcl.cmd[TCL_UCP_VERT_BLEND_CTL] = 
1092       (RADEON_UCP_IN_CLIP_SPACE |
1093        RADEON_CULL_FRONT_IS_CCW);
1094
1095    rmesa->hw.tcl.cmd[TCL_TEXTURE_PROC_CTL] = 0; 
1096
1097    rmesa->hw.tcl.cmd[TCL_LIGHT_MODEL_CTL] = 
1098       (RADEON_SPECULAR_LIGHTS |
1099        RADEON_DIFFUSE_SPECULAR_COMBINE |
1100        RADEON_LOCAL_LIGHT_VEC_GL |
1101        (RADEON_LM_SOURCE_STATE_MULT << RADEON_EMISSIVE_SOURCE_SHIFT) |
1102        (RADEON_LM_SOURCE_STATE_MULT << RADEON_AMBIENT_SOURCE_SHIFT) |
1103        (RADEON_LM_SOURCE_STATE_MULT << RADEON_DIFFUSE_SOURCE_SHIFT) |
1104        (RADEON_LM_SOURCE_STATE_MULT << RADEON_SPECULAR_SOURCE_SHIFT));
1105
1106    for (i = 0 ; i < 8; i++) {
1107       struct gl_light *l = &ctx->Light.Light[i];
1108       GLenum p = GL_LIGHT0 + i;
1109       *(float *)&(rmesa->hw.lit[i].cmd[LIT_RANGE_CUTOFF]) = FLT_MAX;
1110
1111       ctx->Driver.Lightfv( ctx, p, GL_AMBIENT, l->Ambient );
1112       ctx->Driver.Lightfv( ctx, p, GL_DIFFUSE, l->Diffuse );
1113       ctx->Driver.Lightfv( ctx, p, GL_SPECULAR, l->Specular );
1114       ctx->Driver.Lightfv( ctx, p, GL_POSITION, NULL );
1115       ctx->Driver.Lightfv( ctx, p, GL_SPOT_DIRECTION, NULL );
1116       ctx->Driver.Lightfv( ctx, p, GL_SPOT_EXPONENT, &l->SpotExponent );
1117       ctx->Driver.Lightfv( ctx, p, GL_SPOT_CUTOFF, &l->SpotCutoff );
1118       ctx->Driver.Lightfv( ctx, p, GL_CONSTANT_ATTENUATION,
1119                            &l->ConstantAttenuation );
1120       ctx->Driver.Lightfv( ctx, p, GL_LINEAR_ATTENUATION, 
1121                            &l->LinearAttenuation );
1122       ctx->Driver.Lightfv( ctx, p, GL_QUADRATIC_ATTENUATION, 
1123                      &l->QuadraticAttenuation );
1124       *(float *)&(rmesa->hw.lit[i].cmd[LIT_ATTEN_XXX]) = 0.0;
1125    }
1126
1127    ctx->Driver.LightModelfv( ctx, GL_LIGHT_MODEL_AMBIENT, 
1128                              ctx->Light.Model.Ambient );
1129
1130    TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
1131
1132    for (i = 0 ; i < 6; i++) {
1133       ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, NULL );
1134    }
1135
1136    ctx->Driver.Fogfv( ctx, GL_FOG_MODE, NULL );
1137    ctx->Driver.Fogfv( ctx, GL_FOG_DENSITY, &ctx->Fog.Density );
1138    ctx->Driver.Fogfv( ctx, GL_FOG_START, &ctx->Fog.Start );
1139    ctx->Driver.Fogfv( ctx, GL_FOG_END, &ctx->Fog.End );
1140    ctx->Driver.Fogfv( ctx, GL_FOG_COLOR, ctx->Fog.Color );
1141    ctx->Driver.Fogfv( ctx, GL_FOG_COORDINATE_SOURCE_EXT, NULL );
1142    
1143    rmesa->hw.grd.cmd[GRD_VERT_GUARD_CLIP_ADJ] = IEEE_ONE;
1144    rmesa->hw.grd.cmd[GRD_VERT_GUARD_DISCARD_ADJ] = IEEE_ONE;
1145    rmesa->hw.grd.cmd[GRD_HORZ_GUARD_CLIP_ADJ] = IEEE_ONE;
1146    rmesa->hw.grd.cmd[GRD_HORZ_GUARD_DISCARD_ADJ] = IEEE_ONE;
1147
1148    rmesa->hw.eye.cmd[EYE_X] = 0;
1149    rmesa->hw.eye.cmd[EYE_Y] = 0;
1150    rmesa->hw.eye.cmd[EYE_Z] = IEEE_ONE;
1151    rmesa->hw.eye.cmd[EYE_RESCALE_FACTOR] = IEEE_ONE;
1152
1153    if (rmesa->radeon.radeonScreen->kernel_mm) {
1154       radeon_init_query_stateobj(&rmesa->radeon, R100_QUERYOBJ_CMDSIZE);
1155       rmesa->radeon.query.queryobj.cmd[R100_QUERYOBJ_CMD_0] = CP_PACKET0(RADEON_RB3D_ZPASS_DATA, 0);
1156       rmesa->radeon.query.queryobj.cmd[R100_QUERYOBJ_DATA_0] = 0;
1157    }
1158      
1159    rmesa->radeon.hw.all_dirty = GL_TRUE;
1160
1161    rcommonInitCmdBuf(&rmesa->radeon);
1162 }