Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Templates for Windowing, OpenGL #748

Merged
merged 16 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Silk.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrototypeStructChaining.Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Silk.NET.Vulkan.Tests", "src\Vulkan\Silk.NET.Vulkan.Tests\Silk.NET.Vulkan.Tests.csproj", "{225BA79C-36FE-421A-85E4-D15F8B61869B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Templates", "Templates", "{278FE083-D2F7-4DD2-9D27-6AD883E3A4B8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Silk.NET.Templates", "src\Templates\Silk.NET.Templates\Silk.NET.Templates.csproj", "{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.Vulkan.Extensions.SEC", "src\Vulkan\Extensions\Silk.NET.Vulkan.Extensions.SEC\Silk.NET.Vulkan.Extensions.SEC.csproj", "{2B4B5BF6-180E-4867-A4D1-664E522FC778}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Silk.NET.OpenGLES.ANGLE.Native", "src\Native\Silk.NET.OpenGLES.ANGLE.Native\Silk.NET.OpenGLES.ANGLE.Native.csproj", "{8D02DFEB-121A-449B-BC39-09C3F9A88E07}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Silk.NET.Vulkan.Loader.Native", "src\Native\Silk.NET.Vulkan.Loader.Native\Silk.NET.Vulkan.Loader.Native.csproj", "{36C38837-8250-42F9-ABDA-DEFC1AB129E1}"
Expand Down Expand Up @@ -2925,6 +2931,30 @@ Global
{225BA79C-36FE-421A-85E4-D15F8B61869B}.Release|x64.Build.0 = Release|Any CPU
{225BA79C-36FE-421A-85E4-D15F8B61869B}.Release|x86.ActiveCfg = Release|Any CPU
{225BA79C-36FE-421A-85E4-D15F8B61869B}.Release|x86.Build.0 = Release|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Debug|x64.ActiveCfg = Debug|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Debug|x64.Build.0 = Debug|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Debug|x86.ActiveCfg = Debug|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Debug|x86.Build.0 = Debug|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Release|Any CPU.Build.0 = Release|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Release|x64.ActiveCfg = Release|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Release|x64.Build.0 = Release|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Release|x86.ActiveCfg = Release|Any CPU
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2}.Release|x86.Build.0 = Release|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Debug|x64.ActiveCfg = Debug|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Debug|x64.Build.0 = Debug|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Debug|x86.ActiveCfg = Debug|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Debug|x86.Build.0 = Debug|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Release|Any CPU.Build.0 = Release|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Release|x64.ActiveCfg = Release|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Release|x64.Build.0 = Release|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Release|x86.ActiveCfg = Release|Any CPU
{2B4B5BF6-180E-4867-A4D1-664E522FC778}.Release|x86.Build.0 = Release|Any CPU
{8D02DFEB-121A-449B-BC39-09C3F9A88E07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D02DFEB-121A-449B-BC39-09C3F9A88E07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D02DFEB-121A-449B-BC39-09C3F9A88E07}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -3574,6 +3604,9 @@ Global
{EEFF37DA-E4F2-406E-AF97-8615BB7BC34C} = {B15922CB-815C-4038-B635-EE2D8A8F700B}
{BD19250B-E143-4F4E-9E1D-18829CCB3642} = {B15922CB-815C-4038-B635-EE2D8A8F700B}
{225BA79C-36FE-421A-85E4-D15F8B61869B} = {E2ABDF45-C329-47B2-8E09-B7298E2557F7}
{278FE083-D2F7-4DD2-9D27-6AD883E3A4B8} = {16AFCF73-8CC1-4B5D-8969-A90F468DC6D5}
{4D65197C-79B2-40CF-846E-C1EAB4F6EEA2} = {278FE083-D2F7-4DD2-9D27-6AD883E3A4B8}
{2B4B5BF6-180E-4867-A4D1-664E522FC778} = {49BC383A-D7E6-4013-93C7-371479B984CC}
{8D02DFEB-121A-449B-BC39-09C3F9A88E07} = {72E7FA64-5B1E-477D-BD30-63B7F206B3C4}
{36C38837-8250-42F9-ABDA-DEFC1AB129E1} = {72E7FA64-5B1E-477D-BD30-63B7F206B3C4}
{54F439B6-36E4-4FB0-8731-F73D42AD921F} = {72E7FA64-5B1E-477D-BD30-63B7F206B3C4}
Expand Down
10 changes: 7 additions & 3 deletions build/nuke/Build.ReviewHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ partial class Build
// ReSharper disable once RedundantEmptyObjectOrCollectionInitializer
readonly HashSet<string> AllowedExclusions = new()
{
"silkwindow",
"silkgl",
"silkgltriangle",
"DotZLib"
};

Target ValidateSolution => CommonTarget
Expand All @@ -20,8 +24,8 @@ partial class Build
(
() =>
{
var files = SourceDirectory.GlobFiles("**/*.csproj").ToArray();
Logger.Info($"Found {files.Length} csproj files in \"{SourceDirectory}\"");
var files = RootDirectory.GlobFiles("**\\*.csproj").Concat(RootDirectory.GlobFiles("**/*.csproj")).ToArray();
Logger.Info($"Found {files.Length} csproj files in \"{RootDirectory}\"");
var missedOut = new List<string>();
foreach (var file in files)
{
Expand All @@ -41,7 +45,7 @@ partial class Build
(
"A project has not been included in the solution and will not be shipped! " +
$"\"{file}\" if this is acceptable please add the project name (excluding the path and " +
"extension) to the AllowedExclusions array in the NUKE Build.CI.AutoReview.cs file."
"extension) to the AllowedExclusions array in the NUKE Build.ReviewHelpers.cs file."
);

missedOut.Add(Path.GetRelativePath(RootDirectory, file).Replace('\\', '/'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
24 changes: 24 additions & 0 deletions src/Templates/Silk.NET.Templates/Silk.NET.Templates.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<PackageType>Template</PackageType>
<SilkDescription>A package with template projects for Silk.NET.</SilkDescription>
<SilkExtendedDescription>This is a quick and easy way to get started with Silk.NET. First, install the templates using `dotnet new --install Silk.NET.Templates`, and then create a project using these templates:%0a- **OpenGL (with a triangle)**: `dotnet new silkgltriangle`%0a**OpenGL (blank)**: `dotnet new silkgl`%0a- **Blank Window**: `dotnet new silkwindow`</SilkExtendedDescription>
<SilkSourceLinkExempt>true</SilkSourceLinkExempt>

<TargetFramework>netstandard2.0</TargetFramework>

<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<NoWarn>$(NoWarn);NU5128</NoWarn>
</PropertyGroup>

<ItemGroup>
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" />
</ItemGroup>

<Import Project="..\..\..\build\props\common.props"/>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkGL",
"name": "Silk.NET Window (OpenGL)",
"shortName": "silkgl",
"description": "A Silk.NET Window with user input and an OpenGL Context.",
"tags": {
"language": "C#",
"type": "project"
}
}
48 changes: 48 additions & 0 deletions src/Templates/Silk.NET.Templates/templates/silkgl/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Silk.NET.Windowing;
using Silk.NET.Input;
using Silk.NET.OpenGL;

WindowOptions windowOptions = WindowOptions.Default with
{
Title = "My Silk.NET Window",
PreferredDepthBufferBits = 24,
PreferredStencilBufferBits = 8,
API = new GraphicsAPI(ContextAPI.OpenGL, new APIVersion(3, 3))
};

using IWindow window = Window.Create(windowOptions);
IInputContext inputContext = null!;
GL gl = null!;

window.Load += () =>
{
// ran on first startup - use this event to initialize stuff.
gl = window.CreateOpenGL();
inputContext = window.CreateInput();
gl.Viewport(window.FramebufferSize);
};

window.Update += deltaSeconds =>
{
// ran every frame but before render - use this event to update data (e.g. physics).
};

window.Render += deltaSeconds =>
{
// ran every frame but after update - use this event to draw.
};

window.FramebufferResize += newSize =>
{
// ran when the window framebuffer is resized - usually used to update the viewport and, in 3D apps, view matrices.
gl.Viewport(newSize);
};

window.Closing += () =>
{
// ran just before the window closes.
};

window.Run();
gl.Dispose();
inputContext.Dispose();
14 changes: 14 additions & 0 deletions src/Templates/Silk.NET.Templates/templates/silkgl/silkgl.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Silk.NET.Windowing" Version="2.*" />
<PackageReference Include="Silk.NET.Input" Version="2.*" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkGLTriangle",
"name": "Silk.NET Triangle (OpenGL)",
"shortName": "silkgltriangle",
"description": "A Silk.NET Window with an OpenGL Context drawing a triangle.",
"tags": {
"language": "C#",
"type": "project"
}
}
149 changes: 149 additions & 0 deletions src/Templates/Silk.NET.Templates/templates/silkgltriangle/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using Silk.NET.Windowing;
using Silk.NET.Input;
using Silk.NET.OpenGL;

WindowOptions windowOptions = WindowOptions.Default with
{
Title = "My Silk.NET Triangle",
PreferredDepthBufferBits = 24,
PreferredStencilBufferBits = 8,
API = new GraphicsAPI(ContextAPI.OpenGL, new APIVersion(3, 3))
};

using IWindow window = Window.Create(windowOptions);
IInputContext inputContext = null!;
GL gl = null!;

uint vbo = 0;
uint vao = 0;
uint shader = 0;

const string VertexShaderCode = @"
#version 330 core
layout (location = 0) in vec3 vPosition;
layout (location = 1) in vec4 vColor;

out vec4 fColor;

void main() {
gl_Position = vec4(vPosition, 1.0);
fColor = vColor;
}";

const string FragmentShaderCode = @"
#version 330 core
in vec4 fColor;

out vec4 FragColor;

void main() {
FragColor = fColor;
}";

window.Load += () =>
{
// ran on first startup - use this event to initialize stuff.
gl = window.CreateOpenGL();
inputContext = window.CreateInput();

// We create a Span with our vertex data. Since it's very little
// data, we'll just allocate it in the stack.
Span<Vertex> vertexData = stackalloc Vertex[]
{
new Vertex(new Vector3(-0.5f, -0.5f, 0), 255, 0, 0, 255),
new Vertex(new Vector3(0, 0.5f, 0), 0, 255, 0, 255),
new Vertex(new Vector3(0.5f, -0.5f, 0), 0, 0, 255, 255)
};

// We create or GL Buffer Object, bind it and fill it up with data
// from our span.
vbo = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
gl.BufferData<Vertex>(BufferTargetARB.ArrayBuffer, vertexData, BufferUsageARB.StaticDraw);

// We create or GL Vertex Array Object, bind it and specify two
// vertex attributes sourced from the currently bound buffer;
// Attrib 0: 3 floats (a vec3)
// Attrib 1: 4 unsigned bytes, normalized into a vec4
vao = gl.GenVertexArray();
gl.BindVertexArray(vao);
unsafe
{
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.Size, (void*)0);
gl.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, true, Vertex.Size, (void*)12);
}
gl.EnableVertexAttribArray(0);
gl.EnableVertexAttribArray(1);

// We create the vertex shader, give it it's source code, and compile.
uint vs = gl.CreateShader(ShaderType.VertexShader);
gl.ShaderSource(vs, VertexShaderCode);
gl.CompileShader(vs);

// We create the fragment shader, give it it's source code, and compile.
uint fs = gl.CreateShader(ShaderType.FragmentShader);
gl.ShaderSource(fs, FragmentShaderCode);
gl.CompileShader(fs);

// We create the GL Shader Program Object, which links the vertex and
// fragment shaders into a single program.
shader = gl.CreateProgram();
gl.AttachShader(shader, vs);
gl.AttachShader(shader, fs);
gl.LinkProgram(shader);

// We detach the shaders from the program and delete them. This won't
// have any effect until the program itself is re-linked or deleted.
gl.DetachShader(shader, vs);
gl.DetachShader(shader, fs);
gl.DeleteShader(vs);
gl.DeleteShader(fs);

gl.Viewport(window.FramebufferSize);
};

window.Update += deltaSeconds =>
{
// ran every frame but before render - use this event to update data (e.g. physics).
};

window.Render += deltaSeconds =>
{
// ran every frame but after update - use this event to draw.

// Clear the whole screen to black.
gl.ClearColor(0f, 0f, 0f, 1f);
gl.Clear(ClearBufferMask.ColorBufferBit);

// Draw the 3 vertices from our buffer object as a single triangle.
gl.BindVertexArray(vao);
gl.UseProgram(shader);
gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
};

window.FramebufferResize += newSize =>
{
// ran when the window framebuffer is resized - usually used to update the viewport and, in 3D apps, view matrices.
gl.Viewport(newSize);
};

window.Closing += () =>
{
// ran just before the window closes.
gl.DeleteProgram(shader);
gl.DeleteVertexArray(vao);
gl.DeleteBuffer(vbo);
};

window.Run();
gl.Dispose();
inputContext.Dispose();

[StructLayout(LayoutKind.Sequential)]
record struct Vertex(Vector3 Position, byte R, byte G, byte B, byte A)
{
public static uint Size = 3 * 4 + 4;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Silk.NET.Windowing" Version="2.*" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkWindow",
"name": "Silk.NET Window (Empty)",
"shortName": "silkwindow",
"description": "An empty Silk.NET Window.",
"tags": {
"language": "C#",
"type": "project"
}
}
Loading