Crash on Windows when creating an Index-Buffer of Size 0
I noticed that when creating an Index-Buffer of size 0, bs:f crashes with a rather inconvenient error message in the D3D11-Code, in the constructor of D3D11HardwareBuffer:
HRESULT hr = device.getD3D11Device()->CreateBuffer(&mDesc, nullptr, &mD3DBuffer);
if (FAILED(hr) || mDevice.hasError())
{
String msg = device.getErrorDescription();
BS_EXCEPT(RenderingAPIException, "Cannot create D3D11 buffer: " + msg);
}
The error description will be empty.
I think it would be nice if bs:f would check for such a malformed buffer desc and give some details.
Stacktrace:
A fatal error occurred and the program has to terminate!
- Error: RenderingAPIException
- Description: Cannot create D3D11 buffer:
- In function: __cdecl bs::ct::D3D11HardwareBuffer::D3D11HardwareBuffer(enum bs::ct::D3D11HardwareBuffer::BufferType,enum bs::GpuBufferUsage,unsigned int,unsigned int,class bs::ct::D3D11Device &,bool,bool)
- In file: F:\Code\bsfExamples\bsf\Source\Plugins\bsfD3D11RenderAPI\BsD3D11HardwareBuffer.cpp:114
Stack trace:
bs::CrashHandler::reportCrash() - 0x140709769283728 File[bswin32crashhandler.cpp:468 (0)] Module[bsf.dll]
bs::ct::D3D11HardwareBuffer::D3D11HardwareBuffer() - 0x140710836767412 File[bsd3d11hardwarebuffer.cpp:114 (116)] Module[bsfD3D11RenderAPI.dll]
bs::ct::D3D11IndexBuffer::initialize() - 0x140710836707373 File[bsd3d11indexbuffer.cpp:23 (0)] Module[bsfD3D11RenderAPI.dll]
bs::ct::Mesh::initialize() - 0x140709772077878 File[bsmesh.cpp:290 (3)] Module[bsf.dll]
bs::CoreObject::executeGpuCommand() - 0x140709769387572 File[bscoreobject.cpp:148 (22)] Module[bsf.dll]
std::_Func_impl_no_alloc const &,std::function),std::shared_ptr const &,std::function &>,void>::_Do_call() - 0x140709769389478 File[functional:15732480 (0)] Module[bsf.dll]
bs::CommandQueueBase::playbackWithNotify() - 0x140709769377752 File[bscommandqueue.cpp:127 (0)] Module[bsf.dll]
bs::CoreThread::runCoreThread() - 0x140709769423514 File[bscorethread.cpp:118 (0)] Module[bsf.dll]
bs::PooledThread::run() - 0x140709768959742 File[bsthreadpool.cpp:115 (6)] Module[bsf.dll]
std::_Pad::_Call_func() - 0x140709768957847 File[xthread:209 (11)] Module[bsf.dll]
0x140711001441486 Module[ucrtbase.dll]
0x140711012741172 Module[KERNEL32.DLL]
0x140711055799953 Module[ntdll.dll]
If you are in debug mode you should get the following error message: ID3D11Device::CreateBuffer: The Dimensions are invalid. ByteWidth cannot be 0..
I'm assuming you are running in release build, which is intended to be the final build to be sent to the user, with minimal error checks for performance reasons. If debug mode perfomance is an issue, you should also be able to use the RelWithDebInfo configuration to get the debug information.
I've submitted a change that will report an error message more specific to the index buffer, 6f7e0c76e1a93107057aec34405aed6cab753301. Note it will still only trigger in debug mode.
Looks good.
How about letting device.getErrorDescription(); output something other than an empty string in release configurations? Like, <Error description not available in Release-mode> or something along those lines?
I might be more inclined to just remove those checks altogether in release mode. But neither is high on my priority list right now, so if you want to submit a PR with either approach it would be welcome.