The error channel¶
What it is¶
Every failure inside RI goes to one function, RI::reportError, with a category
(RI::ErrorCategory) that says what kind of failure occurred. What happens next (the
policy) is decided in one place, src/RI/Error.cpp, and nowhere else.
Why one channel? The error model is deliberately not decided yet: abort, exceptions, status codes, or a user callback are all possible, and they may differ per category. Keeping every failure site on one function means changing the model later touches one file, not every function in RI.
The categories¶
// <RI/RI.hpp>, namespace RI
enum class ErrorCategory : uint8 {
eProgrammerError, // the caller broke an RI rule
eUnsupported, // the GPU, driver or backend lacks a feature (P11)
eDeviceLost, // the GPU device was lost
eOutOfMemory, // a GPU or CPU allocation failed
eSwapchainOutOfDate, // the swapchain must be recreated
};
| Value | Meaning |
|---|---|
eProgrammerError |
the caller broke an RI rule |
eUnsupported |
the GPU, driver or backend lacks a feature (P11) |
eDeviceLost |
the GPU device was lost |
eOutOfMemory |
a GPU or CPU allocation failed |
eSwapchainOutOfDate |
the swapchain must be recreated |
RI::toString(category) returns the name printed in messages:
| Category | Printed as | Typical cause |
|---|---|---|
eProgrammerError |
programmer error |
You broke an RI rule: for example, passing a GPU that listGpus did not return, destroying a device twice, or triggering a validation-layer error. |
eUnsupported |
unsupported |
The GPU, driver, or backend lacks a required feature: for example, a Vulkan loader older than 1.3 or a missing validation layer in a validation build. |
eDeviceLost |
device lost |
The GPU stopped responding or was removed. |
eOutOfMemory |
out of memory |
A GPU or CPU allocation failed. |
eSwapchainOutOfDate |
swapchain out of date |
The window changed and the swapchain must be recreated. |
Today's policy: print and abort¶
For now, every category prints a message to standard error and aborts the program. The message names the category, the failure, and the source location that reported it:
[EquineRI] programmer error: the GPU 'Nonexistent GPU' is not available; pass a GPU returned by listGpus()
at <RI source file>:<line> (<RI function>)
Because the policy never returns, reportError is declared [[noreturn]]:
// <RI/RI.hpp>, namespace RI
[[noreturn]] void reportError(ErrorCategory category, std::string_view message,
std::source_location where = std::source_location::current());
From the header: Reports a failure. Today's policy never returns (P57 keeps signature changes acceptable while X1 is open).
Because where defaults to std::source_location::current(), it is filled in
automatically at the call site inside RI. As a result, the printed location points to the
RI function that failed (or its internal check), not to your own code. Run under a debugger
to see your own frame in the call stack.
Still open
The per-category policy is left open on purpose: it will be settled by using the API. Proposals on record are to keep aborting for programmer errors, unsupported features, and device loss, and to return a status for swapchain recreation, allocation failure, and pipeline creation (for shader hot reload). Signatures may change when that happens.
Where errors come from¶
- RI's own checks. For example, calling
createDevicewith a GPU that is not available, or callingdestroyDeviceon a device that was already destroyed, reports a programmer error. -
Native results. When a Vulkan (
VkResult) or D3D12 (HRESULT) call fails, RI maps its result code to a category:Native result Category VK_ERROR_OUT_OF_HOST_MEMORY,VK_ERROR_OUT_OF_DEVICE_MEMORY,VK_ERROR_OUT_OF_POOL_MEMORY,VK_ERROR_FRAGMENTED_POOL,VK_ERROR_FRAGMENTATION;E_OUTOFMEMORYout of memory VK_ERROR_DEVICE_LOST;DXGI_ERROR_DEVICE_REMOVED,DXGI_ERROR_DEVICE_HUNG,DXGI_ERROR_DEVICE_RESET,DXGI_ERROR_DRIVER_INTERNAL_ERRORdevice lost VK_ERROR_OUT_OF_DATE_KHRswapchain out of date VK_ERROR_INITIALIZATION_FAILED,VK_ERROR_LAYER_NOT_PRESENT,VK_ERROR_EXTENSION_NOT_PRESENT,VK_ERROR_FEATURE_NOT_PRESENT,VK_ERROR_INCOMPATIBLE_DRIVER,VK_ERROR_FORMAT_NOT_SUPPORTED;DXGI_ERROR_UNSUPPORTED,DXGI_ERROR_SDK_COMPONENT_MISSING,E_NOTIMPLunsupported anything else programmer error The message names the native call, its result, and its numeric code, for example
vkCreateDevice failed: VK_ERROR_FEATURE_NOT_PRESENT (-8). -
Validation messages. In validation builds, validation errors from the Vulkan validation layer and error or corruption messages from the D3D12 debug layer become programmer errors. Warnings (and non-validation Vulkan messages, such as loader errors) are only printed to standard error. See Validation.
Using the channel in your own helpers¶
RI::reportError is public. A helper you write on top of RI can report failures through
the same channel, so one policy covers both RI and your own code:
// A user helper: picks the first GPU with at least `minimum` bytes of memory.
RI::Gpu pickGpuWithMemory(const std::vector<RI::Gpu> &gpus, uint64 minimum) {
for (const RI::Gpu &gpu : gpus) {
if (gpu.memory >= minimum) {
return gpu;
}
}
RI::reportError(RI::ErrorCategory::eUnsupported, "no GPU has enough memory for this program");
}
How native APIs report errors¶
Without RI, each API has its own way, and checking every result is up to you.
// A user helper: picks the first GPU with at least `minimum` bytes of memory.
RI::Gpu pickGpuWithMemory(const std::vector<RI::Gpu> &gpus, uint64 minimum) {
for (const RI::Gpu &gpu : gpus) {
if (gpu.memory >= minimum) {
return gpu;
}
}
RI::reportError(RI::ErrorCategory::eUnsupported, "no GPU has enough memory for this program");
}
// Negative VkResult values are errors; positive ones (VK_INCOMPLETE, ...) are
// successes with extra information.
void check(VkResult result, const char *call) {
if (result < 0) {
std::fprintf(stderr, "%s failed: VkResult %d\n", call, static_cast<int>(result));
std::abort();
}
}
// The validation layer reports mistakes here, not through VkResult.
VKAPI_ATTR VkBool32 VKAPI_CALL onMessage(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
VkDebugUtilsMessageTypeFlagsEXT /*types*/,
const VkDebugUtilsMessengerCallbackDataEXT *data, void * /*user*/) {
if ((severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
std::fprintf(stderr, "validation error: %s\n", data->pMessage);
}
return VK_FALSE; // never VK_TRUE: that would make the call fail
}
// Passing the messenger's create info in the instance's pNext also catches
// messages from vkCreateInstance itself.
const VkDebugUtilsMessengerCreateInfoEXT messengerInfo{
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT,
.pfnUserCallback = &onMessage,
};
const char *const layers[] = {"VK_LAYER_KHRONOS_validation"};
const char *const extensions[] = {VK_EXT_DEBUG_UTILS_EXTENSION_NAME};
const VkApplicationInfo appInfo{.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .apiVersion = VK_API_VERSION_1_3};
const VkInstanceCreateInfo instanceInfo{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = &messengerInfo,
.pApplicationInfo = &appInfo,
.enabledLayerCount = 1,
.ppEnabledLayerNames = layers,
.enabledExtensionCount = 1,
.ppEnabledExtensionNames = extensions,
};
VkInstance instance = VK_NULL_HANDLE;
check(vkCreateInstance(&instanceInfo, nullptr, &instance), "vkCreateInstance");
// FAILED() is true for negative HRESULTs. After DXGI_ERROR_DEVICE_REMOVED,
// GetDeviceRemovedReason says why (hang, reset, driver error).
void check(HRESULT result, const char *call) {
if (FAILED(result)) {
std::fprintf(stderr, "%s failed: HRESULT 0x%08lX\n", call, static_cast<unsigned long>(result));
std::abort();
}
}
// The debug layer reports mistakes here, not through HRESULTs.
void CALLBACK onMessage(D3D12_MESSAGE_CATEGORY /*category*/, D3D12_MESSAGE_SEVERITY severity, D3D12_MESSAGE_ID /*id*/,
LPCSTR description, void * /*context*/) {
if (severity == D3D12_MESSAGE_SEVERITY_ERROR || severity == D3D12_MESSAGE_SEVERITY_CORRUPTION) {
std::fprintf(stderr, "debug layer error: %s\n", description);
}
}
ComPtr<ID3D12Debug> debug;
check(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)), "D3D12GetDebugInterface");
debug->EnableDebugLayer(); // before creating the device
ComPtr<ID3D12Device> device;
check(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&device)), "D3D12CreateDevice");
ComPtr<ID3D12InfoQueue1> infoQueue; // Agility SDK debug layer
DWORD cookie = 0;
if (SUCCEEDED(device.As(&infoQueue))) {
check(infoQueue->RegisterMessageCallback(&onMessage, D3D12_MESSAGE_CALLBACK_FLAG_NONE, nullptr, &cookie),
"RegisterMessageCallback");
}