summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/r300/r300_maos.c
blob: 547ab359b9bd83d3a2c901a4d0309a09f7d68173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* $XFree86: xc/lib/GL/mesa/src/drv/r300/r300_maos_arrays.c,v 1.3 2003/02/23 23:59:01 dawes Exp $ */
/*
Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.

The Weather Channel (TM) funded Tungsten Graphics to develop the
initial release of the Radeon 8500 driver under the XFree86 license.
This notice must be preserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Keith Whitwell <keith@tungstengraphics.com>
 */

#include "glheader.h"
#include "mtypes.h"
#include "colormac.h"
#include "imports.h"
#include "macros.h"

#include "swrast_setup/swrast_setup.h"
#include "math/m_translate.h"
#include "tnl/tnl.h"
#include "tnl/t_context.h"

#include "r300_context.h"
#include "radeon_ioctl.h"
#include "r300_state.h"
#include "r300_maos.h"
#include "r300_ioctl.h"

#define DEBUG_ALL DEBUG_VERTS


#if defined(USE_X86_ASM)
#define COPY_DWORDS( dst, src, nr )					\
do {									\
	int __tmp;							\
	__asm__ __volatile__( "rep ; movsl"				\
			      : "=%c" (__tmp), "=D" (dst), "=S" (__tmp)	\
			      : "0" (nr),				\
			        "D" ((long)dst),			\
			        "S" ((long)src) );			\
} while (0)
#else
#define COPY_DWORDS( dst, src, nr )		\
do {						\
   int j;					\
   for ( j = 0 ; j < nr ; j++ )			\
      dst[j] = ((int *)src)[j];			\
   dst += nr;					\
} while (0)
#endif

static void emit_vec4(GLcontext * ctx,
		      struct r300_dma_region *rvb,
		      char *data, int stride, int count)
{
	int i;
	int *out = (int *)(rvb->address + rvb->start);

	if (RADEON_DEBUG & DEBUG_VERTS)
		fprintf(stderr, "%s count %d stride %d\n",
			__FUNCTION__, count, stride);

	if (stride == 4)
		COPY_DWORDS(out, data, count);
	else
		for (i = 0; i < count; i++) {
			out[0] = *(int *)data;
			out++;
			data += stride;
		}
}

static void emit_vec8(GLcontext * ctx,
		      struct r300_dma_region *rvb,
		      char *data, int stride, int count)
{
	int i;
	int *out = (int *)(rvb->address + rvb->start);

	if (RADEON_DEBUG & DEBUG_VERTS)
		fprintf(stderr, "%s count %d stride %d\n",
			__FUNCTION__, count, stride);

	if (stride == 8)
		COPY_DWORDS(out, data, count * 2);
	else
		for (i = 0; i < count; i++) {
			out[0] = *(int *)data;
			out[1] = *(int *)(data + 4);
			out += 2;
			data += stride;
		}
}

static void emit_vec12(GLcontext * ctx,
		       struct r300_dma_region *rvb,
		       char *data, int stride, int count)
{
	int i;
	int *out = (int *)(rvb->address + rvb->start);

	if (RADEON_DEBUG & DEBUG_VERTS)
		fprintf(stderr, "%s count %d stride %d out %p data %p\n",
			__FUNCTION__, count, stride, (void *)out, (void *)data);

	if (stride == 12)
		COPY_DWORDS(out, data, count * 3);
	else
		for (i = 0; i < count; i++) {
			out[0] = *(int *)data;
			out[1] = *(int *)(data + 4);
			out[2] = *(int *)(data + 8);
			out += 3;
			data += stride;
		}
}

static void emit_vec16(GLcontext * ctx,
		       struct r300_dma_region *rvb,
		       char *data, int stride, int count)
{
	int i;
	int *out = (int *)(rvb->address + rvb->start);

	if (RADEON_DEBUG & DEBUG_VERTS)
		fprintf(stderr, "%s count %d stride %d\n",
			__FUNCTION__, count, stride);

	if (stride == 16)
		COPY_DWORDS(out, data, count * 4);
	else
		for (i = 0; i < count; i++) {
			out[0] = *(int *)data;
			out[1] = *(int *)(data + 4);
			out[2] = *(int *)(data + 8);
			out[3] = *(int *)(data + 12);
			out += 4;
			data += stride;
		}
}

static void emit_vector(GLcontext * ctx,
			struct r300_dma_region *rvb,
			char *data, int size, int stride, int count)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);

	if (RADEON_DEBUG & DEBUG_VERTS)
		fprintf(stderr, "%s count %d size %d stride %d\n",
			__FUNCTION__, count, size, stride);

	assert(!rvb->buf);

	if (stride == 0) {
		r300AllocDmaRegion(rmesa, rvb, size * 4, 4);
		count = 1;
		rvb->aos_offset	= GET_START(rvb);
		rvb->aos_stride	= 0;
		rvb->aos_size	= size;
	} else {
		r300AllocDmaRegion(rmesa, rvb, size * count * 4, 4);	/* alignment? */
		rvb->aos_offset	= GET_START(rvb);
		rvb->aos_stride	= size;
		rvb->aos_size	= size;
	}

	/* Emit the data
	 */
	switch (size) {
	case 1:
		emit_vec4(ctx, rvb, data, stride, count);
		break;
	case 2:
		emit_vec8(ctx, rvb, data, stride, count);
		break;
	case 3:
		emit_vec12(ctx, rvb, data, stride, count);
		break;
	case 4:
		emit_vec16(ctx, rvb, data, stride, count);
		break;
	default:
		assert(0);
		exit(1);
		break;
	}

}

void r300EmitElts(GLcontext * ctx, GLuint *elts, unsigned long n_elts)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	struct r300_dma_region *rvb=&rmesa->state.elt_dma;
	unsigned short int *out;
	int i;
	
	r300AllocDmaRegion(rmesa, rvb, (n_elts+1)*2 , 0x20);
	
	out = (unsigned short int *)(rvb->address + rvb->start);
	
	for(i=0; i < n_elts; i++)
		out[i]=(unsigned short int)elts[i];
	
	if(n_elts & 1)
		out[i]=0;
}

/* Emit vertex data to GART memory (unless immediate mode)
 * Route inputs to the vertex processor
 */
void r300EmitArrays(GLcontext * ctx, GLboolean immd)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	r300ContextPtr r300 = rmesa;
	struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
	GLuint nr = 0;
	GLuint count = VB->Count;
	GLuint dw,mask;
	GLuint vic_1 = 0;	/* R300_VAP_INPUT_CNTL_1 */
	GLuint aa_vap_reg = 0; /* VAP register assignment */
	GLuint i;
	GLuint inputs = 0;
	

#define CONFIGURE_AOS(r, f, v, sz, cn) { \
		if (RADEON_DEBUG & DEBUG_STATE) \
			fprintf(stderr, "Enabling "#v "\n"); \
		if (++nr >= R300_MAX_AOS_ARRAYS) { \
			fprintf(stderr, "Aieee! AOS array count exceeded!\n"); \
			exit(-1); \
		} \
		\
		if (rmesa->current_vp == NULL) \
			rmesa->state.aos[nr-1].aos_reg = aa_vap_reg++; \
		rmesa->state.aos[nr-1].aos_format = f; \
		if (immd) { \
			rmesa->state.aos[nr-1].aos_size = 4; \
			rmesa->state.aos[nr-1].aos_stride = 4; \
			rmesa->state.aos[nr-1].aos_offset = 0; \
		} else { \
			emit_vector(ctx, \
						&rmesa->state.aos[nr-1], \
						v->data, \
						sz, \
						v->stride, \
						cn); \
		rmesa->state.vap_reg.r=rmesa->state.aos[nr-1].aos_reg; \
		} \
}

	if (rmesa->current_vp != NULL) {
		if (rmesa->current_vp->inputs[VERT_ATTRIB_POS] != -1) {
			inputs |= _TNL_BIT_POS;
			rmesa->state.aos[nr++].aos_reg = rmesa->current_vp->inputs[VERT_ATTRIB_POS];
		}
		if (rmesa->current_vp->inputs[VERT_ATTRIB_NORMAL] != -1) {
			inputs |= _TNL_BIT_NORMAL;
			rmesa->state.aos[nr++].aos_reg = rmesa->current_vp->inputs[VERT_ATTRIB_NORMAL];
		}
		if (rmesa->current_vp->inputs[VERT_ATTRIB_COLOR0] != -1) {
			inputs |= _TNL_BIT_COLOR0;
			rmesa->state.aos[nr++].aos_reg = rmesa->current_vp->inputs[VERT_ATTRIB_COLOR0];
		}
		if (rmesa->current_vp->inputs[VERT_ATTRIB_COLOR1] != -1) {
			inputs |= _TNL_BIT_COLOR1;
			rmesa->state.aos[nr++].aos_reg = rmesa->current_vp->inputs[VERT_ATTRIB_COLOR1];
		}
		if (rmesa->current_vp->inputs[VERT_ATTRIB_FOG] != -1) {
			inputs |= _TNL_BIT_FOG;
			rmesa->state.aos[nr++].aos_reg = rmesa->current_vp->inputs[VERT_ATTRIB_FOG];
		}
		if(ctx->Const.MaxTextureUnits > 8) { /* Not sure if this can even happen... */
			fprintf(stderr, "%s: Cant handle that many inputs\n", __FUNCTION__);
			exit(-1);
		}
		for (i=0;i<ctx->Const.MaxTextureUnits;i++) {
			if (rmesa->current_vp->inputs[VERT_ATTRIB_TEX0+i] != -1) {
				inputs |= _TNL_BIT_TEX0<<i;
				rmesa->state.aos[nr++].aos_reg = rmesa->current_vp->inputs[VERT_ATTRIB_TEX0+i];
			}
		}
		nr = 0;
	} else {
		inputs = TNL_CONTEXT(ctx)->render_inputs;
	}
	rmesa->state.render_inputs = inputs;

	if (inputs & _TNL_BIT_POS) {
		CONFIGURE_AOS(i_coords,	AOS_FORMAT_FLOAT,
						VB->ObjPtr,
						immd ? 4 : VB->ObjPtr->size,
						count);

		vic_1 |= R300_INPUT_CNTL_POS;
	}

	if (inputs & _TNL_BIT_NORMAL) {
		CONFIGURE_AOS(i_normal,	AOS_FORMAT_FLOAT,
						VB->NormalPtr,
						immd ? 4 : VB->NormalPtr->size,
						count);

		vic_1 |= R300_INPUT_CNTL_NORMAL;
	}

	if (inputs & _TNL_BIT_COLOR0) {
		int emitsize=4;

		if (!immd) {
			if (VB->ColorPtr[0]->size == 4 &&
			    (VB->ColorPtr[0]->stride != 0 ||
			     VB->ColorPtr[0]->data[0][3] != 1.0)) {
				emitsize = 4;
			} else {
				emitsize = 3;
			}
		}

		CONFIGURE_AOS(i_color[0], AOS_FORMAT_FLOAT_COLOR,
						VB->ColorPtr[0],
						immd ? 4 : emitsize,
						count);

		vic_1 |= R300_INPUT_CNTL_COLOR;
	}

	if (inputs & _TNL_BIT_COLOR1) {
		CONFIGURE_AOS(i_color[1], AOS_FORMAT_FLOAT_COLOR,
						VB->SecondaryColorPtr[0],
						immd ? 4 : VB->SecondaryColorPtr[0]->size,
						count);
	}

#if 0
	if (inputs & _TNL_BIT_FOG) {
		CONFIGURE_AOS(	AOS_FORMAT_FLOAT,
						VB->FogCoordPtr,
						immd ? 4 : VB->FogCoordPtr->size,
						count);
	}
#endif

	for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
		if (inputs & (_TNL_BIT_TEX0 << i)) {
			CONFIGURE_AOS(i_tex[i], AOS_FORMAT_FLOAT,
							VB->TexCoordPtr[i],
							immd ? 4 : VB->TexCoordPtr[i]->size,
							count);

			vic_1 |= R300_INPUT_CNTL_TC0 << i;
		}
	}
	

int cmd_reserved=0;
int cmd_written=0;
drm_radeon_cmd_header_t *cmd = NULL;

#define SHOW_INFO(n) do { \
	if (RADEON_DEBUG & DEBUG_ALL) { \
	fprintf(stderr, "RR[%d] - sz=%d, reg=%d, fmt=%d -- st=%d, of=0x%08x\n", \
		n, \
		r300->state.aos[n].aos_size, \
		r300->state.aos[n].aos_reg, \
		r300->state.aos[n].aos_format, \
		r300->state.aos[n].aos_stride, \
		r300->state.aos[n].aos_offset); \
	} \
} while(0);

	/* setup INPUT_ROUTE */
	R300_STATECHANGE(r300, vir[0]);
	for(i=0;i+1<nr;i+=2){
		SHOW_INFO(i)
		SHOW_INFO(i+1)
		dw=(r300->state.aos[i].aos_size-1)
		| ((r300->state.aos[i].aos_reg)<<8)
		| (r300->state.aos[i].aos_format<<14)
		| (((r300->state.aos[i+1].aos_size-1)
		| ((r300->state.aos[i+1].aos_reg)<<8)
		| (r300->state.aos[i+1].aos_format<<14))<<16);

		if(i+2==nr){
			dw|=(1<<(13+16));
			}
		r300->hw.vir[0].cmd[R300_VIR_CNTL_0+(i>>1)]=dw;
		}
	if(nr & 1){
		SHOW_INFO(nr-1)
		dw=(r300->state.aos[nr-1].aos_size-1)
		| (r300->state.aos[nr-1].aos_format<<14)
		| ((r300->state.aos[nr-1].aos_reg)<<8)
		| (1<<13);
		r300->hw.vir[0].cmd[R300_VIR_CNTL_0+(nr>>1)]=dw;
		//fprintf(stderr, "vir0 dw=%08x\n", dw);
		}
	/* Set the rest of INPUT_ROUTE_0 to 0 */
	//for(i=((count+1)>>1); i<8; i++)r300->hw.vir[0].cmd[R300_VIR_CNTL_0+i]=(0x0);
	((drm_r300_cmd_header_t*)r300->hw.vir[0].cmd)->unchecked_state.count = (nr+1)>>1;


	/* Mesa assumes that all missing components are from (0, 0, 0, 1) */
#define ALL_COMPONENTS ((R300_INPUT_ROUTE_SELECT_X<<R300_INPUT_ROUTE_X_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_Y<<R300_INPUT_ROUTE_Y_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_Z<<R300_INPUT_ROUTE_Z_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_W<<R300_INPUT_ROUTE_W_SHIFT))

#define ALL_DEFAULT ((R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_X_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_Y_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_Z_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_ONE<<R300_INPUT_ROUTE_W_SHIFT))

	R300_STATECHANGE(r300, vir[1]);

	for(i=0;i+1<nr;i+=2){
		/* do i first.. */
		mask=(1<<(r300->state.aos[i].aos_size*3))-1;
		dw=(ALL_COMPONENTS & mask)
		| (ALL_DEFAULT & ~mask)
		| R300_INPUT_ROUTE_ENABLE;

		/* i+1 */
		mask=(1<<(r300->state.aos[i+1].aos_size*3))-1;
		dw|=(
		(ALL_COMPONENTS & mask)
		| (ALL_DEFAULT & ~mask)
		| R300_INPUT_ROUTE_ENABLE
		)<<16;

		r300->hw.vir[1].cmd[R300_VIR_CNTL_0+(i>>1)]=dw;
		}
	if(nr & 1){
		mask=(1<<(r300->state.aos[nr-1].aos_size*3))-1;
		dw=(ALL_COMPONENTS & mask)
		| (ALL_DEFAULT & ~mask)
		| R300_INPUT_ROUTE_ENABLE;
		r300->hw.vir[1].cmd[R300_VIR_CNTL_0+(nr>>1)]=dw;
		//fprintf(stderr, "vir1 dw=%08x\n", dw);
		}
	/* Set the rest of INPUT_ROUTE_1 to 0 */
	//for(i=((count+1)>>1); i<8; i++)r300->hw.vir[1].cmd[R300_VIR_CNTL_0+i]=0x0;
	((drm_r300_cmd_header_t*)r300->hw.vir[1].cmd)->unchecked_state.count = (nr+1)>>1;

	/* Set up input_cntl */
	/* I don't think this is needed for vertex buffers, but it doesn't hurt anything */
	R300_STATECHANGE(r300, vic);
	r300->hw.vic.cmd[R300_VIC_CNTL_0]=0x5555;  /* Hard coded value, no idea what it means */
	r300->hw.vic.cmd[R300_VIC_CNTL_1]=vic_1;

#if 0
	r300->hw.vic.cmd[R300_VIC_CNTL_1]=0;

	if(r300->state.render_inputs & _TNL_BIT_POS)
		r300->hw.vic.cmd[R300_VIC_CNTL_1]|=R300_INPUT_CNTL_POS;

	if(r300->state.render_inputs & _TNL_BIT_NORMAL)
		r300->hw.vic.cmd[R300_VIC_CNTL_1]|=R300_INPUT_CNTL_NORMAL;

	if(r300->state.render_inputs & _TNL_BIT_COLOR0)
		r300->hw.vic.cmd[R300_VIC_CNTL_1]|=R300_INPUT_CNTL_COLOR;

	for(i=0;i < ctx->Const.MaxTextureUnits;i++)
		if(r300->state.render_inputs & (_TNL_BIT_TEX0<<i))
			r300->hw.vic.cmd[R300_VIC_CNTL_1]|=(R300_INPUT_CNTL_TC0<<i);
#endif

	/* Stage 3: VAP output */
	R300_STATECHANGE(r300, vof);
	r300->hw.vof.cmd[R300_VOF_CNTL_0]=R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
					| R300_VAP_OUTPUT_VTX_FMT_0__COLOR_PRESENT;

	r300->hw.vof.cmd[R300_VOF_CNTL_1]=0;
	for(i=0;i < ctx->Const.MaxTextureUnits;i++)
		if(r300->state.render_inputs & (_TNL_BIT_TEX0<<i))
			r300->hw.vof.cmd[R300_VOF_CNTL_1]|=(4<<(3*i));

	rmesa->state.aos_count = nr;
}

void r300ReleaseArrays(GLcontext * ctx)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	int i;

	r300ReleaseDmaRegion(rmesa, &rmesa->state.elt_dma, __FUNCTION__);
	for (i=0;i<rmesa->state.aos_count;i++) {
		r300ReleaseDmaRegion(rmesa, &rmesa->state.aos[i], __FUNCTION__);
	}
}