From 347290bae2118a09cb233cb527f86de44d82b230 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 23 Feb 2024 14:42:47 -0800 Subject: [PATCH] update TextureMipmaps --- TextureMipmaps/TextureMipmapsGame.cs | 75 +++++++++++++++------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/TextureMipmaps/TextureMipmapsGame.cs b/TextureMipmaps/TextureMipmapsGame.cs index 609169f..1d91123 100644 --- a/TextureMipmaps/TextureMipmapsGame.cs +++ b/TextureMipmaps/TextureMipmapsGame.cs @@ -1,15 +1,13 @@ -using MoonWorks; -using MoonWorks.Graphics; +using MoonWorks.Graphics; using MoonWorks.Math.Float; -using RefreshCS; namespace MoonWorks.Test { class TextureMipmapsGame : Game { private GraphicsPipeline pipeline; - private Buffer vertexBuffer; - private Buffer indexBuffer; + private GpuBuffer vertexBuffer; + private GpuBuffer indexBuffer; private Texture texture; private Sampler sampler; @@ -34,10 +32,9 @@ namespace MoonWorks.Test pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1; pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - // Create and populate the GPU resources sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); - vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 4); - indexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Index, 6); + + // Create and populate the GPU resources texture = Texture.CreateTexture2D( GraphicsDevice, 256, @@ -47,43 +44,53 @@ namespace MoonWorks.Test 4 ); - CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - cmdbuf.SetBufferData( - vertexBuffer, - new PositionTextureVertex[] - { + var resourceUploader = new ResourceUploader(GraphicsDevice); + + vertexBuffer = resourceUploader.CreateBuffer( + [ new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)), new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)), new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)), new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)), - } + ], + BufferUsageFlags.Vertex ); - cmdbuf.SetBufferData( - indexBuffer, - new ushort[] - { + + indexBuffer = resourceUploader.CreateBuffer( + [ 0, 1, 2, - 0, 2, 3, - } + 0, 2, 3 + ], + BufferUsageFlags.Index ); // Set the various mip levels - for (int i = 0; i < texture.LevelCount; i += 1) + for (uint i = 0; i < texture.LevelCount; i += 1) { - int w = (int) texture.Width >> i; - int h = (int) texture.Height >> i; - TextureSlice slice = new TextureSlice( - texture, - new Rect(0, 0, w, h), - 0, - 0, - (uint) i - ); + var w = texture.Width >> (int) i; + var h = texture.Height >> (int) i; + var slice = new TextureSlice + { + Texture = texture, + MipLevel = i, + BaseLayer = 0, + LayerCount = 1, + X = 0, + Y = 0, + Z = 0, + Width = w, + Height = h, + Depth = 1 + }; - Texture.SetDataFromImageFile(cmdbuf, slice, TestUtils.GetTexturePath($"mip{i}.png")); + resourceUploader.SetTextureDataFromCompressed( + slice, + TestUtils.GetTexturePath($"mip{i}.png") + ); } - GraphicsDevice.Submit(cmdbuf); + resourceUploader.Upload(); + resourceUploader.Dispose(); } protected override void Update(System.TimeSpan delta) @@ -112,8 +119,8 @@ namespace MoonWorks.Test cmdbuf.BindVertexBuffers(vertexBuffer); cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler)); - uint vertParamOffset = cmdbuf.PushVertexShaderUniforms(vertUniforms); - cmdbuf.DrawIndexedPrimitives(0, 0, 2, vertParamOffset, 0); + cmdbuf.PushVertexShaderUniforms(vertUniforms); + cmdbuf.DrawIndexedPrimitives(0, 0, 2); cmdbuf.EndRenderPass(); } GraphicsDevice.Submit(cmdbuf);