packages feed

mischief-render-0.1.0.0: src/Mischief/Materials/Fullscreen.hs

module Mischief.Materials.Fullscreen where

import Mischief.Materials
import Prelude ()

data Bindings f = Bindings deriving (Generic, Bindable)

newtype VertexInput f = VertexInput (BuiltIn f "vertex_index" Uint)
  deriving stock (Generic)
  deriving anyclass (ShaderParam)

data VertexOutput f = VertexOutput
  { pos :: BuiltIn f "position" (Vec 4 Float),
    uv :: Location f 0 (Vec 2 Float)
  }
  deriving (Generic, ShaderParam)

-- | Material for rendering something to the whole screen.
data FullscreenMaterial

instance Material FullscreenMaterial where
  type VI FullscreenMaterial = VertexInput
  type VO FullscreenMaterial = VertexOutput
  type B FullscreenMaterial = Bindings

  vertex :: VertexInput GPU -> Shader FullscreenMaterial (VertexOutput GPU)
  vertex (VertexInput index) = do
    positions <- var $ array @(Vec 2 Float) [vec2 (-1) (-1), vec2 3 (-1), vec2 (-1) 3]
    let pos = positions !! index
    pure
      $ VertexOutput
        { pos = vec4 pos.x pos.y 0 1,
          uv = vec2 (pos.x * 0.5 + 0.5) (1.0 - (pos.y * 0.5 + 0.5))
        }

  fragment :: VertexOutput GPU -> Shader FullscreenMaterial (Vec 4 Float)
  fragment _ = pure $ diag 0

  fetch = FetchBindings (const $ pure ()) (const $ pure ()) (const $ const Bindings)