Build with XuiView Markdown

Write shaders in C#

A shader is a small program that runs for vertices or fragments. Xui lets you describe those stages with C# structs, then translates their supported operations into backend shader languages at build time.

Start with the complete triangle source. Keep the shader in a named namespace, as the example does; the current generator expects one. TriangleShader is partial because the analyzer adds generated helpers. [ShaderProgram] identifies the program; [VertexShader] and [FragmentShader] identify the nested stages.

Connect the stages

The vertex stage implements IVertexShader<Vertex, Varyings, Bindings>. It receives one vertex and shared bindings, then returns a clip-space position and the data the fragment stage needs. Mark the position with [BuiltIn(BuiltIn.Position)] and the interpolated color with [Location(0)].

The fragment stage implements IFragmentShader<Varyings, FragmentOutput, Bindings>. It receives interpolated varyings and returns the output color. The triangle has no lighting or texture lookup: its purpose is to make the data flow visible.

The generated GenerateShaderSource(backend) selects HLSL, MSL, or GLSL. Pass the corresponding generated entry-point name when compiling each stage. The browser requires GLSL's main; do not hardcode a Windows entry point into shared code.

Write in the supported subset

Shader C# is not arbitrary application C#. Use the shader types such as F32, Float3, Float4x4, and Color4, plus the supported shader operations. Do not allocate objects, access UI services, perform I/O, or use ordinary collection code inside Execute.

Build after each small shader change. A successful C# parse is not enough: translation and the platform shader compiler must also accept the program. Inspect generator diagnostics before debugging a blank surface. An operation accepted by one emitter may still be unsupported by another, so run on each backend you ship.

Keep CPU and GPU responsibilities separate

Prepare geometry and uniforms on the CPU. Resolve services and own resources in the view. Keep Execute focused on transforming that data. Use ordinary C# tests for matrix and data preparation, then a rendered-pixel check for the final backend result.

Change one triangle corner color and rebuild. The smooth color change across the interior comes from interpolation of the varying, not a C# loop over pixels. Next, introduce mesh and uniform data.