载入中...
搜索中...
未找到
GraphicsResourceUpdates.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cstring>
3#include <memory>
4#include "common/Exception.h"
5#include "common/Resource.h"
7#include "graphics/Mesh.h"
8#include "graphics/Texture.h"
11#include "image/Image.h"
12#include "image/ImageData.h"
13namespace eve::graphics::vulkan {
14ResultRef<Texture> Graphics::requestFileTexture(const std::string &filename) {
15 if (filename.empty())
17 DiagnosticCode::InvalidArgument, "file texture path is empty", {}, {}, "graphics.texture.request"));
18 try {
19 const std::string key = normalizeTexPath(filename);
20 auto it = texturesByPath.find(key);
21 if (it != texturesByPath.end() && it->second) return ResultRef<Texture>::success(std::ref(*it->second));
24 "file texture source is absent: " + filename, {}, {},
25 "graphics.texture.request"));
27 auto texture = std::make_unique<Texture>();
28 texture->markDeferredFilePixels(this);
29 Texture *raw = texture.get();
30 DeferredFileTexture pending{key, raw};
31 if (ownedTextures.size() == ownedTextures.capacity())
32 ownedTextures.reserve(std::max(size_t(1), ownedTextures.capacity() * 2));
33 if (deferredFileTextures_.size() == deferredFileTextures_.capacity())
34 deferredFileTextures_.reserve(std::max(size_t(1), deferredFileTextures_.capacity() * 2));
35 texturesByPath.insert_or_assign(key, raw);
36 ownedTextures.push_back(std::move(texture));
37 deferredFileTextures_.push_back(std::move(pending));
38 return ResultRef<Texture>::success(std::ref(*raw));
39 } catch (const std::exception &error) {
41 Diagnostic::error(DiagnosticCode::Failed, error.what(), {}, {}, "graphics.texture.request"));
42 }
43}
44
48
49bool Graphics::reloadTextureFromFile(const std::string &filename) {
50 if (filename.empty()) return false;
51 const std::string key = normalizeTexPath(filename);
52 auto it = texturesByPath.find(key);
53 if (it == texturesByPath.end() || !it->second) return false;
54
56 applyPendingResourceChanges();
57 // The provider hands back a cache-owned ImageData; the pin keeps it alive until
58 // the pixels have been copied out of it.
59 image::ImageData *data = nullptr;
60 eve::ResourcePin keepAlive;
61 try {
62 auto reloaded = eve::ResourceManager::getInstance().reload(filename);
63 if (!reloaded.ok() || !reloaded.value()) return false;
64 auto *imgMod = image::Image::create();
65 data = imgMod->newImageDataFromFile(filename);
66 if (data != nullptr) {
68 if (!pinned.ok()) return false;
69 keepAlive = std::move(pinned).takeValue();
70 // The pin is the authority from here on; the borrowed pointer may have gone
71 // stale before the pin was taken.
72 data = static_cast<image::ImageData *>(keepAlive.get());
73 }
74 } catch (...) {
75 return false;
76 }
77 if (!data) return false;
78 return replaceTexturePixels(it->second, data);
79}
80} // namespace eve::graphics::vulkan
std::vector< QuestEvent > pending
SQObjectPtr key
Definition ECS.cpp:155
double raw
eve::ResourcePin pinned
std::string error
Definition Package.cpp:174
Texture * texture
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
eve::Result< ResourcePin > pin(Resource *resource)
Keeps one cached resource alive across unload()/clear()/reload.
Definition Resource.cpp:311
eve::Result< bool > reload(const std::string &normPath) override
Reloads .
Definition Resource.cpp:390
static ResourceManager & getInstance()
Returns the instance.
Definition Resource.cpp:15
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
std::vector< DeferredFileTexture > deferredFileTextures_
Definition Graphics.h:2313
bool fileTextureSourceExists(const std::string &filename) const
File texture source exists.
void ensureFileTexturesReady()
Finish CPU decode and GPU upload for outstanding newTextureFromFile results. @thread Game/render thre...
void requestFileImageDecode(const std::string &key)
Request file image decode.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
bool uploadDeferredFileTexture(Texture *texture, image::ImageData *data) override
Uploads deferred file texture.
bool reloadTextureFromFile(const std::string &filename) override
Creates a texture from file. @ownership Caller deletes unless documented otherwise.
bool replaceTexturePixels(Texture *tex, image::ImageData *data)
Replace texture pixels.
Represents raw pixel data.
Definition ImageData.h:38
RAII keep-alive for one RuntimeObjectRegistry entry.
T * get() const noexcept
Borrows the pinned object; valid until the pin is released.