Skip to content

Commit 5809f1e

Browse files
author
Mike Solomon
committed
Adds x function
1 parent bae618d commit 5809f1e

2 files changed

Lines changed: 80 additions & 71 deletions

File tree

sandbox/Sandbox.purs

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import Web.GPU.GPUVertexState (GPUVertexState)
6363
import Web.GPU.GPUVertexStepMode as StepMode
6464
import Web.GPU.HTMLCanvasElement (getContext)
6565
import Web.GPU.Internal.Bitwise ((.|.))
66-
import Web.GPU.Internal.RequiredAndOptional (e, (~))
66+
import Web.GPU.Internal.RequiredAndOptional (x)
6767
import Web.GPU.Internal.Types (GPUBuffer)
6868
import Web.GPU.Navigator (gpu)
6969
import Web.HTML (window)
@@ -121,13 +121,13 @@ main = do
121121
throwError $ error "WebGPU is not supported"
122122
Just entry -> pure entry
123123
launchAff_ do
124-
adapter <- (toAffE $ convertPromise <$> requestAdapter entry e) >>=
124+
adapter <- (toAffE $ convertPromise <$> requestAdapter entry (x {})) >>=
125125
case _ of
126126
Nothing -> liftEffect do
127127
showErrorMessage
128128
throwError $ error "WebGPU is not supported"
129129
Just adapter -> pure adapter
130-
device <- (toAffE $ convertPromise <$> requestDevice adapter e) >>=
130+
device <- (toAffE $ convertPromise <$> requestDevice adapter (x {})) >>=
131131
case _ of
132132
Nothing -> liftEffect do
133133
showErrorMessage
@@ -144,10 +144,11 @@ main = do
144144
-> Effect GPUBuffer
145145
createBufferF arr usage = do
146146
let
147-
desc =
147+
desc = x
148148
{ size: ((byteLength (Typed.buffer arr)) + 3) .&. complement 3
149149
, usage
150-
} ~ { mappedAtCreation: true }
150+
, mappedAtCreation: true
151+
}
151152
buffer <- createBuffer device desc
152153
writeArray <- getMappedRange buffer >>= whole
153154
_ <- setTyped writeArray Nothing arr
@@ -159,8 +160,9 @@ main = do
159160
-- 🖍️ Shaders
160161
let
161162
vsmDesc =
162-
{ code:
163-
"""
163+
x
164+
{ code:
165+
"""
164166
struct VSOut {
165167
@builtin(position) Position: vec4<f32>,
166168
@location(0) color: vec3<f32>,
@@ -175,101 +177,102 @@ fn main(@location(0) inPos: vec3<f32>,
175177
return vsOut;
176178
}
177179
"""
178-
} ~ {}
180+
}
179181
vertModule <- liftEffect $ createShaderModule device vsmDesc
180182
let
181183
fsmDesc =
182-
{ code:
183-
"""
184+
x
185+
{ code:
186+
"""
184187
@fragment
185188
fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
186189
return vec4<f32>(inColor, 1.0);
187190
}
188191
"""
189-
} ~ {}
192+
}
190193
fragModule <- liftEffect $ createShaderModule device fsmDesc
191194

192195
-- ⚗️ Graphics Pipeline
193196

194197
-- 🔣 Input Assembly
195198
let
196-
(positionAttribDesc :: GPUVertexAttribute) =
199+
(positionAttribDesc :: GPUVertexAttribute) = x
197200
{ shaderLocation: 0
198201
, -- [[location(0)]]
199202
offset: 0
200203
, format: float32x3
201-
} ~ {}
204+
}
202205
let
203-
(colorAttribDesc :: GPUVertexAttribute) =
206+
(colorAttribDesc :: GPUVertexAttribute) = x
204207
{ shaderLocation: 1
205208
, -- [[location(1)]]
206209
offset: 0
207210
, format: float32x3
208-
} ~ {}
211+
}
209212
let
210-
(positionBufferDesc :: GPUVertexBufferLayout) =
213+
(positionBufferDesc :: GPUVertexBufferLayout) = x
211214
{ attributes: [ positionAttribDesc ]
212215
, arrayStride: 4 * 3
213216
-- sizeof(float) * 3
214-
} ~ { stepMode: StepMode.vertex }
217+
, stepMode: StepMode.vertex
218+
}
215219
let
216-
(colorBufferDesc :: GPUVertexBufferLayout) =
220+
(colorBufferDesc :: GPUVertexBufferLayout) = x
217221
{ attributes: [ colorAttribDesc ]
218222
, arrayStride: 4 * 3
219223
-- sizeof(float) * 3
220-
} ~ { stepMode: StepMode.vertex }
224+
, stepMode: StepMode.vertex
225+
}
221226

222227
-- 🌑 Depth
223228
let
224-
(depthStencil :: GPUDepthStencilState) =
229+
(depthStencil :: GPUDepthStencilState) = x
225230
{ format: depth24plusStencil8
226-
} ~
227-
{ depthWriteEnabled: true
228-
, depthCompare: GPUCompareFunction.less
229-
}
231+
, depthWriteEnabled: true
232+
, depthCompare: GPUCompareFunction.less
233+
}
230234

231235
-- 🦄 Uniform Data
232-
let pipelineLayoutDesc = { bindGroupLayouts: [] } ~ {}
236+
let pipelineLayoutDesc = x { bindGroupLayouts: [] }
233237
layout <- liftEffect $ createPipelineLayout device pipelineLayoutDesc
234238

235239
-- 🎭 Shader Stages
236240
let
237-
(vertex :: GPUVertexState) =
241+
(vertex :: GPUVertexState) = x
238242
{ "module": vertModule
239243
, entryPoint: "main"
240-
} ~ { buffers: [ positionBufferDesc, colorBufferDesc ] }
244+
, buffers: [ positionBufferDesc, colorBufferDesc ]
245+
}
241246

242247
-- 🌀 Color/Blend State
243248
let
244-
(colorState :: GPUColorTargetState) =
249+
(colorState :: GPUColorTargetState) = x
245250
{ format: bgra8unorm
246-
} ~ {}
251+
}
247252

248253
let
249-
(fragment :: GPUFragmentState) =
254+
(fragment :: GPUFragmentState) = x
250255
{ "module": fragModule
251256
, entryPoint: "main"
252257
, targets: [ colorState ]
253-
} ~ {}
258+
}
254259

255260
-- 🟨 Rasterization
256261
let
257-
(primitive :: GPUPrimitiveState) =
258-
{} ~
259-
{ frontFace: cw
260-
, cullMode: none
261-
, topology: triangleList
262-
}
262+
(primitive :: GPUPrimitiveState) = x
263+
{ frontFace: cw
264+
, cullMode: none
265+
, topology: triangleList
266+
}
263267

264268
let
265-
(pipelineDesc :: GPURenderPipelineDescriptor) =
269+
(pipelineDesc :: GPURenderPipelineDescriptor) = x
266270
{ layout
267271
, vertex
268-
} ~
269-
{ fragment
270-
, primitive
271-
, depthStencil
272-
}
272+
, fragment
273+
, primitive
274+
, depthStencil
275+
}
273276
pipeline <- liftEffect $ createRenderPipeline device pipelineDesc
274277
{ canvasWidth, canvasHeight, context } <- liftEffect do
275278
d <- window >>= document
@@ -287,56 +290,52 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
287290
canvasHeight <- height canvas
288291
pure { context, canvasWidth, canvasHeight }
289292
let
290-
(config :: GPUCanvasConfiguration) =
293+
(config :: GPUCanvasConfiguration) = x
291294
{ device
292295
, format: bgra8unorm
293-
} ~
294-
{ usage:
295-
GPUTextureUsage.renderAttachment .|.
296-
GPUTextureUsage.copySrc
297-
, alphaMode: opaque
298-
}
296+
, usage:
297+
GPUTextureUsage.renderAttachment .|.
298+
GPUTextureUsage.copySrc
299+
, alphaMode: opaque
300+
}
299301
liftEffect $ configure context config
300302
let
301-
(depthTextureDesc :: GPUTextureDescriptor) =
303+
(depthTextureDesc :: GPUTextureDescriptor) = x
302304
{ size: gpuExtent3DWHD canvasWidth canvasHeight 1
303305
, format: depth24plusStencil8
304306
, usage: GPUTextureUsage.renderAttachment .|. GPUTextureUsage.copySrc
305-
} ~
306-
{ dimension: GPUTextureDimension.twoD
307-
}
307+
, dimension: GPUTextureDimension.twoD
308+
}
308309
depthTexture <- liftEffect $ createTexture device depthTextureDesc
309310
depthTextureView <- liftEffect $ createView depthTexture
310311
let
311312
encodeCommands colorTextureView = do
312313
let
313-
(colorAttachment :: GPURenderPassColorAttachment) =
314+
(colorAttachment :: GPURenderPassColorAttachment) = x
314315
{ view: colorTextureView
315316
, loadOp: GPULoadOp.clear
316317
, storeOp: GPUStoreOp.store
317-
} ~
318-
{ clearValue: gpuColorDict { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
319-
}
318+
, clearValue: gpuColorDict { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
319+
}
320320

321321
let
322-
(depthAttachment :: GPURenderPassDepthStencilAttachment) =
322+
(depthAttachment :: GPURenderPassDepthStencilAttachment) = x
323323
{ view: depthTextureView
324-
325-
} ~
326-
{ depthClearValue: 1.0
327-
, depthLoadOp: GPULoadOp.clear
328-
, depthStoreOp: GPUStoreOp.store
329-
, stencilClearValue: 0
330-
, stencilLoadOp: GPULoadOp.clear
331-
, stencilStoreOp: GPUStoreOp.store
332-
}
324+
, depthClearValue: 1.0
325+
, depthLoadOp: GPULoadOp.clear
326+
, depthStoreOp: GPUStoreOp.store
327+
, stencilClearValue: 0
328+
, stencilLoadOp: GPULoadOp.clear
329+
, stencilStoreOp: GPUStoreOp.store
330+
}
333331

334332
let
335-
(renderPassDesc :: GPURenderPassDescriptor) =
333+
(renderPassDesc :: GPURenderPassDescriptor) = x
336334
{ colorAttachments: [ colorAttachment ]
337-
} ~ { depthStencilAttachment: depthAttachment }
335+
, depthStencilAttachment: depthAttachment
336+
}
338337

339-
commandEncoder <- createCommandEncoder device e
338+
commandEncoder <- createCommandEncoder device (x {})
340339

341340
-- 🖌️ Encode drawing commands
342341
passEncoder <- beginRenderPass commandEncoder renderPassDesc

src/Web/GPU/Internal/RequiredAndOptional.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Web.GPU.Internal.RequiredAndOptional
44
, e
55
, o
66
, r
7+
, x
78
, requiredAndOptional
89
) where
910

@@ -33,6 +34,15 @@ r
3334
-> nt
3435
r = unsafeCoerce
3536

37+
x
38+
:: forall nt incoming required optional optionalL optionalR
39+
. Newtype nt (RequiredAndOptional required optional)
40+
=> Union required optionalL incoming
41+
=> Union optionalL optionalR optional
42+
=> { | incoming }
43+
-> nt
44+
x = unsafeCoerce
45+
3646
o
3747
:: forall nt optionalL optionalR optional
3848
. Union optionalL optionalR optional

0 commit comments

Comments
 (0)