错误处理

This commit is contained in:
2026-08-16 17:53:46 +08:00
parent 00a3c54820
commit 8612b5ff86
47 changed files with 744 additions and 544 deletions
+93 -24
View File
@@ -64,6 +64,52 @@ struct Type_Descriptor<renderive::render_3d::Renderable_Observation> {
namespace renderive::web {
namespace {
using namespace renderive::render_3d;
Gallery_Render_Error gallery_render_error(Frame_Request_Error error) {
switch (error) {
case Frame_Request_Error::none:
return Gallery_Render_Error::none;
case Frame_Request_Error::cancelled:
return Gallery_Render_Error::cancelled;
case Frame_Request_Error::deadline_exceeded:
return Gallery_Render_Error::deadline_exceeded;
case Frame_Request_Error::external_failure:
return Gallery_Render_Error::backend_failure;
case Frame_Request_Error::scene_shutting_down:
return Gallery_Render_Error::shutting_down;
default:
return Gallery_Render_Error::frame_unavailable;
}
}
Gallery_Render_Error gallery_render_error(Scene_Render_Error error) {
switch (error) {
case Scene_Render_Error::none:
return Gallery_Render_Error::none;
case Scene_Render_Error::cancelled:
return Gallery_Render_Error::cancelled;
case Scene_Render_Error::deadline_exceeded:
return Gallery_Render_Error::deadline_exceeded;
case Scene_Render_Error::external_failure:
return Gallery_Render_Error::backend_failure;
case Scene_Render_Error::shutting_down:
return Gallery_Render_Error::shutting_down;
}
throw std::logic_error("unknown scene render error");
}
Frame_Request_Error frame_request_error(Scene_Render_Error error) {
switch (error) {
case Scene_Render_Error::none:
return Frame_Request_Error::none;
case Scene_Render_Error::cancelled:
return Frame_Request_Error::cancelled;
case Scene_Render_Error::deadline_exceeded:
return Frame_Request_Error::deadline_exceeded;
case Scene_Render_Error::external_failure:
return Frame_Request_Error::external_failure;
case Scene_Render_Error::shutting_down:
return Frame_Request_Error::scene_shutting_down;
}
throw std::logic_error("unknown scene render error");
}
struct Gallery_Point {
double x{};
@@ -251,8 +297,12 @@ public:
void resize(int width, int height) override {
if (width <= 0 || height <= 0)
return;
scene_.resize({static_cast<std::uint32_t>(width),
static_cast<std::uint32_t>(height)});
const auto error = scene_.resize({static_cast<std::uint32_t>(width),
static_cast<std::uint32_t>(height)});
if (error != ::renderive::render_3d::Scene_Control_Error::none) {
last_event_ = "resize_rejected";
return;
}
last_event_ = "resized";
}
@@ -273,7 +323,9 @@ public:
input.buttons = pointer->buttons;
input.modifiers = static_cast<::renderive::Keyboard_Modifier>(
pointer->modifiers);
scene_.dispatch(input);
if (scene_.dispatch(input) !=
::renderive::render_3d::Scene_Control_Error::none)
last_event_ = "pointer_rejected";
return;
}
if (const auto* wheel = std::get_if<Gallery_Wheel_Input>(&event)) {
@@ -286,7 +338,9 @@ public:
input.angle_delta_y = wheel->angle_delta_y;
input.modifiers = static_cast<::renderive::Keyboard_Modifier>(
wheel->modifiers);
scene_.dispatch(input);
if (scene_.dispatch(input) !=
::renderive::render_3d::Scene_Control_Error::none)
last_event_ = "wheel_rejected";
return;
}
const auto& keyboard = std::get<Gallery_Key_Input>(event);
@@ -299,14 +353,17 @@ public:
input.modifiers = static_cast<::renderive::Keyboard_Modifier>(
keyboard.modifiers);
input.auto_repeat = keyboard.auto_repeat;
scene_.dispatch(input);
if (scene_.dispatch(input) !=
::renderive::render_3d::Scene_Control_Error::none)
last_event_ = "key_rejected";
}
bool render_latest_frame() override {
Gallery_Render_Error render_latest_frame() override {
if (!can_render_automatically())
return false;
return Gallery_Render_Error::inactive;
advance_blue_point(0.025F);
return render_request();
const auto request_error = render_request();
return gallery_render_error(request_error);
}
[[nodiscard]] std::optional<std::string> encode_latest_pixels() override {
@@ -377,17 +434,17 @@ public:
blue_offset_ = std::min(0.25F, blue_offset_ + 0.0125F);
publish_points();
}
const bool prepared = scene_.prepare_frame();
const bool prepared = scene_.prepare_frame() == Frame_Request_Error::none;
last_event_ = prepared ? "frame_prepared" : "prepare_rejected";
return prepared ? "3D frame prepared" : "3D frame prepare rejected";
}
if (request.id == "mode_refresh") {
const bool refreshed = scene_.refresh_manual_frame();
const bool refreshed = scene_.refresh_manual_frame() == Frame_Request_Error::none;
last_event_ = refreshed ? "manual_refreshed" : "manual_refresh_failed";
return refreshed ? "Manual 3D frame refreshed" : "No manual frame to refresh";
}
if (request.id == "mode_render" || request.id == "mode_dequeue") {
const bool rendered = render_prepared();
const bool rendered = render_prepared() == Frame_Request_Error::none;
last_event_ = rendered ? "frame_rendered" : "render_rejected";
return rendered ? "3D frame rendered" : "No 3D frame to render";
}
@@ -490,9 +547,7 @@ public:
{"queued", runtime.render_domain.queued},
{"peak_queued", runtime.render_domain.peak_queued},
{"backpressure_count", runtime.render_domain.backpressure_count},
{"backpressure_wait_ns", runtime.render_domain.backpressure_wait_ns},
{"unhandled_exception_count",
runtime.render_domain_unhandled_exception_count}}},
{"backpressure_wait_ns", runtime.render_domain.backpressure_wait_ns}}},
{"gpu_completion",
{{"capacity", runtime.gpu_completion.capacity},
{"active", runtime.gpu_completion.active},
@@ -560,7 +615,9 @@ private:
}
if (extra_attached_)
points.push_back({{0.0F, 0.62F, 0.0F}, {255, 230, 20, 255}, 58.0F});
visual_->update_points(std::move(points));
if (visual_->update_points(std::move(points)) !=
Visual_Data_Error::none)
throw std::logic_error("generated gallery point data is invalid");
}
void advance_blue_point(float step) {
@@ -571,38 +628,50 @@ private:
}
void render_initial_frame() {
Frame_Request_Error error{Frame_Request_Error::none};
if (frame_mode_ == Gallery_Frame_Mode::Manual) {
if (scene_.prepare_frame() && scene_.refresh_manual_frame())
render_prepared();
if (scene_.prepare_frame() == Frame_Request_Error::none &&
scene_.refresh_manual_frame() == Frame_Request_Error::none)
error = render_prepared();
else
error = Frame_Request_Error::no_pending_frame;
} else {
render_request();
error = render_request();
}
if (error != Frame_Request_Error::none)
last_action_result_ = "initial frame is not available";
}
bool render_request() {
Frame_Request_Error render_request() {
const auto started = std::chrono::steady_clock::now();
++render_attempt_count_;
const bool rendered = scene_.request_frame();
auto error = scene_.request_frame();
if (error == Frame_Request_Error::none)
error = frame_request_error(scene_.render_scene().wait_for_render());
const bool rendered = error == Frame_Request_Error::none;
last_render_ms_ = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - started)
.count();
successful_render_count_ += rendered ? 1U : 0U;
if (rendered)
record_sample(render_history_, std::chrono::steady_clock::now());
return rendered;
return error;
}
bool render_prepared() {
Frame_Request_Error render_prepared() {
const auto started = std::chrono::steady_clock::now();
++render_attempt_count_;
const bool rendered = scene_.render_prepared_frame();
auto error = scene_.render_prepared_frame();
if (error == Frame_Request_Error::none)
error = frame_request_error(scene_.render_scene().wait_for_render());
const bool rendered = error == Frame_Request_Error::none;
last_render_ms_ = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - started)
.count();
successful_render_count_ += rendered ? 1U : 0U;
if (rendered)
record_sample(render_history_, std::chrono::steady_clock::now());
return rendered;
return error;
}
std::uint64_t session_id_{};