changeset 167:c8b868871dce

DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
author gongo@localhost.localdomain
date Tue, 09 Dec 2008 15:07:31 +0900
parents e297ecaf2b4d
children cf2aa37d2fe7
files TaskManager/Test/test_render/Tapestry.h TaskManager/Test/test_render/spe/DrawSpan.cpp TaskManager/Test/test_render/spe/DrawSpan.h TaskManager/Test/test_render/spe/Load_Texture.cpp TaskManager/Test/test_render/spe/Load_Texture.h TaskManager/Test/test_render/spe/TileHash.cpp TaskManager/Test/test_render/spe/TileHash.h TaskManager/Test/test_render/task/DrawSpan.cpp TaskManager/Test/test_render/task/DrawSpan.h TaskManager/Test/test_render/task/Load_Texture.cpp TaskManager/Test/test_render/task/Load_Texture.h TaskManager/Test/test_render/task/TileHash.cpp TaskManager/Test/test_render/task/TileHash.h TaskManager/Test/test_render/viewer.cpp
diffstat 14 files changed, 316 insertions(+), 179 deletions(-) [+]
line wrap: on
line diff
--- a/TaskManager/Test/test_render/Tapestry.h	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/Tapestry.h	Tue Dec 09 15:07:31 2008 +0900
@@ -1,6 +1,14 @@
 #ifndef INCLUDED_TAPESTRY
 #define INCLUDED_TAPESTRY
 
+#ifndef INCLUDED_TYPES
+#  include "types.h"
+#endif
+
+#ifndef INCLUDED_VIEWER_TYPES
+#  include "viewer_types.h"
+#endif
+
 /**
  * image file name と tapestry DB の binary tree
  *
@@ -71,4 +79,23 @@
     int tix1, tiy1, tix2, tiy2;
 };
 #endif
+
+typedef struct {
+    uint32 pixel[TEXTURE_BLOCK_SIZE]; // 8*8
+    uint32 *texture_addr;
+    int pad[3];
+} Tile, *TilePtr;
+
+#define MAX_TILE 100
+
+typedef struct {
+    int size;
+    int pad[3];
+    Tile tile[MAX_TILE];
+
+    void init(void) {
+	size = 0;
+    }
+} TileList, *TileListPtr;
+
 #endif
--- a/TaskManager/Test/test_render/spe/DrawSpan.cpp	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/spe/DrawSpan.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -5,20 +5,14 @@
 #include "SpanPack.h"
 #include "texture.h"
 #include "viewer_types.h"
+#include "TileHash.h"
 
 #define SPAN_PACK_LOAD 0
 #define TEX_LOAD 1
 
 SchedDefineTask(DrawSpan);
 
-static const int hashsize = 263;
-
-static TilePtr hash_table[hashsize] = {0};
-
-unsigned short PRIME[8] = {
-    0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
-};
-
+static TileHashPtr hash;
 static TileListPtr tileList;
 
 /**
@@ -79,57 +73,6 @@
     return tex_addr_top + block*TEXTURE_BLOCK_SIZE;
 }
 
-
-static int
-hash(uint32 data)
-{
-    int value = 0;
-    int n = 0;
-    int key;
-
-    for (int i = 0; i < 8; i ++) {
-	key = data & 0xf;
-	value += key * PRIME[n++];
-	data >>= 4;
-    }
-
-    return value % hashsize;
-}
-
-static int
-put(void *key, TilePtr data)
-{
-    int hashval = hash((uint32)key);
-
-    for (int i = 0; i < hashsize/2; i++) {
-	int index = (hashval + i*i)%hashsize;
-
-	if (hash_table[index] == 0) {
-	    hash_table[index] = data;
-	    return index;
-	}
-    }
-
-    return -1;
-}
-
-static TilePtr
-get(void *key)
-{
-    int hashval = hash((uint32)key);
-
-    for (int i = 0; i < hashsize/2; i++) {
-	int index = (hashval + i*i)%hashsize;
-	
-	if (hash_table[index] != NULL &&
-	    hash_table[index]->texture_addr == key) {
-	    return hash_table[index];
-	}
-    }
-
-    return NULL;
-}
-
 void
 DrawSpan::linebuf_init(int *buf, int x, int rgb)
 {
@@ -170,11 +113,11 @@
     /**
      * get,put ϥ֥(HashȤ)äƥ뤫
      */
-    tile = get(addr);
+    tile = hash->get(addr);
     if (tile == NULL) {
 	if (tileList->size >= MAX_TILE) {
 	    tileList->init();
-	    bzero(hash_table, sizeof(TilePtr)*hashsize);
+	    hash->clear();
 	}
 
 	tile = &tileList->tile[tileList->size];
@@ -183,7 +126,7 @@
 	smanager->dma_load(tile->pixel, (uint32)addr,
 			   sizeof(uint32)*TEXTURE_BLOCK_SIZE, TEX_LOAD);
 
-	int index = put(tile->texture_addr, tile);
+	int index = hash->put(tile->texture_addr, tile);
 
 	/**
 	 * TODO:
@@ -223,8 +166,8 @@
     SpanPack *tmp_sp = NULL;
     Span *span;
 
-    tileList = (TileListPtr)smanager->allocate(sizeof(TileList));
-    tileList->init();
+    hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH);
+    tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST);
 
     int rangex_start  = get_param(0); // Υô x ϰϤλ
     int rangex_end    = get_param(1); //  (start <= x <= end)
@@ -232,7 +175,6 @@
     int rangex        = rangex_end - rangex_start + 1;
 
     float *zRow = zRow_init(rangex, rangey);
-
     int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);
 
     for (int i = 0; i < rangey; i++) {
@@ -240,8 +182,6 @@
 	linebuf_init(linebuf[i], rangex, 0xffffff);
     }
 
-    bzero(hash_table, sizeof(TilePtr)*hashsize);
-
     do {
 	/**
 	 * SpanPack->next ¸ߤ硢
@@ -355,7 +295,6 @@
     free(free_sp);
     free(linebuf);
     free(zRow);
-    free(tileList);
 
     return 0;
 }
--- a/TaskManager/Test/test_render/spe/DrawSpan.h	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/spe/DrawSpan.h	Tue Dec 09 15:07:31 2008 +0900
@@ -5,6 +5,10 @@
 #  include "SchedTask.h"
 #endif
 
+#ifndef INCLUDED_TAPESTRY
+#  include "Tapestry.h"
+#endif
+
 typedef int8_t          Sint8;
 typedef uint8_t         Uint8;
 typedef int16_t         Sint16;
@@ -13,24 +17,6 @@
 typedef uint32_t        Uint32;
 typedef unsigned short  GLushort;
 
-typedef struct {
-    uint32 pixel[64]; // 8*8
-    void *texture_addr;
-    int pad[3];
-} Tile, *TilePtr;
-
-#define MAX_TILE 100
-
-typedef struct {
-    int size;
-    int pad[3];
-    Tile tile[MAX_TILE];
-
-    void init(void) {
-	size = 0;
-    }
-} TileList, *TileListPtr;
-
 class DrawSpan : public SchedTask {
 public:
     SchedConstructor(DrawSpan);
--- a/TaskManager/Test/test_render/spe/Load_Texture.cpp	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/spe/Load_Texture.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -2,12 +2,30 @@
 #include <string.h>
 #include "Load_Texture.h"
 #include "texture.h"
+#include "TileHash.h"
 #include "Func.h"
 
 SchedDefineTask(LoadTexture);
 
+/**
+ * 「Load」といいながら、結局 DrawSpan で使う
+ * Hash の準備だけなので、名前変えないとなー
+ */
 int 
 LoadTexture::run(void *rbuf , void *wbuf) 
 {
+    /**
+     * 現在 global_alloc() では new をサポートしてないので
+     * コンストラクタ呼ぶために placement new してます。
+     */
+    void *hash_tmp = smanager->global_alloc(GLOBAL_TEXTURE_HASH,
+					    sizeof(TileHash));
+    TileHashPtr hashtable = new(hash_tmp) TileHash;
+
+    TileListPtr tileList
+	= (TileListPtr)smanager->global_alloc(GLOBAL_TILE_LIST,
+					      sizeof(TileList));
+    tileList->init();
+
     return 0;
 }
--- a/TaskManager/Test/test_render/spe/Load_Texture.h	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/spe/Load_Texture.h	Tue Dec 09 15:07:31 2008 +0900
@@ -5,6 +5,10 @@
 #include  "SchedTask.h"
 #endif
 
+#ifndef INCLUDED_TAPESTRY
+#  include "Tapestry.h"
+#endif
+
 class LoadTexture : public SchedTask {
 public:
     SchedConstructor(LoadTexture);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/spe/TileHash.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -0,0 +1,72 @@
+#include <string.h>
+#include <stdlib.h>
+#include "TileHash.h"
+
+static unsigned short PRIME[8] = {
+    0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
+};
+
+int
+TileHash::hash(uint32 data)
+{
+    int value = 0;
+    int n = 0;
+    int key;
+
+    for (int i = 0; i < 8; i ++) {
+	key = data & 0xf;
+	value += key * PRIME[n++];
+	data >>= 4;
+    }
+
+    return value % hashSize;
+}
+
+TileHash::TileHash(void)
+{
+    hashSize = 263;
+    tableSize = sizeof(TilePtr)*hashSize;
+
+    table = (TilePtr*)malloc(tableSize);
+    clear();
+}
+
+int
+TileHash::put(uint32 *key, TilePtr data)
+{
+    int hashval = hash((uint32)key);
+
+    for (int i = 0; i < hashSize/2; i++) {
+	int index = (hashval + i*i)%hashSize;
+
+	if (table[index] == 0) {
+	    table[index] = data;
+	    return index;
+	}
+    }
+
+    return -1;
+}
+
+TilePtr
+TileHash::get(uint32 *key)
+{
+    int hashval = hash((uint32)key);
+
+    for (int i = 0; i < hashSize/2; i++) {
+	int index = (hashval + i*i)%hashSize;
+	
+	if (table[index] != NULL &&
+	    table[index]->texture_addr == key) {
+	    return table[index];
+	}
+    }
+
+    return NULL;
+}
+
+void
+TileHash::clear(void)
+{
+    bzero(table, tableSize);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/spe/TileHash.h	Tue Dec 09 15:07:31 2008 +0900
@@ -0,0 +1,35 @@
+#ifndef INCLUDED_TEXTURE_HASH
+#define INCLUDED_TEXTURE_HASH
+
+#ifndef INCLUDED_TAPESTRY
+#  include "Tapestry.h"
+#endif
+
+class TileHash {
+public:
+    TileHash(void);
+
+private:
+    TilePtr *table;
+    int hashSize;
+    int tableSize;
+
+public:
+    void clear(void);
+    int hash(uint32 data);
+    int put(uint32 *addr, TilePtr tile);
+    TilePtr get(uint32 *addr);
+
+    /**
+     * 未実装
+     * 
+     * void remove(uint32 *addr);
+     */
+};
+
+typedef TileHash* TileHashPtr;
+
+#endif
+
+const int GLOBAL_TEXTURE_HASH = 0;
+const int GLOBAL_TILE_LIST    = 1;
--- a/TaskManager/Test/test_render/task/DrawSpan.cpp	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/task/DrawSpan.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -5,20 +5,14 @@
 #include "SpanPack.h"
 #include "texture.h"
 #include "viewer_types.h"
+#include "TileHash.h"
 
 #define SPAN_PACK_LOAD 0
 #define TEX_LOAD 1
 
 SchedDefineTask(DrawSpan);
 
-static const int hashsize = 263;
-
-static TilePtr hash_table[hashsize] = {0};
-
-unsigned short PRIME[8] = {
-    0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
-};
-
+static TileHashPtr hash;
 static TileListPtr tileList;
 
 /**
@@ -79,57 +73,6 @@
     return tex_addr_top + block*TEXTURE_BLOCK_SIZE;
 }
 
-
-static int
-hash(uint32 data)
-{
-    int value = 0;
-    int n = 0;
-    int key;
-
-    for (int i = 0; i < 8; i ++) {
-	key = data & 0xf;
-	value += key * PRIME[n++];
-	data >>= 4;
-    }
-
-    return value % hashsize;
-}
-
-static int
-put(void *key, TilePtr data)
-{
-    int hashval = hash((uint32)key);
-
-    for (int i = 0; i < hashsize/2; i++) {
-	int index = (hashval + i*i)%hashsize;
-
-	if (hash_table[index] == 0) {
-	    hash_table[index] = data;
-	    return index;
-	}
-    }
-
-    return -1;
-}
-
-static TilePtr
-get(void *key)
-{
-    int hashval = hash((uint32)key);
-
-    for (int i = 0; i < hashsize/2; i++) {
-	int index = (hashval + i*i)%hashsize;
-	
-	if (hash_table[index] != NULL &&
-	    hash_table[index]->texture_addr == key) {
-	    return hash_table[index];
-	}
-    }
-
-    return NULL;
-}
-
 void
 DrawSpan::linebuf_init(int *buf, int x, int rgb)
 {
@@ -170,11 +113,11 @@
     /**
      * get,put ϥ֥(HashȤ)äƥ뤫
      */
-    tile = get(addr);
+    tile = hash->get(addr);
     if (tile == NULL) {
 	if (tileList->size >= MAX_TILE) {
 	    tileList->init();
-	    bzero(hash_table, sizeof(TilePtr)*hashsize);
+	    hash->clear();
 	}
 
 	tile = &tileList->tile[tileList->size];
@@ -183,7 +126,7 @@
 	smanager->dma_load(tile->pixel, (uint32)addr,
 			   sizeof(uint32)*TEXTURE_BLOCK_SIZE, TEX_LOAD);
 
-	int index = put(tile->texture_addr, tile);
+	int index = hash->put(tile->texture_addr, tile);
 
 	/**
 	 * TODO:
@@ -223,8 +166,8 @@
     SpanPack *tmp_sp = NULL;
     Span *span;
 
-    tileList = (TileListPtr)smanager->allocate(sizeof(TileList));
-    tileList->init();
+    hash = (TileHashPtr)smanager->global_get(GLOBAL_TEXTURE_HASH);
+    tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST);
 
     int rangex_start  = get_param(0); // Υô x ϰϤλ
     int rangex_end    = get_param(1); //  (start <= x <= end)
@@ -232,7 +175,6 @@
     int rangex        = rangex_end - rangex_start + 1;
 
     float *zRow = zRow_init(rangex, rangey);
-
     int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);
 
     for (int i = 0; i < rangey; i++) {
@@ -240,8 +182,6 @@
 	linebuf_init(linebuf[i], rangex, 0xffffff);
     }
 
-    bzero(hash_table, sizeof(TilePtr)*hashsize);
-
     do {
 	/**
 	 * SpanPack->next ¸ߤ硢
@@ -355,7 +295,6 @@
     free(free_sp);
     free(linebuf);
     free(zRow);
-    free(tileList);
 
     return 0;
 }
--- a/TaskManager/Test/test_render/task/DrawSpan.h	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/task/DrawSpan.h	Tue Dec 09 15:07:31 2008 +0900
@@ -5,6 +5,10 @@
 #  include "SchedTask.h"
 #endif
 
+#ifndef INCLUDED_TAPESTRY
+#  include "Tapestry.h"
+#endif
+
 typedef int8_t          Sint8;
 typedef uint8_t         Uint8;
 typedef int16_t         Sint16;
@@ -13,24 +17,6 @@
 typedef uint32_t        Uint32;
 typedef unsigned short  GLushort;
 
-typedef struct {
-    uint32 pixel[64]; // 8*8
-    void *texture_addr;
-    int pad[3];
-} Tile, *TilePtr;
-
-#define MAX_TILE 100
-
-typedef struct {
-    int size;
-    int pad[3];
-    Tile tile[MAX_TILE];
-
-    void init(void) {
-	size = 0;
-    }
-} TileList, *TileListPtr;
-
 class DrawSpan : public SchedTask {
 public:
     SchedConstructor(DrawSpan);
--- a/TaskManager/Test/test_render/task/Load_Texture.cpp	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/task/Load_Texture.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -2,12 +2,30 @@
 #include <string.h>
 #include "Load_Texture.h"
 #include "texture.h"
+#include "TileHash.h"
 #include "Func.h"
 
 SchedDefineTask(LoadTexture);
 
+/**
+ * 「Load」といいながら、結局 DrawSpan で使う
+ * Hash の準備だけなので、名前変えないとなー
+ */
 int 
 LoadTexture::run(void *rbuf , void *wbuf) 
 {
+    /**
+     * 現在 global_alloc() では new をサポートしてないので
+     * コンストラクタ呼ぶために placement new してます。
+     */
+    void *hash_tmp = smanager->global_alloc(GLOBAL_TEXTURE_HASH,
+					    sizeof(TileHash));
+    TileHashPtr hashtable = new(hash_tmp) TileHash;
+
+    TileListPtr tileList
+	= (TileListPtr)smanager->global_alloc(GLOBAL_TILE_LIST,
+					      sizeof(TileList));
+    tileList->init();
+
     return 0;
 }
--- a/TaskManager/Test/test_render/task/Load_Texture.h	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/task/Load_Texture.h	Tue Dec 09 15:07:31 2008 +0900
@@ -5,6 +5,10 @@
 #include  "SchedTask.h"
 #endif
 
+#ifndef INCLUDED_TAPESTRY
+#  include "Tapestry.h"
+#endif
+
 class LoadTexture : public SchedTask {
 public:
     SchedConstructor(LoadTexture);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/task/TileHash.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -0,0 +1,72 @@
+#include <string.h>
+#include <stdlib.h>
+#include "TileHash.h"
+
+static unsigned short PRIME[8] = {
+    0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
+};
+
+int
+TileHash::hash(uint32 data)
+{
+    int value = 0;
+    int n = 0;
+    int key;
+
+    for (int i = 0; i < 8; i ++) {
+	key = data & 0xf;
+	value += key * PRIME[n++];
+	data >>= 4;
+    }
+
+    return value % hashSize;
+}
+
+TileHash::TileHash(void)
+{
+    hashSize = 263;
+    tableSize = sizeof(TilePtr)*hashSize;
+
+    table = (TilePtr*)malloc(tableSize);
+    clear();
+}
+
+int
+TileHash::put(uint32 *key, TilePtr data)
+{
+    int hashval = hash((uint32)key);
+
+    for (int i = 0; i < hashSize/2; i++) {
+	int index = (hashval + i*i)%hashSize;
+
+	if (table[index] == 0) {
+	    table[index] = data;
+	    return index;
+	}
+    }
+
+    return -1;
+}
+
+TilePtr
+TileHash::get(uint32 *key)
+{
+    int hashval = hash((uint32)key);
+
+    for (int i = 0; i < hashSize/2; i++) {
+	int index = (hashval + i*i)%hashSize;
+	
+	if (table[index] != NULL &&
+	    table[index]->texture_addr == key) {
+	    return table[index];
+	}
+    }
+
+    return NULL;
+}
+
+void
+TileHash::clear(void)
+{
+    bzero(table, tableSize);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/task/TileHash.h	Tue Dec 09 15:07:31 2008 +0900
@@ -0,0 +1,35 @@
+#ifndef INCLUDED_TEXTURE_HASH
+#define INCLUDED_TEXTURE_HASH
+
+#ifndef INCLUDED_TAPESTRY
+#  include "Tapestry.h"
+#endif
+
+class TileHash {
+public:
+    TileHash(void);
+
+private:
+    TilePtr *table;
+    int hashSize;
+    int tableSize;
+
+public:
+    void clear(void);
+    int hash(uint32 data);
+    int put(uint32 *addr, TilePtr tile);
+    TilePtr get(uint32 *addr);
+
+    /**
+     * 未実装
+     * 
+     * void remove(uint32 *addr);
+     */
+};
+
+typedef TileHash* TileHashPtr;
+
+#endif
+
+const int GLOBAL_TEXTURE_HASH = 0;
+const int GLOBAL_TILE_LIST    = 1;
--- a/TaskManager/Test/test_render/viewer.cpp	Tue Dec 09 11:53:51 2008 +0900
+++ b/TaskManager/Test/test_render/viewer.cpp	Tue Dec 09 15:07:31 2008 +0900
@@ -29,9 +29,6 @@
 int spackList_length;
 int spackList_length_align;
 
-void *__texture;
-
-
 /**
  * Joystick があればそれを使い、
  * 無ければキーボードを返す
@@ -112,6 +109,7 @@
 Viewer::run_init(char *xml, int sg_number)
 {
     HTaskPtr task_next;
+    HTaskPtr task_tex;
 
     start_time = get_ticks();
     this_time  = 0;
@@ -162,7 +160,8 @@
     spackList_length_align = (spackList_length + 3)&(~3);
 
     /* 各 SPU が持つ、SpanPack の address list */
-    spackList_ptr = (SpanPack**)manager->malloc(sizeof(SpanPack*)*spackList_length_align);
+    spackList_ptr =
+	(SpanPack**)manager->malloc(sizeof(SpanPack*)*spackList_length_align);
 
     for (int i = 0; i < spackList_length; i++) {
 	spackList_ptr[i] = &spackList[i];
@@ -190,8 +189,12 @@
     int tex_height = scene_graph->texture_image->h;
     int tex_blocksize = tex_width*tex_height*4;
 
-    __texture = (void*)manager->malloc(tex_blocksize);
-    memcpy(__texture, scene_graph->texture_image->pixels, tex_blocksize);
+    for (int i = 0; i < spe_num; i++) {
+	task_tex = manager->create_task(TASK_INIT_TEXTURE);
+	task_tex->set_cpu((CPU_TYPE)((int)SPE_0 + i));
+	task_next->wait_for(task_tex);
+	task_tex->spawn();
+    }
 
     task_next->spawn();
 }
@@ -359,6 +362,5 @@
     scene_graph->controller->close();
     delete scene_graph;
 
-    free(__texture);
     quit();
 }