Mercurial > hg > Game > Cerium
view TaskManager/Test/test_render/task/DrawSpan.cpp @ 203:1eba8570808c draft
fix CreateSpan::run
author | gongo@localhost.localdomain |
---|---|
date | Mon, 26 Jan 2009 18:30:35 +0900 |
parents | 1ff24ca2f016 |
children | e75f9eb97180 |
line wrap: on
line source
#include <stdlib.h> #include <string.h> #include "DrawSpan.h" #include "polygon_pack.h" #include "texture.h" #include "viewer_types.h" #include "Func.h" #define SPAN_PACK_LOAD 0 #define TEX_LOAD 1 #define FB_STORE 2 SchedDefineTask(DrawSpan); DrawSpan::~DrawSpan(void) { smanager->dma_wait(FB_STORE); free((void*)((int)linebuf*doneWrite)); } /** * テクスチャは、TEXTURE_SPLIT_PIXEL^2 のブロックに分割する * * +---+---+---+---+---+---+ * | 0 | 1 | 2 | 3 | 4 | 5 | * +---+---+---+---+---+---+ * | | | | | |11 | * +---+---+---+---+---+---+ * | | | | | |17 | * +---+---+---+---+---+---+ * | | | | | |23 | * +---+---+---+---+---+---+ * | | | | | |29 | * +---+---+---+---+---+---+ * | | | | | |35 | * +---+---+---+---+---+---+ * * 一辺を TEXTURE_SPLIT とする * 各ブロックの数字がブロックIDとなる。 */ /** * テクスチャの座標から、 * テクスチャのどのブロックかを求める * * @param[in] tx X coordinates of texture * @param[in] tx Y coordinates of texture * @param[in] twidth Width of texture * @return block ID */ int DrawSpan::getTexBlock(int tx, int ty, int twidth) { int blockX, blockY; blockX = tx / TEXTURE_SPLIT_PIXEL; blockY = ty / TEXTURE_SPLIT_PIXEL; return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY; } /** * block ID と、テクスチャの TOP address から * (tx,ty) で使われるテクスチャの Tile addres を求める * * @param[in] tx X coordinates of texture * @param[in] tx Y coordinates of texture * @param[in] tw Width of texture * @param[in] tex_addr_top (tx,ty) で使うテクスチャの先頭address * @return block ID */ uint32* DrawSpan::getTile(int tx, int ty, int tw, uint32 *tex_addr_top) { int block = getTexBlock(tx, ty, tw); return tex_addr_top + block*TEXTURE_BLOCK_SIZE; } /** * FrameBuffer に書き込む rgb の領域初期化 * * @param width Width of Buffer * @param height Height of Buffer * @param rgb Initial value of RGB at Buffer * @return Buffer */ int* DrawSpan::linebuf_init(int width, int height, int rgb) { int *buf = (int*)smanager->allocate(sizeof(int)*width*height); for (int i = 0; i < width*height; i++) { buf[i] = rgb; } return buf; } /** * Z-Buffer の初期化 * * @param width Width of Z-Buffer * @param height Height of Z-Buffer * @return Z-Buffer */ float* DrawSpan::zRow_init(int width, int height) { float *buf = (float*)smanager->allocate(sizeof(float)*width*height); float def = 65535.0f; for (int i = 0; i < width*height; i++) { buf[i] = def; } return buf; } /** * Span が使う Texture Tile があるか * * @retval != NULL 存在する * @retval NULL 存在しない */ TilePtr DrawSpan::isAvailableTile(uint32 *addr) { return hash->get(addr); } void DrawSpan::set_rgb(uint32 *addr) { TilePtr tile; tile = tileList->nextTile(); /** * FIFO なので、もし前のが残っていれば削除 */ hash->remove(tile->texture_addr); tile->texture_addr = addr; hash->put(tile->texture_addr, tile); smanager->dma_load(tile->pixel, (uint32)addr, sizeof(uint32)*TEXTURE_BLOCK_SIZE, TEX_LOAD); } void DrawSpan::set_rgbs(uint32 *addr, uint32 *max_addr) { uint32 start = (uint32)addr; uint32 end = (uint32)max_addr; int length = (int)end-start; int diff = sizeof(int)*TEXTURE_BLOCK_SIZE; int max_tile = 16; for (int i = 0, j = 0; i <= length && j < max_tile; i += diff, j++) { set_rgb((uint32*)(start + i)); } } uint32 DrawSpan::get_rgb(int tx, int ty, uint32 *addr) { TilePtr tile; tile = hash->get(addr); return tile->pixel[(TEXTURE_SPLIT_PIXEL)*ty+tx]; } /** * DrawSpan の再起動 (DrawSpanRenew 生成) * * @param[in] spack 現在処理している SpanPack * @param[in] cur_span_x span->length_x != 1 の時の Span の処理で * どこまで進んでいるか */ void DrawSpan::reboot(SpanPackPtr spack, int cur_span_x) { DrawSpanArgPtr args = (DrawSpanArgPtr)smanager->allocate(sizeof(DrawSpanArg)); TaskPtr renew_task = smanager->create_task(TASK_DRAW_SPAN2); // 数が多いので構造体で渡す args->display = smanager->get_param(0); args->screen_width = smanager->get_param(1); args->rangex_start = smanager->get_param(2); args->rangex_end = smanager->get_param(3); args->rangey = smanager->get_param(4); renew_task->add_param((int)args); /** * SpanPack は続きから開始するので、 * 現在の状態をコピーしておく。 * spack は rbuf から取得してる可能性があり * rbuf はシステムが自動的に free() するため * アドレスだけ渡すのはNG */ SpanPackPtr curr = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); memcpy(curr, spack, sizeof(SpanPack)); renew_task->add_param((int)curr); renew_task->add_param(cur_span_x); // linebuf と zRow も引き継がせる renew_task->add_param((int)linebuf); renew_task->add_param((int)zRow); /** * 再起動したタスクを待つ */ smanager->wait_task(renew_task); // next_spack は free() するので wait する smanager->dma_wait(SPAN_PACK_LOAD); } void DrawSpan::writebuffer(unsigned int display, int buf_width, int height, int screen_width) { for (int i = 0; i < height; i++) { smanager->dma_store(&linebuf[i*buf_width], display + (sizeof(int)*screen_width*i), sizeof(int)*buf_width, FB_STORE); } doneWrite = 1; } int DrawSpan::run(void *rbuf, void *wbuf) { SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0); SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); SpanPackPtr free_spack = next_spack; // next_spack の free() 用 Span *span; uint32 display = smanager->get_param(0); int screen_width = smanager->get_param(1); int rangex_start = smanager->get_param(2); int rangex_end = smanager->get_param(3); // このタスクが担当する x の範囲 int rangex = rangex_end - rangex_start + 1; // y の範囲 int rangey = smanager->get_param(4); hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH); tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); zRow = zRow_init(rangex, rangey); linebuf = linebuf_init(rangex, rangey, 0xffffffff); doneWrite = 0; 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 = 0; t < spack->info.size; t++) { span = &spack->span[t]; uint32 rgb = 0x0000ff00; 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; if (!isAvailableTile(tex_addr)) { set_rgb(tex_addr); smanager->dma_wait(TEX_LOAD); } rgb = get_rgb(tex_localx, tex_localy, tex_addr); zRow[localx + (rangex*localy)] = zpos; linebuf[localx + (rangex*localy)] = 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; 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; /** * Tile が無い場合、一旦タスクはここで中断し、 * Tile をロードするタスクを走らせた後に再起動する */ if (!isAvailableTile(tex_addr)) { spack->info.start = t; #if 1 set_rgbs(tex_addr, getTile(span->tex_width-1, tex_ypos, span->tex_width, span->tex_addr)); //smanager->dma_wait(TEX_LOAD); reboot(spack, j); goto FINISH; #else set_rgb(tex_addr); smanager->dma_wait(TEX_LOAD); #endif } rgb = get_rgb(tex_localx, tex_localy, tex_addr); zRow[localx + (rangex*localy)] = tex_z; linebuf[localx + (rangex*localy)] = rgb; } } } } smanager->dma_wait(SPAN_PACK_LOAD); SpanPackPtr tmp_spack = spack; spack = next_spack; next_spack = tmp_spack; } while (spack); writebuffer(display, rangex, rangey, screen_width); // linebuf は、writebuffer() の dma_store を wait する // DrawSpan::~DrawSpan() 内で free する。 //free(linebuf); free(zRow); FINISH: /** * goto FINISH; の時は reboot なので * linebuf, zRow は free() しない */ free(free_spack); return 0; }