2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2018 The Dawn Authors
|
2018-05-25 20:23:44 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-24 14:42:33 +00:00
|
|
|
#ifndef DAWNNATIVE_ERROR_H_
|
|
|
|
#define DAWNNATIVE_ERROR_H_
|
2018-05-25 20:23:44 +00:00
|
|
|
|
|
|
|
#include "common/Result.h"
|
2020-01-10 17:58:28 +00:00
|
|
|
#include "dawn_native/ErrorData.h"
|
2018-05-25 20:23:44 +00:00
|
|
|
|
2018-12-03 12:49:56 +00:00
|
|
|
#include <string>
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2018-05-25 20:23:44 +00:00
|
|
|
|
2020-04-01 12:07:43 +00:00
|
|
|
enum class InternalErrorType : uint32_t {
|
|
|
|
Validation,
|
|
|
|
DeviceLost,
|
|
|
|
Internal,
|
|
|
|
OutOfMemory
|
|
|
|
};
|
2018-09-10 14:17:24 +00:00
|
|
|
|
2018-05-25 20:23:44 +00:00
|
|
|
// MaybeError and ResultOrError are meant to be used as return value for function that are not
|
|
|
|
// expected to, but might fail. The handling of error is potentially much slower than successes.
|
2020-01-10 17:58:28 +00:00
|
|
|
using MaybeError = Result<void, ErrorData>;
|
2018-05-25 20:23:44 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2020-01-10 17:58:28 +00:00
|
|
|
using ResultOrError = Result<T, ErrorData>;
|
2018-05-25 20:23:44 +00:00
|
|
|
|
|
|
|
// Returning a success is done like so:
|
|
|
|
// return {}; // for Error
|
|
|
|
// return SomethingOfTypeT; // for ResultOrError<T>
|
|
|
|
//
|
|
|
|
// Returning an error is done via:
|
2018-09-10 14:17:24 +00:00
|
|
|
// return DAWN_MAKE_ERROR(errorType, "My error message");
|
|
|
|
//
|
|
|
|
// but shorthand version for specific error types are preferred:
|
|
|
|
// return DAWN_VALIDATION_ERROR("My error message");
|
2020-04-01 12:07:43 +00:00
|
|
|
//
|
|
|
|
// There are different types of errors that should be used for different purpose:
|
|
|
|
//
|
|
|
|
// - Validation: these are errors that show the user did something bad, which causes the
|
|
|
|
// whole call to be a no-op. It's most commonly found in the frontend but there can be some
|
|
|
|
// backend specific validation in non-conformant backends too.
|
|
|
|
//
|
|
|
|
// - Out of memory: creation of a Buffer or Texture failed because there isn't enough memory.
|
|
|
|
// This is similar to validation errors in that the call becomes a no-op and returns an
|
|
|
|
// error object, but is reported separated from validation to the user.
|
|
|
|
//
|
|
|
|
// - Device loss: the backend driver reported that the GPU has been lost, which means all
|
|
|
|
// previous commands magically disappeared and the only thing left to do is clean up.
|
|
|
|
// Note: Device loss should be used rarely and in most case you want to use Internal
|
|
|
|
// instead.
|
|
|
|
//
|
|
|
|
// - Internal: something happened that the backend didn't expect, and it doesn't know
|
|
|
|
// how to recover from that situation. This causes the device to be lost, but is separate
|
|
|
|
// from device loss, because the GPU execution is still happening so we need to clean up
|
|
|
|
// more gracefully.
|
|
|
|
//
|
|
|
|
// - Unimplemented: same as Internal except it puts "unimplemented" in the error message for
|
|
|
|
// more clarity.
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
#define DAWN_MAKE_ERROR(TYPE, MESSAGE) \
|
2020-01-10 17:58:28 +00:00
|
|
|
::dawn_native::ErrorData::Create(TYPE, MESSAGE, __FILE__, __func__, __LINE__)
|
2021-01-13 17:53:29 +00:00
|
|
|
|
2019-08-27 21:41:56 +00:00
|
|
|
#define DAWN_VALIDATION_ERROR(MESSAGE) DAWN_MAKE_ERROR(InternalErrorType::Validation, MESSAGE)
|
2021-01-13 17:53:29 +00:00
|
|
|
|
|
|
|
// DAWN_DEVICE_LOST_ERROR means that there was a real unrecoverable native device lost error.
|
|
|
|
// We can't even do a graceful shutdown because the Device is gone.
|
2019-08-27 21:41:56 +00:00
|
|
|
#define DAWN_DEVICE_LOST_ERROR(MESSAGE) DAWN_MAKE_ERROR(InternalErrorType::DeviceLost, MESSAGE)
|
2021-01-13 17:53:29 +00:00
|
|
|
|
|
|
|
// DAWN_INTERNAL_ERROR means Dawn hit an unexpected error in the backend and should try to
|
|
|
|
// gracefully shut down.
|
2020-04-01 12:07:43 +00:00
|
|
|
#define DAWN_INTERNAL_ERROR(MESSAGE) DAWN_MAKE_ERROR(InternalErrorType::Internal, MESSAGE)
|
2021-01-13 17:53:29 +00:00
|
|
|
|
2020-04-01 12:07:43 +00:00
|
|
|
#define DAWN_UNIMPLEMENTED_ERROR(MESSAGE) \
|
|
|
|
DAWN_MAKE_ERROR(InternalErrorType::Internal, std::string("Unimplemented: ") + MESSAGE)
|
2021-01-13 17:53:29 +00:00
|
|
|
|
|
|
|
// DAWN_OUT_OF_MEMORY_ERROR means we ran out of memory. It may be used as a signal internally in
|
|
|
|
// Dawn to free up unused resources. Or, it may bubble up to the application to signal an allocation
|
|
|
|
// was too large or they should free some existing resources.
|
2019-08-27 21:41:56 +00:00
|
|
|
#define DAWN_OUT_OF_MEMORY_ERROR(MESSAGE) DAWN_MAKE_ERROR(InternalErrorType::OutOfMemory, MESSAGE)
|
2018-05-25 20:23:44 +00:00
|
|
|
|
2018-07-18 09:45:17 +00:00
|
|
|
#define DAWN_CONCAT1(x, y) x##y
|
|
|
|
#define DAWN_CONCAT2(x, y) DAWN_CONCAT1(x, y)
|
|
|
|
#define DAWN_LOCAL_VAR DAWN_CONCAT2(_localVar, __LINE__)
|
2018-05-25 20:23:44 +00:00
|
|
|
|
|
|
|
// When Errors aren't handled explicitly, calls to functions returning errors should be
|
2018-07-18 09:45:17 +00:00
|
|
|
// wrapped in an DAWN_TRY. It will return the error if any, otherwise keep executing
|
2018-05-25 20:23:44 +00:00
|
|
|
// the current function.
|
2020-01-10 17:58:28 +00:00
|
|
|
#define DAWN_TRY(EXPR) \
|
|
|
|
{ \
|
|
|
|
auto DAWN_LOCAL_VAR = EXPR; \
|
|
|
|
if (DAWN_UNLIKELY(DAWN_LOCAL_VAR.IsError())) { \
|
|
|
|
std::unique_ptr<::dawn_native::ErrorData> error = DAWN_LOCAL_VAR.AcquireError(); \
|
|
|
|
error->AppendBacktrace(__FILE__, __func__, __LINE__); \
|
|
|
|
return {std::move(error)}; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
for (;;) \
|
2018-05-25 20:23:44 +00:00
|
|
|
break
|
|
|
|
|
2018-07-18 09:45:17 +00:00
|
|
|
// DAWN_TRY_ASSIGN is the same as DAWN_TRY for ResultOrError and assigns the success value, if
|
2018-05-25 20:23:44 +00:00
|
|
|
// any, to VAR.
|
2020-01-10 17:58:28 +00:00
|
|
|
#define DAWN_TRY_ASSIGN(VAR, EXPR) \
|
|
|
|
{ \
|
|
|
|
auto DAWN_LOCAL_VAR = EXPR; \
|
|
|
|
if (DAWN_UNLIKELY(DAWN_LOCAL_VAR.IsError())) { \
|
|
|
|
std::unique_ptr<ErrorData> error = DAWN_LOCAL_VAR.AcquireError(); \
|
|
|
|
error->AppendBacktrace(__FILE__, __func__, __LINE__); \
|
|
|
|
return {std::move(error)}; \
|
|
|
|
} \
|
|
|
|
VAR = DAWN_LOCAL_VAR.AcquireSuccess(); \
|
|
|
|
} \
|
|
|
|
for (;;) \
|
2018-05-25 20:23:44 +00:00
|
|
|
break
|
|
|
|
|
2020-01-15 19:02:13 +00:00
|
|
|
// Assert that errors are device loss so that we can continue with destruction
|
2020-06-16 17:53:38 +00:00
|
|
|
void IgnoreErrors(MaybeError maybeError);
|
2020-01-15 19:02:13 +00:00
|
|
|
|
2020-04-01 12:07:43 +00:00
|
|
|
wgpu::ErrorType ToWGPUErrorType(InternalErrorType type);
|
|
|
|
InternalErrorType FromWGPUErrorType(wgpu::ErrorType type);
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|
2018-05-25 20:23:44 +00:00
|
|
|
|
2018-07-24 14:42:33 +00:00
|
|
|
#endif // DAWNNATIVE_ERROR_H_
|