parent
							
								
									5774e2f239
								
							
						
					
					
						commit
						9fc7356a22
					
				
				 4 changed files with 125 additions and 21 deletions
			
			
		@ -1,17 +1,98 @@ | 
				
			|||||||
const std = @import("std"); | 
					const std = @import("std"); | 
				
			||||||
 | 
					const core = @import("mach-core"); | 
				
			||||||
 | 
					const gpu = core.gpu; | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn main() !void { | 
					pub const App = @This(); | 
				
			||||||
    std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); | 
					
 | 
				
			||||||
    const stdout_file = std.io.getStdOut().writer(); | 
					title_timer: core.Timer, | 
				
			||||||
    var bw = std.io.bufferedWriter(stdout_file); | 
					pipeline: *gpu.RenderPipeline, | 
				
			||||||
    const stdout = bw.writer(); | 
					
 | 
				
			||||||
    try stdout.print("Run `zig build test` to run the tests.\n", .{}); | 
					pub fn init(app: *App) !void { | 
				
			||||||
    try bw.flush(); | 
					    try core.init(.{}); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl")); | 
				
			||||||
 | 
					    defer shader_module.release(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const blend = gpu.BlendState{}; | 
				
			||||||
 | 
					    const color_target = gpu.ColorTargetState{ | 
				
			||||||
 | 
					        .format = core.descriptor.format, | 
				
			||||||
 | 
					        .blend = &blend, | 
				
			||||||
 | 
					        .write_mask = gpu.ColorWriteMaskFlags.all, | 
				
			||||||
 | 
					    }; | 
				
			||||||
 | 
					    const fragment = gpu.FragmentState.init(.{ | 
				
			||||||
 | 
					        .module = shader_module, | 
				
			||||||
 | 
					        .entry_point = "frag_main", | 
				
			||||||
 | 
					        .targets = &.{color_target}, | 
				
			||||||
 | 
					    }); | 
				
			||||||
 | 
					    const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ | 
				
			||||||
 | 
					        .vertex = gpu.VertexState{ | 
				
			||||||
 | 
					            .module = shader_module, | 
				
			||||||
 | 
					            .entry_point = "vertex_main", | 
				
			||||||
 | 
					        }, | 
				
			||||||
 | 
					        .fragment = &fragment, | 
				
			||||||
 | 
					    }; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    app.title_timer = try core.Timer.start(); | 
				
			||||||
 | 
					    app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor); | 
				
			||||||
} | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
test "simple test" { | 
					pub fn deinit(app: *App) void { | 
				
			||||||
    var list = std.ArrayList(i32).init(std.testing.allocator); | 
					    defer core.deinit(); | 
				
			||||||
    defer list.deinit(); | 
					    defer app.pipeline.release(); | 
				
			||||||
    try list.append(42); | 
					} | 
				
			||||||
    try std.testing.expectEqual(@as(i32, 42), list.pop()); | 
					
 | 
				
			||||||
 | 
					pub fn update(app: *App) !bool { | 
				
			||||||
 | 
					    var iter = core.pollEvents(); | 
				
			||||||
 | 
					    while (iter.next()) |event| { | 
				
			||||||
 | 
					        switch (event) { | 
				
			||||||
 | 
					            .close => return true, | 
				
			||||||
 | 
					            else => {}, | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Get back buffer texture to render to. | 
				
			||||||
 | 
					    const back_buffer_view = core.swap_chain.getCurrentTextureView().?; | 
				
			||||||
 | 
					    defer back_buffer_view.release(); | 
				
			||||||
 | 
					    // Once rendering is done (hence `defer`), swap back buffer to the front to display. | 
				
			||||||
 | 
					    defer core.swap_chain.present(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const color_attachment = gpu.RenderPassColorAttachment{ | 
				
			||||||
 | 
					        .view = back_buffer_view, | 
				
			||||||
 | 
					        .clear_value = std.mem.zeroes(gpu.Color), | 
				
			||||||
 | 
					        .load_op = .clear, | 
				
			||||||
 | 
					        .store_op = .store, | 
				
			||||||
 | 
					    }; | 
				
			||||||
 | 
					    const render_pass_info = gpu.RenderPassDescriptor.init(.{ | 
				
			||||||
 | 
					        .color_attachments = &.{color_attachment}, | 
				
			||||||
 | 
					    }); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Create a `WGPUCommandEncoder` which provides an interface for recording GPU commands. | 
				
			||||||
 | 
					    const encoder = core.device.createCommandEncoder(null); | 
				
			||||||
 | 
					    defer encoder.release(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    { | 
				
			||||||
 | 
					        const pass = encoder.beginRenderPass(&render_pass_info); | 
				
			||||||
 | 
					        defer pass.release(); | 
				
			||||||
 | 
					        defer pass.end(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pass.setPipeline(app.pipeline); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Draw a triangle with the help of a specialized shader. | 
				
			||||||
 | 
					        pass.draw(3, 1, 0, 0); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Finish recording commands, creating a `WGPUCommandBuffer`. | 
				
			||||||
 | 
					    var command = encoder.finish(null); | 
				
			||||||
 | 
					    defer command.release(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Submit the command(s) to the GPU. | 
				
			||||||
 | 
					    core.queue.submit(&.{command}); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Update the window title to show FPS and input frequency. | 
				
			||||||
 | 
					    if (app.title_timer.read() >= 1.0) { | 
				
			||||||
 | 
					        app.title_timer.reset(); | 
				
			||||||
 | 
					        try core.printTitle("Triangle [ {d}fps ] [ Input {d}hz ]", .{ core.frameRate(), core.inputRate() }); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return false; | 
				
			||||||
} | 
					} | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,14 @@ | 
				
			|||||||
 | 
					@vertex fn vertex_main( | 
				
			||||||
 | 
					    @builtin(vertex_index) index: u32 | 
				
			||||||
 | 
					) -> @builtin(position) vec4<f32> { | 
				
			||||||
 | 
					    let pos = array<vec2<f32>, 3>( | 
				
			||||||
 | 
					        vec2<f32>( 0.0,  0.5), | 
				
			||||||
 | 
					        vec2<f32>(-0.5, -0.5), | 
				
			||||||
 | 
					        vec2<f32>( 0.5, -0.5) | 
				
			||||||
 | 
					    ); | 
				
			||||||
 | 
					    return vec4<f32>(pos[index], 0.0, 1.0); | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@fragment fn frag_main() -> @location(0) vec4<f32> { | 
				
			||||||
 | 
					    return vec4<f32>(1.0, 0.0, 0.0, 1.0); | 
				
			||||||
 | 
					} | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue