Added Texture3D test

pull/2/head
Caleb Cornett 2023-01-22 16:21:31 -05:00
parent 6debea99c8
commit 96689a1d1c
13 changed files with 206 additions and 0 deletions

View File

@ -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));
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 B

View File

@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VideoPlayer", "VideoPlayer\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InstancingAndOffsets", "Instancing\InstancingAndOffsets.csproj", "{01427BDA-45E2-46C0-B116-024EBE541D45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Texture3D", "Texture3D\Texture3D.csproj", "{9D0F0573-7FD4-4480-8F9B-CDD52120A170}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -123,6 +125,10 @@ Global
{01427BDA-45E2-46C0-B116-024EBE541D45}.Debug|x64.Build.0 = Debug|x64
{01427BDA-45E2-46C0-B116-024EBE541D45}.Release|x64.ActiveCfg = Release|x64
{01427BDA-45E2-46C0-B116-024EBE541D45}.Release|x64.Build.0 = Release|x64
{9D0F0573-7FD4-4480-8F9B-CDD52120A170}.Debug|x64.ActiveCfg = Debug|x64
{9D0F0573-7FD4-4480-8F9B-CDD52120A170}.Debug|x64.Build.0 = Debug|x64
{9D0F0573-7FD4-4480-8F9B-CDD52120A170}.Release|x64.ActiveCfg = Release|Any CPU
{9D0F0573-7FD4-4480-8F9B-CDD52120A170}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -67,3 +67,7 @@ Loads an image, then makes three copies of the image. One is a 1:1 scale image,
**VideoPlayer**
Plays a sample Ogg Theora video file. Tests YUV textures and video rendering.
**Texture3D**
Displays 2D slices of a 3D texture. Tests creating and drawing 3D textures.

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\MoonWorks\MoonWorks.csproj" />
<ProjectReference Include="..\MoonWorks.Test.Common\MoonWorks.Test.Common.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
</PropertyGroup>
<Import Project="$(SolutionDir)NativeAOT_Console.targets" Condition="Exists('$(SolutionDir)NativeAOT_Console.targets')" />
</Project>

162
Texture3D/Texture3DGame.cs Normal file
View File

@ -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<PositionTextureVertex>();
pipelineCreateInfo.FragmentShaderInfo = GraphicsShaderInfo.Create<FragUniform>(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<PositionTextureVertex>(GraphicsDevice, BufferUsageFlags.Vertex, 4);
indexBuffer = Buffer.Create<ushort>(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();
}
}
}