From 112fafe9f31ab7056aa1bbab437bb6d92bcf011d Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 17 Jan 2022 21:03:02 -0800 Subject: [PATCH] colors --- content/sprite.frag.spv | Bin 568 -> 672 bytes content/sprite.vert.spv | Bin 1300 -> 1448 bytes content/spritebatch.comp.spv | Bin 2132 -> 2176 bytes moonlibs/windows/Refresh.dll | 4 ++-- shaders/sprite.frag | 4 +++- shaders/sprite.vert | 3 +++ shaders/spritebatch.comp | 1 + src/SpriteBatch.cs | 18 +++++++++++++++++- src/TestGame.cs | 13 +++++++++++-- src/VertexPositionTexcoord.cs | 4 +++- 10 files changed, 40 insertions(+), 7 deletions(-) diff --git a/content/sprite.frag.spv b/content/sprite.frag.spv index 89181f8fd37585f52a0066fc0bdd2601840d3169..a4456803ea8c778424b47e65a3cdfb3da1961639 100644 GIT binary patch delta 174 zcmdnNvVfJBnMs+Qfq{{Mi-ChdVj{1pGzS9<0|OZ6CT8XVnGBpjEC|G+6C<;ES%Ffb zKu&UgPJYqEgU-C}PysohAme04#>>KjFVR|e%QQ&X$j-x9+n>*e}PIY GfEWNBq6}^T diff --git a/content/spritebatch.comp.spv b/content/spritebatch.comp.spv index fc2529a4d2e023e8c52163467e0d430a4c365f3a..b4308a4cf64d4f206b46d149f097500010e42e49 100644 GIT binary patch delta 102 zcmca2&>*;B38Nqz11p0u0|NsS5GUv747Ay43ts;Vs{1>1`{C72E+zH g3{qn{`6Hw7<_}EI82KUQ7y>1XfLM9+LzZG@06gsv+yDRo delta 48 zcmZn=ydtn+3FGD+j4F)0?hGspCJYP=Y(T8A`7aYA6E_QkF_2>j#73KWS&Nwg11<=o 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; } }