zgpu

git clone git://git.electrosoup.com/zgpu
Log | Files | Refs | Submodules | README

surface.zig (4599B)


      1 const c = @import("c.zig").c;
      2 const ChainedStruct = @import("common.zig").ChainedStruct;
      3 const Device = @import("device.zig").Device;
      4 const Status = @import("common.zig").Status;
      5 const StringView = @import("common.zig").StringView;
      6 const texture = @import("texture.zig");
      7 
      8 const CompositeAlphaMode = enum(u32) {
      9     auto = 0x00000000,
     10     @"opaque" = 0x00000001,
     11     premultiplied = 0x00000002,
     12     unpremultiplied = 0x00000003,
     13     inherit = 0x00000004,
     14 };
     15 
     16 const PresentMode = enum(u32) {
     17     undefined = 0x00000000,
     18     fifo = 0x00000001,
     19     fifo_relaxed = 0x00000002,
     20     immediate = 0x00000003,
     21     mailbox = 0x00000004,
     22 };
     23 
     24 // WGPUSurface
     25 // WGPUSurfaceDescriptor
     26 // WGPUSurfaceDescriptorFromAndroidNativeWindow
     27 // WGPUSurfaceDescriptorFromCanvasHTMLSelector
     28 // WGPUSurfaceDescriptorFromMetalLayer
     29 // WGPUSurfaceDescriptorFromWaylandSurface
     30 // WGPUSurfaceDescriptorFromWindowsHWND
     31 // WGPUSurfaceDescriptorFromXcbWindow
     32 // WGPUSurfaceDescriptorFromXlibWindow
     33 pub const Surface = opaque {
     34     pub const Descriptor = extern struct {
     35         pub const Next = extern union {
     36             generic: ?*const ChainedStruct,
     37             from_android_native_window: *const SourceAndroidNativeWindow,
     38             from_metal_layer: *const SourceMetalLayer,
     39             from_wayland_surface: *const SourceWaylandSurface,
     40             from_windows_hwnd: *const SourceWindowsHWND,
     41             from_xcb_window: *const SourceXCBWindow,
     42             from_xlib_window: *const SourceXlibWindow,
     43         };
     44         next: Next = .{ .generic = null },
     45         label: StringView,
     46     };
     47 
     48     pub const SourceAndroidNativeWindow = extern struct {
     49         chain: ChainedStruct = .{
     50             .next = null,
     51             .struct_type = .surface_source_android_native_window,
     52         },
     53         window: *anyopaque,
     54     };
     55 
     56     pub const SourceMetalLayer = extern struct {
     57         chain: ChainedStruct = .{
     58             .next = null,
     59             .struct_type = .surface_source_metal_layer,
     60         },
     61         layer: *anyopaque,
     62     };
     63 
     64     pub const SourceWaylandSurface = extern struct {
     65         chain: ChainedStruct = .{
     66             .next = null,
     67             .struct_type = .surface_source_wayland_surface,
     68         },
     69         display: *anyopaque,
     70         surface: *anyopaque,
     71     };
     72 
     73     pub const SourceWindowsHWND = extern struct {
     74         chain: ChainedStruct = .{
     75             .next = null,
     76             .struct_type = .surface_source_windows_hwnd,
     77         },
     78         hinstance: *anyopaque,
     79         hwnd: *anyopaque,
     80     };
     81 
     82     pub const SourceXCBWindow = extern struct {
     83         chain: ChainedStruct = .{
     84             .next = null,
     85             .struct_type = .surface_source_xcb_window,
     86         },
     87         connection: *anyopaque,
     88         window: u32,
     89     };
     90 
     91     pub const SourceXlibWindow = extern struct {
     92         chain: ChainedStruct = .{
     93             .next = null,
     94             .struct_type = .surface_source_xlib_window,
     95         },
     96         display: *anyopaque,
     97         window: u64,
     98     };
     99 
    100     pub const Configuration = extern struct {
    101         next: ?*const ChainedStruct = null,
    102         device: *Device,
    103         format: texture.Texture.Format,
    104         usage: texture.Texture.Usage,
    105         width: u32,
    106         height: u32,
    107         view_format_count: usize,
    108         view_formats: ?[*]texture.Texture.Format,
    109         alpha_mode: CompositeAlphaMode,
    110         present_mode: PresentMode,
    111     };
    112 
    113     pub const GetCurrentTextureStatus = enum(u32) {
    114         success = 0x00000000,
    115         timeout = 0x00000001,
    116         outdated = 0x00000002,
    117         lost = 0x00000003,
    118         out_of_memory = 0x00000004,
    119         device_lost = 0x00000005,
    120     };
    121 
    122     pub const Texture = extern struct {
    123         next: ?*const ChainedStruct = null,
    124         texture: *texture.Texture,
    125         status: GetCurrentTextureStatus,
    126     };
    127 
    128     pub fn release(surface: *Surface) void {
    129         c.wgpuSurfaceRelease(@ptrCast(surface));
    130     }
    131 
    132     pub fn configure(surface: *Surface, config: *const Configuration) void {
    133         c.wgpuSurfaceConfigure(@ptrCast(surface), @ptrCast(config));
    134     }
    135 
    136     pub fn getCurrentTexture(surface: *Surface) Surface.Texture {
    137         var surface_texture: Surface.Texture = undefined;
    138         c.wgpuSurfaceGetCurrentTexture(
    139             @ptrCast(surface),
    140             @ptrCast(&surface_texture),
    141         );
    142         return surface_texture;
    143     }
    144 
    145     pub fn present(surface: *Surface) Status {
    146         const status = c.wgpuSurfacePresent(@ptrCast(surface));
    147         return @as(*const Status, @ptrCast(&status)).*;
    148     }
    149 
    150     // getCapabilities(...)
    151     // setLabel(...)
    152     // unconfigure(...)
    153     // reference(...)
    154 };