LearnWebGPU-Code icon indicating copy to clipboard operation
LearnWebGPU-Code copied to clipboard

How to correctly bind GLFW3 WebGPU Extension for python

Open prashant-saxena opened this issue 2 years ago • 4 comments

Hello, First of all, A very well written tutorials. I'm really enjoying it. Now the problem:

  1. glfw3 has a python bindings using ctypes.
  2. Let's say bindings for webgpu is also available using ctypes and wgpu_native.dll.

How do I wrap GLFW3 WebGPU Extension or this

WGPUSurface glfwGetWGPUSurface(WGPUInstance instance, GLFWwindow* window);

function in such a way that it can be used inside python. These two functions are the only ones that returns a window, but it seems they are not working.


# https://github.com/FlorianRhiem/pyGLFW/blob/master/glfw/__init__.py#L2893
# This fuction is returning None
print(glfw.get_window_user_pointer(window))

# https://github.com/FlorianRhiem/pyGLFW/blob/master/glfw/__init__.py#L1573
# This function is returning an int
print(glfw.get_win32_window(window))
 

On the wgpu side


# 1. We create a descriptor
desc = wgpu.InstanceDescriptor()
desc.nextInChain = None

# 2. We create the instance using this descriptor
instance = wgpu.CreateInstance(desc)

We have an instance and window as ctypes object but how do I pass them glfw extension function?

Prashant

prashant-saxena avatar Jul 09 '23 19:07 prashant-saxena

With what you already have access to, porting glfw3webgpu should not take long, it's just 20 lines of C per OS: https://github.com/eliemichel/glfw3webgpu/blob/main/glfw3webgpu.c Depending on the OS, you set a different thing in the nextInChain field of the descriptor you pass to wgpu.InstanceCreateSurface. Let me know if you feel stuck I could have a look at it, otherwise feel free to share the result!

eliemichel avatar Jul 10 '23 06:07 eliemichel

I have created a small shared library (glfw3webgpu.dll) using glfw3webgp.c

import glfw
import wgpu # ctypes.CDLL('wgpu_native.dll')
import wgpuglfw # ctypes.CDLL('glfw3webgpu.dll')

glfw_get_wgpu_surface = wgpuglfw.clib.glfwGetWGPUSurface
glfw_get_wgpu_surface.argtypes = [wgpu.clib.Instance, ct.POINTER(glfw._GLFWwindow)]
glfw_get_wgpu_surface.restypes = [ct.POINTER(wgpu.clib.Surface)]   

# Structs are loaded in wgpu.clib and they are not available in wgpuglfw.clib, this could be the issue?

# 1. We create a descriptor
desc = wgpu.InstanceDescriptor()
desc.nextInChain = None

# 2. We create the instance using this descriptor
instance = wgpu.CreateInstance(desc)

surface = glfw_get_wgpu_surface (instance.rval, window)
print(surface) # an integer value is coming here but not Surface struct

I am stuck here completely and have no idea how to resolve this issue. I would really appreciate if someone can help me out here.

prashant-saxena avatar Jul 10 '23 07:07 prashant-saxena

Could you share a full version of the code you currently have, including build instructions? Would save me some time to avoid trying too long to reproduce your setup!

eliemichel avatar Jul 10 '23 18:07 eliemichel

Just give me a day or two. I think I will be able to solve this problem. I will post the results here.

prashant-saxena avatar Jul 11 '23 12:07 prashant-saxena