Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Editor/PackagePathResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace GfxQA.ShaderVariantTool
{
using System;
using System.IO;
using System.Reflection;

using UnityEditor.PackageManager;

using UnityEngine;

public static class PackagePathResolver
{
public static string GetPackageAbsolutePath(string packageName)
{
Type packageInfoType = typeof(PackageInfo);
MethodInfo method = packageInfoType.GetMethod("GetAllRegisteredPackages", BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
var packages = (PackageInfo[])method.Invoke(null, null);
foreach (PackageInfo package in packages)
{
if (package.name == packageName)
{
return Path.GetFullPath(package.resolvedPath);
}
}
}

Debug.LogWarning($"Package '{packageName}' not found.");
return null;
}
}
}
11 changes: 11 additions & 0 deletions Editor/PackagePathResolver.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 59 additions & 1 deletion Editor/ShaderVariantTool_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UnityEditor;
using System.Linq;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace GfxQA.ShaderVariantTool
Expand Down Expand Up @@ -148,6 +149,7 @@ public void SetKeywordDeclareType()
var shader = Shader.Find(name);
var shaderData = ShaderUtil.GetShaderData(shader);
var subShader = shaderData.GetSubshader(0);
var shaderPath = AssetDatabase.GetAssetPath(shader);
for (int i = 0; i < subShader.PassCount; i++)
{
//Get source code
Expand All @@ -160,12 +162,13 @@ public void SetKeywordDeclareType()
//Read #include_with_pragmas lines as declare types are in seperate hlsl
string pattern = @"#include_with_pragmas\s""(.*)""";
MatchCollection matches = Regex.Matches(shaderCode, pattern);
string shaderDirectoryPath = Path.GetDirectoryName(shaderPath);
List<Match> matchesList = matches.ToList();
foreach(Match m in matchesList)
{
//Read hlsl code and find the #pragma lines
string hlslPath = m.Groups[1].Value;
string hlslCode = System.IO.File.ReadAllText(hlslPath);
string hlslCode = ReadFromAnyPath(shaderDirectoryPath, hlslPath);
GetDirectPragmaKeywordType(hlslCode, ref keywordDeclareType);
}
}
Expand All @@ -179,6 +182,61 @@ public void SetKeywordDeclareType()
}
}

private string ReadFromAnyPath(string sourceDirectory, string path)
{
if (TryReadRelativePath(path, out string content))
{
return content;
}

if (TryReadAbsolutePath(path, out content))
{
return content;
}

if (TryReadUPMPackagePath(path, out content))
{
return content;
}

Debug.LogError($"ShaderVariantTool error. Included file {path} is not found.");
return null;

bool TryReadRelativePath(string filePath, out string contentRelativePath)
{
string fullPath = Path.Combine(sourceDirectory, filePath);
return TryReadAbsolutePath(fullPath, out contentRelativePath);
}

bool TryReadAbsolutePath(string filePath, out string contentAbsolutePath)
{
if (File.Exists(filePath))
{
contentAbsolutePath = File.ReadAllText(filePath);
return true;
}

contentAbsolutePath = null;
return false;
}

bool TryReadUPMPackagePath(string filePath, out string contentUPMPackage)
{
const string pattern = @"Packages\/(.*)\/(.*)";
MatchCollection matches = Regex.Matches(filePath, pattern);
if (matches.Count > 0)
{
string packageName = matches[0].Groups[1].Value;
string packageRoot = PackagePathResolver.GetPackageAbsolutePath(packageName);
string fullPath = Path.Combine(packageRoot, matches[0].Groups[2].Value);
return TryReadAbsolutePath(fullPath, out contentUPMPackage);
}

contentUPMPackage = null;
return false;
}
}

private void GetDirectPragmaKeywordType(string shaderCode, ref Dictionary<string, string> keywordDeclareType)
{
//Read shader code and find the #pragma lines
Expand Down