Improved internal handle.

This commit is contained in:
Бранимир Караџић 2022-10-05 21:10:19 -07:00
parent 50250df026
commit 8a405a0c73
3 changed files with 137 additions and 46 deletions

View File

@ -311,19 +311,28 @@ namespace bgfx
PlatformData g_platformData;
bool g_platformDataChangedSinceReset = false;
const char* getTypeName(Handle _handle)
static Handle::TypeName s_typeName[] =
{
switch (_handle.type)
{
case Handle::IndexBuffer: return "IB";
case Handle::Shader: return "S";
case Handle::Texture: return "T";
case Handle::VertexBuffer: return "VB";
default: break;
}
{ "DIB", "DynamicIndexBuffer" },
{ "DVB", "DynamicVertexBuffer" },
{ "FB", "FrameBuffer" },
{ "IB", "IndexBuffer" },
{ "IndB", "IndirectBuffer" },
{ "OQ", "OcclusionQuery" },
{ "P", "Program" },
{ "S", "Shader" },
{ "T", "Texture" },
{ "U", "Uniform" },
{ "VB", "VertexBuffer" },
{ "VL", "VertexLayout" },
{ "?", "?" },
};
BX_STATIC_ASSERT(BX_COUNTOF(s_typeName) == Handle::Count+1, "");
BX_ASSERT(false, "You should not be here.");
return "?";
const Handle::TypeName& Handle::getTypeName(Handle::Enum _enum)
{
BX_ASSERT(_enum < Handle::Count, "Invalid Handle::Enum %d!", _enum);
return s_typeName[bx::min(_enum, Handle::Count)];
}
void AllocatorStub::checkLeaks()

View File

@ -301,37 +301,119 @@ namespace bgfx
typedef uint32_t RenderItemCount;
#endif // BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)
///
struct Handle
{
///
struct TypeName
{
const char* abrvName;
const char* fullName;
};
///
enum Enum
{
DynamicIndexBuffer,
DynamicVertexBuffer,
FrameBuffer,
IndexBuffer,
IndirectBuffer,
OcclusionQuery,
Program,
Shader,
Texture,
Uniform,
VertexBuffer,
VertexLayout,
Count
};
uint16_t type;
template<typename Ty>
static constexpr Enum toEnum();
constexpr Handle()
: idx(kInvalidHandle)
, type(Count)
{
}
template<typename Ty>
constexpr Handle(Ty _handle)
: idx(_handle.idx)
, type(toEnum<Ty>() )
{
}
template<typename Ty>
constexpr Ty to() const
{
if (type == toEnum<Ty>() )
{
return Ty{ idx };
}
BX_ASSERT(type == toEnum<Ty>(), "Handle type %s, cannot be converted to %s."
, getTypeName().fullName
, getTypeName(toEnum<Ty>() ).fullName
);
return { kInvalidHandle };
}
Enum getType() const
{
return Enum(type);
}
static const TypeName& getTypeName(Handle::Enum _enum);
const TypeName& getTypeName() const
{
return getTypeName(getType() );
}
bool isBuffer() const
{
return false
|| type == DynamicIndexBuffer
|| type == DynamicVertexBuffer
|| type == IndexBuffer
|| type == IndirectBuffer
|| type == VertexBuffer
;
}
bool isTexture() const
{
return type == Texture;
}
uint16_t idx;
uint16_t type;
};
#define CONVERT_HANDLE(_name) \
inline Handle convert(_name##Handle _handle) \
{ \
Handle handle = { Handle::_name, _handle.idx }; \
return handle; \
}
#define IMPLEMENT_HANDLE(_name) \
template<> \
inline constexpr Handle::Enum Handle::toEnum<_name##Handle>() \
{ \
return Handle::_name; \
} \
CONVERT_HANDLE(IndexBuffer);
CONVERT_HANDLE(Shader);
CONVERT_HANDLE(Texture);
CONVERT_HANDLE(VertexBuffer);
IMPLEMENT_HANDLE(DynamicIndexBuffer);
IMPLEMENT_HANDLE(DynamicVertexBuffer);
IMPLEMENT_HANDLE(FrameBuffer);
IMPLEMENT_HANDLE(IndexBuffer);
IMPLEMENT_HANDLE(IndirectBuffer);
IMPLEMENT_HANDLE(OcclusionQuery);
IMPLEMENT_HANDLE(Program);
IMPLEMENT_HANDLE(Shader);
IMPLEMENT_HANDLE(Texture);
IMPLEMENT_HANDLE(Uniform);
IMPLEMENT_HANDLE(VertexBuffer);
IMPLEMENT_HANDLE(VertexLayout);
#undef CONVERT_HANDLE
const char* getTypeName(Handle _handle);
#undef IMPLEMENT_HANDLE
inline bool isValid(const VertexLayout& _layout)
{
@ -1785,8 +1867,8 @@ namespace bgfx
uint16_t m_depth;
uint8_t m_srcMip;
uint8_t m_dstMip;
TextureHandle m_src;
TextureHandle m_dst;
Handle m_src;
Handle m_dst;
};
struct IndexBuffer
@ -3232,7 +3314,7 @@ namespace bgfx
cmdbuf.write(_mem);
cmdbuf.write(_flags);
setDebugName(convert(handle) );
setDebugNameForHandle(handle);
}
else
{
@ -3251,7 +3333,7 @@ namespace bgfx
IndexBuffer& ref = m_indexBuffers[_handle.idx];
ref.m_name.set(_name);
setName(convert(_handle), _name);
setNameForHandle(_handle, _name);
}
BGFX_API_FUNC(void destroyIndexBuffer(IndexBufferHandle _handle) )
@ -3349,7 +3431,7 @@ namespace bgfx
cmdbuf.write(layoutHandle);
cmdbuf.write(_flags);
setDebugName(convert(handle) );
setDebugNameForHandle(handle);
return handle;
}
@ -3369,7 +3451,7 @@ namespace bgfx
VertexBuffer& ref = m_vertexBuffers[_handle.idx];
ref.m_name.set(_name);
setName(convert(_handle), _name);
setNameForHandle(_handle, _name);
}
BGFX_API_FUNC(void destroyVertexBuffer(VertexBufferHandle _handle) )
@ -3446,7 +3528,7 @@ namespace bgfx
cmdbuf.write(_size);
cmdbuf.write(_flags);
setDebugName(convert(indexBufferHandle), "Dynamic Index Buffer");
setDebugNameForHandle(indexBufferHandle, "Dynamic Index Buffer");
return uint64_t(indexBufferHandle.idx) << 32;
}
@ -3633,7 +3715,7 @@ namespace bgfx
cmdbuf.write(_size);
cmdbuf.write(_flags);
setDebugName(convert(vertexBufferHandle), "Dynamic Vertex Buffer");
setDebugNameForHandle(vertexBufferHandle, "Dynamic Vertex Buffer");
return uint64_t(vertexBufferHandle.idx)<<32;
}
@ -3832,7 +3914,7 @@ namespace bgfx
tib->size = _size;
tib->handle = handle;
setDebugName(convert(handle), "Transient Index Buffer");
setDebugNameForHandle(handle, "Transient Index Buffer");
}
return tib;
@ -3902,7 +3984,7 @@ namespace bgfx
tvb->handle = handle;
tvb->layoutHandle = layoutHandle;
setDebugName(convert(handle), "Transient Vertex Buffer");
setDebugNameForHandle(handle, "Transient Vertex Buffer");
}
return tvb;
@ -4133,7 +4215,7 @@ namespace bgfx
cmdbuf.write(handle);
cmdbuf.write(_mem);
setDebugName(convert(handle) );
setDebugNameForHandle(handle);
return handle;
}
@ -4157,12 +4239,12 @@ namespace bgfx
return sr.m_num;
}
void setName(Handle _handle, const bx::StringView& _name)
void setNameForHandle(Handle _handle, const bx::StringView& _name)
{
char tmp[1024];
uint16_t len = 1+(uint16_t)bx::snprintf(tmp, BX_COUNTOF(tmp)
, "%sH %d: %S"
, getTypeName(_handle)
, _handle.getTypeName().abrvName
, _handle.idx
, &_name
);
@ -4173,11 +4255,11 @@ namespace bgfx
cmdbuf.write(tmp, len);
}
void setDebugName(Handle _handle, const bx::StringView& _name = "")
void setDebugNameForHandle(Handle _handle, const bx::StringView& _name = "")
{
if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
{
setName(_handle, _name);
setNameForHandle(_handle, _name);
}
}
@ -4190,7 +4272,7 @@ namespace bgfx
ShaderRef& sr = m_shaderRef[_handle.idx];
sr.m_name.set(_name);
setName(convert(_handle), _name);
setNameForHandle(_handle, _name);
}
BGFX_API_FUNC(void destroyShader(ShaderHandle _handle) )
@ -4465,7 +4547,7 @@ namespace bgfx
cmdbuf.write(_flags);
cmdbuf.write(_skip);
setDebugName(convert(handle) );
setDebugNameForHandle(handle);
return handle;
}
@ -4478,7 +4560,7 @@ namespace bgfx
TextureRef& ref = m_textureRef[_handle.idx];
ref.m_name.set(_name);
setName(convert(_handle), _name);
setNameForHandle(_handle, _name);
}
void setDirectAccessPtr(TextureHandle _handle, void* _ptr)
@ -4726,7 +4808,7 @@ namespace bgfx
FrameBufferRef& fbr = m_frameBufferRef[_handle.idx];
fbr.m_name.set(_name);
// setName(convert(_handle), _name);
// setNameForHandle(_handle, _name);
}
BGFX_API_FUNC(TextureHandle getTexture(FrameBufferHandle _handle, uint8_t _attachment) )

View File

@ -6065,7 +6065,7 @@ namespace bgfx { namespace d3d12
{
const BlitItem& blit = _bs.advance();
TextureD3D12& src = m_textures[blit.m_src.idx];
TextureD3D12& src = m_textures[blit.m_src.idx];
const TextureD3D12& dst = m_textures[blit.m_dst.idx];
if (currentSrc.idx != blit.m_src.idx)
@ -6075,7 +6075,7 @@ namespace bgfx { namespace d3d12
m_textures[currentSrc.idx].setState(m_commandList, state);
}
currentSrc = blit.m_src;
currentSrc = blit.m_src.to<TextureHandle>();
if (NULL != src.m_singleMsaa)
{