Mercurial > hg > Game > Cerium
annotate TaskManager/Test/test_render/task/DrawSpan.cpp @ 356:5b995810b86a draft
merge heads
author | aaa |
---|---|
date | Fri, 17 Jul 2009 15:04:02 +0900 |
parents | cfd20d609ace 9738837c225c |
children | 6602b9ba4bfd |
rev | line source |
---|---|
109 | 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" | |
184 | 7 #include "Func.h" |
315 | 8 #include "sys.h" |
352 | 9 #include "global_alloc.h" |
109 | 10 |
210 | 11 SchedDefineTask(DrawSpan); |
109 | 12 |
210 | 13 #define TEX_LOAD1 0 |
14 #define TEX_LOAD2 1 | |
15 #define SPAN_PACK_LOAD 2 | |
16 #define FB_STORE 3 | |
109 | 17 |
193 | 18 DrawSpan::~DrawSpan(void) |
19 { | |
20 smanager->dma_wait(FB_STORE); | |
21 free((void*)((int)linebuf*doneWrite)); | |
22 } | |
23 | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
24 /** |
193 | 25 * テクスチャは、TEXTURE_SPLIT_PIXEL^2 のブロックに分割する |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
26 * |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
27 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
28 * | 0 | 1 | 2 | 3 | 4 | 5 | |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
29 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
30 * | | | | | |11 | |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
31 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
32 * | | | | | |17 | |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
33 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
34 * | | | | | |23 | |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
35 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
36 * | | | | | |29 | |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
37 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
38 * | | | | | |35 | |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
39 * +---+---+---+---+---+---+ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
40 * |
193 | 41 * 一辺を TEXTURE_SPLIT とする |
42 * 各ブロックの数字がブロックIDとなる。 | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
43 */ |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
44 |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
45 /** |
193 | 46 * テクスチャの座標から、 |
47 * テクスチャのどのブロックかを求める | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
48 * |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
49 * @param[in] tx X coordinates of texture |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
50 * @param[in] tx Y coordinates of texture |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
51 * @param[in] twidth Width of texture |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
52 * @return block ID |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
53 */ |
184 | 54 int |
55 DrawSpan::getTexBlock(int tx, int ty, int twidth) | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
56 { |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
57 int blockX, blockY; |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
58 |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
59 blockX = tx / TEXTURE_SPLIT_PIXEL; |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
60 blockY = ty / TEXTURE_SPLIT_PIXEL; |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
61 |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
62 return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY; |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
63 } |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
64 |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
65 /** |
193 | 66 * block ID と、テクスチャの TOP address から |
67 * (tx,ty) で使われるテクスチャの Tile addres を求める | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
68 * |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
69 * @param[in] tx X coordinates of texture |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
70 * @param[in] tx Y coordinates of texture |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
71 * @param[in] tw Width of texture |
193 | 72 * @param[in] tex_addr_top (tx,ty) で使うテクスチャの先頭address |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
73 * @return block ID |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
74 */ |
184 | 75 uint32* |
76 DrawSpan::getTile(int tx, int ty, int tw, uint32 *tex_addr_top) | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
77 { |
184 | 78 int block = getTexBlock(tx, ty, tw); |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
79 return tex_addr_top + block*TEXTURE_BLOCK_SIZE; |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
80 } |
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
81 |
193 | 82 /** |
83 * FrameBuffer に書き込む rgb の領域初期化 | |
84 * | |
85 * @param width Width of Buffer | |
86 * @param height Height of Buffer | |
87 * @param rgb Initial value of RGB at Buffer | |
88 * @return Buffer | |
89 */ | |
90 int* | |
91 DrawSpan::linebuf_init(int width, int height, int rgb) | |
109 | 92 { |
193 | 93 int *buf = (int*)smanager->allocate(sizeof(int)*width*height); |
94 | |
95 for (int i = 0; i < width*height; i++) { | |
352 | 96 buf[i] = rgb; |
109 | 97 } |
193 | 98 |
99 return buf; | |
109 | 100 } |
101 | |
184 | 102 /** |
193 | 103 * Z-Buffer の初期化 |
184 | 104 * |
193 | 105 * @param width Width of Z-Buffer |
106 * @param height Height of Z-Buffer | |
107 * @return Z-Buffer | |
108 */ | |
109 float* | |
110 DrawSpan::zRow_init(int width, int height) | |
111 { | |
112 float *buf = (float*)smanager->allocate(sizeof(float)*width*height); | |
113 float def = 65535.0f; | |
114 | |
115 for (int i = 0; i < width*height; i++) { | |
352 | 116 buf[i] = def; |
193 | 117 } |
118 | |
119 return buf; | |
120 } | |
121 | |
122 | |
123 /** | |
124 * Span が使う Texture Tile があるか | |
125 * | |
126 * @retval != NULL 存在する | |
127 * @retval NULL 存在しない | |
184 | 128 */ |
129 TilePtr | |
130 DrawSpan::isAvailableTile(uint32 *addr) | |
131 { | |
132 return hash->get(addr); | |
133 } | |
134 | |
135 void | |
210 | 136 DrawSpan::set_rgb(uint32 *addr, int tag) |
184 | 137 { |
138 TilePtr tile; | |
139 | |
352 | 140 #if 0 |
210 | 141 if (isAvailableTile(addr)) { |
352 | 142 return; |
210 | 143 } |
352 | 144 #endif |
210 | 145 |
193 | 146 tile = tileList->nextTile(); |
147 /** | |
148 * FIFO なので、もし前のが残っていれば削除 | |
149 */ | |
150 hash->remove(tile->texture_addr); | |
352 | 151 |
193 | 152 tile->texture_addr = addr; |
352 | 153 |
238 | 154 hash->put(tile->texture_addr, tile); |
193 | 155 smanager->dma_load(tile->pixel, (uint32)addr, |
352 | 156 sizeof(uint32)*TEXTURE_BLOCK_SIZE, tag); |
184 | 157 } |
158 | |
352 | 159 #if 0 |
210 | 160 /** |
352 | 161 * |
210 | 162 */ |
184 | 163 void |
210 | 164 DrawSpan::set_rgbs(uint32 *cur_addr, uint32 *max_addr, int wait_tag) |
184 | 165 { |
210 | 166 uint32 curp = (uint32)cur_addr; |
167 uint32 maxp = (uint32)max_addr; | |
168 uint32 startp = curp; | |
169 uint32 diff = sizeof(int)*TEXTURE_BLOCK_SIZE; | |
170 int length = (int)maxp-(int)curp; | |
171 | |
172 int cmp = (length < 0); | |
193 | 173 |
210 | 174 #if 1 |
175 length += cmp*(-1)*length*2; | |
176 startp = cmp*maxp + !cmp*curp; | |
177 #else | |
178 if (length < 0) { | |
352 | 179 length = -length; |
180 startp = maxp; | |
210 | 181 } |
182 #endif | |
183 | |
184 for (int i = 0; i <= length; i += diff) { | |
352 | 185 set_rgb((uint32*)(startp + i), wait_tag); |
184 | 186 } |
187 } | |
352 | 188 #endif |
184 | 189 |
193 | 190 uint32 |
166 | 191 DrawSpan::get_rgb(int tx, int ty, uint32 *addr) |
109 | 192 { |
151 | 193 TilePtr tile; |
215
59f4129a9562
fix SceneGraph Constructor, Destructor
gongo@gendarme.local
parents:
211
diff
changeset
|
194 |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
166
diff
changeset
|
195 tile = hash->get(addr); |
168 | 196 return tile->pixel[(TEXTURE_SPLIT_PIXEL)*ty+tx]; |
109 | 197 } |
198 | |
184 | 199 /** |
193 | 200 * DrawSpan の再起動 (DrawSpanRenew 生成) |
184 | 201 * |
193 | 202 * @param[in] spack 現在処理している SpanPack |
203 * @param[in] cur_span_x span->length_x != 1 の時の Span の処理で | |
204 * どこまで進んでいるか | |
184 | 205 */ |
206 void | |
207 DrawSpan::reboot(SpanPackPtr spack, int cur_span_x) | |
208 { | |
193 | 209 DrawSpanArgPtr args = |
352 | 210 (DrawSpanArgPtr)smanager->allocate(sizeof(DrawSpanArg)); |
184 | 211 TaskPtr renew_task = smanager->create_task(TASK_DRAW_SPAN2); |
193 | 212 |
213 // 数が多いので構造体で渡す | |
214 args->display = smanager->get_param(0); | |
215 args->screen_width = smanager->get_param(1); | |
216 args->rangex_start = smanager->get_param(2); | |
217 args->rangex_end = smanager->get_param(3); | |
218 args->rangey = smanager->get_param(4); | |
219 renew_task->add_param((int)args); | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
220 |
184 | 221 /** |
193 | 222 * SpanPack は続きから開始するので、 |
223 * 現在の状態をコピーしておく。 | |
224 * spack は rbuf から取得してる可能性があり | |
225 * rbuf はシステムが自動的に free() するため | |
226 * アドレスだけ渡すのはNG | |
352 | 227 */ |
184 | 228 SpanPackPtr curr = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); |
229 memcpy(curr, spack, sizeof(SpanPack)); | |
230 renew_task->add_param((int)curr); | |
231 renew_task->add_param(cur_span_x); | |
232 | |
193 | 233 // linebuf と zRow も引き継がせる |
234 renew_task->add_param((int)linebuf); | |
235 renew_task->add_param((int)zRow); | |
236 | |
184 | 237 /** |
193 | 238 * 再起動したタスクを待つ |
352 | 239 */ |
184 | 240 smanager->wait_task(renew_task); |
241 | |
193 | 242 // next_spack は free() するので wait する |
184 | 243 smanager->dma_wait(SPAN_PACK_LOAD); |
244 } | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
245 |
193 | 246 void |
247 DrawSpan::writebuffer(unsigned int display, int buf_width, int height, | |
352 | 248 int screen_width) |
193 | 249 { |
250 for (int i = 0; i < height; i++) { | |
352 | 251 smanager->dma_store(&linebuf[i*buf_width], |
252 display + (sizeof(int)*screen_width*i), | |
253 sizeof(int)*buf_width, FB_STORE); | |
193 | 254 } |
255 | |
256 doneWrite = 1; | |
257 } | |
258 | |
210 | 259 /** |
260 * zRow と Linebuf を更新する | |
261 * | |
262 * @param zpos 更新する pixel のZ座標 | |
263 * @param rangex このタスクが処理する描画領域の x の長さ | |
264 * @param x pixel の、描画領域内での x 座標 | |
265 * @param y 〃 の、y 座標 | |
266 * @param tex_x pixel が使用するテクスチャの、Tile (8x8) 内での x 座標 | |
267 * @param tex_y 〃 の y 座標 | |
268 * @param tex_addr テクスチャのアドレス(MainMemory) | |
269 */ | |
270 void | |
352 | 271 DrawSpan::updateBuffer(float zpos, int rangex, int x, int y, int tex_x, int tex_y, |
315 | 272 float normal_x, float normal_y, float normal_z, uint32 *tex_addr) |
210 | 273 { |
262 | 274 |
315 | 275 int color = get_rgb(tex_x, tex_y, tex_addr); |
262 | 276 /*下位4bitを抽出*/ |
315 | 277 int alpha = color & 0x000F; |
262 | 278 /*完全に透けているか判断*/ |
279 int flag = (alpha != 0); | |
280 | |
315 | 281 color = infinity_light_calc(color,normal_x,normal_y,normal_z); |
282 | |
262 | 283 zRow[x + (rangex*y)] = zpos*flag + zRow[x + (rangex*y)]*(1-flag); |
315 | 284 linebuf[x + (rangex*y)] = color*flag + linebuf[x + (rangex*y)]*(1-flag); |
262 | 285 |
210 | 286 } |
287 | |
288 /** | |
289 * 長さが 1 の Span の描画 (要するに 1 pixel) | |
290 * | |
291 * @param span Span | |
292 * @param startx 描画開始範囲 | |
293 * @param endx 描画終了範囲 | |
294 */ | |
295 int | |
296 DrawSpan::drawDot1(SpanPtr span, int startx, int endx, int wait_tag) | |
297 { | |
298 int rangex = endx - startx + 1; | |
299 | |
315 | 300 float normal_x = span->normal_x; |
301 float normal_y = span->normal_y; | |
302 float normal_z = span->normal_z; | |
303 | |
304 | |
210 | 305 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ |
306 int tex_xpos, tex_ypos; | |
307 | |
308 // span の始点に対応する Texture の座標 (tex1, tey1) | |
309 float tex = span->tex_x1; | |
310 float tey = span->tex_y1; | |
311 | |
312 // span の始点に対応する z 座標 | |
313 float zpos = span->start_z; | |
314 | |
315 /* Tile 内での座標 */ | |
316 int localx = getLocalX(span->x-1); | |
317 int localy = getLocalY(span->y-1); | |
318 | |
319 /** | |
320 * (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
321 * そのブロックのアドレス(MainMemory) | |
322 */ | |
323 int tex_localx; | |
324 int tex_localy; | |
325 uint32 *tex_addr; | |
326 | |
327 if (span->x < startx || endx < span->x) { | |
352 | 328 return -1; |
210 | 329 } |
330 | |
331 tex_xpos = (int)((span->tex_width-1) * tex); | |
332 tex_ypos = (int)((span->tex_height-1) * tey); | |
333 | |
233 | 334 if (zpos < zRow[localx + (rangex*localy)]) { |
352 | 335 tex_addr = getTile(tex_xpos, tex_ypos, |
336 span->tex_width, span->tex_addr); | |
337 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
338 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
210 | 339 |
352 | 340 if (!isAvailableTile(tex_addr)) { |
341 set_rgb(tex_addr, wait_tag); | |
342 smanager->dma_wait(wait_tag); | |
343 //return startx; | |
344 } | |
210 | 345 |
352 | 346 updateBuffer(zpos, rangex, localx, localy, |
347 tex_localx, tex_localy, | |
315 | 348 normal_x,normal_y,normal_z,tex_addr); |
210 | 349 } |
350 | |
351 return -1; | |
352 } | |
353 | |
354 void | |
355 DrawSpan::drawDot2(SpanPtr span, int startx, int end, int js, int wait_tag) | |
356 { | |
357 //printf("%d\n", js); | |
358 } | |
359 | |
233 | 360 /** |
361 * 長さが 1 より大きい Span の描画 | |
362 * | |
363 * 本来の目的として、この関数(drawLine1) では | |
364 * : 既に SPE 上に Tile のある pixel だけ描画 | |
365 * : それ以外は、ここで予め DMA load しておき、 | |
366 * : drawLine2 で一気に描画する | |
367 * ってものだったんだけど、どうも上手く行かなかったので | |
368 * 今は drawLine1 で load -> wait -> rendering を全部やってます | |
369 * (rendering といっても、rendering buffer に書き込むだけで | |
370 * まだ main memory (frame buffer) に dma store してるわけではない) | |
352 | 371 * |
233 | 372 * @param span Span |
373 * @param startx 描画開始範囲 | |
374 * @param endx 描画終了範囲 | |
375 * @return 「span のどの位置まで rendering が終わったか」の x 座標 | |
376 */ | |
210 | 377 int |
378 DrawSpan::drawLine1(SpanPtr span, int startx, int endx, int wait_tag) | |
379 { | |
380 int x = span->x; | |
381 int rangex = endx - startx + 1; | |
382 int x_len = span->length_x; | |
383 | |
315 | 384 float normal_x = span->normal_x; |
385 float normal_y = span->normal_y; | |
386 float normal_z = span->normal_z; | |
387 | |
388 | |
210 | 389 int js = (x < startx) ? startx - x : 0; |
390 int je = (x + x_len > endx) ? endx - x : x_len; | |
391 | |
392 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ | |
393 int tex_xpos, tex_ypos; | |
394 | |
395 // span の始点に対応する座標 (tex1, tey1) | |
396 float tex1 = span->tex_x1; | |
397 float tey1 = span->tex_y1; | |
398 | |
399 // span の終点に対応する座標 (tex2, tey2) | |
400 float tex2 = span->tex_x2; | |
401 float tey2 = span->tex_y2; | |
402 | |
403 // span の始点、終点に対応する z 座標 | |
404 float zpos1 = span->start_z; | |
405 float zpos2 = span->end_z; | |
406 | |
407 // Tile 内での座標 | |
408 int localx, localy = getLocalY(span->y-1); | |
409 | |
410 int ret = je+1; | |
411 | |
412 //for (int j = js; j <= je; j++) { | |
413 for (int j = je; j >= js; j--) { | |
352 | 414 float tex_x, tex_y, tex_z; |
210 | 415 |
352 | 416 localx = getLocalX(x-1+j); |
233 | 417 |
352 | 418 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1); |
233 | 419 |
352 | 420 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1); |
421 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1); | |
422 if (tex_x > 1) tex_x = 1; | |
423 if (tex_x < 0) tex_x = 0; | |
424 if (tex_y > 1) tex_y = 1; | |
425 if (tex_y < 0) tex_y = 0; | |
426 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
427 tex_ypos = (int)((span->tex_height-1) * tex_y); | |
428 | |
429 if (tex_z < zRow[localx + (rangex*localy)]) { | |
430 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
431 // そのブロックのアドレス(MainMemory) | |
432 uint32 *tex_addr; | |
433 int tex_localx; | |
434 int tex_localy; | |
210 | 435 |
352 | 436 tex_addr = getTile(tex_xpos, tex_ypos, |
437 span->tex_width, span->tex_addr); | |
438 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
439 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
210 | 440 |
352 | 441 if (!isAvailableTile(tex_addr)) { |
211 | 442 #if 0 |
352 | 443 // span が必要とするであろう tile を予想紙 |
444 // set_rgbs で複数同時に load しようとしている図 | |
445 // まあ上手くいかなかったんだけど。。 | |
446 tex_x = tex1*(x_len-1-js)/(x_len-1) + tex2*js/(x_len-1); | |
447 if (tex_x > 1) tex_x = 1; | |
448 if (tex_x < 0) tex_x = 0; | |
449 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
210 | 450 |
352 | 451 uint32 *max_addr = getTile(tex_xpos, tex_ypos, |
452 span->tex_width, span->tex_addr); | |
210 | 453 |
352 | 454 set_rgbs(tex_addr, max_addr, wait_tag); |
455 return js; | |
210 | 456 #else |
352 | 457 set_rgb(tex_addr, wait_tag); |
458 smanager->dma_wait(wait_tag); | |
210 | 459 #endif |
352 | 460 } |
210 | 461 |
352 | 462 updateBuffer(tex_z, rangex, localx, localy, |
463 tex_localx, tex_localy, | |
315 | 464 normal_x, normal_y, normal_z,tex_addr); |
352 | 465 } |
210 | 466 } |
467 | |
468 return ret; | |
469 } | |
470 | |
233 | 471 /** |
472 * 長さが 1 より大きい Span の描画 (2段階目) | |
473 * | |
474 * 上にあるように、drawLine2 は今は動いてないです | |
475 * 正確には、js が startx ~ endx 範囲外にあり開始されないってところ | |
476 * | |
477 * @param span Span | |
478 * @param startx 描画開始範囲 | |
479 * @param endx 描画終了範囲 | |
480 * @param js 前回(drawLine1) で span のどこまで終わっているか | |
481 */ | |
210 | 482 void |
483 DrawSpan::drawLine2(SpanPtr span, int startx, int endx, int js, int wait_tag) | |
484 { | |
485 int x = span->x; | |
486 int rangex = endx - startx + 1; | |
487 int x_len = span->length_x; | |
488 | |
315 | 489 float normal_x = span->normal_x; |
490 float normal_y = span->normal_y; | |
491 float normal_z = span->normal_z; | |
492 | |
493 | |
210 | 494 //int js = startx; |
495 int je = (x + x_len > endx) ? endx - x : x_len; | |
496 | |
497 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ | |
498 int tex_xpos, tex_ypos; | |
499 | |
500 // span の始点に対応する座標 (tex1, tey1) | |
501 float tex1 = span->tex_x1; | |
502 float tey1 = span->tex_y1; | |
503 | |
504 // span の終点に対応する座標 (tex2, tey2) | |
505 float tex2 = span->tex_x2; | |
506 float tey2 = span->tex_y2; | |
507 | |
508 // span の始点、終点に対応する z 座標 | |
509 float zpos1 = span->start_z; | |
510 float zpos2 = span->end_z; | |
511 | |
512 // Tile 内での座標 | |
513 int localx, localy = getLocalY(span->y-1); | |
514 | |
515 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
516 // そのブロックのアドレス(MainMemory) | |
517 int tex_localx; | |
518 int tex_localy; | |
519 uint32 *tex_addr; | |
520 | |
521 float tex_x, tex_y, tex_z; | |
522 | |
523 smanager->dma_wait(wait_tag); | |
524 | |
525 for (int j = js; j <= je; j++) { | |
352 | 526 localx = getLocalX(x-1+j); |
210 | 527 |
352 | 528 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1); |
210 | 529 |
352 | 530 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1); |
531 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1); | |
532 if (tex_x > 1) tex_x = 1; | |
533 if (tex_x < 0) tex_x = 0; | |
534 if (tex_y > 1) tex_y = 1; | |
535 if (tex_y < 0) tex_y = 0; | |
536 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
537 tex_ypos = (int)((span->tex_height-1) * tex_y); | |
210 | 538 |
352 | 539 if (tex_z < zRow[localx + (rangex*localy)]) { |
540 tex_addr = getTile(tex_xpos, tex_ypos, | |
541 span->tex_width, span->tex_addr); | |
542 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
543 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
210 | 544 |
352 | 545 updateBuffer(tex_z, rangex, localx, localy, |
546 tex_localx, tex_localy, | |
315 | 547 normal_x, normal_y, normal_z,tex_addr); |
352 | 548 } |
549 } | |
210 | 550 } |
551 | |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
552 |
109 | 553 int |
315 | 554 DrawSpan::infinity_light_calc(int color,float normal_x, float normal_y, float normal_z) |
555 { | |
556 unsigned char rgb[4]; | |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
557 int light_rgb; |
315 | 558 int flag; |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
559 float normal_vector[4] = {normal_x,normal_y,normal_z,0}; |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
560 // 光のベクトル,きめうちしちゃった。どうにかする |
338
d2caa52c1828
add normal vector rotate
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
325
diff
changeset
|
561 float light_vector[4] = {0,0,-1,0}; |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
562 float inner_product; |
315 | 563 |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
564 // 引数で受け取った color の rgb 情報の抜き出し |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
565 rgb[0] = (color & 0xff000000) >> 24; |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
566 rgb[1] = (color & 0x00ff0000) >> 16; |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
567 rgb[2] = (color & 0x0000ff00) >> 8; |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
568 rgb[3] = (color & 0x000000ff); |
315 | 569 |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
570 // 法線ベクトルと光源ベクトルとの内積をとる |
315 | 571 inner_product = innerProduct(normal_vector,light_vector); |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
572 // 内積がマイナスの場合は色がない。 |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
573 flag = (inner_product > 0); |
315 | 574 |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
575 // 内積を rgb にかけていく |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
576 rgb[0] = rgb[0]*inner_product*flag; |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
577 rgb[1] = rgb[1]*inner_product*flag; |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
578 rgb[2] = rgb[2]*inner_product*flag; |
352 | 579 |
325
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
580 //計算した rgb を light_rgb にまとめる。 |
67e82512b15b
add inner_product is minus case.
e065725@yutaka.st.ie.u-ryukyu.ac.jp
parents:
321
diff
changeset
|
581 light_rgb = (rgb[0] << 24) + (rgb[1] << 16) + (rgb[2] << 8) + (rgb[3]); |
315 | 582 |
583 return light_rgb; | |
584 } | |
585 | |
586 | |
587 int | |
109 | 588 DrawSpan::run(void *rbuf, void *wbuf) |
589 { | |
193 | 590 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0); |
591 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); | |
592 SpanPackPtr free_spack = next_spack; // next_spack の free() 用 | |
593 Span *span; | |
594 | |
210 | 595 Span nop_span; |
596 nop_span.length_x = 1; | |
597 | |
598 int (DrawSpan::*drawFunc1[2])(SpanPtr, int, int, int) = { | |
352 | 599 &DrawSpan::drawDot1, &DrawSpan::drawLine1 |
210 | 600 }; |
601 | |
602 void (DrawSpan::*drawFunc2[2])(SpanPtr, int, int, int, int) = { | |
352 | 603 &DrawSpan::drawDot2, &DrawSpan::drawLine2 |
210 | 604 }; |
605 | |
193 | 606 uint32 display = smanager->get_param(0); |
607 int screen_width = smanager->get_param(1); | |
608 int rangex_start = smanager->get_param(2); | |
352 | 609 int rangex_end = smanager->get_param(3); |
610 | |
193 | 611 // このタスクが担当する x の範囲 |
612 int rangex = rangex_end - rangex_start + 1; | |
613 | |
614 // y の範囲 | |
615 int rangey = smanager->get_param(4); | |
210 | 616 |
167
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
166
diff
changeset
|
617 hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH); |
508beb59e0eb
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
166
diff
changeset
|
618 tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); |
130
40978d4b608a
テクスチャの大きさを128x128以外にも使えるように。(若干バグ有り)
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
619 |
193 | 620 zRow = zRow_init(rangex, rangey); |
233 | 621 //linebuf = linebuf_init(rangex, rangey, 0x00ffffff); |
229 | 622 linebuf = linebuf_init(rangex, rangey, 0); |
184 | 623 |
193 | 624 doneWrite = 0; |
184 | 625 |
210 | 626 int tl_tag[2] = {TEX_LOAD1, TEX_LOAD2}; |
627 int tl_tag_flg1 = 0; | |
628 int tl_tag_flg2 = 1; | |
352 | 629 |
109 | 630 do { |
352 | 631 /** |
632 * SpanPack->next が存在する場合、 | |
633 * 現在の SpanPack を処理してる間に | |
634 * 次の SpanPack の DMA 転送を行う | |
635 */ | |
636 if (spack->next != NULL) { | |
637 smanager->dma_load(next_spack, (uint32)spack->next, | |
638 sizeof(SpanPack), SPAN_PACK_LOAD); | |
639 } else { | |
640 next_spack = NULL; | |
641 } | |
109 | 642 |
352 | 643 SpanPtr resume_span = &nop_span; |
644 int resume_span_x = 0; | |
210 | 645 |
352 | 646 for (int t = 0; t < spack->info.size; t++) { |
647 SpanPtr next_span; | |
648 int next_span_x; | |
210 | 649 |
352 | 650 span = &spack->span[t]; |
109 | 651 |
352 | 652 /** |
653 * span の長さによって、drawLine か drawDot を選択している | |
654 */ | |
655 next_span_x | |
656 = (this->*drawFunc1[(span->length_x != 1)])( | |
657 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); | |
658 next_span = span; | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
659 |
352 | 660 (this->*drawFunc2[(resume_span->length_x != 1)])( |
661 resume_span, rangex_start, rangex_end, resume_span_x, | |
662 tl_tag[tl_tag_flg2]); | |
663 | |
664 resume_span = next_span; | |
665 resume_span_x = next_span_x; | |
164
dc7d10ae7460
TilePtr は SPE で計算した方がいいと判断して変更。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
161
diff
changeset
|
666 |
352 | 667 //smanager->dma_wait(tl_tag[tl_tag_flg1]); |
668 | |
669 tl_tag_flg1 ^= 1; | |
670 tl_tag_flg2 ^= 1; | |
671 } | |
184 | 672 |
352 | 673 // 現在 drawLine2、drawDot2 は機能してないので |
674 //(this->*drawFunc2[(resume_span->length_x != 1)])( | |
675 //resume_span, rangex_start, rangex_end, resume_span_x, | |
676 //tl_tag[tl_tag_flg1]); | |
148
74341c8bf935
Span への Texture List の渡し。じゃっかん texture がバグってるので
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
134
diff
changeset
|
677 |
352 | 678 smanager->dma_wait(SPAN_PACK_LOAD); |
109 | 679 |
352 | 680 SpanPackPtr tmp_spack = spack; |
681 spack = next_spack; | |
682 next_spack = tmp_spack; | |
182
8e9ada0c1ed0
add get_inputAddr, get_outputAddr
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
177
diff
changeset
|
683 } while (spack); |
109 | 684 |
193 | 685 writebuffer(display, rangex, rangey, screen_width); |
686 | |
687 // linebuf は、writebuffer() の dma_store を wait する | |
688 // DrawSpan::~DrawSpan() 内で free する。 | |
689 //free(linebuf); | |
690 free(zRow); | |
184 | 691 |
210 | 692 //FINISH: |
193 | 693 /** |
694 * goto FINISH; の時は reboot なので | |
695 * linebuf, zRow は free() しない | |
696 */ | |
210 | 697 |
182
8e9ada0c1ed0
add get_inputAddr, get_outputAddr
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
177
diff
changeset
|
698 free(free_spack); |
109 | 699 return 0; |
700 } |