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);
|
|
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);
|
|
84 return tex_addr_top + block * TEXTURE_BLOCK_SIZE * sizeof(uint32);
|
|
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
|
|
120 for (int i = 0; i < width*height; i++) {
|
|
121 buf[i] = def;
|
|
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,
|
|
138 int buf_width, int height, int screen_width)
|
|
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
|
|
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)
|
|
163 {
|
|
164
|
|
165 int color = get_rgb(tex_x, tex_y, tile);
|
|
166 /*下位4bitを抽出*/
|
|
167 int alpha = color & 0x000F;
|
|
168 /*完全に透けているか判断*/
|
|
169 int flag = (alpha != 0);
|
|
170
|
|
171 color = infinity_light_calc(color,normal_x,normal_y,normal_z);
|
|
172
|
|
173 g->zRow[x + (rangex*y)] = zpos*flag + g->zRow[x + (rangex*y)]*(1-flag);
|
|
174 g->linebuf[x + (rangex*y)] = color*flag + g->linebuf[x + (rangex*y)]*(1-flag);
|
|
175
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * 長さが 1 の Span の描画 (要するに 1 pixel)
|
|
180 *
|
|
181 * @param span Span
|
|
182 * @param startx 描画開始範囲
|
|
183 * @param endx 描画終了範囲
|
|
184 */
|
|
185 static int
|
|
186 drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag)
|
|
187 {
|
|
188 int rangex = endx - startx + 1;
|
|
189
|
|
190 float normal_x = span->normal_x;
|
|
191 float normal_y = span->normal_y;
|
|
192 float normal_z = span->normal_z;
|
|
193
|
|
194
|
|
195 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */
|
|
196 int tex_xpos, tex_ypos;
|
|
197
|
|
198 // span の始点に対応する Texture の座標 (tex1, tey1)
|
|
199 float tex = span->tex_x1;
|
|
200 float tey = span->tex_y1;
|
|
201
|
|
202 // span の始点に対応する z 座標
|
|
203 float zpos = span->start_z;
|
|
204
|
|
205 /* Tile 内での座標 */
|
|
206 int localx = getLocalX(span->x-1);
|
|
207 int localy = getLocalY(span->y-1);
|
|
208
|
|
209 /**
|
|
210 * (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と
|
|
211 * そのブロックのアドレス(MainMemory)
|
|
212 */
|
|
213 int tex_localx;
|
|
214 int tex_localy;
|
|
215 memaddr tex_addr;
|
|
216
|
|
217 if (span->x < startx || endx < span->x) {
|
|
218 return -1;
|
|
219 }
|
|
220
|
|
221 tex_xpos = (int)((span->tex_width-1) * tex);
|
|
222 tex_ypos = (int)((span->tex_height-1) * tey);
|
|
223
|
|
224 if (zpos < g->zRow[localx + (rangex*localy)]) {
|
|
225 tex_addr = getTile(tex_xpos, tex_ypos,
|
|
226 span->tex_width, (memaddr)span->tex_addr);
|
|
227 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL;
|
|
228 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL;
|
|
229
|
|
230 TilePtr tile = smanager->get_segment(tex_addr,g->tileList);
|
|
231 smanager->wait_segment(tile);
|
|
232
|
|
233 updateBuffer(g, zpos, rangex, localx, localy,
|
|
234 tex_localx, tex_localy,
|
|
235 normal_x,normal_y,normal_z,tile);
|
|
236 }
|
|
237
|
|
238 return -1;
|
|
239 }
|
|
240
|
|
241 #if 0
|
|
242 static void
|
|
243 drawDot2(SchedTask *smanager, SpanPtr span, int startx, int end, int js, int wait_tag)
|
|
244 {
|
|
245 //printf("%d\n", js);
|
|
246 }
|
|
247 #endif
|
|
248
|
|
249 /**
|
|
250 * 長さが 1 より大きい Span の描画
|
|
251 *
|
|
252 * 本来の目的として、この関数(drawLine1) では
|
|
253 * : 既に SPE 上に Tile のある pixel だけ描画
|
|
254 * : それ以外は、ここで予め DMA load しておき、
|
|
255 * : drawLine2 で一気に描画する
|
|
256 * ってものだったんだけど、どうも上手く行かなかったので
|
|
257 * 今は drawLine1 で load -> wait -> rendering を全部やってます
|
|
258 * (rendering といっても、rendering buffer に書き込むだけで
|
|
259 * まだ main memory (frame buffer) に dma store してるわけではない)
|
|
260 *
|
|
261 * @param span Span
|
|
262 * @param startx 描画開始範囲
|
|
263 * @param endx 描画終了範囲
|
|
264 * @return 「span のどの位置まで rendering が終わったか」の x 座標
|
|
265 */
|
|
266 static int
|
|
267 drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag)
|
|
268 {
|
|
269 int x = span->x;
|
|
270 int rangex = endx - startx + 1;
|
|
271 int x_len = span->length_x;
|
|
272
|
|
273 float normal_x = span->normal_x;
|
|
274 float normal_y = span->normal_y;
|
|
275 float normal_z = span->normal_z;
|
|
276
|
|
277
|
|
278 int js = (x < startx) ? startx - x : 0;
|
|
279 int je = (x + x_len > endx) ? endx - x : x_len;
|
|
280
|
|
281 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */
|
|
282 int tex_xpos, tex_ypos;
|
|
283
|
|
284 // span の始点に対応する座標 (tex1, tey1)
|
|
285 float tex1 = span->tex_x1;
|
|
286 float tey1 = span->tex_y1;
|
|
287
|
|
288 // span の終点に対応する座標 (tex2, tey2)
|
|
289 float tex2 = span->tex_x2;
|
|
290 float tey2 = span->tex_y2;
|
|
291
|
|
292 // span の始点、終点に対応する z 座標
|
|
293 float zpos1 = span->start_z;
|
|
294 float zpos2 = span->end_z;
|
|
295
|
|
296 // Tile 内での座標
|
|
297 int localx, localy = getLocalY(span->y-1);
|
|
298
|
|
299 int ret = je+1;
|
|
300
|
|
301 //for (int j = js; j <= je; j++) {
|
|
302 for (int j = je; j >= js; j--) {
|
|
303 float tex_x, tex_y, tex_z;
|
|
304
|
|
305 localx = getLocalX(x-1+j);
|
|
306
|
|
307 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1);
|
|
308
|
|
309 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1);
|
|
310 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1);
|
|
311 if (tex_x > 1) tex_x = 1;
|
|
312 if (tex_x < 0) tex_x = 0;
|
|
313 if (tex_y > 1) tex_y = 1;
|
|
314 if (tex_y < 0) tex_y = 0;
|
|
315 tex_xpos = (int)((span->tex_width-1) * tex_x);
|
|
316 tex_ypos = (int)((span->tex_height-1) * tex_y);
|
|
317
|
|
318 if (tex_z < g->zRow[localx + (rangex*localy)]) {
|
|
319 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と
|
|
320 // そのブロックのアドレス(MainMemory)
|
|
321 memaddr tex_addr;
|
|
322 int tex_localx;
|
|
323 int tex_localy;
|
|
324
|
|
325 tex_addr = getTile(tex_xpos, tex_ypos,
|
|
326 span->tex_width, (memaddr)span->tex_addr);
|
|
327 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL;
|
|
328 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL;
|
|
329 TilePtr tile = smanager->get_segment(tex_addr,g->tileList);
|
|
330 smanager->wait_segment(tile);
|
|
331
|
|
332 updateBuffer(g, tex_z, rangex, localx, localy,
|
|
333 tex_localx, tex_localy,
|
|
334 normal_x, normal_y, normal_z, tile);
|
|
335 }
|
|
336 }
|
|
337
|
|
338 return ret;
|
|
339 }
|
|
340
|
|
341 static int
|
|
342 infinity_light_calc(int color,float normal_x, float normal_y, float normal_z)
|
|
343 {
|
|
344 unsigned char rgb[4];
|
|
345 int light_rgb;
|
|
346 int flag;
|
|
347 float normal_vector[4] = {normal_x,normal_y,normal_z,0};
|
|
348 // 光のベクトル,きめうちしちゃった。どうにかする
|
|
349 float light_vector[4] = {0,0,-1,0};
|
|
350 float inner_product;
|
|
351
|
|
352 // 引数で受け取った color の rgb 情報の抜き出し
|
|
353 rgb[0] = (color & 0xff000000) >> 24;
|
|
354 rgb[1] = (color & 0x00ff0000) >> 16;
|
|
355 rgb[2] = (color & 0x0000ff00) >> 8;
|
|
356 rgb[3] = (color & 0x000000ff);
|
|
357
|
|
358 // 法線ベクトルと光源ベクトルとの内積をとる
|
|
359 inner_product = innerProduct(normal_vector,light_vector);
|
|
360 // 内積がマイナスの場合は色がない。
|
|
361 flag = (inner_product > 0);
|
|
362
|
|
363 // 内積を rgb にかけていく
|
|
364 rgb[0] = (unsigned char)(rgb[0]*inner_product*flag);
|
|
365 rgb[1] = (unsigned char)(rgb[1]*inner_product*flag);
|
|
366 rgb[2] = (unsigned char)(rgb[2]*inner_product*flag);
|
|
367
|
|
368 //計算した rgb を light_rgb にまとめる。
|
|
369 light_rgb = (rgb[0] << 24) + (rgb[1] << 16) + (rgb[2] << 8) + (rgb[3]);
|
|
370
|
|
371 return light_rgb;
|
|
372 }
|
|
373
|
|
374
|
|
375 static int
|
|
376 run(SchedTask *smanager, void *rbuf, void *wbuf)
|
|
377 {
|
|
378 Gptr g = (Gptr)smanager->allocate(sizeof(G));
|
|
379
|
|
380 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0);
|
|
381 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack));
|
|
382 SpanPackPtr free_spack = next_spack; // next_spack の free() 用
|
|
383 Span *span;
|
|
384
|
|
385 Span nop_span;
|
|
386 nop_span.length_x = 1;
|
|
387
|
|
388 int (*drawFunc1[2])(SchedTask *, Gptr, SpanPtr, int, int, int) = {
|
|
389 &drawDot1, &drawLine1
|
|
390 };
|
|
391
|
|
392 uint32 display = smanager->get_param(0);
|
|
393 int screen_width = smanager->get_param(1);
|
|
394 int rangex_start = smanager->get_param(2);
|
|
395 int rangex_end = smanager->get_param(3);
|
|
396
|
|
397 // このタスクが担当する x の範囲
|
|
398 int rangex = rangex_end - rangex_start + 1;
|
|
399
|
|
400 // y の範囲
|
|
401 int rangey = smanager->get_param(4);
|
|
402 g->tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST);
|
|
403
|
|
404 g->zRow = zRow_init(smanager, rangex, rangey);
|
|
405 //linebuf = linebuf_init(rangex, rangey, 0x00ffffff);
|
|
406 g->linebuf = linebuf_init(smanager, rangex, rangey, 0);
|
|
407
|
|
408 g->doneWrite = 0;
|
|
409
|
|
410 int tl_tag[2] = {TEX_LOAD1, TEX_LOAD2};
|
|
411 int tl_tag_flg1 = 0;
|
|
412 int tl_tag_flg2 = 1;
|
|
413
|
|
414 do {
|
|
415 /**
|
|
416 * SpanPack->next が存在する場合、
|
|
417 * 現在の SpanPack を処理してる間に
|
|
418 * 次の SpanPack の DMA 転送を行う
|
|
419 */
|
|
420 if (spack->next != NULL) {
|
|
421 smanager->dma_load(next_spack, (uint32)spack->next,
|
|
422 sizeof(SpanPack), SPAN_PACK_LOAD);
|
|
423 } else {
|
|
424 next_spack = NULL;
|
|
425 }
|
|
426
|
|
427 SpanPtr resume_span = &nop_span;
|
|
428 int resume_span_x = 0;
|
|
429
|
|
430 for (int t = 0; t < spack->info.size; t++) {
|
|
431 SpanPtr next_span;
|
|
432 int next_span_x;
|
|
433
|
|
434 span = &spack->span[t];
|
|
435
|
|
436 /**
|
|
437 * span の長さによって、drawLine か drawDot を選択している
|
|
438 */
|
|
439 next_span_x
|
|
440 = (*drawFunc1[(span->length_x != 1)])(
|
|
441 smanager, g,
|
|
442 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]);
|
|
443 next_span = span;
|
|
444
|
|
445 resume_span = next_span;
|
|
446 resume_span_x = next_span_x;
|
|
447
|
|
448 //smanager->dma_wait(tl_tag[tl_tag_flg1]);
|
|
449
|
|
450 tl_tag_flg1 ^= 1;
|
|
451 tl_tag_flg2 ^= 1;
|
|
452 }
|
|
453
|
|
454 // 現在 drawLine2、drawDot2 は機能してないので
|
|
455 //(this->*drawFunc2[(resume_span->length_x != 1)])(
|
|
456 //resume_span, rangex_start, rangex_end, resume_span_x,
|
|
457 //tl_tag[tl_tag_flg1]);
|
|
458
|
|
459 smanager->dma_wait(SPAN_PACK_LOAD);
|
|
460
|
|
461 SpanPackPtr tmp_spack = spack;
|
|
462 spack = next_spack;
|
|
463 next_spack = tmp_spack;
|
|
464 } while (spack);
|
|
465
|
|
466 writebuffer(smanager, g, display, rangex, rangey, screen_width);
|
|
467
|
|
468 // linebuf は、writebuffer() の dma_store を wait する
|
|
469 // DrawSpan::~DrawSpan() 内で free する。
|
|
470 //free(linebuf);
|
|
471 free(g->zRow);
|
|
472
|
|
473 //FINISH:
|
|
474 /**
|
|
475 * goto FINISH; の時は reboot なので
|
|
476 * linebuf, zRow は free() しない
|
|
477 */
|
|
478
|
|
479 free(free_spack);
|
|
480
|
|
481 TaskPtr nextTask = smanager->create_task(TASK_DRAW_SPAN_END);
|
|
482 nextTask->add_param((int)g);
|
|
483 smanager->wait_task(nextTask);
|
|
484
|
|
485 return 0;
|
|
486 }
|
|
487
|
|
488 /* end */
|