view TaskManager/Test/test_render/spe/DrawSpanRenew.cpp @ 187:ac52c139ad45

fix
author gongo@localhost.localdomain
date Thu, 08 Jan 2009 13:46:57 +0900
parents f7ad032575ed
children 06f39635a9b0
line wrap: on
line source

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

#define SPAN_PACK_LOAD 0
#define TEX_LOAD 1

SchedDefineTask(DrawSpanRenew);

int
DrawSpanRenew::run(void *rbuf, void *wbuf)
{
    hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH);
    tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST);

    int rangex_start  = smanager->get_param(0);
    int rangex_end    = smanager->get_param(1); 

    // このタスクが担当する x の範囲
    int rangex        = rangex_end - rangex_start + 1;

    // y の範囲 (render_y + rangey - 1)
    int rangey        = smanager->get_param(2);
    
    float *zRow = (float*)smanager->allocate(sizeof(float)*rangex*rangey);

    for (int i = 0; i < rangex*rangey; i++) {
	zRow[i] = 65535.0f;
    }

    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, 0xffffffff);
    }

    SpanPackPtr spack = (SpanPackPtr)smanager->get_param(3);
    SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack));
    Span *span;
    
    // span->length_x の処理での再起動位置
    int js_cont = smanager->get_param(4);

    do {
	/**
	 * SpanPack->next が存在する場合、
	 * 現在の SpanPack を処理してる間に
	 * 次の SpanPack の DMA 転送を行う
	 */
	if (spack->next != NULL) {
	    smanager->dma_load(next_spack, (uint32)spack->next,
			       sizeof(SpanPack), SPAN_PACK_LOAD);
	} else {
	    next_spack = NULL;
	}

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

	    Uint32 rgb = 0x00ff00;
	    float tex1 = span->tex_x1;
	    float tex2 = span->tex_x2;
	    float tey1 = span->tex_y1;
	    float tey2 = span->tex_y2;

	    /**
	     * Span が持つ 1 pixel 毎の
	     * テクスチャの座標
	     */
	    int tex_xpos;
	    int tex_ypos;

	    /**
	     * (tex_xpos, tex_ypos) の、ブロック内(上の図参照)での座標と
	     * そのブロックのアドレス(MainMemory)
	     */
	    int tex_localx;
	    int tex_localy;
	    uint32 *tex_addr;

	    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);

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

		tex_xpos = (int)((span->tex_width-1) * tex1);
		tex_ypos = (int)((span->tex_height-1) * tey1);

		if (zpos < zRow[localx + (rangex * localy)]) {
		    tex_addr = getTile(tex_xpos, tex_ypos,
				       span->tex_width, span->tex_addr);
		    tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL;
		    tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL;

		    /**
		     * Tile が無い場合、一旦タスクはここで中断し、
		     * Tile をロードするタスクを走らせた後に再起動する
		     */
		    if (!isAvailableTile(tex_addr)) {
			spack->info.start = t;
			set_rgb(tex_addr);
			//set_rgbs(tex_addr,
			//getTile(span->tex_width-1, tex_ypos,
			//span->tex_width, span->tex_addr));
			//reboot(spack, 0);
			//goto FINISH;
		    }
		    
		    rgb = get_rgb(tex_localx, tex_localy, tex_addr);

		    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;
		float tex_x, tex_y, tex_z;
		
		/**
		 * 一回比較すれば、以後再起動するまでは
		 * js_cont は使わないから 0 にしてるわけだけど、
		 * 最初の一回のためだけにこれはめんどくさいのー。
		 */
		js = (js < js_cont) ? js_cont : js;
		js_cont = 0;

		for (int j = js; j <= je; j++) {
		    localx = getLocalX(x-1+j);

		    tex_z = z*(x_len-1-j)/(x_len-1) + zpos*j/(x_len-1);

		    tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1);
		    tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1);
		    if (tex_x > 1) tex_x = 1;
		    if (tex_x < 0) tex_x = 0;
		    if (tex_y > 1) tex_y = 1;
		    if (tex_y < 0) tex_y = 0;
		    tex_xpos = (int)((span->tex_width-1) * tex_x);
		    tex_ypos = (int)((span->tex_height-1) * tex_y);
		    
		    if (tex_z < zRow[localx + (rangex*localy)]) {
			tex_addr = getTile(tex_xpos, tex_ypos,
					   span->tex_width, span->tex_addr);
			tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL;
			tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL;
			goto FINISH;
			if (!isAvailableTile(tex_addr)) {
			    spack->info.start = t;
			    set_rgb(tex_addr);
			    //set_rgbs(tex_addr,
			    //getTile(span->tex_width-1, tex_ypos,
			    //span->tex_width, span->tex_addr));
			    //reboot(spack, j);
			    //goto FINISH;
			}
			
			rgb = get_rgb(tex_localx, tex_localy, tex_addr);
		    
			zRow[localx + (rangex*localy)] = tex_z;
			linebuf[localy][localx] = rgb;
		    }
		}
	    }
	}

	smanager->dma_wait(SPAN_PACK_LOAD);

	SpanPackPtr tmp_spack = spack;
	spack = next_spack;
	next_spack = tmp_spack;
    } while (spack);


FINISH:
    free(next_spack);
    free(linebuf);
    free(zRow);

    // Renew したタスクで allocate されたものなので、これも free
    free(spack);

    return 0;
}