view TaskManager/Test/test_render/task/DrawSpanRenew.cpp @ 188:06f39635a9b0

メモりリーク箇所発見:TaskManager/Test/test_render/ChangeLog参照
author gongo@localhost.localdomain
date Thu, 08 Jan 2009 15:34:19 +0900
parents f7ad032575ed
children 102dad2949a0
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));
    SpanPackPtr free_spack1 = spack;
    SpanPackPtr free_spack2 = next_spack;
    Span *span;
    
    // span->length_x νǤκƵư
    int js_cont = smanager->get_param(4);

    smanager->dma_wait(TEX_LOAD);

    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 = 0x00ff0000;
	    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;
			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:
    smanager->dma_wait(SPAN_PACK_LOAD);
    //free(next_spack);
    free(linebuf);
    free(zRow);

    // Renew  allocate 줿ΤʤΤǡ free
    //free(spack);

    free(free_spack1);
    free(free_spack2);


    return 0;
}