Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Test/test_render/spe/DrawSpan.cpp @ 128:776eca0daa02
texture load use hash table
author | gongo@charles.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Tue, 25 Nov 2008 15:52:28 +0900 |
parents | 7635f223fc7d |
children | 805d27efafd8 |
rev | line source |
---|---|
109 | 1 #include <stdlib.h> |
2 #include <string.h> | |
3 #include "DrawSpan.h" | |
4 #include "polygon_pack.h" | |
5 #include "SpanPack.h" | |
6 #include "texture.h" | |
7 #include "viewer_types.h" | |
8 | |
9 #define SPAN_PACK_LOAD 0 | |
120 | 10 #define TEX_LOAD 1 |
109 | 11 |
12 SchedDefineTask(DrawSpan); | |
13 | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
14 static const int hashsize = 263; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
15 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
16 static TilePtr hash_table[hashsize] = {NULL}; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
17 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
18 unsigned short PRIME[8] = { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
19 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd, |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
20 }; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
21 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
22 static TileListPtr tileList; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
23 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
24 static int |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
25 hash(uint32 data) |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
26 { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
27 int value = 0; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
28 int n = 0; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
29 int key; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
30 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
31 for (int i = 0; i < 8; i ++) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
32 key = data & 0xf; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
33 value += key * PRIME[n++]; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
34 data >>= 4; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
35 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
36 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
37 return value % hashsize; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
38 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
39 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
40 static int |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
41 put(void *key, TilePtr data) |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
42 { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
43 int hashval = hash((uint32)key); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
44 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
45 for (int i = 0; i < hashsize/2; i++) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
46 int index = (hashval + i*i)%hashsize; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
47 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
48 if (hash_table[index] == 0) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
49 hash_table[index] = data; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
50 return index; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
51 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
52 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
53 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
54 return -1; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
55 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
56 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
57 static TilePtr |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
58 get(void *key) |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
59 { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
60 int hashval = hash((uint32)key); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
61 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
62 for (int i = 0; i < hashsize/2; i++) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
63 int index = (hashval + i*i)%hashsize; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
64 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
65 if (hash_table[index]->texture_addr == key) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
66 return hash_table[index]; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
67 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
68 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
69 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
70 return NULL; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
71 } |
120 | 72 |
109 | 73 void |
74 DrawSpan::linebuf_init(int *buf, int x, int rgb) | |
75 { | |
76 for (int i = 0; i < x; i++) { | |
77 buf[i] = rgb; | |
78 } | |
79 } | |
80 | |
81 float* | |
82 DrawSpan::zRow_init(int w, int h) | |
83 { | |
84 float *zRow = NULL; | |
85 float z = 65535.0f; | |
86 int length = w*h; | |
87 | |
88 zRow = (float*)smanager->allocate(sizeof(float)*length); | |
89 | |
90 for (int i = 0; i < length; i++) { | |
91 zRow[i] = z; | |
92 } | |
93 | |
94 return zRow; | |
95 } | |
96 | |
97 | |
98 char* | |
99 DrawSpan::get_pixel(int tx, int ty, void *texture_image) | |
100 { | |
120 | 101 #if 0 |
109 | 102 return (char*)texture_image+(3*((128)*ty+tx)); |
120 | 103 #else |
104 return (char*)texture_image+(4*((8)*ty+tx)); | |
105 #endif | |
109 | 106 } |
107 | |
108 Uint32 | |
109 DrawSpan::get_rgb(int tx, int ty, void *texture) | |
110 { | |
111 Uint8 red, green, blue, alpha; | |
112 | |
113 if (tx<0) tx = 0; | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
114 if (tex_width-1< tx) tx = tex_width-1 ; |
109 | 115 if (ty<0) ty = 0; |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
116 if (tex_height-1< ty) ty = tex_height-1 ; |
109 | 117 |
120 | 118 void *texture_addr; |
109 | 119 |
120 | 120 int blockX = tx / 8; |
121 int blockY = ty / 8; | |
122 void** addrList = (void**)global_get(TEXTURE2_ID); | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
123 TilePtr tile; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
124 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
125 texture_addr = addrList[blockX + (tex_width/8)*blockY]; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
126 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
127 tile = get(texture_addr); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
128 if (tile == NULL) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
129 if (tileList->size >= MAX_TILE) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
130 tileList->init(); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
131 bzero(hash_table, sizeof(TilePtr)*hashsize); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
132 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
133 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
134 tile = &tileList->tile[tileList->size]; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
135 tile->texture_addr = texture_addr; |
120 | 136 |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
137 smanager->dma_load(tile->pixel, (uint32)texture_addr, |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
138 sizeof(uint32)*64, TEX_LOAD); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
139 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
140 int index = put(tile->texture_addr, tile); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
141 if (index < 0) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
142 printf("[%p] Can't entry\n", tile); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
143 return 0xff0000; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
144 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
145 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
146 tileList->size++; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
147 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
148 smanager->dma_wait(TEX_LOAD); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
149 } |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
150 |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
151 char *p = get_pixel(tx%8, ty%8, tile); |
120 | 152 |
126
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
153 alpha = 255; |
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
154 red = (Uint8) p[0]; |
109 | 155 green = (Uint8) p[1]; |
126
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
156 blue = (Uint8) p[2]; |
109 | 157 |
158 return (red & 0xff) * 0x10000 + (green & 0xff) * 0x100 | |
159 + (blue & 0xff) + (alpha << 24); | |
160 } | |
161 | |
162 int | |
163 DrawSpan::run(void *rbuf, void *wbuf) | |
164 { | |
165 SpanPack *sp = (SpanPack*)smanager->get_input(0); | |
166 SpanPack *next_sp = | |
167 (SpanPack*)smanager->allocate(sizeof(SpanPack)); | |
168 SpanPack *free_sp = next_sp; // next_sp の free() 用 | |
169 SpanPack *tmp_sp = NULL; | |
170 Span *span; | |
171 | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
172 tileList = (TileListPtr)smanager->allocate(sizeof(TileList)); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
173 tileList->init(); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
174 |
109 | 175 int render_y = sp->info.y_top; |
176 void *texture_image = global_get(TEXTURE_ID); | |
177 | |
178 int rangex_start = get_param(0); // このタスクが担当する x の範囲の始点 | |
179 int rangex_end = get_param(1); // 終点 (start <= x <= end) | |
180 int rangey = get_param(2); // y の範囲 (render_y + rangey - 1) | |
181 int rangex = rangex_end - rangex_start + 1; | |
182 | |
183 float *zRow = zRow_init(rangex, rangey); | |
184 | |
185 int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey); | |
186 | |
187 for (int i = 0; i < rangey; i++) { | |
188 linebuf[i] = (int*)smanager->get_output(i); | |
121 | 189 linebuf_init(linebuf[i], rangex, 0); |
109 | 190 } |
191 | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
192 bzero(hash_table, sizeof(TilePtr)*hashsize); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
193 |
109 | 194 do { |
195 /** | |
196 * SpanPack->next が存在する場合、 | |
197 * 現在の SpanPack を処理してる間に | |
198 * 次の SpanPack の DMA 転送を行う | |
199 */ | |
200 if (sp->next != NULL) { | |
201 smanager->dma_load(next_sp, (uint32)sp->next, | |
202 sizeof(SpanPack), SPAN_PACK_LOAD); | |
203 } else { | |
204 next_sp = NULL; | |
205 } | |
206 | |
207 for (int t = 0; t < sp->info.size; t++) { | |
208 span = &sp->span[t]; | |
209 | |
210 int end = span->length_x; | |
211 Uint32 rgb = 0x00ff00; | |
212 float tex1 = span->tex_x1; | |
213 float tex2 = span->tex_x2; | |
214 float tey1 = span->tex_y1; | |
215 float tey2 = span->tex_y2; | |
216 int tex_xpos; | |
217 int tex_ypos; | |
218 int tex_zpos; | |
219 int x = span->x; | |
220 int y = span->y; | |
221 float z = span->start_z; | |
222 float zpos = span->end_z; | |
223 | |
224 // 座標が [0 .. split_screen_w-1] に入るように x,y を -1 | |
225 int localx = getLocalX(x-1); | |
226 int localy = getLocalY(y-1); | |
227 | |
228 if (end == 1) { | |
229 if (x < rangex_start || rangex_end < x) { | |
230 continue; | |
231 } | |
232 | |
233 tex_xpos = (int)((span->tex_height-1) * tex1); | |
234 tex_ypos = (int)((span->tex_width-1) * tey1); | |
235 tex_zpos = (int)z; | |
236 | |
237 if (zpos < zRow[localx + (rangex * localy)]) { | |
238 rgb = get_rgb(tex_xpos, tex_ypos, texture_image); | |
239 zRow[localx + (rangex * localy)] = zpos; | |
240 linebuf[localy][localx] = rgb; | |
241 } | |
242 } else { | |
243 float tex_x, tex_y, tex_z; | |
244 int js = (x < rangex_start) ? rangex_start - x : 0; | |
245 int je = (x + end > rangex_end) ? rangex_end - x : end; | |
246 | |
247 for (int j = js; j <= je; j++) { | |
248 localx = getLocalX(x-1+j); | |
249 | |
250 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); | |
251 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); | |
252 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); | |
253 if (tex_x > 1) tex_x = 1; | |
254 if (tex_y > 1) tex_y = 1; | |
255 tex_xpos = (int)((span->tex_height-1) * tex_x); | |
256 tex_ypos = (int)((span->tex_width-1) * tex_y); | |
257 | |
258 if (tex_z < zRow[localx + (rangex*localy)]) { | |
259 rgb = get_rgb(tex_xpos, tex_ypos, texture_image); | |
260 zRow[localx + (rangex*localy)] = tex_z; | |
261 linebuf[localy][localx] = rgb; | |
262 } | |
263 } | |
264 } | |
265 } | |
266 | |
267 smanager->dma_wait(SPAN_PACK_LOAD); | |
268 | |
269 tmp_sp = sp; | |
270 sp = next_sp; | |
271 next_sp = tmp_sp; | |
272 } while (sp); | |
273 | |
274 free(free_sp); | |
275 free(linebuf); | |
276 free(zRow); | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
277 free(tileList); |
109 | 278 |
279 return 0; | |
280 } |