update Texture3DGame

refresh2
cosmonaut 2024-02-23 14:05:05 -08:00
parent 6b0f80119a
commit 1876258114
1 changed files with 34 additions and 30 deletions

View File

@ -1,15 +1,13 @@
using MoonWorks; using MoonWorks.Graphics;
using MoonWorks.Graphics;
using MoonWorks.Math.Float; using MoonWorks.Math.Float;
using RefreshCS;
namespace MoonWorks.Test namespace MoonWorks.Test
{ {
class Texture3DGame : Game class Texture3DGame : Game
{ {
private GraphicsPipeline pipeline; private GraphicsPipeline pipeline;
private Buffer vertexBuffer; private GpuBuffer vertexBuffer;
private Buffer indexBuffer; private GpuBuffer indexBuffer;
private Texture texture; private Texture texture;
private Sampler sampler; private Sampler sampler;
@ -47,47 +45,53 @@ namespace MoonWorks.Test
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
// Create and populate the GPU resources // Create and populate the GPU resources
vertexBuffer = Buffer.Create<PositionTextureVertex>(GraphicsDevice, BufferUsageFlags.Vertex, 4); var resourceInitializer = new ResourceInitializer(GraphicsDevice);
indexBuffer = Buffer.Create<ushort>(GraphicsDevice, BufferUsageFlags.Index, 6);
texture = Texture.CreateTexture3D(GraphicsDevice, 16, 16, 7, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler);
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); vertexBuffer = resourceInitializer.CreateBuffer(
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(0, 0)),
new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 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(1, 1)),
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)), new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1))
} ],
BufferUsageFlags.Vertex
); );
cmdbuf.SetBufferData(
indexBuffer, indexBuffer = resourceInitializer.CreateBuffer<ushort>(
new ushort[] [
{
0, 1, 2, 0, 1, 2,
0, 2, 3, 0, 2, 3,
} ],
BufferUsageFlags.Index
); );
texture = Texture.CreateTexture3D(GraphicsDevice, 16, 16, 7, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler);
// Load each depth subimage of the 3D texture // Load each depth subimage of the 3D texture
for (uint i = 0; i < texture.Depth; i += 1) for (uint i = 0; i < texture.Depth; i += 1)
{ {
TextureSlice slice = new TextureSlice( TextureSlice slice = new TextureSlice
texture, {
new Rect(0, 0, (int) texture.Width, (int) texture.Height), Texture = texture,
i MipLevel = 0,
); BaseLayer = 0,
LayerCount = 1,
X = 0,
Y = 0,
Z = i,
Width = texture.Width,
Height = texture.Height,
Depth = 1
};
Texture.SetDataFromImageFile( resourceInitializer.SetTextureDataFromCompressed(
cmdbuf,
slice, slice,
TestUtils.GetTexturePath($"tex3d_{i}.png") TestUtils.GetTexturePath($"tex3d_{i}.png")
); );
} }
GraphicsDevice.Submit(cmdbuf); resourceInitializer.Upload();
resourceInitializer.Dispose();
} }
protected override void Update(System.TimeSpan delta) protected override void Update(System.TimeSpan delta)
@ -131,8 +135,8 @@ namespace MoonWorks.Test
cmdbuf.BindVertexBuffers(vertexBuffer); cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen); cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler)); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler));
uint fragParamOffset = cmdbuf.PushFragmentShaderUniforms(fragUniform); cmdbuf.PushFragmentShaderUniforms(fragUniform);
cmdbuf.DrawIndexedPrimitives(0, 0, 2, 0, fragParamOffset); cmdbuf.DrawIndexedPrimitives(0, 0, 2);
cmdbuf.EndRenderPass(); cmdbuf.EndRenderPass();
} }
GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Submit(cmdbuf);