Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Serialize content to/from binary using BinaryPack
Browse files Browse the repository at this point in the history
  • Loading branch information
xezno committed Jul 13, 2024
1 parent 3856cd2 commit da5b32a
Show file tree
Hide file tree
Showing 40 changed files with 68 additions and 24 deletions.
1 change: 1 addition & 0 deletions Source/Mocha.Common/Apparatus.Core.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BinaryPack" Version="1.0.3" />
<PackageReference Include="Veldrid.SPIRV" Version="1.0.15" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions Source/Mocha.Common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

global using static Globals;

global using Apparatus.Core.Common;

public static class Globals
{
public static Logger Log { get; } = new();
Expand Down
45 changes: 45 additions & 0 deletions Source/Mocha.Common/Serializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BinaryPack;
using System.IO.Compression;

namespace Apparatus.Core.Common;

public static class Serializer
{
public static byte[] Serialize<T>(T obj) where T : new()
{
var serialized = BinaryConverter.Serialize(obj);

return Compress(serialized);
}

public static T? Deserialize<T>(byte[] data) where T : new()
{
var serialized = Decompress(data);

return BinaryConverter.Deserialize<T>(serialized);
}

public static byte[] Compress(byte[] uncompressedData)
{
using var stream = new MemoryStream();
using var deflate = new DeflateStream(stream, CompressionLevel.Fastest);

deflate.Write(uncompressedData);
deflate.Close();

return stream.ToArray();
}

public static byte[] Decompress(byte[] compressedData)
{
using var outputStream = new MemoryStream();

using (var compressStream = new MemoryStream(compressedData))
{
using var deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress);
deflateStream.CopyTo(outputStream);
}

return outputStream.ToArray();
}
}
11 changes: 5 additions & 6 deletions Source/Mocha.Example/MyApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class MyApp : MochaApplication
private ImageTexture _imageTexture = null!;

private readonly float[] _vertices = [
// vec2 pos, vec3 color vec2 uv
-1,-1, 0,0,1, 0,0,
1,-1, 0,1,1, 1,0,
1,1, 1,1,0, 1,1,
-1,1, 1,0,0, 0,1
// vec2 pos, vec2 uv
-1,-1, 0,0,
1,-1, 1,0,
1,1, 1,1,
-1,1, 0,1
];

private readonly uint[] _indices = [0, 1, 2, 2, 3, 0];
Expand Down Expand Up @@ -97,7 +97,6 @@ protected override void OnBootstrap()
Descriptors = [_descriptor],
VertexAttributes = [
new VertexAttributeInfo() { Format = VertexAttributeFormat.Float2, Name = "Position" },
new VertexAttributeInfo() { Format = VertexAttributeFormat.Float3, Name = "Color" },
new VertexAttributeInfo() { Format = VertexAttributeFormat.Float2, Name = "TexCoord" },
]
} );
Expand Down
Binary file modified Source/Mocha.Example/fs/content/materials/Floor.material
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/Floor_base.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/Floor_normal.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/Floor_packed.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/PBR/Floor_ao.texture
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/PBR/ice_color.texture
Binary file not shown.
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/PBR/ice_normal.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/PBR/ice_rough.texture
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/ice.material
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/ice_base.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/ice_normal.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/ice_packed.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/tiles.material
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/tiles_base.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/tiles_normal.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/materials/tiles_packed.texture
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/shaders/default.shader
Binary file not shown.
Binary file modified Source/Mocha.Example/fs/content/test.texture
Binary file not shown.
13 changes: 4 additions & 9 deletions Source/Mocha.Example/fs/src/shaders/default.shader.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,24 @@
Vertex
{
layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 1) in vec2 inTexCoord;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 texCoord;
layout(location = 0) out vec2 texCoord;

void main() {
gl_Position = vec4(inPosition, 0.0, 1.0);
fragColor = inColor;
texCoord = inTexCoord;
}
}

Fragment
{
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 texCoord;
layout(location = 0) in vec2 texCoord;
layout(location = 0) out vec4 outColor;

layout(binding = 0) uniform sampler2D textureSampler;

void main() {
vec4 texColor = texture(textureSampler, texCoord);
outColor = vec4(fragColor, 1.0) * texColor;
outColor = texture(textureSampler, texCoord);
}
}
Binary file added Source/Mocha.Example/fs/src/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Source/Mocha.Example/fs/src/test.png
Binary file not shown.
8 changes: 4 additions & 4 deletions Source/Mocha.Generators/ResourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ResourceGenerator : IIncrementalGenerator

private const string ResourceAttribute = "Mocha.ResourceAttribute";
private const string FileSystemContent = "global::Mocha.FileSystem.Content";
private const string FileSystemReadAllTextMethod = "ReadAllText";
private const string FileSystemReadAllBytesMethod = "ReadAllBytes";

public void Initialize( IncrementalGeneratorInitializationContext context )
{
Expand Down Expand Up @@ -94,8 +94,8 @@ private void Execute( SourceProductionContext context, ImmutableArray<ResourceDa
writer.WriteLine( "{" );
writer.Indent++;

writer.WriteLine( $"var file = {FileSystemContent}.{FileSystemReadAllTextMethod}( filePath );" );
writer.WriteLine( $"return global::System.Text.Json.JsonSerializer.Deserialize<{entry.TypeName}>( file );" );
writer.WriteLine( $"var file = {FileSystemContent}.{FileSystemReadAllBytesMethod}( filePath );" );
writer.WriteLine( $"return global::Apparatus.Core.Common.Serializer.Deserialize<{entry.TypeName}>( file );" );

writer.Indent--;
writer.WriteLine( "}" );
Expand All @@ -115,7 +115,7 @@ private void Execute( SourceProductionContext context, ImmutableArray<ResourceDa
writer.WriteLine( "{" );
writer.Indent++;

writer.WriteLine( $"return global::System.Text.Json.JsonSerializer.Deserialize<{entry.TypeName}>( data );" );
writer.WriteLine( $"return global::Apparatus.Core.Common.Serializer.Deserialize<{entry.TypeName}>( data );" );

writer.Indent--;
writer.WriteLine( "}" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task<CompileResult> CompileFile( CompileInput compileInput )
// todo: Process?
//

return CompileResult.Success( JsonSerializer.SerializeToUtf8Bytes( materialData ) );
return CompileResult.Success( Serializer.Serialize( materialData ) );
} );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ public async Task<CompileResult> CompileFile( CompileInput compileInput )
if ( shaderFile.Fragment != null )
shader.FragmentData = await CompileShader( shaderFile.Common, shaderFile.Fragment, ShaderStages.Fragment, debugName );

return CompileResult.Success( JsonSerializer.SerializeToUtf8Bytes( shader ) );
return CompileResult.Success( Serializer.Serialize( shader ) );
}
}
2 changes: 1 addition & 1 deletion Source/Mocha.ResourceCompiler/Compilers/TextureCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public async Task<CompileResult> CompileFile( CompileInput compileInput )
Format = TextureFormat.Bc3,
};

return CompileResult.Success( JsonSerializer.SerializeToUtf8Bytes( textureData ) );
return CompileResult.Success( Serializer.Serialize( textureData ) );
}
}
2 changes: 2 additions & 0 deletions Source/Mocha.ResourceCompiler/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;

global using Apparatus.Core.Common;
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static void WriteMaterialFile( string destinationPath )
PackedTexture = $"{assetPath.NormalizePath()}_packed.png"
};

var fileData = JsonSerializer.Serialize( materialData );
File.WriteAllText( destinationPath + ".material.json", fileData );
var fileData = Serializer.Serialize( materialData );
File.WriteAllBytes( destinationPath + ".material.json", fileData );
}
}

0 comments on commit da5b32a

Please sign in to comment.