MoonWorksTest/src/TestGame.cs

321 lines
10 KiB
C#
Raw Normal View History

2021-01-20 04:08:29 +00:00
using MoonWorks;
2021-01-20 05:33:32 +00:00
using MoonWorks.Audio;
2021-01-20 04:08:29 +00:00
using MoonWorks.Graphics;
2021-01-27 04:29:10 +00:00
using MoonWorks.Input;
using MoonWorks.Window;
2021-01-20 05:33:32 +00:00
using System.IO;
2021-01-20 04:08:29 +00:00
using System.Threading;
namespace MoonWorksTest
{
public class TestGame : Game
{
ShaderModule passthroughVertexShaderModule;
ShaderModule raymarchFragmentShaderModule;
RaymarchUniforms raymarchUniforms;
Texture woodTexture;
Texture noiseTexture;
Sampler sampler;
Buffer vertexBuffer;
2021-01-20 21:33:03 +00:00
Rect renderArea;
Rect flip;
Color clearColor;
2021-01-20 04:08:29 +00:00
Texture mainColorTargetTexture;
TextureSlice mainColorTargetTextureSlice;
2021-01-27 04:29:10 +00:00
RenderTarget mainColorTarget;
2021-01-20 04:08:29 +00:00
RenderPass mainRenderPass;
Framebuffer mainFramebuffer;
GraphicsPipeline mainGraphicsPipeline;
byte[] screenshotPixels;
Buffer screenshotBuffer;
uint screenShotBufferSize;
Thread screenshotThread;
2021-01-20 05:33:32 +00:00
StaticSound music;
StaticSoundInstance musicInstance;
2021-01-20 20:43:48 +00:00
StreamingSoundOgg musicStream;
2021-01-20 05:33:32 +00:00
2021-01-20 04:08:29 +00:00
public TestGame(WindowCreateInfo windowCreateInfo, PresentMode presentMode, int targetTimestep = 60, bool debugMode = false) : base(windowCreateInfo, presentMode, targetTimestep, debugMode)
{
var windowWidth = windowCreateInfo.WindowWidth;
var windowHeight = windowCreateInfo.WindowHeight;
2021-01-29 02:08:14 +00:00
passthroughVertexShaderModule = new ShaderModule(GraphicsDevice, Path.Combine("Content", "passthrough_vert.spv"));
raymarchFragmentShaderModule = new ShaderModule(GraphicsDevice, Path.Combine("Content", "hexagon_grid.spv"));
2021-01-20 04:08:29 +00:00
raymarchUniforms.time = 0;
raymarchUniforms.padding = 0;
raymarchUniforms.resolutionX = windowWidth;
raymarchUniforms.resolutionY = windowHeight;
2021-01-29 02:08:14 +00:00
woodTexture = Texture.LoadPNG(GraphicsDevice, "Content/woodgrain.png");
noiseTexture = Texture.LoadPNG(GraphicsDevice, "Content/noise.png");
2021-01-20 04:08:29 +00:00
2021-01-29 01:45:03 +00:00
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearWrap);
2021-01-20 04:08:29 +00:00
/* Load Vertex Data */
var vertices = new Vertex[3];
vertices[0].x = -1;
vertices[0].y = -1;
vertices[0].z = 0;
vertices[0].u = 0;
vertices[0].v = 1;
vertices[1].x = 3;
vertices[1].y = -1;
vertices[1].z = 0;
vertices[1].u = 1;
vertices[1].v = 1;
vertices[2].x = -1;
vertices[2].y = 3;
vertices[2].z = 0;
vertices[2].u = 0;
vertices[2].v = 0;
2021-01-20 21:33:03 +00:00
vertexBuffer = new Buffer(GraphicsDevice, BufferUsageFlags.Vertex, 4 * 5 * 3);
2021-01-30 00:03:11 +00:00
vertexBuffer.SetData(vertices);
2021-01-20 04:08:29 +00:00
/* Render Pass */
2021-01-29 01:45:03 +00:00
renderArea.X = 0;
renderArea.Y = 0;
renderArea.W = (int) windowWidth;
renderArea.H = (int) windowHeight;
2021-01-20 04:08:29 +00:00
2021-01-29 01:45:03 +00:00
flip.X = 0;
flip.Y = (int) windowHeight;
flip.W = (int) windowWidth;
flip.H = -(int) windowHeight;
2021-01-20 04:08:29 +00:00
2021-01-27 04:29:10 +00:00
clearColor.R = 237;
clearColor.G = 41;
clearColor.B = 57;
clearColor.A = byte.MaxValue;
2021-01-20 04:08:29 +00:00
2021-01-20 21:33:03 +00:00
ColorTargetDescription colorTargetDescription = new ColorTargetDescription
2021-01-20 04:08:29 +00:00
{
2021-01-29 01:45:03 +00:00
Format = TextureFormat.R8G8B8A8,
MultisampleCount = SampleCount.One,
LoadOp = LoadOp.Clear,
StoreOp = StoreOp.Store
2021-01-20 04:08:29 +00:00
};
mainRenderPass = new RenderPass(GraphicsDevice, colorTargetDescription);
mainColorTargetTexture = Texture.CreateTexture2D(
GraphicsDevice,
windowWidth,
windowHeight,
2021-01-27 04:29:10 +00:00
TextureFormat.R8G8B8A8,
TextureUsageFlags.ColorTarget
2021-01-20 04:08:29 +00:00
);
mainColorTargetTextureSlice = new TextureSlice(mainColorTargetTexture);
2021-01-27 04:29:10 +00:00
mainColorTarget = new RenderTarget(GraphicsDevice, mainColorTargetTextureSlice);
2021-01-20 04:08:29 +00:00
mainFramebuffer = new Framebuffer(
GraphicsDevice,
windowWidth,
windowHeight,
mainRenderPass,
null,
mainColorTarget
);
/* Pipeline */
ColorTargetBlendState[] colorTargetBlendStates = new ColorTargetBlendState[1]
{
ColorTargetBlendState.None
};
ColorBlendState colorBlendState = new ColorBlendState
{
LogicOpEnable = false,
2021-01-20 21:33:03 +00:00
LogicOp = LogicOp.NoOp,
2021-01-20 04:08:29 +00:00
BlendConstants = new BlendConstants(),
ColorTargetBlendStates = colorTargetBlendStates
};
DepthStencilState depthStencilState = DepthStencilState.Disable;
ShaderStageState vertexShaderState = new ShaderStageState
{
ShaderModule = passthroughVertexShaderModule,
EntryPointName = "main",
UniformBufferSize = 0
};
ShaderStageState fragmentShaderState = new ShaderStageState
{
ShaderModule = raymarchFragmentShaderModule,
EntryPointName = "main",
2021-01-27 04:29:10 +00:00
UniformBufferSize = 16
2021-01-20 04:08:29 +00:00
};
MultisampleState multisampleState = MultisampleState.None;
2021-01-29 01:45:03 +00:00
GraphicsPipelineLayoutInfo pipelineLayoutInfo = new GraphicsPipelineLayoutInfo
2021-01-20 04:08:29 +00:00
{
VertexSamplerBindingCount = 0,
FragmentSamplerBindingCount = 2
};
2021-01-27 04:29:10 +00:00
RasterizerState rasterizerState = RasterizerState.CW_CullBack;
2021-01-20 04:08:29 +00:00
2021-01-20 21:33:03 +00:00
var vertexBindings = new VertexBinding[1]
2021-01-20 04:08:29 +00:00
{
2021-01-20 21:33:03 +00:00
new VertexBinding
2021-01-20 04:08:29 +00:00
{
2021-01-29 01:45:03 +00:00
Binding = 0,
InputRate = VertexInputRate.Vertex,
Stride = 4 * 5
2021-01-20 04:08:29 +00:00
}
};
2021-01-20 21:33:03 +00:00
var vertexAttributes = new VertexAttribute[2]
2021-01-20 04:08:29 +00:00
{
2021-01-20 21:33:03 +00:00
new VertexAttribute
2021-01-20 04:08:29 +00:00
{
2021-01-29 01:45:03 +00:00
Binding = 0,
Location = 0,
Format = VertexElementFormat.Vector3,
Offset = 0
2021-01-20 04:08:29 +00:00
},
2021-01-20 21:33:03 +00:00
new VertexAttribute
2021-01-20 04:08:29 +00:00
{
2021-01-29 01:45:03 +00:00
Binding = 0,
Location = 1,
Format = VertexElementFormat.Vector2,
Offset = 4 * 3
2021-01-20 04:08:29 +00:00
}
};
VertexInputState vertexInputState = new VertexInputState
{
VertexBindings = vertexBindings,
VertexAttributes = vertexAttributes
};
2021-01-20 21:33:03 +00:00
var viewports = new Viewport[1]
2021-01-20 04:08:29 +00:00
{
2021-01-20 21:33:03 +00:00
new Viewport
2021-01-20 04:08:29 +00:00
{
2021-01-29 01:45:03 +00:00
X = 0,
Y = 0,
W = windowWidth,
H = windowHeight,
MinDepth = 0,
MaxDepth = 1
2021-01-20 04:08:29 +00:00
}
};
2021-01-20 21:33:03 +00:00
var scissors = new Rect[1]
2021-01-20 04:08:29 +00:00
{
2021-01-20 21:33:03 +00:00
new Rect
2021-01-20 04:08:29 +00:00
{
2021-01-29 01:45:03 +00:00
X = 0,
Y = 0,
W = (int) windowWidth,
H = (int) windowHeight
2021-01-20 04:08:29 +00:00
}
};
ViewportState viewportState = new ViewportState
{
Viewports = viewports,
Scissors = scissors
};
2021-01-29 01:45:03 +00:00
var graphicsPipelineCreateInfo = new GraphicsPipelineCreateInfo
{
ColorBlendState = colorBlendState,
DepthStencilState = depthStencilState,
VertexShaderState = vertexShaderState,
FragmentShaderState = fragmentShaderState,
MultisampleState = multisampleState,
PipelineLayoutInfo = pipelineLayoutInfo,
RasterizerState = rasterizerState,
PrimitiveType = PrimitiveType.TriangleList,
VertexInputState = vertexInputState,
ViewportState = viewportState,
RenderPass = mainRenderPass
};
2021-01-20 04:08:29 +00:00
mainGraphicsPipeline = new GraphicsPipeline(
GraphicsDevice,
2021-01-29 01:45:03 +00:00
graphicsPipelineCreateInfo
2021-01-20 04:08:29 +00:00
);
screenShotBufferSize = windowWidth * windowHeight * 4;
screenshotPixels = new byte[screenShotBufferSize];
screenshotBuffer = new Buffer(GraphicsDevice, 0, screenShotBufferSize);
screenshotThread = new Thread(new ThreadStart(SaveScreenshot));
2021-01-20 05:33:32 +00:00
2021-01-29 02:08:14 +00:00
music = StaticSound.LoadOgg(AudioDevice, Path.Combine("Content", "title_screen.ogg"));
2021-01-20 05:33:32 +00:00
musicInstance = music.CreateInstance();
2021-01-20 20:43:48 +00:00
// musicInstance.Play();
2021-01-29 02:08:14 +00:00
musicStream = StreamingSoundOgg.Load(AudioDevice, Path.Combine("Content", "title_screen.ogg"), false, true);
2021-01-20 20:43:48 +00:00
musicStream.Play();
2021-01-20 04:08:29 +00:00
}
protected override void Update(double dt)
{
raymarchUniforms.time += (float) dt;
}
2021-01-27 04:29:10 +00:00
protected override void Draw(double dt, double alpha)
2021-01-20 04:08:29 +00:00
{
2021-01-27 04:29:10 +00:00
var screenshotPressed = Inputs.Keyboard.IsPressed(Keycode.S);
2021-01-20 04:08:29 +00:00
var commandBuffer = GraphicsDevice.AcquireCommandBuffer();
commandBuffer.BeginRenderPass(
mainRenderPass,
mainFramebuffer,
2021-01-27 04:29:10 +00:00
renderArea,
2021-01-20 04:08:29 +00:00
clearColor
);
commandBuffer.BindGraphicsPipeline(mainGraphicsPipeline);
var fragmentParamOffset = commandBuffer.PushFragmentShaderParams(raymarchUniforms);
commandBuffer.BindVertexBuffers(0, new BufferBinding(vertexBuffer, 0));
commandBuffer.BindFragmentSamplers(
new TextureSamplerBinding(woodTexture, sampler),
new TextureSamplerBinding(noiseTexture, sampler)
);
commandBuffer.DrawPrimitives(0, 1, 0, fragmentParamOffset);
commandBuffer.EndRenderPass();
if (screenshotPressed)
{
2021-01-27 04:29:10 +00:00
commandBuffer.CopyTextureToBuffer(mainColorTargetTextureSlice, screenshotBuffer);
2021-01-20 04:08:29 +00:00
}
2021-01-27 04:29:10 +00:00
commandBuffer.QueuePresent(mainColorTargetTextureSlice, flip, Filter.Nearest);
2021-01-20 04:08:29 +00:00
GraphicsDevice.Submit(commandBuffer);
if (screenshotPressed)
{
screenshotThread.Start();
}
}
private void SaveScreenshot()
{
GraphicsDevice.Wait();
screenshotBuffer.GetData(screenshotPixels, screenShotBufferSize);
Texture.SavePNG("screenshot.png", 1280, 720, screenshotPixels);
}
}
}