Refactor and enhance codebase for maintainability

Refactored and reorganized the codebase to improve readability, performance, and maintainability. Introduced new interfaces and structs for better resource management, updated project configuration files, and refactored shader and graphics pipeline management. Improved error handling, code formatting, and removed unused code and namespaces. Updated DLL references and method signatures for consistency and maintainability.
This commit is contained in:
2025-10-22 18:46:39 +09:00
parent 6d1b510ac1
commit d2d9f5feb7
80 changed files with 2836 additions and 2198 deletions

View File

@@ -1,5 +1,3 @@
using Ghost.Shader.Compiler;
namespace Ghost.Shader.Compiler.Parser;
internal class DefinesBlock : IBlockParser<List<Token>, List<string>>

View File

@@ -1,5 +1,3 @@
using Ghost.Shader.Compiler;
namespace Ghost.Shader.Compiler.Parser;
internal class IncludesBlock : IBlockParser<List<Token>, List<string>>

View File

@@ -1,4 +1,4 @@
using Ghost.Shader.Compiler.Parser;
using Ghost.Core.Graphics;
namespace Ghost.Shader.Compiler.Parser;

View File

@@ -1,3 +1,5 @@
using Ghost.Core.Graphics;
namespace Ghost.Shader.Compiler.Parser;
// TODO: Add pass template support.
@@ -66,7 +68,7 @@ internal class PassBlock : IBlockParser<PassSyntax, PassSemantic>
return null;
}
var model = new PassSemantic
var semantic = new PassSemantic
{
name = syntax.name.lexeme,
defines = DefinesBlock.SemanticAnalysis(syntax.defines, errors),
@@ -76,8 +78,8 @@ internal class PassBlock : IBlockParser<PassSyntax, PassSemantic>
localPipeline = PipelineBlock.SemanticAnalysis(syntax.localPipeline, errors),
};
if (model.localProperties != null
&& model.localProperties.Any(p => p.scope == PropertyScope.Global))
if (semantic.localProperties != null
&& semantic.localProperties.Any(p => p.scope == PropertyScope.Global))
{
errors.Add(new ShaderError
{
@@ -93,50 +95,22 @@ internal class PassBlock : IBlockParser<PassSyntax, PassSemantic>
{
switch (func.name.lexeme)
{
case "vs":
if (func.arguments?.Count != 2)
{
errors.Add(new ShaderError
{
message = "Vertex shader declaration requires exactly two arguments: (shaderPath, entryPoint).",
line = func.name.line,
column = func.name.column
});
}
else
{
model.vertexShader = new ShaderEntryPoint
{
shader = func.arguments[0].lexeme,
entry = func.arguments[1].lexeme
};
}
case TokenLexicon.KnownFunctions.TASK_SHADER:
AnalysisShaderEntry(errors, func, ref semantic.taskShader);
break;
case "ps":
if (func.arguments?.Count != 2)
{
errors.Add(new ShaderError
{
message = "Pixel shader declaration requires exactly two arguments: (shaderPath, entryPoint).",
line = func.name.line,
column = func.name.column
});
}
else
{
model.pixelShader = new ShaderEntryPoint
{
shader = func.arguments[0].lexeme,
entry = func.arguments[1].lexeme
};
}
case TokenLexicon.KnownFunctions.MESH_SHADER:
AnalysisShaderEntry(errors, func, ref semantic.meshShader);
break;
case TokenLexicon.KnownFunctions.PIXEL_SHADER:
AnalysisShaderEntry(errors, func, ref semantic.pixelShader);
break;
default:
errors.Add(new ShaderError
{
message = $"Unknown function '{func.name.lexeme}' in pass.",
message = $"Unknown function '{func.name.lexeme}' in pass {syntax.name.lexeme}.",
line = func.name.line,
column = func.name.column
});
@@ -145,18 +119,39 @@ internal class PassBlock : IBlockParser<PassSyntax, PassSemantic>
}
}
if (model.vertexShader.shader == null || model.pixelShader.shader == null)
if (semantic.meshShader.shader == null || semantic.pixelShader.shader == null)
{
// TODO: Inheritance from base pass.
// TODO: Add mesh shader support.
errors.Add(new ShaderError
{
message = "Pass must contain a vertex shader (vs) and a pixel shader (ps) declaration.",
message = $"Pass {syntax.name.lexeme} must contain a mesh shader (ms) and a pixel shader (ps) declaration.",
line = syntax.name.line,
column = syntax.name.column
});
}
return model;
return semantic;
}
private static void AnalysisShaderEntry(List<ShaderError> errors, FunctionCallDeclaration func, ref ShaderEntryPoint shaderEntryPoint)
{
if (func.arguments?.Count != 2)
{
errors.Add(new ShaderError
{
message = "Shader declaration requires exactly two arguments: (shaderPath, entryPoint).",
line = func.name.line,
column = func.name.column
});
}
else
{
shaderEntryPoint = new ShaderEntryPoint
{
shader = func.arguments[0].lexeme,
entry = func.arguments[1].lexeme
};
}
}
}

View File

@@ -1,3 +1,5 @@
using Ghost.Core.Graphics;
namespace Ghost.Shader.Compiler.Parser;
internal class PipelineBlock : IBlockParser<PipelineSyntax, PipelineSemantic>

View File

@@ -1,4 +1,4 @@
using Ghost.Shader.Compiler.Parser;
using Ghost.Core.Graphics;
using Misaki.HighPerformance.Mathematics;
using System.Globalization;

View File

@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;
namespace Ghost.Shader.Compiler.Parser;
namespace Ghost.Shader.Compiler.Parser;
internal class ShaderBlock : IBlockParser<ShaderSyntax, ShaderSemantics>
{
@@ -110,7 +108,7 @@ internal class ShaderBlock : IBlockParser<ShaderSyntax, ShaderSemantics>
}
}
}
return shaderModel;
}
}

View File

@@ -1,5 +1,5 @@
using Ghost.Shader.Compiler.Parser;
using System.Collections.Generic;
using Ghost.Core.Graphics;
using Ghost.Shader.Compiler.Parser;
using System.Text;
namespace Ghost.Shader.Compiler;
@@ -227,7 +227,8 @@ internal static class ShaderCompiler
var fullPass = new FullPassDescriptor
{
uniqueIdentifier = GetPassUniqueId(semantics, pass),
vertexShader = pass.vertexShader,
taskShader = pass.taskShader,
meshShader = pass.meshShader,
pixelShader = pass.pixelShader,
localPipeline = localPipeline,
defines = pass.defines,

View File

@@ -28,7 +28,8 @@ internal class PipelineSemantic
internal class PassSemantic
{
public string name = string.Empty;
public ShaderEntryPoint vertexShader;
public ShaderEntryPoint taskShader;
public ShaderEntryPoint meshShader;
public ShaderEntryPoint pixelShader;
public List<string>? defines;
public List<string>? includes;

View File

@@ -4,27 +4,27 @@
public enum TokenType
{
None = 0,
// Literals
Identifier = 1 << 0, // variable names, function names, etc.
Keyword = 1 << 1, // shader, properties, pipeline, pass, etc.
Number = 1 << 2, // numeric literals (123, 45.67, .5)
StringLiteral = 1 << 3, // "ForwardVS.hlsl"
// Operators and Punctuation
Equals = 1 << 4, // =
Semicolon = 1 << 5, // ;
Comma = 1 << 6, // ,
// Delimiters
LBrace = 1 << 7, // {
RBrace = 1 << 8, // }
LParen = 1 << 9, // (
RParen = 1 << 10, // )
// Special
EndOfFile = 1 << 11, // EOF
// Future extensions (commented out for now)
// LBracket = 1 << 12, // [
// RBracket = 1 << 13, // ]
@@ -137,9 +137,9 @@ internal static class TokenLexicon
public static class KnownFunctions
{
public const string VERTEX_SHADER = "vs";
public const string PIXEL_SHADER = "ps";
public const string TASK_SHADER = "ts";
public const string MESH_SHADER = "ms";
public const string PIXEL_SHADER = "ps";
public const string COMPUTE_SHADER = "cs";
public const string DYNAMIC = "dynamic";
public const string STATIC = "static";
@@ -153,22 +153,22 @@ internal static class TokenLexicon
public const string FLOAT2 = "float2";
public const string FLOAT3 = "float3";
public const string FLOAT4 = "float4";
public const string INT = "int";
public const string INT2 = "int2";
public const string INT3 = "int3";
public const string INT4 = "int4";
public const string UINT = "uint";
public const string UINT2 = "uint2";
public const string UINT3 = "uint3";
public const string UINT4 = "uint4";
public const string BOOL = "bool";
public const string BOOL2 = "bool2";
public const string BOOL3 = "bool3";
public const string BOOL4 = "bool4";
// Texture types
public const string TEXTURE2D = "texture2d";
public const string TEXTURE2D_ARRAY = "texture2d_array";
@@ -201,7 +201,7 @@ internal static class TokenLexicon
private static readonly HashSet<string> s_functions = new()
{
KnownFunctions.VERTEX_SHADER,
KnownFunctions.TASK_SHADER,
KnownFunctions.PIXEL_SHADER,
KnownFunctions.MESH_SHADER,
KnownFunctions.COMPUTE_SHADER,
@@ -229,9 +229,9 @@ internal static class TokenLexicon
};
public static bool IsKeyword(string lexeme) => s_keywords.Contains(lexeme);
public static bool IsFunction(string lexeme) => s_functions.Contains(lexeme);
public static bool IsType(string lexeme) => s_types.Contains(lexeme);
public static bool IsTextureDefaultValue(string lexeme) => s_textureDefaultValues.Contains(lexeme);