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