commit 4736c0d1e91c254a52094dedd7afbfb2d90bf6e9
parent 90fe75cd89b391655ad09203d6bf3e9e5752a2ad
Author: Christian Ermann <christianermann@gmail.com>
Date: Thu, 8 May 2025 20:14:44 -0700
Get and release view to current surface texture
Diffstat:
4 files changed, 81 insertions(+), 1 deletion(-)
diff --git a/src/main.zig b/src/main.zig
@@ -120,5 +120,17 @@ pub fn main() !void {
while (!window.shouldClose()) {
glfw.pollEvents();
+ const current_texture = surface.getCurrentTexture();
+ const texture = current_texture.texture;
+ const view = texture.createView(&.{
+ .format = .bgra8_unorm,
+ .dimension = .@"2d",
+ .base_mip_level = 0,
+ .mip_level_count = 1,
+ .base_array_layer = 0,
+ .array_layer_count = 1,
+ .aspect = .all,
+ }).?;
+ defer view.release();
}
}
diff --git a/src/surface.zig b/src/surface.zig
@@ -116,6 +116,21 @@ pub const Surface = opaque {
present_mode: PresentMode,
};
+ pub const GetCurrentTextureStatus = enum(u32) {
+ success = 0x00000000,
+ timeout = 0x00000001,
+ outdated = 0x00000002,
+ lost = 0x00000003,
+ out_of_memory = 0x00000004,
+ device_lost = 0x00000005,
+ };
+
+ pub const Texture = extern struct {
+ texture: *texture.Texture,
+ suboptimal: bool,
+ status: GetCurrentTextureStatus,
+ };
+
pub fn release(surface: *Surface) void {
c.wgpuSurfaceRelease(@ptrCast(surface));
}
@@ -124,8 +139,16 @@ pub const Surface = opaque {
c.wgpuSurfaceConfigure(@ptrCast(surface), @ptrCast(config));
}
+ pub fn getCurrentTexture(surface: *Surface) Surface.Texture {
+ var surface_texture: Surface.Texture = undefined;
+ c.wgpuSurfaceGetCurrentTexture(
+ @ptrCast(surface),
+ @ptrCast(&surface_texture),
+ );
+ return surface_texture;
+ }
+
// getCapabilities(...)
- // getCurrentTexture(...)
// present(...)
// setLabel(...)
// unconfigure(...)
diff --git a/src/texture.zig b/src/texture.zig
@@ -1,4 +1,5 @@
const c = @import("c.zig").c;
+const TextureView = @import("texture_view.zig").TextureView;
pub const Texture = opaque {
pub const Aspect = enum(u32) {
@@ -114,4 +115,17 @@ pub const Texture = opaque {
render_attachment: bool = false,
_padding: u27 = 0,
};
+
+ pub fn createView(
+ texture: *Texture,
+ descriptor: *const TextureView.Descriptor,
+ ) ?*TextureView {
+ return @ptrCast(
+ c.wgpuTextureCreateView(@ptrCast(texture), @ptrCast(descriptor)),
+ );
+ }
+
+ pub fn release(texture: *Texture) void {
+ c.wgpuTextureRelease(@ptrCast(texture));
+ }
};
diff --git a/src/texture_view.zig b/src/texture_view.zig
@@ -0,0 +1,31 @@
+const c = @import("c.zig").c;
+const ChainedStruct = @import("common.zig").ChainedStruct;
+const Texture = @import("texture.zig").Texture;
+
+pub const TextureView = opaque {
+ pub const Descriptor = extern struct {
+ next: ?*const ChainedStruct = null,
+ label: ?[*:0]const u8 = null,
+ format: Texture.Format,
+ dimension: Dimension,
+ base_mip_level: u32,
+ mip_level_count: u32,
+ base_array_layer: u32,
+ array_layer_count: u32,
+ aspect: Texture.Aspect,
+ };
+
+ pub const Dimension = enum(u32) {
+ undefined = 0x00000000,
+ @"1d" = 0x00000001,
+ @"2d" = 0x00000002,
+ @"2d_array" = 0x00000003,
+ cube = 0x00000004,
+ cube_array = 0x00000005,
+ @"3d" = 0x00000006,
+ };
+
+ pub fn release(view: *TextureView) void {
+ c.wgpuTextureViewRelease(@ptrCast(view));
+ }
+};