Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/spe/DrawSpan.cc @ 614:4e44147d78ee
remove uncessary Task Name definision
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 14 Nov 2009 18:21:14 +0900 |
parents | e923249c318a |
children | 360032cc500e |
rev | line source |
---|---|
507 | 1 #include <stdlib.h> |
2 #include <string.h> | |
3 #include "DrawSpan.h" | |
4 #include "polygon_pack.h" | |
5 #include "texture.h" | |
6 #include "viewer_types.h" | |
7 #include "Func.h" | |
594 | 8 #include "sys.h" |
507 | 9 #include "global_alloc.h" |
10 | |
11 SchedDefineTask(DrawSpan); | |
12 SchedDefineTask1(DrawSpanEnd,draw_span_end); | |
13 | |
14 #define TEX_LOAD1 0 | |
15 #define TEX_LOAD2 1 | |
16 #define SPAN_PACK_LOAD 2 | |
17 #define FB_STORE 3 | |
18 | |
19 static int | |
20 draw_span_end(SchedTask *s, void *rbuf, void *wbuf) | |
21 { | |
22 Gptr g = (Gptr)s->get_param(0); | |
23 s->dma_wait(FB_STORE); | |
24 free((void*)((int)g->linebuf*g->doneWrite)); | |
25 free(g); | |
26 return 0; | |
27 } | |
28 | |
29 /** | |
30 * テクスチャは、TEXTURE_SPLIT_PIXEL^2 のブロックに分割する | |
31 * | |
32 * +---+---+---+---+---+---+ | |
33 * | 0 | 1 | 2 | 3 | 4 | 5 | | |
34 * +---+---+---+---+---+---+ | |
35 * | | | | | |11 | | |
36 * +---+---+---+---+---+---+ | |
37 * | | | | | |17 | | |
38 * +---+---+---+---+---+---+ | |
39 * | | | | | |23 | | |
40 * +---+---+---+---+---+---+ | |
41 * | | | | | |29 | | |
42 * +---+---+---+---+---+---+ | |
43 * | | | | | |35 | | |
44 * +---+---+---+---+---+---+ | |
45 * | |
46 * 一辺を TEXTURE_SPLIT とする | |
47 * 各ブロックの数字がブロックIDとなる。 | |
48 */ | |
49 | |
50 /** | |
51 * テクスチャの座標から、 | |
52 * テクスチャのどのブロックかを求める | |
53 * | |
54 * @param[in] tx X coordinates of texture | |
55 * @param[in] tx Y coordinates of texture | |
56 * @param[in] twidth Width of texture | |
57 * @return block ID | |
58 */ | |
59 static int | |
60 getTexBlock(int tx, int ty, int twidth) | |
61 { | |
62 int blockX, blockY; | |
63 | |
64 blockX = tx / TEXTURE_SPLIT_PIXEL; | |
65 blockY = ty / TEXTURE_SPLIT_PIXEL; | |
66 | |
67 return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY; | |
68 } | |
69 | |
70 /** | |
71 * block ID と、テクスチャの TOP address から | |
72 * (tx,ty) で使われるテクスチャの Tile addres を求める | |
73 * | |
74 * @param[in] tx X coordinates of texture | |
75 * @param[in] tx Y coordinates of texture | |
76 * @param[in] tw Width of texture | |
77 * @param[in] tex_addr_top (tx,ty) で使うテクスチャの先頭address | |
78 * @return block ID | |
79 */ | |
80 static memaddr | |
81 getTile(int tx, int ty, int tw, memaddr tex_addr_top) | |
82 { | |
83 int block = getTexBlock(tx, ty, tw); | |
594 | 84 return tex_addr_top + block * TEXTURE_BLOCK_SIZE * sizeof(uint32); |
507 | 85 } |
86 | |
87 /** | |
88 * FrameBuffer に書き込む rgb の領域初期化 | |
89 * | |
90 * @param width Width of Buffer | |
91 * @param height Height of Buffer | |
92 * @param rgb Initial value of RGB at Buffer | |
93 * @return Buffer | |
94 */ | |
95 static int* | |
96 linebuf_init(SchedTask *smanager, int width, int height, int rgb) | |
97 { | |
98 int *buf = (int*)smanager->allocate(sizeof(int)*width*height); | |
99 | |
100 for (int i = 0; i < width*height; i++) { | |
101 buf[i] = rgb; | |
102 } | |
103 | |
104 return buf; | |
105 } | |
106 | |
107 /** | |
108 * Z-Buffer の初期化 | |
109 * | |
110 * @param width Width of Z-Buffer | |
111 * @param height Height of Z-Buffer | |
112 * @return Z-Buffer | |
113 */ | |
114 static float* | |
115 zRow_init(SchedTask *smanager, int width, int height) | |
116 { | |
117 float *buf = (float*)smanager->allocate(sizeof(float)*width*height); | |
118 float def = 65535.0f; | |
119 | |
594 | 120 for (int i = 0; i < width*height; i++) { |
121 buf[i] = def; | |
507 | 122 } |
123 | |
124 return buf; | |
125 } | |
126 | |
127 | |
128 static uint32 | |
129 get_rgb(int tx, int ty, TilePtr tile) | |
130 { | |
131 uint32 *data = (uint32 *)tile->data; | |
132 return data[(TEXTURE_SPLIT_PIXEL)*ty+tx]; | |
133 } | |
134 | |
135 | |
136 static void | |
137 writebuffer(SchedTask *smanager, Gptr g, unsigned int display, | |
594 | 138 int buf_width, int height, int screen_width) |
507 | 139 { |
140 for (int i = 0; i < height; i++) { | |
141 smanager->dma_store(&g->linebuf[i*buf_width], | |
142 display + (sizeof(int)*screen_width*i), | |
143 sizeof(int)*buf_width, FB_STORE); | |
144 } | |
145 | |
146 g->doneWrite = 1; | |
147 } | |
148 | |
149 /** | |
150 * zRow と Linebuf を更新する | |
151 * | |
152 * @param zpos 更新する pixel のZ座標 | |
153 * @param rangex このタスクが処理する描画領域の x の長さ | |
154 * @param x pixel の、描画領域内での x 座標 | |
155 * @param y 〃 の、y 座標 | |
156 * @param tex_x pixel が使用するテクスチャの、Tile (8x8) 内での x 座標 | |
157 * @param tex_y 〃 の y 座標 | |
158 * @param tex_addr テクスチャのアドレス(MainMemory) | |
159 */ | |
160 static void | |
594 | 161 updateBuffer(Gptr g, float zpos, int rangex, int x, int y, int tex_x, int tex_y, |
162 float normal_x, float normal_y, float normal_z, TilePtr tile) | |
507 | 163 { |
594 | 164 |
507 | 165 |
594 | 166 int color = get_rgb(tex_x, tex_y, tile); |
167 /*下位4bitを抽出*/ | |
599 | 168 int alpha = color & 0xff000000; |
594 | 169 /*完全に透けているか判断*/ |
170 int flag = (alpha != 0); | |
171 | |
172 color = infinity_light_calc(color,normal_x,normal_y,normal_z); | |
173 | |
174 g->zRow[x + (rangex*y)] = zpos*flag + g->zRow[x + (rangex*y)]*(1-flag); | |
175 g->linebuf[x + (rangex*y)] = color*flag + g->linebuf[x + (rangex*y)]*(1-flag); | |
176 | |
507 | 177 } |
178 | |
179 /** | |
180 * 長さが 1 の Span の描画 (要するに 1 pixel) | |
181 * | |
182 * @param span Span | |
183 * @param startx 描画開始範囲 | |
184 * @param endx 描画終了範囲 | |
185 */ | |
186 static int | |
187 drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
188 { | |
189 int rangex = endx - startx + 1; | |
190 | |
594 | 191 float normal_x = span->normal_x; |
192 float normal_y = span->normal_y; | |
193 float normal_z = span->normal_z; | |
194 | |
195 | |
507 | 196 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ |
197 int tex_xpos, tex_ypos; | |
198 | |
199 // span の始点に対応する Texture の座標 (tex1, tey1) | |
200 float tex = span->tex_x1; | |
201 float tey = span->tex_y1; | |
202 | |
203 // span の始点に対応する z 座標 | |
204 float zpos = span->start_z; | |
205 | |
206 /* Tile 内での座標 */ | |
207 int localx = getLocalX(span->x-1); | |
208 int localy = getLocalY(span->y-1); | |
209 | |
210 /** | |
211 * (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
212 * そのブロックのアドレス(MainMemory) | |
213 */ | |
214 int tex_localx; | |
215 int tex_localy; | |
216 memaddr tex_addr; | |
217 | |
218 if (span->x < startx || endx < span->x) { | |
219 return -1; | |
220 } | |
221 | |
222 tex_xpos = (int)((span->tex_width-1) * tex); | |
223 tex_ypos = (int)((span->tex_height-1) * tey); | |
224 | |
225 if (zpos < g->zRow[localx + (rangex*localy)]) { | |
226 tex_addr = getTile(tex_xpos, tex_ypos, | |
227 span->tex_width, (memaddr)span->tex_addr); | |
228 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
229 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
230 | |
594 | 231 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); |
232 smanager->wait_segment(tile); | |
507 | 233 |
234 updateBuffer(g, zpos, rangex, localx, localy, | |
594 | 235 tex_localx, tex_localy, |
236 normal_x,normal_y,normal_z,tile); | |
507 | 237 } |
238 | |
239 return -1; | |
240 } | |
241 | |
594 | 242 #if 0 |
243 static void | |
244 drawDot2(SchedTask *smanager, SpanPtr span, int startx, int end, int js, int wait_tag) | |
245 { | |
246 //printf("%d\n", js); | |
247 } | |
248 #endif | |
507 | 249 |
250 /** | |
251 * 長さが 1 より大きい Span の描画 | |
252 * | |
253 * 本来の目的として、この関数(drawLine1) では | |
254 * : 既に SPE 上に Tile のある pixel だけ描画 | |
255 * : それ以外は、ここで予め DMA load しておき、 | |
256 * : drawLine2 で一気に描画する | |
257 * ってものだったんだけど、どうも上手く行かなかったので | |
258 * 今は drawLine1 で load -> wait -> rendering を全部やってます | |
259 * (rendering といっても、rendering buffer に書き込むだけで | |
260 * まだ main memory (frame buffer) に dma store してるわけではない) | |
261 * | |
262 * @param span Span | |
263 * @param startx 描画開始範囲 | |
264 * @param endx 描画終了範囲 | |
265 * @return 「span のどの位置まで rendering が終わったか」の x 座標 | |
266 */ | |
267 static int | |
268 drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
269 { | |
270 int x = span->x; | |
271 int rangex = endx - startx + 1; | |
272 int x_len = span->length_x; | |
273 | |
594 | 274 float normal_x = span->normal_x; |
275 float normal_y = span->normal_y; | |
276 float normal_z = span->normal_z; | |
277 | |
278 | |
507 | 279 int js = (x < startx) ? startx - x : 0; |
280 int je = (x + x_len > endx) ? endx - x : x_len; | |
281 | |
282 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ | |
283 int tex_xpos, tex_ypos; | |
284 | |
285 // span の始点に対応する座標 (tex1, tey1) | |
286 float tex1 = span->tex_x1; | |
287 float tey1 = span->tex_y1; | |
288 | |
289 // span の終点に対応する座標 (tex2, tey2) | |
290 float tex2 = span->tex_x2; | |
291 float tey2 = span->tex_y2; | |
292 | |
293 // span の始点、終点に対応する z 座標 | |
294 float zpos1 = span->start_z; | |
295 float zpos2 = span->end_z; | |
296 | |
297 // Tile 内での座標 | |
298 int localx, localy = getLocalY(span->y-1); | |
299 | |
300 int ret = je+1; | |
301 | |
302 //for (int j = js; j <= je; j++) { | |
303 for (int j = je; j >= js; j--) { | |
304 float tex_x, tex_y, tex_z; | |
305 | |
306 localx = getLocalX(x-1+j); | |
307 | |
308 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1); | |
309 | |
310 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1); | |
311 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1); | |
312 if (tex_x > 1) tex_x = 1; | |
313 if (tex_x < 0) tex_x = 0; | |
314 if (tex_y > 1) tex_y = 1; | |
315 if (tex_y < 0) tex_y = 0; | |
316 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
317 tex_ypos = (int)((span->tex_height-1) * tex_y); | |
318 | |
319 if (tex_z < g->zRow[localx + (rangex*localy)]) { | |
320 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
321 // そのブロックのアドレス(MainMemory) | |
322 memaddr tex_addr; | |
323 int tex_localx; | |
324 int tex_localy; | |
325 | |
326 tex_addr = getTile(tex_xpos, tex_ypos, | |
327 span->tex_width, (memaddr)span->tex_addr); | |
328 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
329 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
330 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); | |
331 smanager->wait_segment(tile); | |
332 | |
333 updateBuffer(g, tex_z, rangex, localx, localy, | |
594 | 334 tex_localx, tex_localy, |
335 normal_x, normal_y, normal_z, tile); | |
507 | 336 } |
337 } | |
338 | |
339 return ret; | |
340 } | |
341 | |
594 | 342 float |
343 innerProduct(float *v0, float *v1) | |
344 { | |
345 return (v0[0]*v1[0] + v0[1]*v1[1] + v0[2]*v1[2]); | |
346 } | |
347 | |
348 static int | |
349 infinity_light_calc(int color,float normal_x, float normal_y, float normal_z) | |
507 | 350 { |
594 | 351 unsigned char rgb[4]; |
352 int light_rgb; | |
353 int flag; | |
354 float normal_vector[4] = {normal_x,normal_y,normal_z,0}; | |
355 // 光のベクトル,きめうちしちゃった。どうにかする | |
356 float light_vector[4] = {0,0,-1,0}; | |
357 float inner_product; | |
358 | |
359 // 引数で受け取った color の rgb 情報の抜き出し | |
360 rgb[3] = (color & 0xff000000) >> 24; | |
361 rgb[2] = (color & 0x00ff0000) >> 16; | |
362 rgb[1] = (color & 0x0000ff00) >> 8; | |
363 rgb[0] = (color & 0x000000ff); | |
364 | |
365 // 法線ベクトルと光源ベクトルとの内積をとる | |
366 inner_product = innerProduct(normal_vector,light_vector); | |
367 // 内積がマイナスの場合は色がない。 | |
368 flag = (inner_product > 0); | |
369 | |
370 // 内積を rgb にかけていく | |
371 rgb[0] = (unsigned char)(rgb[0]*inner_product*flag); | |
372 rgb[1] = (unsigned char)(rgb[1]*inner_product*flag); | |
373 rgb[2] = (unsigned char)(rgb[2]*inner_product*flag); | |
374 | |
375 //計算した rgb を light_rgb にまとめる。 | |
376 light_rgb = (rgb[3] << 24) + (rgb[2] << 16) + (rgb[1] << 8) + (rgb[0]); | |
377 | |
378 return light_rgb; | |
507 | 379 } |
594 | 380 |
507 | 381 |
382 static int | |
383 run(SchedTask *smanager, void *rbuf, void *wbuf) | |
384 { | |
385 Gptr g = (Gptr)smanager->allocate(sizeof(G)); | |
386 | |
387 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0); | |
388 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); | |
389 SpanPackPtr free_spack = next_spack; // next_spack の free() 用 | |
390 Span *span; | |
391 | |
392 Span nop_span; | |
393 nop_span.length_x = 1; | |
394 | |
594 | 395 int (*drawFunc1[2])(SchedTask *, Gptr, SpanPtr, int, int, int) = { |
396 &drawDot1, &drawLine1 | |
397 }; | |
507 | 398 |
399 uint32 display = smanager->get_param(0); | |
400 int screen_width = smanager->get_param(1); | |
401 int rangex_start = smanager->get_param(2); | |
402 int rangex_end = smanager->get_param(3); | |
403 | |
404 // このタスクが担当する x の範囲 | |
405 int rangex = rangex_end - rangex_start + 1; | |
406 | |
407 // y の範囲 | |
408 int rangey = smanager->get_param(4); | |
409 g->tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); | |
410 | |
411 g->zRow = zRow_init(smanager, rangex, rangey); | |
412 //linebuf = linebuf_init(rangex, rangey, 0x00ffffff); | |
413 g->linebuf = linebuf_init(smanager, rangex, rangey, 0); | |
414 | |
415 g->doneWrite = 0; | |
416 | |
417 int tl_tag[2] = {TEX_LOAD1, TEX_LOAD2}; | |
418 int tl_tag_flg1 = 0; | |
419 int tl_tag_flg2 = 1; | |
420 | |
421 do { | |
422 /** | |
423 * SpanPack->next が存在する場合、 | |
424 * 現在の SpanPack を処理してる間に | |
425 * 次の SpanPack の DMA 転送を行う | |
426 */ | |
427 if (spack->next != NULL) { | |
594 | 428 smanager->dma_load(next_spack, (uint32)spack->next, |
507 | 429 sizeof(SpanPack), SPAN_PACK_LOAD); |
430 } else { | |
431 next_spack = NULL; | |
432 } | |
433 | |
434 SpanPtr resume_span = &nop_span; | |
435 int resume_span_x = 0; | |
436 | |
437 for (int t = 0; t < spack->info.size; t++) { | |
438 SpanPtr next_span; | |
439 int next_span_x; | |
440 | |
441 span = &spack->span[t]; | |
442 | |
443 /** | |
444 * span の長さによって、drawLine か drawDot を選択している | |
445 */ | |
594 | 446 next_span_x |
447 = (*drawFunc1[(span->length_x != 1)])( | |
448 smanager, g, | |
449 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); | |
507 | 450 next_span = span; |
451 | |
452 resume_span = next_span; | |
453 resume_span_x = next_span_x; | |
454 | |
455 //smanager->dma_wait(tl_tag[tl_tag_flg1]); | |
456 | |
457 tl_tag_flg1 ^= 1; | |
458 tl_tag_flg2 ^= 1; | |
459 } | |
460 | |
461 // 現在 drawLine2、drawDot2 は機能してないので | |
462 //(this->*drawFunc2[(resume_span->length_x != 1)])( | |
463 //resume_span, rangex_start, rangex_end, resume_span_x, | |
464 //tl_tag[tl_tag_flg1]); | |
465 | |
466 smanager->dma_wait(SPAN_PACK_LOAD); | |
467 | |
468 SpanPackPtr tmp_spack = spack; | |
469 spack = next_spack; | |
470 next_spack = tmp_spack; | |
471 } while (spack); | |
472 | |
473 writebuffer(smanager, g, display, rangex, rangey, screen_width); | |
474 | |
475 // linebuf は、writebuffer() の dma_store を wait する | |
476 // DrawSpan::~DrawSpan() 内で free する。 | |
477 //free(linebuf); | |
478 free(g->zRow); | |
479 | |
480 //FINISH: | |
481 /** | |
482 * goto FINISH; の時は reboot なので | |
483 * linebuf, zRow は free() しない | |
484 */ | |
485 | |
486 free(free_spack); | |
487 | |
614
4e44147d78ee
remove uncessary Task Name definision
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
599
diff
changeset
|
488 TaskPtr nextTask = smanager->create_task(DrawSpanEnd); |
507 | 489 nextTask->add_param((int)g); |
490 smanager->wait_task(nextTask); | |
491 | |
492 return 0; | |
493 } | |
594 | 494 |
495 /* end */ |