packages feed

fir-0.1.0.0: test/Tests/Array/ApplicativeConvert.hs

{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE PolyKinds           #-}
{-# LANGUAGE RebindableSyntax    #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeOperators       #-}

module Tests.Array.ApplicativeConvert where

-- base
import Data.Maybe
  ( fromJust )

-- vector-sized
import qualified Data.Vector.Sized as Vector

-- fir
import FIR

------------------------------------------------
-- program

-- Mapping a function over an array, with an element type for the result
-- that differs from that of the input array.

table :: Array 4 Word32
table = MkArray ( fromJust $ Vector.fromList [ 1, 2, 3, 4 ] )

type Defs =
  '[ "ubo"  ':-> Uniform '[ DescriptorSet 0, Binding 0 ]
                   ( Struct '[ "i" ':-> Word32 ] )
   , "out"  ':-> StorageBuffer '[ DescriptorSet 0, Binding 1 ]
                   ( Struct '[ "res" ':-> Float ] )
   , "main" ':-> EntryPoint '[ LocalSize 1 1 1 ] Compute
   ]

program :: Module Defs
program = Module do

  entryPoint @"main" @Compute do

    i <- use @( Name "ubo" :.: Name "i" )

    let
      arr :: Code ( Array 4 Word32 )
      arr = Lit table
      toFloat :: Code Word32 -> Code Float
      toFloat = fromIntegral
      arr' :: Code ( Array 4 Float )
      arr' = toFloat <$$> arr

    assign @( Name "out" :.: Name "res" )
      ( view @( Index 0 ) arr' + toFloat ( view @( AnIndex ( Code Word32 ) ) i arr ) )