api.go 3.4 KB

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