Skip to content

Test on MacOS #4

@apenngrace

Description

@apenngrace

I tried making triangle.py work on MacOS (using glfw to create a window & surface), but I got stuck. Any interest on trying to make it work? One issue is that I do not know where to put the mainloop for glfw (so that it does not just immediately exit). However, the an issue even before that is that I'm getting an error with glfw.create_window_surface. I think I'm just doing something wrong with ctypes, but I'm not sure.

I know that the vulkan SDK for Mac works at least, because the code from this repo will run (and that doing some FFI to Ctypes converting).

If you have MacOS, you can install the vulkan-sdk with brew cask install apenngrace/vulkan/vulkan-sdk.

In vk.py I added:

elif system_name == 'Darwin':
    from ctypes import CFUNCTYPE, cdll
    FUNCTYPE = CFUNCTYPE
    vk = cdll.LoadLibrary('libvulkan.dylib')

In triangle.py I added (in the system_name = platform.system() block):

elif system_name == 'Darwin':
    from macos import MacOSWindow as Window, MacOSSwapchain as BaseSwapchain

In triangle.py I added (in the extensions block at the top of create_instance):

        elif system_name == 'Darwin':
            extensions = [b'VK_KHR_surface', b'VK_MVK_macos_surface']

I created the file macos.py with this code:

import vk
import glfw

from ctypes import *
import weakref
import asyncio

class MacOSWindow(object):
    
    def __init__(self, app):
        self.width = 1280
        self.height = 720
        self.window = None
        self.app = weakref.ref(app)

        glfw.init()
        
        glfw.window_hint(glfw.CLIENT_API, glfw.NO_API)
        glfw.window_hint(glfw.VISIBLE, False)           #hidden

        self.window = glfw.create_window(self.width, self.height, 
            "Python vulkan test", None, None)

        # asyncio.ensure_future(process_events(self.app))    

    def __del__(self):
        glfw.terminate()
        

    @property
    def handle(self):
        return self.window
        # return self.__hwnd

    def dimensions(self):
        return (self.width, self.height)
        # dim = RECT()
        # GetClientRect(self.__hwnd, byref(dim))
        # return (dim.right, dim.bottom)

    def show(self):
        glfw.show_window(self.window)
        # ShowWindow(self.__hwnd, SW_SHOWNORMAL)

    def set_title(self, title):
        glfw.set_window_title(self.window, title)

        # glfwSetWindowTitle(window, "My Window");

        # title = c_wchar_p(title)
        # SetWindowTextW(self.__hwnd, title)


class MacOSSwapchain(object):

    def __init__(self, app):
        self.app = weakref.ref(app)
        self.surface = None
        self.swapchain = None
        self.images = None
        self.views = None
        self.create_surface()

    def create_surface(self):
        """
            Create a surface for the window
        """
        
        app = self.app()    #think this paren is because of weakref

        surface = c_void_p(0)

        glfw.create_window_surface(
            instance = app.instance,
            window = app.window.handle, 
            allocator = None,
            surface = byref( surface ) 
        )

        self.surface = surface

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions