gpu: implement Device.nativeCreateSwapChain, SwapChain

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-03-08 23:15:08 -07:00 committed by Stephen Gutekanst
parent 738e9da8ee
commit e9ac31f264
6 changed files with 112 additions and 143 deletions

View file

@ -10,6 +10,8 @@ const FeatureName = @import("feature_name.zig").FeatureName;
const Limits = @import("Limits.zig");
const Queue = @import("Queue.zig");
const ShaderModule = @import("ShaderModule.zig");
const Surface = @import("Surface.zig");
const SwapChain = @import("SwapChain.zig");
const Device = @This();
@ -35,7 +37,7 @@ pub const VTable = struct {
// WGPU_EXPORT void wgpuDeviceCreateRenderPipelineAsync(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor, WGPUCreateRenderPipelineAsyncCallback callback, void * userdata);
// WGPU_EXPORT WGPUSampler wgpuDeviceCreateSampler(WGPUDevice device, WGPUSamplerDescriptor const * descriptor);
createShaderModule: fn (ptr: *anyopaque, descriptor: *const ShaderModule.Descriptor) ShaderModule,
// WGPU_EXPORT WGPUSwapChain wgpuDeviceCreateSwapChain(WGPUDevice device, WGPUSurface surface, WGPUSwapChainDescriptor const * descriptor);
nativeCreateSwapChain: fn (ptr: *anyopaque, surface: Surface, descriptor: SwapChain.Descriptor) SwapChain,
// WGPU_EXPORT WGPUTexture wgpuDeviceCreateTexture(WGPUDevice device, WGPUTextureDescriptor const * descriptor);
// WGPU_EXPORT void wgpuDeviceDestroy(WGPUDevice device);
// WGPU_EXPORT size_t wgpuDeviceEnumerateFeatures(WGPUDevice device, WGPUFeatureName * features);
@ -71,6 +73,10 @@ pub inline fn createShaderModule(device: Device, descriptor: *const ShaderModule
return device.vtable.createShaderModule(device.ptr, descriptor);
}
pub inline fn nativeCreateSwapChain(device: Device, surface: Surface, descriptor: SwapChain.Descriptor) SwapChain {
return device.vtable.nativeCreateSwapChain(device.ptr, surface, descriptor);
}
// TODO: docs
pub const Descriptor = struct {
label: ?[]const u8 = null,
@ -85,4 +91,5 @@ test "syntax" {
_ = release;
_ = createShaderModule;
_ = Descriptor;
_ = nativeCreateSwapChain;
}

View file

@ -19,6 +19,11 @@ const Limits = @import("Limits.zig");
const Queue = @import("Queue.zig");
const CommandBuffer = @import("CommandBuffer.zig");
const ShaderModule = @import("ShaderModule.zig");
const SwapChain = @import("SwapChain.zig");
const TextureUsage = @import("texture_usage.zig").TextureUsage;
const TextureFormat = @import("texture_format.zig").TextureFormat;
const PresentMode = @import("present_mode.zig").PresentMode;
const NativeInstance = @This();
@ -337,6 +342,25 @@ const device_vtable = Device.VTable{
}
}
}).createShaderModule,
.nativeCreateSwapChain = (struct {
pub fn nativeCreateSwapChain(ptr: *anyopaque, surface: Surface, descriptor: SwapChain.Descriptor) SwapChain {
const desc = c.WGPUSwapChainDescriptor{
.nextInChain = null,
.label = if (descriptor.label) |l| @ptrCast([*c]const u8, l) else null,
.usage = @enumToInt(descriptor.usage),
.format = @enumToInt(descriptor.format),
.width = descriptor.width,
.height = descriptor.height,
.presentMode = @enumToInt(descriptor.present_mode),
.implementation = descriptor.implementation,
};
return wrapSwapChain(c.wgpuDeviceCreateSwapChain(
@ptrCast(c.WGPUDevice, ptr),
@ptrCast(c.WGPUSurface, surface.ptr),
&desc,
));
}
}).nativeCreateSwapChain,
};
// TODO: maybe make Limits an extern struct that can be cast?
@ -442,6 +466,26 @@ const shader_module_vtable = ShaderModule.VTable{
}).release,
};
fn wrapSwapChain(swap_chain: c.WGPUSwapChain) SwapChain {
return .{
.ptr = swap_chain.?,
.vtable = &swap_chain_vtable,
};
}
const swap_chain_vtable = SwapChain.VTable{
.reference = (struct {
pub fn reference(ptr: *anyopaque) void {
c.wgpuSwapChainReference(@ptrCast(c.WGPUSwapChain, ptr));
}
}).reference,
.release = (struct {
pub fn release(ptr: *anyopaque) void {
c.wgpuSwapChainRelease(@ptrCast(c.WGPUSwapChain, ptr));
}
}).release,
};
test "syntax" {
_ = wrap;
_ = interface_vtable;
@ -454,4 +498,5 @@ test "syntax" {
_ = convertLimits;
_ = wrapQueue;
_ = wrapShaderModule;
_ = wrapSwapChain;
}

View file

@ -8,6 +8,9 @@ vtable: *const VTable,
pub const VTable = struct {
reference: fn (ptr: *anyopaque) void,
release: fn (ptr: *anyopaque) void,
// TODO:
// WGPU_EXPORT void wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, WGPUCompilationInfoCallback callback, void * userdata);
// WGPU_EXPORT void wgpuShaderModuleSetLabel(WGPUShaderModule shaderModule, char const * label);
};
pub inline fn reference(queue: ShaderModule) void {
@ -31,6 +34,10 @@ pub const Descriptor = struct {
},
};
// // Methods of ShaderModule
// WGPU_EXPORT void wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, WGPUCompilationInfoCallback callback, void * userdata);
// WGPU_EXPORT void wgpuShaderModuleSetLabel(WGPUShaderModule shaderModule, char const * label);
test "syntax" {
_ = VTable;
_ = reference;
_ = release;
_ = CodeTag;
_ = Descriptor;
}

44
gpu/src/SwapChain.zig Normal file
View file

@ -0,0 +1,44 @@
const TextureUsage = @import("texture_usage.zig").TextureUsage;
const TextureFormat = @import("texture_format.zig").TextureFormat;
const PresentMode = @import("present_mode.zig").PresentMode;
const SwapChain = @This();
/// The type erased pointer to the SwapChain implementation
/// Equal to c.WGPUSwapChain for NativeInstance.
ptr: *anyopaque,
vtable: *const VTable,
pub const VTable = struct {
reference: fn (ptr: *anyopaque) void,
release: fn (ptr: *anyopaque) void,
// TODO:
// WGPU_EXPORT void wgpuSwapChainConfigure(WGPUSwapChain swapChain, WGPUTextureFormat format, WGPUTextureUsageFlags allowedUsage, uint32_t width, uint32_t height);
// WGPU_EXPORT WGPUTextureView wgpuSwapChainGetCurrentTextureView(WGPUSwapChain swapChain);
// WGPU_EXPORT void wgpuSwapChainPresent(WGPUSwapChain swapChain);
};
pub inline fn reference(swap_chain: SwapChain) void {
swap_chain.vtable.reference(swap_chain.ptr);
}
pub inline fn release(swap_chain: SwapChain) void {
swap_chain.vtable.release(swap_chain.ptr);
}
pub const Descriptor = struct {
label: ?[]const u8 = null,
usage: TextureUsage,
format: TextureFormat,
width: u32,
height: u32,
present_mode: PresentMode,
implementation: u64,
};
test "syntax" {
_ = VTable;
_ = reference;
_ = release;
_ = Descriptor;
}

View file

@ -24,7 +24,6 @@ typedef struct WGPURenderBundleEncoderImpl* WGPURenderBundleEncoder;
typedef struct WGPURenderPassEncoderImpl* WGPURenderPassEncoder;
typedef struct WGPURenderPipelineImpl* WGPURenderPipeline;
typedef struct WGPUSamplerImpl* WGPUSampler;
typedef struct WGPUSwapChainImpl* WGPUSwapChain;
typedef struct WGPUTextureImpl* WGPUTexture;
typedef struct WGPUTextureViewImpl* WGPUTextureView;
@ -230,13 +229,6 @@ typedef enum WGPUPredefinedColorSpace {
WGPUPredefinedColorSpace_Force32 = 0x7FFFFFFF
} WGPUPredefinedColorSpace;
typedef enum WGPUPresentMode {
WGPUPresentMode_Immediate = 0x00000000,
WGPUPresentMode_Mailbox = 0x00000001,
WGPUPresentMode_Fifo = 0x00000002,
WGPUPresentMode_Force32 = 0x7FFFFFFF
} WGPUPresentMode;
typedef enum WGPUPrimitiveTopology {
WGPUPrimitiveTopology_PointList = 0x00000000,
WGPUPrimitiveTopology_LineList = 0x00000001,
@ -329,107 +321,6 @@ typedef enum WGPUTextureDimension {
WGPUTextureDimension_Force32 = 0x7FFFFFFF
} WGPUTextureDimension;
typedef enum WGPUTextureFormat {
WGPUTextureFormat_Undefined = 0x00000000,
WGPUTextureFormat_R8Unorm = 0x00000001,
WGPUTextureFormat_R8Snorm = 0x00000002,
WGPUTextureFormat_R8Uint = 0x00000003,
WGPUTextureFormat_R8Sint = 0x00000004,
WGPUTextureFormat_R16Uint = 0x00000005,
WGPUTextureFormat_R16Sint = 0x00000006,
WGPUTextureFormat_R16Float = 0x00000007,
WGPUTextureFormat_RG8Unorm = 0x00000008,
WGPUTextureFormat_RG8Snorm = 0x00000009,
WGPUTextureFormat_RG8Uint = 0x0000000A,
WGPUTextureFormat_RG8Sint = 0x0000000B,
WGPUTextureFormat_R32Float = 0x0000000C,
WGPUTextureFormat_R32Uint = 0x0000000D,
WGPUTextureFormat_R32Sint = 0x0000000E,
WGPUTextureFormat_RG16Uint = 0x0000000F,
WGPUTextureFormat_RG16Sint = 0x00000010,
WGPUTextureFormat_RG16Float = 0x00000011,
WGPUTextureFormat_RGBA8Unorm = 0x00000012,
WGPUTextureFormat_RGBA8UnormSrgb = 0x00000013,
WGPUTextureFormat_RGBA8Snorm = 0x00000014,
WGPUTextureFormat_RGBA8Uint = 0x00000015,
WGPUTextureFormat_RGBA8Sint = 0x00000016,
WGPUTextureFormat_BGRA8Unorm = 0x00000017,
WGPUTextureFormat_BGRA8UnormSrgb = 0x00000018,
WGPUTextureFormat_RGB10A2Unorm = 0x00000019,
WGPUTextureFormat_RG11B10Ufloat = 0x0000001A,
WGPUTextureFormat_RGB9E5Ufloat = 0x0000001B,
WGPUTextureFormat_RG32Float = 0x0000001C,
WGPUTextureFormat_RG32Uint = 0x0000001D,
WGPUTextureFormat_RG32Sint = 0x0000001E,
WGPUTextureFormat_RGBA16Uint = 0x0000001F,
WGPUTextureFormat_RGBA16Sint = 0x00000020,
WGPUTextureFormat_RGBA16Float = 0x00000021,
WGPUTextureFormat_RGBA32Float = 0x00000022,
WGPUTextureFormat_RGBA32Uint = 0x00000023,
WGPUTextureFormat_RGBA32Sint = 0x00000024,
WGPUTextureFormat_Stencil8 = 0x00000025,
WGPUTextureFormat_Depth16Unorm = 0x00000026,
WGPUTextureFormat_Depth24Plus = 0x00000027,
WGPUTextureFormat_Depth24PlusStencil8 = 0x00000028,
WGPUTextureFormat_Depth24UnormStencil8 = 0x00000029,
WGPUTextureFormat_Depth32Float = 0x0000002A,
WGPUTextureFormat_Depth32FloatStencil8 = 0x0000002B,
WGPUTextureFormat_BC1RGBAUnorm = 0x0000002C,
WGPUTextureFormat_BC1RGBAUnormSrgb = 0x0000002D,
WGPUTextureFormat_BC2RGBAUnorm = 0x0000002E,
WGPUTextureFormat_BC2RGBAUnormSrgb = 0x0000002F,
WGPUTextureFormat_BC3RGBAUnorm = 0x00000030,
WGPUTextureFormat_BC3RGBAUnormSrgb = 0x00000031,
WGPUTextureFormat_BC4RUnorm = 0x00000032,
WGPUTextureFormat_BC4RSnorm = 0x00000033,
WGPUTextureFormat_BC5RGUnorm = 0x00000034,
WGPUTextureFormat_BC5RGSnorm = 0x00000035,
WGPUTextureFormat_BC6HRGBUfloat = 0x00000036,
WGPUTextureFormat_BC6HRGBFloat = 0x00000037,
WGPUTextureFormat_BC7RGBAUnorm = 0x00000038,
WGPUTextureFormat_BC7RGBAUnormSrgb = 0x00000039,
WGPUTextureFormat_ETC2RGB8Unorm = 0x0000003A,
WGPUTextureFormat_ETC2RGB8UnormSrgb = 0x0000003B,
WGPUTextureFormat_ETC2RGB8A1Unorm = 0x0000003C,
WGPUTextureFormat_ETC2RGB8A1UnormSrgb = 0x0000003D,
WGPUTextureFormat_ETC2RGBA8Unorm = 0x0000003E,
WGPUTextureFormat_ETC2RGBA8UnormSrgb = 0x0000003F,
WGPUTextureFormat_EACR11Unorm = 0x00000040,
WGPUTextureFormat_EACR11Snorm = 0x00000041,
WGPUTextureFormat_EACRG11Unorm = 0x00000042,
WGPUTextureFormat_EACRG11Snorm = 0x00000043,
WGPUTextureFormat_ASTC4x4Unorm = 0x00000044,
WGPUTextureFormat_ASTC4x4UnormSrgb = 0x00000045,
WGPUTextureFormat_ASTC5x4Unorm = 0x00000046,
WGPUTextureFormat_ASTC5x4UnormSrgb = 0x00000047,
WGPUTextureFormat_ASTC5x5Unorm = 0x00000048,
WGPUTextureFormat_ASTC5x5UnormSrgb = 0x00000049,
WGPUTextureFormat_ASTC6x5Unorm = 0x0000004A,
WGPUTextureFormat_ASTC6x5UnormSrgb = 0x0000004B,
WGPUTextureFormat_ASTC6x6Unorm = 0x0000004C,
WGPUTextureFormat_ASTC6x6UnormSrgb = 0x0000004D,
WGPUTextureFormat_ASTC8x5Unorm = 0x0000004E,
WGPUTextureFormat_ASTC8x5UnormSrgb = 0x0000004F,
WGPUTextureFormat_ASTC8x6Unorm = 0x00000050,
WGPUTextureFormat_ASTC8x6UnormSrgb = 0x00000051,
WGPUTextureFormat_ASTC8x8Unorm = 0x00000052,
WGPUTextureFormat_ASTC8x8UnormSrgb = 0x00000053,
WGPUTextureFormat_ASTC10x5Unorm = 0x00000054,
WGPUTextureFormat_ASTC10x5UnormSrgb = 0x00000055,
WGPUTextureFormat_ASTC10x6Unorm = 0x00000056,
WGPUTextureFormat_ASTC10x6UnormSrgb = 0x00000057,
WGPUTextureFormat_ASTC10x8Unorm = 0x00000058,
WGPUTextureFormat_ASTC10x8UnormSrgb = 0x00000059,
WGPUTextureFormat_ASTC10x10Unorm = 0x0000005A,
WGPUTextureFormat_ASTC10x10UnormSrgb = 0x0000005B,
WGPUTextureFormat_ASTC12x10Unorm = 0x0000005C,
WGPUTextureFormat_ASTC12x10UnormSrgb = 0x0000005D,
WGPUTextureFormat_ASTC12x12Unorm = 0x0000005E,
WGPUTextureFormat_ASTC12x12UnormSrgb = 0x0000005F,
WGPUTextureFormat_R8BG8Biplanar420Unorm = 0x00000060,
WGPUTextureFormat_Force32 = 0x7FFFFFFF
} WGPUTextureFormat;
typedef enum WGPUTextureSampleType {
WGPUTextureSampleType_Undefined = 0x00000000,
WGPUTextureSampleType_Float = 0x00000001,
@ -536,18 +427,6 @@ typedef enum WGPUShaderStage {
} WGPUShaderStage;
typedef WGPUFlags WGPUShaderStageFlags;
typedef enum WGPUTextureUsage {
WGPUTextureUsage_None = 0x00000000,
WGPUTextureUsage_CopySrc = 0x00000001,
WGPUTextureUsage_CopyDst = 0x00000002,
WGPUTextureUsage_TextureBinding = 0x00000004,
WGPUTextureUsage_StorageBinding = 0x00000008,
WGPUTextureUsage_RenderAttachment = 0x00000010,
WGPUTextureUsage_Present = 0x00000020,
WGPUTextureUsage_Force32 = 0x7FFFFFFF
} WGPUTextureUsage;
typedef WGPUFlags WGPUTextureUsageFlags;
typedef struct WGPUChainedStruct {
struct WGPUChainedStruct const * next;
WGPUSType sType;
@ -803,17 +682,6 @@ typedef struct WGPUStorageTextureBindingLayout {
WGPUTextureViewDimension viewDimension;
} WGPUStorageTextureBindingLayout;
typedef struct WGPUSwapChainDescriptor {
WGPUChainedStruct const * nextInChain;
char const * label;
WGPUTextureUsageFlags usage;
WGPUTextureFormat format;
uint32_t width;
uint32_t height;
WGPUPresentMode presentMode;
uint64_t implementation;
} WGPUSwapChainDescriptor;
typedef struct WGPUTextureBindingLayout {
WGPUChainedStruct const * nextInChain;
WGPUTextureSampleType sampleType;
@ -1186,13 +1054,6 @@ WGPU_EXPORT void wgpuSamplerSetLabel(WGPUSampler sampler, char const * label);
WGPU_EXPORT void wgpuSamplerReference(WGPUSampler sampler);
WGPU_EXPORT void wgpuSamplerRelease(WGPUSampler sampler);
// Methods of SwapChain
WGPU_EXPORT void wgpuSwapChainConfigure(WGPUSwapChain swapChain, WGPUTextureFormat format, WGPUTextureUsageFlags allowedUsage, uint32_t width, uint32_t height);
WGPU_EXPORT WGPUTextureView wgpuSwapChainGetCurrentTextureView(WGPUSwapChain swapChain);
WGPU_EXPORT void wgpuSwapChainPresent(WGPUSwapChain swapChain);
WGPU_EXPORT void wgpuSwapChainReference(WGPUSwapChain swapChain);
WGPU_EXPORT void wgpuSwapChainRelease(WGPUSwapChain swapChain);
// Methods of Texture
WGPU_EXPORT WGPUTextureView wgpuTextureCreateView(WGPUTexture texture, WGPUTextureViewDescriptor const * descriptor);
WGPU_EXPORT void wgpuTextureDestroy(WGPUTexture texture);

View file

@ -34,8 +34,12 @@ pub const Limits = @import("Limits.zig");
pub const Queue = @import("Queue.zig");
pub const CommandBuffer = @import("CommandBuffer.zig");
pub const ShaderModule = @import("ShaderModule.zig");
pub const SwapChain = @import("SwapChain.zig");
pub const FeatureName = @import("feature_name.zig").FeatureName;
pub const TextureUsage = @import("texture_usage.zig").TextureUsage;
pub const TextureFormat = @import("texture_format.zig").TextureFormat;
pub const PresentMode = @import("present_mode.zig").PresentMode;
test "syntax" {
_ = Interface;
@ -48,6 +52,7 @@ test "syntax" {
_ = Queue;
_ = CommandBuffer;
_ = ShaderModule;
_ = SwapChain;
_ = FeatureName;
}