update Cube for new API

refresh2
cosmonaut 2024-02-21 23:30:15 -08:00
parent 691ba400a0
commit 110151ad56
2 changed files with 219 additions and 153 deletions

View File

@ -9,6 +9,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<Import Project="$(SolutionDir)NativeAOT_Console.targets" Condition="Exists('$(SolutionDir)NativeAOT_Console.targets')" /> <Import Project="$(SolutionDir)NativeAOT_Console.targets" Condition="Exists('$(SolutionDir)NativeAOT_Console.targets')" />

View File

@ -1,7 +1,7 @@
using MoonWorks; using MoonWorks.Graphics;
using MoonWorks.Graphics;
using MoonWorks.Math; using MoonWorks.Math;
using MoonWorks.Math.Float; using MoonWorks.Math.Float;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MoonWorks.Test namespace MoonWorks.Test
@ -18,10 +18,13 @@ namespace MoonWorks.Test
private Sampler depthSampler; private Sampler depthSampler;
private DepthUniforms depthUniforms; private DepthUniforms depthUniforms;
private Buffer cubeVertexBuffer; private GpuBuffer cubeVertexBuffer;
private Buffer skyboxVertexBuffer; private GpuBuffer skyboxVertexBuffer;
private Buffer blitVertexBuffer; private GpuBuffer blitVertexBuffer;
private Buffer indexBuffer; private GpuBuffer indexBuffer;
private CpuBuffer transferBuffer;
private CpuBuffer cubemapTransferBuffer;
private Texture skyboxTexture; private Texture skyboxTexture;
private Sampler skyboxSampler; private Sampler skyboxSampler;
@ -48,19 +51,38 @@ namespace MoonWorks.Test
} }
} }
void LoadCubemap(CommandBuffer cmdbuf, string[] imagePaths) unsafe void LoadCubemap(string[] imagePaths)
{ {
/* Upload cubemap layers one at a time to minimize transfer size */
for (uint i = 0; i < imagePaths.Length; i++) for (uint i = 0; i < imagePaths.Length; i++)
{ {
var textureSlice = new TextureSlice( var commandBuffer = GraphicsDevice.AcquireCommandBuffer();
skyboxTexture,
new Rect(0, 0, (int) skyboxTexture.Width, (int) skyboxTexture.Height),
0,
i,
0
);
Texture.SetDataFromImageFile(cmdbuf, textureSlice, imagePaths[i]); var textureSlice = new TextureSlice
{
Texture = skyboxTexture,
MipLevel = 0,
BaseLayer = i,
LayerCount = 1,
X = 0,
Y = 0,
Z = 0,
Width = skyboxTexture.Width,
Height = skyboxTexture.Height,
Depth = 1
};
var pixelData = ImageUtils.GetPixelDataFromFile(imagePaths[i], out var _, out var _, out var sizeInBytes);
cubemapTransferBuffer.SetData(new Span<byte>((void*) pixelData, (int) sizeInBytes), SetDataOptions.Overwrite);
ImageUtils.FreePixelData(pixelData);
commandBuffer.BeginCopyPass();
commandBuffer.UploadToTexture(cubemapTransferBuffer, textureSlice, new BufferImageCopy(0, 0, 0));
commandBuffer.EndCopyPass();
var fence = GraphicsDevice.SubmitAndAcquireFence(commandBuffer);
GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence);
} }
} }
@ -111,28 +133,31 @@ namespace MoonWorks.Test
); );
skyboxSampler = new Sampler(GraphicsDevice, new SamplerCreateInfo()); skyboxSampler = new Sampler(GraphicsDevice, new SamplerCreateInfo());
cubeVertexBuffer = Buffer.Create<PositionColorVertex>( cubeVertexBuffer = GpuBuffer.Create<PositionColorVertex>(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Vertex, BufferUsageFlags.Vertex,
24 24
); );
skyboxVertexBuffer = Buffer.Create<PositionVertex>( skyboxVertexBuffer = GpuBuffer.Create<PositionVertex>(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Vertex, BufferUsageFlags.Vertex,
24 24
); );
indexBuffer = Buffer.Create<uint>( indexBuffer = GpuBuffer.Create<uint>(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Index, BufferUsageFlags.Index,
36 36
); // Using uint here just to test IndexElementSize=32 ); // Using uint here just to test IndexElementSize=32
blitVertexBuffer = Buffer.Create<PositionTextureVertex>( blitVertexBuffer = GpuBuffer.Create<PositionTextureVertex>(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Vertex, BufferUsageFlags.Vertex,
6 6
); );
transferBuffer = new CpuBuffer(GraphicsDevice, 32768);
cubemapTransferBuffer = new CpuBuffer(GraphicsDevice, 2048 * 2048 * 4);
Task loadingTask = Task.Run(() => UploadGPUAssets()); Task loadingTask = Task.Run(() => UploadGPUAssets());
// Create the cube pipelines // Create the cube pipelines
@ -199,12 +224,7 @@ namespace MoonWorks.Test
{ {
Logger.LogInfo("Loading..."); Logger.LogInfo("Loading...");
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); var cubeVertexData = new Span<PositionColorVertex>([
cmdbuf.SetBufferData(
cubeVertexBuffer,
new PositionColorVertex[]
{
new PositionColorVertex(new Vector3(-1, -1, -1), new Color(1f, 0f, 0f)), new PositionColorVertex(new Vector3(-1, -1, -1), new Color(1f, 0f, 0f)),
new PositionColorVertex(new Vector3(1, -1, -1), new Color(1f, 0f, 0f)), new PositionColorVertex(new Vector3(1, -1, -1), new Color(1f, 0f, 0f)),
new PositionColorVertex(new Vector3(1, 1, -1), new Color(1f, 0f, 0f)), new PositionColorVertex(new Vector3(1, 1, -1), new Color(1f, 0f, 0f)),
@ -234,13 +254,9 @@ namespace MoonWorks.Test
new PositionColorVertex(new Vector3(-1, 1, 1), new Color(0f, 0.5f, 0f)), new PositionColorVertex(new Vector3(-1, 1, 1), new Color(0f, 0.5f, 0f)),
new PositionColorVertex(new Vector3(1, 1, 1), new Color(0f, 0.5f, 0f)), new PositionColorVertex(new Vector3(1, 1, 1), new Color(0f, 0.5f, 0f)),
new PositionColorVertex(new Vector3(1, 1, -1), new Color(0f, 0.5f, 0f)) new PositionColorVertex(new Vector3(1, 1, -1), new Color(0f, 0.5f, 0f))
} ]);
);
cmdbuf.SetBufferData( var skyboxVertexData = new Span<PositionVertex>([
skyboxVertexBuffer,
new PositionVertex[]
{
new PositionVertex(new Vector3(-10, -10, -10)), new PositionVertex(new Vector3(-10, -10, -10)),
new PositionVertex(new Vector3(10, -10, -10)), new PositionVertex(new Vector3(10, -10, -10)),
new PositionVertex(new Vector3(10, 10, -10)), new PositionVertex(new Vector3(10, 10, -10)),
@ -270,36 +286,82 @@ namespace MoonWorks.Test
new PositionVertex(new Vector3(-10, 10, 10)), new PositionVertex(new Vector3(-10, 10, 10)),
new PositionVertex(new Vector3(10, 10, 10)), new PositionVertex(new Vector3(10, 10, 10)),
new PositionVertex(new Vector3(10, 10, -10)) new PositionVertex(new Vector3(10, 10, -10))
} ]);
);
cmdbuf.SetBufferData( var indexData = new Span<uint>([
indexBuffer,
new uint[]
{
0, 1, 2, 0, 2, 3, 0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4, 6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11, 8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12, 14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19, 16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20 22, 21, 20, 23, 22, 20
} ]);
);
cmdbuf.SetBufferData( var blitVertexData = new Span<PositionTextureVertex>([
blitVertexBuffer,
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, 0)), new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 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)),
} ]);
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.BeginCopyPass();
uint offset = 0;
uint length = transferBuffer.SetData(cubeVertexData, SetDataOptions.Overwrite);
cmdbuf.UploadToBuffer(
transferBuffer,
cubeVertexBuffer,
new BufferCopy(
offset,
0,
length
)
); );
LoadCubemap(cmdbuf, new string[] offset += length;
length = transferBuffer.SetData(skyboxVertexData, offset, SetDataOptions.Overwrite);
cmdbuf.UploadToBuffer(
transferBuffer,
skyboxVertexBuffer,
new BufferCopy(
offset,
0,
length
)
);
offset += length;
length = transferBuffer.SetData(indexData, offset, SetDataOptions.Overwrite);
cmdbuf.UploadToBuffer(
transferBuffer,
indexBuffer,
new BufferCopy(
offset,
0,
length
)
);
offset += length;
length = transferBuffer.SetData(blitVertexData, offset, SetDataOptions.Overwrite);
cmdbuf.UploadToBuffer(
transferBuffer,
blitVertexBuffer,
new BufferCopy(
offset,
0,
length
)
);
cmdbuf.EndCopyPass();
GraphicsDevice.Submit(cmdbuf);
LoadCubemap(new string[]
{ {
TestUtils.GetTexturePath("right.png"), TestUtils.GetTexturePath("right.png"),
TestUtils.GetTexturePath("left.png"), TestUtils.GetTexturePath("left.png"),
@ -309,7 +371,8 @@ namespace MoonWorks.Test
TestUtils.GetTexturePath("back.png") TestUtils.GetTexturePath("back.png")
}); });
GraphicsDevice.Submit(cmdbuf); cubemapTransferBuffer.Dispose();
transferBuffer.Dispose();
finishedLoading = true; finishedLoading = true;
Logger.LogInfo("Finished loading!"); Logger.LogInfo("Finished loading!");
@ -408,16 +471,16 @@ namespace MoonWorks.Test
cmdbuf.BindGraphicsPipeline(depthOnlyEnabled ? cubePipelineDepthOnly : cubePipeline); cmdbuf.BindGraphicsPipeline(depthOnlyEnabled ? cubePipelineDepthOnly : cubePipeline);
cmdbuf.BindVertexBuffers(cubeVertexBuffer); cmdbuf.BindVertexBuffers(cubeVertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.ThirtyTwo); cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.ThirtyTwo);
uint vertexParamOffset = cmdbuf.PushVertexShaderUniforms(cubeUniforms); cmdbuf.PushVertexShaderUniforms(cubeUniforms);
cmdbuf.DrawIndexedPrimitives(0, 0, 12, vertexParamOffset, 0); cmdbuf.DrawIndexedPrimitives(0, 0, 12);
// Draw skybox // Draw skybox
cmdbuf.BindGraphicsPipeline(depthOnlyEnabled ? skyboxPipelineDepthOnly : skyboxPipeline); cmdbuf.BindGraphicsPipeline(depthOnlyEnabled ? skyboxPipelineDepthOnly : skyboxPipeline);
cmdbuf.BindVertexBuffers(skyboxVertexBuffer); cmdbuf.BindVertexBuffers(skyboxVertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.ThirtyTwo); cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.ThirtyTwo);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(skyboxTexture, skyboxSampler)); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(skyboxTexture, skyboxSampler));
vertexParamOffset = cmdbuf.PushVertexShaderUniforms(skyboxUniforms); cmdbuf.PushVertexShaderUniforms(skyboxUniforms);
cmdbuf.DrawIndexedPrimitives(0, 0, 12, vertexParamOffset, 0); cmdbuf.DrawIndexedPrimitives(0, 0, 12);
cmdbuf.EndRenderPass(); cmdbuf.EndRenderPass();
@ -429,8 +492,8 @@ namespace MoonWorks.Test
cmdbuf.BindGraphicsPipeline(blitPipeline); cmdbuf.BindGraphicsPipeline(blitPipeline);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(depthTexture, depthSampler)); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(depthTexture, depthSampler));
cmdbuf.BindVertexBuffers(blitVertexBuffer); cmdbuf.BindVertexBuffers(blitVertexBuffer);
uint fragParamOffset = cmdbuf.PushFragmentShaderUniforms(depthUniforms); cmdbuf.PushFragmentShaderUniforms(depthUniforms);
cmdbuf.DrawPrimitives(0, 2, vertexParamOffset, fragParamOffset); cmdbuf.DrawPrimitives(0, 2);
cmdbuf.EndRenderPass(); cmdbuf.EndRenderPass();
} }
@ -452,10 +515,12 @@ namespace MoonWorks.Test
private System.Action<object?> TakeScreenshot = texture => private System.Action<object?> TakeScreenshot = texture =>
{ {
/*
if (texture != null) if (texture != null)
{ {
((Texture) texture).SavePNG(System.IO.Path.Combine(System.AppContext.BaseDirectory, "screenshot.png")); ((Texture) texture).SavePNG(System.IO.Path.Combine(System.AppContext.BaseDirectory, "screenshot.png"));
} }
*/
}; };
public static void Main(string[] args) public static void Main(string[] args)