diff --git a/content/sprite.frag.spv b/content/sprite.frag.spv index 89181f8..a445680 100644 Binary files a/content/sprite.frag.spv and b/content/sprite.frag.spv differ diff --git a/content/sprite.vert.spv b/content/sprite.vert.spv index 3804160..654effa 100644 Binary files a/content/sprite.vert.spv and b/content/sprite.vert.spv differ diff --git a/content/spritebatch.comp.spv b/content/spritebatch.comp.spv index fc2529a..b4308a4 100644 Binary files a/content/spritebatch.comp.spv and b/content/spritebatch.comp.spv differ diff --git a/moonlibs/windows/Refresh.dll b/moonlibs/windows/Refresh.dll index 2202a38..8064ee8 100644 --- a/moonlibs/windows/Refresh.dll +++ b/moonlibs/windows/Refresh.dll @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5e283c69d0930dc39ff684524d8fb36e2c96d9e602dea33e6da84731b56ca72e -size 340992 +oid sha256:4de87c9bf2a8f6286d7adfcaaa5554590370049f67127495fc289572f86b9629 +size 122880 diff --git a/shaders/sprite.frag b/shaders/sprite.frag index bad4382..f757247 100644 --- a/shaders/sprite.frag +++ b/shaders/sprite.frag @@ -3,9 +3,11 @@ layout(set = 1, binding = 0) uniform sampler2D uniformTexture; layout(location = 0) in vec2 fragCoord; +layout(location = 1) in vec4 color; + layout(location = 0) out vec4 fragColor; void main() { - fragColor = texture(uniformTexture, fragCoord); + fragColor = texture(uniformTexture, fragCoord) * color; } diff --git a/shaders/sprite.vert b/shaders/sprite.vert index ebdfbeb..3f6785e 100644 --- a/shaders/sprite.vert +++ b/shaders/sprite.vert @@ -2,8 +2,10 @@ layout(location = 0) in vec3 inPosition; layout(location = 1) in vec2 inTexCoord; +layout(location = 2) in vec4 inColor; layout(location = 0) out vec2 fragCoord; +layout(location = 1) out vec4 color; layout(set = 2, binding = 0) uniform UBO { @@ -14,4 +16,5 @@ void main() { gl_Position = ubo.viewProjection * vec4(inPosition, 1.0); fragCoord = inTexCoord; + color = inColor; } diff --git a/shaders/spritebatch.comp b/shaders/spritebatch.comp index 6a17c04..f7a1b2d 100644 --- a/shaders/spritebatch.comp +++ b/shaders/spritebatch.comp @@ -4,6 +4,7 @@ struct Vertex { vec3 position; vec2 texcoord; + vec4 color; }; // Binding 0: vertices diff --git a/src/SpriteBatch.cs b/src/SpriteBatch.cs index 1d6ba59..40ee580 100644 --- a/src/SpriteBatch.cs +++ b/src/SpriteBatch.cs @@ -82,27 +82,43 @@ namespace MoonWorksComputeSpriteBatch CurrentSampler = sampler; } - public void Add(Sprite sprite, Matrix4x4 transform) + public void Add(Sprite sprite, Matrix4x4 transform, Color color) { Vertices[VertexCount].position.X = 0; Vertices[VertexCount].position.Y = 0; Vertices[VertexCount].texcoord.X = sprite.Texcoord.X; Vertices[VertexCount].texcoord.Y = sprite.Texcoord.Y; + Vertices[VertexCount].color.X = color.R / 255f; + Vertices[VertexCount].color.Y = color.G / 255f; + Vertices[VertexCount].color.Z = color.B / 255f; + Vertices[VertexCount].color.W = color.A / 255f; Vertices[VertexCount + 1].position.X = sprite.Width; Vertices[VertexCount + 1].position.Y = 0; Vertices[VertexCount + 1].texcoord.X = sprite.Texcoord.X + sprite.Texcoord.W; Vertices[VertexCount + 1].texcoord.Y = sprite.Texcoord.Y; + Vertices[VertexCount + 1].color.X = color.R / 255f; + Vertices[VertexCount + 1].color.Y = color.G / 255f; + Vertices[VertexCount + 1].color.Z = color.B / 255f; + Vertices[VertexCount + 1].color.W = color.A / 255f; Vertices[VertexCount + 2].position.X = 0; Vertices[VertexCount + 2].position.Y = sprite.Height; Vertices[VertexCount + 2].texcoord.X = sprite.Texcoord.X; Vertices[VertexCount + 2].texcoord.Y = sprite.Texcoord.Y + sprite.Texcoord.H; + Vertices[VertexCount + 2].color.X = color.R / 255f; + Vertices[VertexCount + 2].color.Y = color.G / 255f; + Vertices[VertexCount + 2].color.Z = color.B / 255f; + Vertices[VertexCount + 2].color.W = color.A / 255f; Vertices[VertexCount + 3].position.X = sprite.Width; Vertices[VertexCount + 3].position.Y = sprite.Height; Vertices[VertexCount + 3].texcoord.X = sprite.Texcoord.X + sprite.Texcoord.W; Vertices[VertexCount + 3].texcoord.Y = sprite.Texcoord.Y + sprite.Texcoord.H; + Vertices[VertexCount + 3].color.X = color.R / 255f; + Vertices[VertexCount + 3].color.Y = color.G / 255f; + Vertices[VertexCount + 3].color.Z = color.B / 255f; + Vertices[VertexCount + 3].color.W = color.A / 255f; VertexCount += 4; diff --git a/src/TestGame.cs b/src/TestGame.cs index f73a041..99469d9 100644 --- a/src/TestGame.cs +++ b/src/TestGame.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using MoonWorks; @@ -124,7 +125,7 @@ namespace MoonWorksComputeSpriteBatch } }; - var vertexAttributes = new VertexAttribute[2] + var vertexAttributes = new VertexAttribute[3] { new VertexAttribute { @@ -139,6 +140,13 @@ namespace MoonWorksComputeSpriteBatch Location = 1, Format = VertexElementFormat.Vector2, Offset = (uint) Marshal.OffsetOf("texcoord") + }, + new VertexAttribute + { + Binding = 0, + Location = 2, + Format = VertexElementFormat.Vector4, + Offset = (uint) Marshal.OffsetOf("color") } }; @@ -225,7 +233,8 @@ namespace MoonWorksComputeSpriteBatch for (var i = 0; i < SPRITECOUNT; i += 1) { var transform = Matrix4x4.CreateTranslation(positions[i]); - spriteBatch.Add(new Sprite(new Rect { X = 0, Y = 0, W = 0, H = 0 }, 128, 128), transform); + var color = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble(), 255f); + spriteBatch.Add(new Sprite(new Rect { X = 0, Y = 0, W = 0, H = 0 }, 128, 128), transform, color); } spriteBatch.Flush(commandBuffer, mainRenderPass, mainFramebuffer, mainRenderArea, spritePipeline, new CameraUniforms { viewProjectionMatrix = viewProjection }); diff --git a/src/VertexPositionTexcoord.cs b/src/VertexPositionTexcoord.cs index 9e95c45..673baac 100644 --- a/src/VertexPositionTexcoord.cs +++ b/src/VertexPositionTexcoord.cs @@ -4,12 +4,14 @@ using System.Runtime.InteropServices; namespace MoonWorksComputeSpriteBatch { // SPIR-V requires vectors to not cross 16-byte boundaries - [StructLayout(LayoutKind.Explicit, Size=32)] + [StructLayout(LayoutKind.Explicit)] public struct VertexPositionTexcoord { [FieldOffset(0)] public Vector3 position; [FieldOffset(16)] public Vector2 texcoord; + [FieldOffset(32)] + public Vector4 color; } }