Fixed handle leaks.

This commit is contained in:
Branimir Karadžić 2018-07-02 16:33:11 -07:00
parent 90daafba80
commit fac2937a1f

View File

@ -2992,7 +2992,13 @@ namespace bgfx
{
BGFX_MUTEX_SCOPE(m_resourceApiLock);
DynamicIndexBufferHandle handle = BGFX_INVALID_HANDLE;
DynamicIndexBufferHandle handle = { m_dynamicIndexBufferHandle.alloc() };
BX_WARN(isValid(handle), "Failed to allocate dynamic index buffer handle.");
if (!isValid(handle) )
{
return handle;
}
const uint32_t indexSize = 0 == (_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
uint32_t size = BX_ALIGN_16(_num*indexSize);
@ -3002,7 +3008,8 @@ namespace bgfx
IndexBufferHandle indexBufferHandle = { m_indexBufferHandle.alloc() };
if (!isValid(indexBufferHandle) )
{
return handle;
m_dynamicIndexBufferHandle.free(handle.idx);
return BGFX_INVALID_HANDLE;
}
IndexBuffer& ib = m_indexBuffers[indexBufferHandle.idx];
@ -3020,17 +3027,11 @@ namespace bgfx
ptr = allocDynamicIndexBuffer(size, _flags);
if (ptr == NonLocalAllocator::kInvalidBlock)
{
return handle;
m_dynamicIndexBufferHandle.free(handle.idx);
return BGFX_INVALID_HANDLE;
}
}
handle.idx = m_dynamicIndexBufferHandle.alloc();
BX_WARN(isValid(handle), "Failed to allocate dynamic index buffer handle.");
if (!isValid(handle) )
{
return handle;
}
DynamicIndexBuffer& dib = m_dynamicIndexBuffers[handle.idx];
dib.m_handle.idx = uint16_t(ptr>>32);
dib.m_offset = uint32_t(ptr);
@ -3049,15 +3050,14 @@ namespace bgfx
const uint32_t indexSize = 0 == (_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
DynamicIndexBufferHandle handle = createDynamicIndexBuffer(_mem->size/indexSize, _flags);
if (isValid(handle) )
{
update(handle, 0, _mem);
}
else
if (!isValid(handle) )
{
release(_mem);
return BGFX_INVALID_HANDLE;
}
update(handle, 0, _mem);
return handle;
}
@ -3167,7 +3167,20 @@ namespace bgfx
{
BGFX_MUTEX_SCOPE(m_resourceApiLock);
DynamicVertexBufferHandle handle = BGFX_INVALID_HANDLE;
VertexDeclHandle declHandle = findVertexDecl(_decl);
if (!isValid(declHandle) )
{
BX_TRACE("WARNING: Failed to allocate vertex decl handle (BGFX_CONFIG_MAX_VERTEX_DECLS, max: %d).", BGFX_CONFIG_MAX_VERTEX_DECLS);
return BGFX_INVALID_HANDLE;
}
DynamicVertexBufferHandle handle = { m_dynamicVertexBufferHandle.alloc() };
if (!isValid(handle) )
{
BX_TRACE("WARNING: Failed to allocate dynamic vertex buffer handle (BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS, max: %d).", BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS);
return BGFX_INVALID_HANDLE;
}
uint32_t size = bx::strideAlign16(_num*_decl.m_stride, _decl.m_stride);
uint64_t ptr = 0;
@ -3176,8 +3189,9 @@ namespace bgfx
VertexBufferHandle vertexBufferHandle = { m_vertexBufferHandle.alloc() };
if (!isValid(vertexBufferHandle) )
{
m_dynamicVertexBufferHandle.free(handle.idx);
BX_TRACE("WARNING: Failed to allocate vertex buffer handle (BGFX_CONFIG_MAX_VERTEX_BUFFERS, max: %d).", BGFX_CONFIG_MAX_VERTEX_BUFFERS);
return handle;
return BGFX_INVALID_HANDLE;
}
VertexBuffer& vb = m_vertexBuffers[vertexBufferHandle.idx];
@ -3196,24 +3210,11 @@ namespace bgfx
ptr = allocDynamicVertexBuffer(size, _flags);
if (ptr == NonLocalAllocator::kInvalidBlock)
{
return handle;
m_dynamicVertexBufferHandle.free(handle.idx);
return BGFX_INVALID_HANDLE;
}
}
VertexDeclHandle declHandle = findVertexDecl(_decl);
if (!isValid(declHandle) )
{
BX_TRACE("WARNING: Failed to allocate vertex decl handle (BGFX_CONFIG_MAX_VERTEX_DECLS, max: %d).", BGFX_CONFIG_MAX_VERTEX_DECLS);
return handle;
}
handle.idx = m_dynamicVertexBufferHandle.alloc();
if (!isValid(handle) )
{
BX_TRACE("WARNING: Failed to allocate dynamic vertex buffer handle (BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS, max: %d).", BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS);
return handle;
}
DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[handle.idx];
dvb.m_handle.idx = uint16_t(ptr>>32);
dvb.m_offset = uint32_t(ptr);
@ -3235,15 +3236,14 @@ namespace bgfx
uint32_t numVertices = _mem->size/_decl.m_stride;
DynamicVertexBufferHandle handle = createDynamicVertexBuffer(numVertices, _decl, _flags);
if (isValid(handle) )
{
update(handle, 0, _mem);
}
else
if (!isValid(handle) )
{
release(_mem);
return BGFX_INVALID_HANDLE;
}
update(handle, 0, _mem);
return handle;
}
@ -3899,42 +3899,45 @@ namespace bgfx
_info->numMips = 0;
_info->bitsPerPixel = 0;
_info->cubeMap = false;
return BGFX_INVALID_HANDLE;
}
TextureHandle handle = { m_textureHandle.alloc() };
BX_WARN(isValid(handle), "Failed to allocate texture handle.");
if (isValid(handle) )
if (!isValid(handle) )
{
TextureRef& ref = m_textureRef[handle.idx];
ref.init(_ratio
, _info->format
, _info->storageSize
, imageContainer.m_numMips
, 0 != (g_caps.supported & BGFX_CAPS_TEXTURE_DIRECT_ACCESS)
, _immutable
, 0 != (_flags & BGFX_TEXTURE_RT_MASK)
);
release(_mem);
return BGFX_INVALID_HANDLE;
}
if (ref.m_rt)
{
m_rtMemoryUsed += int64_t(ref.m_storageSize);
}
else
{
m_textureMemoryUsed += int64_t(ref.m_storageSize);
}
TextureRef& ref = m_textureRef[handle.idx];
ref.init(
_ratio
, _info->format
, _info->storageSize
, imageContainer.m_numMips
, 0 != (g_caps.supported & BGFX_CAPS_TEXTURE_DIRECT_ACCESS)
, _immutable
, 0 != (_flags & BGFX_TEXTURE_RT_MASK)
);
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateTexture);
cmdbuf.write(handle);
cmdbuf.write(_mem);
cmdbuf.write(_flags);
cmdbuf.write(_skip);
if (ref.m_rt)
{
m_rtMemoryUsed += int64_t(ref.m_storageSize);
}
else
{
release(_mem);
m_textureMemoryUsed += int64_t(ref.m_storageSize);
}
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateTexture);
cmdbuf.write(handle);
cmdbuf.write(_mem);
cmdbuf.write(_flags);
cmdbuf.write(_skip);
return handle;
}
@ -4240,11 +4243,10 @@ namespace bgfx
{
BGFX_MUTEX_SCOPE(m_resourceApiLock);
BX_WARN(PredefinedUniform::Count == nameToPredefinedUniformEnum(_name), "%s is predefined uniform name.", _name);
if (PredefinedUniform::Count != nameToPredefinedUniformEnum(_name) )
{
UniformHandle handle = BGFX_INVALID_HANDLE;
return handle;
BX_TRACE("%s is predefined uniform name.", _name);
return BGFX_INVALID_HANDLE;
}
_num = bx::max<uint16_t>(1, _num);
@ -4284,32 +4286,31 @@ namespace bgfx
UniformHandle handle = { m_uniformHandle.alloc() };
if (isValid(handle) )
{
BX_TRACE("Creating uniform (handle %3d) %s", handle.idx, _name);
UniformRef& uniform = m_uniformRef[handle.idx];
uniform.m_name.set(_name);
uniform.m_refCount = 1;
uniform.m_type = _type;
uniform.m_num = _num;
bool ok = m_uniformHashMap.insert(bx::hash<bx::HashMurmur2A>(_name), handle.idx);
BX_CHECK(ok, "Uniform already exists (name: %s)!", _name); BX_UNUSED(ok);
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateUniform);
cmdbuf.write(handle);
cmdbuf.write(_type);
cmdbuf.write(_num);
uint8_t len = (uint8_t)bx::strLen(_name)+1;
cmdbuf.write(len);
cmdbuf.write(_name, len);
}
else
if (!isValid(handle) )
{
BX_TRACE("Failed to allocate uniform handle.");
return BGFX_INVALID_HANDLE;
}
BX_TRACE("Creating uniform (handle %3d) %s", handle.idx, _name);
UniformRef& uniform = m_uniformRef[handle.idx];
uniform.m_name.set(_name);
uniform.m_refCount = 1;
uniform.m_type = _type;
uniform.m_num = _num;
bool ok = m_uniformHashMap.insert(bx::hash<bx::HashMurmur2A>(_name), handle.idx);
BX_CHECK(ok, "Uniform already exists (name: %s)!", _name); BX_UNUSED(ok);
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateUniform);
cmdbuf.write(handle);
cmdbuf.write(_type);
cmdbuf.write(_num);
uint8_t len = (uint8_t)bx::strLen(_name)+1;
cmdbuf.write(len);
cmdbuf.write(_name, len);
return handle;
}