view TaskManager/Test/test_render/task/DrawSpan.cpp @ 161:1f4c3f3238e6 fullHD_omedetou

texture の座標がマイナスになったあと、それを 0 にし忘れてた
author gongo@localhost.localdomain
date Mon, 08 Dec 2008 16:37:02 +0900
parents cd5ad7adc5e1
children 38cbb7aecc70
line wrap: on
line source

#include <stdlib.h>
#include <string.h>
#include "DrawSpan.h"
#include "polygon_pack.h"
#include "SpanPack.h"
#include "texture.h"
#include "viewer_types.h"

#define SPAN_PACK_LOAD 0
#define TEX_LOAD 1
#define TILE_INFO_LOAD 2

SchedDefineTask(DrawSpan);

static const int hashsize = 263;

static TilePtr hash_table[hashsize] = {0};

unsigned short PRIME[8] = {
    0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
};

static TileListPtr tileList;

static int
hash(uint32 data)
{
    int value = 0;
    int n = 0;
    int key;

    for (int i = 0; i < 8; i ++) {
	key = data & 0xf;
	value += key * PRIME[n++];
	data >>= 4;
    }

    return value % hashsize;
}

static int
put(void *key, TilePtr data)
{
    int hashval = hash((uint32)key);

    for (int i = 0; i < hashsize/2; i++) {
	int index = (hashval + i*i)%hashsize;

	if (hash_table[index] == 0) {
	    hash_table[index] = data;
	    return index;
	}
    }

    return -1;
}

static TilePtr
get(void *key)
{
    int hashval = hash((uint32)key);

    for (int i = 0; i < hashsize/2; i++) {
	int index = (hashval + i*i)%hashsize;
	
	if (hash_table[index] != NULL &&
	    hash_table[index]->texture_addr == key) {
	    return hash_table[index];
	}
    }

    return NULL;
}

void
DrawSpan::linebuf_init(int *buf, int x, int rgb)
{
    for (int i = 0; i < x; i++) {
	buf[i] = rgb;
    }
}

float*
DrawSpan::zRow_init(int w, int h)
{
    float *zRow = NULL;
    float z = 65535.0f;
    int length = w*h;

    zRow = (float*)smanager->allocate(sizeof(float)*length);

    for (int i = 0; i < length; i++) {
	zRow[i] = z;
    }

    return zRow;
}


char*
DrawSpan::get_pixel(int tx, int ty, void *texture_image)
{
    return (char*)texture_image+(4*((TEXTURE_SPLIT_PIXEL)*ty+tx));
}

Uint32
DrawSpan::get_rgb(int tx, int ty, void *addr)
{
    Uint8 red, green, blue, alpha;
    TilePtr tile;

    /**
     * get,put ϥ֥(HashȤ)äƥ뤫
     */
    tile = get(addr);
    if (tile == NULL) {
	if (tileList->size >= MAX_TILE) {
	    tileList->init();
	    bzero(hash_table, sizeof(TilePtr)*hashsize);
	}

	tile = &tileList->tile[tileList->size];
	tile->texture_addr = addr;

	smanager->dma_load(tile->pixel, (uint32)addr,
			   sizeof(uint32)*TEXTURE_BLOCK_SIZE, TEX_LOAD);

	int index = put(tile->texture_addr, tile);

	/**
	 * TODO:
	 *   ʤäĤ
	 *   ޤǤΤĤ褷Ƥ³򤹤Ȥ
	 */
	if (index < 0) {
	    printf("[%p] Can't entry\n", tile);
	    return 0xff0000;
	}

	tileList->size++;

	smanager->dma_wait(TEX_LOAD);
    }

    char *p = get_pixel(tx, ty, tile->pixel);
    
    alpha = 255;
    red   = (Uint8) p[0];
    green = (Uint8) p[1];
    blue  = (Uint8) p[2];

    return (red & 0xff) * 0x10000 + (green & 0xff) * 0x100
	+ (blue & 0xff) + (alpha << 24);
}

int
DrawSpan::run(void *rbuf, void *wbuf)
{
    SpanPack *sp = (SpanPack*)smanager->get_input(0);
    SpanPack *next_sp =
	(SpanPack*)smanager->allocate(sizeof(SpanPack));
    SpanPack *free_sp = next_sp; // next_sp  free() 
    SpanPack *tmp_sp = NULL;
    Span *span;

    TileInfoListPtr tilist =
	(TileInfoListPtr)smanager->allocate(sizeof(TileInfoList));
    TileInfoListPtr next_tilist =
	(TileInfoListPtr)smanager->allocate(sizeof(TileInfoList));

    tileList = (TileListPtr)smanager->allocate(sizeof(TileList));
    tileList->init();

    void *texture_image = global_get(TEXTURE_ID);    
    int rangex_start  = get_param(0); // Υô x ϰϤλ
    int rangex_end    = get_param(1); //  (start <= x <= end)
    int rangey        = get_param(2); // y ϰ (render_y + rangey - 1)
    int rangex        = rangex_end - rangex_start + 1;

    float *zRow = zRow_init(rangex, rangey);

    int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);

    for (int i = 0; i < rangey; i++) {
	linebuf[i] = (int*)smanager->get_output(i);
	linebuf_init(linebuf[i], rangex, 0xffffff);
    }

    bzero(hash_table, sizeof(TilePtr)*hashsize);

    do {
	/**
	 * SpanPack->next ¸ߤ硢
	 * ߤ SpanPack Ƥ֤
	 *  SpanPack  DMA žԤ
	 */
	if (sp->next != NULL) {
	    smanager->dma_load(next_sp, (uint32)sp->next,
			       sizeof(SpanPack), SPAN_PACK_LOAD);
	} else {
	    next_sp = NULL;
	}

	for (int t = 0; t < sp->info.size; t++) {	  
	    span = &sp->span[t];

	    smanager->dma_load(tilist, (uint32)span->tilelist,
			       sizeof(TileInfoList), TILE_INFO_LOAD);

	    Uint32 rgb = 0x00ff00;
	    int tex_xpos;
	    int tex_ypos;
	    int tex_zpos;
	    int x = span->x;
	    int y = span->y;
	    int x_len = span->length_x;
	    float z = span->start_z;
	    float zpos = span->end_z;

	    // ɸ [0 .. split_screen_w-1] 褦 x,y  -1
	    int localx = getLocalX(x-1);
	    int localy = getLocalY(y-1);

	    smanager->dma_wait(TILE_INFO_LOAD);

	    if (x_len == 1) {
		if (x < rangex_start || rangex_end < x) {
		    continue;
		}

		tex_xpos = tilist->tileinfo[0].tix;
		tex_ypos = tilist->tileinfo[0].tiy;
		tex_zpos = (int)z;
		texture_image = tilist->tileinfo[0].tile;

		if (zpos < zRow[localx + (rangex * localy)]) {
		    rgb = get_rgb(tex_xpos, tex_ypos, texture_image);
		    zRow[localx + (rangex * localy)] = zpos;
		    linebuf[localy][localx] = rgb;
		}
	    } else {
		int js = (x < rangex_start) ? rangex_start - x : 0;
		int je = (x + x_len > rangex_end) ? rangex_end - x : x_len;
		
		if (js > je) continue;

		int cur_x = 0;
		int max_x = je;
		int len = 0;

		/**
		 * Span ɽʤϰ([js..je])  TileInfoList 
		 * äƤΤǡФ
		 */
		while (cur_x + MAX_TILE_LIST <= js) {
		    smanager->dma_load(next_tilist, (uint32)tilist->next,
				       sizeof(TileInfoList), TILE_INFO_LOAD);
		    smanager->dma_wait(TILE_INFO_LOAD);

		    TileInfoListPtr tmp = tilist;
		    tilist = next_tilist;
		    next_tilist = tmp;

		    cur_x += MAX_TILE_LIST;
		}

		cur_x = js;

		while (cur_x <= max_x) {
		    /**
		     * TileInfoList  30 ĤǶڤƤΤ
		     * cur_x  TileInfoList 椫ϤޤäƤ
		     * MAX_TILE_LIST ˼ޤͤ
		     * (ex.)
		     *   cur_x (=js)  0 Ϥޤä硢
		     *     cur_x = 0, len = 29
		     *   cur_x (=js)  15 Ϥޤä硢
		     *     cur_x = 15, len = 14 ˤʤ
		     */
		    int cur_x_diff = MAX_TILE_LIST - (cur_x % MAX_TILE_LIST);

		    if (cur_x + cur_x_diff - 1 < max_x) {
			smanager->dma_load(next_tilist, (uint32)tilist->next,
					   sizeof(TileInfoList),
					   TILE_INFO_LOAD);
			len = cur_x_diff - 1;
		    } else {
			len = max_x - cur_x;
		    }

		    for (int j = cur_x; j <= cur_x + len; j++) {
			TileInfoPtr tinfo = &tilist->tileinfo[j%MAX_TILE_LIST];
			float tex_z
			     = z*(x_len-1-j)/(x_len-1) + zpos*j/(x_len-1);

			localx = getLocalX(x-1+j);

			tex_xpos = tinfo->tix;
			tex_ypos = tinfo->tiy;
			texture_image = tinfo->tile;

			if (tex_z < zRow[localx + (rangex*localy)]) {
			    rgb = get_rgb(tex_xpos, tex_ypos, texture_image);
			    zRow[localx + (rangex*localy)] = tex_z;
			    linebuf[localy][localx] = rgb;
			}
		    }
		    
		    smanager->dma_wait(TILE_INFO_LOAD);
		    
		    TileInfoListPtr tmp = tilist;
		    tilist = next_tilist;
		    next_tilist = tmp;
		    
		    cur_x += cur_x_diff;
		}
	    }
	}

	smanager->dma_wait(SPAN_PACK_LOAD);

	tmp_sp = sp;
	sp = next_sp;
	next_sp = tmp_sp;
    } while (sp);

    free(free_sp);
    free(linebuf);
    free(zRow);
    free(tileList);
    free(tilist);
    free(next_tilist);

    return 0;
}