realitix / vulkan

The ultimate Python binding for Vulkan API
Apache License 2.0
505 stars 46 forks source link

VkResult & vkCreateInstance #2

Closed sunbearc22 closed 7 years ago

sunbearc22 commented 7 years ago
from vulkan import *

APP_SHORT_NAME = "LunarG SDK Cube Example"

# Create Instance of Application

# create application info
app_Info = VkApplicationInfo(
    sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, #indicates the type of the structure
    pNext = None,
    pApplicationName = APP_SHORT_NAME,
    applicationVersion = 1,
    pEngineName = APP_SHORT_NAME, 
    engineVersion = 1,
    apiVersion = VK_API_VERSION_1_0)

# create application instance info
inst_Info = VkInstanceCreateInfo(
    sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
    pNext = None,
    flags = 0,
    pApplicationInfo = app_Info,
    enabledLayerCount = 0,
    ppEnabledLayerNames = None,
    enabledExtensionCount = 0,
    ppEnabledExtensionNames = None)

#inst = VkInstance()
#res = VkResult()

# create application instance
instance = vkCreateInstance(
    pCreateInfo = inst_Info,
    pAllocator = None)#,
    #pInstance = inst)

print('vkResult(instance) = ', vkResult(instance))

Error message:

print('VkResult(instance) = ', VkResult(instance))
NameError: name 'VkResult' is not defined

Question 1: How do I call "VkResult" in your wrapper? VkResult is use to return the state of Vulkan function. I noticed your example.py code, VkResult is not used. How can I report the state of a Vulkan function in vulkan?

Question 2: In the definition of instance using vkCreateInstance, what do I give to pInstance? My confusion here is instance is created so what is the purpose of pInstance. I tried inst = VkInstance() but got error message NameError: name 'VkInstance' is not defined. At the moment, I commented it out to avoid error.

realitix commented 7 years ago

Hello @sunbearc22.

Take a look at this part of the README: https://github.com/realitix/vulkan#functions

1

The wrapper handles VkResult for you. If VkResult is not VK_SUCCESS, an exception is raised. So in your example, If you want to be sure that the function is OK:

try:
    instance = vkCreateInstance(
    pCreateInfo = inst_Info,
    pAllocator = None)#,
    #pInstance = inst)
except VkError:
    print('error')

2

Here the magic of this wrapper, functions will return what you wait for. Don't forget something, in Python, there is no pointer like in C. When you pass pInstance to the function, in the base Vulkan API, it's a pointer which is filled by the driver. You can't do that in pure Python. With CFFI, you can, and the wrapper does that for you. You don't have to know CFFI to use this wrapper, that's my rule.

When you call vkCreateInstance, you don't pass pInstance, you receive it. If the Vulkan function returns several elements, you will get a list. It's what I do in the exemple.py file.

Is this answer enough for you or do you need more informations ?

realitix commented 7 years ago

Don't forget that I am on IRC, we can speak faster by chatting

sunbearc22 commented 7 years ago

Thanks. I understood Ans 1. My bad for forgetting you wrote that in Readme. I was just about to reply with the same answer. I think it will be helpful to include your answer 1 as an example to Readme too. Just a suggestion.

For Ans 2, is there another example you can give?

sunbearc22 commented 7 years ago

Just tried IRC ##vulkan but you are offline. I am sunbearc22.

sunbearc22 commented 7 years ago

How do i see the next level of error message under VkError?

VK_ERROR_OUT_OF_{HOST, DEVICE}_MEMORY
VK_ERROR_{INITIALIZATION, MEMORY_MAP}_FAILED
VK_ERROR_DEVICE_LOST
VK_ERROR_{EXTENSION, FEATURE, LAYER}_NOT_PRESENT
VK_ERROR_INCOMPATIBLE_DRIVER
VK_ERROR_TOO_MANY_OBJECTS
VK_ERROR_FORMAT_NOT_SUPPORTED
VK_ERROR_SURFACE_LOST_KHR
VK_ERROR_OUT_OF_DATE_KHR
VK_ERROR_INCOMPATIBLE_DISPLAY_KHR
VK_ERROR_NATIVE_WINDOW_IN_USE_KHR
VK_ERROR_VALIDATION_FAILED_EXT

Is this correct?

try:
    instance = vkCreateInstance(
    pCreateInfo = inst_Info,
    pAllocator = None)#,
    #pInstance = inst)
except VkError:
    print('error')
    if VkError == VK_ERROR_INCOMPATIBLE_DRIVER:
        print("cannot find a compatible Vulkan ICD")
realitix commented 7 years ago

Sorry I was offline.

For answer 2, I can point you different example in the example.py file:

Return an object:

https://github.com/realitix/vulkan/blob/master/example/example.py#L18 https://github.com/realitix/vulkan/blob/master/example/example.py#L45 https://github.com/realitix/vulkan/blob/master/example/example.py#L200

Return a list:

https://github.com/realitix/vulkan/blob/master/example/example.py#L26 https://github.com/realitix/vulkan/blob/master/example/example.py#L30 https://github.com/realitix/vulkan/blob/master/example/example.py#L137

Return nothing:

https://github.com/realitix/vulkan/blob/master/example/example.py#L684

You can easily know if a function will return a list or an object, for example, if you look at vkCreateInstance documentation here, the last parameter is indicated as

pInstance points a VkInstance handle in which the resulting instance is returned.

It's clear it will return an object.

For vkEnumerateInstanceExtensionProperties here:

pProperties is either NULL or a pointer to an array of VkExtensionProperties structures.

It's clear it will return a list. Moreover, you don't have to bother with pPropertyCount, the wrapper do it for you. It's why you only pass the first parameter pLayerName (None in example.py)

Is it ok now or do you need more informations ?

realitix commented 7 years ago

For the last question:

You must do:

try:
    instance = vkCreateInstance(
    pCreateInfo = inst_Info,
    pAllocator = None)
except VkErrorIncompatibleDriver:
    print("cannot find a compatible Vulkan ICD")
except VkError:
    print('error')
sunbearc22 commented 7 years ago

Thanks for the Ans 2 elaboration. Helpful.

Can you clarify if my Exception code is correct for my question on Ans 1? I submitted the code with not python error but not sure if it is correct.

realitix commented 7 years ago

I have just answered above.

sunbearc22 commented 7 years ago

Sorry. Screen did not refresh so did not see your answer. So what is the purpose of VkError & VkException given that their subsets may be called directly?

realitix commented 7 years ago

It's a classic Python pattern. The goal of the parent exception is to catch all others errors. I mean, errors that you don't care. For example, you could want to react if there is an error (and you don't need to know the exact error)... In well written Python script, you will always find this patter, a precise exception that extend a global exception. It gives you much freedom.

sunbearc22 commented 7 years ago

Got it.

realitix commented 7 years ago

Can I close this issue ?

sunbearc22 commented 7 years ago

yep. Thanks.

realitix commented 7 years ago

You're welcome !