gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/internal/vk/vulkan_x11.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build ((linux && !android) || freebsd) && !nox11
     4  // +build linux,!android freebsd
     5  // +build !nox11
     6  
     7  package vk
     8  
     9  /*
    10  #define VK_USE_PLATFORM_XLIB_KHR
    11  #define VK_NO_PROTOTYPES 1
    12  #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
    13  #include <vulkan/vulkan.h>
    14  
    15  static VkResult vkCreateXlibSurfaceKHR(PFN_vkCreateXlibSurfaceKHR f, VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
    16  	return f(instance, pCreateInfo, pAllocator, pSurface);
    17  }
    18  */
    19  import "C"
    20  import (
    21  	"fmt"
    22  	"unsafe"
    23  )
    24  
    25  var x11Funcs struct {
    26  	vkCreateXlibSurfaceKHR C.PFN_vkCreateXlibSurfaceKHR
    27  }
    28  
    29  func init() {
    30  	loadFuncs = append(loadFuncs, func(dlopen func(name string) *[0]byte) {
    31  		x11Funcs.vkCreateXlibSurfaceKHR = dlopen("vkCreateXlibSurfaceKHR")
    32  	})
    33  }
    34  
    35  func CreateXlibSurface(inst Instance, dpy unsafe.Pointer, window uintptr) (Surface, error) {
    36  	inf := C.VkXlibSurfaceCreateInfoKHR{
    37  		sType:  C.VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
    38  		dpy:    (*C.Display)(dpy),
    39  		window: (C.Window)(window),
    40  	}
    41  	var surf Surface
    42  	if err := vkErr(C.vkCreateXlibSurfaceKHR(x11Funcs.vkCreateXlibSurfaceKHR, inst, &inf, nil, &surf)); err != nil {
    43  		return 0, fmt.Errorf("vulkan: vkCreateXlibSurfaceKHR: %w", err)
    44  	}
    45  	return surf, nil
    46  }