-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtriangle.zig
More file actions
174 lines (147 loc) · 6.23 KB
/
triangle.zig
File metadata and controls
174 lines (147 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const std = @import("std");
const wgpu = @import("wgpu");
const bmp = @import("bmp");
const output_extent = wgpu.Extent3D {
.width = 640,
.height = 480,
.depth_or_array_layers = 1,
};
const output_bytes_per_row = 4 * output_extent.width;
const output_size = output_bytes_per_row * output_extent.height;
fn handleBufferMap(status: wgpu.MapAsyncStatus, _: wgpu.StringView, userdata1: ?*anyopaque, _: ?*anyopaque) callconv(.c) void {
std.log.info("buffer_map status={x:.8}\n", .{@intFromEnum(status)});
const complete: *bool = @ptrCast(@alignCast(userdata1));
complete.* = true;
}
// Based off of headless triangle example from https://github.com/eliemichel/LearnWebGPU-Code/tree/step030-headless
pub fn main() !void {
const instance = wgpu.Instance.create(null).?;
defer instance.release();
const adapter_request = instance.requestAdapterSync(&wgpu.RequestAdapterOptions {}, 0);
const adapter = switch(adapter_request.status) {
.success => adapter_request.adapter.?,
else => return error.NoAdapter,
};
defer adapter.release();
const device_request = adapter.requestDeviceSync(instance, &wgpu.DeviceDescriptor {
.required_limits = null,
}, 0);
const device = switch(device_request.status) {
.success => device_request.device.?,
else => return error.NoDevice,
};
defer device.release();
const queue = device.getQueue().?;
defer queue.release();
const swap_chain_format = wgpu.TextureFormat.bgra8_unorm_srgb;
const target_texture = device.createTexture(&wgpu.TextureDescriptor {
.label = wgpu.StringView.fromSlice("Render texture"),
.size = output_extent,
.format = swap_chain_format,
.usage = wgpu.TextureUsages.render_attachment | wgpu.TextureUsages.copy_src,
}).?;
defer target_texture.release();
const target_texture_view = target_texture.createView(&wgpu.TextureViewDescriptor {
.label = wgpu.StringView.fromSlice("Render texture view"),
.mip_level_count = 1,
.array_layer_count = 1,
}).?;
const shader_module = device.createShaderModule(&wgpu.shaderModuleWGSLDescriptor(.{
.code = @embedFile("./shader.wgsl"),
})).?;
defer shader_module.release();
const staging_buffer = device.createBuffer(&wgpu.BufferDescriptor {
.label = wgpu.StringView.fromSlice("staging_buffer"),
.usage = wgpu.BufferUsages.map_read | wgpu.BufferUsages.copy_dst,
.size = output_size,
.mapped_at_creation = @as(u32, @intFromBool(false)),
}).?;
defer staging_buffer.release();
const color_targets = &[_] wgpu.ColorTargetState{
wgpu.ColorTargetState {
.format = swap_chain_format,
.blend = &wgpu.BlendState {
.color = wgpu.BlendComponent {
.operation = .add,
.src_factor = .src_alpha,
.dst_factor = .one_minus_src_alpha,
},
.alpha = wgpu.BlendComponent {
.operation = .add,
.src_factor = .zero,
.dst_factor = .one,
},
},
},
};
const pipeline = device.createRenderPipeline(&wgpu.RenderPipelineDescriptor {
.vertex = wgpu.VertexState {
.module = shader_module,
.entry_point = wgpu.StringView.fromSlice("vs_main"),
},
.primitive = wgpu.PrimitiveState {},
.fragment = &wgpu.FragmentState {
.module = shader_module,
.entry_point = wgpu.StringView.fromSlice("fs_main"),
.target_count = color_targets.len,
.targets = color_targets.ptr
},
.multisample = wgpu.MultisampleState {},
}).?;
defer pipeline.release();
{ // Mock main "loop"
const next_texture = target_texture_view;
const encoder = device.createCommandEncoder(&wgpu.CommandEncoderDescriptor {
.label = wgpu.StringView.fromSlice("Command Encoder"),
}).?;
defer encoder.release();
const color_attachments = &[_]wgpu.ColorAttachment{
wgpu.ColorAttachment {
.view = next_texture,
.clear_value = wgpu.Color {},
}
};
const render_pass = encoder.beginRenderPass(&wgpu.RenderPassDescriptor {
.color_attachment_count = color_attachments.len,
.color_attachments = color_attachments.ptr,
}).?;
render_pass.setPipeline(pipeline);
render_pass.draw(3, 1, 0, 0);
render_pass.end();
// The render pass has to be released after .end() or otherwise we'll crash on queue.submit
// https://github.com/gfx-rs/wgpu-native/issues/412#issuecomment-2311719154
render_pass.release();
defer next_texture.release();
const img_copy_src = wgpu.TexelCopyTextureInfo {
.origin = wgpu.Origin3D {},
.texture = target_texture,
};
const img_copy_dst = wgpu.TexelCopyBufferInfo {
.layout = wgpu.TexelCopyBufferLayout {
.bytes_per_row = output_bytes_per_row,
.rows_per_image = output_extent.height,
},
.buffer = staging_buffer,
};
encoder.copyTextureToBuffer(&img_copy_src, &img_copy_dst, &output_extent);
const command_buffer = encoder.finish(&wgpu.CommandBufferDescriptor {
.label = wgpu.StringView.fromSlice("Command Buffer"),
}).?;
defer command_buffer.release();
queue.submit(&[_]*const wgpu.CommandBuffer{command_buffer});
var buffer_map_complete = false;
_ = staging_buffer.mapAsync(wgpu.MapModes.read, 0, output_size, wgpu.BufferMapCallbackInfo {
.callback = handleBufferMap,
.userdata1 = @ptrCast(&buffer_map_complete),
});
instance.processEvents();
while(!buffer_map_complete) {
instance.processEvents();
}
// _ = device.poll(true, null);
const buf: [*]u8 = @ptrCast(@alignCast(staging_buffer.getMappedRange(0, output_size).?));
defer staging_buffer.unmap();
const output = buf[0..output_size];
try bmp.write24BitBMP("examples/output/triangle.bmp", output_extent.width, output_extent.height, output);
}
}