Skip to content

Commit

Permalink
Optimize ProgramState::setTexture() to avoid vector (de)allocations (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
smilediver authored Jul 30, 2024
1 parent 1553121 commit fed5f8a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion core/renderer/backend/ProgramState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ void TextureInfo::assign(TextureInfo&& other)
}
}

void TextureInfo::assign(int slot, int index, backend::TextureBackend* texture)
{
if (textures.size() != 1 or textures[0] != texture or slots[0] != slot or indexs[0] != index)
{
releaseTextures();
indexs.resize(1);
indexs[0] = index;
slots.resize(1);
slots[0] = slot;
textures.resize(1);
textures[0] = texture;
AX_SAFE_RETAIN(texture);

#if AX_ENABLE_CACHE_TEXTURE_DATA
location = -1;
#endif
}
}

/* CLASS ProgramState */
ProgramState::ProgramState(Program* program)
{
Expand Down Expand Up @@ -361,7 +380,7 @@ void ProgramState::setTexture(int location,
return;

auto& info = textureInfo[location];
info = {{slot}, {index}, {texture}};
info.assign(slot, index, texture);

#if AX_ENABLE_CACHE_TEXTURE_DATA
info.location = location;
Expand Down
1 change: 1 addition & 0 deletions core/renderer/backend/ProgramState.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct AX_DLL TextureInfo

void assign(const TextureInfo& other);
void assign(TextureInfo&& other);
void assign(int slot, int index, backend::TextureBackend* texture);

void retainTextures();
void releaseTextures();
Expand Down

0 comments on commit fed5f8a

Please sign in to comment.