Skip to content

Migrate to js-promise #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sandbox/Sandbox.purs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import Web.HTML (window)
import Web.HTML.HTMLCanvasElement (fromElement, height, width)
import Web.HTML.HTMLDocument (toNonElementParentNode)
import Web.HTML.Window (document, navigator, requestAnimationFrame)
import Web.Promise as Web.Promise
import Promise as Promise

averager :: forall a. EuclideanRing a => Effect (a -> Effect a)
averager = do
Expand All @@ -105,7 +105,7 @@ hackyFloatConv = unsafeCoerce
hackyIntConv :: Array Int -> Array UInt
hackyIntConv = unsafeCoerce

convertPromise :: Web.Promise.Promise ~> Control.Promise.Promise
convertPromise :: Promise.Promise ~> Control.Promise.Promise
convertPromise = unsafeCoerce

-- a port of alaingalvan/webgpu-seed
Expand Down Expand Up @@ -517,7 +517,7 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
@group(0) @binding(0) var<storage, read_write> resultMatrix : array<f32>;
@group(1) @binding(0) var<storage, read> time : f32;
fn xyt2trig(x: u32, y: u32, time: f32) -> f32 {
const pi = 3.14159;
const pi = 3.14159;
var o = (x << 1) + y;
return sin((time * pi) + (f32((o + 1) % 3) * (pi / 2.0)));
}
Expand Down Expand Up @@ -1052,7 +1052,7 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
end passEncoder
--------
-- write to output buffer
-- we use this as a test
-- we use this as a test
copyBufferToBuffer commandEncoder perspectiveResultBuffer 0
buf
0
Expand Down
14 changes: 1 addition & 13 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
{-
Welcome to a Spago project!
You can edit this file as you like.

Need help? See the following resources:
- Spago documentation: https://github.com/purescript/spago
- Dhall language tour: https://docs.dhall-lang.org/tutorials/Language-Tour.html

When creating a new Spago project, you can use
`spago init --no-comments` or `spago init -C`
to generate this file without the comments in this block.
-}
{ name = "webgpu"
, dependencies =
[ "arraybuffer-types"
Expand All @@ -18,6 +6,7 @@ to generate this file without the comments in this block.
, "foreign-object"
, "functions"
, "integers"
, "js-promise"
, "maybe"
, "newtype"
, "ordered-collections"
Expand All @@ -26,7 +15,6 @@ to generate this file without the comments in this block.
, "unsafe-coerce"
, "web-events"
, "web-html"
, "web-promise"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs" ]
Expand Down
15 changes: 10 additions & 5 deletions src/Web/GPU/GPU.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ module Web.GPU.GPU
) where

import Data.Maybe (Maybe(..))
import Effect.Uncurried(EffectFn1, runEffectFn1,EffectFn4, runEffectFn4)
import Effect.Uncurried (EffectFn1, runEffectFn1, EffectFn4, runEffectFn4)
import Effect (Effect)
import Web.GPU.GPURequestAdapterOptions (GPURequestAdapterOptions)
import Web.GPU.GPUTextureFormat (GPUTextureFormat)
import Web.Promise (Promise)
import Promise (Promise)
import Web.GPU.GPUAdapter (GPUAdapter)

data GPU

-- requestAdapter

foreign import requestAdapterImpl :: EffectFn4 (GPUAdapter -> Maybe GPUAdapter) (Maybe GPUAdapter) GPU GPURequestAdapterOptions (Promise (Maybe GPUAdapter))
foreign import requestAdapterImpl
:: EffectFn4 (GPUAdapter -> Maybe GPUAdapter) (Maybe GPUAdapter) GPU
GPURequestAdapterOptions
(Promise (Maybe GPUAdapter))

requestAdapter
:: GPU
-> GPURequestAdapterOptions
Expand All @@ -27,6 +31,7 @@ requestAdapter a b = runEffectFn4 requestAdapterImpl Just Nothing a b

-- getPreferredCanvasFormat

foreign import getPreferredCanvasFormatImpl :: EffectFn1 GPU GPUTextureFormat
foreign import getPreferredCanvasFormatImpl :: EffectFn1 GPU GPUTextureFormat

getPreferredCanvasFormat :: GPU -> Effect GPUTextureFormat
getPreferredCanvasFormat a = runEffectFn1 getPreferredCanvasFormatImpl a
getPreferredCanvasFormat a = runEffectFn1 getPreferredCanvasFormatImpl a
32 changes: 23 additions & 9 deletions src/Web/GPU/GPUAdapter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,57 @@ module Web.GPU.GPUAdapter
) where

import Data.Maybe (Maybe(..))
import Effect.Uncurried(EffectFn1, runEffectFn1,EffectFn2, runEffectFn2,EffectFn3, runEffectFn3,EffectFn4, runEffectFn4)
import Effect.Uncurried (EffectFn1, runEffectFn1, EffectFn2, runEffectFn2, EffectFn3, runEffectFn3, EffectFn4, runEffectFn4)
import Data.Set as Set
import Effect (Effect)
import Web.GPU.GPUDeviceDescriptor (GPUDeviceDescriptor)
import Web.GPU.GPUFeatureName (GPUFeatureName)
import Web.GPU.GPUDevice (GPUDevice)
import Web.GPU.GPUSupportedLimits (GPUSupportedLimits)
import Web.GPU.UnmaskHint (UnmaskHint)
import Web.Promise (Promise)
import Promise (Promise)

data GPUAdapter

-- features
foreign import featuresImpl :: EffectFn3 (GPUFeatureName -> Set.Set GPUFeatureName -> Set.Set GPUFeatureName) (Set.Set GPUFeatureName) GPUAdapter (Set.Set GPUFeatureName)
foreign import featuresImpl
:: EffectFn3
(GPUFeatureName -> Set.Set GPUFeatureName -> Set.Set GPUFeatureName)
(Set.Set GPUFeatureName)
GPUAdapter
(Set.Set GPUFeatureName)

features :: GPUAdapter -> Effect (Set.Set GPUFeatureName)
features a = runEffectFn3 featuresImpl Set.insert Set.empty a

foreign import limitsImpl :: EffectFn1 GPUAdapter { | GPUSupportedLimits }
foreign import limitsImpl :: EffectFn1 GPUAdapter { | GPUSupportedLimits }

limits :: GPUAdapter -> Effect { | GPUSupportedLimits }
limits a = runEffectFn1 limitsImpl a

foreign import isFallbackAdapterImpl :: EffectFn1 GPUAdapter Boolean
foreign import isFallbackAdapterImpl :: EffectFn1 GPUAdapter Boolean

isFallbackAdapter :: GPUAdapter -> Effect Boolean
isFallbackAdapter a = runEffectFn1 isFallbackAdapterImpl a

-- requestDevice

foreign import requestDeviceImpl :: EffectFn4 (GPUDevice -> Maybe GPUDevice)( Maybe GPUDevice) GPUAdapter GPUDeviceDescriptor (Promise (Maybe GPUDevice))
foreign import requestDeviceImpl
:: EffectFn4 (GPUDevice -> Maybe GPUDevice) (Maybe GPUDevice) GPUAdapter
GPUDeviceDescriptor
(Promise (Maybe GPUDevice))

requestDevice
:: GPUAdapter
-> GPUDeviceDescriptor
-> Effect (Promise (Maybe GPUDevice))
requestDevice a b= runEffectFn4 requestDeviceImpl Just Nothing a b
requestDevice a b = runEffectFn4 requestDeviceImpl Just Nothing a b

-- requestAdapterInfo

foreign import requestAdapterInfoImpl :: EffectFn2 GPUAdapter (Array UnmaskHint) (Promise GPUAdapterInfo)
foreign import requestAdapterInfoImpl
:: EffectFn2 GPUAdapter (Array UnmaskHint) (Promise GPUAdapterInfo)

type GPUAdapterInfo =
{ vendor :: String
, architecture :: String
Expand All @@ -60,4 +74,4 @@ type GPUAdapterInfo =

requestAdapterInfo
:: GPUAdapter -> Array UnmaskHint -> Effect (Promise GPUAdapterInfo)
requestAdapterInfo a b = runEffectFn2 requestAdapterInfoImpl a b
requestAdapterInfo a b = runEffectFn2 requestAdapterInfoImpl a b
62 changes: 44 additions & 18 deletions src/Web/GPU/GPUBuffer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,96 @@ module Web.GPU.GPUBuffer
) where

import Prelude
import Effect.Uncurried(EffectFn1, runEffectFn1,EffectFn2, runEffectFn2,EffectFn3, runEffectFn3,EffectFn4, runEffectFn4)
import Effect.Uncurried (EffectFn1, runEffectFn1, EffectFn2, runEffectFn2, EffectFn3, runEffectFn3, EffectFn4, runEffectFn4)

import Data.ArrayBuffer.Types (ArrayBuffer)
import Effect (Effect)
import Web.GPU.GPUBufferMapState (GPUBufferMapState)
import Web.GPU.GPUBufferUsage (GPUBufferUsage)
import Web.GPU.GPUMapMode (GPUMapMode)
import Web.GPU.Internal.Types (GPUSize64)
import Web.Promise (Promise)
import Promise (Promise)

data GPUBuffer

foreign import sizeImpl :: EffectFn1 GPUBuffer GPUSize64
foreign import sizeImpl :: EffectFn1 GPUBuffer GPUSize64

size :: GPUBuffer -> Effect GPUSize64
size a = runEffectFn1 sizeImpl a

foreign import usageImpl :: EffectFn1 GPUBuffer GPUBufferUsage
foreign import usageImpl :: EffectFn1 GPUBuffer GPUBufferUsage

usage :: GPUBuffer -> Effect GPUBufferUsage
usage a = runEffectFn1 usageImpl a

foreign import mapStateImpl :: EffectFn1 GPUBuffer GPUBufferMapState
foreign import mapStateImpl :: EffectFn1 GPUBuffer GPUBufferMapState

mapState :: GPUBuffer -> Effect GPUBufferMapState
mapState a = runEffectFn1 mapStateImpl a

foreign import mapAsyncImpl :: EffectFn2 GPUBuffer GPUMapMode (Promise Unit)
foreign import mapAsyncImpl :: EffectFn2 GPUBuffer GPUMapMode (Promise Unit)

mapAsync :: GPUBuffer -> GPUMapMode -> Effect (Promise Unit)
mapAsync a b = runEffectFn2 mapAsyncImpl a b

foreign import mapAsyncWithOffsetImpl :: EffectFn3 GPUBuffer GPUMapMode GPUSize64 (Promise Unit)
foreign import mapAsyncWithOffsetImpl
:: EffectFn3 GPUBuffer GPUMapMode GPUSize64 (Promise Unit)

mapAsyncWithOffset
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> Effect (Promise Unit)
mapAsyncWithOffset a b c = runEffectFn3 mapAsyncWithOffsetImpl a b c

foreign import mapAsyncWithSizeImpl :: EffectFn3 GPUBuffer GPUMapMode GPUSize64 (Promise Unit)
foreign import mapAsyncWithSizeImpl
:: EffectFn3 GPUBuffer GPUMapMode GPUSize64 (Promise Unit)

mapAsyncWithSize
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> Effect (Promise Unit)
mapAsyncWithSize a b c = runEffectFn3 mapAsyncWithSizeImpl a b c

foreign import mapAsyncWithOffsetAndSizeImpl :: EffectFn4 GPUBuffer GPUMapMode GPUSize64 GPUSize64 (Promise Unit)
foreign import mapAsyncWithOffsetAndSizeImpl
:: EffectFn4 GPUBuffer GPUMapMode GPUSize64 GPUSize64 (Promise Unit)

mapAsyncWithOffsetAndSize
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> GPUSize64 -> Effect (Promise Unit)
mapAsyncWithOffsetAndSize a b c d = runEffectFn4 mapAsyncWithOffsetAndSizeImpl a b c d
mapAsyncWithOffsetAndSize a b c d = runEffectFn4 mapAsyncWithOffsetAndSizeImpl a
b
c
d

foreign import getMappedRangeImpl :: EffectFn1 GPUBuffer ArrayBuffer

foreign import getMappedRangeImpl :: EffectFn1 GPUBuffer ArrayBuffer
getMappedRange :: GPUBuffer -> Effect ArrayBuffer
getMappedRange a = runEffectFn1 getMappedRangeImpl a

foreign import getMappedRangeWithOffsetImpl :: EffectFn2 GPUBuffer GPUSize64 ArrayBuffer
foreign import getMappedRangeWithOffsetImpl
:: EffectFn2 GPUBuffer GPUSize64 ArrayBuffer

getMappedRangeWithOffset :: GPUBuffer -> GPUSize64 -> Effect ArrayBuffer
getMappedRangeWithOffset a b = runEffectFn2 getMappedRangeWithOffsetImpl a b

foreign import getMappedRangeWithSizeImpl :: EffectFn2 GPUBuffer GPUSize64 ArrayBuffer
foreign import getMappedRangeWithSizeImpl
:: EffectFn2 GPUBuffer GPUSize64 ArrayBuffer

getMappedRangeWithSize :: GPUBuffer -> GPUSize64 -> Effect ArrayBuffer
getMappedRangeWithSize a b = runEffectFn2 getMappedRangeWithSizeImpl a b

foreign import getMappedRangeWithOffsetAndSizeImpl :: EffectFn3 GPUBuffer GPUSize64 GPUSize64 ArrayBuffer
foreign import getMappedRangeWithOffsetAndSizeImpl
:: EffectFn3 GPUBuffer GPUSize64 GPUSize64 ArrayBuffer

getMappedRangeWithOffsetAndSize
:: GPUBuffer -> GPUSize64 -> GPUSize64 -> Effect ArrayBuffer
getMappedRangeWithOffsetAndSize a b c = runEffectFn3 getMappedRangeWithOffsetAndSizeImpl a b c
getMappedRangeWithOffsetAndSize a b c = runEffectFn3
getMappedRangeWithOffsetAndSizeImpl
a
b
c

foreign import unmapImpl :: EffectFn1 GPUBuffer Unit

foreign import unmapImpl :: EffectFn1 GPUBuffer Unit
unmap :: GPUBuffer -> Effect Unit
unmap a = runEffectFn1 unmapImpl a

foreign import destroyImpl :: EffectFn1 GPUBuffer Unit
foreign import destroyImpl :: EffectFn1 GPUBuffer Unit

destroy :: GPUBuffer -> Effect Unit
destroy a = runEffectFn1 destroyImpl a
destroy a = runEffectFn1 destroyImpl a
Loading