From 96689a1d1c583f29df0f3f7f45b340c088c33e16 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Sun, 22 Jan 2023 16:21:31 -0500 Subject: [PATCH] Added Texture3D test --- .../Compiled/TexturedQuad3DFrag.refresh | Bin 0 -> 969 bytes .../Shaders/Source/TexturedQuad3DFrag.frag | 17 ++ .../Content/Textures/tex3d_0.png | Bin 0 -> 94 bytes .../Content/Textures/tex3d_1.png | Bin 0 -> 94 bytes .../Content/Textures/tex3d_2.png | Bin 0 -> 94 bytes .../Content/Textures/tex3d_3.png | Bin 0 -> 92 bytes .../Content/Textures/tex3d_4.png | Bin 0 -> 94 bytes .../Content/Textures/tex3d_5.png | Bin 0 -> 94 bytes .../Content/Textures/tex3d_6.png | Bin 0 -> 94 bytes MoonWorksGraphicsTests.sln | 6 + README.md | 4 + Texture3D/Texture3D.csproj | 17 ++ Texture3D/Texture3DGame.cs | 162 ++++++++++++++++++ 13 files changed, 206 insertions(+) create mode 100644 MoonWorks.Test.Common/Content/Shaders/Compiled/TexturedQuad3DFrag.refresh create mode 100644 MoonWorks.Test.Common/Content/Shaders/Source/TexturedQuad3DFrag.frag create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_0.png create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_1.png create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_2.png create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_3.png create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_4.png create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_5.png create mode 100644 MoonWorks.Test.Common/Content/Textures/tex3d_6.png create mode 100644 Texture3D/Texture3D.csproj create mode 100644 Texture3D/Texture3DGame.cs diff --git a/MoonWorks.Test.Common/Content/Shaders/Compiled/TexturedQuad3DFrag.refresh b/MoonWorks.Test.Common/Content/Shaders/Compiled/TexturedQuad3DFrag.refresh new file mode 100644 index 0000000000000000000000000000000000000000..a94da746b3527463cb657efade61409a75a4eee9 GIT binary patch literal 969 zcmYk4T~FF@6vhui!IvpAhK{)@%B2a}#WYLImZ+KKf`n~s*=0j$)y6_eWy}0_W}j*| zCO*H?COygFob#OLf8NUXt5Lu7ST-xKbZcfM+ptaR*iss{BE*DMQ|k^!gWmnZ?|nFV z4`a*pPy(lJHLJ*?({VVHu{CK!x-A~K8ptcFs@Xq1s@S@@1J4}}-N6@cGM{_*i-#!i zqHq>?ewYN4MffLxw_4zZvq|*ZFA^LvihUh((%x6e_~tT>;za%6Y|4(t(|Hsmik)<} zAHm8{>$dt&a?(eJ7( zT=26jW(Tmgwpo{jPpzVqvS3fl4f*h>_aei=s7K7s2>t7_c|G{sim3$-pO~9~?W;ET zUd~^!SMr$y3@$PE0Bb4Uk?lx%dz{6)@)3HymW4|_X4%Vpc0JYY$#EA1?7t94_LETd z*^;sgG4sBZGV8fm)NV_ehn{fg^F|8(O&sF4Qr;ms_D((+bAJ_!x%f}OB}a{4de1jH SkTM7P&zh2N1@@%ZAEnRWtV0+8 literal 0 HcmV?d00001 diff --git a/MoonWorks.Test.Common/Content/Shaders/Source/TexturedQuad3DFrag.frag b/MoonWorks.Test.Common/Content/Shaders/Source/TexturedQuad3DFrag.frag new file mode 100644 index 0000000..8a9119e --- /dev/null +++ b/MoonWorks.Test.Common/Content/Shaders/Source/TexturedQuad3DFrag.frag @@ -0,0 +1,17 @@ +#version 450 + +layout (location = 0) in vec2 TexCoord; + +layout (location = 0) out vec4 FragColor; + +layout(binding = 0, set = 1) uniform sampler3D Sampler; + +layout (binding = 0, set = 3) uniform UniformBlock +{ + float depth; +}; + +void main() +{ + FragColor = texture(Sampler, vec3(TexCoord, depth)); +} diff --git a/MoonWorks.Test.Common/Content/Textures/tex3d_0.png b/MoonWorks.Test.Common/Content/Textures/tex3d_0.png new file mode 100644 index 0000000000000000000000000000000000000000..6ab7f804eb172a698e7fc99c678efb8d7e2b2661 GIT binary patch literal 94 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`s-7;6Ar*6yE7cc&n%~HDz^U_D rAhX+H2DXRbP0l+XkK0<9QP literal 0 HcmV?d00001 diff --git a/MoonWorks.Test.Common/Content/Textures/tex3d_1.png b/MoonWorks.Test.Common/Content/Textures/tex3d_1.png new file mode 100644 index 0000000000000000000000000000000000000000..ce7f7771dcd712afe144a7feada847b27f61a845 GIT binary patch literal 94 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`s-7;6Ar*6yf8G=SHouYSfK%tQ rKxVhY3~UhzJVlJBS1|I3v@gTe~DWM4fBOnJ?>1u{!$@G&!#voae>2Yp)z)WG2B>gTe~DWM4f8gUrl literal 0 HcmV?d00001 diff --git a/MoonWorks.Test.Common/Content/Textures/tex3d_3.png b/MoonWorks.Test.Common/Content/Textures/tex3d_3.png new file mode 100644 index 0000000000000000000000000000000000000000..923e8e87a2ec7cd4fac9436e8d881bf27d0c8c97 GIT binary patch literal 92 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`%APKcAr*6y6Myixh%ktGne00z3q-y&w+v literal 0 HcmV?d00001 diff --git a/MoonWorks.Test.Common/Content/Textures/tex3d_4.png b/MoonWorks.Test.Common/Content/Textures/tex3d_4.png new file mode 100644 index 0000000000000000000000000000000000000000..3278d86deac3b57b701e52e9a16bf4e977d8a1ae GIT binary patch literal 94 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`s-7;6Ar*6y6My{wZ_muy&?J?>1u{!$@G+{gTe~DWM4fBX}7} literal 0 HcmV?d00001 diff --git a/MoonWorks.Test.Common/Content/Textures/tex3d_5.png b/MoonWorks.Test.Common/Content/Textures/tex3d_5.png new file mode 100644 index 0000000000000000000000000000000000000000..f0d75e4b350035c190a446bea4e6108b0d619e34 GIT binary patch literal 94 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`s-7;6Ar*6yFB;GJGQW}OfK%tQ rKxVhY3~UhzJVlJBS1|I3v@bP0l+XkK5s(<1 literal 0 HcmV?d00001 diff --git a/MoonWorks.Test.Common/Content/Textures/tex3d_6.png b/MoonWorks.Test.Common/Content/Textures/tex3d_6.png new file mode 100644 index 0000000000000000000000000000000000000000..bf980b2373a4ce0815f7eda98a991d30b04151fc GIT binary patch literal 94 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`s-7;6Ar*6yXD+$-e|{s=0jJJq rfy{1)8Q3Bcc#0TLuVCa6X=h|OF^{PuBJ0;bpaup{S3j3^P6 + + + + + + + + Exe + net7.0 + enable + x64 + + + + + diff --git a/Texture3D/Texture3DGame.cs b/Texture3D/Texture3DGame.cs new file mode 100644 index 0000000..db7a9d4 --- /dev/null +++ b/Texture3D/Texture3DGame.cs @@ -0,0 +1,162 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Math.Float; +using RefreshCS; + +namespace MoonWorks.Test +{ + class Texture3DGame : Game + { + private GraphicsPipeline pipeline; + private Buffer vertexBuffer; + private Buffer indexBuffer; + private Texture texture; + private Sampler sampler; + + private int currentDepth = 0; + + struct FragUniform + { + public float Depth; + + public FragUniform(float depth) + { + Depth = depth; + } + } + + public Texture3DGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) + { + Logger.LogInfo("Press Left and Right to cycle between depth slices"); + + // Load the shaders + ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadVert")); + ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad3DFrag")); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + MainWindow.SwapchainFormat, + vertShaderModule, + fragShaderModule + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + pipelineCreateInfo.FragmentShaderInfo = GraphicsShaderInfo.Create(fragShaderModule, "main", 1); + pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create samplers + sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); + + // Create and populate the GPU resources + vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 4); + indexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Index, 6); + texture = new Texture(GraphicsDevice, new TextureCreateInfo + { + Width = 16, + Height = 16, + Depth = 7, + Format = TextureFormat.R8G8B8A8, + IsCube = false, + LevelCount = 1, + UsageFlags = TextureUsageFlags.Sampler + }); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + cmdbuf.SetBufferData( + vertexBuffer, + new PositionTextureVertex[] + { + new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)), + new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(4, 0)), + new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(4, 4)), + new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 4)), + } + ); + cmdbuf.SetBufferData( + indexBuffer, + new ushort[] + { + 0, 1, 2, + 0, 2, 3, + } + ); + + // Load each depth subimage of the 3D texture + for (uint i = 0; i < texture.Depth; i += 1) + { + TextureSlice slice = new TextureSlice( + texture, + new Rect(0, 0, (int) texture.Width, (int) texture.Height), + i + ); + + var pixels = Refresh.Refresh_Image_Load( + TestUtils.GetTexturePath($"tex3d_{i}.png"), + out var width, + out var height, + out var channels + ); + + var byteCount = (uint) (width * height * channels); + cmdbuf.SetTextureData(slice, pixels, byteCount); + + Refresh.Refresh_Image_Free(pixels); + } + + GraphicsDevice.Submit(cmdbuf); + } + + protected override void Update(System.TimeSpan delta) + { + int prevDepth = currentDepth; + + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) + { + currentDepth -= 1; + if (currentDepth < 0) + { + currentDepth = (int) texture.Depth - 1; + } + } + + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) + { + currentDepth += 1; + if (currentDepth >= texture.Depth) + { + currentDepth = 0; + } + } + + if (prevDepth != currentDepth) + { + Logger.LogInfo("Setting depth to: " + currentDepth); + } + } + + protected override void Draw(double alpha) + { + FragUniform fragUniform = new FragUniform((float) currentDepth / texture.Depth); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow); + if (backbuffer != null) + { + cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, Color.Black)); + cmdbuf.BindGraphicsPipeline(pipeline); + cmdbuf.BindVertexBuffers(vertexBuffer); + cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen); + cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler)); + uint fragParamOffset = cmdbuf.PushFragmentShaderUniforms(fragUniform); + cmdbuf.DrawIndexedPrimitives(0, 0, 2, 0, fragParamOffset); + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } + + public static void Main(string[] args) + { + Texture3DGame game = new Texture3DGame(); + game.Run(); + } + } +}