Prevent invalid texture update calls to immutable texture. Issue #1338.

This commit is contained in:
Branimir Karadžić 2018-02-19 09:42:02 -08:00
parent cef850f3dd
commit 627abbd9d3
2 changed files with 34 additions and 13 deletions

View File

@ -3647,7 +3647,7 @@ error:
TextureHandle createTexture(const Memory* _mem, uint32_t _flags, uint8_t _skip, TextureInfo* _info) TextureHandle createTexture(const Memory* _mem, uint32_t _flags, uint8_t _skip, TextureInfo* _info)
{ {
BX_CHECK(NULL != _mem, "_mem can't be NULL"); BX_CHECK(NULL != _mem, "_mem can't be NULL");
return s_ctx->createTexture(_mem, _flags, _skip, _info, BackbufferRatio::Count); return s_ctx->createTexture(_mem, _flags, _skip, _info, BackbufferRatio::Count, false);
} }
void getTextureSizeFromRatio(BackbufferRatio::Enum _ratio, uint16_t& _width, uint16_t& _height) void getTextureSizeFromRatio(BackbufferRatio::Enum _ratio, uint16_t& _width, uint16_t& _height)
@ -3718,7 +3718,7 @@ error:
tc.m_mem = _mem; tc.m_mem = _mem;
bx::write(&writer, tc); bx::write(&writer, tc);
return s_ctx->createTexture(mem, _flags, 0, NULL, _ratio); return s_ctx->createTexture(mem, _flags, 0, NULL, _ratio, NULL != _mem);
} }
TextureHandle createTexture2D(uint16_t _width, uint16_t _height, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem) TextureHandle createTexture2D(uint16_t _width, uint16_t _height, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem)
@ -3771,7 +3771,7 @@ error:
tc.m_mem = _mem; tc.m_mem = _mem;
bx::write(&writer, tc); bx::write(&writer, tc);
return s_ctx->createTexture(mem, _flags, 0, NULL, BackbufferRatio::Count); return s_ctx->createTexture(mem, _flags, 0, NULL, BackbufferRatio::Count, NULL != _mem);
} }
TextureHandle createTextureCube(uint16_t _size, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem) TextureHandle createTextureCube(uint16_t _size, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem)
@ -3813,7 +3813,7 @@ error:
tc.m_mem = _mem; tc.m_mem = _mem;
bx::write(&writer, tc); bx::write(&writer, tc);
return s_ctx->createTexture(mem, _flags, 0, NULL, BackbufferRatio::Count); return s_ctx->createTexture(mem, _flags, 0, NULL, BackbufferRatio::Count, NULL != _mem);
} }
void setName(TextureHandle _handle, const char* _name) void setName(TextureHandle _handle, const char* _name)

View File

@ -3742,7 +3742,7 @@ namespace bgfx
} }
} }
BGFX_API_FUNC(TextureHandle createTexture(const Memory* _mem, uint32_t _flags, uint8_t _skip, TextureInfo* _info, BackbufferRatio::Enum _ratio) ) BGFX_API_FUNC(TextureHandle createTexture(const Memory* _mem, uint32_t _flags, uint8_t _skip, TextureInfo* _info, BackbufferRatio::Enum _ratio, bool _immutable) )
{ {
BGFX_MUTEX_SCOPE(m_resourceApiLock); BGFX_MUTEX_SCOPE(m_resourceApiLock);
@ -3782,7 +3782,12 @@ namespace bgfx
if (isValid(handle) ) if (isValid(handle) )
{ {
TextureRef& ref = m_textureRef[handle.idx]; TextureRef& ref = m_textureRef[handle.idx];
ref.init(_ratio, _info->format, imageContainer.m_numMips, 0 != (g_caps.supported & BGFX_CAPS_TEXTURE_DIRECT_ACCESS) ); ref.init(_ratio
, _info->format
, imageContainer.m_numMips
, 0 != (g_caps.supported & BGFX_CAPS_TEXTURE_DIRECT_ACCESS)
, _immutable
);
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateTexture); CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateTexture);
cmdbuf.write(handle); cmdbuf.write(handle);
@ -3925,6 +3930,14 @@ namespace bgfx
{ {
BGFX_MUTEX_SCOPE(m_resourceApiLock); BGFX_MUTEX_SCOPE(m_resourceApiLock);
const TextureRef& textureRef = m_textureRef[_handle.idx];
if (textureRef.m_immutable)
{
BX_WARN(false, "Can't update immutable texture.");
release(_mem);
return;
}
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateTexture); CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateTexture);
cmdbuf.write(_handle); cmdbuf.write(_handle);
cmdbuf.write(_side); cmdbuf.write(_side);
@ -4547,7 +4560,13 @@ namespace bgfx
struct TextureRef struct TextureRef
{ {
void init(BackbufferRatio::Enum _ratio, TextureFormat::Enum _format, uint8_t _numMips, bool _ptrPending) void init(
BackbufferRatio::Enum _ratio
, TextureFormat::Enum _format
, uint8_t _numMips
, bool _ptrPending
, bool _immutable
)
{ {
m_ptr = _ptrPending ? (void*)UINTPTR_MAX : NULL; m_ptr = _ptrPending ? (void*)UINTPTR_MAX : NULL;
m_refCount = 1; m_refCount = 1;
@ -4555,6 +4574,7 @@ namespace bgfx
m_format = uint8_t(_format); m_format = uint8_t(_format);
m_numMips = _numMips; m_numMips = _numMips;
m_owned = false; m_owned = false;
m_immutable = _immutable;
} }
String m_name; String m_name;
@ -4564,6 +4584,7 @@ namespace bgfx
uint8_t m_format; uint8_t m_format;
uint8_t m_numMips; uint8_t m_numMips;
bool m_owned; bool m_owned;
bool m_immutable;
}; };
struct FrameBufferRef struct FrameBufferRef