module Mischief.WGPU.Core where

import Data.ByteString
import Data.ByteString qualified as BS
import Data.ByteString.Char8 as BS8
import Data.Data (Proxy (Proxy))
import Data.Void
import Foreign
import Foreign.C.ConstPtr
import Foreign.C.Types
import Mischief.WGPU.Callbacks
import Mischief.WGPU.Opaque
import Mischief.WGPU.Types.Enums
import Mischief.WGPU.Types.General

foreign import ccall "webgpu.h wgpuCreateInstance" c_wgpuCreateInstance :: Ptr () -> IO (Ptr WGPUInstance)

wgpuCreateInstance :: IO (Ptr WGPUInstance)
wgpuCreateInstance :: IO (Ptr WGPUInstance)
wgpuCreateInstance = Ptr () -> IO (Ptr WGPUInstance)
c_wgpuCreateInstance Ptr ()
forall a. Ptr a
nullPtr

foreign import ccall "webgpu.h wgpuInstanceCreateSurface" wgpuInstanceCreateSurface :: Ptr WGPUInstance -> ConstPtr WGPUSurfaceDescriptor -> IO (Ptr WGPUSurface)

newWGPURequestAdapterOptions :: WGPURequestAdapterOptions
newWGPURequestAdapterOptions :: WGPURequestAdapterOptions
newWGPURequestAdapterOptions =
  WGPURequestAdapterOptions
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      featureLevel :: WGPUFeatureLevel
featureLevel = WGPUFeatureLevel
wGPUFeatureLevel_Core,
      powerPreference :: WGPUPowerPreference
powerPreference = WGPUPowerPreference
wGPUPowerPreference_Undefined,
      forceFallbackAdapter :: WGPUBool
forceFallbackAdapter = WGPUBool
wgpuFalse,
      backendType :: WGPUBackendType
backendType = WGPUBackendType
wGPUBackendType_Undefined,
      compatibleSurface :: Ptr WGPUSurface
compatibleSurface = Ptr WGPUSurface
forall a. Ptr a
nullPtr
    }

newWGPURequestCallbackInfo :: WGPURequestCallbackInfo a
newWGPURequestCallbackInfo :: forall a. WGPURequestCallbackInfo a
newWGPURequestCallbackInfo =
  WGPURequestCallbackInfo
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      mode :: WGPUCallbackMode
mode = WGPUCallbackMode
wGPUCallbackMode_WaitAnyOnly,
      callback :: FunPtr a
callback = FunPtr a
forall a. FunPtr a
nullFunPtr,
      userdata1 :: Ptr ()
userdata1 = Ptr ()
forall a. Ptr a
nullPtr,
      userdata2 :: Ptr ()
userdata2 = Ptr ()
forall a. Ptr a
nullPtr
    }

foreign import ccall "wrapper.h wgpuInstanceProcessEvents" wgpuInstanceProcessEvents :: Ptr WGPUInstance -> IO ()

foreign import ccall "wrapper.h hs_wgpuInstanceRequestAdapter" wgpuInstanceRequestAdapter :: Ptr WGPUInstance -> Ptr WGPUSurface -> IO (Ptr WGPUAdapter)

foreign import ccall "wrapper.h hs_wgpuAdapterRequestDevice" wgpuAdapterRequestDevice :: Ptr WGPUAdapter -> IO (Ptr WGPUDevice)

foreign import ccall "webgpu.h wgpuDeviceGetQueue" wgpuDeviceGetQueue :: Ptr WGPUDevice -> IO (Ptr WGPUQueue)

foreign import ccall "webgpu.h wgpuDeviceCreateShaderModule" wgpuDeviceCreateShaderModule :: Ptr WGPUDevice -> ConstPtr WGPUShaderModuleDescriptor -> IO (Ptr WGPUShaderModule)

newWGPUPipelineLayoutDescriptor :: WGPUPipelineLayoutDescriptor
newWGPUPipelineLayoutDescriptor :: WGPUPipelineLayoutDescriptor
newWGPUPipelineLayoutDescriptor =
  WGPUPipelineLayoutDescriptor
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      label :: WGPUStringView
label = WGPUStringView {_data :: ConstPtr CChar
_data = Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
forall a. Ptr a
nullPtr, length :: Int
length = Int
0},
      bindGroupLayoutCount :: CSize
bindGroupLayoutCount = CSize
0,
      bindGroupLayouts :: ConstPtr (Ptr WGPUBindGroupLayout)
bindGroupLayouts = Ptr (Ptr WGPUBindGroupLayout) -> ConstPtr (Ptr WGPUBindGroupLayout)
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr (Ptr WGPUBindGroupLayout)
forall a. Ptr a
nullPtr,
      immediateSize :: CUInt
immediateSize = CUInt
0
    }

bsAsWGPUString :: ByteString -> (WGPUStringView -> IO a) -> IO a
bsAsWGPUString :: forall a. ByteString -> (WGPUStringView -> IO a) -> IO a
bsAsWGPUString ByteString
bs WGPUStringView -> IO a
f = ByteString -> (CStringLen -> IO a) -> IO a
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
bs ((CStringLen -> IO a) -> IO a) -> (CStringLen -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ptr, Int
length) -> WGPUStringView -> IO a
f WGPUStringView {_data :: ConstPtr CChar
_data = Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
ptr, Int
length :: Int
length :: Int
length}

foreign import ccall "webgpu.h wgpuDeviceCreatePipelineLayout" wgpuDeviceCreatePipelineLayout :: Ptr WGPUDevice -> ConstPtr WGPUPipelineLayoutDescriptor -> IO (Ptr WGPUPipelineLayout)

foreign import ccall "webgpu.h wgpuSurfaceGetCapabilities" wgpuSurfaceGetCapabilities :: Ptr WGPUSurface -> Ptr WGPUAdapter -> Ptr WGPUSurfaceCapabilities -> IO ()

withWGPUString :: String -> (WGPUStringView -> IO a) -> IO a
withWGPUString :: forall a. String -> (WGPUStringView -> IO a) -> IO a
withWGPUString String
s = ByteString -> (WGPUStringView -> IO a) -> IO a
forall a. ByteString -> (WGPUStringView -> IO a) -> IO a
bsAsWGPUString (String -> ByteString
BS8.pack String
s)

foreign import ccall "webgpu.h wgpuDeviceCreateRenderPipeline" wgpuDeviceCreateRenderPipeline :: Ptr WGPUDevice -> Ptr WGPURenderPipelineDescriptor -> IO (Ptr WGPURenderPipeline)

newVertexState :: WGPUVertexState
newVertexState :: WGPUVertexState
newVertexState =
  WGPUVertexState
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      _module :: Ptr WGPUShaderModule
_module = Ptr WGPUShaderModule
forall a. Ptr a
nullPtr,
      entryPoint :: WGPUStringView
entryPoint = WGPUStringView {_data :: ConstPtr CChar
_data = Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
forall a. Ptr a
nullPtr, length :: Int
length = Int
0},
      constantCount :: Int
constantCount = Int
0,
      constants :: ConstPtr WGPUConstantEntry
constants = Ptr WGPUConstantEntry -> ConstPtr WGPUConstantEntry
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr WGPUConstantEntry
forall a. Ptr a
nullPtr,
      bufferCount :: Int
bufferCount = Int
0,
      buffers :: ConstPtr WGPUVertexBufferLayout
buffers = Ptr WGPUVertexBufferLayout -> ConstPtr WGPUVertexBufferLayout
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr WGPUVertexBufferLayout
forall a. Ptr a
nullPtr,
      _WGPUVertexState :: Proxy ()
_WGPUVertexState = Proxy ()
forall {k} (t :: k). Proxy t
Proxy
    }

newFragmentState :: WGPUFragmentState
newFragmentState :: WGPUFragmentState
newFragmentState =
  WGPUFragmentState
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      _module :: Ptr WGPUShaderModule
_module = Ptr WGPUShaderModule
forall a. Ptr a
nullPtr,
      entryPoint :: WGPUStringView
entryPoint = WGPUStringView {_data :: ConstPtr CChar
_data = Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
forall a. Ptr a
nullPtr, length :: Int
length = Int
0},
      constantCount :: Int
constantCount = Int
0,
      constants :: ConstPtr WGPUConstantEntry
constants = Ptr WGPUConstantEntry -> ConstPtr WGPUConstantEntry
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr WGPUConstantEntry
forall a. Ptr a
nullPtr,
      targetCount :: Int
targetCount = Int
0,
      targets :: ConstPtr WGPUColorTargetState
targets = Ptr WGPUColorTargetState -> ConstPtr WGPUColorTargetState
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr WGPUColorTargetState
forall a. Ptr a
nullPtr,
      _WGPUFragmentState :: Proxy ()
_WGPUFragmentState = Proxy ()
forall {k} (t :: k). Proxy t
Proxy
    }

newWGPUPrimitiveState :: WGPUPrimitiveState
newWGPUPrimitiveState :: WGPUPrimitiveState
newWGPUPrimitiveState =
  WGPUPrimitiveState
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      topology :: WGPUPrimitiveTopology
topology = WGPUPrimitiveTopology
wGPUPrimitiveTopology_TriangleList,
      stripIndexFormat :: WGPUIndexFormat
stripIndexFormat = WGPUIndexFormat
wGPUIndexFormat_Undefined,
      frontFace :: WGPUFrontFace
frontFace = WGPUFrontFace
wGPUFrontFace_CCW,
      cullMode :: WGPUCullMode
cullMode = WGPUCullMode
wGPUCullMode_Undefined,
      unclippedDepth :: WGPUBool
unclippedDepth = WGPUBool
wgpuFalse
    }

newWGPUSurfaceConfiguration :: WGPUSurfaceConfiguration
newWGPUSurfaceConfiguration :: WGPUSurfaceConfiguration
newWGPUSurfaceConfiguration =
  WGPUSurfaceConfiguration
    { nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr,
      device :: Ptr WGPUDevice
device = Ptr WGPUDevice
forall a. Ptr a
nullPtr,
      format :: WGPUTextureFormat
format = WGPUTextureFormat
wGPUTextureFormat_Undefined,
      usage :: WGPUTextureUsage
usage = WGPUTextureUsage
wGPUTextureUsage_RenderAttachment,
      width :: CInt
width = CInt
0,
      height :: CInt
height = CInt
0,
      viewFormatCount :: Int
viewFormatCount = Int
0,
      viewFormats :: ConstPtr WGPUTextureFormat
viewFormats = Ptr WGPUTextureFormat -> ConstPtr WGPUTextureFormat
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr WGPUTextureFormat
forall a. Ptr a
nullPtr,
      alphaMode :: WGPUCompositeAlphaMode
alphaMode = WGPUCompositeAlphaMode
wGPUCompositeAlphaMode_Auto,
      presentMode :: WGPUPresentMode
presentMode = WGPUPresentMode
wGPUPresentMode_Undefined
    }

foreign import ccall "webgpu.h wgpuSurfaceConfigure" wgpuSurfaceConfigure :: Ptr WGPUSurface -> ConstPtr WGPUSurfaceConfiguration -> IO ()

foreign import ccall "webgpu.h wgpuSurfaceGetCurrentTexture" wgpuSurfaceGetCurrentTexture :: Ptr WGPUSurface -> Ptr WGPUSurfaceTexture -> IO ()

foreign import ccall "webgpu.h wgpuTextureCreateView" wgpuTextureCreateView :: Ptr WGPUTexture -> ConstPtr WGPUTextureViewDescriptor -> IO (Ptr WGPUTextureView)

foreign import ccall "webgpu.h wgpuDeviceCreateCommandEncoder" wgpuDeviceCreateCommandEncoder :: Ptr WGPUDevice -> Ptr WGPUCommandEncoderDescriptor -> IO (Ptr WGPUCommandEncoder)

foreign import ccall "webgpu.h wgpuCommandEncoderBeginRenderPass" wgpuCommandEncoderBeginRenderPass :: Ptr WGPUCommandEncoder -> ConstPtr WGPURenderPassDescriptor -> IO (Ptr WGPURenderPassEncoder)

foreign import ccall "webgpu.h wgpuRenderPassEncoderSetPipeline" wgpuRenderPassEncoderSetPipeline :: Ptr WGPURenderPassEncoder -> Ptr WGPURenderPipeline -> IO ()

foreign import ccall "webgpu.h wgpuRenderPassEncoderDraw" wgpuRenderPassEncoderDraw :: Ptr WGPURenderPassEncoder -> Word32 -> Word32 -> Word32 -> Word32 -> IO ()

foreign import ccall "webgpu.h wgpuRenderPassEncoderEnd" wgpuRenderPassEncoderEnd :: Ptr WGPURenderPassEncoder -> IO ()

foreign import ccall "webgpu.h wgpuCommandEncoderFinish" wgpuCommandEncoderFinish :: Ptr WGPUCommandEncoder -> ConstPtr WGPUCommandBufferDescriptor -> IO (Ptr WGPUCommandBuffer)

foreign import ccall "webgpu.h wgpuQueueSubmit" wgpuQueueSubmit :: Ptr WGPUQueue -> CSize -> ConstPtr (Ptr WGPUCommandBuffer) -> IO ()

foreign import ccall "webgpu.h wgpuRenderPassEncoderRelease" wgpuRenderPassEncoderRelease :: Ptr WGPURenderPassEncoder -> IO ()

foreign import ccall "webgpu.h wgpuRenderPipelineRelease" wgpuRenderPipelineRelease :: Ptr WGPURenderPipeline -> IO ()

foreign import ccall "webgpu.h wgpuPipelineLayoutRelease" wgpuPipelineLayoutRelease :: Ptr WGPUPipelineLayout -> IO ()

foreign import ccall "webgpu.h wgpuSurfaceCapabilitiesFreeMembers" wgpuSurfaceCapabilitiesFreeMembers :: Ptr WGPUSurfaceCapabilities -> IO ()

foreign import ccall "webgpu.h wgpuSurfacePresent" wgpuSurfacePresent :: Ptr WGPUSurface -> IO ()

foreign import ccall "webgpu.h wgpuQueueRelease" wgpuQueueRelease :: Ptr WGPUQueue -> IO ()

foreign import ccall "webgpu.h wgpuDeviceRelease" wgpuDeviceRelease :: Ptr WGPUDevice -> IO ()

foreign import ccall "webgpu.h wgpuAdapterRelease" wgpuAdapterRelease :: Ptr WGPUAdapter -> IO ()

foreign import ccall "webgpu.h wgpuSurfaceRelease" wgpuSurfaceRelease :: Ptr WGPUSurface -> IO ()

foreign import ccall "webgpu.h wgpuShaderModuleRelease" wgpuShaderModuleRelease :: Ptr WGPUShaderModule -> IO ()

foreign import ccall "webgpu.h wgpuInstanceRelease" wgpuInstanceRelease :: Ptr WGPUInstance -> IO ()

foreign import ccall "webgpu.h wgpuCommandBufferRelease" wgpuCommandBufferRelease :: Ptr WGPUCommandBuffer -> IO ()

foreign import ccall "webgpu.h wgpuCommandEncoderRelease" wgpuCommandEncoderRelease :: Ptr WGPUCommandEncoder -> IO ()

foreign import ccall "webgpu.h wgpuTextureViewRelease" wgpuTextureViewRelease :: Ptr WGPUTextureView -> IO ()

foreign import ccall "webgpu.h wgpuTextureRelease" wgpuTextureRelease :: Ptr WGPUTexture -> IO ()

wgpuColor :: Double -> Double -> Double -> Double -> WGPUColor
wgpuColor :: Double -> Double -> Double -> Double -> WGPUColor
wgpuColor Double
r Double
g Double
b Double
a = CDouble -> CDouble -> CDouble -> CDouble -> WGPUColor
WGPUColor (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
r) (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
g) (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
b) (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
a)

foreign import ccall safe "webgpu.h wgpuQueueWriteTexture" wgpuQueueWriteTexture :: Ptr WGPUQueue -> ConstPtr WGPUTexelCopyTextureInfo -> ConstPtr Void -> CSize -> ConstPtr WGPUTexelCopyBufferLayout -> ConstPtr WGPUExtent3D -> IO ()

foreign import ccall safe "webgpu.h wgpuDeviceCreateTexture" wgpuDeviceCreateTexture :: Ptr WGPUDevice -> ConstPtr WGPUTextureDescriptor -> IO (Ptr WGPUTexture)

foreign import ccall safe "webgpu.h wgpuDeviceCreateSampler" wgpuDeviceCreateSampler :: Ptr WGPUDevice -> ConstPtr WGPUSamplerDescriptor -> IO (Ptr WGPUSampler)

foreign import ccall "webgpu.h wgpuDeviceCreateBindGroup" wgpuDeviceCreateBindGroup :: Ptr WGPUDevice -> ConstPtr WGPUBindGroupDescriptor -> IO (Ptr WGPUBindGroup)

unusedBufferLayout :: WGPUBufferBindingLayout
unusedBufferLayout :: WGPUBufferBindingLayout
unusedBufferLayout =
  WGPUBufferBindingLayout
    { minBindingSize :: Word64
minBindingSize = Word64
0,
      hasDynamicOffset :: WGPUBool
hasDynamicOffset = WGPUBool
wgpuFalse,
      _type :: WGPUBufferBindingType
_type = WGPUBufferBindingType
wGPUBufferBindingType_BindingNotUsed,
      nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr
    }

unusedSamplerLayout :: WGPUSamplerBindingLayout
unusedSamplerLayout :: WGPUSamplerBindingLayout
unusedSamplerLayout =
  WGPUSamplerBindingLayout
    { _type :: WGPUSamplerBindingType
_type = WGPUSamplerBindingType
wGPUSamplerBindingType_BindingNotUsed,
      nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr
    }

unusedTextureLayout :: WGPUTextureBindingLayout
unusedTextureLayout :: WGPUTextureBindingLayout
unusedTextureLayout =
  WGPUTextureBindingLayout
    { sampleType :: WGPUTextureSampleType
sampleType = WGPUTextureSampleType
wGPUTextureSampleType_BindingNotUsed,
      multisampled :: WGPUBool
multisampled = WGPUBool
wgpuFalse,
      viewDimension :: WGPUTextureViewDimension
viewDimension = WGPUTextureViewDimension
wGPUTextureViewDimension_1D,
      nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr
    }

unusedStorageTextureLayout :: WGPUStorageTextureBindingLayout
unusedStorageTextureLayout :: WGPUStorageTextureBindingLayout
unusedStorageTextureLayout =
  WGPUStorageTextureBindingLayout
    { viewDimension :: WGPUTextureViewDimension
viewDimension = WGPUTextureViewDimension
wGPUTextureViewDimension_1D,
      format :: WGPUTextureFormat
format = WGPUTextureFormat
wGPUTextureFormat_ASTC10x10Unorm,
      access :: WGPUStorageTextureAccess
access = WGPUStorageTextureAccess
wGPUStorageTextureAccess_BindingNotUsed,
      nextInChain :: Ptr WGPUChainedStruct
nextInChain = Ptr WGPUChainedStruct
forall a. Ptr a
nullPtr
    }

foreign import ccall "webgpu.h wgpuDeviceCreateBindGroupLayout" wgpuDeviceCreateBindGroupLayout :: Ptr WGPUDevice -> ConstPtr WGPUBindGroupLayoutDescriptor -> IO (Ptr WGPUBindGroupLayout)

foreign import ccall "webgpu.h wgpuRenderPassEncoderSetBindGroup" wgpuRenderPassEncoderSetBindGroup :: Ptr WGPURenderPassEncoder -> Word32 -> Ptr WGPUBindGroup -> CSize -> ConstPtr Word32 -> IO ()

foreign import ccall "webgpu.h wgpuDeviceCreateBuffer" wgpuDeviceCreateBuffer :: Ptr WGPUDevice -> ConstPtr WGPUBufferDescriptor -> IO (Ptr WGPUBuffer)

foreign import ccall "webgpu.h wgpuQueueWriteBuffer" wgpuQueueWriteBuffer :: Ptr WGPUQueue -> Ptr WGPUBuffer -> Word64 -> ConstPtr Void -> CSize -> IO ()