Skip to content

Instantly share code, notes, and snippets.

@dmitsuki
Last active June 26, 2018 19:14
Show Gist options
  • Save dmitsuki/53065e7d840bf8e10464fe33686c662c to your computer and use it in GitHub Desktop.
Save dmitsuki/53065e7d840bf8e10464fe33686c662c to your computer and use it in GitHub Desktop.
using ImGuiNET;
using System;
using System.IO;
using System.Numerics;
using System.Text;
using Veldrid;
using Veldrid.ImageSharp;
using Veldrid.Sdl2;
using Veldrid.StartupUtilities;
namespace GarPixel
{
class Program
{
static void Main(string[] args)
{
App app = new App();
app.Run();
}
}
public class App
{
Sdl2Window AppWindow;
GraphicsDevice Device;
ResourceFactory Factory;
CommandList CmdList;
DeviceBuffer CanvasVertextBuffer;
DeviceBuffer CanvasIndexBuffer;
DeviceBuffer ModelBuffer;
DeviceBuffer ProjectionBuffer;
ResourceSet ModelProjectionSet;
ResourceLayout ModelProjectionLayout;
Texture CanvasTexture;
TextureView CanvasTextureView;
ResourceSet CanvasTextureSet;
ResourceLayout CanvasTextureLayout;
Pipeline CanvasPipeline;
ImGuiRenderer GuiRenderer;
string ImageUrl = @"C:\LocalProjects\GarPixel\GarPixel\asset\elf.png";
//Quad points
readonly float[] QuadVerts = new float[] {
// Pos // Tex
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};
ushort[] QuadIndices = { 0, 1, 2, 3, 4, 5 };
public App()
{
WindowCreateInfo windowCI = new WindowCreateInfo()
{
X = 100,
Y = 100,
WindowWidth = 1280,
WindowHeight = 720,
WindowTitle = "Gar Pixel"
};
AppWindow = VeldridStartup.CreateWindow(ref windowCI);
Device = VeldridStartup.CreateGraphicsDevice(AppWindow, GraphicsBackend.OpenGL);
CreateResources();
}
const string VertexShaderSource = @"
#version 330
layout(location = 0) in vec4 posuv;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 projection;
void main(void)
{
TexCoords = posuv.zw;
gl_Position = projection * model * vec4(posuv.xy, 0.0, 1.0);
}
";
const string FragmentShaderSource = @"
#version 330
in vec2 TexCoords;
out vec4 color;
uniform sampler2D image;
void main(void)
{
color = texture(image, TexCoords);
}
";
private void CreateResources()
{
Factory = Device.ResourceFactory;
CmdList = Factory.CreateCommandList();
GuiRenderer = new ImGuiRenderer(
Device,
Device.MainSwapchain.Framebuffer.OutputDescription,
AppWindow.Width,
AppWindow.Height
);
AppWindow.Resized += () =>
{
GuiRenderer.WindowResized(AppWindow.Width, AppWindow.Height);
Device.MainSwapchain.Resize((uint)AppWindow.Width, (uint)AppWindow.Height);
};
CanvasVertextBuffer = Factory.CreateBuffer(new BufferDescription((uint)(QuadVerts.Length * sizeof(float)),
BufferUsage.VertexBuffer));
CanvasIndexBuffer = Factory.CreateBuffer(new BufferDescription(6 * sizeof(ushort), BufferUsage.IndexBuffer));
Device.UpdateBuffer(CanvasVertextBuffer, 0, QuadVerts);
Device.UpdateBuffer(CanvasIndexBuffer, 0, QuadIndices);
ModelBuffer = Factory.CreateBuffer(new BufferDescription(
64,
BufferUsage.UniformBuffer
));
ProjectionBuffer = Factory.CreateBuffer(new BufferDescription(
64,
BufferUsage.UniformBuffer
));
ModelProjectionLayout = Factory.CreateResourceLayout
(
new ResourceLayoutDescription
(
new ResourceLayoutElementDescription
(
"model",
ResourceKind.UniformBuffer,
ShaderStages.Vertex
),
new ResourceLayoutElementDescription
(
"projection",
ResourceKind.UniformBuffer,
ShaderStages.Vertex
)
)
);
ModelProjectionSet = Factory.CreateResourceSet(
new ResourceSetDescription(ModelProjectionLayout, ModelBuffer, ProjectionBuffer));
Shader vertexShader = Factory.CreateShader(new ShaderDescription(
ShaderStages.Vertex,
Encoding.ASCII.GetBytes(VertexShaderSource),
"VS"
));
Shader fragmentShader = Factory.CreateShader(new ShaderDescription(
ShaderStages.Fragment,
Encoding.ASCII.GetBytes(FragmentShaderSource),
"FS"
));
CanvasTextureLayout = Factory.CreateResourceLayout(
new ResourceLayoutDescription(
new ResourceLayoutElementDescription("image", ResourceKind.TextureReadOnly, ShaderStages.Fragment)
)
);
CanvasTexture = new ImageSharpTexture(ImageUrl, false).CreateDeviceTexture(Device, Factory);
CanvasTextureView = Factory.CreateTextureView(CanvasTexture);
CanvasTextureSet = Factory.CreateResourceSet
(
new ResourceSetDescription(
CanvasTextureLayout,
CanvasTextureView)
);
GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription
(
BlendStateDescription.SingleAlphaBlend,
DepthStencilStateDescription.Disabled,
RasterizerStateDescription.CullNone,
PrimitiveTopology.TriangleList,
new ShaderSetDescription(
new VertexLayoutDescription[]
{
new VertexLayoutDescription(
16,
new VertexElementDescription
(
"posuv",
VertexElementSemantic.Position, VertexElementFormat.Float4
)
)
},
new Shader[] { vertexShader, fragmentShader }
),
new[] { ModelProjectionLayout, CanvasTextureLayout },
Device.MainSwapchain.Framebuffer.OutputDescription
);
pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend;
CanvasPipeline = Factory.CreateGraphicsPipeline(pipelineDescription);
}
public void Run()
{
while (AppWindow.Exists)
{
InputSnapshot snapshot = AppWindow.PumpEvents();
GuiRenderer.Update(1f / 60f, snapshot);
bool showWindow = true;
ImGuiNative.igShowDemoWindow(ref showWindow);
if (ImGui.BeginWindow("Test"))
{
ImGui.Text("Hello");
if (ImGui.Button("Quit"))
{
}
ImGui.EndWindow();
}
Device.UpdateBuffer(ProjectionBuffer, 0,
Matrix4x4.CreateOrthographic(AppWindow.Width, AppWindow.Height,
-1, 1));
Matrix4x4 model = Matrix4x4.Identity;
model *= Matrix4x4.CreateScale(200);
model *= Matrix4x4.CreateTranslation(100, 100, 0);
Device.UpdateBuffer(ModelBuffer, 0, model);
CmdList.Begin();
CmdList.SetFramebuffer(Device.MainSwapchain.Framebuffer);
CmdList.ClearColorTarget(0, new RgbaFloat(.3f, .3f, .3f, 1f));
//GuiRenderer.Render(Device, CmdList);
CmdList.SetPipeline(CanvasPipeline);
CmdList.SetVertexBuffer(0, CanvasVertextBuffer);
CmdList.SetIndexBuffer(CanvasIndexBuffer, IndexFormat.UInt16);
CmdList.SetGraphicsResourceSet(0, ModelProjectionSet);
CmdList.SetGraphicsResourceSet(1, CanvasTextureSet);
CmdList.DrawIndexed(6);
CmdList.End();
Device.SubmitCommands(CmdList);
Device.SwapBuffers(Device.MainSwapchain);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment