resources: share shader source parsing utilities

This commit is contained in:
2026-04-07 11:16:02 +08:00
parent 0d6a4113e7
commit 1c87650fb3
5 changed files with 279 additions and 422 deletions

View File

@@ -1,9 +1,6 @@
#include "ShaderAuthoringParser.h"
#include "ShaderSourceUtils.h"
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <cctype>
#include <filesystem>
#include <sstream>
#include <string>
#include <unordered_set>
@@ -12,198 +9,6 @@
namespace XCEngine {
namespace Resources {
std::string ToStdString(const Containers::String& value) {
return std::string(value.CStr());
}
Containers::String NormalizePathString(const std::filesystem::path& path) {
return Containers::String(path.lexically_normal().generic_string().c_str());
}
size_t SkipWhitespace(const std::string& text, size_t pos) {
while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos])) != 0) {
++pos;
}
return pos;
}
std::string TrimCopy(const std::string& text) {
const size_t first = SkipWhitespace(text, 0);
if (first >= text.size()) {
return std::string();
}
size_t last = text.size();
while (last > first && std::isspace(static_cast<unsigned char>(text[last - 1])) != 0) {
--last;
}
return text.substr(first, last - first);
}
bool ParseQuotedString(
const std::string& text,
size_t quotePos,
Containers::String& outValue,
size_t* nextPos = nullptr) {
if (quotePos >= text.size() || text[quotePos] != '"') {
return false;
}
std::string parsed;
++quotePos;
while (quotePos < text.size()) {
const char ch = text[quotePos];
if (ch == '\\') {
if (quotePos + 1 >= text.size()) {
return false;
}
parsed.push_back(text[quotePos + 1]);
quotePos += 2;
continue;
}
if (ch == '"') {
outValue = parsed.c_str();
if (nextPos != nullptr) {
*nextPos = quotePos + 1;
}
return true;
}
parsed.push_back(ch);
++quotePos;
}
return false;
}
bool TryParseShaderLanguage(const Containers::String& value, ShaderLanguage& outLanguage) {
const Containers::String normalized = value.Trim().ToLower();
if (normalized == "glsl") {
outLanguage = ShaderLanguage::GLSL;
return true;
}
if (normalized == "hlsl") {
outLanguage = ShaderLanguage::HLSL;
return true;
}
if (normalized == "spirv" || normalized == "spv") {
outLanguage = ShaderLanguage::SPIRV;
return true;
}
return false;
}
bool TryParseShaderBackend(const Containers::String& value, ShaderBackend& outBackend) {
const Containers::String normalized = value.Trim().ToLower();
if (normalized == "generic") {
outBackend = ShaderBackend::Generic;
return true;
}
if (normalized == "d3d12" || normalized == "dx12") {
outBackend = ShaderBackend::D3D12;
return true;
}
if (normalized == "opengl" || normalized == "gl") {
outBackend = ShaderBackend::OpenGL;
return true;
}
if (normalized == "vulkan" || normalized == "vk") {
outBackend = ShaderBackend::Vulkan;
return true;
}
return false;
}
bool TryParseShaderPropertyType(const Containers::String& value, ShaderPropertyType& outType) {
const Containers::String normalized = value.Trim().ToLower();
if (normalized == "float") {
outType = ShaderPropertyType::Float;
return true;
}
if (normalized == "range") {
outType = ShaderPropertyType::Range;
return true;
}
if (normalized == "int" || normalized == "integer") {
outType = ShaderPropertyType::Int;
return true;
}
if (normalized == "vector" || normalized == "float4") {
outType = ShaderPropertyType::Vector;
return true;
}
if (normalized == "color") {
outType = ShaderPropertyType::Color;
return true;
}
if (normalized == "2d" || normalized == "texture2d" || normalized == "texture") {
outType = ShaderPropertyType::Texture2D;
return true;
}
if (normalized == "cube" || normalized == "cubemap" || normalized == "texturecube") {
outType = ShaderPropertyType::TextureCube;
return true;
}
return false;
}
bool TryParseShaderResourceType(const Containers::String& value, ShaderResourceType& outType) {
const Containers::String normalized = value.Trim().ToLower();
if (normalized == "constantbuffer" || normalized == "cbuffer" || normalized == "cbv") {
outType = ShaderResourceType::ConstantBuffer;
return true;
}
if (normalized == "texture2d" || normalized == "texture" || normalized == "srvtexture2d") {
outType = ShaderResourceType::Texture2D;
return true;
}
if (normalized == "texturecube" || normalized == "cubemap") {
outType = ShaderResourceType::TextureCube;
return true;
}
if (normalized == "sampler" || normalized == "samplerstate") {
outType = ShaderResourceType::Sampler;
return true;
}
return false;
}
Containers::String ResolveShaderDependencyPath(
const Containers::String& dependencyPath,
const Containers::String& sourcePath) {
if (dependencyPath.Empty()) {
return dependencyPath;
}
const std::filesystem::path dependencyFsPath(dependencyPath.CStr());
if (dependencyFsPath.is_absolute()) {
return NormalizePathString(dependencyFsPath);
}
const std::filesystem::path sourceFsPath(sourcePath.CStr());
if (sourceFsPath.is_absolute()) {
return NormalizePathString(sourceFsPath.parent_path() / dependencyFsPath);
}
const Containers::String& resourceRoot = ResourceManager::Get().GetResourceRoot();
if (!resourceRoot.Empty()) {
return NormalizePathString(
std::filesystem::path(resourceRoot.CStr()) /
sourceFsPath.parent_path() /
dependencyFsPath);
}
return NormalizePathString(sourceFsPath.parent_path() / dependencyFsPath);
}
struct ExtractedProgramBlock {
enum class Kind {
SharedInclude,