-EntityTemplate.getComponent additional data check

-LinearLayout proggress:
 *added alignment to data
 *fixed add/remove component functions
This commit is contained in:
Mergul 2018-09-26 12:50:51 +02:00
parent c18ac54265
commit c915b1a8c7
3 changed files with 63 additions and 38 deletions

View file

@ -45,6 +45,7 @@ export struct EntityTemplate
T* getComponent(T)() T* getComponent(T)()
{ {
if(T.component_id >= info.tmpl_deltas.length)return null;
version(LinearLayout)return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]); version(LinearLayout)return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]);
else return cast(T*)(entity_data.ptr + info.deltas[T.component_id]); else return cast(T*)(entity_data.ptr + info.deltas[T.component_id]);
} }

View file

@ -83,7 +83,7 @@ class EntityManager
i++; i++;
if (isPointer!param) if (isPointer!param)
{ {
ret ~= "if(opt_ptr" ~ opt.to!string ~ " !is null)opt_ptr" ret ~= "if(opt_array" ~ opt.to!string ~ " !is null)opt_ptr"
~ opt.to!string ~ " = &opt_array" ~ opt.to!string ~ "[i];"; ~ opt.to!string ~ " = &opt_array" ~ opt.to!string ~ "[i];";
opt++; opt++;
} }
@ -151,6 +151,10 @@ class EntityManager
{ {
ret ~= "PointerTarget!(types[" ~ i.to!string ret ~= "PointerTarget!(types[" ~ i.to!string
~ "])[] opt_array" ~ opt.to!string ~ ";"; ~ "])[] opt_array" ~ opt.to!string ~ ";";
ret ~= "if(info.deltas[types[" ~ i.to!string ~ "].component_id] != ushort.max)opt_array"
~ opt.to!string ~ "
= (cast(types[" ~ i.to!string ~ "])(cast(void*)block + info.deltas[types["
~ i.to!string ~ "].component_id]))[0..block.entities_count];";
ret ~= "types[" ~ i.to!string ~ "] opt_ptr" ~ opt.to!string ~ ";"; ret ~= "types[" ~ i.to!string ~ "] opt_ptr" ~ opt.to!string ~ ";";
opt++; opt++;
} }
@ -452,6 +456,11 @@ class EntityManager
num = cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); //num += alignment - (num & (alignment - 1)); num = cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); //num += alignment - (num & (alignment - 1));
} }
static ushort alignedNum(ushort num, ushort alignment)
{
return cast(ushort)((num + alignment - 1) & (-cast(int) alignment));
}
extern (C) static int compareUShorts(const void* a, const void* b) extern (C) static int compareUShorts(const void* a, const void* b)
{ {
ushort _a = *cast(ushort*) a; ushort _a = *cast(ushort*) a;
@ -523,52 +532,60 @@ class EntityManager
info.components = Mallocator.instance.makeArray(ids); info.components = Mallocator.instance.makeArray(ids);
info.deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1); info.deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1);
version (LinearLayout)
{
info.tmpl_deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1);
uint block_memory = page_size - EntitiesBlock.sizeof;
uint entity_comps_size = EntityID.sizeof;
uint mem_begin = EntitiesBlock.sizeof;
foreach (id; ids)
{
entity_comps_size += components[id].size;
}
uint entites_in_block = block_memory / entity_comps_size;
info.max_entities = cast(ushort) entites_in_block;
uint current_delta = cast(uint)(mem_begin + entites_in_block * EntityID.sizeof);
}
info.size = EntityID.sizeof; info.size = EntityID.sizeof;
info.alignment = EntityID.alignof; info.alignment = EntityID.alignof;
foreach (i, id; ids) version (LinearLayout)
{ {
info.alignment = max(info.alignment, components[id].alignment); info.tmpl_deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1);
alignNum(info.size, components[id].alignment); uint components_size = EntityID.sizeof;
version (LinearLayout) foreach (i, id; ids)
{ {
info.alignment = max(info.alignment, components[id].alignment);
alignNum(info.size, components[id].alignment);
info.tmpl_deltas[id] = info.size; info.tmpl_deltas[id] = info.size;
info.size += components[id].size;
components_size += components[id].size;
}
alignNum(info.size, info.alignment);
/**/
uint block_memory = cast(uint)(
page_size - EntitiesBlock.sizeof - (info.size - components_size));
//uint entity_comps_size = EntityID.sizeof;
uint mem_begin = EntitiesBlock.sizeof;
/*foreach (id; ids)
{
entity_comps_size += components[id].size;
}*/
uint entites_in_block = block_memory / info.size; //entity_comps_size;
info.max_entities = cast(ushort) entites_in_block;
ushort current_delta = cast(ushort)(mem_begin + entites_in_block * EntityID.sizeof);
foreach (i, id; ids)
{
alignNum(current_delta, components[id].alignment);
info.deltas[id] = cast(ushort) current_delta; info.deltas[id] = cast(ushort) current_delta;
current_delta += entites_in_block * components[id].size; current_delta += entites_in_block * components[id].size;
} }
else
{
info.deltas[id] = info.size;
}
info.size += components[id].size;
}
alignNum(info.size, info.alignment);
version (LinearLayout)
{
} }
else else
{
foreach (i, id; ids)
{
info.alignment = max(info.alignment, components[id].alignment);
alignNum(info.size, components[id].alignment);
info.deltas[id] = info.size;
info.size += components[id].size;
}
alignNum(info.size, info.alignment);
info.max_entities = (page_size - EntitiesBlock.sizeof) / info.size; info.max_entities = (page_size - EntitiesBlock.sizeof) / info.size;
}
foreach (uint i, ref system; systems) foreach (uint i, ref system; systems)
{ {
@ -707,7 +724,10 @@ class EntityManager
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info); EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size; version (LinearLayout)
void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof;
else
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size;
Entity* new_entity = cast(Entity*) start; Entity* new_entity = cast(Entity*) start;
new_entity.id = entity.id; new_entity.id = entity.id;
@ -715,6 +735,7 @@ class EntityManager
version (LinearLayout) version (LinearLayout)
{ {
static if (EntityID.sizeof == 8) static if (EntityID.sizeof == 8)
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else else
@ -831,14 +852,17 @@ class EntityManager
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info); EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size; //removeEntityNoID(entity, block);
version (LinearLayout)
void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof;
else
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size;
Entity* new_entity = cast(Entity*) start; Entity* new_entity = cast(Entity*) start;
new_entity.id = entity.id; new_entity.id = entity.id;
new_entity.updateID(); new_entity.updateID();
//removeEntityNoID(entity, block);
j = 0; j = 0;
k = 0; k = 0;
version (LinearLayout) version (LinearLayout)

View file

@ -194,7 +194,7 @@ public:
} }
export ref T opIndex(size_t elemNum) { export ref T opIndex(size_t elemNum) {
assert(elemNum < used, "Range violation [index]"); debug assert(elemNum < used, "Range violation [index]");
return array.ptr[elemNum]; return array.ptr[elemNum];
} }