Refactor UI panels, shader logic, and project settings

Refactored Hierarchy and Inspector panels into separate controls with improved styling and modularity. Cached EngineCore in EditorShaderCompilerBridge and updated shader cache logic for correctness. Renamed static field in PropertyField for consistency. Enhanced test coverage and fixed IDisposable implementation. Added XAML debugging properties for Debug_Editor and cleaned up obsolete scripts.
This commit is contained in:
2026-05-09 15:20:22 +09:00
parent 1cc65e8218
commit e2bc68d359
11 changed files with 219 additions and 261 deletions

View File

@@ -9,7 +9,7 @@ namespace Ghost.Editor.Core.Controls;
public sealed partial class PropertyField : ContentControl public sealed partial class PropertyField : ContentControl
{ {
private static readonly Dictionary<Type, DependencyProperty> _valueProperties = new() private static readonly Dictionary<Type, DependencyProperty> s_valueProperties = new()
{ {
{ typeof(TextBox), TextBox.TextProperty }, { typeof(TextBox), TextBox.TextProperty },
{ typeof(NumberBox), NumberBox.ValueProperty }, { typeof(NumberBox), NumberBox.ValueProperty },
@@ -48,7 +48,7 @@ public sealed partial class PropertyField : ContentControl
{ {
while (fieldType != null) while (fieldType != null)
{ {
if (_valueProperties.TryGetValue(fieldType, out var dp)) if (s_valueProperties.TryGetValue(fieldType, out var dp))
{ {
return dp; return dp;
} }

View File

@@ -19,6 +19,7 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
private readonly IAssetRegistry _assetRegistry; private readonly IAssetRegistry _assetRegistry;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly IShaderCompiler _compiler; private readonly IShaderCompiler _compiler;
private EngineCore? _engineCore;
private readonly ConcurrentDictionary<ulong, Guid> _shaderIdToAssetId = new(); private readonly ConcurrentDictionary<ulong, Guid> _shaderIdToAssetId = new();
private readonly ConcurrentDictionary<Guid, Dictionary<int, string>[]> _assetKeywordMappings = new(); private readonly ConcurrentDictionary<Guid, Dictionary<int, string>[]> _assetKeywordMappings = new();
@@ -49,11 +50,11 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
_shaderIdToAssetId[nameHash] = guid; _shaderIdToAssetId[nameHash] = guid;
BuildKeywordMappings(result.Value, guid); BuildKeywordMappings(result.Value, guid);
var engineCore = _serviceProvider.GetService<EngineCore>(); _engineCore ??= _serviceProvider.GetService<EngineCore>();
if (engineCore != null) if (_engineCore != null)
{ {
var shaderLibrary = engineCore.RenderSystem.ShaderLibrary; var shaderLibrary = _engineCore.RenderSystem.ShaderLibrary;
var pipelineLibrary = engineCore.RenderSystem.GraphicsEngine.PipelineLibrary; var pipelineLibrary = _engineCore.RenderSystem.GraphicsEngine.PipelineLibrary;
shaderLibrary.InvalidateShaderCache(nameHash, pipelineLibrary); shaderLibrary.InvalidateShaderCache(nameHash, pipelineLibrary);
} }
} }
@@ -264,12 +265,11 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
var compileResult = _compiler.CompileShaderPass(ref descriptor, ref additionalConfig, AllocationHandle.Persistent); var compileResult = _compiler.CompileShaderPass(ref descriptor, ref additionalConfig, AllocationHandle.Persistent);
if (compileResult.IsFailure) if (compileResult.IsFailure)
{ {
Ghost.Core.Logger.Error($"Failed to compile graphics shader {shaderId}: {compileResult.Message}"); Logger.Error($"Failed to compile graphics shader {shaderId}: {compileResult.Message}");
return Task.CompletedTask; return Task.CompletedTask;
} }
var engineCore = _serviceProvider.GetService<EngineCore>(); if (_engineCore == null)
if (engineCore == null)
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -298,7 +298,7 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
byteCodes[idx++] = new ShaderByteCode { pCode = (byte*)compiled.psResult.GetUnsafePtr(), size = (ulong)compiled.psResult.Length }; byteCodes[idx++] = new ShaderByteCode { pCode = (byte*)compiled.psResult.GetUnsafePtr(), size = (ulong)compiled.psResult.Length };
} }
var shaderLibrary = engineCore.RenderSystem.ShaderLibrary; var shaderLibrary = _engineCore.RenderSystem.ShaderLibrary;
shaderLibrary.CacheCompiledResult(shaderId, passIndex, variantKey, new ReadOnlySpan<ShaderByteCode>(byteCodes, stageCount)); shaderLibrary.CacheCompiledResult(shaderId, passIndex, variantKey, new ReadOnlySpan<ShaderByteCode>(byteCodes, stageCount));
var (compiledHash, _) = shaderLibrary.GetCompiledHash(shaderId, passIndex, variantKey); var (compiledHash, _) = shaderLibrary.GetCompiledHash(shaderId, passIndex, variantKey);
@@ -327,12 +327,11 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
var compileResult = _compiler.Compile(ref config, AllocationHandle.Persistent); var compileResult = _compiler.Compile(ref config, AllocationHandle.Persistent);
if (compileResult.IsFailure) if (compileResult.IsFailure)
{ {
Ghost.Core.Logger.Error($"Failed to compile compute shader {shaderId}: {compileResult.Message}"); Logger.Error($"Failed to compile compute shader {shaderId}: {compileResult.Message}");
return Task.CompletedTask; return Task.CompletedTask;
} }
var engineCore = _serviceProvider.GetService<EngineCore>(); if (_engineCore == null)
if (engineCore == null)
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -345,7 +344,7 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
size = (ulong)bytecodeArray.Length size = (ulong)bytecodeArray.Length
}; };
var shaderLibrary = engineCore.RenderSystem.ShaderLibrary; var shaderLibrary = _engineCore.RenderSystem.ShaderLibrary;
shaderLibrary.CacheCompiledResult(shaderId, passIndex, variantKey, new ReadOnlySpan<ShaderByteCode>(ref byteCode)); shaderLibrary.CacheCompiledResult(shaderId, passIndex, variantKey, new ReadOnlySpan<ShaderByteCode>(ref byteCode));
var (compiledHash, _) = shaderLibrary.GetCompiledHash(shaderId, passIndex, variantKey); var (compiledHash, _) = shaderLibrary.GetCompiledHash(shaderId, passIndex, variantKey);

View File

@@ -12,6 +12,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="Views\Controls\Hierarchy.xaml" /> <None Remove="Views\Controls\Hierarchy.xaml" />
<None Remove="Views\Controls\Inspector.xaml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" /> <Content Include="Assets\SplashScreen.scale-200.png" />
@@ -140,6 +141,9 @@
<None Update="Assets\icon.ico"> <None Update="Assets\icon.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<Page Update="Views\Controls\Inspector.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\Controls\LogViewer.xaml"> <Page Update="Views\Controls\LogViewer.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
@@ -214,6 +218,11 @@
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu> <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug_Editor'">
<XamlDebuggingInformation>True</XamlDebuggingInformation>
<DisableXbfLineInfo>False</DisableXbfLineInfo>
</PropertyGroup>
<!-- Publish Properties --> <!-- Publish Properties -->
<PropertyGroup> <PropertyGroup>
<PublishReadyToRun Condition="'$(Configuration)'=='Debug_Editor'">False</PublishReadyToRun> <PublishReadyToRun Condition="'$(Configuration)'=='Debug_Editor'">False</PublishReadyToRun>

View File

@@ -14,49 +14,55 @@
<RowDefinition Height="*" /> <RowDefinition Height="*" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid <StackPanel
Grid.Row="0" Grid.Row="0"
Height="40" Padding="8,2,4,4"
Padding="8,8,8,4" Background="{ThemeResource CardBackgroundFillColorDefaultBrush}">
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}" <Grid>
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}" <Grid.ColumnDefinitions>
BorderThickness="0,0,0,1"> <ColumnDefinition Width="*" />
<Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" /> </Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<AutoSuggestBox <TextBlock
Grid.Column="0" Grid.Column="0"
PlaceholderText="Search" HorizontalAlignment="Left"
QueryIcon="Find" /> VerticalAlignment="Center"
<StackPanel Grid.Column="1" Orientation="Horizontal"> Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
<AppBarSeparator /> Style="{StaticResource BodyLargeStrongTextBlockStyle}"
<Button Style="{ThemeResource ToolbarButton}"> Text="Hierarchy" />
<FontIcon FontSize="{StaticResource ToolbarIconSize}" Glyph="&#xE8F4;" /> <Button
Grid.Column="1"
HorizontalAlignment="Right"
Style="{ThemeResource ToolbarButton}">
<FontIcon Glyph="&#xE712;" />
</Button> </Button>
</StackPanel> </Grid>
</Grid>
<Border Grid.Row="1" Padding="4"> <Grid Margin="0,2">
<ListView> <Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Spacing="4"> <ColumnDefinition Width="Auto" />
<FontIcon FontSize="{StaticResource ToolbarIconSize}" Glyph="&#xF159;" /> <ColumnDefinition Width="*" />
<TextBlock Text="Test" /> </Grid.ColumnDefinitions>
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="4"> <FontIcon
<FontIcon FontSize="{StaticResource ToolbarIconSize}" Glyph="&#xF159;" /> Grid.Column="0"
<TextBlock Text="Test" /> Margin="0,0,4,0"
</StackPanel> FontSize="{StaticResource ToolbarFontIconFontSize}"
<StackPanel Orientation="Horizontal" Spacing="4"> Glyph="&#xE721;" />
<FontIcon FontSize="{StaticResource ToolbarIconSize}" Glyph="&#xF159;" /> <TextBox Grid.Column="1" PlaceholderText="Sreach item..." />
<TextBlock Text="Test" /> </Grid>
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="4"> <Border Margin="-8,8,-4,-4" Style="{StaticResource HorizontalStrongDivider}" />
<FontIcon FontSize="{StaticResource ToolbarIconSize}" Glyph="&#xF159;" /> </StackPanel>
<TextBlock Text="Test" />
</StackPanel> <ListView Grid.Row="1" Padding="4,2,0,2">
</ListView> <ListViewItem Content="Test" />
</Border> <ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
</ListView>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8" ?>
<UserControl
x:Class="Ghost.Editor.Views.Controls.Inspector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Ghost.Editor.Views.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Inspector Header -->
<Grid
Grid.Row="0"
Padding="12,12,8,12"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource ControlElevationBorderBrush}"
BorderThickness="0,0,0,1"
ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FontIcon
Grid.Column="0"
FontSize="18"
Glyph="&#xF158;" />
<TextBox
Grid.Column="1"
FontSize="14"
Text="Name" />
<DropDownButton
Grid.Column="2"
Padding="2"
Style="{ThemeResource ToolbarButton}">
<DropDownButton.Flyout>
<MenuFlyout Placement="Bottom">
<MenuFlyoutItem Text="Send" />
<MenuFlyoutItem Text="Reply" />
<MenuFlyoutItem Text="Reply All" />
</MenuFlyout>
</DropDownButton.Flyout>
<FontIcon FontSize="12" Glyph="&#xF8B0;" />
</DropDownButton>
</Grid>
<!-- Content -->
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" MaxHeight="150" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Padding="8,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
VerticalAlignment="Center"
Style="{StaticResource BodyStrongTextBlockStyle}"
Text="Components" />
<Button Grid.Column="1" Style="{ThemeResource ToolbarButton}">
<FontIcon FontSize="{StaticResource ToolbarFontIconFontSize}" Glyph="&#xE710;" />
</Button>
<Button Grid.Column="2" Style="{ThemeResource ToolbarButton}">
<FontIcon FontSize="{StaticResource ToolbarFontIconFontSize}" Glyph="&#xE738;" />
</Button>
</Grid>
<AutoSuggestBox
Grid.Row="1"
Margin="8,0"
PlaceholderText="Search components..." />
<!-- Components List -->
<ListView
Grid.Row="2"
Padding="4,2,0,2"
SelectionMode="Extended">
<TextBlock Text="Test" />
<TextBlock Text="Test" />
<TextBlock Text="Test" />
</ListView>
<!-- Component Properties for Selected Component -->
<ScrollView
Grid.Row="3"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="0,1,0,0">
<ItemsRepeater />
</ScrollView>
</Grid>
</Grid>
</UserControl>

View File

@@ -0,0 +1,27 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Ghost.Editor.Views.Controls;
public sealed partial class Inspector : UserControl
{
public Inspector()
{
InitializeComponent();
}
}

View File

@@ -95,67 +95,12 @@
<ColumnDefinition Width="0.25*" MaxWidth="350" /> <ColumnDefinition Width="0.25*" MaxWidth="350" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<!-- Hierarchy --> <Border
<Grid
Grid.Column="0" Grid.Column="0"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}" BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="0,0,1,0"> BorderThickness="0,0,1,0">
<Grid.RowDefinitions> <controls:Hierarchy />
<RowDefinition Height="Auto" /> </Border>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="0"
Padding="8,2,4,4"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
Style="{StaticResource BodyLargeStrongTextBlockStyle}"
Text="Hierarchy" />
<Button
Grid.Column="1"
HorizontalAlignment="Right"
Style="{ThemeResource ToolbarButton}">
<FontIcon Glyph="&#xE712;" />
</Button>
</Grid>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<FontIcon
Grid.Column="0"
Margin="0,0,4,0"
FontSize="{StaticResource ToolbarFontIconFontSize}"
Glyph="&#xE721;" />
<TextBox Grid.Column="1" PlaceholderText="Sreach item..." />
</Grid>
<Border Margin="-8,8,-4,-4" Style="{StaticResource HorizontalStrongDivider}" />
</StackPanel>
<ListView Grid.Row="1" Padding="4,2,0,2">
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
<ListViewItem Content="Test" />
</ListView>
</Grid>
<!-- Scene and Content --> <!-- Scene and Content -->
<Grid Grid.Column="1"> <Grid Grid.Column="1">
@@ -195,7 +140,6 @@
Stretch="UniformToFill" /> Stretch="UniformToFill" />
</Border> </Border>
<!-- Content Brower --> <!-- Content Brower -->
<Border <Border
Grid.Row="2" Grid.Row="2"
@@ -213,105 +157,12 @@
</Grid> </Grid>
<!-- Inspector --> <!-- Inspector -->
<Grid <Border
Grid.Column="2" Grid.Column="2"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}" BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="1,0,0,0"> BorderThickness="1,0,0,0">
<Grid.RowDefinitions> <controls:Inspector />
<RowDefinition Height="Auto" /> </Border>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Inspector Header -->
<Grid
Grid.Row="0"
Padding="12,12,8,12"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource ControlElevationBorderBrush}"
BorderThickness="0,0,0,1"
ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FontIcon
Grid.Column="0"
FontSize="18"
Glyph="&#xF158;" />
<TextBox
Grid.Column="1"
FontSize="14"
Text="Name" />
<DropDownButton
Grid.Column="2"
Padding="2"
Style="{ThemeResource ToolbarButton}">
<DropDownButton.Flyout>
<MenuFlyout Placement="Bottom">
<MenuFlyoutItem Text="Send" />
<MenuFlyoutItem Text="Reply" />
<MenuFlyoutItem Text="Reply All" />
</MenuFlyout>
</DropDownButton.Flyout>
<FontIcon FontSize="12" Glyph="&#xF8B0;" />
</DropDownButton>
</Grid>
<!-- Content -->
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" MaxHeight="150" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Padding="8,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
VerticalAlignment="Center"
Style="{StaticResource BodyStrongTextBlockStyle}"
Text="Components" />
<Button Grid.Column="1" Style="{ThemeResource ToolbarButton}">
<FontIcon FontSize="{StaticResource ToolbarFontIconFontSize}" Glyph="&#xE710;" />
</Button>
<Button Grid.Column="2" Style="{ThemeResource ToolbarButton}">
<FontIcon FontSize="{StaticResource ToolbarFontIconFontSize}" Glyph="&#xE738;" />
</Button>
</Grid>
<AutoSuggestBox
Grid.Row="1"
Margin="8,0"
PlaceholderText="Search components..." />
<!-- Components List -->
<ListView
Grid.Row="2"
Padding="4,2,0,2"
SelectionMode="Extended">
<TextBlock Text="Test" />
<TextBlock Text="Test" />
<TextBlock Text="Test" />
</ListView>
<!-- Component Properties for Selected Component -->
<ScrollView
Grid.Row="3"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="0,1,0,0">
<ItemsRepeater />
</ScrollView>
</Grid>
</Grid>
</Grid> </Grid>
</Grid> </Grid>
</Page> </Page>

View File

@@ -91,8 +91,6 @@ namespace Ghost.Generator
continue; continue;
} }
var definedSymbol = $"__{info.Name.ToUpper()}_G_HLSL";
var fieldsBuilder = new StringBuilder(); var fieldsBuilder = new StringBuilder();
fieldsBuilder.AppendLine($" public static readonly global::Ghost.Core.Graphics.ShaderPropertyFieldInfo[] ReflectionData = new global::Ghost.Core.Graphics.ShaderPropertyFieldInfo[]"); fieldsBuilder.AppendLine($" public static readonly global::Ghost.Core.Graphics.ShaderPropertyFieldInfo[] ReflectionData = new global::Ghost.Core.Graphics.ShaderPropertyFieldInfo[]");
fieldsBuilder.AppendLine(" {"); fieldsBuilder.AppendLine(" {");
@@ -204,13 +202,10 @@ namespace {info.TypeSymbol.ContainingNamespace.ToDisplayString()}
{{ {{
#if GHOST_EDITOR #if GHOST_EDITOR
public const string HLSL_SOURCE = @"" public const string HLSL_SOURCE = @""
# ifndef {definedSymbol}
# define {definedSymbol}
struct {info.Name} struct {info.Name}
{{ {{
{codeBuilder} {codeBuilder}
}}; }};"";
# endif // {definedSymbol}"";
{fieldsBuilder} {fieldsBuilder}
#endif #endif

View File

@@ -128,7 +128,8 @@ internal unsafe class ShaderLibrary : IDisposable
}; };
var offsets = stackalloc ulong[byteCodes.Length]; var offsets = stackalloc ulong[byteCodes.Length];
var offset = (nuint)(sizeof(CacheHeader) + (sizeof(ulong) * byteCodes.Length)); var headerSize = (nuint)(sizeof(CacheHeader) + (sizeof(ulong) * byteCodes.Length));
var offset = headerSize;
for (var i = 0; i < byteCodes.Length; i++) for (var i = 0; i < byteCodes.Length; i++)
{ {
offsets[i] = offset; offsets[i] = offset;
@@ -136,9 +137,9 @@ internal unsafe class ShaderLibrary : IDisposable
} }
var alignment = Math.Max(Math.Max(MemoryUtility.AlignOf<CacheHeader>(), MemoryUtility.AlignOf<ulong>()), 8); var alignment = Math.Max(Math.Max(MemoryUtility.AlignOf<CacheHeader>(), MemoryUtility.AlignOf<ulong>()), 8);
offset = MemoryUtility.AlignUp(offset, alignment); var alignedOffset = MemoryUtility.AlignUp(offset, alignment);
var data = new MemoryBlock(offset, alignment, AllocationHandle.Persistent); var data = new MemoryBlock(alignedOffset, alignment, AllocationHandle.Persistent);
var writer = new SpanWriter(data.AsSpan<byte>()); var writer = new SpanWriter(data.AsSpan<byte>());
writer.Write(header); writer.Write(header);
@@ -159,11 +160,7 @@ internal unsafe class ShaderLibrary : IDisposable
var codeHash = 0UL; var codeHash = 0UL;
if (byteCodes.Length > 0) if (byteCodes.Length > 0)
{ {
ulong totalBytecodeSize = 0; var bytecodeSpan = data.AsSpan<byte>().Slice((int)headerSize, (int)(offset - headerSize));
for (int i = 0; i < byteCodes.Length; i++) totalBytecodeSize += byteCodes[i].size;
// We skip the header and offsets at the beginning of the MemoryBlock
var bytecodeSpan = data.AsSpan<byte>().Slice((int)(sizeof(CacheHeader) + (sizeof(ulong) * byteCodes.Length)), (int)totalBytecodeSize);
codeHash = XxHash64.HashToUInt64(bytecodeSpan); codeHash = XxHash64.HashToUInt64(bytecodeSpan);
} }

View File

@@ -42,6 +42,10 @@ public class ShaderLibraryTest
{ {
OnShaderVariantCompiled?.Invoke(variantKey, newHash); OnShaderVariantCompiled?.Invoke(variantKey, newHash);
} }
public void Dispose()
{
}
} }
[TestInitialize] [TestInitialize]
@@ -151,8 +155,7 @@ public class ShaderLibraryTest
{ {
// Arrange // Arrange
using var shaderLibrary = new ShaderLibrary(null, "TestShaderCache"); using var shaderLibrary = new ShaderLibrary(null, "TestShaderCache");
ulong testShaderId = 111; var testShaderId = 111UL;
var scope = AllocationManager.CreateStackScope();
// Act // Act
var result = shaderLibrary.GetCompiledCache(testShaderId, 99); var result = shaderLibrary.GetCompiledCache(testShaderId, 99);
@@ -160,7 +163,5 @@ public class ShaderLibraryTest
// Assert // Assert
Assert.IsFalse(result.IsSuccess); Assert.IsFalse(result.IsSuccess);
Assert.AreEqual(Error.NotFound, result.Error); Assert.AreEqual(Error.NotFound, result.Error);
scope.Dispose();
} }
} }

View File

@@ -1,34 +0,0 @@
using System;
class Program {
static void Main() {
string definedSymbol = "__FOO_HLSL__";
string infoName = "Foo";
string codeBuilder = " float4 myColor;\n";
string fieldsBuilder = " // fields";
var code = $$"""
// <auto-generated/>
namespace MyNamespace
{
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 4)]
public partial struct {{infoName}}
{
#if GHOST_EDITOR
public const string HLSL_SOURCE = @"
#ifndef {{definedSymbol}}
#define {{definedSymbol}}
struct {{infoName}}
{
{{codeBuilder}}
};
#endif // {{definedSymbol}}";
{{fieldsBuilder}}
#endif
}
}
""";
Console.WriteLine(code);
}
}