diff --git a/src/engine/client/backend/opengl/backend_opengl.cpp b/src/engine/client/backend/opengl/backend_opengl.cpp index cb0ef4824..176f74b1f 100644 --- a/src/engine/client/backend/opengl/backend_opengl.cpp +++ b/src/engine/client/backend/opengl/backend_opengl.cpp @@ -729,7 +729,7 @@ void CCommandProcessorFragment_OpenGL::TextureCreate(int Slot, int Width, int He glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_MaxTexSize); } - if(Slot >= (int)m_vTextures.size()) + while(Slot >= (int)m_vTextures.size()) m_vTextures.resize(m_vTextures.size() * 2); m_vTextures[Slot].m_ResizeWidth = -1.f; diff --git a/src/engine/client/backend/opengl/backend_opengl3.cpp b/src/engine/client/backend/opengl/backend_opengl3.cpp index e251329ed..5fdee8c2c 100644 --- a/src/engine/client/backend/opengl/backend_opengl3.cpp +++ b/src/engine/client/backend/opengl/backend_opengl3.cpp @@ -579,7 +579,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Texture_Destroy(const CCommandBuff void CCommandProcessorFragment_OpenGL3_3::TextureCreate(int Slot, int Width, int Height, int GLFormat, int GLStoreFormat, int Flags, void *pTexData) { - if(Slot >= (int)m_vTextures.size()) + while(Slot >= (int)m_vTextures.size()) m_vTextures.resize(m_vTextures.size() * 2); // resample if needed diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 474c593a8..0960182b4 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -264,18 +264,14 @@ void CGraphics_Threaded::LinesDraw(const CLineItem *pArray, int Num) IGraphics::CTextureHandle CGraphics_Threaded::FindFreeTextureIndex() { - int Tex = m_FirstFreeTexture; - if(Tex == -1) + const size_t CurSize = m_vTextureIndices.size(); + if(m_FirstFreeTexture == CurSize) { - const size_t CurSize = m_vTextureIndices.size(); m_vTextureIndices.resize(CurSize * 2); - for(size_t i = 0; i < CurSize - 1; ++i) - { + for(size_t i = 0; i < CurSize; ++i) m_vTextureIndices[CurSize + i] = CurSize + i + 1; - } - m_vTextureIndices.back() = -1; - Tex = CurSize; } + const size_t Tex = m_FirstFreeTexture; m_FirstFreeTexture = m_vTextureIndices[Tex]; m_vTextureIndices[Tex] = -1; return CreateTextureHandle(Tex); @@ -284,6 +280,8 @@ IGraphics::CTextureHandle CGraphics_Threaded::FindFreeTextureIndex() void CGraphics_Threaded::FreeTextureIndex(CTextureHandle *pIndex) { dbg_assert(pIndex->IsValid(), "Cannot free invalid texture index"); + dbg_assert(m_vTextureIndices[pIndex->Id()] == -1, "Cannot free already freed texture index"); + m_vTextureIndices[pIndex->Id()] = m_FirstFreeTexture; m_FirstFreeTexture = pIndex->Id(); pIndex->Invalidate(); @@ -2598,9 +2596,8 @@ int CGraphics_Threaded::Init() // init textures m_FirstFreeTexture = 0; m_vTextureIndices.resize(CCommandBuffer::MAX_TEXTURES); - for(size_t i = 0; i < m_vTextureIndices.size() - 1; ++i) + for(size_t i = 0; i < m_vTextureIndices.size(); ++i) m_vTextureIndices[i] = i + 1; - m_vTextureIndices.back() = -1; m_FirstFreeVertexArrayInfo = -1; m_FirstFreeBufferObjectIndex = -1; diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 7a3932684..72877e793 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -819,7 +819,7 @@ class CGraphics_Threaded : public IEngineGraphics CTextureHandle m_InvalidTexture; std::vector m_vTextureIndices; - int m_FirstFreeTexture; + size_t m_FirstFreeTexture; int m_TextureMemoryUsage; std::vector m_vSpriteHelper;