Skip to content

Commit ede4201

Browse files
author
Mike Solomon
authored
Adds more EffectFn bindings (#1)
These bindings will speed up the code execution, especially when using the ES optimizer.
1 parent 49d9200 commit ede4201

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1391
-1817
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"sandbox": "spago -x sandbox.dhall build && vite build sandbox/",
88
"sandbox-dev": "spago -x sandbox-dev.dhall build && vite dev sandbox/",
9-
"format": "purs-tidy format-in-place \"src/**/*.purs sandbox/**/*.purs\" && prettier --write \"src/**/*.js\""
9+
"format": "purs-tidy format-in-place \"src/**/*.purs sandbox/**/*.purs\" && prettier --arrow-parens avoid --write \"src/**/*.js\""
1010
},
1111
"keywords": [],
1212
"author": "",

spago.dhall

+2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ to generate this file without the comments in this block.
1616
, "effect"
1717
, "foreign"
1818
, "foreign-object"
19+
, "functions"
1920
, "integers"
2021
, "maybe"
2122
, "newtype"
2223
, "ordered-collections"
2324
, "prelude"
25+
, "uint"
2426
, "unsafe-coerce"
2527
, "web-events"
2628
, "web-html"

src/Web/GPU/BufferSource.purs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Data.ArrayBuffer.Types (ArrayBuffer, DataView, Float32Array, Float64Array
1515
import Unsafe.Coerce (unsafeCoerce)
1616

1717
data BufferSource
18+
1819
fromArrayBuffer :: ArrayBuffer -> BufferSource
1920
fromArrayBuffer = unsafeCoerce
2021

src/Web/GPU/GPU.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
export const requestAdapterImpl =
2-
(just) => (nothing) => (gpu) => (options) => () =>
3-
gpu.requestAdapter(options).then((o) => (o ? just(o) : nothing));
1+
export const requestAdapterImpl = (just, nothing, gpu, options) =>
2+
gpu.requestAdapter(options).then(o => (o ? just(o) : nothing));
43

5-
export const getPreferredCanvasFormatImpl = (gpu) => () =>
4+
export const getPreferredCanvasFormatImpl = gpu =>
65
gpu.getPreferredCanvasFormat();

src/Web/GPU/GPU.purs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
-- @inline export requestAdapter arity=1
22
-- @inline export getPreferredCanvasFormat arity=1
33
module Web.GPU.GPU
4-
( GPU,requestAdapter
4+
( GPU
5+
, requestAdapter
56
, getPreferredCanvasFormat
67
) where
78

89
import Data.Maybe (Maybe(..))
10+
import Effect.Uncurried(EffectFn1, runEffectFn1,EffectFn4, runEffectFn4)
911
import Effect (Effect)
1012
import Web.GPU.GPURequestAdapterOptions (GPURequestAdapterOptions)
1113
import Web.GPU.GPUTextureFormat (GPUTextureFormat)
1214
import Web.Promise (Promise)
13-
import Web.GPU.GPUAdapter(GPUAdapter)
15+
import Web.GPU.GPUAdapter (GPUAdapter)
1416

1517
data GPU
16-
-- requestAdapter
1718

18-
foreign import requestAdapterImpl
19-
:: (GPUAdapter -> Maybe GPUAdapter)
20-
-> Maybe GPUAdapter
21-
-> GPU
22-
-> GPURequestAdapterOptions
23-
-> Effect (Promise (Maybe GPUAdapter))
19+
-- requestAdapter
2420

21+
foreign import requestAdapterImpl :: EffectFn4 (GPUAdapter -> Maybe GPUAdapter) (Maybe GPUAdapter) GPU GPURequestAdapterOptions (Promise (Maybe GPUAdapter))
2522
requestAdapter
2623
:: GPU
2724
-> GPURequestAdapterOptions
2825
-> Effect (Promise (Maybe GPUAdapter))
29-
requestAdapter = requestAdapterImpl Just Nothing
26+
requestAdapter a b = runEffectFn4 requestAdapterImpl Just Nothing a b
3027

3128
-- getPreferredCanvasFormat
3229

33-
foreign import getPreferredCanvasFormatImpl :: GPU -> Effect GPUTextureFormat
34-
30+
foreign import getPreferredCanvasFormatImpl :: EffectFn1 GPU GPUTextureFormat
3531
getPreferredCanvasFormat :: GPU -> Effect GPUTextureFormat
36-
getPreferredCanvasFormat = getPreferredCanvasFormatImpl
32+
getPreferredCanvasFormat a = runEffectFn1 getPreferredCanvasFormatImpl a

src/Web/GPU/GPUAdapter.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
export const featuresImpl = (insert) => (empty) => (adapter) => () => {
1+
export const featuresImpl = (insert, empty, adapter) => {
22
const iterator1 = adapter.features.entries();
33
let out = empty;
44
for (const entry of iterator1) {
55
out = insert(entry)(out);
66
}
77
return out;
88
};
9-
export const limitsImpl = (adapter) => () => adapter.limits;
10-
export const isFallbackAdapterImpl = (adapter) => () =>
11-
adapter.isFallbackAdapter;
12-
export const requestDeviceImpl =
13-
(just) => (nothing) => (adapter) => (options) => () =>
14-
adapter.requestDevice(options).then((o) => (o ? just(o) : nothing));
15-
export const requestAdapterInfoImpl = (adapter) => (unmaskHints) => () =>
9+
export const limitsImpl = adapter => adapter.limits;
10+
export const isFallbackAdapterImpl = adapter => adapter.isFallbackAdapter;
11+
export const requestDeviceImpl = (just, nothing, adapter, options) =>
12+
adapter.requestDevice(options).then(o => (o ? just(o) : nothing));
13+
export const requestAdapterInfoImpl = (adapter, unmaskHints) =>
1614
adapter.requestAdapterInfo(unmaskHints);

src/Web/GPU/GPUAdapter.purs

+13-28
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,46 @@ module Web.GPU.GPUAdapter
1111
, limits
1212
, requestAdapterInfo
1313
, requestDevice
14-
)
15-
where
14+
) where
1615

1716
import Data.Maybe (Maybe(..))
17+
import Effect.Uncurried(EffectFn1, runEffectFn1,EffectFn2, runEffectFn2,EffectFn3, runEffectFn3,EffectFn4, runEffectFn4)
1818
import Data.Set as Set
1919
import Effect (Effect)
2020
import Web.GPU.GPUDeviceDescriptor (GPUDeviceDescriptor)
2121
import Web.GPU.GPUFeatureName (GPUFeatureName)
22-
import Web.GPU.GPUDevice(GPUDevice)
22+
import Web.GPU.GPUDevice (GPUDevice)
2323
import Web.GPU.GPUSupportedLimits (GPUSupportedLimits)
2424
import Web.GPU.UnmaskHint (UnmaskHint)
2525
import Web.Promise (Promise)
2626

2727
data GPUAdapter
2828

2929
-- features
30-
foreign import featuresImpl
31-
:: (GPUFeatureName -> Set.Set GPUFeatureName -> Set.Set GPUFeatureName)
32-
-> Set.Set GPUFeatureName
33-
-> GPUAdapter
34-
-> Effect (Set.Set GPUFeatureName)
35-
30+
foreign import featuresImpl :: EffectFn3 (GPUFeatureName -> Set.Set GPUFeatureName -> Set.Set GPUFeatureName) (Set.Set GPUFeatureName) GPUAdapter (Set.Set GPUFeatureName)
3631
features :: GPUAdapter -> Effect (Set.Set GPUFeatureName)
37-
features = featuresImpl Set.insert Set.empty
38-
39-
foreign import limitsImpl :: GPUAdapter -> Effect { | GPUSupportedLimits }
32+
features a = runEffectFn3 featuresImpl Set.insert Set.empty a
4033

34+
foreign import limitsImpl :: EffectFn1 GPUAdapter { | GPUSupportedLimits }
4135
limits :: GPUAdapter -> Effect { | GPUSupportedLimits }
42-
limits = limitsImpl
43-
44-
foreign import isFallbackAdapterImpl :: GPUAdapter -> Effect Boolean
36+
limits a = runEffectFn1 limitsImpl a
4537

38+
foreign import isFallbackAdapterImpl :: EffectFn1 GPUAdapter Boolean
4639
isFallbackAdapter :: GPUAdapter -> Effect Boolean
47-
isFallbackAdapter = isFallbackAdapterImpl
40+
isFallbackAdapter a = runEffectFn1 isFallbackAdapterImpl a
4841

4942
-- requestDevice
5043

51-
foreign import requestDeviceImpl
52-
:: (GPUDevice -> Maybe GPUDevice)
53-
-> Maybe GPUDevice
54-
-> GPUAdapter
55-
-> GPUDeviceDescriptor
56-
-> Effect (Promise (Maybe GPUDevice))
57-
44+
foreign import requestDeviceImpl :: EffectFn4 (GPUDevice -> Maybe GPUDevice)( Maybe GPUDevice) GPUAdapter GPUDeviceDescriptor (Promise (Maybe GPUDevice))
5845
requestDevice
5946
:: GPUAdapter
6047
-> GPUDeviceDescriptor
6148
-> Effect (Promise (Maybe GPUDevice))
62-
requestDevice = requestDeviceImpl Just Nothing
49+
requestDevice a b= runEffectFn4 requestDeviceImpl Just Nothing a b
6350

6451
-- requestAdapterInfo
6552

66-
foreign import requestAdapterInfoImpl
67-
:: GPUAdapter -> Array (UnmaskHint) -> Effect (Promise GPUAdapterInfo)
68-
53+
foreign import requestAdapterInfoImpl :: EffectFn2 GPUAdapter (Array UnmaskHint) (Promise GPUAdapterInfo)
6954
type GPUAdapterInfo =
7055
{ vendor :: String
7156
, architecture :: String
@@ -75,4 +60,4 @@ type GPUAdapterInfo =
7560

7661
requestAdapterInfo
7762
:: GPUAdapter -> Array UnmaskHint -> Effect (Promise GPUAdapterInfo)
78-
requestAdapterInfo = requestAdapterInfoImpl
63+
requestAdapterInfo a b = runEffectFn2 requestAdapterInfoImpl a b

src/Web/GPU/GPUBindGroup.purs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Web.GPU.GPUBindGroup
22
( GPUBindGroup
3-
)
4-
where
3+
) where
54

65
data GPUBindGroup

src/Web/GPU/GPUBindGroupLayout.purs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Web.GPU.GPUBindGroupLayout
22
( GPUBindGroupLayout
3-
)
4-
where
3+
) where
54

65
data GPUBindGroupLayout

src/Web/GPU/GPUBuffer.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
export const sizeImpl = (buffer) => () => buffer.size;
2-
export const usageImpl = (buffer) => () => buffer.usage;
3-
export const mapStateImpl = (buffer) => () => buffer.mapState;
4-
export const mapAsyncImpl = (buffer) => (mode) => () => buffer.mapAsync(mode);
5-
export const mapAsyncWithOffsetImpl = (buffer) => (mode) => (offset) => () =>
1+
export const sizeImpl = buffer => buffer.size;
2+
export const usageImpl = buffer => buffer.usage;
3+
export const mapStateImpl = buffer => buffer.mapState;
4+
export const mapAsyncImpl = (buffer, mode) => buffer.mapAsync(mode);
5+
export const mapAsyncWithOffsetImpl = (buffer, mode, offset) =>
66
buffer.mapAsync(mode, offset);
7-
export const mapAsyncWithSizeImpl = (buffer) => (mode) => (size) => () =>
7+
export const mapAsyncWithSizeImpl = (buffer, mode, size) =>
88
buffer.mapAsync(mode, undefined, size);
9-
export const mapAsyncWithOffsetAndSizeImpl =
10-
(buffer) => (mode) => (offset) => (size) => () =>
11-
buffer.mapAsync(mode, offset, size);
12-
export const getMappedRangeImpl = (buffer) => () => buffer.getMappedRange();
13-
export const getMappedRangeWithOffsetImpl = (buffer) => (offset) => () =>
9+
export const mapAsyncWithOffsetAndSizeImpl = (buffer, mode, offset, size) =>
10+
buffer.mapAsync(mode, offset, size);
11+
export const getMappedRangeImpl = buffer => buffer.getMappedRange();
12+
export const getMappedRangeWithOffsetImpl = (buffer, offset) =>
1413
buffer.getMappedRange(offset);
15-
export const getMappedRangeWithSizeImpl = (buffer) => (size) => () =>
14+
export const getMappedRangeWithSizeImpl = (buffer, size) =>
1615
buffer.getMappedRange(undefined, size);
17-
export const getMappedRangeWithOffsetAndSizeImpl =
18-
(buffer) => (offset) => (size) => () =>
19-
buffer.getMappedRange(offset, size);
20-
export const unmapImpl = (buffer) => () => buffer.unmap();
21-
export const destroyImpl = (buffer) => () => buffer.destroy();
16+
export const getMappedRangeWithOffsetAndSizeImpl = (buffer, offset, size) =>
17+
buffer.getMappedRange(offset, size);
18+
export const unmapImpl = buffer => buffer.unmap();
19+
export const destroyImpl = buffer => buffer.destroy();

src/Web/GPU/GPUBuffer.purs

+28-46
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ module Web.GPU.GPUBuffer
2626
, size
2727
, unmap
2828
, usage
29-
)
30-
where
29+
) where
3130

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

3434
import Data.ArrayBuffer.Types (ArrayBuffer)
3535
import Effect (Effect)
@@ -40,77 +40,59 @@ import Web.GPU.Internal.Types (GPUSize64)
4040
import Web.Promise (Promise)
4141

4242
data GPUBuffer
43-
foreign import sizeImpl :: GPUBuffer -> Effect GPUSize64
4443

44+
foreign import sizeImpl :: EffectFn1 GPUBuffer GPUSize64
4545
size :: GPUBuffer -> Effect GPUSize64
46-
size = sizeImpl
47-
48-
foreign import usageImpl :: GPUBuffer -> Effect GPUBufferUsage
46+
size a = runEffectFn1 sizeImpl a
4947

48+
foreign import usageImpl :: EffectFn1 GPUBuffer GPUBufferUsage
5049
usage :: GPUBuffer -> Effect GPUBufferUsage
51-
usage = usageImpl
52-
53-
foreign import mapStateImpl :: GPUBuffer -> Effect GPUBufferMapState
50+
usage a = runEffectFn1 usageImpl a
5451

52+
foreign import mapStateImpl :: EffectFn1 GPUBuffer GPUBufferMapState
5553
mapState :: GPUBuffer -> Effect GPUBufferMapState
56-
mapState = mapStateImpl
57-
58-
foreign import mapAsyncImpl :: GPUBuffer -> GPUMapMode -> Effect (Promise Unit)
54+
mapState a = runEffectFn1 mapStateImpl a
5955

56+
foreign import mapAsyncImpl :: EffectFn2 GPUBuffer GPUMapMode (Promise Unit)
6057
mapAsync :: GPUBuffer -> GPUMapMode -> Effect (Promise Unit)
61-
mapAsync = mapAsyncImpl
62-
63-
foreign import mapAsyncWithOffsetImpl
64-
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> Effect (Promise Unit)
58+
mapAsync a b = runEffectFn2 mapAsyncImpl a b
6559

60+
foreign import mapAsyncWithOffsetImpl :: EffectFn3 GPUBuffer GPUMapMode GPUSize64 (Promise Unit)
6661
mapAsyncWithOffset
6762
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> Effect (Promise Unit)
68-
mapAsyncWithOffset = mapAsyncWithOffsetImpl
69-
70-
foreign import mapAsyncWithSizeImpl
71-
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> Effect (Promise Unit)
63+
mapAsyncWithOffset a b c = runEffectFn3 mapAsyncWithOffsetImpl a b c
7264

65+
foreign import mapAsyncWithSizeImpl :: EffectFn3 GPUBuffer GPUMapMode GPUSize64 (Promise Unit)
7366
mapAsyncWithSize
7467
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> Effect (Promise Unit)
75-
mapAsyncWithSize = mapAsyncWithSizeImpl
76-
77-
foreign import mapAsyncWithOffsetAndSizeImpl
78-
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> GPUSize64 -> Effect (Promise Unit)
68+
mapAsyncWithSize a b c = runEffectFn3 mapAsyncWithSizeImpl a b c
7969

70+
foreign import mapAsyncWithOffsetAndSizeImpl :: EffectFn4 GPUBuffer GPUMapMode GPUSize64 GPUSize64 (Promise Unit)
8071
mapAsyncWithOffsetAndSize
8172
:: GPUBuffer -> GPUMapMode -> GPUSize64 -> GPUSize64 -> Effect (Promise Unit)
82-
mapAsyncWithOffsetAndSize = mapAsyncWithOffsetAndSizeImpl
83-
84-
foreign import getMappedRangeImpl :: GPUBuffer -> Effect ArrayBuffer
73+
mapAsyncWithOffsetAndSize a b c d = runEffectFn4 mapAsyncWithOffsetAndSizeImpl a b c d
8574

75+
foreign import getMappedRangeImpl :: EffectFn1 GPUBuffer ArrayBuffer
8676
getMappedRange :: GPUBuffer -> Effect ArrayBuffer
87-
getMappedRange = getMappedRangeImpl
88-
89-
foreign import getMappedRangeWithOffsetImpl
90-
:: GPUBuffer -> GPUSize64 -> Effect ArrayBuffer
77+
getMappedRange a = runEffectFn1 getMappedRangeImpl a
9178

79+
foreign import getMappedRangeWithOffsetImpl :: EffectFn2 GPUBuffer GPUSize64 ArrayBuffer
9280
getMappedRangeWithOffset :: GPUBuffer -> GPUSize64 -> Effect ArrayBuffer
93-
getMappedRangeWithOffset = getMappedRangeWithOffsetImpl
94-
95-
foreign import getMappedRangeWithSizeImpl
96-
:: GPUBuffer -> GPUSize64 -> Effect ArrayBuffer
81+
getMappedRangeWithOffset a b = runEffectFn2 getMappedRangeWithOffsetImpl a b
9782

83+
foreign import getMappedRangeWithSizeImpl :: EffectFn2 GPUBuffer GPUSize64 ArrayBuffer
9884
getMappedRangeWithSize :: GPUBuffer -> GPUSize64 -> Effect ArrayBuffer
99-
getMappedRangeWithSize = getMappedRangeWithSizeImpl
100-
101-
foreign import getMappedRangeWithOffsetAndSizeImpl
102-
:: GPUBuffer -> GPUSize64 -> GPUSize64 -> Effect ArrayBuffer
85+
getMappedRangeWithSize a b = runEffectFn2 getMappedRangeWithSizeImpl a b
10386

87+
foreign import getMappedRangeWithOffsetAndSizeImpl :: EffectFn3 GPUBuffer GPUSize64 GPUSize64 ArrayBuffer
10488
getMappedRangeWithOffsetAndSize
10589
:: GPUBuffer -> GPUSize64 -> GPUSize64 -> Effect ArrayBuffer
106-
getMappedRangeWithOffsetAndSize = getMappedRangeWithOffsetAndSizeImpl
107-
108-
foreign import unmapImpl :: GPUBuffer -> Effect Unit
90+
getMappedRangeWithOffsetAndSize a b c = runEffectFn3 getMappedRangeWithOffsetAndSizeImpl a b c
10991

92+
foreign import unmapImpl :: EffectFn1 GPUBuffer Unit
11093
unmap :: GPUBuffer -> Effect Unit
111-
unmap = unmapImpl
112-
113-
foreign import destroyImpl :: GPUBuffer -> Effect Unit
94+
unmap a = runEffectFn1 unmapImpl a
11495

96+
foreign import destroyImpl :: EffectFn1 GPUBuffer Unit
11597
destroy :: GPUBuffer -> Effect Unit
116-
destroy = destroyImpl
98+
destroy a = runEffectFn1 destroyImpl a

src/Web/GPU/GPUCanvasConfiguration.purs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Web.GPU.GPUTextureUsage (GPUTextureUsageFlags)
77
import Web.GPU.Internal.RequiredAndOptional (RequiredAndOptional)
88
import Web.GPU.PredefinedColorSpace (PredefinedColorSpace)
99
import Web.GPU.GPUDevice (GPUDevice)
10+
1011
newtype GPUCanvasConfiguration = GPUCanvasConfiguration
1112
( RequiredAndOptional
1213
( device :: GPUDevice

src/Web/GPU/GPUCanvasContext.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
export const canvasImpl = (context) => () => context.canvas;
2-
export const configureImpl = (context) => (descriptor) => () =>
1+
export const canvasImpl = context => context.canvas;
2+
export const configureImpl = (context, descriptor) =>
33
context.configure(descriptor);
4-
export const unconfigureImpl = (context) => () => context.unconfigure();
5-
export const getCurrentTextureImpl = (context) => () =>
6-
context.getCurrentTexture();
4+
export const unconfigureImpl = context => context.unconfigure();
5+
export const getCurrentTextureImpl = context => context.getCurrentTexture();

0 commit comments

Comments
 (0)