载入中...
搜索中...
未找到
GraphicsFileTextures.cpp
浏览该文件的文档.
1#include <memory>
2#include "common/Exception.h"
3#include "common/Resource.h"
7#include "graphics/Graphics.h"
8#include "graphics/Texture.h"
9#include "image/Image.h"
10#include "image/ImageData.h"
11namespace eve::graphics {
12Texture* Graphics::newTextureFromFile(const std::string& filename) {
13 auto requested = requestFileTexture(filename);
14 if (!requested.ok()) throw eve::Exception("%s", requested.status().describe().c_str());
15 return &requested.value().get();
16}
17
18ResultRef<Texture> Graphics::loadTexture(const std::string& filename) {
19 try {
20 auto requested = requestFileTexture(filename);
21 if (!requested.ok()) return ResultRef<Texture>::failure(requested.status());
22 auto* texture = &requested.value().get();
23 if (texture->hasDeferredFilePixels()) ensureFileTexturesReady();
24 if (!texture->gpuHandle)
26 DiagnosticCode::Failed, "file texture did not become resident", {}, {}, "graphics.texture.load"));
27 return ResultRef<Texture>::success(std::ref(*texture));
28 } catch (const std::exception& error) {
30 Diagnostic::error(DiagnosticCode::Failed, error.what(), {}, {}, "graphics.texture.load"));
31 }
32}
34 if (deferredFileTextures_.empty() || realizingFileTextures_) return;
36 StartupStage stage("graphics: realize file textures");
37 struct Guard {
38 Graphics* g;
39 ~Guard() { g->realizingFileTextures_ = false; }
40 } guard{this};
41
42 std::vector<DeferredFileTexture> pending = std::move(deferredFileTextures_);
44
45 auto restoreUnrealized = [&]() {
46 for (const auto& item : pending) {
47 if (item.texture && item.texture->hasDeferredFilePixels()) deferredFileTextures_.push_back(item);
48 }
49 };
50
52 for (const auto& item : pending) {
53 auto waited = resources.waitFor(item.key);
54 if (!waited.ok()) {
55 restoreUnrealized();
56 throw eve::Exception("%s", waited.status().describe().c_str());
57 }
58 }
59
60 try {
61 for (const auto& item : pending) {
62 auto waited = resources.waitFor(item.key);
63 if (!waited.ok()) throw eve::Exception("%s", waited.status().describe().c_str());
64 auto* data = dynamic_cast<image::ImageData*>(&waited.value().get());
65 if (!data || !uploadDeferredFileTexture(item.texture, data))
66 throw eve::Exception("newTextureFromFile: GPU upload failed '%s'", item.key.c_str());
67 if (item.texture) item.texture->clearDeferredFilePixels();
68 }
69 } catch (...) {
70 restoreUnrealized();
71 throw;
72 }
73}
74
75Texture* Graphics::newTextureFromFileRepeated(const std::string& filename, bool repeatU, bool repeatV) {
76 if (filename.empty()) throw eve::Exception("newTextureFromFileRepeated: empty filename");
77 auto* fs = eve::filesystem::Filesystem::create();
78 std::unique_ptr<eve::filesystem::FileData> fileData(fs->read(filename));
79 if (!fileData) throw eve::Exception("newTextureFromFileRepeated: failed to read '%s'", filename.c_str());
80 auto* imgMod = eve::image::Image::create();
81 std::unique_ptr<eve::image::ImageData> data(imgMod->newImageData(fileData.get()));
82 return newTextureFromImageData(data.get(), repeatU, repeatV);
83}
84} // namespace eve::graphics
std::vector< QuestEvent > pending
tensor::Graph g
Definition GpuGraph.cpp:7
std::vector< std::weak_ptr< DeviceBytes > > resources
Definition OnnxGpgpu.cpp:55
std::string error
Definition Package.cpp:174
Texture * texture
bool repeatV
bool repeatU
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
static ResourceManager & getInstance()
Returns the instance.
Definition Resource.cpp:15
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
StartupStage public API.
ResultRef< Texture > loadTexture(const std::string &filename) override
Load a ready cached texture through the same file-resource owner as the legacy facade.
std::vector< DeferredFileTexture > deferredFileTextures_
Definition Graphics.h:2313
void ensureFileTexturesReady()
Finish CPU decode and GPU upload for outstanding newTextureFromFile results. @thread Game/render thre...
virtual ResultRef< Texture > requestFileTexture(const std::string &filename)=0
Canonical checked cache request; pixel realization is performed by loadTexture or legacy consumers....
Texture * newTextureFromFile(const std::string &filename) override
Load a path-cached file texture; cache hits return the same resource without I/O or upload....
Texture * newTextureFromImageData(image::ImageData *data, bool repeatU=false, bool repeatV=false)
Creates a texture from image data. @ownership Caller deletes unless documented otherwise.
Texture * newTextureFromFileRepeated(const std::string &filename, bool repeatU, bool repeatV)
Creates a texture from file repeated. @ownership Caller deletes unless documented otherwise.
virtual bool uploadDeferredFileTexture(Texture *texture, image::ImageData *data)
Uploads deferred file texture.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
Represents raw pixel data.
Definition ImageData.h:38
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25