github.com/Seikaijyu/gio@v0.0.1/gpu/internal/driver/api.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package driver
     4  
     5  import (
     6  	"fmt"
     7  	"unsafe"
     8  
     9  	"github.com/Seikaijyu/gio/internal/gl"
    10  )
    11  
    12  // See gpu/api.go for documentation for the API types.
    13  
    14  type API interface {
    15  	implementsAPI()
    16  }
    17  
    18  type RenderTarget interface {
    19  	ImplementsRenderTarget()
    20  }
    21  
    22  type OpenGLRenderTarget gl.Framebuffer
    23  
    24  type Direct3D11RenderTarget struct {
    25  	// RenderTarget is a *ID3D11RenderTargetView.
    26  	RenderTarget unsafe.Pointer
    27  }
    28  
    29  type MetalRenderTarget struct {
    30  	// Texture is a MTLTexture.
    31  	Texture uintptr
    32  }
    33  
    34  type VulkanRenderTarget struct {
    35  	// WaitSem is a VkSemaphore that must signaled before accessing Framebuffer.
    36  	WaitSem uint64
    37  	// SignalSem is a VkSemaphore that signal access to Framebuffer is complete.
    38  	SignalSem uint64
    39  	// Fence is a VkFence that is set when all commands to Framebuffer has completed.
    40  	Fence uint64
    41  	// Image is the VkImage to render into.
    42  	Image uint64
    43  	// Framebuffer is a VkFramebuffer for Image.
    44  	Framebuffer uint64
    45  }
    46  
    47  type OpenGL struct {
    48  	// ES forces the use of ANGLE OpenGL ES libraries on macOS. It is
    49  	// ignored on all other platforms.
    50  	ES bool
    51  	// Context contains the WebGL context for WebAssembly platforms. It is
    52  	// empty for all other platforms; an OpenGL context is assumed current when
    53  	// calling NewDevice.
    54  	Context gl.Context
    55  	// Shared instructs users of the context to restore the GL state after
    56  	// use.
    57  	Shared bool
    58  }
    59  
    60  type Direct3D11 struct {
    61  	// Device contains a *ID3D11Device.
    62  	Device unsafe.Pointer
    63  }
    64  
    65  type Metal struct {
    66  	// Device is an MTLDevice.
    67  	Device uintptr
    68  	// Queue is a MTLCommandQueue.
    69  	Queue uintptr
    70  	// PixelFormat is the MTLPixelFormat of the default framebuffer.
    71  	PixelFormat int
    72  }
    73  
    74  type Vulkan struct {
    75  	// PhysDevice is a VkPhysicalDevice.
    76  	PhysDevice unsafe.Pointer
    77  	// Device is a VkDevice.
    78  	Device unsafe.Pointer
    79  	// QueueFamily is the queue familily index of the queue.
    80  	QueueFamily int
    81  	// QueueIndex is the logical queue index of the queue.
    82  	QueueIndex int
    83  	// Format is a VkFormat that matches render targets.
    84  	Format int
    85  }
    86  
    87  // API specific device constructors.
    88  var (
    89  	NewOpenGLDevice     func(api OpenGL) (Device, error)
    90  	NewDirect3D11Device func(api Direct3D11) (Device, error)
    91  	NewMetalDevice      func(api Metal) (Device, error)
    92  	NewVulkanDevice     func(api Vulkan) (Device, error)
    93  )
    94  
    95  // NewDevice creates a new Device given the api.
    96  //
    97  // Note that the device does not assume ownership of the resources contained in
    98  // api; the caller must ensure the resources are valid until the device is
    99  // released.
   100  func NewDevice(api API) (Device, error) {
   101  	switch api := api.(type) {
   102  	case OpenGL:
   103  		if NewOpenGLDevice != nil {
   104  			return NewOpenGLDevice(api)
   105  		}
   106  	case Direct3D11:
   107  		if NewDirect3D11Device != nil {
   108  			return NewDirect3D11Device(api)
   109  		}
   110  	case Metal:
   111  		if NewMetalDevice != nil {
   112  			return NewMetalDevice(api)
   113  		}
   114  	case Vulkan:
   115  		if NewVulkanDevice != nil {
   116  			return NewVulkanDevice(api)
   117  		}
   118  	}
   119  	return nil, fmt.Errorf("driver: no driver available for the API %T", api)
   120  }
   121  
   122  func (OpenGL) implementsAPI()                          {}
   123  func (Direct3D11) implementsAPI()                      {}
   124  func (Metal) implementsAPI()                           {}
   125  func (Vulkan) implementsAPI()                          {}
   126  func (OpenGLRenderTarget) ImplementsRenderTarget()     {}
   127  func (Direct3D11RenderTarget) ImplementsRenderTarget() {}
   128  func (MetalRenderTarget) ImplementsRenderTarget()      {}
   129  func (VulkanRenderTarget) ImplementsRenderTarget()     {}