mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-05-28 18:21:22 +00:00
Dawn used to return "nullptr" when an error happened while creating an object. To match WebGPU we want to return valid pointers but to "error" objects. This commit implements WebGPU error handling for all "descriptorized" objects and changes the nullptr error checks into "ValidateObject" checks. This method is used both to check that the object isn't an error, but also that all objects in a function call are from the same device. New validation is added to objects with methods (apart from Device) so they check they aren't error objects. A large number of ASSERTs were added to check that frontend objects aren't accessed when they are errors, so that missing validation would hit the asserts instead of crashing randomly. The bind group validation tests were modified to test the behavior with both nullptrs and error objects. Future commits will change the CommandBufferBuilder to not be a builder anymore but an "Encoder" instead with special-cased error handling. Then once all objects are descriptorized, the notion of builders will be removed from the code. BUG=dawn:8 Change-Id: I8647712d5de3deb0e99e3bc58f34496f67710edd Reviewed-on: https://dawn-review.googlesource.com/c/4360 Reviewed-by: Kai Ninomiya <kainino@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
99 lines
4.7 KiB
C++
99 lines
4.7 KiB
C++
// Copyright 2018 The Dawn Authors
|
|
//
|
|
// 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.
|
|
|
|
#ifndef DAWNNATIVE_ERROR_H_
|
|
#define DAWNNATIVE_ERROR_H_
|
|
|
|
#include "common/Result.h"
|
|
|
|
#include <string>
|
|
|
|
namespace dawn_native {
|
|
|
|
// This is the content of an error value for MaybeError or ResultOrError, split off to its own
|
|
// file to avoid having all files including headers like <string> and <vector>
|
|
class ErrorData;
|
|
|
|
enum class ErrorType : uint32_t { Validation, ContextLost, Unimplemented };
|
|
|
|
// 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.
|
|
using MaybeError = Result<void, ErrorData*>;
|
|
|
|
template <typename T>
|
|
using ResultOrError = Result<T, ErrorData*>;
|
|
|
|
// Returning a success is done like so:
|
|
// return {}; // for Error
|
|
// return SomethingOfTypeT; // for ResultOrError<T>
|
|
//
|
|
// Returning an error is done via:
|
|
// return DAWN_MAKE_ERROR(errorType, "My error message");
|
|
//
|
|
// but shorthand version for specific error types are preferred:
|
|
// return DAWN_VALIDATION_ERROR("My error message");
|
|
#define DAWN_MAKE_ERROR(TYPE, MESSAGE) \
|
|
::dawn_native::MakeError(TYPE, MESSAGE, __FILE__, __func__, __LINE__)
|
|
#define DAWN_VALIDATION_ERROR(MESSAGE) DAWN_MAKE_ERROR(ErrorType::Validation, MESSAGE)
|
|
#define DAWN_CONTEXT_LOST_ERROR(MESSAGE) DAWN_MAKE_ERROR(ErrorType::ContextLost, MESSAGE)
|
|
#define DAWN_UNIMPLEMENTED_ERROR(MESSAGE) DAWN_MAKE_ERROR(ErrorType::Unimplemented, MESSAGE)
|
|
|
|
#define DAWN_CONCAT1(x, y) x##y
|
|
#define DAWN_CONCAT2(x, y) DAWN_CONCAT1(x, y)
|
|
#define DAWN_LOCAL_VAR DAWN_CONCAT2(_localVar, __LINE__)
|
|
|
|
// When Errors aren't handled explicitly, calls to functions returning errors should be
|
|
// wrapped in an DAWN_TRY. It will return the error if any, otherwise keep executing
|
|
// the current function.
|
|
#define DAWN_TRY(EXPR) \
|
|
{ \
|
|
auto DAWN_LOCAL_VAR = EXPR; \
|
|
if (DAWN_UNLIKELY(DAWN_LOCAL_VAR.IsError())) { \
|
|
ErrorData* error = DAWN_LOCAL_VAR.AcquireError(); \
|
|
::dawn_native::AppendBacktrace(error, __FILE__, __func__, __LINE__); \
|
|
return {std::move(error)}; \
|
|
} \
|
|
} \
|
|
for (;;) \
|
|
break
|
|
|
|
// DAWN_TRY_ASSIGN is the same as DAWN_TRY for ResultOrError and assigns the success value, if
|
|
// any, to VAR.
|
|
#define DAWN_TRY_ASSIGN(VAR, EXPR) \
|
|
{ \
|
|
auto DAWN_LOCAL_VAR = EXPR; \
|
|
if (DAWN_UNLIKELY(DAWN_LOCAL_VAR.IsError())) { \
|
|
ErrorData* error = DAWN_LOCAL_VAR.AcquireError(); \
|
|
::dawn_native::AppendBacktrace(error, __FILE__, __func__, __LINE__); \
|
|
return {std::move(error)}; \
|
|
} \
|
|
VAR = DAWN_LOCAL_VAR.AcquireSuccess(); \
|
|
} \
|
|
for (;;) \
|
|
break
|
|
|
|
// Implementation detail of DAWN_TRY and DAWN_TRY_ASSIGN's adding to the Error's backtrace.
|
|
void AppendBacktrace(ErrorData* error, const char* file, const char* function, int line);
|
|
|
|
// Implementation detail of DAWN_MAKE_ERROR
|
|
ErrorData* MakeError(ErrorType type,
|
|
std::string message,
|
|
const char* file,
|
|
const char* function,
|
|
int line);
|
|
|
|
} // namespace dawn_native
|
|
|
|
#endif // DAWNNATIVE_ERROR_H_
|