commit ab6a5b18c47ebefb5298b217c9806920310be085
parent 4736c0d1e91c254a52094dedd7afbfb2d90bf6e9
Author: Christian Ermann <christianermann@gmail.com>
Date: Thu, 8 May 2025 20:20:10 -0700
Clear screen
Diffstat:
8 files changed, 185 insertions(+), 2 deletions(-)
diff --git a/src/command_buffer.zig b/src/command_buffer.zig
@@ -0,0 +1,13 @@
+const c = @import("c.zig").c;
+const ChainedStruct = @import("common.zig").ChainedStruct;
+
+pub const CommandBuffer = opaque {
+ pub const Descriptor = extern struct {
+ next: ?*const ChainedStruct = null,
+ label: ?[*:0]const u8 = null,
+ };
+
+ pub fn release(buffer: *CommandBuffer) void {
+ c.wgpuCommandBufferRelease(@ptrCast(buffer));
+ }
+};
diff --git a/src/command_encoder.zig b/src/command_encoder.zig
@@ -0,0 +1,40 @@
+const c = @import("c.zig").c;
+const ChainedStruct = @import("common.zig").ChainedStruct;
+const CommandBuffer = @import("command_buffer.zig").CommandBuffer;
+const RenderPass = @import("render_pass.zig").RenderPass;
+const RenderPassEncoder = @import("render_pass.zig").RenderPassEncoder;
+
+pub const CommandEncoder = opaque {
+ pub const Descriptor = extern struct {
+ next: ?*const ChainedStruct = null,
+ label: ?[*:0]const u8 = null,
+ };
+
+ pub fn release(encoder: *CommandEncoder) void {
+ c.wgpuCommandEncoderRelease(@ptrCast(encoder));
+ }
+
+ pub fn beginRenderPass(
+ encoder: *CommandEncoder,
+ descriptor: *const RenderPass.Descriptor,
+ ) *RenderPassEncoder {
+ return @ptrCast(
+ c.wgpuCommandEncoderBeginRenderPass(
+ @ptrCast(encoder),
+ @ptrCast(descriptor),
+ ),
+ );
+ }
+
+ pub fn finish(
+ encoder: *CommandEncoder,
+ descriptor: *const CommandBuffer.Descriptor,
+ ) ?*CommandBuffer {
+ return @ptrCast(
+ c.wgpuCommandEncoderFinish(
+ @ptrCast(encoder),
+ @ptrCast(descriptor),
+ ),
+ );
+ }
+};
diff --git a/src/common.zig b/src/common.zig
@@ -20,3 +20,22 @@ pub const ChainedStruct = extern struct {
next: ?*const ChainedStruct,
struct_type: SType,
};
+
+pub const LoadOp = enum(u32) {
+ undefined = 0x00000000,
+ clear = 0x00000001,
+ load = 0x00000002,
+};
+
+pub const StoreOp = enum(u32) {
+ undefined = 0x00000000,
+ store = 0x00000001,
+ discard = 0x00000002,
+};
+
+pub const Color = extern struct {
+ r: f64,
+ g: f64,
+ b: f64,
+ a: f64,
+};
diff --git a/src/device.zig b/src/device.zig
@@ -2,6 +2,7 @@ const std = @import("std");
const c = @import("c.zig").c;
const ChainedStruct = @import("common.zig").ChainedStruct;
+const CommandEncoder = @import("command_encoder.zig").CommandEncoder;
const Queue = @import("queue.zig").Queue;
pub const FeatureName = enum(u32) {
@@ -112,6 +113,16 @@ pub const Device = opaque {
return @ptrCast(c.wgpuDeviceGetQueue(@ptrCast(device)).?);
}
+ pub fn createCommandEncoder(
+ device: *Device,
+ descriptor: *const CommandEncoder.Descriptor,
+ ) ?*CommandEncoder {
+ return @ptrCast(c.wgpuDeviceCreateCommandEncoder(
+ @ptrCast(device),
+ @ptrCast(descriptor),
+ ));
+ }
+
// createBindGroup
// createBindGroupLayout
// createBuffer
diff --git a/src/main.zig b/src/main.zig
@@ -5,6 +5,7 @@ const Instance = @import("instance.zig").Instance;
const Logging = @import("logging.zig");
const Device = @import("device.zig");
const Callback = @import("callback.zig").Callback;
+const CommandBuffer = @import("command_buffer.zig").CommandBuffer;
pub fn zgpu_log_callback(level: Logging.LogLevel, message: []const u8) void {
switch (level) {
@@ -132,5 +133,35 @@ pub fn main() !void {
.aspect = .all,
}).?;
defer view.release();
+ {
+ const encoder = device.createCommandEncoder(&.{}).?;
+ defer encoder.release();
+ {
+ const pass = encoder.beginRenderPass(&.{
+ .color_attachment_count = 1,
+ .color_attachments = &.{
+ .{
+ .view = view,
+ .depth_slice = 0,
+ .resolve_target = null,
+ .load_op = .clear,
+ .store_op = .store,
+ .clear_value = .{ .r = 1, .g = 1, .b = 1, .a = 1 },
+ },
+ },
+ .depth_stencil_attachment = null,
+ .occlusion_query_set = null,
+ .timestamp_writes = null,
+ });
+ defer pass.release();
+ defer pass.end();
+ }
+ {
+ var command = encoder.finish(&.{}).?;
+ defer command.release();
+ queue.submit(&[_]*CommandBuffer{command});
+ }
+ }
+ surface.present();
}
}
diff --git a/src/queue.zig b/src/queue.zig
@@ -1,5 +1,6 @@
const c = @import("c.zig").c;
const ChainedStruct = @import("common.zig").ChainedStruct;
+const CommandBuffer = @import("command_buffer.zig").CommandBuffer;
pub const Queue = opaque {
pub const Descriptor = extern struct {
@@ -11,9 +12,16 @@ pub const Queue = opaque {
c.wgpuQueueRelease(@ptrCast(queue));
}
+ pub fn submit(queue: *Queue, commands: []const *CommandBuffer) void {
+ c.wgpuQueueSubmit(
+ @ptrCast(queue),
+ @intCast(commands.len),
+ @ptrCast(commands.ptr),
+ );
+ }
+
// onSubmittedWorkDone
// setLabel
- // submit
// writeBuffer
// writeTexture
// reference
diff --git a/src/render_pass.zig b/src/render_pass.zig
@@ -0,0 +1,58 @@
+const c = @import("c.zig").c;
+const ChainedStruct = @import("common.zig").ChainedStruct;
+const TextureView = @import("texture_view.zig").TextureView;
+const LoadOp = @import("common.zig").LoadOp;
+const StoreOp = @import("common.zig").StoreOp;
+const Color = @import("common.zig").Color;
+
+pub const QuerySet = opaque {};
+
+pub const RenderPass = opaque {
+ pub const Descriptor = extern struct {
+ next: ?*ChainedStruct = null,
+ label: ?[*:0]const u8 = null,
+ color_attachment_count: u64,
+ color_attachments: ?[*]const ColorAttachment,
+ depth_stencil_attachment: ?*const DepthStencilAttachment,
+ occlusion_query_set: ?*const QuerySet,
+ timestamp_writes: ?*const TimestampWrites,
+ };
+
+ pub const ColorAttachment = extern struct {
+ next: ?*ChainedStruct = null,
+ view: ?*TextureView,
+ depth_slice: u32,
+ resolve_target: ?*TextureView,
+ load_op: LoadOp,
+ store_op: StoreOp,
+ clear_value: Color,
+ };
+
+ pub const DepthStencilAttachment = extern struct {
+ view: *TextureView,
+ depth_load_op: LoadOp,
+ depth_store_op: StoreOp,
+ depth_clear_value: f32,
+ depth_read_only: bool,
+ stencil_load_op: LoadOp,
+ stencil_store_op: StoreOp,
+ stencil_clear_value: u32,
+ stencil_read_only: bool,
+ };
+
+ pub const TimestampWrites = extern struct {
+ query_set: ?*QuerySet,
+ beginning_of_pass_write_index: u32,
+ end_of_pass_write_index: u32,
+ };
+};
+
+pub const RenderPassEncoder = opaque {
+ pub fn release(encoder: *RenderPassEncoder) void {
+ c.wgpuRenderPassEncoderRelease(@ptrCast(encoder));
+ }
+
+ pub fn end(encoder: *RenderPassEncoder) void {
+ c.wgpuRenderPassEncoderEnd(@ptrCast(encoder));
+ }
+};
diff --git a/src/surface.zig b/src/surface.zig
@@ -148,8 +148,11 @@ pub const Surface = opaque {
return surface_texture;
}
+ pub fn present(surface: *Surface) void {
+ c.wgpuSurfacePresent(@ptrCast(surface));
+ }
+
// getCapabilities(...)
- // present(...)
// setLabel(...)
// unconfigure(...)
// reference(...)