├── .gitignore ├── AvaloniaSilkExample.sln ├── AvaloniaSilkExample ├── .gitignore ├── App.axaml ├── App.axaml.cs ├── AvaloniaSilkExample.csproj ├── Gl │ ├── BufferObject.cs │ ├── Draw.cs │ ├── GlErrorException.cs │ ├── Shader.cs │ ├── SilkNetExample.cs │ ├── Texture.cs │ └── VertexArrayObject.cs ├── MainWindow.axaml ├── MainWindow.axaml.cs ├── Program.cs ├── shader.frag └── shader.vert └── nuget.config /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Visual Studio 3 | ################# 4 | 5 | ## Ignore Visual Studio temporary files, build results, and 6 | ## files generated by popular Visual Studio add-ons. 7 | 8 | # User-specific files 9 | *.suo 10 | *.user 11 | *.sln.docstates 12 | .vs/ 13 | 14 | # Build results 15 | 16 | *.sln.ide/ 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | [Bb]in/ 21 | [Oo]bj/ 22 | 23 | # MSTest test Results 24 | [Tt]est[Rr]esult*/ 25 | [Bb]uild[Ll]og.* 26 | 27 | *_i.c 28 | *_p.c 29 | *.ilk 30 | *.meta 31 | *.obj 32 | *.pch 33 | *.pdb 34 | *.pgc 35 | *.pgd 36 | *.rsp 37 | *.sbr 38 | *.tlb 39 | *.tli 40 | *.tlh 41 | *.tmp 42 | *.tmp_proj 43 | *.log 44 | *.vspscc 45 | *.vssscc 46 | .builds 47 | *.pidb 48 | *.log 49 | *.scc 50 | 51 | # Visual C++ cache files 52 | ipch/ 53 | *.aps 54 | *.ncb 55 | *.opensdf 56 | *.sdf 57 | *.cachefile 58 | 59 | # Visual Studio profiler 60 | *.psess 61 | *.vsp 62 | *.vspx 63 | 64 | # Guidance Automation Toolkit 65 | *.gpState 66 | 67 | # ReSharper is a .NET coding add-in 68 | _ReSharper*/ 69 | *.[Rr]e[Ss]harper 70 | 71 | # TeamCity is a build add-in 72 | _TeamCity* 73 | 74 | # DotCover is a Code Coverage Tool 75 | *.dotCover 76 | 77 | # Installshield output folder 78 | [Ee]xpress/ 79 | 80 | # DocProject is a documentation generator add-in 81 | DocProject/buildhelp/ 82 | DocProject/Help/*.HxT 83 | DocProject/Help/*.HxC 84 | DocProject/Help/*.hhc 85 | DocProject/Help/*.hhk 86 | DocProject/Help/*.hhp 87 | DocProject/Help/Html2 88 | DocProject/Help/html 89 | 90 | # Click-Once directory 91 | publish/ 92 | 93 | # Publish Web Output 94 | *.Publish.xml 95 | *.pubxml 96 | 97 | # Windows Azure Build Output 98 | csx 99 | *.build.csdef 100 | 101 | # Windows Store app package directory 102 | AppPackages/ 103 | 104 | # NCrunch 105 | _NCrunch_*/ 106 | *.ncrunchsolution.user 107 | nCrunchTemp_* 108 | 109 | # CodeRush 110 | .cr/ 111 | 112 | # Others 113 | sql/ 114 | *.Cache 115 | ClientBin/ 116 | [Ss]tyle[Cc]op.* 117 | ~$* 118 | *~ 119 | *.dbmdl 120 | *.[Pp]ublish.xml 121 | *.pfx 122 | *.publishsettings 123 | 124 | # RIA/Silverlight projects 125 | Generated_Code/ 126 | 127 | # Backup & report files from converting an old project file to a newer 128 | # Visual Studio version. Backup files are not needed, because we have git ;-) 129 | _UpgradeReport_Files/ 130 | Backup*/ 131 | UpgradeLog*.XML 132 | UpgradeLog*.htm 133 | 134 | # SQL Server files 135 | App_Data/*.mdf 136 | App_Data/*.ldf 137 | 138 | ############# 139 | ## Windows detritus 140 | ############# 141 | 142 | # Windows image file caches 143 | Thumbs.db 144 | ehthumbs.db 145 | 146 | # Folder config file 147 | Desktop.ini 148 | 149 | # Recycle Bin used on file shares 150 | $RECYCLE.BIN/ 151 | 152 | # Mac crap 153 | .DS_Store 154 | 155 | ################# 156 | ## Monodevelop 157 | ################# 158 | *.userprefs 159 | *.nugetreferenceswitcher 160 | 161 | 162 | ################# 163 | ## Rider 164 | ################# 165 | .idea 166 | 167 | ################# 168 | ## VS Code 169 | ################# 170 | .vscode/ 171 | 172 | ################# 173 | ## Cake 174 | ################# 175 | tools/* 176 | !tools/packages.config 177 | .nuget 178 | artifacts/ 179 | nuget 180 | project.lock.json 181 | .idea/* 182 | 183 | 184 | ################## 185 | ## BenchmarkDotNet 186 | ################## 187 | BenchmarkDotNet.Artifacts/ 188 | 189 | 190 | 191 | ################## 192 | # XCode 193 | ################## 194 | Index/ 195 | Logs/ 196 | ModuleCache.noindex/ 197 | Build/Intermediates.noindex/ 198 | info.plist 199 | build-intermediate 200 | obj-Direct2D1/ 201 | obj-Skia/ 202 | 203 | ################## 204 | # Vim 205 | ################## 206 | .vim 207 | coc-settings.json 208 | .ccls-cache 209 | .ccls 210 | -------------------------------------------------------------------------------- /AvaloniaSilkExample.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaSilkExample", "AvaloniaSilkExample\AvaloniaSilkExample.csproj", "{6404A683-95A2-4A88-8E9B-08C3EE8A1C6F}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {6404A683-95A2-4A88-8E9B-08C3EE8A1C6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {6404A683-95A2-4A88-8E9B-08C3EE8A1C6F}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {6404A683-95A2-4A88-8E9B-08C3EE8A1C6F}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {6404A683-95A2-4A88-8E9B-08C3EE8A1C6F}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/App.axaml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/App.axaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia; 2 | using Avalonia.Controls.ApplicationLifetimes; 3 | using Avalonia.Markup.Xaml; 4 | 5 | namespace AvaloniaSilkExample 6 | { 7 | public class App : Application 8 | { 9 | public override void Initialize() 10 | { 11 | AvaloniaXamlLoader.Load(this); 12 | } 13 | 14 | public override void OnFrameworkInitializationCompleted() 15 | { 16 | if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) 17 | { 18 | desktop.MainWindow = new MainWindow(); 19 | } 20 | 21 | base.OnFrameworkInitializationCompleted(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /AvaloniaSilkExample/AvaloniaSilkExample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | WinExe 4 | net5.0 5 | enable 6 | true 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | PreserveNewest 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/BufferObject.cs: -------------------------------------------------------------------------------- 1 | using AvaloniaSilkExample.Gl; 2 | using Silk.NET.OpenGL; 3 | using System; 4 | 5 | namespace Tutorial 6 | { 7 | public class BufferObject : IDisposable 8 | where TDataType : unmanaged 9 | { 10 | private uint _handle; 11 | private BufferTargetARB _bufferType; 12 | private GL _gl; 13 | 14 | public unsafe BufferObject(GL gl, Span data, BufferTargetARB bufferType) 15 | { 16 | _gl = gl; 17 | _bufferType = bufferType; 18 | //Clear existing error code. 19 | GLEnum error; 20 | do error = _gl.GetError(); 21 | while (error != GLEnum.NoError); 22 | _handle = _gl.GenBuffer(); 23 | Bind(); 24 | GlErrorException.ThrowIfError(gl); 25 | fixed (void* d = data) 26 | { 27 | _gl.BufferData(bufferType, (nuint) (data.Length * sizeof(TDataType)), d, BufferUsageARB.StaticDraw); 28 | } 29 | GlErrorException.ThrowIfError(gl); 30 | } 31 | 32 | public void Bind() 33 | { 34 | _gl.BindBuffer(_bufferType, _handle); 35 | } 36 | 37 | public void Dispose() 38 | { 39 | _gl.DeleteBuffer(_handle); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/Draw.cs: -------------------------------------------------------------------------------- 1 | 2 | using Silk.NET.OpenGL; 3 | using Silk.NET.Maths; 4 | 5 | namespace Tutorial 6 | { 7 | /* 8 | class Program 9 | { 10 | private static IWindow window; 11 | private static GL Gl; 12 | 13 | private static BufferObject Vbo; 14 | private static BufferObject Ebo; 15 | private static VertexArrayObject Vao; 16 | //Create a texture object. 17 | private static Texture Texture; 18 | private static Shader Shader; 19 | 20 | // OpenGL has image origin in the bottom-left corner. 21 | private static readonly float[] Vertices = 22 | { 23 | //X Y Z U V 24 | 0.5f, 0.5f, 0.0f, 1f, 0f, 25 | 0.5f, -0.5f, 0.0f, 1f, 1f, 26 | -0.5f, -0.5f, 0.0f, 0f, 1f, 27 | -0.5f, 0.5f, 0.5f, 0f, 0f 28 | }; 29 | 30 | private static readonly uint[] Indices = 31 | { 32 | 0, 1, 3, 33 | 1, 2, 3 34 | }; 35 | 36 | 37 | private static void Main(string[] args) 38 | { 39 | var options = WindowOptions.Default; 40 | options.Size = new Vector2D(800, 600); 41 | options.Title = "LearnOpenGL with Silk.NET"; 42 | window = Window.Create(options); 43 | 44 | window.Load += OnLoad; 45 | window.Render += OnRender; 46 | window.Closing += OnClose; 47 | 48 | window.Run(); 49 | } 50 | 51 | 52 | private unsafe static void OnLoad() 53 | { 54 | IInputContext input = window.CreateInput(); 55 | for (int i = 0; i < input.Keyboards.Count; i++) 56 | { 57 | input.Keyboards[i].KeyDown += KeyDown; 58 | } 59 | 60 | Gl = GL.GetApi(window); 61 | 62 | Ebo = new BufferObject(Gl, Indices, BufferTargetARB.ElementArrayBuffer); 63 | Vbo = new BufferObject(Gl, Vertices, BufferTargetARB.ArrayBuffer); 64 | Vao = new VertexArrayObject(Gl, Vbo, Ebo); 65 | 66 | Vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 5, 0); 67 | Vao.VertexAttributePointer(1, 2, VertexAttribPointerType.Float, 5, 3); 68 | 69 | Shader = new Shader(Gl, "shader.vert", "shader.frag"); 70 | 71 | //Loading a texture. 72 | Texture = new Texture(Gl, "silk.png"); 73 | } 74 | 75 | private static unsafe void OnRender(double obj) 76 | { 77 | Gl.Clear((uint) ClearBufferMask.ColorBufferBit); 78 | 79 | Vao.Bind(); 80 | Shader.Use(); 81 | //Bind a texture and and set the uTexture0 to use texture0. 82 | Texture.Bind(TextureUnit.Texture0); 83 | Shader.SetUniform("uTexture0", 0); 84 | 85 | Gl.DrawElements(PrimitiveType.Triangles, (uint) Indices.Length, DrawElementsType.UnsignedInt, null); 86 | } 87 | 88 | private static void OnClose() 89 | { 90 | Vbo.Dispose(); 91 | Ebo.Dispose(); 92 | Vao.Dispose(); 93 | Shader.Dispose(); 94 | //Remember to dispose the texture. 95 | Texture.Dispose(); 96 | } 97 | 98 | private static void KeyDown(IKeyboard arg1, Key arg2, int arg3) 99 | { 100 | if (arg2 == Key.Escape) 101 | { 102 | window.Close(); 103 | } 104 | } 105 | }*/ 106 | } 107 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/GlErrorException.cs: -------------------------------------------------------------------------------- 1 | using Silk.NET.OpenGL; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace AvaloniaSilkExample.Gl { 9 | public class GlErrorException : Exception { 10 | public GlErrorException(string message) : base (message){ } 11 | 12 | public static void ThrowIfError(GL Gl) { 13 | GLEnum error = Gl.GetError(); 14 | if (error != GLEnum.NoError) { 15 | throw new GlErrorException(error.ToString()); 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/Shader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using Silk.NET.OpenGL; 4 | 5 | namespace Tutorial 6 | { 7 | public class Shader : IDisposable 8 | { 9 | private uint _handle; 10 | private GL _gl; 11 | 12 | public Shader(GL gl, string vertexPath, string fragmentPath) 13 | { 14 | _gl = gl; 15 | 16 | uint vertex = LoadShader(ShaderType.VertexShader, vertexPath); 17 | uint fragment = LoadShader(ShaderType.FragmentShader, fragmentPath); 18 | _handle = _gl.CreateProgram(); 19 | _gl.AttachShader(_handle, vertex); 20 | _gl.AttachShader(_handle, fragment); 21 | _gl.LinkProgram(_handle); 22 | _gl.GetProgram(_handle, GLEnum.LinkStatus, out var status); 23 | if (status == 0) 24 | { 25 | throw new Exception($"Program failed to link with error: {_gl.GetProgramInfoLog(_handle)}"); 26 | } 27 | _gl.DetachShader(_handle, vertex); 28 | _gl.DetachShader(_handle, fragment); 29 | _gl.DeleteShader(vertex); 30 | _gl.DeleteShader(fragment); 31 | } 32 | 33 | public void Use() 34 | { 35 | _gl.UseProgram(_handle); 36 | } 37 | 38 | public void SetUniform(string name, int value) 39 | { 40 | int location = _gl.GetUniformLocation(_handle, name); 41 | if (location == -1) 42 | { 43 | throw new Exception($"{name} uniform not found on shader."); 44 | } 45 | _gl.Uniform1(location, value); 46 | } 47 | 48 | public void SetUniform(string name, float value) 49 | { 50 | int location = _gl.GetUniformLocation(_handle, name); 51 | if (location == -1) 52 | { 53 | throw new Exception($"{name} uniform not found on shader."); 54 | } 55 | _gl.Uniform1(location, value); 56 | } 57 | 58 | public void Dispose() 59 | { 60 | _gl.DeleteProgram(_handle); 61 | } 62 | 63 | private uint LoadShader(ShaderType type, string path) 64 | { 65 | string src = File.ReadAllText(path); 66 | uint handle = _gl.CreateShader(type); 67 | _gl.ShaderSource(handle, src); 68 | _gl.CompileShader(handle); 69 | string infoLog = _gl.GetShaderInfoLog(handle); 70 | if (!string.IsNullOrWhiteSpace(infoLog)) 71 | { 72 | throw new Exception($"Error compiling shader of type {type}, failed with error {infoLog}"); 73 | } 74 | 75 | return handle; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/SilkNetExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using Avalonia.OpenGL; 4 | using Avalonia.OpenGL.Controls; 5 | using Avalonia.Threading; 6 | using Silk.NET.OpenGL; 7 | 8 | namespace Tutorial 9 | { 10 | public class SilkNetExample : OpenGlControlBase 11 | { 12 | private GL Gl; 13 | private BufferObject Vbo; 14 | private BufferObject Ebo; 15 | private VertexArrayObject Vao; 16 | private Shader Shader; 17 | 18 | private static readonly float[] Vertices = 19 | { 20 | //X Y Z R G B A 21 | 0.5f, 0.5f, 0.0f, 1, 0, 0, 1, 22 | 0.5f, -0.5f, 0.0f, 0, 0, 0, 1, 23 | -0.5f, -0.5f, 0.0f, 0, 0, 1, 1, 24 | -0.5f, 0.5f, 0.5f, 0, 0, 0, 1 25 | }; 26 | 27 | private static readonly uint[] Indices = 28 | { 29 | 0, 1, 3, 30 | 1, 2, 3 31 | }; 32 | 33 | 34 | 35 | protected override void OnOpenGlInit(GlInterface gl, int fb) 36 | { 37 | base.OnOpenGlInit(gl, fb); 38 | 39 | Gl = GL.GetApi(gl.GetProcAddress); 40 | 41 | 42 | //Instantiating our new abstractions 43 | Ebo = new BufferObject(Gl, Indices, BufferTargetARB.ElementArrayBuffer); 44 | Vbo = new BufferObject(Gl, Vertices, BufferTargetARB.ArrayBuffer); 45 | Vao = new VertexArrayObject(Gl, Vbo, Ebo); 46 | 47 | //Telling the VAO object how to lay out the attribute pointers 48 | Vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 7, 0); 49 | Vao.VertexAttributePointer(1, 4, VertexAttribPointerType.Float, 7, 3); 50 | 51 | Shader = new Shader(Gl, "shader.vert", "shader.frag"); 52 | 53 | } 54 | 55 | 56 | protected override void OnOpenGlDeinit(GlInterface gl, int fb) 57 | { 58 | Vbo.Dispose(); 59 | Ebo.Dispose(); 60 | Vao.Dispose(); 61 | Shader.Dispose(); 62 | base.OnOpenGlDeinit(gl, fb); 63 | } 64 | 65 | protected override unsafe void OnOpenGlRender(GlInterface gl, int fb) 66 | { 67 | Gl.ClearColor(Color.Firebrick); 68 | Gl.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit)); 69 | Gl.Enable(EnableCap.DepthTest); 70 | Gl.Viewport(0,0, (uint)Bounds.Width, (uint)Bounds.Height); 71 | 72 | Ebo.Bind(); 73 | Vbo.Bind(); 74 | Vao.Bind(); 75 | Shader.Use(); 76 | Shader.SetUniform("uBlue", (float) Math.Sin(DateTime.Now.Millisecond / 1000f * Math.PI)); 77 | 78 | Gl.DrawElements(PrimitiveType.Triangles, (uint) Indices.Length, DrawElementsType.UnsignedInt, null); 79 | Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/Texture.cs: -------------------------------------------------------------------------------- 1 | using Silk.NET.OpenGL; 2 | using System; 3 | using SixLabors.ImageSharp; 4 | using SixLabors.ImageSharp.PixelFormats; 5 | 6 | namespace Tutorial 7 | { 8 | public class Texture : IDisposable 9 | { 10 | private uint _handle; 11 | private GL _gl; 12 | 13 | public unsafe Texture(GL gl, string path) 14 | { 15 | _gl = gl; 16 | 17 | _handle = _gl.GenTexture(); 18 | Bind(); 19 | 20 | //Loading an image using imagesharp. 21 | using (var img = Image.Load(path)) 22 | { 23 | //Reserve enough memory from the gpu for the whole image 24 | gl.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba8, (uint) img.Width, (uint) img.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, null); 25 | 26 | img.ProcessPixelRows(accessor => 27 | { 28 | //ImageSharp 2 does not store images in contiguous memory by default, so we must send the image row by row 29 | for (int y = 0; y < accessor.Height; y++) 30 | { 31 | fixed (void* data = accessor.GetRowSpan(y)) 32 | { 33 | //Loading the actual image. 34 | gl.TexSubImage2D(TextureTarget.Texture2D, 0, 0, y, (uint) accessor.Width, 1, PixelFormat.Rgba, PixelType.UnsignedByte, data); 35 | } 36 | } 37 | }); 38 | } 39 | 40 | SetParameters(); 41 | 42 | } 43 | 44 | public unsafe Texture(GL gl, Span data, uint width, uint height) 45 | { 46 | //Saving the gl instance. 47 | _gl = gl; 48 | 49 | //Generating the opengl handle; 50 | _handle = _gl.GenTexture(); 51 | Bind(); 52 | 53 | //We want the ability to create a texture using data generated from code aswell. 54 | fixed (void* d = &data[0]) 55 | { 56 | //Setting the data of a texture. 57 | _gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, d); 58 | SetParameters(); 59 | } 60 | } 61 | 62 | private void SetParameters() 63 | { 64 | //Setting some texture perameters so the texture behaves as expected. 65 | _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) GLEnum.ClampToEdge); 66 | _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) GLEnum.ClampToEdge); 67 | _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) GLEnum.LinearMipmapLinear); 68 | _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) GLEnum.Linear); 69 | _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); 70 | _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 8); 71 | //Generating mipmaps. 72 | _gl.GenerateMipmap(TextureTarget.Texture2D); 73 | } 74 | 75 | public void Bind(TextureUnit textureSlot = TextureUnit.Texture0) 76 | { 77 | //When we bind a texture we can choose which textureslot we can bind it to. 78 | _gl.ActiveTexture(textureSlot); 79 | _gl.BindTexture(TextureTarget.Texture2D, _handle); 80 | } 81 | 82 | public void Dispose() 83 | { 84 | //In order to dispose we need to delete the opengl handle for the texure. 85 | _gl.DeleteTexture(_handle); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/Gl/VertexArrayObject.cs: -------------------------------------------------------------------------------- 1 | using Silk.NET.OpenGL; 2 | using System; 3 | 4 | namespace Tutorial 5 | { 6 | public class VertexArrayObject : IDisposable 7 | where TVertexType : unmanaged 8 | where TIndexType : unmanaged 9 | { 10 | private uint _handle; 11 | private GL _gl; 12 | 13 | public VertexArrayObject(GL gl, BufferObject vbo, BufferObject ebo) 14 | { 15 | _gl = gl; 16 | 17 | _handle = _gl.GenVertexArray(); 18 | Bind(); 19 | vbo.Bind(); 20 | ebo.Bind(); 21 | } 22 | 23 | public unsafe void VertexAttributePointer(uint index, int count, VertexAttribPointerType type, uint vertexSize, int offSet) 24 | { 25 | _gl.VertexAttribPointer(index, count, type, false, vertexSize * (uint) sizeof(TVertexType), (void*) (offSet * sizeof(TVertexType))); 26 | _gl.EnableVertexAttribArray(index); 27 | } 28 | 29 | public void Bind() 30 | { 31 | _gl.BindVertexArray(_handle); 32 | } 33 | 34 | public void Dispose() 35 | { 36 | _gl.DeleteVertexArray(_handle); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/MainWindow.axaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/MainWindow.axaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia; 2 | using Avalonia.Controls; 3 | using Avalonia.Markup.Xaml; 4 | 5 | namespace AvaloniaSilkExample 6 | { 7 | public partial class MainWindow : Window 8 | { 9 | public MainWindow() 10 | { 11 | InitializeComponent(); 12 | #if DEBUG 13 | this.AttachDevTools(); 14 | #endif 15 | } 16 | 17 | private void InitializeComponent() 18 | { 19 | AvaloniaXamlLoader.Load(this); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /AvaloniaSilkExample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Avalonia; 3 | using Avalonia.Controls; 4 | using Avalonia.Controls.ApplicationLifetimes; 5 | 6 | namespace AvaloniaSilkExample 7 | { 8 | class Program 9 | { 10 | // Initialization code. Don't use any Avalonia, third-party APIs or any 11 | // SynchronizationContext-reliant code before AppMain is called: things aren't initialized 12 | // yet and stuff might break. 13 | [STAThread] 14 | public static void Main(string[] args) => BuildAvaloniaApp() 15 | .StartWithClassicDesktopLifetime(args); 16 | 17 | // Avalonia configuration, don't remove; also used by visual designer. 18 | public static AppBuilder BuildAvaloniaApp() 19 | => AppBuilder.Configure() 20 | .UsePlatformDetect() 21 | .With(new Win32PlatformOptions() 22 | { 23 | UseWgl = true, 24 | }) 25 | .LogToTrace(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /AvaloniaSilkExample/shader.frag: -------------------------------------------------------------------------------- 1 | //Specifying the version like in our vertex shader. 2 | #version 330 core 3 | //The input variables, again prefixed with an f as they are the input variables of our fragment shader. 4 | //These have to share name for now even though there is a way around this later on. 5 | in vec4 fColor; 6 | 7 | //The output of our fragment shader, this just has to be a vec3 or a vec4, containing the color information about 8 | //each "fragment" or pixel of our geometry. 9 | out vec4 FragColor; 10 | 11 | void main() 12 | { 13 | //Here we are setting our output variable, for which the name is not important. 14 | FragColor = fColor; 15 | } -------------------------------------------------------------------------------- /AvaloniaSilkExample/shader.vert: -------------------------------------------------------------------------------- 1 | //Here we specify the version of our shader. 2 | #version 330 core 3 | //These lines specify the location and type of our attributes, 4 | //the attributes here are prefixed with a "v" as they are our inputs to the vertex shader 5 | //this isn't strictly necessary though, but a good habit. 6 | layout (location = 0) in vec3 vPos; 7 | layout (location = 1) in vec4 vColor; 8 | 9 | //This is how we declare a uniform, they can be used in all our shaders and share the same name. 10 | //This is prefixed with a u as it's our uniform. 11 | uniform float uBlue; 12 | 13 | //This is our output variable, notice that this is prefixed with an f as it's the input of our fragment shader. 14 | out vec4 fColor; 15 | 16 | void main() 17 | { 18 | //gl_Position, is a built-in variable on all vertex shaders that will specify the position of our vertex. 19 | gl_Position = vec4(vPos, 1.0); 20 | //The rest of this code looks like plain old c (almost c#) 21 | vec4 color = vec4(vColor.rb / 2, uBlue, vColor.a); //Swizzling and constructors in glsl. 22 | fColor = color; 23 | } -------------------------------------------------------------------------------- /nuget.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | --------------------------------------------------------------------------------