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