Mercurial > hg > Game > Cerium
view TaskManager/Test/test_render/spe/DrawSpan.cpp @ 186:06cdf8baa989 draft
fix
author | gongo@localhost.localdomain |
---|---|
date | Tue, 06 Jan 2009 16:18:37 +0900 |
parents | a19d3ed4ce5b |
children | fe854a6ee9e2 |
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 SchedDefineTask(DrawSpan); /** * テクスチャは、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; } void DrawSpan::linebuf_init(int *buf, int x, int rgb) { for (int i = 0; i < x; i++) { buf[i] = rgb; } } /** * Span が使う Texture Tile があるか * * @retval != NULL 存在する * @retval NULL 存在しない */ TilePtr DrawSpan::isAvailableTile(uint32 *addr) { return hash->get(addr); } void DrawSpan::set_rgb(uint32 *addr) { TilePtr tile; if (!isAvailableTile(addr)) { 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); smanager->dma_wait(TEX_LOAD); } } void DrawSpan::set_rgbs(uint32 *addr, uint32 *max_addr) { uint32 start = (uint32)addr; uint32 end = (uint32)max_addr; uint32 length = end-start; uint32 diff = sizeof(uint32)*TEXTURE_BLOCK_SIZE; uint32 max_tile = 8; for (uint32 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) { TaskPtr renew_task = smanager->create_task(TASK_DRAW_SPAN2); int rangey = smanager->get_param(2); /** * 共通の outData, param はそのまま渡す */ for (int i = 0; i < rangey; i++) { renew_task->add_outData(smanager->get_outputAddr(i), smanager->get_outputSize(i)); } // rangex_start, rangex_end, rangey renew_task->add_param(smanager->get_param(0)); renew_task->add_param(smanager->get_param(1)); renew_task->add_param(smanager->get_param(2)); /** * 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); /** * 再起動したタスクを待つ */ smanager->wait_task(renew_task); // next_spack は free() するので wait する smanager->dma_wait(SPAN_PACK_LOAD); } int DrawSpan::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_input(0); SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); SpanPackPtr free_spack = next_spack; // next_spack の free() 用 Span *span; 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 = 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; 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: free(free_spack); free(linebuf); free(zRow); return 0; }