comparison 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
comparison
equal deleted inserted replaced
127:9ff71404cff2 128:776eca0daa02
9 #define SPAN_PACK_LOAD 0 9 #define SPAN_PACK_LOAD 0
10 #define TEX_LOAD 1 10 #define TEX_LOAD 1
11 11
12 SchedDefineTask(DrawSpan); 12 SchedDefineTask(DrawSpan);
13 13
14 unsigned char *tex; 14 static const int hashsize = 263;
15
16 static TilePtr hash_table[hashsize] = {NULL};
17
18 unsigned short PRIME[8] = {
19 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
20 };
21
22 static TileListPtr tileList;
23
24 static int
25 hash(uint32 data)
26 {
27 int value = 0;
28 int n = 0;
29 int key;
30
31 for (int i = 0; i < 8; i ++) {
32 key = data & 0xf;
33 value += key * PRIME[n++];
34 data >>= 4;
35 }
36
37 return value % hashsize;
38 }
39
40 static int
41 put(void *key, TilePtr data)
42 {
43 int hashval = hash((uint32)key);
44
45 for (int i = 0; i < hashsize/2; i++) {
46 int index = (hashval + i*i)%hashsize;
47
48 if (hash_table[index] == 0) {
49 hash_table[index] = data;
50 return index;
51 }
52 }
53
54 return -1;
55 }
56
57 static TilePtr
58 get(void *key)
59 {
60 int hashval = hash((uint32)key);
61
62 for (int i = 0; i < hashsize/2; i++) {
63 int index = (hashval + i*i)%hashsize;
64
65 if (hash_table[index]->texture_addr == key) {
66 return hash_table[index];
67 }
68 }
69
70 return NULL;
71 }
15 72
16 void 73 void
17 DrawSpan::linebuf_init(int *buf, int x, int rgb) 74 DrawSpan::linebuf_init(int *buf, int x, int rgb)
18 { 75 {
19 for (int i = 0; i < x; i++) { 76 for (int i = 0; i < x; i++) {
52 DrawSpan::get_rgb(int tx, int ty, void *texture) 109 DrawSpan::get_rgb(int tx, int ty, void *texture)
53 { 110 {
54 Uint8 red, green, blue, alpha; 111 Uint8 red, green, blue, alpha;
55 112
56 if (tx<0) tx = 0; 113 if (tx<0) tx = 0;
57 if (128-1< tx) tx = 128-1 ; 114 if (tex_width-1< tx) tx = tex_width-1 ;
58 if (ty<0) ty = 0; 115 if (ty<0) ty = 0;
59 if (128-1< ty) ty = 128-1 ; 116 if (tex_height-1< ty) ty = tex_height-1 ;
60 117
61 #if 0
62 char *p = get_pixel(tx,ty,texture);
63 #else
64 void *texture_addr; 118 void *texture_addr;
65 119
66 int blockX = tx / 8; 120 int blockX = tx / 8;
67 int blockY = ty / 8; 121 int blockY = ty / 8;
68 void** addrList = (void**)global_get(TEXTURE2_ID); 122 void** addrList = (void**)global_get(TEXTURE2_ID);
69 123 TilePtr tile;
70 texture_addr = addrList[blockX + 16*blockY]; 124
71 smanager->dma_load(tex, (uint32)texture_addr, sizeof(uint32)*64, TEX_LOAD); 125 texture_addr = addrList[blockX + (tex_width/8)*blockY];
72 smanager->dma_wait(TEX_LOAD); 126
73 127 tile = get(texture_addr);
74 char *p = get_pixel(tx%8, ty%8, tex); 128 if (tile == NULL) {
75 #endif 129 if (tileList->size >= MAX_TILE) {
130 tileList->init();
131 bzero(hash_table, sizeof(TilePtr)*hashsize);
132 }
133
134 tile = &tileList->tile[tileList->size];
135 tile->texture_addr = texture_addr;
136
137 smanager->dma_load(tile->pixel, (uint32)texture_addr,
138 sizeof(uint32)*64, TEX_LOAD);
139
140 int index = put(tile->texture_addr, tile);
141 if (index < 0) {
142 printf("[%p] Can't entry\n", tile);
143 return 0xff0000;
144 }
145
146 tileList->size++;
147
148 smanager->dma_wait(TEX_LOAD);
149 }
150
151 char *p = get_pixel(tx%8, ty%8, tile);
76 152
77 alpha = 255; 153 alpha = 255;
78 red = (Uint8) p[0]; 154 red = (Uint8) p[0];
79 green = (Uint8) p[1]; 155 green = (Uint8) p[1];
80 blue = (Uint8) p[2]; 156 blue = (Uint8) p[2];
91 (SpanPack*)smanager->allocate(sizeof(SpanPack)); 167 (SpanPack*)smanager->allocate(sizeof(SpanPack));
92 SpanPack *free_sp = next_sp; // next_sp の free() 用 168 SpanPack *free_sp = next_sp; // next_sp の free() 用
93 SpanPack *tmp_sp = NULL; 169 SpanPack *tmp_sp = NULL;
94 Span *span; 170 Span *span;
95 171
172 tileList = (TileListPtr)smanager->allocate(sizeof(TileList));
173 tileList->init();
174
96 int render_y = sp->info.y_top; 175 int render_y = sp->info.y_top;
97 void *texture_image = global_get(TEXTURE_ID); 176 void *texture_image = global_get(TEXTURE_ID);
98 177
99 int rangex_start = get_param(0); // このタスクが担当する x の範囲の始点 178 int rangex_start = get_param(0); // このタスクが担当する x の範囲の始点
100 int rangex_end = get_param(1); // 終点 (start <= x <= end) 179 int rangex_end = get_param(1); // 終点 (start <= x <= end)
103 182
104 float *zRow = zRow_init(rangex, rangey); 183 float *zRow = zRow_init(rangex, rangey);
105 184
106 int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey); 185 int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);
107 186
108 tex = (unsigned char*)smanager->allocate(sizeof(unsigned char)*64*4);
109
110 for (int i = 0; i < rangey; i++) { 187 for (int i = 0; i < rangey; i++) {
111 linebuf[i] = (int*)smanager->get_output(i); 188 linebuf[i] = (int*)smanager->get_output(i);
112 //linebuf_init(linebuf[i], rangex, 0x00ff00ff);
113 linebuf_init(linebuf[i], rangex, 0); 189 linebuf_init(linebuf[i], rangex, 0);
114 } 190 }
191
192 bzero(hash_table, sizeof(TilePtr)*hashsize);
115 193
116 do { 194 do {
117 /** 195 /**
118 * SpanPack->next が存在する場合、 196 * SpanPack->next が存在する場合、
119 * 現在の SpanPack を処理してる間に 197 * 現在の SpanPack を処理してる間に
194 } while (sp); 272 } while (sp);
195 273
196 free(free_sp); 274 free(free_sp);
197 free(linebuf); 275 free(linebuf);
198 free(zRow); 276 free(zRow);
199 free(tex); 277 free(tileList);
200 278
201 return 0; 279 return 0;
202 } 280 }