blink_rdp.c 9.41 KB
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
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1995, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

/*
 * File:      blink_rdp.c
 *
 * Highlight particular triangles for debugging or extract them into a 
 * separate display list.  Useful for debugging microcode.
 *
 */

#include "ultra64.h"

/* TEST TEST TEST */
/* #define RDP_STANDALONE */

#ifdef PRINTF
#undef PRINTF
#endif

#ifndef  RDP_STANDALONE
#define PRINTF		osSyncPrintf
#else
#include "stdio.h"
#define PRINTF		printf
#endif

#ifndef ABS
#define ABS(x)			((x) > 0 ? (x) : (-(x)))
#endif

#ifndef MAX
#define MAX(x, y)		((x) > (y) ? (x) : (y))
#endif

#ifndef INRANGE
#define INRANGE(x, min, max)		(((x) >= (min)) && ((x) <= (max)))
#endif

#ifndef SQ
#define SQ(x)		((x) * (x))
#endif

/*
 *  Edge walker coordinate ranges
 */
#define MAX_COORD_X		((float) (1 << 11) - 1)
#define MIN_COORD_X		(- (float) (1 << 11))
#define MAX_COORD_Y		((float) (1 << 11) - 1)
#define MIN_COORD_Y		(- (float) (1 << 11))

/*
 *   G l o b a l s
 */

/*
 *  Flags
 */
static u8       do_blink = FALSE;
static u8       do_extract = FALSE;

/*
 *  User parameters
 */
static u64     *RdpDlIn, *RdpDlOut;
static f32		BlinkX, BlinkY, BlinkRadSq;
static u8		BlinkRed, BlinkGrn, BlinkBlu;

/*
 *  Internal
 */
static f32             _yl,
	                _ym,
	                _yh;
static f32             _xl,
	                _xh,
	                _xm;
static f32             _dxhdy,
	                _dxldy,
	                _dxmdy;
static u64		LastPrimColor;
static u64		LastCombMode;
static u8		LastCycleType;

/*
 *   F u n c t i o n s
 */

static void
save_setcombine(void)
{
	LastCombMode = *RdpDlIn;
}

static void
save_setprimcolor(void)
{
	LastPrimColor = *RdpDlIn;
}

static void
save_cycle_type(void)
{
	union {
		struct {
			u32             pad0:2;
			u32             cmd:6;
			u32             atomic:1;
			u32             reserved0:1;
			u32             cycle_type:2;
			u32             persp_tex_en:1;
			u32             detail_tex_en:1;
			u32             sharpen_tex_en:1;
			u32             tex_lod_en:1;
			u32             en_tlut:1;
			u32             tlut_type:1;
			u32             sample_type:1;
			u32             mid_texel:1;
			u32             bi_lerp_0:1;
			u32             bi_lerp_1:1;
			u32             convert_one:1;
			u32             key_en:1;
			u32             rgb_dither_select:2;
			u32             alpha_dither_select:2;
			u32             reserved1:4;
			u32             b_m1a_0:2;
			u32             b_m1a_1:2;
			u32             b_m1b_0:2;
			u32             b_m1b_1:2;
			u32             b_m2a_0:2;
			u32             b_m2a_1:2;
			u32             b_m2b_0:2;
			u32             b_m2b_1:2;
			u32             reserved2:1;
			u32             force_blend:1;
			u32             alpha_cvg_select:1;
			u32             cvg_times_alpha:1;
			u32             z_mode:2;
			u32             cvg_dest:2;
			u32             color_on_cvg:1;
			u32             image_read_en:1;
			u32             z_update_en:1;
			u32             z_compare_en:1;
			u32             antialias_en:1;
			u32             z_source_sel:1;
			u32             dither_alpha_en:1;
			u32             alpha_compare_en:1;
		} bf;
		u64             word;
	} w0;

	w0.word = *RdpDlIn;
	LastCycleType = w0.bf.cycle_type;
}


static void
get_tri_verts(void)
{
	u32             *buffer;
	s16             YL,
	                YM,
	                YH;
	s32             XL,
	                XH,
	                XM;
	s32             DxLDy,
	                DxHDy,
	                DxMDy;

	buffer = (u32 *) RdpDlIn;

	YL = buffer[0] & 0x0000ffff;
	YM = (buffer[1] & 0xffff0000) >> 16;
	YH = buffer[1] & 0x0000ffff;

	XL = *((s32 *) &(buffer[2]));
	DxLDy = *((s32 *) &(buffer[3]));
	XH = *((s32 *) &(buffer[4]));
	DxHDy = *((s32 *) &(buffer[5]));
	XM = *((s32 *) &(buffer[6]));
	DxMDy = *((s32 *) &(buffer[7]));

	_yl = (f32) YL / 4.0;
	_ym = (f32) YM / 4.0;
	_yh = (f32) YH / 4.0;

	_xl = (f32) XL / 65536.0;
	_xm = (f32) XM / 65536.0;
	_xh = (f32) XH / 65536.0;

	_dxhdy = (f32) DxHDy / 65536.0;
	_dxldy = (f32) DxLDy / 65536.0;
	_dxmdy = (f32) DxMDy / 65536.0;

}


static int
in_radius(void)
{
	f32 x0, x1;

	if(_yh <= BlinkY && BlinkY <= _yl) {
		/* find x on major edge at BlinkY */
		x0 = _xh + _dxhdy * (BlinkY - _yh);

		/* find x on minor edge at BlinkY */
		if(BlinkY > _ym) {
			x1 = _xl + _dxldy * (BlinkY - _ym);	
		} else {
			x1 = _xm + _dxmdy * (BlinkY - _yh);	
		}

		/* sort x's and do compare */
		if(x0 > x1) {
			if(x1 <= BlinkX && BlinkX <= x0)
				return(1);
		} else {
			if(x0 <= BlinkX && BlinkX <= x1)
				return(1);
		}
	}

	return(0);
}


static void
blink_tri(u32 nwords)
{
	u32 i;

	/*
	 *  Is tri within radius of cursor?
	 */
	if(in_radius() && do_extract) {
		/*
		 *  insert prim color and combine mode
		 */
		gDPPipeSync((Gfx *)RdpDlOut++);
		gDPSetPrimColor((Gfx *)RdpDlOut++, 0, 0, BlinkRed, BlinkGrn, BlinkBlu, 255);
	
		if(LastCycleType == G_CYC_1CYCLE) {
			gDPSetCombineMode((Gfx *)RdpDlOut++, G_CC_PRIMITIVE, G_CC_PRIMITIVE);
		} else {
			gDPSetCombineMode((Gfx *)RdpDlOut++, G_CC_PRIMITIVE, G_CC_PASS2);
		}

		/*
		 *  Copy triangle
		 */
		for(i = 0; i < nwords; i++) {
			*RdpDlOut++ = *RdpDlIn++;
		}
	
		/*
		 *  Restore previous combine mode and prim color
		 */
		gDPPipeSync((Gfx *)RdpDlOut++);
		if(LastPrimColor)
			*RdpDlOut++ = LastPrimColor;
		if(LastCombMode)
			*RdpDlOut++ = LastCombMode;

	} else if (!do_extract) {

		/*
		 *  Copy triangle
		 */
		for(i = 0; i < nwords; i++) {
			*RdpDlOut++ = *RdpDlIn++;
		}
	}
}
	
	
/*
 *  Parse RDP display list.
 */
static void
read_rdp(void)
{
	u8              op;
	u32				i;

	/*
	 *  Command ID
	 */
	op = (u8) (((*RdpDlIn) & 0xff00000000000000LLU) >> 56);

	/*
	 *  Blink only triangles
	 */
	switch (op) {

		case  G_SETCIMG:
		case  G_SETMIMG:
		case  G_SETTIMG:
		case  G_SETENVCOLOR:
		case  G_SETBLENDCOLOR:
		case  G_SETFOGCOLOR:
		case  G_SETFILLCOLOR:
		case  G_FILLRECT:
		case  G_SETTILE:
		case  G_LOADTILE:
		case  G_LOADBLOCK:
		case  G_SETTILESIZE:
		case  G_LOADTLUT:
		case  G_SETPRIMDEPTH:
		case  G_SETSCISSOR:
		case  G_SETCONVERT:
		case  G_SETKEYR:
		case  G_SETKEYGB:
		case  G_RDPFULLSYNC:
		case  G_RDPTILESYNC:
		case  G_RDPPIPESYNC:
		case  G_RDPLOADSYNC:
		case  G_NOOP:
			*RdpDlOut++ = *RdpDlIn++;
			break;

		case  G_TEXRECTFLIP:
		case  G_TEXRECT:
			*RdpDlOut++ = *RdpDlIn++;
			*RdpDlOut++ = *RdpDlIn++;;
			break;

		case  G_RDPSETOTHERMODE:
			save_cycle_type();
			*RdpDlOut++ = *RdpDlIn++;
			break;

		case  G_SETCOMBINE:
			save_setcombine();
			*RdpDlOut++ = *RdpDlIn++;
			break;

		case  G_SETPRIMCOLOR:
			save_setprimcolor();
			*RdpDlOut++ = *RdpDlIn++;
			break;

		case  G_TRI_FILL:
			get_tri_verts();
			blink_tri(4); /* updates pointers */
			break;

		case  G_TRI_SHADE:
			get_tri_verts();
			blink_tri(12); /* updates pointers */
			break;

		case  G_TRI_TXTR:
			get_tri_verts();
			blink_tri(12); /* updates pointers */
			break;

		case  G_TRI_SHADE_TXTR:
			get_tri_verts();
			blink_tri(20); /* updates pointers */
			break;

		case  G_TRI_FILL_ZBUFF:
			get_tri_verts();
			blink_tri(6); /* updates pointers */
			break;

		case  G_TRI_SHADE_ZBUFF:
			get_tri_verts();
			blink_tri(14); /* updates pointers */
			break;

		case  G_TRI_TXTR_ZBUFF:
			get_tri_verts();
			blink_tri(14); /* updates pointers */
			break;

		case  G_TRI_SHADE_TXTR_ZBUFF:
			get_tri_verts();
			blink_tri(22); /* updates pointers */
			break;

		default:
			break;
	}
}

/*
 * external interface to rdp display list blink
 */
void
guBlinkRdpDL(u64 *rdp_dl_in, u64 nbytes_in, 
			 u64 *rdp_dl_out, u64 *nbytes_out, 
			 u32 x, u32 y, u32 radius,
			 u8  red, u8 green, u8 blue,
			 u8 flags)
{
	rmonPrintf("made it, blink\n");
	do_blink = flags & GU_BLINKRDP_HILITE;
	do_extract = flags & GU_BLINKRDP_EXTRACT;

	LastPrimColor = 0;
	LastCombMode = 0;

	BlinkX = x;
	BlinkY = y;
	BlinkRadSq = SQ(radius);

	BlinkRed = red;
	BlinkGrn = green;
	BlinkBlu = blue;

	rmonPrintf("do_blink %d, do_extract %d, BlinkX %d, BlinkY %d, BlinkRadSq %d\n", do_blink, do_extract, BlinkX, BlinkY, BlinkRadSq);

	rmonPrintf("BlinkRed %d, BlinkGrn %d, BlinkBlu %d\n", BlinkRed, BlinkGrn, BlinkBlu);

	/*
	 *  Global pointer, updated by function that parse commands
	 */
	RdpDlIn  = rdp_dl_in;
	RdpDlOut = rdp_dl_out;


	while (RdpDlIn != (rdp_dl_in + (nbytes_in / sizeof(u64)))) {
		read_rdp();
	}

	*nbytes_out = (u64)((u32)RdpDlOut - (u32)rdp_dl_out);
}


#ifdef RDP_STANDALONE

#define MY_BUF_SIZE		10000

main(int argc, char **argv)
{
	u64 	buffer[MY_BUF_SIZE];
	FILE 	*fp;
	int		i;

	if(argc != 2) {
		fprintf(stderr,"Usage: %s file.rdp\n", argv[0]);
		exit(1);
	}

	if((fp = fopen(argv[1],"rb")) == NULL) {
		fprintf(stderr,"Could not open %s\n", argv[1]);
		exit(1);
	}

	i = 0;
	while((fread(&buffer[i++], sizeof(u64), 1, fp) == 1) && 
			(i < MY_BUF_SIZE))
		;

	parse_rdp(buffer, i-1, 0, 0, 0);
	exit(0);
}

#endif /* RDP_STANDALONE */