Skip to content

Commit fed5f8a

Browse files
authored
Optimize ProgramState::setTexture() to avoid vector (de)allocations (#2061)
1 parent 1553121 commit fed5f8a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

core/renderer/backend/ProgramState.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ void TextureInfo::assign(TextureInfo&& other)
127127
}
128128
}
129129

130+
void TextureInfo::assign(int slot, int index, backend::TextureBackend* texture)
131+
{
132+
if (textures.size() != 1 or textures[0] != texture or slots[0] != slot or indexs[0] != index)
133+
{
134+
releaseTextures();
135+
indexs.resize(1);
136+
indexs[0] = index;
137+
slots.resize(1);
138+
slots[0] = slot;
139+
textures.resize(1);
140+
textures[0] = texture;
141+
AX_SAFE_RETAIN(texture);
142+
143+
#if AX_ENABLE_CACHE_TEXTURE_DATA
144+
location = -1;
145+
#endif
146+
}
147+
}
148+
130149
/* CLASS ProgramState */
131150
ProgramState::ProgramState(Program* program)
132151
{
@@ -361,7 +380,7 @@ void ProgramState::setTexture(int location,
361380
return;
362381

363382
auto& info = textureInfo[location];
364-
info = {{slot}, {index}, {texture}};
383+
info.assign(slot, index, texture);
365384

366385
#if AX_ENABLE_CACHE_TEXTURE_DATA
367386
info.location = location;

core/renderer/backend/ProgramState.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct AX_DLL TextureInfo
6868

6969
void assign(const TextureInfo& other);
7070
void assign(TextureInfo&& other);
71+
void assign(int slot, int index, backend::TextureBackend* texture);
7172

7273
void retainTextures();
7374
void releaseTextures();

0 commit comments

Comments
 (0)