Add Unity-like shader authoring MVP importer
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
@@ -583,6 +584,909 @@ bool ReadTextFile(const Containers::String& path, Containers::String& outText) {
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t CalculateShaderMemorySize(const Shader& shader);
|
||||
|
||||
struct AuthoringTagEntry {
|
||||
Containers::String name;
|
||||
Containers::String value;
|
||||
};
|
||||
|
||||
struct AuthoringBackendVariantEntry {
|
||||
ShaderBackend backend = ShaderBackend::Generic;
|
||||
ShaderLanguage language = ShaderLanguage::GLSL;
|
||||
Containers::String vertexSourcePath;
|
||||
Containers::String fragmentSourcePath;
|
||||
Containers::String vertexProfile;
|
||||
Containers::String fragmentProfile;
|
||||
};
|
||||
|
||||
struct AuthoringPassEntry {
|
||||
Containers::String name;
|
||||
std::vector<AuthoringTagEntry> tags;
|
||||
Containers::Array<ShaderResourceBindingDesc> resources;
|
||||
Containers::String vertexEntryPoint;
|
||||
Containers::String fragmentEntryPoint;
|
||||
std::vector<AuthoringBackendVariantEntry> backendVariants;
|
||||
};
|
||||
|
||||
struct AuthoringSubShaderEntry {
|
||||
std::vector<AuthoringTagEntry> tags;
|
||||
std::vector<AuthoringPassEntry> passes;
|
||||
};
|
||||
|
||||
struct AuthoringShaderDesc {
|
||||
Containers::String name;
|
||||
Containers::Array<ShaderPropertyDesc> properties;
|
||||
std::vector<AuthoringSubShaderEntry> subShaders;
|
||||
};
|
||||
|
||||
std::string StripAuthoringLineComment(const std::string& line) {
|
||||
bool inString = false;
|
||||
bool escaped = false;
|
||||
for (size_t index = 0; index + 1 < line.size(); ++index) {
|
||||
const char ch = line[index];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\\') {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"') {
|
||||
inString = !inString;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!inString && ch == '/' && line[index + 1] == '/') {
|
||||
return line.substr(0, index);
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
bool StartsWithKeyword(const std::string& line, const char* keyword) {
|
||||
const std::string keywordString(keyword);
|
||||
if (line.size() < keywordString.size() ||
|
||||
line.compare(0, keywordString.size(), keywordString) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return line.size() == keywordString.size() ||
|
||||
std::isspace(static_cast<unsigned char>(line[keywordString.size()])) != 0;
|
||||
}
|
||||
|
||||
void SplitShaderAuthoringLines(
|
||||
const std::string& sourceText,
|
||||
std::vector<std::string>& outLines) {
|
||||
outLines.clear();
|
||||
|
||||
std::istringstream stream(sourceText);
|
||||
std::string rawLine;
|
||||
while (std::getline(stream, rawLine)) {
|
||||
std::string line = TrimCopy(StripAuthoringLineComment(rawLine));
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!StartsWithKeyword(line, "Tags") &&
|
||||
line.size() > 1 &&
|
||||
line.back() == '{') {
|
||||
line.pop_back();
|
||||
const std::string prefix = TrimCopy(line);
|
||||
if (!prefix.empty()) {
|
||||
outLines.push_back(prefix);
|
||||
}
|
||||
outLines.push_back("{");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.size() > 1 && line.front() == '}') {
|
||||
outLines.push_back("}");
|
||||
line = TrimCopy(line.substr(1));
|
||||
if (!line.empty()) {
|
||||
outLines.push_back(line);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
outLines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
size_t FindMatchingDelimiter(
|
||||
const std::string& text,
|
||||
size_t openPos,
|
||||
char openChar,
|
||||
char closeChar) {
|
||||
if (openPos >= text.size() || text[openPos] != openChar) {
|
||||
return std::string::npos;
|
||||
}
|
||||
|
||||
bool inString = false;
|
||||
bool escaped = false;
|
||||
int depth = 0;
|
||||
for (size_t pos = openPos; pos < text.size(); ++pos) {
|
||||
const char ch = text[pos];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\\') {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"') {
|
||||
inString = !inString;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inString) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == openChar) {
|
||||
++depth;
|
||||
} else if (ch == closeChar) {
|
||||
--depth;
|
||||
if (depth == 0) {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return std::string::npos;
|
||||
}
|
||||
|
||||
size_t FindFirstTopLevelChar(const std::string& text, char target) {
|
||||
bool inString = false;
|
||||
bool escaped = false;
|
||||
int roundDepth = 0;
|
||||
int squareDepth = 0;
|
||||
|
||||
for (size_t pos = 0; pos < text.size(); ++pos) {
|
||||
const char ch = text[pos];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\\') {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"') {
|
||||
inString = !inString;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inString) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == target && roundDepth == 0 && squareDepth == 0) {
|
||||
return pos;
|
||||
}
|
||||
|
||||
if (ch == '(') {
|
||||
++roundDepth;
|
||||
continue;
|
||||
}
|
||||
if (ch == ')') {
|
||||
--roundDepth;
|
||||
continue;
|
||||
}
|
||||
if (ch == '[') {
|
||||
++squareDepth;
|
||||
continue;
|
||||
}
|
||||
if (ch == ']') {
|
||||
--squareDepth;
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return std::string::npos;
|
||||
}
|
||||
|
||||
bool SplitCommaSeparatedAuthoring(const std::string& text, std::vector<std::string>& outParts) {
|
||||
outParts.clear();
|
||||
|
||||
bool inString = false;
|
||||
bool escaped = false;
|
||||
int roundDepth = 0;
|
||||
int squareDepth = 0;
|
||||
size_t partStart = 0;
|
||||
|
||||
for (size_t pos = 0; pos < text.size(); ++pos) {
|
||||
const char ch = text[pos];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\\') {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"') {
|
||||
inString = !inString;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inString) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '(') {
|
||||
++roundDepth;
|
||||
continue;
|
||||
}
|
||||
if (ch == ')') {
|
||||
--roundDepth;
|
||||
continue;
|
||||
}
|
||||
if (ch == '[') {
|
||||
++squareDepth;
|
||||
continue;
|
||||
}
|
||||
if (ch == ']') {
|
||||
--squareDepth;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == ',' && roundDepth == 0 && squareDepth == 0) {
|
||||
outParts.push_back(TrimCopy(text.substr(partStart, pos - partStart)));
|
||||
partStart = pos + 1;
|
||||
}
|
||||
}
|
||||
|
||||
outParts.push_back(TrimCopy(text.substr(partStart)));
|
||||
return true;
|
||||
}
|
||||
|
||||
Containers::String UnquoteAuthoringValue(const std::string& text) {
|
||||
const std::string trimmed = TrimCopy(text);
|
||||
if (trimmed.size() >= 2 &&
|
||||
trimmed.front() == '"' &&
|
||||
trimmed.back() == '"') {
|
||||
Containers::String parsed;
|
||||
if (ParseQuotedString(trimmed, 0, parsed)) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return Containers::String(trimmed.c_str());
|
||||
}
|
||||
|
||||
bool TryTokenizeQuotedArguments(const std::string& line, std::vector<std::string>& outTokens) {
|
||||
outTokens.clear();
|
||||
|
||||
std::string current;
|
||||
bool inString = false;
|
||||
bool escaped = false;
|
||||
|
||||
for (size_t pos = 0; pos < line.size(); ++pos) {
|
||||
const char ch = line[pos];
|
||||
if (escaped) {
|
||||
current.push_back(ch);
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\\' && inString) {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"') {
|
||||
inString = !inString;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!inString && std::isspace(static_cast<unsigned char>(ch)) != 0) {
|
||||
if (!current.empty()) {
|
||||
outTokens.push_back(current);
|
||||
current.clear();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
current.push_back(ch);
|
||||
}
|
||||
|
||||
if (inString) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!current.empty()) {
|
||||
outTokens.push_back(current);
|
||||
}
|
||||
|
||||
return !outTokens.empty();
|
||||
}
|
||||
|
||||
bool TryParseInlineTagAssignments(
|
||||
const std::string& line,
|
||||
std::vector<AuthoringTagEntry>& outTags) {
|
||||
const size_t openBrace = line.find('{');
|
||||
const size_t closeBrace = line.rfind('}');
|
||||
if (openBrace == std::string::npos ||
|
||||
closeBrace == std::string::npos ||
|
||||
closeBrace <= openBrace) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string content = line.substr(openBrace + 1, closeBrace - openBrace - 1);
|
||||
size_t pos = 0;
|
||||
while (pos < content.size()) {
|
||||
pos = SkipWhitespace(content, pos);
|
||||
while (pos < content.size() && content[pos] == ',') {
|
||||
++pos;
|
||||
pos = SkipWhitespace(content, pos);
|
||||
}
|
||||
if (pos >= content.size()) {
|
||||
break;
|
||||
}
|
||||
|
||||
Containers::String key;
|
||||
if (!ParseQuotedString(content, pos, key, &pos)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pos = SkipWhitespace(content, pos);
|
||||
if (pos >= content.size() || content[pos] != '=') {
|
||||
return false;
|
||||
}
|
||||
|
||||
pos = SkipWhitespace(content, pos + 1);
|
||||
Containers::String value;
|
||||
if (!ParseQuotedString(content, pos, value, &pos)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outTags.push_back({ key, value });
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseSemanticAttributes(
|
||||
const std::string& attributesText,
|
||||
Containers::String& outSemantic) {
|
||||
outSemantic.Clear();
|
||||
|
||||
size_t pos = 0;
|
||||
while (pos < attributesText.size()) {
|
||||
pos = attributesText.find('[', pos);
|
||||
if (pos == std::string::npos) {
|
||||
break;
|
||||
}
|
||||
|
||||
const size_t closePos = FindMatchingDelimiter(attributesText, pos, '[', ']');
|
||||
if (closePos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string attributeBody = TrimCopy(attributesText.substr(pos + 1, closePos - pos - 1));
|
||||
if (attributeBody.size() > 10 &&
|
||||
attributeBody.compare(0, 9, "Semantic(") == 0 &&
|
||||
attributeBody.back() == ')') {
|
||||
outSemantic = UnquoteAuthoringValue(attributeBody.substr(9, attributeBody.size() - 10));
|
||||
}
|
||||
|
||||
pos = closePos + 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseAuthoringPropertyLine(
|
||||
const std::string& line,
|
||||
ShaderPropertyDesc& outProperty) {
|
||||
outProperty = {};
|
||||
|
||||
const size_t openParen = line.find('(');
|
||||
if (openParen == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t closeParen = FindMatchingDelimiter(line, openParen, '(', ')');
|
||||
if (closeParen == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outProperty.name = Containers::String(TrimCopy(line.substr(0, openParen)).c_str());
|
||||
if (outProperty.name.Empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> headerParts;
|
||||
if (!SplitCommaSeparatedAuthoring(line.substr(openParen + 1, closeParen - openParen - 1), headerParts) ||
|
||||
headerParts.size() < 2u) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outProperty.displayName = UnquoteAuthoringValue(headerParts[0]);
|
||||
std::string propertyTypeName = headerParts[1];
|
||||
for (size_t index = 2; index < headerParts.size(); ++index) {
|
||||
propertyTypeName += ",";
|
||||
propertyTypeName += headerParts[index];
|
||||
}
|
||||
|
||||
propertyTypeName = TrimCopy(propertyTypeName);
|
||||
const size_t rangePos = propertyTypeName.find('(');
|
||||
if (rangePos != std::string::npos &&
|
||||
TrimCopy(propertyTypeName.substr(0, rangePos)) == "Range") {
|
||||
propertyTypeName = "Range";
|
||||
}
|
||||
|
||||
if (!TryParseShaderPropertyType(propertyTypeName.c_str(), outProperty.type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t equalsPos = line.find('=', closeParen + 1);
|
||||
if (equalsPos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string tail = TrimCopy(line.substr(equalsPos + 1));
|
||||
const size_t attributePos = FindFirstTopLevelChar(tail, '[');
|
||||
const std::string defaultValueText =
|
||||
attributePos == std::string::npos ? tail : TrimCopy(tail.substr(0, attributePos));
|
||||
if (defaultValueText.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outProperty.defaultValue = UnquoteAuthoringValue(defaultValueText);
|
||||
|
||||
if (attributePos != std::string::npos &&
|
||||
!TryParseSemanticAttributes(tail.substr(attributePos), outProperty.semantic)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseAuthoringResourceLine(
|
||||
const std::string& line,
|
||||
ShaderResourceBindingDesc& outBinding) {
|
||||
outBinding = {};
|
||||
|
||||
const size_t openParen = line.find('(');
|
||||
if (openParen == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t closeParen = FindMatchingDelimiter(line, openParen, '(', ')');
|
||||
if (closeParen == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outBinding.name = Containers::String(TrimCopy(line.substr(0, openParen)).c_str());
|
||||
if (outBinding.name.Empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> parts;
|
||||
if (!SplitCommaSeparatedAuthoring(line.substr(openParen + 1, closeParen - openParen - 1), parts) ||
|
||||
parts.size() != 3u) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TryParseShaderResourceType(parts[0].c_str(), outBinding.type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
outBinding.set = static_cast<Core::uint32>(std::stoul(parts[1]));
|
||||
outBinding.binding = static_cast<Core::uint32>(std::stoul(parts[2]));
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t attributePos = FindFirstTopLevelChar(line.substr(closeParen + 1), '[');
|
||||
if (attributePos != std::string::npos) {
|
||||
const std::string attributesText = line.substr(closeParen + 1 + attributePos);
|
||||
if (!TryParseSemanticAttributes(attributesText, outBinding.semantic)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParseUnityLikeShaderAuthoring(
|
||||
const Containers::String& path,
|
||||
const std::string& sourceText,
|
||||
AuthoringShaderDesc& outDesc,
|
||||
Containers::String* outError) {
|
||||
(void)path;
|
||||
outDesc = {};
|
||||
|
||||
enum class BlockKind {
|
||||
None,
|
||||
Shader,
|
||||
Properties,
|
||||
SubShader,
|
||||
Pass,
|
||||
Resources
|
||||
};
|
||||
|
||||
auto fail = [&outError](const std::string& message, size_t lineNumber) -> bool {
|
||||
if (outError != nullptr) {
|
||||
*outError = Containers::String(
|
||||
("Unity-like shader parse error at line " + std::to_string(lineNumber) + ": " + message).c_str());
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
std::vector<std::string> lines;
|
||||
SplitShaderAuthoringLines(sourceText, lines);
|
||||
if (lines.empty()) {
|
||||
return fail("shader file is empty", 0);
|
||||
}
|
||||
|
||||
std::vector<BlockKind> blockStack;
|
||||
BlockKind pendingBlock = BlockKind::None;
|
||||
AuthoringSubShaderEntry* currentSubShader = nullptr;
|
||||
AuthoringPassEntry* currentPass = nullptr;
|
||||
bool inProgram = false;
|
||||
|
||||
auto currentBlock = [&blockStack]() -> BlockKind {
|
||||
return blockStack.empty() ? BlockKind::None : blockStack.back();
|
||||
};
|
||||
|
||||
for (size_t lineIndex = 0; lineIndex < lines.size(); ++lineIndex) {
|
||||
const std::string& line = lines[lineIndex];
|
||||
const size_t humanLine = lineIndex + 1;
|
||||
|
||||
if (inProgram) {
|
||||
if (line == "ENDHLSL" || line == "ENDCG") {
|
||||
inProgram = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<std::string> pragmaTokens;
|
||||
if (!TryTokenizeQuotedArguments(line, pragmaTokens) || pragmaTokens.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pragmaTokens[0] != "#pragma") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pragmaTokens.size() >= 3u && pragmaTokens[1] == "vertex") {
|
||||
currentPass->vertexEntryPoint = pragmaTokens[2].c_str();
|
||||
continue;
|
||||
}
|
||||
if (pragmaTokens.size() >= 3u && pragmaTokens[1] == "fragment") {
|
||||
currentPass->fragmentEntryPoint = pragmaTokens[2].c_str();
|
||||
continue;
|
||||
}
|
||||
if (pragmaTokens.size() >= 6u && pragmaTokens[1] == "backend") {
|
||||
AuthoringBackendVariantEntry backendVariant = {};
|
||||
if (!TryParseShaderBackend(pragmaTokens[2].c_str(), backendVariant.backend)) {
|
||||
return fail("invalid backend pragma backend name", humanLine);
|
||||
}
|
||||
if (!TryParseShaderLanguage(pragmaTokens[3].c_str(), backendVariant.language)) {
|
||||
return fail("invalid backend pragma language name", humanLine);
|
||||
}
|
||||
|
||||
backendVariant.vertexSourcePath = pragmaTokens[4].c_str();
|
||||
backendVariant.fragmentSourcePath = pragmaTokens[5].c_str();
|
||||
if (pragmaTokens.size() >= 7u) {
|
||||
backendVariant.vertexProfile = pragmaTokens[6].c_str();
|
||||
}
|
||||
if (pragmaTokens.size() >= 8u) {
|
||||
backendVariant.fragmentProfile = pragmaTokens[7].c_str();
|
||||
}
|
||||
|
||||
currentPass->backendVariants.push_back(std::move(backendVariant));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "{") {
|
||||
switch (pendingBlock) {
|
||||
case BlockKind::Shader:
|
||||
blockStack.push_back(BlockKind::Shader);
|
||||
break;
|
||||
case BlockKind::Properties:
|
||||
blockStack.push_back(BlockKind::Properties);
|
||||
break;
|
||||
case BlockKind::SubShader:
|
||||
outDesc.subShaders.emplace_back();
|
||||
currentSubShader = &outDesc.subShaders.back();
|
||||
blockStack.push_back(BlockKind::SubShader);
|
||||
break;
|
||||
case BlockKind::Pass:
|
||||
if (currentSubShader == nullptr) {
|
||||
return fail("pass block must be inside a SubShader", humanLine);
|
||||
}
|
||||
currentSubShader->passes.emplace_back();
|
||||
currentPass = ¤tSubShader->passes.back();
|
||||
blockStack.push_back(BlockKind::Pass);
|
||||
break;
|
||||
case BlockKind::Resources:
|
||||
if (currentPass == nullptr) {
|
||||
return fail("resources block must be inside a Pass", humanLine);
|
||||
}
|
||||
blockStack.push_back(BlockKind::Resources);
|
||||
break;
|
||||
case BlockKind::None:
|
||||
default:
|
||||
return fail("unexpected opening brace", humanLine);
|
||||
}
|
||||
|
||||
pendingBlock = BlockKind::None;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "}") {
|
||||
if (blockStack.empty()) {
|
||||
return fail("unexpected closing brace", humanLine);
|
||||
}
|
||||
|
||||
const BlockKind closingBlock = blockStack.back();
|
||||
blockStack.pop_back();
|
||||
if (closingBlock == BlockKind::Pass) {
|
||||
currentPass = nullptr;
|
||||
} else if (closingBlock == BlockKind::SubShader) {
|
||||
currentSubShader = nullptr;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StartsWithKeyword(line, "Shader")) {
|
||||
std::vector<std::string> tokens;
|
||||
if (!TryTokenizeQuotedArguments(line, tokens) || tokens.size() < 2u) {
|
||||
return fail("Shader declaration is missing a name", humanLine);
|
||||
}
|
||||
outDesc.name = tokens[1].c_str();
|
||||
pendingBlock = BlockKind::Shader;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "Properties") {
|
||||
pendingBlock = BlockKind::Properties;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StartsWithKeyword(line, "SubShader")) {
|
||||
pendingBlock = BlockKind::SubShader;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StartsWithKeyword(line, "Pass")) {
|
||||
pendingBlock = BlockKind::Pass;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "Resources") {
|
||||
pendingBlock = BlockKind::Resources;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (StartsWithKeyword(line, "Tags")) {
|
||||
std::vector<AuthoringTagEntry> parsedTags;
|
||||
if (!TryParseInlineTagAssignments(line, parsedTags)) {
|
||||
return fail("Tags block must use inline key/value pairs", humanLine);
|
||||
}
|
||||
|
||||
if (currentPass != nullptr) {
|
||||
currentPass->tags.insert(currentPass->tags.end(), parsedTags.begin(), parsedTags.end());
|
||||
} else if (currentSubShader != nullptr) {
|
||||
currentSubShader->tags.insert(currentSubShader->tags.end(), parsedTags.begin(), parsedTags.end());
|
||||
} else {
|
||||
return fail("Tags block is only supported inside SubShader or Pass", humanLine);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (currentBlock() == BlockKind::Properties) {
|
||||
ShaderPropertyDesc property = {};
|
||||
if (!TryParseAuthoringPropertyLine(line, property)) {
|
||||
return fail("invalid Properties entry", humanLine);
|
||||
}
|
||||
outDesc.properties.PushBack(property);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (currentBlock() == BlockKind::Resources) {
|
||||
ShaderResourceBindingDesc resourceBinding = {};
|
||||
if (!TryParseAuthoringResourceLine(line, resourceBinding)) {
|
||||
return fail("invalid Resources entry", humanLine);
|
||||
}
|
||||
currentPass->resources.PushBack(resourceBinding);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (currentBlock() == BlockKind::Pass && currentPass != nullptr) {
|
||||
if (StartsWithKeyword(line, "Name")) {
|
||||
std::vector<std::string> tokens;
|
||||
if (!TryTokenizeQuotedArguments(line, tokens) || tokens.size() < 2u) {
|
||||
return fail("pass Name directive is missing a value", humanLine);
|
||||
}
|
||||
currentPass->name = tokens[1].c_str();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "HLSLPROGRAM" || line == "CGPROGRAM") {
|
||||
inProgram = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return fail("unsupported authoring statement: " + line, humanLine);
|
||||
}
|
||||
|
||||
if (inProgram) {
|
||||
return fail("program block was not closed", lines.size());
|
||||
}
|
||||
if (!blockStack.empty()) {
|
||||
return fail("one or more blocks were not closed", lines.size());
|
||||
}
|
||||
if (outDesc.name.Empty()) {
|
||||
return fail("shader name is missing", 0);
|
||||
}
|
||||
if (outDesc.subShaders.empty()) {
|
||||
return fail("shader does not declare any SubShader blocks", 0);
|
||||
}
|
||||
|
||||
for (const AuthoringSubShaderEntry& subShader : outDesc.subShaders) {
|
||||
if (subShader.passes.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const AuthoringPassEntry& pass : subShader.passes) {
|
||||
if (pass.name.Empty()) {
|
||||
return fail("a Pass is missing a Name directive", 0);
|
||||
}
|
||||
if (pass.backendVariants.empty()) {
|
||||
return fail("a Pass is missing backend variants", 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
LoadResult BuildShaderFromAuthoringDesc(
|
||||
const Containers::String& path,
|
||||
const AuthoringShaderDesc& authoringDesc) {
|
||||
auto shader = std::make_unique<Shader>();
|
||||
IResource::ConstructParams params;
|
||||
params.path = path;
|
||||
params.guid = ResourceGUID::Generate(path);
|
||||
params.name = authoringDesc.name;
|
||||
shader->Initialize(params);
|
||||
|
||||
for (const ShaderPropertyDesc& property : authoringDesc.properties) {
|
||||
shader->AddProperty(property);
|
||||
}
|
||||
|
||||
for (const AuthoringSubShaderEntry& subShader : authoringDesc.subShaders) {
|
||||
for (const AuthoringPassEntry& pass : subShader.passes) {
|
||||
ShaderPass shaderPass = {};
|
||||
shaderPass.name = pass.name;
|
||||
shader->AddPass(shaderPass);
|
||||
|
||||
for (const AuthoringTagEntry& subShaderTag : subShader.tags) {
|
||||
shader->SetPassTag(pass.name, subShaderTag.name, subShaderTag.value);
|
||||
}
|
||||
for (const AuthoringTagEntry& passTag : pass.tags) {
|
||||
shader->SetPassTag(pass.name, passTag.name, passTag.value);
|
||||
}
|
||||
|
||||
for (const ShaderResourceBindingDesc& resourceBinding : pass.resources) {
|
||||
shader->AddPassResourceBinding(pass.name, resourceBinding);
|
||||
}
|
||||
|
||||
for (const AuthoringBackendVariantEntry& backendVariant : pass.backendVariants) {
|
||||
ShaderStageVariant vertexVariant = {};
|
||||
vertexVariant.stage = ShaderType::Vertex;
|
||||
vertexVariant.backend = backendVariant.backend;
|
||||
vertexVariant.language = backendVariant.language;
|
||||
vertexVariant.entryPoint = !pass.vertexEntryPoint.Empty()
|
||||
? pass.vertexEntryPoint
|
||||
: GetDefaultEntryPoint(backendVariant.language, ShaderType::Vertex);
|
||||
vertexVariant.profile = !backendVariant.vertexProfile.Empty()
|
||||
? backendVariant.vertexProfile
|
||||
: GetDefaultProfile(backendVariant.language, backendVariant.backend, ShaderType::Vertex);
|
||||
|
||||
const Containers::String resolvedVertexPath =
|
||||
ResolveShaderDependencyPath(backendVariant.vertexSourcePath, path);
|
||||
if (!ReadTextFile(resolvedVertexPath, vertexVariant.sourceCode)) {
|
||||
return LoadResult("Failed to read shader authoring vertex source: " + resolvedVertexPath);
|
||||
}
|
||||
shader->AddPassVariant(pass.name, vertexVariant);
|
||||
|
||||
ShaderStageVariant fragmentVariant = {};
|
||||
fragmentVariant.stage = ShaderType::Fragment;
|
||||
fragmentVariant.backend = backendVariant.backend;
|
||||
fragmentVariant.language = backendVariant.language;
|
||||
fragmentVariant.entryPoint = !pass.fragmentEntryPoint.Empty()
|
||||
? pass.fragmentEntryPoint
|
||||
: GetDefaultEntryPoint(backendVariant.language, ShaderType::Fragment);
|
||||
fragmentVariant.profile = !backendVariant.fragmentProfile.Empty()
|
||||
? backendVariant.fragmentProfile
|
||||
: GetDefaultProfile(backendVariant.language, backendVariant.backend, ShaderType::Fragment);
|
||||
|
||||
const Containers::String resolvedFragmentPath =
|
||||
ResolveShaderDependencyPath(backendVariant.fragmentSourcePath, path);
|
||||
if (!ReadTextFile(resolvedFragmentPath, fragmentVariant.sourceCode)) {
|
||||
return LoadResult("Failed to read shader authoring fragment source: " + resolvedFragmentPath);
|
||||
}
|
||||
shader->AddPassVariant(pass.name, fragmentVariant);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shader->m_memorySize = CalculateShaderMemorySize(*shader);
|
||||
return LoadResult(shader.release());
|
||||
}
|
||||
|
||||
bool LooksLikeUnityLikeShaderAuthoring(const std::string& sourceText) {
|
||||
std::vector<std::string> lines;
|
||||
SplitShaderAuthoringLines(sourceText, lines);
|
||||
return !lines.empty() && StartsWithKeyword(lines.front(), "Shader");
|
||||
}
|
||||
|
||||
bool CollectUnityLikeShaderDependencyPaths(
|
||||
const Containers::String& path,
|
||||
const std::string& sourceText,
|
||||
Containers::Array<Containers::String>& outDependencies) {
|
||||
outDependencies.Clear();
|
||||
|
||||
AuthoringShaderDesc authoringDesc = {};
|
||||
Containers::String parseError;
|
||||
if (!ParseUnityLikeShaderAuthoring(path, sourceText, authoringDesc, &parseError)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> seenPaths;
|
||||
for (const AuthoringSubShaderEntry& subShader : authoringDesc.subShaders) {
|
||||
for (const AuthoringPassEntry& pass : subShader.passes) {
|
||||
for (const AuthoringBackendVariantEntry& backendVariant : pass.backendVariants) {
|
||||
const Containers::String resolvedVertexPath =
|
||||
ResolveShaderDependencyPath(backendVariant.vertexSourcePath, path);
|
||||
const std::string vertexKey = ToStdString(resolvedVertexPath);
|
||||
if (!vertexKey.empty() && seenPaths.insert(vertexKey).second) {
|
||||
outDependencies.PushBack(resolvedVertexPath);
|
||||
}
|
||||
|
||||
const Containers::String resolvedFragmentPath =
|
||||
ResolveShaderDependencyPath(backendVariant.fragmentSourcePath, path);
|
||||
const std::string fragmentKey = ToStdString(resolvedFragmentPath);
|
||||
if (!fragmentKey.empty() && seenPaths.insert(fragmentKey).second) {
|
||||
outDependencies.PushBack(resolvedFragmentPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
LoadResult LoadUnityLikeShaderAuthoring(const Containers::String& path, const std::string& sourceText) {
|
||||
AuthoringShaderDesc authoringDesc = {};
|
||||
Containers::String parseError;
|
||||
if (!ParseUnityLikeShaderAuthoring(path, sourceText, authoringDesc, &parseError)) {
|
||||
return LoadResult(parseError);
|
||||
}
|
||||
|
||||
return BuildShaderFromAuthoringDesc(path, authoringDesc);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ReadShaderArtifactValue(const Containers::Array<Core::uint8>& data, size_t& offset, T& outValue) {
|
||||
if (offset + sizeof(T) > data.Size()) {
|
||||
@@ -1115,6 +2019,9 @@ LoadResult ShaderLoader::Load(const Containers::String& path, const ImportSettin
|
||||
if (ext == "shader" && LooksLikeShaderManifest(sourceText)) {
|
||||
return LoadShaderManifest(path, sourceText);
|
||||
}
|
||||
if (ext == "shader" && LooksLikeUnityLikeShaderAuthoring(sourceText)) {
|
||||
return LoadUnityLikeShaderAuthoring(path, sourceText);
|
||||
}
|
||||
|
||||
return LoadLegacySingleStageShader(path, sourceText);
|
||||
}
|
||||
@@ -1143,7 +2050,9 @@ bool ShaderLoader::CollectSourceDependencies(const Containers::String& path,
|
||||
|
||||
const std::string sourceText = ToStdString(data);
|
||||
if (!LooksLikeShaderManifest(sourceText)) {
|
||||
return true;
|
||||
return LooksLikeUnityLikeShaderAuthoring(sourceText)
|
||||
? CollectUnityLikeShaderDependencyPaths(path, sourceText, outDependencies)
|
||||
: true;
|
||||
}
|
||||
|
||||
return CollectShaderManifestDependencyPaths(path, sourceText, outDependencies);
|
||||
|
||||
Reference in New Issue
Block a user