github.com/Seikaijyu/gio@v0.0.1/gpu/headless/headless_vulkan.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build (linux || freebsd) && !novulkan
     4  // +build linux freebsd
     5  // +build !novulkan
     6  
     7  package headless
     8  
     9  import (
    10  	"unsafe"
    11  
    12  	"github.com/Seikaijyu/gio/gpu"
    13  	"github.com/Seikaijyu/gio/internal/vk"
    14  )
    15  
    16  type vkContext struct {
    17  	physDev  vk.PhysicalDevice
    18  	inst     vk.Instance
    19  	dev      vk.Device
    20  	queueFam int
    21  }
    22  
    23  func init() {
    24  	newContextFallback = newVulkanContext
    25  }
    26  
    27  func newVulkanContext() (context, error) {
    28  	inst, err := vk.CreateInstance()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	physDev, qFam, err := vk.ChoosePhysicalDevice(inst, 0)
    33  	if err != nil {
    34  		vk.DestroyInstance(inst)
    35  		return nil, err
    36  	}
    37  	dev, err := vk.CreateDeviceAndQueue(physDev, qFam)
    38  	if err != nil {
    39  		vk.DestroyInstance(inst)
    40  		return nil, err
    41  	}
    42  	ctx := &vkContext{
    43  		physDev:  physDev,
    44  		inst:     inst,
    45  		dev:      dev,
    46  		queueFam: qFam,
    47  	}
    48  	return ctx, nil
    49  }
    50  
    51  func (c *vkContext) API() gpu.API {
    52  	return gpu.Vulkan{
    53  		PhysDevice:  unsafe.Pointer(c.physDev),
    54  		Device:      unsafe.Pointer(c.dev),
    55  		Format:      int(vk.FORMAT_R8G8B8A8_SRGB),
    56  		QueueFamily: c.queueFam,
    57  		QueueIndex:  0,
    58  	}
    59  }
    60  
    61  func (c *vkContext) MakeCurrent() error {
    62  	return nil
    63  }
    64  
    65  func (c *vkContext) ReleaseCurrent() {
    66  }
    67  
    68  func (c *vkContext) Release() {
    69  	vk.DeviceWaitIdle(c.dev)
    70  
    71  	vk.DestroyDevice(c.dev)
    72  	vk.DestroyInstance(c.inst)
    73  	*c = vkContext{}
    74  }