EmbreeSharp 0.6.5

dotnet add package EmbreeSharp --version 0.6.5                
NuGet\Install-Package EmbreeSharp -Version 0.6.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EmbreeSharp" Version="0.6.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EmbreeSharp --version 0.6.5                
#r "nuget: EmbreeSharp, 0.6.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EmbreeSharp as a Cake Addin
#addin nuget:?package=EmbreeSharp&version=0.6.5

// Install EmbreeSharp as a Cake Tool
#tool nuget:?package=EmbreeSharp&version=0.6.5                

EmbreeSharp

NuGet

EmbreeSharp is an unofficial C# low-level binding for embree

Provide a tiny safe wrapper in C#

Target framework is net8.0

This project is still under development. API are not stable.

Use

Now target embree version is v4.3.3

Install this package from nuget.

Install the native library package for your platform from nuget. Or, compile embree by yourself and put native library into runtimes/${rid}/native

OS x64 arm64
win NuGet /
linux NuGet /
osx NuGet NuGet

If you are using version 4.3.0. osx-x64 should first install tbb in system.

Directory structure

  • EmbreeSharp: source code, tiny safe wrapper also in this dir
    • Native: low-level bindings
  • EmbreeSharp.Test: some tests
  • Samples: some simple examples
  • Script: some scripts to package the official native libraries

Details

About native library

The official only provides binary files for win-x64, linux-x64, osx-x64 and osx-arm64. If you want to run on other platforms, you should compile embree by yourself.

Because native libraries uploaded to nuget are separate, everyone can upload their compiled native libraries for others to use.

If you upload your compiled library, you can tell me the package name. I will display it in readme.

The Script folder contains some help scripts to package the official compiled native libraries into the nuget package.

About load native library in C#

Loading native dynamic library of embree is complicated. Because official release depends on TBB. P/Invoke cannot find its path automatically. There is no way to set library search path (as far as I know). So I use System.Runtime.InteropServices.NativeLibrary.SetDllImportResolver to custom loading logic.

Shared Buffer

The memory for shared buffer is managed by user.

Accouding to the API reference: rtcSetSharedGeometryBuffer

The buffer data must remain valid for as long as the buffer may be used, and the user is responsible for freeing the buffer data when no longer required.

This is a lifecycle problem.

Currently, I provide EmbreeSharp.ISharedBufferAllocation as buffer allocator. It ensures that the allocated buffer address is available until it is released. EmbreeSharp.ISharedBufferAllocation as allocate result. It provides the buffer address and length.

The most crucial part is EmbreeSharp.SharedBufferHandle. It inherits from System.Runtime.InteropServices.SafeHandle. The original purpose of SafeHandle was serve as a secure native handle when interop with C API. It has built-in reference counting function. Therefore, here we borrow its reference count function.

SharedBufferHandle is used to manage the lifecycle of ISharedBufferAllocation. For example, pass it to construct EmbreeSharp.EmbreeSharedBuffer. It will increase the reference count. Due to EmbreeSharedBuffer implements the dispose pattern, GC will use finalizer when the user forgets to release it. At the same time decrease the reference count. When the reference count returns to zero, it will free shared buffer

Ideally, it can avoid memory leaks caused by users forgetting to free. But it has not been tested

Limitation

Cannot use all APIs related to SYCL.

TODO

I will provide more safe functions for C# wrapper. But limited by language difference, It is almost impossible to have both performance and safety.

If you have any idea, welcome issues and pull requests

SetGeometry[Intersect/Occluded/Bounds]Function

Just provide safe API

MXCSR control and status register

link

This is x86-64 instruction. C# API does not provide them.

Make a native library and use P/Invoke?

Hmm...maybe we can use OS api. Write binary code to memory and call OS api to set memory as executable such as VirtualProtectEx on win32.

Add more samples

same as title. like path tracer?

License

The copyright of Embree belongs to the Embree development team

This project is under MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.5 152 8/4/2024
0.6.4 106 6/15/2024
0.6.3 115 4/25/2024
0.6.2 122 3/13/2024
0.6.1 127 3/12/2024
0.6.0 110 3/12/2024
0.5.0 127 3/9/2024
0.4.1 125 2/19/2024
0.4.0 225 11/29/2023
0.3.0 1,494 4/11/2023
0.2.0 368 4/8/2023
0.2.0-alpha 326 4/7/2023

add helper func