Giter VIP home page Giter VIP logo

python-vulkan-triangle's Introduction

Vulkan in Python

This project use python and vulkan to draw a triangle in a window using ctypes for bindings.
The program is stand-alone, you don't have to install anything.
It was tested on Windows 10 and windows 10 using a A-10-7300 Radeon R6 (an APU). All configuration are using the lastest available drivers.

To run the program, simple call
python triangle.py

The program is kind of a port of the vulkan example by Sascha Willems (at https://github.com/SaschaWillems/Vulkan ). All credits to him.

Requirements

Python 3.5 (I use asyncio to handle the system events and the rendering phase asynchronously)
The latest Vulkan driver
Windows or Linux (only tested on Ubuntu 16.04 LTS)
XCB (only on linux)

Also make sure that the vulkan library is visible to the program. If the program can't find it and error about "libvulkan.so" being not found will be raised.

Performances

Keep in mind that the program is not a 1:1 copy of the original example.

Windows 10 / R9 380 / i7 3770 @ 3.4 GHZ : ~ 4000 fps (python/no debugger) VS ~4300 fps (c++/Release build)
Windows 10 / A-10-7300 Radeon R6 : ~ 750 fps (python/no debugging) VS 750 fps (c++)
Ubuntu 16.04 LTS/ R9 380 / i7 3770 @ 3.4 GHZ : ~4800 fps (python no debugger) VS SEGFAULT (c++)

Not much of a suprise, the c++ version is faster, but not my much. Also while the c++ framerate stays relatively stable ~[+-100], the python framerate is much less stable ~(+-300). This is mostly due to the GC I guess.

On my shitty laptop, the python script is as fast as the c++ release build.

Suprise. On ubuntu, the python script is faster than the c++ example on Windows. Sadly, I coudn't test the c++ build as it segfault when I try to run it. :(

Screenshots

Alt text
Alt text

python-vulkan-triangle's People

Contributors

gabdube avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

python-vulkan-triangle's Issues

Test on MacOS

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

Triangle.py not working — CPython 3.6 Win10 64bit

I tried to launch the example
python .\triangle.py

Traceback (most recent call last):
  File ".\triangle.py", line 1693, in <module>
    main()
  File ".\triangle.py", line 1686, in main
    app = TriangleApplication()
  File ".\triangle.py", line 1626, in __init__
    Application.__init__(self)
  File ".\triangle.py", line 832, in __init__
    self.create_instance()
  File ".\triangle.py", line 291, in create_instance
    raise RuntimeError('Instance creation failed. Error code: {}'.format(result))
RuntimeError: Instance creation failed. Error code: 4294967289

Then I installed the VulkanSDK from https://vulkan.lunarg.com/
Now I get a different (OS) Error:

Traceback (most recent call last):
  File ".\triangle.py", line 1693, in <module>
    main()
  File ".\triangle.py", line 1686, in main
    app = TriangleApplication()
  File ".\triangle.py", line 1626, in __init__
    Application.__init__(self)
  File ".\triangle.py", line 832, in __init__
    self.create_instance()
  File ".\triangle.py", line 276, in create_instance
    result = vk.CreateInstance(byref(create_info), None, byref(instance))
OSError: exception: access violation reading 0x0000000200000001

Could you help me to launch your example?

Need To Reference Specific Shared Library Version

When referencing a shared library on Linux, it is important to note that the unversioned name “foo.so” comes from the development package, while a versioned name like “foo.so.1” comes from the runtime package. It is important that your Python binding has a dependency only on the runtime package, not the development package.

The enclosed patch fixes this.

Also: death to wildcard imports!

base_events.py:412: RuntimeWarning: coroutine 'process_events' was never awaited

While trying this triangle example I got the following output

Traceback (most recent call last):
File "triangle.py", line 1693, in
main()
File "triangle.py", line 1686, in main
app = TriangleApplication()
File "triangle.py", line 1626, in init
Application.init(self)
File "triangle.py", line 832, in init
self.create_instance()
File "triangle.py", line 291, in create_instance
raise RuntimeError('Instance creation failed. Error code: {}'.format(result))
RuntimeError: Instance creation failed. Error code: 4294967289
Task was destroyed but it is pending!
task: <Task pending coro=<process_events() running at /home/..../python-vulkan-triangle-master/xlib.py:277>>
/usr/lib/python3.5/asyncio/base_events.py:412: RuntimeWarning: coroutine 'process_events' was never awaited

Python 3.5 virtualenv
Linux Mint 18.3 Kernel 4.15.0-38 (Ubuntu 16.04)
libvulkan1 1.0.61.1+dfsg1-1ubuntu1~16.04.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.