Skip to content

Commit fd1d4c0

Browse files
committed
Avoid allocating new texture for every video frame
1 parent 3728b82 commit fd1d4c0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/content/Texture.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Handle::TextureHandle TextureAllocator::loadTextureDDS(const std::vector<uint8_t
4242
m_Allocator.getElement(h).textureFormat = bgfx::TextureFormat::Unknown;
4343
m_Allocator.getElement(h).imageData = data;
4444
//m_Allocator.getElement(h).m_TextureHandle = bth;
45+
m_Allocator.getElement(h).m_TextureHandle.idx = bgfx::kInvalidHandle;
4546
m_Allocator.getElement(h).m_TextureName = name;
4647

4748
ZenLoad::DDSURFACEDESC2 desc = ZenLoad::getSurfaceDesc(data);
@@ -80,6 +81,7 @@ Handle::TextureHandle TextureAllocator::loadTextureRGBA8(const std::vector<uint8
8081
m_Allocator.getElement(h).textureFormat = bgfx::TextureFormat::RGBA8;
8182
m_Allocator.getElement(h).imageData = data;
8283
//m_Allocator.getElement(h).m_TextureHandle = bth;
84+
m_Allocator.getElement(h).m_TextureHandle.idx = bgfx::kInvalidHandle;
8385
m_Allocator.getElement(h).m_TextureName = name;
8486
m_Allocator.getElement(h).m_Width = width;
8587
m_Allocator.getElement(h).m_Height = height;
@@ -185,6 +187,12 @@ bool TextureAllocator::finalizeLoad(Handle::TextureHandle h)
185187
{
186188
Texture& tx = m_Allocator.getElement(h);
187189

190+
// if there's already a texture loaded for the object, destroy it
191+
if (tx.m_TextureHandle.idx != bgfx::kInvalidHandle) {
192+
bgfx::destroy(tx.m_TextureHandle);
193+
tx.m_TextureHandle.idx = bgfx::kInvalidHandle;
194+
}
195+
188196
std::uint32_t textureFlags = BGFX_TEXTURE_NONE;
189197

190198
if (this->m_Engine.getEngineArgs().noTextureFiltering)
@@ -204,8 +212,10 @@ bool TextureAllocator::finalizeLoad(Handle::TextureHandle h)
204212
//stbi_image_free(out);
205213

206214
// Couldn't load this one?
207-
if (bth.idx == bgfx::kInvalidHandle)
215+
if (bth.idx == bgfx::kInvalidHandle) {
216+
LogWarn() << "Could not finalize texture load";
208217
return false;
218+
}
209219

210220
tx.m_TextureHandle = bth;
211221
}

src/media/Video.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ namespace Media {
236236
if (!texture.isValid())
237237
texture = alloc.loadTextureRGBA8(data, videoCodecContext->width, videoCodecContext->height);
238238
else
239-
alloc.getTexture(texture).imageData = data;
239+
std::swap(alloc.getTexture(texture).imageData, data);
240240
alloc.asyncFinalizeLoad(texture);
241241
view->setImage(texture, videoCodecContext->width, videoCodecContext->height);
242242
}

0 commit comments

Comments
 (0)