comparison TaskManager/Test/test_render/spe/DrawSpan.cpp @ 193:1ff24ca2f016 draft

fix
author gongo@localhost.localdomain
date Tue, 13 Jan 2009 19:06:13 +0900
parents 6694da357750
children b56fb6ac2fc4
comparison
equal deleted inserted replaced
192:6694da357750 193:1ff24ca2f016
7 #include "viewer_types.h" 7 #include "viewer_types.h"
8 #include "Func.h" 8 #include "Func.h"
9 9
10 #define SPAN_PACK_LOAD 0 10 #define SPAN_PACK_LOAD 0
11 #define TEX_LOAD 1 11 #define TEX_LOAD 1
12 #define FB_STORE 2
12 13
13 SchedDefineTask(DrawSpan); 14 SchedDefineTask(DrawSpan);
14 15
15 /** 16 DrawSpan::~DrawSpan(void)
16 * テクスチャは、TEXTURE_SPLIT_PIXEL^2 のブロックに分割する 17 {
18 smanager->dma_wait(FB_STORE);
19 free((void*)((int)linebuf*doneWrite));
20 }
21
22 /**
23 * 鴻cTEXTURE_SPLIT_PIXEL^2 蚊
17 * 24 *
18 * +---+---+---+---+---+---+ 25 * +---+---+---+---+---+---+
19 * | 0 | 1 | 2 | 3 | 4 | 5 | 26 * | 0 | 1 | 2 | 3 | 4 | 5 |
20 * +---+---+---+---+---+---+ 27 * +---+---+---+---+---+---+
21 * | | | | | |11 | 28 * | | | | | |11 |
27 * | | | | | |29 | 34 * | | | | | |29 |
28 * +---+---+---+---+---+---+ 35 * +---+---+---+---+---+---+
29 * | | | | | |35 | 36 * | | | | | |35 |
30 * +---+---+---+---+---+---+ 37 * +---+---+---+---+---+---+
31 * 38 *
32 * 一辺を TEXTURE_SPLIT とする 39 * 筝莨冴 TEXTURE_SPLIT
33 * 各ブロックの数字がブロックIDとなる。 40 * 医ID
34 */ 41 */
35 42
36 /** 43 /**
37 * テクスチャの座標から、 44 * 鴻c綺ф
38 * テクスチャのどのブロックかを求める 45 * 鴻c羆
39 * 46 *
40 * @param[in] tx X coordinates of texture 47 * @param[in] tx X coordinates of texture
41 * @param[in] tx Y coordinates of texture 48 * @param[in] tx Y coordinates of texture
42 * @param[in] twidth Width of texture 49 * @param[in] twidth Width of texture
43 * @return block ID 50 * @return block ID
52 59
53 return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY; 60 return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY;
54 } 61 }
55 62
56 /** 63 /**
57 * block ID と、テクスチャの TOP address から 64 * block ID 鴻c TOP address
58 * (tx,ty) で使われるテクスチャの Tile addres を求める 65 * (tx,ty) т戎鴻c Tile addres 羆
59 * 66 *
60 * @param[in] tx X coordinates of texture 67 * @param[in] tx X coordinates of texture
61 * @param[in] tx Y coordinates of texture 68 * @param[in] tx Y coordinates of texture
62 * @param[in] tw Width of texture 69 * @param[in] tw Width of texture
63 * @param[in] tex_addr_top (tx,ty) で使うテクスチャの先頭address 70 * @param[in] tex_addr_top (tx,ty) т戎鴻caddress
64 * @return block ID 71 * @return block ID
65 */ 72 */
66 uint32* 73 uint32*
67 DrawSpan::getTile(int tx, int ty, int tw, uint32 *tex_addr_top) 74 DrawSpan::getTile(int tx, int ty, int tw, uint32 *tex_addr_top)
68 { 75 {
69 int block = getTexBlock(tx, ty, tw); 76 int block = getTexBlock(tx, ty, tw);
70 return tex_addr_top + block*TEXTURE_BLOCK_SIZE; 77 return tex_addr_top + block*TEXTURE_BLOCK_SIZE;
71 } 78 }
72 79
73 void 80 /**
74 DrawSpan::linebuf_init(int *buf, int x, int rgb) 81 * FrameBuffer 吾莨若 rgb
75 { 82 *
76 for (int i = 0; i < x; i++) { 83 * @param width Width of Buffer
84 * @param height Height of Buffer
85 * @param rgb Initial value of RGB at Buffer
86 * @return Buffer
87 */
88 int*
89 DrawSpan::linebuf_init(int width, int height, int rgb)
90 {
91 int *buf = (int*)smanager->allocate(sizeof(int)*width*height);
92
93 for (int i = 0; i < width*height; i++) {
77 buf[i] = rgb; 94 buf[i] = rgb;
78 } 95 }
79 } 96
80 97 return buf;
81 /** 98 }
82 * Span が使う Texture Tile があるか 99
83 * 100 /**
84 * @retval != NULL 存在する 101 * Z-Buffer
85 * @retval NULL 存在しない 102 *
103 * @param width Width of Z-Buffer
104 * @param height Height of Z-Buffer
105 * @return Z-Buffer
106 */
107 float*
108 DrawSpan::zRow_init(int width, int height)
109 {
110 float *buf = (float*)smanager->allocate(sizeof(float)*width*height);
111 float def = 65535.0f;
112
113 for (int i = 0; i < width*height; i++) {
114 buf[i] = def;
115 }
116
117 return buf;
118 }
119
120
121 /**
122 * Span 篏帥 Texture Tile
123 *
124 * @retval != NULL 絖
125 * @retval NULL 絖
86 */ 126 */
87 TilePtr 127 TilePtr
88 DrawSpan::isAvailableTile(uint32 *addr) 128 DrawSpan::isAvailableTile(uint32 *addr)
89 { 129 {
90 return hash->get(addr); 130 return hash->get(addr);
93 void 133 void
94 DrawSpan::set_rgb(uint32 *addr) 134 DrawSpan::set_rgb(uint32 *addr)
95 { 135 {
96 TilePtr tile; 136 TilePtr tile;
97 137
98 if (!isAvailableTile(addr)) { 138 tile = tileList->nextTile();
99 tile = tileList->nextTile(); 139 /**
100 /** 140 * FIFO с罧c医
101 * FIFO なので、もし前のが残っていれば削除 141 */
102 */ 142 hash->remove(tile->texture_addr);
103 hash->remove(tile->texture_addr); 143
104 144 tile->texture_addr = addr;
105 tile->texture_addr = addr; 145
106 146 hash->put(tile->texture_addr, tile);
107 hash->put(tile->texture_addr, tile); 147
108 148 smanager->dma_load(tile->pixel, (uint32)addr,
109 smanager->dma_load(tile->pixel, (uint32)addr, 149 sizeof(uint32)*TEXTURE_BLOCK_SIZE, TEX_LOAD);
110 sizeof(uint32)*TEXTURE_BLOCK_SIZE, TEX_LOAD);
111
112 //smanager->dma_wait(TEX_LOAD);
113 }
114 } 150 }
115 151
116 void 152 void
117 DrawSpan::set_rgbs(uint32 *addr, uint32 *max_addr) 153 DrawSpan::set_rgbs(uint32 *addr, uint32 *max_addr)
118 { 154 {
119 uint32 start = (uint32)addr; 155 uint32 start = (uint32)addr;
120 uint32 end = (uint32)max_addr; 156 uint32 end = (uint32)max_addr;
121 uint32 length = end-start; 157 int length = (int)end-start;
122 uint32 diff = sizeof(uint32)*TEXTURE_BLOCK_SIZE; 158 int diff = sizeof(int)*TEXTURE_BLOCK_SIZE;
123 uint32 max_tile = 16; 159 int max_tile = 16;
124 160
125 for (uint32 i = 0, j = 0; i <= length && j < max_tile; i += diff, j++) { 161 for (int i = 0, j = 0; i <= length && j < max_tile; i += diff, j++) {
126 set_rgb((uint32*)(start + i)); 162 set_rgb((uint32*)(start + i));
127 } 163 }
128 } 164 }
129 165
130 Uint32 166 uint32
131 DrawSpan::get_rgb(int tx, int ty, uint32 *addr) 167 DrawSpan::get_rgb(int tx, int ty, uint32 *addr)
132 { 168 {
133 TilePtr tile; 169 TilePtr tile;
134 170
135 tile = hash->get(addr); 171 tile = hash->get(addr);
136 return tile->pixel[(TEXTURE_SPLIT_PIXEL)*ty+tx]; 172 return tile->pixel[(TEXTURE_SPLIT_PIXEL)*ty+tx];
137 } 173 }
138 174
139 /** 175 /**
140 * DrawSpan の再起動 (DrawSpanRenew 生成) 176 * DrawSpan 莎桁 (DrawSpanRenew )
141 * 177 *
142 * @param[in] spack 現在処理している SpanPack 178 * @param[in] spack 憜 SpanPack
143 * @param[in] cur_span_x span->length_x != 1 の時の Span の処理で 179 * @param[in] cur_span_x span->length_x != 1 Span
144 * どこまで進んでいるか 180 * 障ч蚊с
145 */ 181 */
146 void 182 void
147 DrawSpan::reboot(SpanPackPtr spack, int cur_span_x) 183 DrawSpan::reboot(SpanPackPtr spack, int cur_span_x)
148 { 184 {
185 DrawSpanArgPtr args =
186 (DrawSpanArgPtr)smanager->allocate(sizeof(DrawSpanArg));
149 TaskPtr renew_task = smanager->create_task(TASK_DRAW_SPAN2); 187 TaskPtr renew_task = smanager->create_task(TASK_DRAW_SPAN2);
150 int rangey = smanager->get_param(2); 188
189 // 違紊ф篏ф検
190 args->display = smanager->get_param(0);
191 args->screen_width = smanager->get_param(1);
192 args->rangex_start = smanager->get_param(2);
193 args->rangex_end = smanager->get_param(3);
194 args->rangey = smanager->get_param(4);
195 renew_task->add_param((int)args);
151 196
152 /** 197 /**
153 * 共通の outData, param はそのまま渡す 198 * SpanPack 膓紮с
154 */ 199 * 憜倶潟若
155 for (int i = 0; i < rangey; i++) { 200 * spack rbuf 緇醇с
156 renew_task->add_outData(smanager->get_outputAddr(i), 201 * rbuf 激鴻 free()
157 smanager->get_outputSize(i)); 202 * ≪鴻羝<NG
158 }
159
160 // rangex_start, rangex_end, rangey
161 renew_task->add_param(smanager->get_param(0));
162 renew_task->add_param(smanager->get_param(1));
163 renew_task->add_param(smanager->get_param(2));
164
165 /**
166 * SpanPack は続きから開始するので、
167 * 現在の状態をコピーしておく。
168 * spack は rbuf から取得してる可能性があり
169 * rbuf はシステムが自動的に free() するため
170 * アドレスだけ渡すのはNG
171 */ 203 */
172 SpanPackPtr curr = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); 204 SpanPackPtr curr = (SpanPackPtr)smanager->allocate(sizeof(SpanPack));
173 memcpy(curr, spack, sizeof(SpanPack)); 205 memcpy(curr, spack, sizeof(SpanPack));
174 renew_task->add_param((int)curr); 206 renew_task->add_param((int)curr);
175 renew_task->add_param(cur_span_x); 207 renew_task->add_param(cur_span_x);
176 208
177 fprintf(stderr, "[%p] start %u\n", 209 // linebuf zRow 綣膓
178 curr, spu_readch(SPU_RdDec)); 210 renew_task->add_param((int)linebuf);
211 renew_task->add_param((int)zRow);
212
213 //fprintf(stderr, "[%p] start %u\n", curr, spu_readch(SPU_RdDec));
179 214
180 /** 215 /**
181 * 再起動したタスクを待つ 216 * 莎桁帥鴻緇
182 */ 217 */
183 smanager->wait_task(renew_task); 218 smanager->wait_task(renew_task);
184 219
185 // next_spack は free() するので wait する 220 // next_spack free() wait
186 smanager->dma_wait(SPAN_PACK_LOAD); 221 smanager->dma_wait(SPAN_PACK_LOAD);
222 }
223
224 void
225 DrawSpan::writebuffer(unsigned int display, int buf_width, int height,
226 int screen_width)
227 {
228 for (int i = 0; i < height; i++) {
229 smanager->dma_store(&linebuf[i*buf_width],
230 display + (sizeof(int)*screen_width*i),
231 sizeof(int)*buf_width, FB_STORE);
232 }
233
234 doneWrite = 1;
187 } 235 }
188 236
189 int 237 int
190 DrawSpan::run(void *rbuf, void *wbuf) 238 DrawSpan::run(void *rbuf, void *wbuf)
191 { 239 {
240 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0);
241 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack));
242 SpanPackPtr free_spack = next_spack; // next_spack free()
243 Span *span;
244
245 uint32 display = smanager->get_param(0);
246 int screen_width = smanager->get_param(1);
247 int rangex_start = smanager->get_param(2);
248 int rangex_end = smanager->get_param(3);
249
250 // 帥鴻綵 x 膀
251 int rangex = rangex_end - rangex_start + 1;
252
253 // y 膀
254 int rangey = smanager->get_param(4);
255
192 hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH); 256 hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH);
193 tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); 257 tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST);
194 258
195 int rangex_start = smanager->get_param(0); 259 zRow = zRow_init(rangex, rangey);
196 int rangex_end = smanager->get_param(1); 260 linebuf = linebuf_init(rangex, rangey, 0xffffffff);
197 261
198 // このタスクが担当する x の範囲 262 doneWrite = 0;
199 int rangex = rangex_end - rangex_start + 1;
200
201 // y の範囲 (render_y + rangey - 1)
202 int rangey = smanager->get_param(2);
203
204 float *zRow = (float*)smanager->allocate(sizeof(float)*rangex*rangey);
205
206 for (int i = 0; i < rangex*rangey; i++) {
207 zRow[i] = 65535.0f;
208 }
209
210 int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);
211
212 for (int i = 0; i < rangey; i++) {
213 linebuf[i] = (int*)smanager->get_output(i);
214 linebuf_init(linebuf[i], rangex, 0xffffffff);
215 }
216
217 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0);
218 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack));
219 SpanPackPtr free_spack = next_spack; // next_spack の free() 用
220 Span *span;
221 263
222 do { 264 do {
223 /** 265 /**
224 * SpanPack->next が存在する場合、 266 * SpanPack->next 絖翫
225 * 現在の SpanPack を処理してる間に 267 * 憜 SpanPack
226 * 次の SpanPack の DMA 転送を行う 268 * 罨< SpanPack DMA 荵∫茵
227 */ 269 */
228 if (spack->next != NULL) { 270 if (spack->next != NULL) {
229 smanager->dma_load(next_spack, (uint32)spack->next, 271 smanager->dma_load(next_spack, (uint32)spack->next,
230 sizeof(SpanPack), SPAN_PACK_LOAD); 272 sizeof(SpanPack), SPAN_PACK_LOAD);
231 } else { 273 } else {
233 } 275 }
234 276
235 for (int t = 0; t < spack->info.size; t++) { 277 for (int t = 0; t < spack->info.size; t++) {
236 span = &spack->span[t]; 278 span = &spack->span[t];
237 279
238 Uint32 rgb = 0x00ff00; 280 uint32 rgb = 0x0000ff00;
239 float tex1 = span->tex_x1; 281 float tex1 = span->tex_x1;
240 float tex2 = span->tex_x2; 282 float tex2 = span->tex_x2;
241 float tey1 = span->tex_y1; 283 float tey1 = span->tex_y1;
242 float tey2 = span->tex_y2; 284 float tey2 = span->tex_y2;
243 285
244 /** 286 /**
245 * Span が持つ 1 pixel 毎の 287 * Span 1 pixel 罸
246 * テクスチャの座標 288 * 鴻c綺ф
247 */ 289 */
248 int tex_xpos; 290 int tex_xpos;
249 int tex_ypos; 291 int tex_ypos;
250 292
251 /** 293 /**
252 * (tex_xpos, tex_ypos) の、ブロック内(上の図参照)での座標と 294 * (tex_xpos, tex_ypos) (筝喝)с綺ф
253 * そのブロックのアドレス(MainMemory) 295 * ≪(MainMemory)
254 */ 296 */
255 int tex_localx; 297 int tex_localx;
256 int tex_localy; 298 int tex_localy;
257 uint32 *tex_addr; 299 uint32 *tex_addr;
258 300
260 int y = span->y; 302 int y = span->y;
261 int x_len = span->length_x; 303 int x_len = span->length_x;
262 float z = span->start_z; 304 float z = span->start_z;
263 float zpos = span->end_z; 305 float zpos = span->end_z;
264 306
265 // 座標が [0 .. split_screen_w-1] に入るように x,y を -1 307 // 綺ф [0 .. split_screen_w-1] ャ x,y -1
266 int localx = getLocalX(x-1); 308 int localx = getLocalX(x-1);
267 int localy = getLocalY(y-1); 309 int localy = getLocalY(y-1);
268 310
269 if (x_len == 1) { 311 if (x_len == 1) {
270 if (x < rangex_start || rangex_end < x) { 312 if (x < rangex_start || rangex_end < x) {
272 } 314 }
273 315
274 tex_xpos = (int)((span->tex_width-1) * tex1); 316 tex_xpos = (int)((span->tex_width-1) * tex1);
275 tex_ypos = (int)((span->tex_height-1) * tey1); 317 tex_ypos = (int)((span->tex_height-1) * tey1);
276 318
277 if (zpos < zRow[localx + (rangex * localy)]) { 319 if (zpos < zRow[localx + (rangex*localy)]) {
278 tex_addr = getTile(tex_xpos, tex_ypos, 320 tex_addr = getTile(tex_xpos, tex_ypos,
279 span->tex_width, span->tex_addr); 321 span->tex_width, span->tex_addr);
280 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; 322 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL;
281 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; 323 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL;
282 324
283 /**
284 * Tile が無い場合、一旦タスクはここで中断し、
285 * Tile をロードするタスクを走らせた後に再起動する
286 */
287 if (!isAvailableTile(tex_addr)) { 325 if (!isAvailableTile(tex_addr)) {
288 spack->info.start = t;
289 set_rgb(tex_addr); 326 set_rgb(tex_addr);
290 //set_rgbs(tex_addr, 327 smanager->dma_wait(TEX_LOAD);
291 //getTile(span->tex_width-1, tex_ypos,
292 //span->tex_width, span->tex_addr));
293 //reboot(spack, 0);
294 //goto FINISH;
295 } 328 }
296 329
297 rgb = get_rgb(tex_localx, tex_localy, tex_addr); 330 rgb = get_rgb(tex_localx, tex_localy, tex_addr);
298 331
299 zRow[localx + (rangex * localy)] = zpos; 332 zRow[localx + (rangex*localy)] = zpos;
300 linebuf[localy][localx] = rgb; 333 linebuf[localx + (rangex*localy)] = rgb;
301 } 334 }
302 } else { 335 } else {
303 int js = (x < rangex_start) ? rangex_start - x : 0; 336 int js = (x < rangex_start) ? rangex_start - x : 0;
304 int je = (x + x_len > rangex_end) ? rangex_end - x : x_len; 337 int je = (x + x_len > rangex_end) ? rangex_end - x : x_len;
305 float tex_x, tex_y, tex_z; 338 float tex_x, tex_y, tex_z;
322 tex_addr = getTile(tex_xpos, tex_ypos, 355 tex_addr = getTile(tex_xpos, tex_ypos,
323 span->tex_width, span->tex_addr); 356 span->tex_width, span->tex_addr);
324 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; 357 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL;
325 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; 358 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL;
326 359
360 /**
361 * Tile <翫筝帥鴻т賢
362 * Tile 若帥鴻莎違緇莎桁
363 */
327 if (!isAvailableTile(tex_addr)) { 364 if (!isAvailableTile(tex_addr)) {
328 spack->info.start = t; 365 spack->info.start = t;
329 //set_rgb(tex_addr); 366 #if 1
330 set_rgbs(tex_addr, 367 set_rgbs(tex_addr,
331 getTile(span->tex_width-1, tex_ypos, 368 getTile(span->tex_width-1, tex_ypos,
332 span->tex_width, span->tex_addr)); 369 span->tex_width, span->tex_addr));
370 //smanager->dma_wait(TEX_LOAD);
333 reboot(spack, j); 371 reboot(spack, j);
334 goto FINISH; 372 goto FINISH;
373 #else
374 set_rgb(tex_addr);
375 smanager->dma_wait(TEX_LOAD);
376 #endif
335 } 377 }
336 378
337 rgb = get_rgb(tex_localx, tex_localy, tex_addr); 379 rgb = get_rgb(tex_localx, tex_localy, tex_addr);
338 380
339 zRow[localx + (rangex*localy)] = tex_z; 381 zRow[localx + (rangex*localy)] = tex_z;
340 linebuf[localy][localx] = rgb; 382 linebuf[localx + (rangex*localy)] = rgb;
341 } 383 }
342 } 384 }
343 } 385 }
344 } 386 }
345 387
348 SpanPackPtr tmp_spack = spack; 390 SpanPackPtr tmp_spack = spack;
349 spack = next_spack; 391 spack = next_spack;
350 next_spack = tmp_spack; 392 next_spack = tmp_spack;
351 } while (spack); 393 } while (spack);
352 394
395 writebuffer(display, rangex, rangey, screen_width);
396
397 // linebuf writebuffer() dma_store wait
398 // DrawSpan::~DrawSpan() free
399 //free(linebuf);
400 free(zRow);
353 401
354 FINISH: 402 FINISH:
403 /**
404 * goto FINISH; reboot
405 * linebuf, zRow free()
406 */
355 free(free_spack); 407 free(free_spack);
356 free(linebuf);
357 free(zRow);
358 408
359 return 0; 409 return 0;
360 } 410 }