Commit Graph

45835 Commits

Author SHA1 Message Date
Ignacio Roldán Etcheverry f033764ffe C#: Refactor Array & Dictionary interface implementations 2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry e235cef09f C#: Re-implement assembly reloading with ALCs 2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry d78e0a8426 C#: Make GodotSharp API a NuGet package
In the past, the Godot editor distributed the API assemblies and
copied them to project directories for projects to reference them.
This changed with the move to .NET 5/6. Godot no longer copies the
assemblies to project directories. However, the project Sdk still
tried to reference them from the same location.
From now on, the GodotSharp API is distributed as a NuGet package,
which the Sdk can reference.

Added an option to `build_assemblies.py` to copy all Godot NuGet
packages to an existing local NuGet source. This will be needed
during development, while packages are not published to a remote
NuGet repository.
This option also makes sure to remove packages of the same version
installed (~/.nuget/packages). Very useful during development, when
packages change, to make sure the package being used by a project is
the same we just built and not one from a previous build.

A local NuGet source can be created like this:

```
mkdir ~/MyLocalNuGetSource && \
dotnet nuget add source ~/MyLocalNuGetSource/ -n MyLocalNuGetSource
```
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 4b90d16250 C#: Initial NativeAOT support
This commit adds initial support for games exported as NativeAOT shared
libraries.

At this moment, the NativeAOT runtime is experimental. Additionally,
Godot is not trim-safe as it still makes some use of reflection.
For the time being, a rd.xml file is needed to prevent code triming:

```
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="GodotSharp" Dynamic="Required All" />
    <Assembly Name="GAME_ASSEMBLY" Dynamic="Required All" />
  </Application>
</Directives>
```

These are the csproj changes for publishing:

```
  <PropertyGroup>
    <NativeLib>Shared</NativeLib>
  </PropertyGroup>
  <ItemGroup>
    <RdXmlFile Include="rd.xml" />
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="7.0.0-*" />
  </ItemGroup>
```

More info:
- https://github.com/dotnet/runtimelab/blob/feature/NativeAOT/docs/using-nativeaot/compiling.md
- https://github.com/dotnet/runtimelab/tree/feature/NativeAOT/samples/NativeLibrary
- https://github.com/dotnet/runtimelab/blob/feature/NativeAOT/docs/using-nativeaot/rd-xml-format.md
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 18f805b3aa C#: Upgrade to .NET 6 (5.0 -> 6.0) 2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry e22dd3bc6a C#: Static marshaling for bindings and source generators
Previously, we added source generators for invoking/accessing methods,
properties and fields in scripts. This freed us from the overhead of
reflection. However, the generated code still used our dynamic
marshaling functions, which do runtime type checking and box value
types.

This commit changes the bindings and source generators to include
'static' marshaling. Based on the types known at compile time, now
we generate the appropriate marshaling call for each type.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 778007a358 C#: Re-introduce exception logging and error stack traces in editor
These two had been disabled while moving to .NET 5, as the previous
implementation relied on Mono embedding APIs.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 67db89988d C#: Ensure we only create one CSharpScript per type
Previously, for each scripts class instance that was created from code
rather than by the engine, we were constructing, configuring and
assigning a new CSharpScript.
This has changed now and we make sure there's only one CSharpScript
associated to each type.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 92503ae8db C#: Add source generator for properties and exports default values
The editor no longer needs to create temporary instances to get the
default values. The initializer values of the exported properties are
still evaluated at runtime. For example, in the following example,
`GetInitialValue()` will be called when first looks for default values:

```
[Export] int MyValue = GetInitialValue();
```

Exporting fields with a non-supported type now results in a compiler
error rather than a runtime error when the script is used.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 88e367a406 C#/netcore: Add base desktop game export implementation
This base implementation is still very barebones but it defines the path
for how exporting will work (at least when embedding the .NET runtime).

Many manual steps are still needed, which should be automatized in the
future. For example, in addition to the API assemblies, now you also
need to copy the GodotPlugins assembly to each game project.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry f88d8902cf C#: Ensure native handles are freed after switch to .NET Core
Finalizers are longer guaranteed to be called on exit now that
we switched to .NET Core. This results in native instances leaking.

The only solution I can think of so far is to keep a list of all
instances alive to dispose when the AssemblyLoadContext.Unloading
event is raised.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 4d710bf659 C#: Add initial implementation of source generator for script members
This replaces the way we invoke methods and set/get properties.
This first iteration rids us of runtime type checking in those
cases, as it's now done at compile time.
Later it will also stop needing the use of reflection. After that,
we will only depend on reflection for generic Godot Array and
Dictionary. We're stuck with reflection in generic collections
for now as C# doesn't support generic/template specialization.

This is only the initial implementation. Further iterations are
coming, specially once we switch to the native extension system
which completely changes the way members are accessed/invoked.
For example, with the native extension system we will likely need
to create `UnmanagedCallersOnly` invoke wrapper methods and return
function pointers to the engine.

Other kind of members, like event signals will be receiving the
same treatment in the future.
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry e5e7a795b1 C#: Code cleanup and greatly reduce use of C# pointers 2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 34db8d2c6c CI: Setup .NET Sdk to fix CI and build C# code as well 2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry f16c8e3759 Fix detection of .NET Sdk from snap and allow higher versions 2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry 315636e77c C#: Fallback to `dotnet --info` to determine .NET RID
Some Linux distros use their distro name as the RID for directory names.
If the .NET Host directory cannot be found with the generic RID,
try to get the rid from `dotnet --info`.

The generic RID should still be the first choice. Some platforms like
Windows 10 define the RID as `win10-x64` but still use the generic
`win-x64` for directory names.

Co-authored-by: Lewis James <lewiji+github@gmail.com>
2022-08-22 03:36:51 +02:00
Ignacio Roldán Etcheverry f9a67ee9da C#: Begin move to .NET Core
We're targeting .NET 5 for now to make development easier while
.NET 6 is not yet released.

TEMPORARY REGRESSIONS
---------------------

Assembly unloading is not implemented yet. As such, many Godot
resources are leaked at exit. This will be re-implemented later
together with assembly hot-reloading.
2022-08-22 03:35:59 +02:00
Ignacio Roldán Etcheverry 513ee857a9 C#: Restructure code prior move to .NET Core
The main focus here was to remove the majority of code that relied on
Mono's embedding APIs, specially the reflection APIs. The embedding
APIs we still use are the bare minimum we need for things to work.
A lot of code was moved to C#. We no longer deal with any managed
objects (`MonoObject*`, and such) in native code, and all marshaling
is done in C#.

The reason for restructuring the code and move away from embedding APIs
is that once we move to .NET Core, we will be limited by the much more
minimal .NET hosting.

PERFORMANCE REGRESSIONS
-----------------------

Some parts of the code were written with little to no concern about
performance. This includes code that calls into script methods and
accesses script fields, properties and events.
The reason for this is that all of that will be moved to source
generators, so any work prior to that would be a waste of time.

DISABLED FEATURES
-----------------

Some code was removed as it no longer makes sense (or won't make sense
in the future).
Other parts were commented out with `#if 0`s and TODO warnings because
it doesn't make much sense to work on them yet as those parts will
change heavily when we switch to .NET Core but also when we start
introducing source generators.
As such, the following features were disabled temporarily:
- Assembly-reloading (will be done with ALCs in .NET Core).
- Properties/fields exports and script method listing (will be
  handled by source generators in the future).
- Exception logging in the editor and stack info for errors.
- Exporting games.
- Building of C# projects. We no longer copy the Godot API assemblies
  to the project directory, so MSBuild won't be able to find them. The
  idea is to turn them into NuGet packages in the future, which could
  also be obtained from local NuGet sources during development.
2022-08-22 03:35:59 +02:00
Ignacio Roldán Etcheverry 5e37d073bb C#: Re-write GD and some other icalls as P/Invoke 2022-08-22 03:35:59 +02:00
Ignacio Roldán Etcheverry 9a51430441 C#: Re-write Array, Dictionary, NodePath, String icalls as P/Invoke 2022-08-22 03:35:59 +02:00
Ignacio Roldán Etcheverry c4ccabd3fb C#: Remove DynamicGodotObject/Object.DynamicObject
We are moving in the direction of no dynamic code generation,
so this is no longer desired.

The feature can still be easily implemented by any project that
still want it.
2022-08-22 03:35:59 +02:00
Ignacio Roldán Etcheverry 124fbf95f8 C#: Move marshaling logic and generated glue to C#
We will be progressively moving most code to C#.
The plan is to only use Mono's embedding APIs to set things at launch.
This will make it much easier to later support CoreCLR too which
doesn't have rich embedding APIs.

Additionally the code in C# is more maintainable and makes it easier
to implement new features, e.g.: runtime codegen which we could use to
avoid using reflection for marshaling everytime a field, property or
method is accessed.

SOME NOTES ON INTEROP

We make the same assumptions as GDNative about the size of the Godot
structures we use. We take it a bit further by also assuming the layout
of fields in some cases, which is riskier but let's us squeeze out some
performance by avoiding unnecessary managed to native calls.

Code that deals with native structs is less safe than before as there's
no RAII and copy constructors in C#. It's like using the GDNative C API
directly. One has to take special care to free values they own.
Perhaps we could use roslyn analyzers to check this, but I don't know
any that uses attributes to determine what's owned or borrowed.

As to why we maily use pointers for native structs instead of ref/out:
- AFAIK (and confirmed with a benchmark) ref/out are pinned
  during P/Invoke calls and that has a cost.
- Native struct fields can't be ref/out in the first place.
- A `using` local can't be passed as ref/out, only `in`. Calling a
  method or property on an `in` value makes a silent copy, so we want
  to avoid `in`.

REGARDING THE BUILD SYSTEM

There's no longer a `mono_glue=yes/no` SCons options. We no longer
need to build with `mono_glue=no`, generate the glue and then build
again with `mono_glue=yes`. We build only once and generate the glue
(which is in C# now).
However, SCons no longer builds the C# projects for us. Instead one
must run `build_assemblies.py`, e.g.:
```sh
%godot_src_root%/modules/mono/build_scripts/build_assemblies.py \
        --godot-output-dir=%godot_src_root%/bin \
        --godot-target=release_debug`
```
We could turn this into a custom build target, but I don't know how
to do that with SCons (it's possible with Meson).

OTHER NOTES

Most of the moved code doesn't follow the C# naming convention and
still has the word Mono in the names despite no longer dealing with
Mono's embedding APIs. This is just temporary while transitioning,
to make it easier to understand what was moved where.
2022-08-22 03:35:59 +02:00
Hugo Locurcio 0c5f254956
Merge pull request #64110 from Geometror/fix-light-shadow-off 2022-08-20 17:06:03 +02:00
Fabio Alessandrelli 1e044da15c
Merge pull request #64481 from jamie-pate/master
Improve performance of screen_get_dpi() in Javascript
2022-08-20 13:36:04 +02:00
Yuri Rubinsky 5119d0d974
Merge pull request #64625 from Chaosus/shader_pp_defined 2022-08-20 11:51:58 +03:00
Yuri Rubinsky 499fd0a4e7 Add `defined` keyword support to shader preprocessor 2022-08-20 11:26:59 +03:00
Max Hilbrunner 5e0d2b5097
Merge pull request #62046 from clayjohn/vertexless-draw
Allow creating meshes without vertex positions
2022-08-20 05:32:28 +02:00
Hendrik Brucker 18585c69b3 Fix OmniLight/SpotLight shadow opacity calculation 2022-08-20 01:05:44 +02:00
Max Hilbrunner 47d3fd99a2
Merge pull request #64461 from bitsawer/fix_windows_list_dir
Fix Windows list dir handle leak
2022-08-20 00:58:42 +02:00
Max Hilbrunner 79717dcb46
Merge pull request #64170 from YuriSizov/docs-annotations-in-technicolor
Add documentation for all annotations
2022-08-20 00:57:01 +02:00
clayjohn bbbcdd725a Remove requirement to have vertex positions when creating a mesh. Meshes can now be constructed from an index buffer alone 2022-08-19 14:50:12 -06:00
Yuri Rubinsky 8779f51a5e
Merge pull request #63960 from bitsawer/fix-macro-expansion 2022-08-19 23:37:47 +03:00
Max Hilbrunner fafd15014f
Merge pull request #64510 from KoBeWi/PR_packed_with_docs😎
Complete documentation of packed arrays
2022-08-19 22:05:05 +02:00
Max Hilbrunner 019d6584ee
Merge pull request #64167 from clayjohn/screen-texture-hint
Add shader uniform hints for screen textures
2022-08-19 21:51:39 +02:00
Max Hilbrunner 117c530e1d
Merge pull request #64342 from clayjohn/DOF-scale
Properly scale depth in bokeh_dof effect
2022-08-19 21:16:32 +02:00
Yuri Sizov b35ff86c17
Merge pull request #64612 from bruvzg/fix_shortcuts
Fix MenuBar not processing shortcuts.
2022-08-19 20:43:17 +03:00
Yuri Sizov 469c18152f
Merge pull request #64592 from mhilbrunner/move-fast-break-things
Unexpose VelocityTracker3D
2022-08-19 20:43:02 +03:00
Yuri Sizov 96ba6e8cf9
Merge pull request #64595 from mhilbrunner/ergo-proxy
Unexpose ProxyTexture
2022-08-19 20:42:27 +03:00
Yuri Sizov 7895a66cf2
Merge pull request #64334 from YuriSizov/core-bind-property-revert-methods
Make `property_*_revert` methods multilevel and expose them for scripting
2022-08-19 20:41:41 +03:00
bruvzg 2040087898
Fix MenuBar not processing shortcuts. 2022-08-19 16:49:50 +03:00
Yuri Sizov a0995182fe Add documentation for all annotations 2022-08-19 15:06:39 +03:00
Yuri Rubinsky 35cfaafda8
Merge pull request #64459 from Chaosus/shader_elif 2022-08-19 07:01:22 +03:00
Max Hilbrunner 92bdeb7eb5 Unexpose ProxyTexture 2022-08-19 03:01:47 +02:00
Max Hilbrunner da567cb1f9 Unexpose VelocityTracker3D 2022-08-19 02:28:24 +02:00
Max Hilbrunner 0ba1046355
Merge pull request #64580 from asmaloney/doc-grammar-less
[doc] Fix grammar in class docs: less vs. fewer/lower
2022-08-19 01:51:31 +02:00
Yuri Sizov 0d95b8bff2
Merge pull request #59045 from Calinou/editor-theme-popup-no-rounded-corners
Don't use rounded corners for PopupMenus in the editor theme
2022-08-19 02:42:17 +03:00
Max Hilbrunner 8cc0d07b49
Merge pull request #59851 from Calinou/doc-object-meta-editor-only
Document that Object metadata starting with `_` is editor-only
2022-08-19 01:41:25 +02:00
Max Hilbrunner 6f273d2156
Merge pull request #63326 from MewPurPur/binary-highlighting-fix
Fix highlighting of multiple operators
2022-08-19 01:27:02 +02:00
Max Hilbrunner 420a8c888e
Merge pull request #63950 from bruvzg/menu_bar3
Implement MenuBar control to wrap Popup menus or native menu, use native menu for editor.
2022-08-19 01:24:24 +02:00
kobewi b099a8570c Complete documentation of packed arrays 2022-08-19 00:43:29 +02:00