nano-ui-shader: Typed fragment shader EDSL and multi-target compiler for nano-ui

[ graphics, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

A pure-Haskell typed fragment shader EDSL for nano-ui nodes. Author surface fills, color effects, distortion warps, and multi-pass layer effects once using OverloadedRecordDot swizzles and Generic std140 uniforms, fuse color/distortion passes algebraically, and compile directly to GLSL 150, GLSL ES 300, SPIR-V, MSL, and a CPU reference evaluator.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.2.0.0
Change log CHANGELOG.md
Dependencies base (>=4.20 && <4.23), bytestring (>=0.12 && <0.13), containers (>=0.7 && <0.9), hashable (>=1.5 && <1.6), nano-ui (>=0.2 && <0.3), primitive (>=0.9 && <0.10), text (>=2.1 && <2.2), unordered-containers (>=0.2 && <0.3), vector (>=0.13 && <0.14) [details]
Tested with ghc ==9.10.3 || ==9.14.1
License MIT
Author goolord
Maintainer zacharyachurchill@gmail.com
Uploaded by goolord at 2026-12-03T05:00:35Z
Category Graphics
Home page https://github.com/goolord/nano-ui
Bug tracker https://github.com/goolord/nano-ui/issues
Source repo head: git clone https://github.com/goolord/nano-ui.git(packages/nano-ui-shader)
Distributions
Downloads 0 total (0 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for nano-ui-shader-0.2.0.0

[back to package description]

nano-ui-shader

Typed fragment shader EDSL and multi-target compiler for nano-ui. Write surface fills, color effects, distortion warps, and multi-pass layer effects once in Haskell, fuse compatible passes algebraically, and compile directly to GLSL 150 (OpenGL 3.2), GLSL ES 300 (WebGL2), SPIR-V (Vulkan / SDL3 GPU), MSL (Metal), and a CPU reference evaluator.

{-# LANGUAGE DataKinds, DeriveAnyClass, DeriveGeneric, OverloadedRecordDot #-}

import GHC.Generics (Generic)
import NanoUI
import NanoUI.Shader

data PulseParams = PulseParams
  { ppColor :: !Color
  , ppPhase :: !Float
  , ppRings :: !Float
  }
  deriving (Eq, Generic, Uniforms)

pulseRing :: Shader 'Surface PulseParams
pulseRing = surfaceShader $ \u ctx ->
  let p = ctx.uv *^ 2.0 - vec2 1.0 1.0
      wave = 0.5 + 0.5 * sin (vLength p * u.ppRings - u.ppPhase)
      half = ctx.size *^ 0.5
      edge = sdRoundedBox (ctx.pos - half) half ctx.cornerRadius
      aa = 0.75 / maxE ctx.scale 1.0
      mask = 1.0 - smoothstep (negate aa) aa edge
   in vec4_31 (u.ppColor.rgb *^ wave) (u.ppColor.a * mask)

Define shaders at top level so compilation happens once; uniform updates pack into std140 bytes and drive damage diffing without recompiling GPU programs. Render leaf fills with shaderBox or shaderConfigured (zero offscreen targets), and wrap interactive subtrees with withShader or withShader'.

Two tiers and pass fusion

Surface fills ('Surface) draw directly into the main render target (meshGradient4, radialGlow, sdfRoundedBox, colorWheelHsv, noiseGrain, checkerboard). Subtree effects ('ColorEffect, 'DistortionEffect, 'LayerEffect) post-process children over pooled offscreen targets while keeping hit-testing, focus, and scrolling intact (groupOpacity, grayscale, tint, brightnessContrast, colorMatrix, vignette, scanlines, waveDistort, rippleDistort, pixelate, boxBlur, gaussianBlur, dropShadow, outerGlow, chromaticAberration).

Compatible subtree passes fuse into a single GPU program with composeColor, composeDistort, and distortThenColor. Multi-pass chains combine with chainLayer.

Authoring and fallback

Enable DataKinds and OverloadedRecordDot to project uniform fields (u.ppColor.rgb) and vector swizzles (ctx.uv.yx). Scalar-vector operators are (*^), (^*), (/^), (+^), and (-^); branchless selection is selectE. Use surfaceShaderM, colorEffectM, distortionEffectM, or layerEffectM when a shader needs bind sharing or bounded forRange loops. For hand-written backend shader sources without this EDSL, import NanoUI.Shader.Raw from nano-ui.

Query live GPU support inside a view with askShaderCapabilities. When GPU shaders are unavailable (such as under SDL's software renderer), shaderConfigured paints scFallback and withShader renders its children without the effect.

Documentation