From fed5f8a37448aae2eb51267c5fbdc25b1861701d Mon Sep 17 00:00:00 2001 From: smilediver Date: Tue, 30 Jul 2024 19:19:56 +0300 Subject: [PATCH] Optimize ProgramState::setTexture() to avoid vector (de)allocations (#2061) --- core/renderer/backend/ProgramState.cpp | 21 ++++++++++++++++++++- core/renderer/backend/ProgramState.h | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/renderer/backend/ProgramState.cpp b/core/renderer/backend/ProgramState.cpp index 11ebef98c39d..18a5c41e2c97 100644 --- a/core/renderer/backend/ProgramState.cpp +++ b/core/renderer/backend/ProgramState.cpp @@ -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) { @@ -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; diff --git a/core/renderer/backend/ProgramState.h b/core/renderer/backend/ProgramState.h index 1df1d068c72e..f310cfa6ab0e 100644 --- a/core/renderer/backend/ProgramState.h +++ b/core/renderer/backend/ProgramState.h @@ -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();