MoonWorksGraphicsTests/Examples/CubeExample.cs

598 lines
18 KiB
C#
Raw Normal View History

2024-06-06 19:06:37 +00:00
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Input;
using MoonWorks.Math;
using MoonWorks.Math.Float;
2024-02-22 07:30:15 +00:00
using System;
2024-02-23 06:51:01 +00:00
using System.IO;
using System.Threading.Tasks;
2024-06-11 17:08:37 +00:00
using Buffer = MoonWorks.Graphics.Buffer;
2024-06-06 19:06:37 +00:00
namespace MoonWorksGraphicsTests
{
2024-06-06 19:06:37 +00:00
struct DepthUniforms
2024-02-07 15:27:55 +00:00
{
2024-06-06 19:06:37 +00:00
public float ZNear;
public float ZFar;
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
public DepthUniforms(float zNear, float zFar)
{
ZNear = zNear;
ZFar = zFar;
}
}
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
class CubeExample : Example
{
private GraphicsPipeline CubePipeline;
private GraphicsPipeline CubePipelineDepthOnly;
private GraphicsPipeline SkyboxPipeline;
private GraphicsPipeline SkyboxPipelineDepthOnly;
private GraphicsPipeline BlitPipeline;
2024-02-22 07:30:15 +00:00
2024-06-06 19:06:37 +00:00
private Texture DepthTexture;
private Sampler DepthSampler;
private DepthUniforms DepthUniforms;
2024-02-07 15:27:55 +00:00
2024-06-11 17:08:37 +00:00
private Buffer CubeVertexBuffer;
private Buffer skyboxVertexBuffer;
private Buffer BlitVertexBuffer;
private Buffer IndexBuffer;
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
private TransferBuffer ScreenshotTransferBuffer;
private Texture ScreenshotTexture;
private Fence ScreenshotFence;
private Texture SkyboxTexture;
private Sampler SkyboxSampler;
2023-04-19 07:42:10 +00:00
private bool takeScreenshot;
private bool screenshotInProgress;
2024-06-06 19:06:37 +00:00
private bool swapchainDownloaded; // don't want to take screenshot if the swapchain was invalid
2023-04-19 07:42:10 +00:00
2024-06-06 19:06:37 +00:00
private bool finishedLoading;
private float cubeTimer;
private Quaternion cubeRotation;
private Quaternion previousCubeRotation;
private bool depthOnlyEnabled;
private Vector3 camPos;
2024-02-07 15:27:55 +00:00
2024-03-01 23:03:29 +00:00
// Upload cubemap layers one at a time to minimize transfer size
unsafe void LoadCubemap(string[] imagePaths)
{
var cubemapUploader = new ResourceUploader(GraphicsDevice);
2024-03-01 23:03:29 +00:00
for (uint i = 0; i < imagePaths.Length; i++)
{
var textureRegion = new TextureRegion
{
TextureSlice = new TextureSlice
{
2024-06-06 19:06:37 +00:00
Texture = SkyboxTexture,
2024-03-01 23:03:29 +00:00
MipLevel = 0,
Layer = i,
},
X = 0,
Y = 0,
Z = 0,
2024-06-06 19:06:37 +00:00
Width = SkyboxTexture.Width,
Height = SkyboxTexture.Height,
2024-03-01 23:03:29 +00:00
Depth = 1
};
2024-02-22 07:30:15 +00:00
2024-02-23 22:30:53 +00:00
cubemapUploader.SetTextureDataFromCompressed(
2024-03-01 23:03:29 +00:00
textureRegion,
2024-02-23 22:30:53 +00:00
imagePaths[i]
);
2024-02-22 07:30:15 +00:00
2024-02-23 22:30:53 +00:00
cubemapUploader.UploadAndWait();
2024-03-01 23:03:29 +00:00
}
2024-02-23 22:30:53 +00:00
cubemapUploader.Dispose();
2024-03-01 23:03:29 +00:00
}
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
{
Window = window;
GraphicsDevice = graphicsDevice;
Inputs = inputs;
Window.SetTitle("Cube");
finishedLoading = false;
cubeTimer = 0;
cubeRotation = Quaternion.Identity;
previousCubeRotation = Quaternion.Identity;
depthOnlyEnabled = false;
camPos = new Vector3(0, 1.5f, 4);
Shader cubeVertShader = new Shader(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
TestUtils.GetShaderPath("PositionColorWithMatrix.vert"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Vertex,
ShaderFormat = ShaderFormat.SPIRV,
UniformBufferCount = 1
}
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
Shader cubeFragShader = new Shader(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
TestUtils.GetShaderPath("SolidColor.frag"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Fragment,
ShaderFormat = ShaderFormat.SPIRV
}
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
Shader skyboxVertShader = new Shader(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
TestUtils.GetShaderPath("Skybox.vert"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Vertex,
ShaderFormat = ShaderFormat.SPIRV,
UniformBufferCount = 1
}
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
Shader skyboxFragShader = new Shader(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
TestUtils.GetShaderPath("Skybox.frag"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Fragment,
ShaderFormat = ShaderFormat.SPIRV,
SamplerCount = 1
}
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
Shader blitVertShader = new Shader(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
TestUtils.GetShaderPath("TexturedQuad.vert"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Vertex,
ShaderFormat = ShaderFormat.SPIRV
}
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
Shader blitFragShader = new Shader(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
TestUtils.GetShaderPath("TexturedDepthQuad.frag"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Fragment,
ShaderFormat = ShaderFormat.SPIRV,
SamplerCount = 1,
UniformBufferCount = 1
}
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
DepthTexture = Texture.CreateTexture2D(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-06-06 19:06:37 +00:00
Window.Width,
Window.Height,
TextureFormat.D16_UNORM,
TextureUsageFlags.DepthStencil | TextureUsageFlags.Sampler
2024-02-07 15:27:55 +00:00
);
2024-06-06 19:06:37 +00:00
DepthTexture.Name = "Depth Texture";
2024-03-11 23:11:27 +00:00
2024-06-06 19:06:37 +00:00
DepthSampler = new Sampler(GraphicsDevice, new SamplerCreateInfo());
DepthUniforms = new DepthUniforms(0.01f, 100f);
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
SkyboxTexture = Texture.CreateTextureCube(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2048,
TextureFormat.R8G8B8A8,
TextureUsageFlags.Sampler
);
2024-06-06 19:06:37 +00:00
SkyboxTexture.Name = "Skybox";
2024-03-01 23:03:29 +00:00
2024-06-06 19:06:37 +00:00
SkyboxSampler = new Sampler(GraphicsDevice, new SamplerCreateInfo());
2024-02-22 07:30:15 +00:00
2024-06-06 19:06:37 +00:00
ScreenshotTransferBuffer = new TransferBuffer(
GraphicsDevice,
TransferUsage.Texture,
TransferBufferMapFlags.Read,
Window.Width * Window.Height * 4
);
ScreenshotTexture = Texture.CreateTexture2D(
GraphicsDevice,
Window.Width,
Window.Height,
Window.SwapchainFormat,
TextureUsageFlags.Sampler
);
ScreenshotTexture.Name = "Screenshot";
2024-02-07 15:27:55 +00:00
Task loadingTask = Task.Run(() => UploadGPUAssets());
// Create the cube pipelines
GraphicsPipelineCreateInfo cubePipelineCreateInfo = new GraphicsPipelineCreateInfo
{
AttachmentInfo = new GraphicsPipelineAttachmentInfo(
2024-06-06 19:06:37 +00:00
TextureFormat.D16_UNORM,
2024-02-10 06:19:42 +00:00
new ColorAttachmentDescription(
2024-06-06 19:06:37 +00:00
Window.SwapchainFormat,
2024-02-10 06:19:42 +00:00
ColorAttachmentBlendState.Opaque
)
),
2024-02-07 15:27:55 +00:00
DepthStencilState = DepthStencilState.DepthReadWrite,
VertexInputState = VertexInputState.CreateSingleBinding<PositionColorVertex>(),
PrimitiveType = PrimitiveType.TriangleList,
RasterizerState = RasterizerState.CW_CullBack,
2024-06-06 19:06:37 +00:00
MultisampleState = MultisampleState.None,
VertexShader = cubeVertShader,
FragmentShader = cubeFragShader
2024-02-07 15:27:55 +00:00
};
2024-06-06 19:06:37 +00:00
CubePipeline = new GraphicsPipeline(GraphicsDevice, cubePipelineCreateInfo);
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
cubePipelineCreateInfo.AttachmentInfo = new GraphicsPipelineAttachmentInfo(TextureFormat.D16_UNORM);
CubePipelineDepthOnly = new GraphicsPipeline(GraphicsDevice, cubePipelineCreateInfo);
2024-02-07 15:27:55 +00:00
// Create the skybox pipelines
GraphicsPipelineCreateInfo skyboxPipelineCreateInfo = new GraphicsPipelineCreateInfo
{
AttachmentInfo = new GraphicsPipelineAttachmentInfo(
2024-06-06 19:06:37 +00:00
TextureFormat.D16_UNORM,
2024-02-07 15:27:55 +00:00
new ColorAttachmentDescription(
2024-06-06 19:06:37 +00:00
Window.SwapchainFormat,
2024-02-07 15:27:55 +00:00
ColorAttachmentBlendState.Opaque
)
),
DepthStencilState = DepthStencilState.DepthReadWrite,
VertexInputState = VertexInputState.CreateSingleBinding<PositionVertex>(),
PrimitiveType = PrimitiveType.TriangleList,
RasterizerState = RasterizerState.CW_CullNone,
MultisampleState = MultisampleState.None,
2024-06-06 19:06:37 +00:00
VertexShader = skyboxVertShader,
FragmentShader = skyboxFragShader
2024-02-07 15:27:55 +00:00
};
2024-06-06 19:06:37 +00:00
SkyboxPipeline = new GraphicsPipeline(GraphicsDevice, skyboxPipelineCreateInfo);
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
skyboxPipelineCreateInfo.AttachmentInfo = new GraphicsPipelineAttachmentInfo(TextureFormat.D16_UNORM);
SkyboxPipelineDepthOnly = new GraphicsPipeline(GraphicsDevice, skyboxPipelineCreateInfo);
2024-02-07 15:27:55 +00:00
// Create the blit pipeline
GraphicsPipelineCreateInfo blitPipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
2024-06-06 19:06:37 +00:00
Window.SwapchainFormat,
blitVertShader,
blitFragShader
2024-02-07 15:27:55 +00:00
);
blitPipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
2024-06-06 19:06:37 +00:00
BlitPipeline = new GraphicsPipeline(GraphicsDevice, blitPipelineCreateInfo);
2024-02-07 15:27:55 +00:00
}
private void UploadGPUAssets()
{
Logger.LogInfo("Loading...");
2024-03-01 23:03:29 +00:00
var cubeVertexData = new Span<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(0f, 1f, 0f)),
new PositionColorVertex(new Vector3(1, -1, 1), new Color(0f, 1f, 0f)),
new PositionColorVertex(new Vector3(1, 1, 1), new Color(0f, 1f, 0f)),
new PositionColorVertex(new Vector3(-1, 1, 1), new Color(0f, 1f, 0f)),
new PositionColorVertex(new Vector3(-1, -1, -1), new Color(0f, 0f, 1f)),
new PositionColorVertex(new Vector3(-1, 1, -1), new Color(0f, 0f, 1f)),
new PositionColorVertex(new Vector3(-1, 1, 1), new Color(0f, 0f, 1f)),
new PositionColorVertex(new Vector3(-1, -1, 1), new Color(0f, 0f, 1f)),
new PositionColorVertex(new Vector3(1, -1, -1), new Color(1f, 0.5f, 0f)),
new PositionColorVertex(new Vector3(1, 1, -1), new Color(1f, 0.5f, 0f)),
new PositionColorVertex(new Vector3(1, 1, 1), new Color(1f, 0.5f, 0f)),
new PositionColorVertex(new Vector3(1, -1, 1), new Color(1f, 0.5f, 0f)),
new PositionColorVertex(new Vector3(-1, -1, -1), new Color(1f, 0f, 0.5f)),
new PositionColorVertex(new Vector3(-1, -1, 1), new Color(1f, 0f, 0.5f)),
new PositionColorVertex(new Vector3(1, -1, 1), new Color(1f, 0f, 0.5f)),
new PositionColorVertex(new Vector3(1, -1, -1), new Color(1f, 0f, 0.5f)),
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))
]);
var skyboxVertexData = new Span<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)),
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)),
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)),
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))
]);
var indexData = new Span<uint>([
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
]);
var blitVertexData = new Span<PositionTextureVertex>([
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, 0)),
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)),
]);
2024-02-22 07:30:15 +00:00
var resourceUploader = new ResourceUploader(GraphicsDevice);
2024-02-22 07:30:15 +00:00
2024-06-06 19:06:37 +00:00
CubeVertexBuffer = resourceUploader.CreateBuffer(cubeVertexData, BufferUsageFlags.Vertex);
skyboxVertexBuffer = resourceUploader.CreateBuffer(skyboxVertexData, BufferUsageFlags.Vertex);
2024-06-06 19:06:37 +00:00
IndexBuffer = resourceUploader.CreateBuffer(indexData, BufferUsageFlags.Index);
BlitVertexBuffer = resourceUploader.CreateBuffer(blitVertexData, BufferUsageFlags.Vertex);
2024-02-22 07:30:15 +00:00
2024-06-06 19:06:37 +00:00
CubeVertexBuffer.Name = "Cube Vertices";
2024-03-11 23:11:27 +00:00
skyboxVertexBuffer.Name = "Skybox Vertices";
2024-06-06 19:06:37 +00:00
IndexBuffer.Name = "Cube Indices";
BlitVertexBuffer.Name = "Blit Vertices";
2024-03-11 23:11:27 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-22 07:30:15 +00:00
2024-03-01 23:03:29 +00:00
LoadCubemap(new string[]
{
TestUtils.GetTexturePath("right.png"),
TestUtils.GetTexturePath("left.png"),
TestUtils.GetTexturePath("top.png"),
TestUtils.GetTexturePath("bottom.png"),
TestUtils.GetTexturePath("front.png"),
TestUtils.GetTexturePath("back.png")
});
2024-02-22 07:30:15 +00:00
2024-02-07 15:27:55 +00:00
finishedLoading = true;
Logger.LogInfo("Finished loading!");
Logger.LogInfo("Press Left to toggle Depth-Only Mode");
Logger.LogInfo("Press Down to move the camera upwards");
2023-04-19 07:42:10 +00:00
Logger.LogInfo("Press Right to save a screenshot");
2024-02-07 15:27:55 +00:00
}
2024-06-06 19:06:37 +00:00
public override void Update(System.TimeSpan delta)
2024-02-07 15:27:55 +00:00
{
cubeTimer += (float) delta.TotalSeconds;
previousCubeRotation = cubeRotation;
cubeRotation = Quaternion.CreateFromYawPitchRoll(
cubeTimer * 2f,
0,
cubeTimer * 2f
);
if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Bottom))
{
camPos.Y = System.MathF.Min(camPos.Y + 0.2f, 15f);
}
else
{
camPos.Y = System.MathF.Max(camPos.Y - 0.4f, 1.5f);
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{
depthOnlyEnabled = !depthOnlyEnabled;
Logger.LogInfo("Depth-Only Mode enabled: " + depthOnlyEnabled);
}
2023-04-19 07:42:10 +00:00
if (!screenshotInProgress && TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
2023-04-19 07:42:10 +00:00
{
takeScreenshot = true;
}
2024-02-07 15:27:55 +00:00
}
2024-06-06 19:06:37 +00:00
public override void Draw(double alpha)
2024-02-07 15:27:55 +00:00
{
Matrix4x4 proj = Matrix4x4.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(75f),
2024-06-06 19:06:37 +00:00
(float) Window.Width / Window.Height,
DepthUniforms.ZNear,
DepthUniforms.ZFar
2024-02-07 15:27:55 +00:00
);
Matrix4x4 view = Matrix4x4.CreateLookAt(
camPos,
Vector3.Zero,
Vector3.Up
);
TransformVertexUniform skyboxUniforms = new TransformVertexUniform(view * proj);
Matrix4x4 model = Matrix4x4.CreateFromQuaternion(
Quaternion.Slerp(
previousCubeRotation,
cubeRotation,
(float) alpha
)
);
TransformVertexUniform cubeUniforms = new TransformVertexUniform(model * view * proj);
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
2024-06-06 19:06:37 +00:00
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
2024-02-07 15:27:55 +00:00
if (swapchainTexture != null)
{
if (!finishedLoading)
{
float sine = System.MathF.Abs(System.MathF.Sin(cubeTimer));
Color clearColor = new Color(sine, sine, sine);
// Just show a clear screen.
2024-06-06 19:06:37 +00:00
var renderPass = cmdbuf.BeginRenderPass(
new ColorAttachmentInfo(
swapchainTexture,
false,
clearColor
)
);
cmdbuf.EndRenderPass(renderPass);
2024-02-07 15:27:55 +00:00
}
else
{
2024-06-06 19:06:37 +00:00
RenderPass renderPass;
2024-02-07 15:27:55 +00:00
if (!depthOnlyEnabled)
{
2024-06-06 19:06:37 +00:00
renderPass = cmdbuf.BeginRenderPass(
new DepthStencilAttachmentInfo(DepthTexture, true, new DepthStencilValue(1f, 0)),
new ColorAttachmentInfo(swapchainTexture, false, LoadOp.DontCare)
2024-02-07 15:27:55 +00:00
);
}
else
{
2024-06-06 19:06:37 +00:00
renderPass = cmdbuf.BeginRenderPass(
new DepthStencilAttachmentInfo(DepthTexture, true, new DepthStencilValue(1f, 0), StoreOp.Store)
2024-02-07 15:27:55 +00:00
);
}
2024-03-01 23:03:29 +00:00
// Draw cube
2024-06-06 19:06:37 +00:00
renderPass.BindGraphicsPipeline(depthOnlyEnabled ? CubePipelineDepthOnly : CubePipeline);
renderPass.BindVertexBuffer(CubeVertexBuffer);
renderPass.BindIndexBuffer(IndexBuffer, IndexElementSize.ThirtyTwo);
renderPass.PushVertexUniformData(cubeUniforms);
renderPass.DrawIndexedPrimitives(0, 0, 12);
2024-03-01 23:03:29 +00:00
// Draw skybox
2024-06-06 19:06:37 +00:00
renderPass.BindGraphicsPipeline(depthOnlyEnabled ? SkyboxPipelineDepthOnly : SkyboxPipeline);
renderPass.BindVertexBuffer(skyboxVertexBuffer);
renderPass.BindIndexBuffer(IndexBuffer, IndexElementSize.ThirtyTwo);
renderPass.BindFragmentSampler(new TextureSamplerBinding(SkyboxTexture, SkyboxSampler));
renderPass.PushVertexUniformData(skyboxUniforms);
renderPass.DrawIndexedPrimitives(0, 0, 12);
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
cmdbuf.EndRenderPass(renderPass);
2024-02-07 15:27:55 +00:00
if (depthOnlyEnabled)
{
// Draw the depth buffer as a grayscale image
2024-06-06 19:06:37 +00:00
renderPass = cmdbuf.BeginRenderPass(
new ColorAttachmentInfo(
swapchainTexture,
false,
LoadOp.Load
)
);
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
renderPass.BindGraphicsPipeline(BlitPipeline);
renderPass.BindFragmentSampler(new TextureSamplerBinding(DepthTexture, DepthSampler));
renderPass.BindVertexBuffer(BlitVertexBuffer);
renderPass.PushFragmentUniformData(DepthUniforms);
renderPass.DrawPrimitives(0, 2);
2024-02-07 15:27:55 +00:00
2024-06-06 19:06:37 +00:00
cmdbuf.EndRenderPass(renderPass);
2024-02-07 15:27:55 +00:00
}
2024-02-23 06:51:01 +00:00
if (takeScreenshot)
{
2024-06-06 19:06:37 +00:00
var copyPass = cmdbuf.BeginCopyPass();
copyPass.DownloadFromTexture(swapchainTexture, ScreenshotTransferBuffer, new BufferImageCopy(0, 0, 0));
cmdbuf.EndCopyPass(copyPass);
2024-02-23 06:51:01 +00:00
2024-06-06 19:06:37 +00:00
swapchainDownloaded = true;
2024-02-23 06:51:01 +00:00
}
2024-02-07 15:27:55 +00:00
}
}
2024-06-06 19:06:37 +00:00
if (takeScreenshot && swapchainDownloaded)
2023-04-19 07:42:10 +00:00
{
2024-06-06 19:06:37 +00:00
ScreenshotFence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
2024-02-23 06:51:01 +00:00
Task.Run(TakeScreenshot);
2023-04-19 07:42:10 +00:00
takeScreenshot = false;
2024-06-06 19:06:37 +00:00
swapchainDownloaded = false;
2023-04-19 07:42:10 +00:00
}
else
{
GraphicsDevice.Submit(cmdbuf);
}
2024-02-07 15:27:55 +00:00
}
2024-02-23 06:51:01 +00:00
private unsafe void TakeScreenshot()
2023-04-19 07:42:10 +00:00
{
screenshotInProgress = true;
2024-02-23 06:51:01 +00:00
2024-06-06 19:06:37 +00:00
GraphicsDevice.WaitForFence(ScreenshotFence);
2024-02-23 06:51:01 +00:00
ImageUtils.SavePNG(
Path.Combine(System.AppContext.BaseDirectory, "screenshot.png"),
2024-06-06 19:06:37 +00:00
ScreenshotTransferBuffer,
2024-02-23 06:51:01 +00:00
0,
2024-06-06 19:06:37 +00:00
(int) ScreenshotTexture.Width,
(int) ScreenshotTexture.Height,
ScreenshotTexture.Format == TextureFormat.B8G8R8A8
2024-02-23 06:51:01 +00:00
);
2024-06-06 19:06:37 +00:00
GraphicsDevice.ReleaseFence(ScreenshotFence);
ScreenshotFence = null;
screenshotInProgress = false;
2024-02-23 06:51:01 +00:00
}
2023-04-19 07:42:10 +00:00
2024-06-06 19:06:37 +00:00
public override void Destroy()
{
CubePipeline.Dispose();
CubePipelineDepthOnly.Dispose();
SkyboxPipeline.Dispose();
SkyboxPipelineDepthOnly.Dispose();
BlitPipeline.Dispose();
DepthTexture.Dispose();
DepthSampler.Dispose();
CubeVertexBuffer.Dispose();
skyboxVertexBuffer.Dispose();
BlitVertexBuffer.Dispose();
IndexBuffer.Dispose();
ScreenshotTransferBuffer.Dispose();
ScreenshotTexture.Dispose();
SkyboxTexture.Dispose();
SkyboxSampler.Dispose();
}
2024-02-07 15:27:55 +00:00
}
}