{"id":15051927,"url":"https://github.com/thoughtstuff/thoughtstuff.glsourcegen","last_synced_at":"2026-01-28T00:04:36.191Z","repository":{"id":257106003,"uuid":"855202080","full_name":"ThoughtStuff/ThoughtStuff.GLSourceGen","owner":"ThoughtStuff","description":"Generates GL calls to map vertex data to VBO and shader attributes.","archived":false,"fork":false,"pushed_at":"2024-10-28T18:10:33.000Z","size":186,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-01T08:22:58.066Z","etag":null,"topics":["code-generation","glsl","opengl","source-generation","webgl"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/ThoughtStuff.GLSourceGen/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ThoughtStuff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-10T13:42:23.000Z","updated_at":"2024-10-28T18:10:37.000Z","dependencies_parsed_at":"2024-09-14T22:08:53.416Z","dependency_job_id":"24b27e42-dfd8-4aaa-9388-5280f1aaad3a","html_url":"https://github.com/ThoughtStuff/ThoughtStuff.GLSourceGen","commit_stats":{"total_commits":57,"total_committers":1,"mean_commits":57.0,"dds":0.0,"last_synced_commit":"c59c97ae930187d0da22252fe73803c657395af3"},"previous_names":["thoughtstuff/thoughtstuff.glsourcegen"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/ThoughtStuff/ThoughtStuff.GLSourceGen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.GLSourceGen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.GLSourceGen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.GLSourceGen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.GLSourceGen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ThoughtStuff","download_url":"https://codeload.github.com/ThoughtStuff/ThoughtStuff.GLSourceGen/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.GLSourceGen/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28828177,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T23:29:49.665Z","status":"ssl_error","status_checked_at":"2026-01-27T23:25:58.379Z","response_time":168,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generation","glsl","opengl","source-generation","webgl"],"created_at":"2024-09-24T21:37:06.716Z","updated_at":"2026-01-28T00:04:36.164Z","avatar_url":"https://github.com/ThoughtStuff.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ThoughtStuff.GLSourceGen\n\n`ThoughtStuff.GLSourceGen` is a source generator that automates OpenGL/WebGL calls for mapping vertex data structures to shaders.\nSpecifically, it generates calls to:\n\n- `GL.BindBuffer`\n- `GL.BufferData`\n- `GL.GetAttribLocation`\n- `GL.EnableVertexAttribArray`\n- `GL.VertexAttribPointer`\n\n### Benefits of Source Generation\n\n- **Compile-Time Generation**: Code is generated during compilation, without using reflection.\n- **Performance**: No reflection means faster execution and lower memory usage.\n- **AOT Compatibility**: Fully supports Ahead-of-Time (AOT) compilation for platforms like WebAssembly.\n\n## Get Started\n\n- Add Package Reference\n    ```sh\n    dotnet add package ThoughtStuff.GLSourceGen\n    ```\n- Add shaders as `AdditionalFiles` in csproj\n    ```xml\n    \u003cItemGroup\u003e\n        \u003c!-- Include shader files as Additional Files so they can be used by source generation --\u003e\n        \u003cAdditionalFiles Include=\"Shaders\\**\\*.glsl\" /\u003e\n    \u003c/ItemGroup\u003e\n    ```\n\n## Example\n\nGiven a typical vertex structure for 2D colored vertices with `Position` and `Color`:\n\n```csharp\n[StructLayout(LayoutKind.Sequential, Pack = 1)]\nstruct ColorVertex2(Vector2 position, Vector4 color)\n{\n    public Vector2 Position = position;\n    public Vector4 Color = color;\n}\n```\n\nAnd shader with `a_VertexPosition` and `a_VertexColor` input attribute variables.\n(The source generator will parse the shader to identify attribute names that match the vertex structure fields.)\n\n```glsl\nattribute vec4 a_VertexPosition;\nattribute vec4 a_VertexColor;\n\nvarying mediump vec4 v_Color;\n\nvoid main(void) {\n    gl_Position = a_VertexPosition;\n    v_Color = a_VertexColor;\n}\n```\n\nWe can leverage the `SetupVertexAttrib` attribute to generate the necessary OpenGL calls:\n\n```csharp\n// Declare the partial class which will be implemented by the source generator.\n[SetupVertexAttrib(\"Shaders/Basic_vert.glsl\", typeof(ColorVertex2))]\npartial class ColorVertex2ShaderBinding\n{\n    // Generated methods:\n\n    // Call SetVertexData during initialization to set up the vertex buffer and pass the data to the GPU.\n    // internal static void SetVertexData(JSObject shaderProgram,\n    //                                            JSObject vertexBuffer,\n    //                                            Span\u003cColorVertex2\u003e vertices,\n    //                                            List\u003cint\u003e vertexAttributeLocations);\n\n    // Call EnableVertexBuffer during rendering if the vertex buffer needs to be re-enabled,\n    // or if you are switching between multiple vertex buffers.\n    // internal static void EnableVertexBuffer(JSObject vertexBuffer,\n    //                                         List\u003cint\u003e? vertexAttributeLocations = null);\n}\n```\n\nIn the main program, we can now use the generated code to set up the vertex buffer:\n```csharp\npartial class HelloTriangle\n{\n    public void InitializeScene(IShaderLoader shaderLoader)\n    {\n        var shaderProgram = shaderLoader.LoadShaderProgram(\"Shaders/Basic_vert.glsl\", ...);\n\n        // Define the vertices for the triangle. Assume NDC coordinates [-1 ... 1].\n        Span\u003cColorVertex2\u003e vertices =\n        [\n            new ColorVertex2(new(0, 1), new(1, 0, 0, 1)),    // Red vertex\n            new ColorVertex2(new(-1, -1), new(0, 1, 0, 1)),  // Green vertex\n            new ColorVertex2(new(1, -1), new(0, 0, 1, 1))    // Blue vertex\n        ];\n\n        // Create a buffer for the triangle's vertices.\n        var vertexBuffer = GL.CreateBuffer();\n        // Call the generated function which sets up the vertex buffer and passes the data to the GPU.\n        ColorVertex2ShaderBinding.SetBufferData(shaderProgram, vertexBuffer, vertices, vertexAttributeLocations);\n    }\n\n    public void Render()\n    {\n        GL.Clear(GL.COLOR_BUFFER_BIT);\n        // If necessary:\n        // ColorVertex2ShaderBinding.EnableVertexBuffer(vertexBuffer);\n        // GL.UseProgram(shaderProgram);\n        GL.DrawArrays(GL.TRIANGLES, 0, 3);\n    }\n```\n\nThe source generator will generate the following code:\n\n```csharp\n\npartial class ColorVertex2ShaderBinding\n{\n    // Private \"cache\" fields for attribute locations, strides, and offsets\n    private static int _ColorVertex2_Position_location;\n    private static int _ColorVertex2_Position_stride;\n    private static int _ColorVertex2_Position_offset;\n    private static int _ColorVertex2_Color_location;\n    private static int _ColorVertex2_Color_stride;\n    private static int _ColorVertex2_Color_offset;\n    private static bool _ColorVertex2_vertexLayoutInitialized;\n\n    private static void _InitVertexLayoutFields_ColorVertex2(JSObject shaderProgram)\n    {\n        if (_ColorVertex2_vertexLayoutInitialized)\n            return;\n\n        // Cache the attribute locations, strides, and offsets\n        _ColorVertex2_Position_location = GL.GetAttribLocation(shaderProgram, \"a_VertexPosition\");\n        if (_ColorVertex2_Position_location == -1)\n            throw new InvalidOperationException($\"Could not find shader attribute location for a_VertexPosition.\");\n        _ColorVertex2_Position_stride = Marshal.SizeOf\u003cGenShaderBinding.GameApp.Examples.ColorVertex2\u003e();\n        _ColorVertex2_Position_offset = Marshal.OffsetOf\u003cGenShaderBinding.GameApp.Examples.ColorVertex2\u003e(nameof(GenShaderBinding.GameApp.Examples.ColorVertex2.Position)).ToInt32();\n\n        _ColorVertex2_Color_location = GL.GetAttribLocation(shaderProgram, \"a_VertexColor\");\n        if (_ColorVertex2_Color_location == -1)\n            throw new InvalidOperationException($\"Could not find shader attribute location for a_VertexColor.\");\n        _ColorVertex2_Color_stride = Marshal.SizeOf\u003cGenShaderBinding.GameApp.Examples.ColorVertex2\u003e();\n        _ColorVertex2_Color_offset = Marshal.OffsetOf\u003cGenShaderBinding.GameApp.Examples.ColorVertex2\u003e(nameof(GenShaderBinding.GameApp.Examples.ColorVertex2.Color)).ToInt32();\n\n        _ColorVertex2_vertexLayoutInitialized = true;\n    }\n\n    internal static void EnableVertexBuffer(JSObject vertexBuffer,\n                                            List\u003cint\u003e? vertexAttributeLocations = null)\n    {\n        if (!_ColorVertex2_vertexLayoutInitialized)\n            throw new InvalidOperationException(\"Vertex layout fields have not been initialized.\");\n\n        // Bind the vertex buffer\n        GL.BindBuffer(GL.ARRAY_BUFFER, vertexBuffer);\n\n        // Enable the vertex attributes\n        vertexAttributeLocations?.Add(_ColorVertex2_Position_location);\n        GL.VertexAttribPointer(_ColorVertex2_Position_location,\n                            size: 2,\n                            type: GL.FLOAT,\n                            normalized: false,\n                            stride: _ColorVertex2_Position_stride,\n                            offset: _ColorVertex2_Position_offset);\n        GL.EnableVertexAttribArray(_ColorVertex2_Position_location);\n\n        vertexAttributeLocations?.Add(_ColorVertex2_Color_location);\n        GL.VertexAttribPointer(_ColorVertex2_Color_location,\n                            size: 4,\n                            type: GL.FLOAT,\n                            normalized: false,\n                            stride: _ColorVertex2_Color_stride,\n                            offset: _ColorVertex2_Color_offset);\n        GL.EnableVertexAttribArray(_ColorVertex2_Color_location);\n\n    }\n\n    internal static void SetVertexData(JSObject shaderProgram,\n                                       JSObject vertexBuffer,\n                                       Span\u003cGenShaderBinding.GameApp.Examples.ColorVertex2\u003e vertices,\n                                       List\u003cint\u003e vertexAttributeLocations)\n    {\n        // Initialize the vertex layout fields\n        _InitVertexLayoutFields_ColorVertex2(shaderProgram);\n\n        // Enable the vertex buffer and attributes\n        EnableVertexBuffer(vertexBuffer, vertexAttributeLocations);\n\n        // Upload the vertex data to the GPU\n        GL.BufferData(GL.ARRAY_BUFFER, vertices, GL.STATIC_DRAW);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoughtstuff%2Fthoughtstuff.glsourcegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthoughtstuff%2Fthoughtstuff.glsourcegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoughtstuff%2Fthoughtstuff.glsourcegen/lists"}