DanM.GlslOptimizer 1.1.0

dotnet add package DanM.GlslOptimizer --version 1.1.0
                    
NuGet\Install-Package DanM.GlslOptimizer -Version 1.1.0
                    
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="DanM.GlslOptimizer" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DanM.GlslOptimizer" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DanM.GlslOptimizer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DanM.GlslOptimizer --version 1.1.0
                    
#r "nuget: DanM.GlslOptimizer, 1.1.0"
                    
#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.
#:package DanM.GlslOptimizer@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DanM.GlslOptimizer&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DanM.GlslOptimizer&version=1.1.0
                    
Install as a Cake Tool

DanM.GlslOptimizer

NuGet Downloads

.NET bindings for glsl-optimizer, the GLSL optimizing compiler originally from Mesa and maintained by Unity. Native binaries for Windows, Linux and macOS ship inside the package, so there is nothing to build or install separately.

Install

dotnet add package DanM.GlslOptimizer

Usage

Wrappers are exposed in static class GlslShader. These wrappers throw GlslOptimizerException (with glsl-optimizer error details) in case of failure:

using DanM.GlslOptimizer;

_ = GlslShader.Optimize(GlslTarget.OpenGL, GlslShaderType.Vertex, shaderSource, GlslOptimizeOptions.None);
_ = GlslShader.OptimizeOpenGL(GlslShaderType.Fragment, shaderSource, GlslOptimizeOptions.SkipPreprocessor);
_ = GlslShader.OptimizeOpenGLFragment(shaderSource, GlslOptimizeOptions.NotFullShader);
_ = GlslShader.OptimizeOpenGLVertex(shaderSource);

// More wrappers for GLES20, GLES30, Metal.

Or, more explicit:

using DanM.GlslOptimizer;

using var ctx = new GlslOptimizerContext(GlslTarget.OpenGL);
using var shader = ctx.Optimize(
    GlslShaderType.Fragment,
    "void main() { float x = 1.0 + 2.0; gl_FragColor = vec4(x, 0.0, 0.0, 1.0); }");

if (shader.Status) // success?
    Console.WriteLine(shader.Output); // optimized output
else
    Console.WriteLine(shader.Log); // error details

Output:

void main ()
{
  vec4 tmpvar_1;
  tmpvar_1.yzw = vec3(0.0, 0.0, 1.0);
  tmpvar_1.x = 3.0;
  gl_FragColor = tmpvar_1;
}

GlslOptimizerContext and OptimizedShader both own native handles, so dispose them (using) rather than leaving them to the finalizer.

How it works

aras-p/glsl-optimizer was taken and its CMakeLists.txt stripped to only build the static library. glsl_optimizer is linked to glslopt_dll, the dynamic library shipped with this project. The .NET library P/Invokes into that.

The dynamic library's code is in src/. The stripped CMakeLists.txt is in the root of the repo.

Supported platforms

RID Native library
win-x64 glslopt_dll.dll
linux-x64 libglslopt_dll.so
linux-arm64 libglslopt_dll.so
osx-x64 libglslopt_dll.dylib
osx-arm64 libglslopt_dll.dylib

As of now, I compiled these binaries manually in Release for all platforms on Windows, Ubuntu, and a Mac. There are no automated build scripts, contributions welcome.

There is currently no win-arm64 or linux-musl-x64/arm64 (Alpine) binary. If you make an issue specifically for those I might find time to compile them. I will not merge PRs containing binaries. If you want to contribute to the binaries, make an automated build step for them.

You are of course free to clone and build them yourself for your own use. Use the CMakeLists.txt in this repository or the one from glsl-optimizer and then link glsl_optimizer to a shared library that compiles src/interface.cpp to create the dynamic lib. See Native.cs for calling the DLL from managed code.

AI disclosure

AI (Opus 5) was used write the NuGet publishing pipeline (GitHub Actions and some .csproj editing), parts of this README, and the interface.h DLL_EXPORT define (the boring stuff).

License

MIT. The bundled native binaries are built from glsl-optimizer, which is also MIT licensed (Brian Paul; Unity Technologies) — see LICENSE.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.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
1.1.0 58 9/20/2026
1.0.0 54 9/20/2026