ThoughtStuff.GLSourceGen
1.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package ThoughtStuff.GLSourceGen --version 1.0.1
NuGet\Install-Package ThoughtStuff.GLSourceGen -Version 1.0.1
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="ThoughtStuff.GLSourceGen" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ThoughtStuff.GLSourceGen --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ThoughtStuff.GLSourceGen, 1.0.1"
#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 ThoughtStuff.GLSourceGen as a Cake Addin #addin nuget:?package=ThoughtStuff.GLSourceGen&version=1.0.1 // Install ThoughtStuff.GLSourceGen as a Cake Tool #tool nuget:?package=ThoughtStuff.GLSourceGen&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ThoughtStuff.GLSourceGen
ThoughtStuff.GLSourceGen
is a source generator that automates OpenGL/WebGL calls for mapping vertex data structures to shaders.
Specifically, it generates calls to:
GL.BindBuffer
GL.BufferData
GL.GetAttribLocation
GL.EnableVertexAttribArray
GL.VertexAttribPointer
Benefits of Source Generation
- Compile-Time Generation: Code is generated during compilation, eliminating without using reflection.
- Performance: No reflection means faster execution and lower memory usage.
- AOT Compatibility: Fully supports Ahead-of-Time (AOT) compilation for platforms like WebAssembly.
Get Started
- Add Package Reference
dotnet add package ThoughtStuff.GLSourceGen
- Add shaders as
AdditionalFiles
in csproj<ItemGroup> <AdditionalFiles Include="Shaders\**\*.glsl" /> </ItemGroup>
Example
Given a typical vertex structure for 2D colored vertices with Position
and Color
:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ColorVertex2(Vector2 position, Vector4 color)
{
public Vector2 Position = position;
public Vector4 Color = color;
}
And shader with a_VertexPosition
and a_VertexColor
input attribute variables:
attribute vec4 a_VertexPosition;
attribute vec4 a_VertexColor;
varying mediump vec4 v_Color;
void main(void) {
gl_Position = a_VertexPosition;
v_Color = a_VertexColor;
}
partial class HelloTriangle
{
// Declare the partial function which will be implemented by the source generator.
[ThoughtStuff.GLSourceGen.SetupVertexAttrib("Shaders/Basic_vert.glsl")]
partial void SetBufferData(JSObject shaderProgram,
JSObject vertexBuffer,
Span<ColorVertex2> vertices,
List<int> vertexAttributeLocations);
public void InitializeScene(IShaderLoader shaderLoader)
{
var shaderProgram = shaderLoader.LoadShaderProgram("Shaders/Basic_vert.glsl", ...);
// Define the vertices for the triangle. Assume NDC coordinates [-1 ... 1].
Span<ColorVertex2> vertices =
[
new ColorVertex2(new(0, 1), new(1, 0, 0, 1)), // Red vertex
new ColorVertex2(new(-1, -1), new(0, 1, 0, 1)), // Green vertex
new ColorVertex2(new(1, -1), new(0, 0, 1, 1)) // Blue vertex
];
// Create a buffer for the triangle's vertex positions.
var positionBuffer = GL.CreateBuffer();
// Call the generated function which sets up the vertex buffer and passes the data to the GPU.
SetBufferData(shaderProgram, positionBuffer, vertices, vertexAttributeLocations);
}
The source generator will generate the following code:
partial class HelloTriangle
{
partial void SetBufferData(JSObject shaderProgram,
JSObject vertexBuffer,
Span<ColorVertex2> vertices,
List<int> vertexAttributeLocations)
{
// Bind the buffer to the ARRAY_BUFFER target.
GL.BindBuffer(GL.ARRAY_BUFFER, vertexBuffer);
// Copy the vertex data to the GPU.
GL.BufferData(GL.ARRAY_BUFFER, vertices, GL.STATIC_DRAW);
// Get the location of the 'a_Position' attribute variable in the shader program.
int positionLocation = GL.GetAttribLocation(shaderProgram, "a_Position");
// Keep track of the attribute locations so they can be disabled or modified (e.g. for instancing)
vertexAttributeLocations.Add(positionLocation);
// Enable the 'a_Position' attribute.
GL.EnableVertexAttribArray(positionLocation);
// Define the layout of the 'a_Position' attribute.
GL.VertexAttribPointer(positionLocation,
size: 2, // Vector2 has 2 floats
type: GL.FLOAT,
normalized: false,
stride: Marshal.SizeOf<ColorVertex2>(),
offset: Marshal.OffsetOf<ColorVertex2>(nameof(ColorVertex2.Position)).ToInt32());
// Get the location of the 'a_Color' attribute in the shader program.
int colorLocation = GL.GetAttribLocation(shaderProgram, "a_Color");
// Keep track of the attribute locations so they can be disabled or modified (e.g. for instancing)
vertexAttributeLocations.Add(colorLocation);
// Enable the 'a_Color' attribute.
GL.EnableVertexAttribArray(colorLocation);
// Define the layout of the 'a_Color' attribute.
GL.VertexAttribPointer(colorLocation,
size: 4, // Vector4 has 4 floats
type: GL.FLOAT,
normalized: false,
stride: Marshal.SizeOf<ColorVertex2>(),
offset: Marshal.OffsetOf<ColorVertex2>(nameof(ColorVertex2.Color)).ToInt32());
}
}
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.