mem_span.c 11.3 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

#include "graphic.h"

do_mem_span()
{
  struct MS_span_data *ms;
  struct Color_depth *cd;
  int x_cur, x_cnt, x_inc;
  int base;
  int new_color, old_color;
  int new_depth, old_depth;
  int new_delta, old_delta;
  int dzx,dzy,delta;
  int nr,ng,nb, na;
  int or,og,ob;
  int rr,gg,bb;
  int mask;
  int half_mask;
  int alpha;
  int ncvg, ocvg;
  int nsca, osca;
  int temp, i;

  ms = &mem_span_data[0];
  cd = &col_dep[0];
  base = ms->color.base;
  
  /*
    if (ms->x_left <= ms->x_right)
    do_memtraffic(ms->x_left, ms->x_right, base);
    */

  x_cnt = ms->x_right - ms->x_left;

  if (ms->left) {
    x_cur = ms->x_left;
    x_inc = 1;
  } else {
    x_cur = ms->x_right;
    x_inc = -1;
  }
  
  dzx = abs(dzdx);
  dzy = abs(dzdy);
  temp = dzx+dzy;
  new_delta = 0x00010000;
  for (i=0 ;i<16; i++)
      if ((temp>>16) > (1<<i)) new_delta = 1<<(i+17);
  if (new_delta <= 0) new_delta = 0x7FFFFFFF;
  
  while (x_cnt >= 0) {
    do_coverage( x_cur, &ms->s_left, &ms->s_right, &mask);
    do_modulate(mask);
    new_color = cd->c;
    if (en_coverage) {	/* only coverage modes are supported by hardware */
      if (en_antialias) {
	half_mask = mask & AA_SP_HALF;
	mask_to_count( half_mask, &ncvg);
      } else {
	if (mask & AA_SP_CENTER) {
	  ncvg = 8;
	} else {
	  ncvg = 0;
	}
      }
      if (ncvg>0) {
	get_pixel( x_cur, base, &old_color);
	unpack_color(old_color,or,og,ob);
	unpack_colora(new_color,nr,ng,nb,na);
	get_filter( x_cur, base, &ocvg);
	ocvg++;
	if (en_zbuffer) {
	  new_depth = cd->d;
	  get_zbuffer( x_cur, base, &old_depth, &old_delta);
	  delta = (new_delta>old_delta) ? new_delta : old_delta;
	  /* z buffered rendering modes */
	  if (en_line && (render_mode != RENDER_OPAQUE)) {
	    switch (render_mode) {
	    case RENDER_TRANSP:
	      if ((old_depth == 0x7FFFFFFF) ||
		  ((new_depth) < old_depth)) {
		/* visible */
		/* blend by alpha times coverage */
		/* and add coverage times alpha to pixel coverage */
		if (en_antialias) 
		  nsca = na * ncvg >> 3;
		else
		  nsca = na;
                rr = blend_color( or, nr, nsca);
                gg = blend_color( og, ng, nsca);
                bb = blend_color( ob, nb, nsca);
                new_color = pack_color(rr,gg,bb);
		put_pixel( x_cur, base, new_color);
		if (en_antialias) 
		  ncvg = times_alpha( ncvg, na) + ocvg;
		else
		  ncvg += ocvg;
		if (ncvg>8) ncvg=8;
		put_filter( x_cur, base, ncvg-1);
	      } /* end visible */
	      break;
	    case RENDER_DECAL:
	      if ((old_depth != 0x7FFFFFFF) &&
		  ((new_depth-delta) <= old_depth) &&
		  ((new_depth+delta) >= old_depth) ) {
		/* visible */
		/* within z range of surface, blend by alpha * coverage */
		/* but don't update surface coverage */
		if (en_antialias) 
		  nsca = na * ncvg >> 3;
		else
		  nsca = na;
                rr = blend_color( or, nr, nsca);
                gg = blend_color( og, ng, nsca);
                bb = blend_color( ob, nb, nsca);
                new_color = pack_color(rr,gg,bb);
		put_pixel( x_cur, base, new_color);
	      } /* end visible */
	      break;
	    } /* end switch render mode */
	  } else { /* surfaces or opaque lines */
	    switch (render_mode) {
	    case RENDER_TEXEDGE:
	      /* attenuate coverage by alpha from texture edge */
	      /* fall thru to opaque if > 0 */
	      ncvg = times_alpha( ncvg, (na+(na>>7))); /* bump to 1.0 */
	      if (ncvg < 1) break;
	    case RENDER_OPAQUE:
	      if ((old_depth == 0x7FFFFFFF) ||
		  (((ncvg+ocvg) <= 8) && ((new_depth-delta) <= old_depth)) ||
		  (((ncvg+ocvg) > 8) && (new_depth < old_depth))) {
		/* visible */
		if (((new_depth+delta) >= old_depth) &&
		    ((ncvg+ocvg) <= 8) &&
		    en_antialias) {
		  /* merge color and sum coverage if within range */
		  nsca = old_delta>>16;
		  osca = new_delta>>16;
		  rr = (ncvg*nr*nsca+ocvg*or*osca)/(ncvg*nsca+ocvg*osca);
		  gg = (ncvg*ng*nsca+ocvg*og*osca)/(ncvg*nsca+ocvg*osca);
		  bb = (ncvg*nb*nsca+ocvg*ob*osca)/(ncvg*nsca+ocvg*osca);
		  new_color = pack_color(rr,gg,bb);
		  put_pixel( x_cur, base, new_color);
		  ncvg += ocvg;
		  put_filter( x_cur, base, ncvg-1);
		  put_zbuffer( x_cur, base, new_depth, new_delta);
		} else {
		  put_pixel( x_cur, base, new_color);
		  put_filter( x_cur, base, ncvg-1);
		  put_zbuffer( x_cur, base, new_depth, new_delta);
		}
	      } /* end visible */
	      break;
	    case RENDER_TRANSP:
	      if ((old_depth == 0x7FFFFFFF) ||
		  (new_depth < old_depth)) {
		/* visible */
		if ((ncvg+ocvg) > 8) {
		  /* wrap coverage and blend if overlap */
		  ncvg = ncvg + ocvg - 8;
		  put_filter( x_cur, base, ncvg-1);
		  rr = blend_color( or, nr, na);
		  gg = blend_color( og, ng, na);
		  bb = blend_color( ob, nb, na);
		  new_color = pack_color(rr,gg,bb);
		  put_pixel( x_cur, base, new_color);
		} else {
		  /* sum coverage if partial */
		  ncvg += ocvg;
		  put_filter( x_cur, base, ncvg-1);
		}
	      } /* end visible */
	      break;
	    case RENDER_DECAL:
	      if ((old_depth != 0x7FFFFFFF) &&
		  ((new_depth-delta) <= old_depth) &&
		  ((new_depth+delta) >= old_depth)) {
		/* visible */
		if (((new_depth+delta) >= old_depth) &&
		    ((ncvg+ocvg) <= 8) &&
		    en_antialias) {
		  /* merge color and sum coverage if partial */
		  nsca = old_delta>>16;
		  osca = new_delta>>16;
		  rr = (ncvg*nr*nsca+ocvg*or*osca)/(ncvg*nsca+ocvg*osca);
		  gg = (ncvg*ng*nsca+ocvg*og*osca)/(ncvg*nsca+ocvg*osca);
		  bb = (ncvg*nb*nsca+ocvg*ob*osca)/(ncvg*nsca+ocvg*osca);
		  new_color = pack_color(rr,gg,bb);
		  put_pixel( x_cur, base, new_color);
		  ncvg += ocvg;
		  put_filter( x_cur, base, ncvg-1);
		} else {
		  /* write new color and wrap if overlap */
		  put_pixel( x_cur, base, new_color);
		  ncvg = ncvg /* + ocvg - 8*/ ;
		  put_filter( x_cur, base, ncvg-1);
		}
	      } /* end visible */
	      break;
	    case RENDER_INTERPEN:
	      if ((old_depth == 0x7FFFFFFF) ||
		  (((ncvg+ocvg) <= 8) && ((new_depth-delta) <= old_depth)) ||
		  (((ncvg+ocvg) > 8) && (new_depth < old_depth))) {
		/* visible */
		if (((new_depth+delta) >= old_depth) &&
		    ((ncvg+ocvg) <= 8) &&
		    en_antialias) {
		  /* merge color and sum coverage if within range */
		  nsca = old_delta>>16;
		  osca = new_delta>>16;
		  rr = (ncvg*nr*nsca+ocvg*or*osca)/(ncvg*nsca+ocvg*osca);
		  gg = (ncvg*ng*nsca+ocvg*og*osca)/(ncvg*nsca+ocvg*osca);
		  bb = (ncvg*nb*nsca+ocvg*ob*osca)/(ncvg*nsca+ocvg*osca);
		  new_color = pack_color(rr,gg,bb);
		  put_pixel( x_cur, base, new_color);
		  ncvg += ocvg;
		  put_filter( x_cur, base, ncvg-1);
		  put_zbuffer( x_cur, base, new_depth, new_delta);
		} else {
		  if ((new_depth < old_depth) &&
		      ((new_depth+delta) >= old_depth) &&
		      ((ncvg+ocvg) > 8)) {
		    /* coverage times z difference scaled by delta */
		    ncvg = ncvg * (old_depth-new_depth) / delta;
		    if (ncvg < 1) break;
		  }
		  put_pixel( x_cur, base, new_color);
		  put_filter( x_cur, base, ncvg-1);
		  put_zbuffer( x_cur, base, new_depth, new_delta);
		}
	      } /* end visible */
	      break;
	    } /* end switch render mode */
	  } /* end surface or opaque lines */
	} else {	/* not z buffered */
	  if (en_line && (render_mode != RENDER_OPAQUE)) {
	    switch (render_mode) {
	    case RENDER_TRANSP:
	      /* blend by alpha times coverage */
	      /* and add coverage times alpha to pixel coverage */
	      if (en_antialias) 
		nsca = na * ncvg >> 3;
	      else
		nsca = na;
	      rr = blend_color( or, nr, nsca);
	      gg = blend_color( og, ng, nsca);
	      bb = blend_color( ob, nb, nsca);
	      new_color = pack_color(rr,gg,bb);
	      put_pixel( x_cur, base, new_color);
	      if (en_antialias) 
		ncvg = times_alpha( ncvg, na) + ocvg;
	      else
		ncvg += ocvg;
	      if (ncvg>8) ncvg=8;
	      put_filter( x_cur, base, ncvg-1);
	      break;
	    case RENDER_DECAL:
	      /* blend by alpha * coverage */
	      /* zap surface coverage */
	      if (en_antialias) 
		nsca = na * ncvg >> 3;
	      else
		nsca = na;
	      rr = blend_color( or, nr, nsca);
	      gg = blend_color( og, ng, nsca);
	      bb = blend_color( ob, nb, nsca);
	      new_color = pack_color(rr,gg,bb);
	      put_pixel( x_cur, base, new_color);
	      put_filter( x_cur, base, 7); /* force full cvg */
	      break;
	    } /* end switch render mode */
	  } else { /* surfaces or opaque lines */
	    switch (render_mode) {
	    case RENDER_TEXEDGE:
	      /* attenuate coverage by alpha from texture edge */
	      /* fall thru to opaque if > 0 */
	      ncvg = times_alpha( ncvg, (na+(na>>7))); /* bump to 1.0 */
	      if (ncvg < 1) break;
	    case RENDER_DECAL:
	      /* same as render_opaque */
	    case RENDER_OPAQUE:
	      /* point sample surface */
	      put_pixel( x_cur, base, new_color);
	      ncvg += ocvg;
	      if (ncvg>8) ncvg -= 8;
	      put_filter( x_cur, base, ncvg-1);
	      break;
	    case RENDER_TRANSP:
	      if ((ncvg+ocvg) > 8) {
		/* wrap coverage and blend if overlap */
		ncvg = ncvg + ocvg - 8;
		put_filter( x_cur, base, ncvg-1);
		rr = blend_color( or, nr, na);
		gg = blend_color( og, ng, na);
		bb = blend_color( ob, nb, na);
		new_color = pack_color(rr,gg,bb);
		put_pixel( x_cur, base, new_color);
	      } else {
		/* sum coverage if partial */
		ncvg += ocvg;
		put_filter( x_cur, base, ncvg-1);
	      }
	      break;
	    } /* end switch render mode */
	  } /* end surface or opaque lines */
	}
      }
    }	/* end of coverage stuff (non-coverage modes not implemented in hdw) */
    else {
      if (en_antialias) {
	/* mask buffer: read, and/or mask, write */
	mask_to_alpha( mask, &alpha);
	get_pixel( x_cur, base, &old_color);
	do_blend( alpha, new_color, old_color, &new_color);
	put_pixel( x_cur, base, new_color);
      } else {
	if (mask & AA_SP_CENTER)
	  put_pixel( x_cur, base, new_color);
      }
    }
    x_cnt--;
    x_cur += x_inc;
  }
}

#define	PIXEL_SIZE		2	/* in bytes */
#define	WORD_SIZE		8	/* in bytes */
#define	BURST_SIZE		8	/* in words */
#define	PAGE_SIZE		2048	/* in bytes */

#define	CLOCK			66	/* Mhz */
#define	CLK_WORD		1	/* clock/word */
#define	CLK_BURST		3
#define	CLK_PAGE		8

static int	npix = 0;
static int	nword = 0;
static int	nburst = 0;
static int	npage = 0;

do_memtraffic(lx, rx, y)
int	lx, rx, y;
{
	extern int	xsize;
	extern int	zoom;

	int		saddr, eaddr, paddr;

/*
if (debug) printf("y %3d\tlx %3d\trx %3d\t ", y, lx, rx);
*/

	saddr = (y*xsize/zoom+lx) * PIXEL_SIZE;
	eaddr = (y*xsize/zoom+rx) * PIXEL_SIZE;
	if (eaddr/PAGE_SIZE != saddr/PAGE_SIZE) {
		paddr = (eaddr/PAGE_SIZE)*PAGE_SIZE;
		do_memburst(saddr, paddr-1);
		do_memburst(paddr, eaddr);
	}
	else
		do_memburst(saddr, eaddr);
}

do_memburst(saddr, eaddr)
int saddr, eaddr;
{
	static int	cur_page = 0;
	int		pix, word, burst, page, pagemiss;

	/*
	 * Words written
	 */

	word = (eaddr/WORD_SIZE) - (saddr/WORD_SIZE) + 1;

	/*
	 * Write burst
	 */

	burst = word/BURST_SIZE;
	if (word % BURST_SIZE)
		burst++;

	/*
	 * Page miss
	 */

	page = saddr/PAGE_SIZE;
	if (page != cur_page) {
		pagemiss = 1;
		cur_page = page;
	}
	else
		pagemiss = 0;

	/*
	 * Pixels written
	 */

	pix = (eaddr - saddr)/PIXEL_SIZE + 1;

	npix += pix;
	nword += word;
	nburst += burst;
	npage += pagemiss;

/*
if (debug) printf("np %3d\tnw %3d\tnb %3d\tpm %3d\n", pix, word, burst, pagemiss);
*/
}

print_memcycle()
{
	int	clocks;

	printf("page miss\t%d\n", npage);
	printf("bursts\t\t%d\n", nburst);
	printf("words\t\t%d\n", nword);
	printf("pixels\t\t%d\n", npix);

	clocks = npage * CLK_PAGE + nburst * CLK_BURST + nword * CLK_WORD;
	printf("\nclocks\t\t%d\n", clocks);
	printf("clocks/pix\t%5.2f\n", (float)clocks/npix);
}