Skip to content

Commit 06136cd

Browse files
committed
noggit: texture_set: use std::unique_ptr for temp edit values and alphamaps to avoid unnecessary memory allocations
1 parent 311956c commit 06136cd

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/noggit/texture_set.cpp

+22-22
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ TextureSet::TextureSet (MapChunkHeader const& header, MPQFile* f, size_t base, M
4545
if (_layers_info[layer].flags & 0x100)
4646
{
4747
f->seek (alpha_base + _layers_info[layer].ofsAlpha);
48-
alphamaps[layer - 1] = boost::in_place (f, _layers_info[layer].flags, use_big_alphamaps, do_not_fix_alpha_map);
48+
alphamaps[layer - 1] = std::make_unique<Alphamap> (f, _layers_info[layer].flags, use_big_alphamaps, do_not_fix_alpha_map);
4949
}
5050
}
5151

@@ -73,12 +73,12 @@ int TextureSet::addTexture (scoped_blp_texture_reference texture)
7373

7474
if (texLevel)
7575
{
76-
alphamaps[texLevel - 1] = boost::in_place();
76+
alphamaps[texLevel - 1] = std::make_unique<Alphamap>();
7777
}
7878

7979
if (tmp_edit_values && nTextures == 1)
8080
{
81-
tmp_edit_values.get()[0].fill(255.f);
81+
tmp_edit_values.get()->map[0].fill(255.f);
8282
}
8383
}
8484

@@ -175,7 +175,7 @@ void TextureSet::eraseTextures()
175175
{
176176
if (i > 0)
177177
{
178-
alphamaps[i - 1] = boost::none;
178+
alphamaps[i - 1].reset();
179179
}
180180
_layers_info[i] = ENTRY_MCLY();
181181
}
@@ -188,7 +188,7 @@ void TextureSet::eraseTextures()
188188
_need_amap_update = true;
189189
_need_lod_texture_map_update = true;
190190

191-
tmp_edit_values = boost::none;
191+
tmp_edit_values.reset();
192192
}
193193

194194
void TextureSet::eraseTexture(size_t id)
@@ -203,21 +203,21 @@ void TextureSet::eraseTexture(size_t id)
203203
{
204204
if (i)
205205
{
206-
alphamaps[i - 1] = boost::none;
206+
alphamaps[i - 1].reset();
207207
std::swap (alphamaps[i - 1], alphamaps[i]);
208208
}
209209

210210
if (tmp_edit_values)
211211
{
212-
tmp_edit_values.get()[i].swap(tmp_edit_values.get()[i+1]);
212+
tmp_edit_values.get()->map[i].swap(tmp_edit_values.get()->map[i+1]);
213213
}
214214

215215
_layers_info[i] = _layers_info[i + 1];
216216
}
217217

218218
if (nTextures > 1)
219219
{
220-
alphamaps[nTextures - 2] = boost::none;
220+
alphamaps[nTextures - 2].reset();
221221
}
222222

223223
textures.erase(textures.begin()+id);
@@ -229,7 +229,7 @@ void TextureSet::eraseTexture(size_t id)
229229
// set the default values for the temporary alphamap too
230230
if (tmp_edit_values)
231231
{
232-
tmp_edit_values.get()[nTextures].fill(0.f);
232+
tmp_edit_values.get()->map[nTextures].fill(0.f);
233233
}
234234

235235
_need_amap_update = true;
@@ -295,7 +295,7 @@ bool TextureSet::eraseUnusedTextures()
295295

296296
if (tmp_edit_values)
297297
{
298-
auto& amaps = tmp_edit_values.get();
298+
auto& amaps = *tmp_edit_values.get();
299299

300300
for (int i = 0; i < 4096 && visible_tex.size() < nTextures; ++i)
301301
{
@@ -394,7 +394,7 @@ bool TextureSet::paintTexture(float xbase, float zbase, float x, float z, Brush*
394394
}
395395

396396
create_temporary_alphamaps_if_needed();
397-
auto& amaps = tmp_edit_values.get();
397+
auto& amaps = *tmp_edit_values.get();
398398

399399
zPos = zbase;
400400

@@ -582,7 +582,7 @@ bool TextureSet::replace_texture( float xbase
582582
}
583583

584584
create_temporary_alphamaps_if_needed();
585-
auto& amap = tmp_edit_values.get();
585+
auto& amap = *tmp_edit_values.get();
586586

587587
for (int j = 0; j < 64; j++)
588588
{
@@ -701,7 +701,7 @@ std::vector<std::vector<uint8_t>> TextureSet::save_alpha(bool big_alphamap)
701701
{
702702
alphas_to_old_alpha(tab);
703703
}
704-
704+
705705

706706
auto const combine_nibble
707707
(
@@ -857,7 +857,7 @@ void TextureSet::merge_layers(size_t id1, size_t id2)
857857

858858
create_temporary_alphamaps_if_needed();
859859

860-
auto& amap = tmp_edit_values.get();
860+
auto& amap = *tmp_edit_values.get();
861861

862862
for (int i = 0; i < 64 * 64; ++i)
863863
{
@@ -902,7 +902,7 @@ void TextureSet::bind_alpha(std::size_t id)
902902
if (tmp_edit_values)
903903
{
904904
std::vector<float> amap(3 * 64 * 64);
905-
auto& tmp_amaps = tmp_edit_values.get();
905+
auto& tmp_amaps = *tmp_edit_values.get();
906906

907907
for (int i = 0; i < 64 * 64; ++i)
908908
{
@@ -951,12 +951,12 @@ void TextureSet::bind_alpha(std::size_t id)
951951
namespace
952952
{
953953
misc::max_capacity_stack_vector<std::size_t, 4> current_layer_values
954-
(std::uint8_t nTextures, boost::optional<Alphamap> const* alphamaps, std::size_t pz, std::size_t px)
954+
(std::uint8_t nTextures, std::unique_ptr<Alphamap> const* alphamaps, std::size_t pz, std::size_t px)
955955
{
956956
misc::max_capacity_stack_vector<std::size_t, 4> values (nTextures, 0xFF);
957957
for (std::uint8_t i = 1; i < nTextures; ++i)
958958
{
959-
values[i] = alphamaps[i - 1].get().getAlpha(64 * pz + px);
959+
values[i] = alphamaps[i - 1].get()->getAlpha(64 * pz + px);
960960
values[0] -= values[i];
961961
}
962962
return values;
@@ -1028,11 +1028,11 @@ bool TextureSet::apply_alpha_changes()
10281028
{
10291029
if (!tmp_edit_values || nTextures < 2)
10301030
{
1031-
tmp_edit_values = boost::none;
1031+
tmp_edit_values.reset();
10321032
return false;
10331033
}
10341034

1035-
auto& new_amaps = tmp_edit_values.get();
1035+
auto& new_amaps = *tmp_edit_values.get();
10361036
std::array<std::uint16_t, 64 * 64> totals;
10371037
totals.fill(0);
10381038

@@ -1059,7 +1059,7 @@ bool TextureSet::apply_alpha_changes()
10591059
_need_amap_update = true;
10601060
_need_lod_texture_map_update = true;
10611061

1062-
tmp_edit_values = boost::none;
1062+
tmp_edit_values.reset();
10631063

10641064
return true;
10651065
}
@@ -1071,9 +1071,9 @@ void TextureSet::create_temporary_alphamaps_if_needed()
10711071
return;
10721072
}
10731073

1074-
tmp_edit_values.emplace();
1074+
tmp_edit_values = std::make_unique<tmp_edit_alpha_values>();
10751075

1076-
tmp_edit_alpha_values& values = tmp_edit_values.get();
1076+
tmp_edit_alpha_values& values = *tmp_edit_values.get();
10771077

10781078
for (int i = 0; i < 64 * 64; ++i)
10791079
{

src/noggit/texture_set.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ class TextureSet
7676
std::vector<uint8_t> lod_texture_map();
7777

7878
bool apply_alpha_changes();
79-
79+
8080
void create_temporary_alphamaps_if_needed();
8181
size_t nTextures;
82-
boost::optional<tmp_edit_alpha_values> tmp_edit_values;
82+
std::unique_ptr<tmp_edit_alpha_values> tmp_edit_values;
8383
private:
8484
int get_texture_index_or_add (scoped_blp_texture_reference texture, float target);
8585

@@ -91,7 +91,7 @@ class TextureSet
9191
void update_lod_texture_map();
9292

9393
std::vector<scoped_blp_texture_reference> textures;
94-
std::array<boost::optional<Alphamap>, 3> alphamaps;
94+
std::array<std::unique_ptr<Alphamap>, 3> alphamaps;
9595
opengl::texture amap_gl_tex;
9696
bool _need_amap_update = true;
9797

0 commit comments

Comments
 (0)