fix extracting components and nodes functions

This commit is contained in:
2019-11-17 07:16:19 +07:00
parent 9f1737c2b5
commit a78cdaf72a
6 changed files with 145 additions and 62 deletions

View File

@@ -100,63 +100,107 @@ namespace e2d::nodes
void for_extracted_nodes(const node_iptr& root, F&& f) { void for_extracted_nodes(const node_iptr& root, F&& f) {
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector<node_iptr> nodes; static thread_local vector<node_iptr> nodes;
const std::size_t begin_index = nodes.size();
try { try {
extract_nodes(root, std::back_inserter(nodes)); extract_nodes(
for ( const node_iptr& n : nodes ) { root,
f(n); std::back_inserter(nodes));
const std::size_t end_index = nodes.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
f(nodes[i]);
} }
} catch (...) { } catch (...) {
nodes.clear(); nodes.erase(
nodes.begin() + begin_index,
nodes.end());
throw; throw;
} }
nodes.clear();
nodes.erase(
nodes.begin() + begin_index,
nodes.end());
} }
template < typename F > template < typename F >
void for_extracted_nodes(const const_node_iptr& root, F&& f) { void for_extracted_nodes(const const_node_iptr& root, F&& f) {
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector<const_node_iptr> nodes; static thread_local vector<const_node_iptr> nodes;
const std::size_t begin_index = nodes.size();
try { try {
extract_nodes(root, std::back_inserter(nodes)); extract_nodes(
for ( const const_node_iptr& n : nodes ) { root,
f(n); std::back_inserter(nodes));
const std::size_t end_index = nodes.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
f(nodes[i]);
} }
} catch (...) { } catch (...) {
nodes.clear(); nodes.erase(
nodes.begin() + begin_index,
nodes.end());
throw; throw;
} }
nodes.clear();
nodes.erase(
nodes.begin() + begin_index,
nodes.end());
} }
template < typename F > template < typename F >
void for_extracted_nodes_reversed(const node_iptr& root, F&& f) { void for_extracted_nodes_reversed(const node_iptr& root, F&& f) {
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector<node_iptr> nodes; static thread_local vector<node_iptr> nodes;
const std::size_t begin_index = nodes.size();
try { try {
extract_nodes_reversed(root, std::back_inserter(nodes)); extract_nodes_reversed(
for ( const node_iptr& n : nodes ) { root,
f(n); std::back_inserter(nodes));
const std::size_t end_index = nodes.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
f(nodes[i]);
} }
} catch (...) { } catch (...) {
nodes.clear(); nodes.erase(
nodes.begin() + begin_index,
nodes.end());
throw; throw;
} }
nodes.clear();
nodes.erase(
nodes.begin() + begin_index,
nodes.end());
} }
template < typename F > template < typename F >
void for_extracted_nodes_reversed(const const_node_iptr& root, F&& f) { void for_extracted_nodes_reversed(const const_node_iptr& root, F&& f) {
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector<const_node_iptr> nodes; static thread_local vector<const_node_iptr> nodes;
const std::size_t begin_index = nodes.size();
try { try {
extract_nodes_reversed(root, std::back_inserter(nodes)); extract_nodes_reversed(
for ( const const_node_iptr& n : nodes ) { root,
f(n); std::back_inserter(nodes));
const std::size_t end_index = nodes.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
f(nodes[i]);
} }
} catch (...) { } catch (...) {
nodes.clear(); nodes.erase(
nodes.begin() + begin_index,
nodes.end());
throw; throw;
} }
nodes.clear();
nodes.erase(
nodes.begin() + begin_index,
nodes.end());
} }
} }

View File

@@ -70,19 +70,28 @@ namespace e2d::systems
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector< static thread_local vector<
std::tuple<ecs::entity, Ts...>> components; std::tuple<ecs::entity, Ts...>> components;
const std::size_t begin_index = components.size();
try { try {
extract_components<Ts...>( extract_components<Ts...>(
owner, owner,
std::back_inserter(components), std::back_inserter(components),
std::forward<Opts>(opts)...); std::forward<Opts>(opts)...);
for ( auto& t : components ) {
std::apply(f, t); const std::size_t end_index = components.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
std::apply(f, components[i]);
} }
} catch (...) { } catch (...) {
components.clear(); components.erase(
components.begin() + begin_index,
components.end());
throw; throw;
} }
components.clear();
components.erase(
components.begin() + begin_index,
components.end());
} }
template < typename... Ts, typename F, typename... Opts > template < typename... Ts, typename F, typename... Opts >
@@ -90,19 +99,28 @@ namespace e2d::systems
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector< static thread_local vector<
std::tuple<ecs::const_entity, Ts...>> components; std::tuple<ecs::const_entity, Ts...>> components;
const std::size_t begin_index = components.size();
try { try {
extract_components<Ts...>( extract_components<Ts...>(
owner, owner,
std::back_inserter(components), std::back_inserter(components),
std::forward<Opts>(opts)...); std::forward<Opts>(opts)...);
for ( const auto& t : components ) {
std::apply(f, t); const std::size_t end_index = components.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
std::apply(f, components[i]);
} }
} catch (...) { } catch (...) {
components.clear(); components.erase(
components.begin() + begin_index,
components.end());
throw; throw;
} }
components.clear();
components.erase(
components.begin() + begin_index,
components.end());
} }
} }
@@ -113,20 +131,33 @@ namespace e2d::systems
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector< static thread_local vector<
std::tuple<ecs::entity, Ts...>> components; std::tuple<ecs::entity, Ts...>> components;
const std::size_t begin_index = components.size();
try { try {
extract_components<Ts...>( extract_components<Ts...>(
owner, owner,
std::back_inserter(components), std::back_inserter(components),
std::forward<Opts>(opts)...); std::forward<Opts>(opts)...);
std::sort(components.begin(), components.end(), comp);
for ( auto& t : components ) { std::sort(
std::apply(f, t); components.begin() + begin_index,
components.end(),
comp);
const std::size_t end_index = components.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
std::apply(f, components[i]);
} }
} catch (...) { } catch (...) {
components.clear(); components.erase(
components.begin() + begin_index,
components.end());
throw; throw;
} }
components.clear();
components.erase(
components.begin() + begin_index,
components.end());
} }
template < typename... Ts, typename Comp, typename F, typename... Opts > template < typename... Ts, typename Comp, typename F, typename... Opts >
@@ -134,19 +165,32 @@ namespace e2d::systems
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector< static thread_local vector<
std::tuple<ecs::const_entity, Ts...>> components; std::tuple<ecs::const_entity, Ts...>> components;
const std::size_t begin_index = components.size();
try { try {
extract_components<Ts...>( extract_components<Ts...>(
owner, owner,
std::back_inserter(components), std::back_inserter(components),
std::forward<Opts>(opts)...); std::forward<Opts>(opts)...);
std::sort(components.begin(), components.end(), comp);
for ( const auto& t : components ) { std::sort(
std::apply(f, t); components.begin() + begin_index,
components.end(),
comp);
const std::size_t end_index = components.size();
for ( std::size_t i = begin_index; i < end_index; ++i ) {
std::apply(f, components[i]);
} }
} catch (...) { } catch (...) {
components.clear(); components.erase(
components.begin() + begin_index,
components.end());
throw; throw;
} }
components.clear();
components.erase(
components.begin() + begin_index,
components.end());
} }
} }

View File

@@ -2,11 +2,8 @@
local world = { local world = {
} }
---@overload fun(): gobject
---@overload fun(prefab: prefab): gobject ---@overload fun(prefab: prefab): gobject
---@overload fun(parent: node): gobject
---@overload fun(prefab: prefab, parent: node): gobject ---@overload fun(prefab: prefab, parent: node): gobject
---@overload fun(parent: node, transform: t3f): gobject
---@overload fun(prefab: prefab, parent: node, transform: t3f): gobject ---@overload fun(prefab: prefab, parent: node, transform: t3f): gobject
---@return gobject ---@return gobject
function world:instantiate(...) end function world:instantiate(...) end

View File

@@ -15,21 +15,12 @@ namespace e2d::bindings::high
sol::no_constructor, sol::no_constructor,
"instantiate", sol::overload( "instantiate", sol::overload(
[](world& w) -> gobject {
return w.instantiate();
},
[](world& w, const prefab& prefab) -> gobject { [](world& w, const prefab& prefab) -> gobject {
return w.instantiate(prefab); return w.instantiate(prefab);
}, },
[](world& w, const node_iptr& parent) -> gobject {
return w.instantiate(parent);
},
[](world& w, const prefab& prefab, const node_iptr& parent) -> gobject { [](world& w, const prefab& prefab, const node_iptr& parent) -> gobject {
return w.instantiate(prefab, parent); return w.instantiate(prefab, parent);
}, },
[](world& w, const node_iptr& parent, const t3f& transform) -> gobject {
return w.instantiate(parent, transform);
},
[](world& w, const prefab& prefab, const node_iptr& parent, const t3f& transform) -> gobject { [](world& w, const prefab& prefab, const node_iptr& parent, const t3f& transform) -> gobject {
return w.instantiate(prefab, parent, transform); return w.instantiate(prefab, parent, transform);
} }

View File

@@ -210,7 +210,10 @@ namespace
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector<glyph_desc> glyphs; static thread_local vector<glyph_desc> glyphs;
glyphs.clear(); glyphs.clear();
glyphs.reserve(text.size());
if ( glyphs.capacity() < text.size() ) {
glyphs.reserve(math::max(glyphs.capacity() * 2u, text.size()));
}
for ( std::size_t i = 0, e = text.size(); i < e; ++i ) { for ( std::size_t i = 0, e = text.size(); i < e; ++i ) {
glyph_desc desc; glyph_desc desc;
@@ -248,7 +251,11 @@ namespace
//TODO(BlackMat): replace it to frame allocator //TODO(BlackMat): replace it to frame allocator
static thread_local vector<string_desc> strings; static thread_local vector<string_desc> strings;
strings.clear(); strings.clear();
strings.reserve(calculate_string_count(text));
const std::size_t string_count = calculate_string_count(text);
if ( strings.capacity() < string_count ) {
strings.reserve(math::max(strings.capacity() * 2u, string_count));
}
f32 last_space_width = 0.f; f32 last_space_width = 0.f;
std::size_t last_space_index = std::size_t(-1); std::size_t last_space_index = std::size_t(-1);

View File

@@ -187,10 +187,10 @@ namespace e2d::render_system_impl
continue; continue;
} }
int vertex_count = 0;
float* uvs = nullptr; float* uvs = nullptr;
unsigned short* indices = nullptr; unsigned short* indices = nullptr;
int index_count = 0; std::size_t index_count = 0;
std::size_t vertex_count = 0;
const spAtlasPage* atlas_page = nullptr; const spAtlasPage* atlas_page = nullptr;
const spColor* attachment_color = nullptr; const spColor* attachment_color = nullptr;
@@ -205,8 +205,8 @@ namespace e2d::render_system_impl
try { try {
vertex_count = 8; vertex_count = 8;
if ( vertex_count > math::numeric_cast<int>(temp_vertices.size()) ) { if ( temp_vertices.size() < vertex_count ) {
temp_vertices.resize(vertex_count); temp_vertices.resize(math::max(temp_vertices.size() * 2u, vertex_count));
} }
} catch (...) { } catch (...) {
property_cache_.clear(); property_cache_.clear();
@@ -236,8 +236,8 @@ namespace e2d::render_system_impl
try { try {
vertex_count = mesh->super.worldVerticesLength; vertex_count = mesh->super.worldVerticesLength;
if ( vertex_count > math::numeric_cast<int>(temp_vertices.size()) ) { if ( temp_vertices.size() < vertex_count ) {
temp_vertices.resize(vertex_count); temp_vertices.resize(math::max(temp_vertices.size() * 2u, vertex_count));
} }
} catch (...) { } catch (...) {
property_cache_.clear(); property_cache_.clear();
@@ -383,10 +383,10 @@ namespace e2d::render_system_impl
} }
try { try {
const std::size_t batch_vertex_count = const std::size_t batch_vertex_count = vertex_count >> 1;
math::numeric_cast<std::size_t>(vertex_count >> 1);
if ( batch_vertex_count > batch_vertices.size() ) { if ( batch_vertices.size() < batch_vertex_count ) {
batch_vertices.resize(batch_vertex_count); batch_vertices.resize(math::max(batch_vertices.size() * 2u, batch_vertex_count));
} }
for ( std::size_t j = 0; j < batch_vertex_count; ++j ) { for ( std::size_t j = 0; j < batch_vertex_count; ++j ) {