zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

vulkan.go (3367B)


      1 package glfw
      2 
      3 /*
      4 #include "glfw/src/internal.h"
      5 
      6 GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface);
      7 GLFWAPI GLFWvkproc glfwGetInstanceProcAddress(VkInstance instance, const char* procname);
      8 
      9 // Helper function for doing raw pointer arithmetic
     10 static inline const char* getArrayIndex(const char** array, unsigned int index) {
     11 	return array[index];
     12 }
     13 
     14 void* getVulkanProcAddr() {
     15 	return glfwGetInstanceProcAddress;
     16 }
     17 */
     18 import "C"
     19 import (
     20 	"errors"
     21 	"fmt"
     22 	"reflect"
     23 	"unsafe"
     24 )
     25 
     26 // VulkanSupported reports whether the Vulkan loader has been found. This check is performed by Init.
     27 //
     28 // The availability of a Vulkan loader does not by itself guarantee that window surface creation or
     29 // even device creation is possible. Call GetRequiredInstanceExtensions to check whether the
     30 // extensions necessary for Vulkan surface creation are available and GetPhysicalDevicePresentationSupport
     31 // to check whether a queue family of a physical device supports image presentation.
     32 func VulkanSupported() bool {
     33 	return glfwbool(C.glfwVulkanSupported())
     34 }
     35 
     36 // GetVulkanGetInstanceProcAddress returns the function pointer used to find Vulkan core or
     37 // extension functions. The return value of this function can be passed to the Vulkan library.
     38 //
     39 // Note that this function does not work the same way as the glfwGetInstanceProcAddress.
     40 func GetVulkanGetInstanceProcAddress() unsafe.Pointer {
     41 	return C.getVulkanProcAddr()
     42 }
     43 
     44 // GetRequiredInstanceExtensions returns a slice of Vulkan instance extension names required
     45 // by GLFW for creating Vulkan surfaces for GLFW windows. If successful, the list will always
     46 // contain VK_KHR_surface, so if you don't require any additional extensions you can pass this list
     47 // directly to the VkInstanceCreateInfo struct.
     48 //
     49 // If Vulkan is not available on the machine, this function returns nil. Call
     50 // VulkanSupported to check whether Vulkan is available.
     51 //
     52 // If Vulkan is available but no set of extensions allowing window surface creation was found, this
     53 // function returns nil. You may still use Vulkan for off-screen rendering and compute work.
     54 func (window *Window) GetRequiredInstanceExtensions() []string {
     55 	var count C.uint32_t
     56 	strarr := C.glfwGetRequiredInstanceExtensions(&count)
     57 	if count == 0 {
     58 		return nil
     59 	}
     60 
     61 	extensions := make([]string, count)
     62 	for i := uint(0); i < uint(count); i++ {
     63 		extensions[i] = C.GoString(C.getArrayIndex(strarr, C.uint(i)))
     64 	}
     65 	return extensions
     66 }
     67 
     68 // CreateWindowSurface creates a Vulkan surface for this window.
     69 func (window *Window) CreateWindowSurface(instance interface{}, allocCallbacks unsafe.Pointer) (surface uintptr, err error) {
     70 	if instance == nil {
     71 		return 0, errors.New("vulkan: instance is nil")
     72 	}
     73 	val := reflect.ValueOf(instance)
     74 	if val.Kind() != reflect.Ptr {
     75 		return 0, fmt.Errorf("vulkan: instance is not a VkInstance (expected kind Ptr, got %s)", val.Kind())
     76 	}
     77 	var vulkanSurface C.VkSurfaceKHR
     78 	ret := C.glfwCreateWindowSurface(
     79 		(C.VkInstance)(unsafe.Pointer(reflect.ValueOf(instance).Pointer())), window.data,
     80 		(*C.VkAllocationCallbacks)(allocCallbacks), (*C.VkSurfaceKHR)(unsafe.Pointer(&vulkanSurface)))
     81 	if ret != C.VK_SUCCESS {
     82 		return 0, fmt.Errorf("vulkan: error creating window surface: %d", ret)
     83 	}
     84 	return uintptr(unsafe.Pointer(&vulkanSurface)), nil
     85 }