Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/spe/DrawSpan.cc @ 775:97a514cf6ad3 before-simple-task
simd
author | yutaka@henri.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Tue, 16 Feb 2010 15:09:54 +0900 |
parents | dc26593f8c40 |
children | 4455e7b0caf3 |
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" |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
10 #include "SchedTask.h" |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
11 #include "Tapestry.h" |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
12 #include "SpanPack.h" |
765 | 13 #include <spu_intrinsics.h> |
762 | 14 #include <math.h> |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
15 |
623 | 16 #if (__LITTLE_ENDIAN__) |
17 #define LITTLEENDIAN 1 | |
18 #else | |
19 #define LITTLEENDIAN 0 | |
20 #endif | |
507 | 21 |
22 SchedDefineTask(DrawSpan); | |
23 | |
24 #define TEX_LOAD1 0 | |
25 #define TEX_LOAD2 1 | |
26 #define SPAN_PACK_LOAD 2 | |
27 #define FB_STORE 3 | |
28 | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
29 typedef struct { |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
30 int **linebuf; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
31 float *zRow; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
32 TileListPtr tileList; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
33 int doneWrite; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
34 } G, *Gptr; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
35 |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
36 static int** linebuf_init(SchedTask *smanager, int width, int height, int rgb); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
37 static float* zRow_init(SchedTask *smanager, int width, int height); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
38 // static TilePtr set_rgb(memaddr addr, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
39 // static void set_rgbs(memaddr addr, uint32 *max_addr, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
40 static uint32 get_rgb(int tx, int ty, TilePtr tile); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
41 // static TilePtr isAvailableTile(memaddr addr); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
42 static memaddr getTile(int tx, int ty, int tw, memaddr tex_addr_top); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
43 static int getTexBlock(int tx, int ty, int twidth); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
44 |
748 | 45 static void updateBuffer(Gptr g, float zpos, int rangex, int loaclx, int localy, |
46 int tex_x, int tex_y, float normal_x, float nomral_x, float normal_z, | |
47 TilePtr tile, int world_x, int world_y, float world_z,SchedTask *smanager); | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
48 |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
49 // static void reboot(SpanPackPtr spack, int cur_span_x); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
50 |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
51 static int drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
52 //static void drawDot2(SchedTask *smanager, SpanPtr span, int startx, int endx, int js, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
53 static int drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
54 // static void drawLine2(SchedTask *smanager, SpanPtr span, int startx, int endx, int js, int wait_tag); |
748 | 55 |
56 static int infinity_light_calc(int color,float normal_x, float normal_y, float normal_z, | |
57 SchedTask *smanager,int x, int y, float z, int world_x, int world_y, float world_z); | |
58 | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
59 |
775 | 60 |
61 static inline void | |
62 normalize(vector float& v0, vector float& v1) | |
762 | 63 { |
775 | 64 float norm; |
65 vector float ret __attribute__((aligned(16))) = {0,0,0,0}; | |
762 | 66 |
775 | 67 ret = spu_mul(v0,v1); |
68 norm = (ret[0] + ret[1] + ret[2]); | |
69 | |
70 ret = (vector float)spu_splats(norm); | |
71 ret = spu_rsqrte(ret); | |
72 v0 = spu_mul(v1,ret); | |
762 | 73 } |
74 | |
775 | 75 static inline float |
76 innerProduct1(vector float& v0, vector float& v1) | |
621 | 77 { |
765 | 78 |
775 | 79 vector float ret __attribute__((aligned(16))) = {0,0,0,0}; |
80 float inner; | |
81 ret = spu_mul(v0,v1); | |
82 | |
83 inner = (ret[0] + ret[1] + ret[2]); | |
84 if (inner < 0) { | |
85 inner = 0; | |
86 } | |
87 | |
88 return inner; | |
621 | 89 } |
90 | |
507 | 91 |
765 | 92 |
775 | 93 |
507 | 94 /** |
95 * テクスチャは、TEXTURE_SPLIT_PIXEL^2 のブロックに分割する | |
96 * | |
97 * +---+---+---+---+---+---+ | |
98 * | 0 | 1 | 2 | 3 | 4 | 5 | | |
99 * +---+---+---+---+---+---+ | |
100 * | | | | | |11 | | |
101 * +---+---+---+---+---+---+ | |
102 * | | | | | |17 | | |
103 * +---+---+---+---+---+---+ | |
104 * | | | | | |23 | | |
105 * +---+---+---+---+---+---+ | |
106 * | | | | | |29 | | |
107 * +---+---+---+---+---+---+ | |
108 * | | | | | |35 | | |
109 * +---+---+---+---+---+---+ | |
110 * | |
111 * 一辺を TEXTURE_SPLIT とする | |
112 * 各ブロックの数字がブロックIDとなる。 | |
113 */ | |
114 | |
115 /** | |
116 * テクスチャの座標から、 | |
117 * テクスチャのどのブロックかを求める | |
118 * | |
119 * @param[in] tx X coordinates of texture | |
120 * @param[in] tx Y coordinates of texture | |
121 * @param[in] twidth Width of texture | |
122 * @return block ID | |
123 */ | |
124 static int | |
125 getTexBlock(int tx, int ty, int twidth) | |
126 { | |
127 int blockX, blockY; | |
128 | |
129 blockX = tx / TEXTURE_SPLIT_PIXEL; | |
130 blockY = ty / TEXTURE_SPLIT_PIXEL; | |
131 | |
132 return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY; | |
133 } | |
134 | |
135 /** | |
136 * block ID と、テクスチャの TOP address から | |
137 * (tx,ty) で使われるテクスチャの Tile addres を求める | |
138 * | |
139 * @param[in] tx X coordinates of texture | |
140 * @param[in] tx Y coordinates of texture | |
141 * @param[in] tw Width of texture | |
142 * @param[in] tex_addr_top (tx,ty) で使うテクスチャの先頭address | |
143 * @return block ID | |
144 */ | |
145 static memaddr | |
146 getTile(int tx, int ty, int tw, memaddr tex_addr_top) | |
147 { | |
148 int block = getTexBlock(tx, ty, tw); | |
594 | 149 return tex_addr_top + block * TEXTURE_BLOCK_SIZE * sizeof(uint32); |
507 | 150 } |
151 | |
152 /** | |
153 * FrameBuffer に書き込む rgb の領域初期化 | |
154 * | |
155 * @param width Width of Buffer | |
156 * @param height Height of Buffer | |
157 * @param rgb Initial value of RGB at Buffer | |
158 * @return Buffer | |
159 */ | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
160 static int ** |
507 | 161 linebuf_init(SchedTask *smanager, int width, int height, int rgb) |
162 { | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
163 int **linebuf = (int**)smanager->allocate(height*sizeof(int*)); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
164 for(int h = 0;h<height;h++) { |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
165 int *buf = linebuf[h] = (int*)smanager->get_output(h); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
166 for (int i = 0; i < width; i++) { |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
167 buf[i] = rgb; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
168 } |
507 | 169 } |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
170 return linebuf; |
507 | 171 } |
172 | |
173 /** | |
174 * Z-Buffer の初期化 | |
175 * | |
176 * @param width Width of Z-Buffer | |
177 * @param height Height of Z-Buffer | |
178 * @return Z-Buffer | |
179 */ | |
180 static float* | |
181 zRow_init(SchedTask *smanager, int width, int height) | |
182 { | |
183 float *buf = (float*)smanager->allocate(sizeof(float)*width*height); | |
184 float def = 65535.0f; | |
185 | |
594 | 186 for (int i = 0; i < width*height; i++) { |
187 buf[i] = def; | |
507 | 188 } |
189 | |
190 return buf; | |
191 } | |
192 | |
193 | |
194 static uint32 | |
195 get_rgb(int tx, int ty, TilePtr tile) | |
196 { | |
197 uint32 *data = (uint32 *)tile->data; | |
198 return data[(TEXTURE_SPLIT_PIXEL)*ty+tx]; | |
199 } | |
200 | |
201 | |
202 | |
203 /** | |
204 * zRow と Linebuf を更新する | |
205 * | |
206 * @param zpos 更新する pixel のZ座標 | |
207 * @param rangex このタスクが処理する描画領域の x の長さ | |
208 * @param x pixel の、描画領域内での x 座標 | |
209 * @param y 〃 の、y 座標 | |
210 * @param tex_x pixel が使用するテクスチャの、Tile (8x8) 内での x 座標 | |
211 * @param tex_y 〃 の y 座標 | |
212 * @param tex_addr テクスチャのアドレス(MainMemory) | |
213 */ | |
214 static void | |
748 | 215 updateBuffer(Gptr g, float zpos, int rangex, int localx, int localy, int tex_x, int tex_y, |
216 float normal_x, float normal_y, float normal_z, TilePtr tile, | |
217 int world_x, int world_y, float world_z, SchedTask *smanager) | |
507 | 218 { |
594 | 219 |
220 int color = get_rgb(tex_x, tex_y, tile); | |
221 /*下位4bitを抽出*/ | |
623 | 222 #if LITTLEENDIAN |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
223 int alpha = color & 0x000000ff; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
224 #else |
599 | 225 int alpha = color & 0xff000000; |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
226 #endif |
594 | 227 /*完全に透けているか判断*/ |
228 int flag = (alpha != 0); | |
229 | |
748 | 230 color = infinity_light_calc(color,normal_x,normal_y,normal_z, |
231 smanager,localx,localy,zpos, | |
232 world_x,world_y,world_z); | |
594 | 233 |
748 | 234 g->zRow[localx + (rangex*localy)] = zpos*flag + g->zRow[localx + (rangex*localy)]*(1-flag); |
235 int *point = &g->linebuf[localy][localx] ; | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
236 *point = color*flag + *point *(1-flag); |
594 | 237 |
748 | 238 |
239 | |
507 | 240 } |
241 | |
242 /** | |
243 * 長さが 1 の Span の描画 (要するに 1 pixel) | |
244 * | |
245 * @param span Span | |
246 * @param startx 描画開始範囲 | |
247 * @param endx 描画終了範囲 | |
248 */ | |
249 static int | |
250 drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
251 { | |
252 int rangex = endx - startx + 1; | |
253 | |
594 | 254 float normal_x = span->normal_x; |
255 float normal_y = span->normal_y; | |
256 float normal_z = span->normal_z; | |
257 | |
258 | |
507 | 259 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ |
260 int tex_xpos, tex_ypos; | |
261 | |
262 // span の始点に対応する Texture の座標 (tex1, tey1) | |
263 float tex = span->tex_x1; | |
264 float tey = span->tex_y1; | |
265 | |
266 // span の始点に対応する z 座標 | |
267 float zpos = span->start_z; | |
268 | |
269 /* Tile 内での座標 */ | |
270 int localx = getLocalX(span->x-1); | |
271 int localy = getLocalY(span->y-1); | |
272 | |
273 /** | |
274 * (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
275 * そのブロックのアドレス(MainMemory) | |
276 */ | |
277 int tex_localx; | |
278 int tex_localy; | |
279 memaddr tex_addr; | |
280 | |
281 if (span->x < startx || endx < span->x) { | |
282 return -1; | |
283 } | |
284 | |
285 tex_xpos = (int)((span->tex_width-1) * tex); | |
286 tex_ypos = (int)((span->tex_height-1) * tey); | |
287 | |
288 if (zpos < g->zRow[localx + (rangex*localy)]) { | |
289 tex_addr = getTile(tex_xpos, tex_ypos, | |
290 span->tex_width, (memaddr)span->tex_addr); | |
291 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
292 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
293 | |
594 | 294 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); |
295 smanager->wait_segment(tile); | |
507 | 296 |
297 updateBuffer(g, zpos, rangex, localx, localy, | |
594 | 298 tex_localx, tex_localy, |
748 | 299 normal_x,normal_y,normal_z,tile, |
300 span->x, span->y, zpos, smanager); | |
507 | 301 } |
302 | |
303 return -1; | |
304 } | |
305 | |
594 | 306 #if 0 |
307 static void | |
308 drawDot2(SchedTask *smanager, SpanPtr span, int startx, int end, int js, int wait_tag) | |
309 { | |
310 //printf("%d\n", js); | |
311 } | |
312 #endif | |
507 | 313 |
314 /** | |
315 * 長さが 1 より大きい Span の描画 | |
316 * | |
317 * 本来の目的として、この関数(drawLine1) では | |
318 * : 既に SPE 上に Tile のある pixel だけ描画 | |
319 * : それ以外は、ここで予め DMA load しておき、 | |
320 * : drawLine2 で一気に描画する | |
321 * ってものだったんだけど、どうも上手く行かなかったので | |
322 * 今は drawLine1 で load -> wait -> rendering を全部やってます | |
323 * (rendering といっても、rendering buffer に書き込むだけで | |
324 * まだ main memory (frame buffer) に dma store してるわけではない) | |
325 * | |
326 * @param span Span | |
327 * @param startx 描画開始範囲 | |
328 * @param endx 描画終了範囲 | |
329 * @return 「span のどの位置まで rendering が終わったか」の x 座標 | |
330 */ | |
331 static int | |
332 drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
333 { | |
334 int x = span->x; | |
335 int rangex = endx - startx + 1; | |
336 int x_len = span->length_x; | |
337 | |
594 | 338 float normal_x = span->normal_x; |
339 float normal_y = span->normal_y; | |
340 float normal_z = span->normal_z; | |
341 | |
342 | |
507 | 343 int js = (x < startx) ? startx - x : 0; |
344 int je = (x + x_len > endx) ? endx - x : x_len; | |
345 | |
346 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ | |
347 int tex_xpos, tex_ypos; | |
348 | |
349 // span の始点に対応する座標 (tex1, tey1) | |
350 float tex1 = span->tex_x1; | |
351 float tey1 = span->tex_y1; | |
352 | |
353 // span の終点に対応する座標 (tex2, tey2) | |
354 float tex2 = span->tex_x2; | |
355 float tey2 = span->tex_y2; | |
356 | |
357 // span の始点、終点に対応する z 座標 | |
358 float zpos1 = span->start_z; | |
359 float zpos2 = span->end_z; | |
360 | |
748 | 361 //spanを右から左に見ていくうちに、zが下がるのか、上がっていくのか。 |
762 | 362 float z_inclination = (zpos2 - zpos1) / x_len; |
748 | 363 float world_z = zpos2; |
364 | |
507 | 365 // Tile 内での座標 |
366 int localx, localy = getLocalY(span->y-1); | |
367 | |
368 int ret = je+1; | |
369 | |
370 //for (int j = js; j <= je; j++) { | |
371 for (int j = je; j >= js; j--) { | |
372 float tex_x, tex_y, tex_z; | |
373 | |
762 | 374 world_z -= z_inclination; |
748 | 375 |
507 | 376 localx = getLocalX(x-1+j); |
377 | |
378 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1); | |
379 | |
380 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1); | |
381 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1); | |
382 if (tex_x > 1) tex_x = 1; | |
383 if (tex_x < 0) tex_x = 0; | |
384 if (tex_y > 1) tex_y = 1; | |
385 if (tex_y < 0) tex_y = 0; | |
386 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
387 tex_ypos = (int)((span->tex_height-1) * tex_y); | |
388 | |
389 if (tex_z < g->zRow[localx + (rangex*localy)]) { | |
390 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
391 // そのブロックのアドレス(MainMemory) | |
392 memaddr tex_addr; | |
393 int tex_localx; | |
394 int tex_localy; | |
395 | |
396 tex_addr = getTile(tex_xpos, tex_ypos, | |
397 span->tex_width, (memaddr)span->tex_addr); | |
398 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
399 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
400 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); | |
401 smanager->wait_segment(tile); | |
402 | |
403 updateBuffer(g, tex_z, rangex, localx, localy, | |
594 | 404 tex_localx, tex_localy, |
748 | 405 normal_x, normal_y, normal_z, tile, |
406 span->x+j, span->y, world_z, smanager); | |
507 | 407 } |
408 } | |
409 | |
410 return ret; | |
411 } | |
412 | |
594 | 413 static int |
748 | 414 infinity_light_calc(int color,float normal_x, float normal_y, |
415 float normal_z, SchedTask *smanager, int x, int y, float z, | |
416 int world_x, int world_y, float world_z) | |
417 | |
507 | 418 { |
765 | 419 |
594 | 420 unsigned char rgb[4]; |
421 | |
422 // 引数で受け取った color の rgb 情報の抜き出し | |
623 | 423 #if LITTLEENDIAN |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
424 rgb[0] = (color & 0xff000000) >> 24; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
425 rgb[1] = (color & 0x00ff0000) >> 16; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
426 rgb[2] = (color & 0x0000ff00) >> 8; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
427 rgb[3] = (color & 0x000000ff); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
428 #else |
594 | 429 rgb[3] = (color & 0xff000000) >> 24; |
430 rgb[2] = (color & 0x00ff0000) >> 16; | |
431 rgb[1] = (color & 0x0000ff00) >> 8; | |
432 rgb[0] = (color & 0x000000ff); | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
433 #endif |
594 | 434 |
765 | 435 |
775 | 436 vector float *light_xyz = (vector float*)smanager->global_get(Light); |
437 | |
765 | 438 vector float v_rgb __attribute__((aligned(16))) = {(float)rgb[0],(float)rgb[1],(float)rgb[2],0}; |
775 | 439 vector float normal_vector __attribute__((aligned(16))) = {normal_x,normal_y,normal_z,0}; |
440 vector float light_vector __attribute__((aligned(16))) = {0,0,0,0}; | |
765 | 441 vector float v_inner __attribute__((aligned(16))); |
442 vector float v_world[4] __attribute__((aligned(16))) = {{world_x, world_y, -world_z, 0}, | |
443 {world_x, world_y, -world_z, 0}, | |
444 {world_x, world_y, -world_z, 0}, | |
445 {0, 0, 0, 0}}; | |
446 | |
447 | |
775 | 448 int light_rgb; |
449 float inner_product; | |
765 | 450 |
775 | 451 normalize(normal_vector, normal_vector); |
452 | |
453 vector float vtmp_rgb __attribute__((aligned(16))) = {0,0,0,0}; | |
765 | 454 |
762 | 455 int light_num = 4; |
765 | 456 |
762 | 457 for (int i = 0; i < light_num; i++) { |
458 | |
775 | 459 light_vector = spu_sub(v_world[i],light_xyz[i]); |
460 normalize(light_vector, light_vector); | |
461 inner_product = innerProduct1(normal_vector,light_vector); | |
765 | 462 v_inner = spu_splats(inner_product); |
775 | 463 vtmp_rgb = spu_madd(v_rgb,v_inner,vtmp_rgb); |
762 | 464 |
465 } | |
466 | |
765 | 467 vector unsigned int v_flag __attribute__((aligned(16))); |
775 | 468 vector float max_rgb __attribute__((aligned(16))) = (vector float)spu_splats((float)255.0f); |
762 | 469 |
775 | 470 v_flag = spu_cmpgt(max_rgb,vtmp_rgb); |
471 vtmp_rgb = spu_sel(max_rgb,vtmp_rgb,v_flag); | |
472 | |
473 vector unsigned int last_rgb __attribute__((aligned(16))); | |
474 last_rgb = spu_convtu(vtmp_rgb,0); | |
762 | 475 |
594 | 476 |
477 //計算した rgb を light_rgb にまとめる。 | |
623 | 478 #if LITTLEENDIAN |
765 | 479 light_rgb = (last_rgb[0] << 24) + (last_rgb[1] << 16) + (last_rgb[2] << 8) + (last_rgb[3]); |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
480 #else |
765 | 481 light_rgb = (last_rgb[3] << 24) + (last_rgb[2] << 16) + (last_rgb[1] << 8) + (last_rgb[0]); |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
482 #endif |
594 | 483 |
484 return light_rgb; | |
507 | 485 } |
594 | 486 |
507 | 487 |
488 static int | |
489 run(SchedTask *smanager, void *rbuf, void *wbuf) | |
490 { | |
623 | 491 G g0; |
492 Gptr g = &g0; | |
507 | 493 |
494 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0); | |
495 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); | |
496 SpanPackPtr free_spack = next_spack; // next_spack の free() 用 | |
497 Span *span; | |
498 | |
499 Span nop_span; | |
500 nop_span.length_x = 1; | |
501 | |
626
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
502 // uint32 display = (long)smanager->get_param(0); |
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
503 // int screen_width = (long)smanager->get_param(1); |
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
504 int rangex_start = (long)smanager->get_param(2); |
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
505 int rangex_end = (long)smanager->get_param(3); |
507 | 506 |
507 // このタスクが担当する x の範囲 | |
508 int rangex = rangex_end - rangex_start + 1; | |
509 | |
510 // y の範囲 | |
626
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
511 int rangey = (long)smanager->get_param(4); |
507 | 512 g->tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); |
513 | |
514 g->zRow = zRow_init(smanager, rangex, rangey); | |
515 g->linebuf = linebuf_init(smanager, rangex, rangey, 0); | |
516 | |
517 int tl_tag[2] = {TEX_LOAD1, TEX_LOAD2}; | |
518 int tl_tag_flg1 = 0; | |
519 int tl_tag_flg2 = 1; | |
520 | |
521 do { | |
522 /** | |
523 * SpanPack->next が存在する場合、 | |
524 * 現在の SpanPack を処理してる間に | |
525 * 次の SpanPack の DMA 転送を行う | |
526 */ | |
527 if (spack->next != NULL) { | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
528 smanager->dma_load(next_spack, (memaddr)spack->next, |
507 | 529 sizeof(SpanPack), SPAN_PACK_LOAD); |
530 } else { | |
531 next_spack = NULL; | |
532 } | |
533 | |
534 SpanPtr resume_span = &nop_span; | |
535 | |
536 for (int t = 0; t < spack->info.size; t++) { | |
537 SpanPtr next_span; | |
538 | |
539 span = &spack->span[t]; | |
540 | |
622
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
541 if (span->length_x != 1) { |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
542 drawLine1( |
594 | 543 smanager, g, |
544 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); | |
622
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
545 } else { |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
546 drawDot1( |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
547 smanager, g, |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
548 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
549 } |
507 | 550 next_span = span; |
551 | |
552 resume_span = next_span; | |
553 | |
554 tl_tag_flg1 ^= 1; | |
555 tl_tag_flg2 ^= 1; | |
556 } | |
557 | |
558 smanager->dma_wait(SPAN_PACK_LOAD); | |
559 | |
560 SpanPackPtr tmp_spack = spack; | |
561 spack = next_spack; | |
562 next_spack = tmp_spack; | |
563 } while (spack); | |
564 | |
565 | |
566 free(g->zRow); | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
567 free(g->linebuf); |
507 | 568 |
569 //FINISH: | |
570 /** | |
571 * goto FINISH; の時は reboot なので | |
572 * linebuf, zRow は free() しない | |
573 */ | |
574 | |
575 free(free_spack); | |
576 | |
577 | |
578 return 0; | |
579 } | |
594 | 580 |
581 /* end */ |