Skip to content

Commit

Permalink
Reduce the amount of size that SpriteBatchData would take
Browse files Browse the repository at this point in the history
  • Loading branch information
Terria-K committed Jan 22, 2025
1 parent 34482e0 commit b62eaa6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 34 deletions.
16 changes: 8 additions & 8 deletions Riateu/Core/Graphics/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public unsafe void Draw(TextureQuad quad, Vector2 position, Color color, Vector2
Position = new Vector3(position, layerDepth),
Scale = new Vector2(quad.Source.Width, quad.Source.Height) * scale,
Origin = origin * scale,
UV = quad.UV,
UV = new Vector4(quad.UV.TopLeft.X, quad.UV.TopLeft.Y, quad.UV.BottomRight.X, quad.UV.BottomRight.Y),
Rotation = rotation,
Color = color.ToVector4(),
};
Expand Down Expand Up @@ -464,20 +464,20 @@ private struct BatchQueue
public Matrix4x4 Matrix;
}

[StructLayout(LayoutKind.Explicit, Size = 80)]
[StructLayout(LayoutKind.Explicit, Size = 64)]
public struct BatchData
{
[FieldOffset(0)]
public UV UV;
[FieldOffset(32)]
public Vector4 UV;
[FieldOffset(16)]
public Vector3 Position;
[FieldOffset(44)]
[FieldOffset(28)]
public float Rotation;
[FieldOffset(48)]
[FieldOffset(32)]
public Vector4 Color;
[FieldOffset(64)]
[FieldOffset(48)]
public Vector2 Scale;
[FieldOffset(72)]
[FieldOffset(56)]
public Vector2 Origin;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Riateu/Core/Graphics/SpriteFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ internal void Draw(Span<Batch.BatchData> computeData, ref uint vertexIndex, Read
Position = new Vector3(pos, layerDepth),
Scale = new Vector2(c.Quad.Source.Width, c.Quad.Source.Height) * scale,
Origin = Vector2.Zero,
UV = c.Quad.UV,
UV = new Vector4(c.Quad.UV.TopLeft.X, c.Quad.UV.TopLeft.Y, c.Quad.UV.BottomRight.X, c.Quad.UV.BottomRight.Y),
Rotation = 0,
Color = color.ToVector4(),
};
Expand Down
Binary file modified Riateu/Core/Misc/Compiled/Spritebatch.vert.spv
Binary file not shown.
9 changes: 2 additions & 7 deletions Riateu/Core/Misc/Shaders/ImGuiShader.vert.slang
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
struct VertexInput
{
float2 Position : POSITION0;
float2 TexCoord : TEXCOORD1;
float4 Color : COLOR2;
};
import common.structs;

struct VertexOutput
{
Expand All @@ -19,7 +14,7 @@ cbuffer UniformBlock : register(b0, space1)
};

[shader("vertex")]
VertexOutput main(VertexInput input)
VertexOutput main(Position2DTextureColor input)
{
VertexOutput output;
output.Position = mul(ViewProjectionMatrix, float4(input.Position.xy, 0., 1.));
Expand Down
9 changes: 2 additions & 7 deletions Riateu/Core/Misc/Shaders/PositionTextureColor.vert.slang
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
struct VertexInput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD1;
float4 Color : COLOR2;
};
import common.structs;

struct VertexOutput
{
Expand All @@ -19,7 +14,7 @@ cbuffer UniformBlock : register(b0, space1)
};

[shader("vertex")]
VertexOutput main(VertexInput input)
VertexOutput main(PositionTextureColor input)
{
VertexOutput output;
output.Position = mul(ViewProjectionMatrix, input.Position);
Expand Down
23 changes: 12 additions & 11 deletions Riateu/Core/Misc/Shaders/Spritebatch.vert.slang
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
struct SpriteBatchData
{
float2 UV[4]; // 32
float2 UV[2];

float3 Position;
float Rotation; // 48
float Rotation;

float4 Color; // 64
float4 Color;

float2 Scale;
float2 Origin; // 80
float2 Origin;
};

struct VertexOutput
Expand All @@ -27,11 +27,11 @@ cbuffer UniformBlock : register(b0, space1)
float4x4 ViewProjectionMatrix : packoffset(c0);
};

static const float2 vertices[4] = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 0.0f, 1.0f },
{ 1.0f, 1.0f },
static const uint2 vertices[4] = {
{ 0, 0 },
{ 1, 0 },
{ 0, 1 },
{ 1, 1 },
};

[shader("vertex")]
Expand All @@ -45,7 +45,8 @@ VertexOutput main(uint id: SV_VertexID, uint instanceID: SV_InstanceID)
float c = cos(sprite.Rotation);
float s = sin(sprite.Rotation);

float2 coord = vertices[vert] * sprite.Scale;
uint2 vertIndex = vertices[vert];
float2 coord = vertIndex * sprite.Scale;

float2x2 rotation = { c, s, -s, c };
coord -= sprite.Origin;
Expand All @@ -55,7 +56,7 @@ VertexOutput main(uint id: SV_VertexID, uint instanceID: SV_InstanceID)

VertexOutput output;
output.Position = mul(ViewProjectionMatrix, float4(coordDepth, 1.0f));
output.TexCoord = sprite.UV[vert % 4];
output.TexCoord = float2(sprite.UV[vertIndex.x].x, sprite.UV[vertIndex.y].y);
output.Color = sprite.Color;

return output;
Expand Down
13 changes: 13 additions & 0 deletions Riateu/Core/Misc/Shaders/common/structs.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct Position2DTextureColor
{
float2 Position : POSITION0;
float2 TexCoord : TEXCOORD1;
float4 Color : COLOR2;
};

struct PositionTextureColor
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD1;
float4 Color : COLOR2;
};
2 changes: 2 additions & 0 deletions scripts/compile_shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

shaders = []
for (dirpath, dirnames, filenames) in os.walk("Shaders"):
if dirpath == "Shaders/common":
continue
shaders = filenames

for shader in shaders:
Expand Down

0 comments on commit b62eaa6

Please sign in to comment.