Mercurial > hg > Game > Cerium
annotate Renderer/Engine/spe/DrawSpan.cc @ 1144:e068c1269292 draft
light fix
author | Yutaka_Kinjyo |
---|---|
date | Sat, 19 Feb 2011 03:54:12 +0900 |
parents | 2a63ba2c9506 |
children | 4e898dca4ab9 |
rev | line source |
---|---|
539 | 1 #include <stdlib.h> |
2 #include <string.h> | |
1067 | 3 #include "polygon_pack.h" |
539 | 4 #include "DrawSpan.h" |
860 | 5 #include "task_texture.h" |
539 | 6 #include "viewer_types.h" |
7 #include "Func.h" | |
1050 | 8 #include "matrix_calc.h" |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
9 #include "SchedTask.h" |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
10 #include "Tapestry.h" |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
11 #include "SpanPack.h" |
764 | 12 #include <spu_intrinsics.h> |
761 | 13 #include <math.h> |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
14 |
623 | 15 #if (__LITTLE_ENDIAN__) |
16 #define LITTLEENDIAN 1 | |
17 #else | |
18 #define LITTLEENDIAN 0 | |
19 #endif | |
539 | 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
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
28 typedef struct { |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
29 int **linebuf; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
30 float *zRow; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
31 TileListPtr tileList; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
32 int doneWrite; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
33 } G, *Gptr; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
34 |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
35 static int** linebuf_init(SchedTask *smanager, int width, int height, int rgb); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
36 static float* zRow_init(SchedTask *smanager, int width, int height); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
37 // static TilePtr set_rgb(memaddr addr, int wait_tag); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
38 // static void set_rgbs(memaddr addr, uint32 *max_addr, int wait_tag); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
39 static uint32 get_rgb(int tx, int ty, TilePtr tile); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
40 // static TilePtr isAvailableTile(memaddr addr); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
41 static memaddr getTile(int tx, int ty, int tw, memaddr tex_addr_top); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
42 static int getTexBlock(int tx, int ty, int twidth); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
43 |
747 | 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
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
47 |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
48 // static void reboot(SpanPackPtr spack, int cur_span_x); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
49 |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
50 static int drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
51 //static void drawDot2(SchedTask *smanager, SpanPtr span, int startx, int endx, int js, int wait_tag); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
52 static int drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
53 // static void drawLine2(SchedTask *smanager, SpanPtr span, int startx, int endx, int js, int wait_tag); |
747 | 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
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
58 |
774 | 59 |
60 static inline void | |
61 normalize(vector float& v0, vector float& v1) | |
761 | 62 { |
774 | 63 float norm; |
64 vector float ret __attribute__((aligned(16))) = {0,0,0,0}; | |
761 | 65 |
774 | 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); | |
761 | 72 } |
73 | |
774 | 74 static inline float |
75 innerProduct1(vector float& v0, vector float& v1) | |
621 | 76 { |
764 | 77 |
774 | 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 | |
539 | 90 |
764 | 91 |
774 | 92 |
539 | 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); |
539 | 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
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
159 static int ** |
539 | 160 linebuf_init(SchedTask *smanager, int width, int height, int rgb) |
161 { | |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
162 int **linebuf = (int**)smanager->allocate(height*sizeof(int*)); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
163 for(int h = 0;h<height;h++) { |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
164 int *buf = linebuf[h] = (int*)smanager->get_output(h); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
165 for (int i = 0; i < width; i++) { |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
166 buf[i] = rgb; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
167 } |
539 | 168 } |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
169 return linebuf; |
539 | 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; | |
539 | 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 | |
747 | 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) | |
539 | 217 { |
594 | 218 |
994 | 219 uint32 color = get_rgb(tex_x, tex_y, tile); |
594 | 220 /*下位4bitを抽出*/ |
623 | 221 #if LITTLEENDIAN |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
222 int alpha = color & 0x000000ff; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
223 #else |
599 | 224 int alpha = color & 0xff000000; |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
225 #endif |
994 | 226 |
227 //完全に透けているか判断 | |
1054 | 228 int flag = (alpha != 0); |
1123 | 229 |
793 | 230 int *light_sysswitch = (int*)smanager->global_get(LightSysSwitch); |
231 //smanager->printf("sys %d\n",light_sysswitch); | |
594 | 232 |
856 | 233 if ( *light_sysswitch == 1 && flag) { |
792 | 234 color = infinity_light_calc(color,normal_x,normal_y,normal_z, |
235 smanager,localx,localy,zpos, | |
236 world_x,world_y,world_z); | |
237 } | |
594 | 238 |
747 | 239 g->zRow[localx + (rangex*localy)] = zpos*flag + g->zRow[localx + (rangex*localy)]*(1-flag); |
994 | 240 int *point = &g->linebuf[localy][localx]; |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
241 *point = color*flag + *point *(1-flag); |
594 | 242 |
539 | 243 } |
244 | |
245 /** | |
246 * 長さが 1 の Span の描画 (要するに 1 pixel) | |
247 * | |
248 * @param span Span | |
249 * @param startx 描画開始範囲 | |
250 * @param endx 描画終了範囲 | |
251 */ | |
252 static int | |
253 drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
254 { | |
255 int rangex = endx - startx + 1; | |
256 | |
594 | 257 float normal_x = span->normal_x; |
258 float normal_y = span->normal_y; | |
259 float normal_z = span->normal_z; | |
260 | |
261 | |
539 | 262 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ |
263 int tex_xpos, tex_ypos; | |
264 | |
265 // span の始点に対応する Texture の座標 (tex1, tey1) | |
266 float tex = span->tex_x1; | |
267 float tey = span->tex_y1; | |
268 | |
269 // span の始点に対応する z 座標 | |
270 float zpos = span->start_z; | |
271 | |
272 /* Tile 内での座標 */ | |
273 int localx = getLocalX(span->x-1); | |
274 int localy = getLocalY(span->y-1); | |
275 | |
276 /** | |
277 * (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
278 * そのブロックのアドレス(MainMemory) | |
279 */ | |
280 int tex_localx; | |
281 int tex_localy; | |
282 memaddr tex_addr; | |
283 | |
284 if (span->x < startx || endx < span->x) { | |
285 return -1; | |
286 } | |
287 | |
288 tex_xpos = (int)((span->tex_width-1) * tex); | |
289 tex_ypos = (int)((span->tex_height-1) * tey); | |
290 | |
291 if (zpos < g->zRow[localx + (rangex*localy)]) { | |
1087 | 292 if (!span->tex_addr) return -1; // broken case, I'd like to write some thing... |
293 | |
539 | 294 tex_addr = getTile(tex_xpos, tex_ypos, |
295 span->tex_width, (memaddr)span->tex_addr); | |
296 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
297 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
298 | |
594 | 299 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); |
1121 | 300 smanager->wait_segment(tile); |
539 | 301 |
302 updateBuffer(g, zpos, rangex, localx, localy, | |
594 | 303 tex_localx, tex_localy, |
747 | 304 normal_x,normal_y,normal_z,tile, |
305 span->x, span->y, zpos, smanager); | |
539 | 306 } |
307 | |
308 return -1; | |
309 } | |
310 | |
594 | 311 #if 0 |
312 static void | |
313 drawDot2(SchedTask *smanager, SpanPtr span, int startx, int end, int js, int wait_tag) | |
314 { | |
315 //printf("%d\n", js); | |
316 } | |
317 #endif | |
539 | 318 |
319 /** | |
320 * 長さが 1 より大きい Span の描画 | |
321 * | |
322 * 本来の目的として、この関数(drawLine1) では | |
323 * : 既に SPE 上に Tile のある pixel だけ描画 | |
324 * : それ以外は、ここで予め DMA load しておき、 | |
325 * : drawLine2 で一気に描画する | |
326 * ってものだったんだけど、どうも上手く行かなかったので | |
327 * 今は drawLine1 で load -> wait -> rendering を全部やってます | |
328 * (rendering といっても、rendering buffer に書き込むだけで | |
329 * まだ main memory (frame buffer) に dma store してるわけではない) | |
330 * | |
331 * @param span Span | |
332 * @param startx 描画開始範囲 | |
333 * @param endx 描画終了範囲 | |
334 * @return 「span のどの位置まで rendering が終わったか」の x 座標 | |
335 */ | |
336 static int | |
337 drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
338 { | |
339 int x = span->x; | |
340 int rangex = endx - startx + 1; | |
341 int x_len = span->length_x; | |
342 | |
594 | 343 float normal_x = span->normal_x; |
344 float normal_y = span->normal_y; | |
345 float normal_z = span->normal_z; | |
346 | |
347 | |
539 | 348 int js = (x < startx) ? startx - x : 0; |
349 int je = (x + x_len > endx) ? endx - x : x_len; | |
350 | |
351 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ | |
352 int tex_xpos, tex_ypos; | |
353 | |
354 // span の始点に対応する座標 (tex1, tey1) | |
355 float tex1 = span->tex_x1; | |
356 float tey1 = span->tex_y1; | |
357 | |
358 // span の終点に対応する座標 (tex2, tey2) | |
359 float tex2 = span->tex_x2; | |
360 float tey2 = span->tex_y2; | |
361 | |
362 // span の始点、終点に対応する z 座標 | |
363 float zpos1 = span->start_z; | |
364 float zpos2 = span->end_z; | |
365 | |
747 | 366 //spanを右から左に見ていくうちに、zが下がるのか、上がっていくのか。 |
761 | 367 float z_inclination = (zpos2 - zpos1) / x_len; |
747 | 368 float world_z = zpos2; |
369 | |
539 | 370 // Tile 内での座標 |
371 int localx, localy = getLocalY(span->y-1); | |
372 | |
373 int ret = je+1; | |
374 | |
375 //for (int j = js; j <= je; j++) { | |
376 for (int j = je; j >= js; j--) { | |
377 float tex_x, tex_y, tex_z; | |
378 | |
761 | 379 world_z -= z_inclination; |
747 | 380 |
539 | 381 localx = getLocalX(x-1+j); |
382 | |
383 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1); | |
384 | |
385 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1); | |
386 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1); | |
387 if (tex_x > 1) tex_x = 1; | |
388 if (tex_x < 0) tex_x = 0; | |
389 if (tex_y > 1) tex_y = 1; | |
390 if (tex_y < 0) tex_y = 0; | |
391 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
392 tex_ypos = (int)((span->tex_height-1) * tex_y); | |
393 | |
394 if (tex_z < g->zRow[localx + (rangex*localy)]) { | |
395 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
396 // そのブロックのアドレス(MainMemory) | |
397 memaddr tex_addr; | |
398 int tex_localx; | |
399 int tex_localy; | |
400 | |
1087 | 401 if (!span->tex_addr) return -1; // broken case, I'd like to write some thing... |
402 tex_addr = getTile(tex_xpos, tex_ypos, | |
539 | 403 span->tex_width, (memaddr)span->tex_addr); |
404 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
405 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
406 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); | |
407 smanager->wait_segment(tile); | |
408 | |
409 updateBuffer(g, tex_z, rangex, localx, localy, | |
594 | 410 tex_localx, tex_localy, |
747 | 411 normal_x, normal_y, normal_z, tile, |
412 span->x+j, span->y, world_z, smanager); | |
539 | 413 } |
414 } | |
415 | |
416 return ret; | |
417 } | |
418 | |
594 | 419 static int |
747 | 420 infinity_light_calc(int color,float normal_x, float normal_y, |
421 float normal_z, SchedTask *smanager, int x, int y, float z, | |
422 int world_x, int world_y, float world_z) | |
423 | |
539 | 424 { |
764 | 425 |
594 | 426 unsigned char rgb[4]; |
427 | |
428 // 引数で受け取った color の rgb 情報の抜き出し | |
623 | 429 #if LITTLEENDIAN |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
430 rgb[0] = (color & 0xff000000) >> 24; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
431 rgb[1] = (color & 0x00ff0000) >> 16; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
432 rgb[2] = (color & 0x0000ff00) >> 8; |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
433 rgb[3] = (color & 0x000000ff); |
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
434 #else |
594 | 435 rgb[3] = (color & 0xff000000) >> 24; |
436 rgb[2] = (color & 0x00ff0000) >> 16; | |
437 rgb[1] = (color & 0x0000ff00) >> 8; | |
438 rgb[0] = (color & 0x000000ff); | |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
439 #endif |
594 | 440 |
764 | 441 |
774 | 442 vector float *light_xyz = (vector float*)smanager->global_get(Light); |
443 | |
764 | 444 vector float v_rgb __attribute__((aligned(16))) = {(float)rgb[0],(float)rgb[1],(float)rgb[2],0}; |
774 | 445 vector float normal_vector __attribute__((aligned(16))) = {normal_x,normal_y,normal_z,0}; |
446 vector float light_vector __attribute__((aligned(16))) = {0,0,0,0}; | |
764 | 447 vector float v_inner __attribute__((aligned(16))); |
448 vector float v_world[4] __attribute__((aligned(16))) = {{world_x, world_y, -world_z, 0}, | |
449 {world_x, world_y, -world_z, 0}, | |
450 {world_x, world_y, -world_z, 0}, | |
793 | 451 {world_x, world_y, -world_z, 0}}; |
764 | 452 |
453 | |
774 | 454 int light_rgb; |
1144 | 455 float inner_product = 0.2; |
764 | 456 |
774 | 457 normalize(normal_vector, normal_vector); |
458 | |
459 vector float vtmp_rgb __attribute__((aligned(16))) = {0,0,0,0}; | |
764 | 460 |
761 | 461 int light_num = 4; |
792 | 462 int *light_switch = (int*)smanager->global_get(LightSwitch); |
764 | 463 |
761 | 464 for (int i = 0; i < light_num; i++) { |
465 | |
792 | 466 if (light_switch[i] == 1) { |
467 light_vector = spu_sub(v_world[i],light_xyz[i]); | |
468 normalize(light_vector, light_vector); | |
1144 | 469 float tmp_inner_product = innerProduct1(normal_vector,light_vector); |
470 | |
471 if (inner_product < tmp_inner_product) { | |
472 inner_product = tmp_inner_product; | |
473 } | |
474 | |
792 | 475 v_inner = spu_splats(inner_product); |
476 vtmp_rgb = spu_madd(v_rgb,v_inner,vtmp_rgb); | |
477 } | |
761 | 478 |
479 } | |
480 | |
764 | 481 vector unsigned int v_flag __attribute__((aligned(16))); |
774 | 482 vector float max_rgb __attribute__((aligned(16))) = (vector float)spu_splats((float)255.0f); |
761 | 483 |
774 | 484 v_flag = spu_cmpgt(max_rgb,vtmp_rgb); |
485 vtmp_rgb = spu_sel(max_rgb,vtmp_rgb,v_flag); | |
486 | |
487 vector unsigned int last_rgb __attribute__((aligned(16))); | |
488 last_rgb = spu_convtu(vtmp_rgb,0); | |
761 | 489 |
594 | 490 |
491 //計算した rgb を light_rgb にまとめる。 | |
623 | 492 #if LITTLEENDIAN |
764 | 493 light_rgb = (last_rgb[0] << 24) + (last_rgb[1] << 16) + (last_rgb[2] << 8) + (last_rgb[3]); |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
494 #else |
764 | 495 light_rgb = (last_rgb[3] << 24) + (last_rgb[2] << 16) + (last_rgb[1] << 8) + (last_rgb[0]); |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
496 #endif |
594 | 497 |
498 return light_rgb; | |
539 | 499 } |
594 | 500 |
539 | 501 |
502 static int | |
503 run(SchedTask *smanager, void *rbuf, void *wbuf) | |
504 { | |
838 | 505 |
506 if ((int)smanager->get_param(5) == 0) { | |
507 | |
508 int rangex_start = (long)smanager->get_param(2); | |
509 int rangex_end = (long)smanager->get_param(3); | |
510 | |
511 int rangex = rangex_end - rangex_start + 1; | |
512 int rangey = (long)smanager->get_param(4); | |
513 | |
914 | 514 for(int h = 0; h < rangey; h++) { |
515 int *buf = (int*)smanager->get_output(h); | |
516 for (int i = 0; i < rangex; i++) { | |
517 buf[i] = 0; | |
518 } | |
519 } | |
838 | 520 |
521 return 0; | |
522 | |
523 } | |
524 | |
623 | 525 G g0; |
526 Gptr g = &g0; | |
539 | 527 |
528 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0); | |
529 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); | |
530 SpanPackPtr free_spack = next_spack; // next_spack の free() 用 | |
531 Span *span; | |
532 | |
533 Span nop_span; | |
534 nop_span.length_x = 1; | |
535 | |
626
0e91ddaad798
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
536 // uint32 display = (long)smanager->get_param(0); |
0e91ddaad798
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
537 // int screen_width = (long)smanager->get_param(1); |
0e91ddaad798
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
538 int rangex_start = (long)smanager->get_param(2); |
0e91ddaad798
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
539 int rangex_end = (long)smanager->get_param(3); |
539 | 540 |
541 // このタスクが担当する x の範囲 | |
542 int rangex = rangex_end - rangex_start + 1; | |
543 | |
544 // y の範囲 | |
626
0e91ddaad798
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
545 int rangey = (long)smanager->get_param(4); |
539 | 546 g->tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); |
547 | |
548 g->zRow = zRow_init(smanager, rangex, rangey); | |
549 g->linebuf = linebuf_init(smanager, rangex, rangey, 0); | |
550 | |
551 int tl_tag[2] = {TEX_LOAD1, TEX_LOAD2}; | |
552 int tl_tag_flg1 = 0; | |
553 int tl_tag_flg2 = 1; | |
554 | |
555 do { | |
556 /** | |
557 * SpanPack->next が存在する場合、 | |
558 * 現在の SpanPack を処理してる間に | |
559 * 次の SpanPack の DMA 転送を行う | |
560 */ | |
561 if (spack->next != NULL) { | |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
562 smanager->dma_load(next_spack, (memaddr)spack->next, |
539 | 563 sizeof(SpanPack), SPAN_PACK_LOAD); |
564 } else { | |
565 next_spack = NULL; | |
566 } | |
567 | |
568 SpanPtr resume_span = &nop_span; | |
569 | |
570 for (int t = 0; t < spack->info.size; t++) { | |
571 SpanPtr next_span; | |
572 | |
573 span = &spack->span[t]; | |
574 | |
622
911050e9f99d
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
575 if (span->length_x != 1) { |
994 | 576 drawLine1( |
577 smanager, g, | |
594 | 578 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); |
622
911050e9f99d
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
579 } else { |
994 | 580 drawDot1( |
581 smanager, g, | |
582 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); | |
622
911050e9f99d
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
583 } |
539 | 584 next_span = span; |
585 | |
586 resume_span = next_span; | |
587 | |
588 tl_tag_flg1 ^= 1; | |
589 tl_tag_flg2 ^= 1; | |
590 } | |
591 | |
592 smanager->dma_wait(SPAN_PACK_LOAD); | |
593 | |
594 SpanPackPtr tmp_spack = spack; | |
595 spack = next_spack; | |
596 next_spack = tmp_spack; | |
597 } while (spack); | |
598 | |
599 | |
600 free(g->zRow); | |
618
58417006268c
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
615
diff
changeset
|
601 free(g->linebuf); |
539 | 602 |
603 //FINISH: | |
604 /** | |
605 * goto FINISH; の時は reboot なので | |
606 * linebuf, zRow は free() しない | |
607 */ | |
608 | |
609 free(free_spack); | |
610 | |
611 | |
612 return 0; | |
613 } | |
594 | 614 |
615 /* end */ |