Mercurial > hg > Members > kono > Cerium
view TaskManager/Test/test_render/spe/CreateSpan.cpp @ 155:77dac07efd79
TextureInfoList がずれてたのを修正
author | gongo@gendarme.local |
---|---|
date | Thu, 04 Dec 2008 13:58:41 +0900 |
parents | 9642aeef298d |
children | cd5ad7adc5e1 |
line wrap: on
line source
#include "CreateSpan.h" #include "viewer_types.h" static const int SPAN_PACK_LOAD = 5; static const int SPAN_PACK_STORE = 6; static const int POLYGON_PACK_LOAD = 7; static const int TILE_ALLOCATE = 8; static const int TILE_LOAD = 9; static const int TILE_STORE = 10; static SpanPackPtr spack = NULL; static SpanPackPtr send_spack = NULL; static int prev_index = 0; SchedDefineTask(CreateSpan); static float calc(float f1, float f2,int i, float base) { float ans; ans = f1/f2*i + base; return ans; } /** * TrianglePack から、vMin, vMid, vMax を求める * * @param [triPack] TrianglePack * @param [vMin] [vMid] [vMax] */ static void make_vertex(TrianglePack *triPack, VertexPackPtr *vMin, VertexPackPtr *vMid, VertexPackPtr *vMax) { if (triPack->ver1.y <= triPack->ver2.y) { if (triPack->ver2.y <= triPack->ver3.y) { *vMin = &triPack->ver1; *vMid = &triPack->ver2; *vMax = &triPack->ver3; } else if (triPack->ver3.y <= triPack->ver1.y) { *vMin = &triPack->ver3; *vMid = &triPack->ver1; *vMax = &triPack->ver2; } else { *vMin = &triPack->ver1; *vMid = &triPack->ver3; *vMax = &triPack->ver2; } } else { if (triPack->ver1.y <= triPack->ver3.y) { *vMin = &triPack->ver2; *vMid = &triPack->ver1; *vMax = &triPack->ver3; } else if (triPack->ver3.y <= triPack->ver2.y) { *vMin = &triPack->ver3; *vMid = &triPack->ver2; *vMax = &triPack->ver1; } else { *vMin = &triPack->ver2; *vMid = &triPack->ver3; *vMax = &triPack->ver1; } } } static void make_vMid10(VertexPack *v, VertexPack *vMin, VertexPack *vMid, VertexPack *vMax) { //int d, d1; float d; int d1; d = vMax->y - vMin->y; d1 = (int)(vMid->y - vMin->y); v->tex_x = calc(vMax->tex_x - vMin->tex_x, d, d1, vMin->tex_x); v->tex_y = calc(vMax->tex_y - vMin->tex_y, d, d1, vMin->tex_y); v->x = calc(vMax->x - vMin->x, d, d1, vMin->x); v->y = vMid->y; v->z = calc(vMax->z - vMin->z, d, d1, vMin->z); } /** * Span の1ドットずつ、必要なテクスチャのブロック(Tile)を求めていく * */ void CreateSpan::setTile(SpanPtr span) { TileInfoListPtr tilelist = (TileInfoListPtr)smanager->allocate(sizeof(TileInfoList)); TileInfoListPtr send_tilelist = (TileInfoListPtr)smanager->allocate(sizeof(TileInfoList)); TileInfoListPtr tilelist_addr; TileInfoListPtr send_tilelist_addr; TileInfoPtr tile; int cur_x = 0; int len = 0; smanager->mainMem_wait(); tilelist_addr = (TileInfoListPtr)smanager->mainMem_get(TILE_ALLOCATE); span->tilelist = tilelist_addr; float tex1 = span->tex_x1; float tex2 = span->tex_x2; float tey1 = span->tex_y1; float tey2 = span->tex_y2; int tex_xpos; int tex_ypos; int end = span->length_x; if (span->length_x == 1) { tilelist->init(); tile = &tilelist->tileinfo[tilelist->size++]; tex_xpos = (int)((span->tex_width-1) * tex1); tex_ypos = (int)((span->tex_height-1) * tey1); tile->tix = tex_xpos; tile->tiy = tex_ypos; tile->tile = span->tex_addr; tile->padding = 1; } else { while (cur_x <= span->length_x) { if (cur_x + MAX_TILE_LIST - 1 < span->length_x) { len = MAX_TILE_LIST - 1; smanager->mainMem_alloc(TILE_ALLOCATE, sizeof(TileInfoList)); } else { len = span->length_x - cur_x; } float tex_x, tex_y; tilelist->init(); for (int i = cur_x; i <= cur_x + len; i++) { tex_x = tex1*(end-1-i)/(end-1) + tex2*i/(end-1); tex_y = tey1*(end-1-i)/(end-1) + tey2*i/(end-1); if (tex_x > 1) tex_x = 1; if (tex_y > 1) tex_y = 1; tex_xpos = (int)((span->tex_width-1) * tex_x); tex_ypos = (int)((span->tex_height-1) * tex_y); tile = &tilelist->tileinfo[tilelist->size++]; tile->tix = tex_xpos; tile->tiy = tex_ypos; tile->padding = i; int blockX = tex_xpos / TEXTURE_SPLIT_PIXEL; int blockY = tex_ypos / TEXTURE_SPLIT_PIXEL; int block = blockX + (span->tex_width/TEXTURE_SPLIT_PIXEL)*blockY; uint32 *addr = (uint32*)span->tex_addr; tile->tile = (void*)(addr + block*64); //tile->tile = span->tex_addr; } if (cur_x + MAX_TILE_LIST - 1 < span->length_x) { TileInfoListPtr tmp = tilelist; tilelist = send_tilelist; send_tilelist = tmp; send_tilelist_addr = tilelist_addr; smanager->mainMem_wait(); tilelist_addr = (TileInfoListPtr)smanager->mainMem_get(TILE_ALLOCATE); send_tilelist->next = tilelist_addr; smanager->dma_wait(TILE_STORE); smanager->dma_store(send_tilelist, (uint32)send_tilelist_addr, sizeof(TileInfoList), TILE_STORE); } cur_x += MAX_TILE_LIST; } } smanager->dma_wait(TILE_STORE); tilelist->next = NULL; smanager->dma_store(tilelist, (uint32)tilelist_addr, sizeof(TileInfoList), TILE_STORE); free(send_tilelist); smanager->dma_wait(TILE_STORE); free(tilelist); } /** * x軸に水平な辺を持つ三角形ポリゴンから、 * Span を抜き出す */ void CreateSpan::half_triangle(SpanPackPtr *spackList, int charge_y_top, int charge_y_end, long *tex_addr, long tex_width, long tex_height, VertexPack *vMin,VertexPack *vMid,VertexPack *vMid10) { float tmp_z,tmp_tex1, tmp_tex2 ,tmp_tey1,tmp_tey2; float tmp_xpos,tmp_end,tmp_zpos; float start_z, end_z; float start_tex_x, end_tex_x, start_tex_y, end_tex_y; int x, y, length; #if 1 // これじゃないと // テクスチャの貼りに微妙に隙間が。謎だ int start_y = (int)vMid->y; int end_y = (int)vMin->y; #else float start_y = vMid->y; float end_y = vMin->y; #endif float div_y = start_y - end_y; int k = 0; int l = 1; SpanPackPtr tmp_spack; /** * 三角形ポリゴンをx軸に水平に二つに分けようとして * ある一辺がすでに水平だった場合、つまり * (環境によっては、back slash が 円マークかも) * * |\ * | \ * | \ * ----- * * * 上のようなポリゴンだった場合は、本来なら上の部分の三角形にだけ * half_triangle の処理をするべきだが、現在の処理だと * この half_triangle に「上の部分の三角形」と、 * 「『下の部分の三角形と判断してしまった』直線」が来てしまう。 * 直線の部分が来ると、calc() で 0 除算とかで、値不定で暴走するので * 現在はこれで代用。 * half_triangle 呼ぶ前にこれを判断できれば良いかもしれない。 * てかこんなんでいいのかよ。。。 * */ #if 1 if ((int)div_y == 0) { return; } #else if (vMid10->x == vMin->x && vMid10->y == vMin->y) { return; } #endif if (div_y < 0) { div_y = -div_y; k = 1; l = -1; } for (int i = k; i < (int)div_y+1; i++) { y = (int)vMin->y + i*l; /** * 担当 y 範囲内 */ if (charge_y_top <= y && y <= charge_y_end) { // 1..8 を index0, 9..16 を index1 にするために y を -1 int index = (y-1) / split_screen_h; //if (index != 0) continue; /** * 違う SpanPack を扱う場合、 * 現在の SpanPack をメインメモリに送り、 * 新しい SpanPack を取ってくる */ if (index != prev_index) { tmp_spack = spack; spack = send_spack; send_spack = tmp_spack; smanager->dma_wait(SPAN_PACK_STORE); smanager->dma_store(send_spack, (uint32)spackList[prev_index], sizeof(SpanPack), SPAN_PACK_STORE); smanager->dma_load(spack, (uint32)spackList[index], sizeof(SpanPack), SPAN_PACK_LOAD); prev_index = index; smanager->dma_wait(SPAN_PACK_LOAD); } /** * 書き込む SpanPack が満杯だったら * メインメモリで allocate した領域 (next) を持ってきて * 現在の spack->next につなぎ、next を次の spack とする。 */ if (spack->info.size >= MAX_SIZE_SPAN) { SpanPackPtr next; smanager->mainMem_alloc(0, sizeof(SpanPack)); smanager->mainMem_wait(); next = (SpanPackPtr)smanager->mainMem_get(0); spack->next = next; tmp_spack = spack; spack = send_spack; send_spack = tmp_spack; smanager->dma_wait(SPAN_PACK_STORE); smanager->dma_store(send_spack, (uint32)spackList[index], sizeof(SpanPack), SPAN_PACK_STORE); spackList[index] = next; smanager->dma_load(spack, (uint32)spackList[index], sizeof(SpanPack), SPAN_PACK_LOAD); smanager->dma_wait(SPAN_PACK_LOAD); spack->init((index+1)*split_screen_h); } } else { /** * 担当範囲外だったら無視 */ continue; } tmp_xpos = calc(vMid10->x - vMin->x ,div_y, i, vMin->x); tmp_end = calc(vMid->x - vMin->x ,div_y, i, vMin->x); tmp_z = calc(vMid10->z - vMin->z ,div_y, i, vMin->z); tmp_zpos = calc(vMid->z - vMin->z ,div_y, i, vMin->z); length = (tmp_xpos > tmp_end) ? (int)tmp_xpos - (int)tmp_end : (int)tmp_end - (int)tmp_xpos; if (length == 0) { continue; } /** * メインメモリに、Tile 用の領域確保をここで指定しておく */ // ちと仕様変更によりここでは使わない // 予めやっておいて、その間に処理、だとちょっと動作が怪しいらしい smanager->mainMem_alloc(TILE_ALLOCATE, sizeof(TileInfoList)); tmp_tex1 =((i/(div_y)) * vMid10->tex_x) + ( ((div_y - i)/(div_y)) * vMin->tex_x); tmp_tex2 =( (i/(div_y)) * vMid->tex_x) + ( ((div_y - i)/(div_y)) * vMin->tex_x); tmp_tey1 =( (i/(div_y)) * vMid10->tex_y) + ( ((div_y - i)/(div_y)) * vMin->tex_y); tmp_tey2 =( (i/(div_y)) * vMid->tex_y) + ( ((div_y - i)/(div_y)) * vMin->tex_y); if (tmp_xpos > tmp_end) { x = (int)tmp_end; /** * +1 は要らない気がする・・・ */ //length = (int)(tmp_xpos)-(int)(tmp_end)+1; length = (int)(tmp_xpos)-(int)(tmp_end); start_z = tmp_zpos; end_z = tmp_z; start_tex_x = tmp_tex2; end_tex_x = tmp_tex1; start_tex_y = tmp_tey2; end_tex_y = tmp_tey1; } else { x = (int)tmp_xpos; //length = (int)(tmp_end)-(int)(tmp_xpos)+1; length = (int)(tmp_end)-(int)(tmp_xpos); start_z = tmp_z; end_z = tmp_zpos; start_tex_x = tmp_tex1; end_tex_x = tmp_tex2; start_tex_y = tmp_tey1; end_tex_y = tmp_tey2; } smanager->dma_wait(SPAN_PACK_LOAD); Span *span = &spack->span[spack->info.size++]; span->tex_addr = tex_addr; span->tex_width = tex_width; span->tex_height = tex_height; span->x = x; span->y = y; span->length_x = length; span->start_z = start_z; span->end_z = end_z; span->tex_x1 = start_tex_x; span->tex_x2 = end_tex_x; span->tex_y1 = start_tex_y; span->tex_y2 = end_tex_y; // TilePtr の計算 // tix, tiy (Tile 内での x, y) // //smanager->mainMem_wait(); setTile(span); } } int CreateSpan::run(void *rbuf, void *wbuf) { PolygonPack *pp = (PolygonPack*)smanager->get_input(0); PolygonPack *next_pp = (PolygonPack*)smanager->allocate(sizeof(PolygonPack)); PolygonPack *free_pp = next_pp; PolygonPack *tmp_pp; TrianglePackPtr triPack; VertexPackPtr vMin, vMid, vMax; VertexPackPtr vMid10 = (VertexPackPtr)smanager->allocate(sizeof(VertexPack)); SpanPackPtr *spackList = (SpanPackPtr*)smanager->get_input(1); spack = (SpanPackPtr)smanager->get_input(2); send_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); prev_index = get_param(0); // spack と send_spack は swap しながら DMA を繰り返すので // 自分で allocate した send_spack を覚えてないといけない SpanPackPtr free_spack = send_spack; int charge_y_top = get_param(1); int charge_y_end = get_param(2); do { if (pp->next != NULL) { smanager->dma_load(next_pp, (uint32)pp->next, sizeof(PolygonPack), POLYGON_PACK_LOAD); } else { next_pp = NULL; } for (int i = 0; i < pp->info.size; i++) { triPack = &pp->tri[i]; #if 0 make_vertex(triPack, &vMin, &vMid, &vMax); #else if (triPack->ver1.y <= triPack->ver2.y) { if (triPack->ver2.y <= triPack->ver3.y) { vMin = &triPack->ver1; vMid = &triPack->ver2; vMax = &triPack->ver3; } else if (triPack->ver3.y <= triPack->ver1.y) { vMin = &triPack->ver3; vMid = &triPack->ver1; vMax = &triPack->ver2; } else { vMin = &triPack->ver1; vMid = &triPack->ver3; vMax = &triPack->ver2; } } else { if (triPack->ver1.y <= triPack->ver3.y) { vMin = &triPack->ver2; vMid = &triPack->ver1; vMax = &triPack->ver3; } else if (triPack->ver3.y <= triPack->ver2.y) { vMin = &triPack->ver3; vMid = &triPack->ver2; vMax = &triPack->ver1; } else { vMin = &triPack->ver2; vMid = &triPack->ver3; vMax = &triPack->ver1; } } #endif make_vMid10(vMid10, vMin, vMid, vMax); /** * ポリゴンを、x軸に水平に分割して二つの三角形を作り、 * それぞれから Span を求める */ half_triangle(spackList, charge_y_top, charge_y_end, triPack->tex_addr, triPack->tex_width, triPack->tex_height, vMin, vMid, vMid10); half_triangle(spackList, charge_y_top, charge_y_end, pp->tri[0].tex_addr, pp->tri[0].tex_width, pp->tri[0].tex_height, vMax, vMid, vMid10); } smanager->dma_wait(POLYGON_PACK_LOAD); tmp_pp = pp; pp = next_pp; next_pp = tmp_pp; } while (pp); smanager->dma_wait(SPAN_PACK_STORE); smanager->dma_store(spack, (uint32)spackList[prev_index], sizeof(SpanPack), SPAN_PACK_STORE); smanager->dma_wait(SPAN_PACK_STORE); free(free_pp); free(free_spack); free(vMid10); return 0; }