Skip to content

Commit

Permalink
Add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
KeeVeeGames committed Oct 10, 2024
1 parent fd262dd commit 6e1ea83
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 49 deletions.
Binary file modified Example/#shady/Shady
Binary file not shown.
Binary file modified Example/#shady/Shady.exe
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
varying vec2 v_vTexcoord;
// Date: 2024-10-10T16:58:56.9612898+04:00
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

// begin import sh_example_exports.fsh.random
Expand All @@ -16,13 +17,9 @@ vec4 color_channel(vec4 color)
{
#ifdef RED
return vec4(color.r, 0.0, 0.0, color.a);
#endif

#ifdef GREEN
#elif defined(GREEN)
return vec4(0.0, color.g, 0.0, color.a);
#endif

#ifdef BLUE
#elif defined(BLUE)
return vec4(0.0, 0.0, color.b, color.a);
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// variant of sh_example_flip_base.fsh
// Date: 2024-10-10T16:58:56.9622901+04:00
// variant of sh_example_flip_base.fsh
#define BLUE
#define NOISE

Expand All @@ -20,13 +21,9 @@ vec4 color_channel(vec4 color)
{
#ifdef RED
return vec4(color.r, 0.0, 0.0, color.a);
#endif

#ifdef GREEN
#elif defined(GREEN)
return vec4(0.0, color.g, 0.0, color.a);
#endif

#ifdef BLUE
#elif defined(BLUE)
return vec4(0.0, 0.0, color.b, color.a);
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// variant of sh_example_flip_base.fsh
// Date: 2024-10-10T16:58:56.9622901+04:00
// variant of sh_example_flip_base.fsh
#define RED

varying vec2 v_vTexcoord;
Expand All @@ -19,13 +20,9 @@ vec4 color_channel(vec4 color)
{
#ifdef RED
return vec4(color.r, 0.0, 0.0, color.a);
#endif

#ifdef GREEN
#elif defined(GREEN)
return vec4(0.0, color.g, 0.0, color.a);
#endif

#ifdef BLUE
#elif defined(BLUE)
return vec4(0.0, 0.0, color.b, color.a);
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
varying vec2 v_vTexcoord;
// Date: 2024-10-10T16:58:56.9672914+04:00
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

// begin import sh_example_macros.fsh.__shady_export
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// begin import sh_example_exports.fsh.__shady_export
// Date: 2024-10-10T17:06:33.4141186+04:00
// begin import sh_example_exports.fsh.__shady_export
float random(vec2 st) {
return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
varying vec2 v_vTexcoord;
// Date: 2024-10-10T16:58:56.9753265+04:00
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
Expand Down
3 changes: 2 additions & 1 deletion Example/shaders/sh_example_macros/sh_example_macros.fsh_mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
varying vec2 v_vTexcoord;
// Date: 2024-10-10T16:58:56.9762902+04:00
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

// begin import sh_example_exports.fsh.GRAYSCALE_FACTOR
Expand Down
84 changes: 61 additions & 23 deletions Source/Shady/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Text.RegularExpressions;
using static Shady.Parser;
Expand Down Expand Up @@ -53,7 +56,7 @@ static void Main(string[] args)
{
Shader shader = shaderKeyValue.Value;

if (shader.WillModify)
if (shader.WillModify || shader.IsCahced)
{
File.Copy(shader.FileName, $"{shader.FileName}_bak", true);
}
Expand Down Expand Up @@ -105,6 +108,24 @@ private static Shader ParseShader(string path)
{
Shader shader = new Shader(path);

// cache checking
if (File.Exists($"{path}_mod"))
{
IEnumerable<string> modLines = File.ReadLines($"{path}_mod");

string modLineFirst = modLines.First();
if (modLineFirst.StartsWith("// Date: "))
{
string modDateString = modLineFirst.Replace("// Date: ", "");
DateTime modDate = DateTime.ParseExact(modDateString, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

if (modDate.CompareTo(File.GetLastWriteTime(path)) == 0)
{
shader.IsCahced = true;
}
}
}

IEnumerable<string> lines = File.ReadLines(path);
int i = -1;

Expand Down Expand Up @@ -227,6 +248,8 @@ private static void ParseTokens(KeyValuePair<string, Shader> shaderKeyValue)
switch (lineTokens[0].TokenType)
{
case TokenType.Import:
if (shader.IsCahced) break;

shaderLine.ImportRegion.ShaderName = lineTokens[2].Value + shader.Extension;

if (lineTokens[3].TokenType == TokenType.Dot)
Expand All @@ -243,6 +266,8 @@ private static void ParseTokens(KeyValuePair<string, Shader> shaderKeyValue)
break;

case TokenType.Inline:
if (shader.IsCahced) break;

shaderLine.ImportRegion.ShaderName = lineTokens[2].Value + shader.Extension;

if (lineTokens[3].TokenType == TokenType.Dot)
Expand All @@ -255,6 +280,8 @@ private static void ParseTokens(KeyValuePair<string, Shader> shaderKeyValue)
break;

case TokenType.Variant:
if (shader.IsCahced) break;

shader.VariantArguments = lineTokens.Where(token => token.TokenType is TokenType.Identifier or TokenType.Argument)
.Select(token => token.Value)
.ToArray();
Expand Down Expand Up @@ -490,39 +517,50 @@ private static void WriteShaders(Dictionary<string, Shader> shaders)
{
Shader shader = shaderKeyValue.Value;

if (shader.WillModify)
if (!shader.IsCahced)
{
HashSet<(string ShaderName, string RegionName)> imported = new HashSet<(string ShaderName, string RegionName)>();
imported.Add((shaderKeyValue.Key, Shader.FullRegion));

using (TextWriter textWriter = new StreamWriter($"{shader.FileName}_mod", false, Encoding.UTF8, 65536))
if (shader.WillModify)
{
if (shader.VariantArguments == null)
{
ExpandRegion(shaders, textWriter, shader.Lines, imported);
}
else
{
textWriter.WriteLine($"// variant of {shader.VariantArguments[0]}");

foreach (string variantArgument in shader.VariantArguments.Skip(1))
{
textWriter.WriteLine($"#define {variantArgument}");
}
HashSet<(string ShaderName, string RegionName)> imported = new HashSet<(string ShaderName, string RegionName)>();
imported.Add((shaderKeyValue.Key, Shader.FullRegion));

textWriter.WriteLine();
using (TextWriter textWriter = new StreamWriter($"{shader.FileName}_mod", false, Encoding.UTF8, 65536))
{
// write date of original file into mod file for caching
DateTime date = File.GetLastWriteTime(shader.FileName);
textWriter.WriteLine($"// Date: {date.ToString("O")}");

if (shaders.ContainsKey(shader.VariantArguments[0]))
if (shader.VariantArguments == null)
{
ExpandRegion(shaders, textWriter, shaders[shader.VariantArguments[0]].Lines, imported);
ExpandRegion(shaders, textWriter, shader.Lines, imported);
}
else
{
Console.WriteLine($"[Shady] Variant Error in {shader.Name}: Cannot create a variant of '{shader.VariantArguments[0]}', shader doesn't exist!");
textWriter.WriteLine($"// variant of {shader.VariantArguments[0]}");

foreach (string variantArgument in shader.VariantArguments.Skip(1))
{
textWriter.WriteLine($"#define {variantArgument}");
}

textWriter.WriteLine();

if (shaders.ContainsKey(shader.VariantArguments[0]))
{
ExpandRegion(shaders, textWriter, shaders[shader.VariantArguments[0]].Lines, imported);
}
else
{
Console.WriteLine($"[Shady] Variant Error in {shader.Name}: Cannot create a variant of '{shader.VariantArguments[0]}', shader doesn't exist!");
}
}
}
}

File.Copy($"{shader.FileName}_mod", shader.FileName, true);
}
}
else
{
File.Copy($"{shader.FileName}_mod", shader.FileName, true);
}
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Shady/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class Shader
public LinkedList<ShaderLine> Lines { get; }
public string[]? VariantArguments;
public bool WillModify { get; set; }
public bool IsCahced { get; set; }

private readonly Dictionary<string, LinkedList<ShaderLine>> _regions;

Expand All @@ -21,6 +22,7 @@ public Shader(string fileName)
FileName = fileName;
Lines = new LinkedList<ShaderLine>();
WillModify = false;
IsCahced = false;

_regions = new Dictionary<string, LinkedList<ShaderLine>>();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Shady/Shady.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
</PropertyGroup>

</Project>

0 comments on commit 6e1ea83

Please sign in to comment.