refactor: externalize builtin forward-lit shader asset

This commit is contained in:
2026-04-02 23:04:59 +08:00
parent da2782c0c0
commit 307e8dbd0e
9 changed files with 362 additions and 0 deletions

View File

@@ -7,10 +7,13 @@
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <XCEngine/Resources/Shader/Shader.h>
#include <XCEngine/Resources/Shader/ShaderLoader.h>
#include <XCEngine/Resources/Texture/Texture.h>
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <system_error>
#include <vector>
namespace XCEngine {
@@ -36,6 +39,87 @@ struct MeshBuffers {
std::vector<Core::uint32> indices;
};
size_t CalculateBuiltinShaderMemorySize(const Shader& shader);
constexpr const char* kBuiltinForwardLitShaderManifestRelativePath =
"engine/assets/builtin/shaders/forward-lit/forward-lit.shader";
Containers::String NormalizeBuiltinAssetPath(const std::filesystem::path& path) {
return Containers::String(path.lexically_normal().generic_string().c_str());
}
bool TryResolveBuiltinAssetPathFromAnchor(
const std::filesystem::path& anchor,
const std::filesystem::path& relativePath,
std::filesystem::path& outPath) {
if (anchor.empty()) {
return false;
}
std::error_code ec;
std::filesystem::path current = anchor.lexically_normal();
if (std::filesystem::is_regular_file(current, ec)) {
current = current.parent_path();
}
while (!current.empty()) {
const std::filesystem::path candidate = (current / relativePath).lexically_normal();
if (std::filesystem::exists(candidate, ec)) {
outPath = candidate;
return true;
}
const std::filesystem::path parent = current.parent_path();
if (parent == current) {
break;
}
current = parent;
}
return false;
}
bool TryResolveBuiltinForwardLitShaderManifestPath(Containers::String& outPath) {
const std::filesystem::path relativePath(kBuiltinForwardLitShaderManifestRelativePath);
std::filesystem::path resolvedPath;
std::error_code ec;
if (TryResolveBuiltinAssetPathFromAnchor(std::filesystem::current_path(ec), relativePath, resolvedPath)) {
outPath = NormalizeBuiltinAssetPath(resolvedPath);
return true;
}
const Containers::String& resourceRoot = ResourceManager::Get().GetResourceRoot();
if (!resourceRoot.Empty() &&
TryResolveBuiltinAssetPathFromAnchor(std::filesystem::path(resourceRoot.CStr()), relativePath, resolvedPath)) {
outPath = NormalizeBuiltinAssetPath(resolvedPath);
return true;
}
if (TryResolveBuiltinAssetPathFromAnchor(std::filesystem::path(__FILE__), relativePath, resolvedPath)) {
outPath = NormalizeBuiltinAssetPath(resolvedPath);
return true;
}
return false;
}
Shader* LoadBuiltinShaderFromManifest(
const Containers::String& builtinPath,
const Containers::String& manifestPath) {
ShaderLoader shaderLoader;
LoadResult result = shaderLoader.Load(manifestPath);
if (!result || result.resource == nullptr) {
return nullptr;
}
auto* shader = static_cast<Shader*>(result.resource);
shader->m_path = builtinPath;
shader->m_guid = ResourceGUID::Generate(builtinPath);
shader->m_memorySize = CalculateBuiltinShaderMemorySize(*shader);
return shader;
}
const char kBuiltinForwardHlsl[] = R"(
Texture2D gBaseColorTexture : register(t1);
SamplerState gLinearSampler : register(s1);
@@ -1049,6 +1133,13 @@ size_t CalculateBuiltinShaderMemorySize(const Shader& shader) {
}
Shader* BuildBuiltinForwardLitShader(const Containers::String& path) {
Containers::String manifestPath;
if (TryResolveBuiltinForwardLitShaderManifestPath(manifestPath)) {
if (Shader* shader = LoadBuiltinShaderFromManifest(path, manifestPath)) {
return shader;
}
}
auto* shader = new Shader();
IResource::ConstructParams params;
params.name = Containers::String("Builtin Forward Lit");