Working self contained rendering (simple circle)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const std = @import("std");
|
||||
const c = @import("utils.zig").c;
|
||||
const GpuAllocator = @import("GpuAllocator.zig");
|
||||
const GpuBuffer = @import("GpuBuffer.zig");
|
||||
const GpuTextureFormat = @import("lib.zig").GpuTextureFormat;
|
||||
const GpuTextureUsage = @import("lib.zig").GpuTextureUsage;
|
||||
|
||||
@@ -40,6 +41,45 @@ pub fn getConstMappedRange(self: @This(), offset: u64, size: u64) ?*const anyopa
|
||||
return c.wgpuBufferGetConstMappedRange(self.raw, offset, size);
|
||||
}
|
||||
|
||||
pub fn bytesSize(self: @This()) u32 {
|
||||
return self.bytesSizeRow() * self.def.size.height;
|
||||
}
|
||||
|
||||
pub fn bytesSizeRow(self: @This()) u32 {
|
||||
return self.def.size.width * self.def.format.bytesPerPixel();
|
||||
}
|
||||
|
||||
/// Return a GpuBuffer containing a copy of the texture.
|
||||
pub fn buffCopy(self: @This(), gloc: GpuAllocator) !GpuBuffer {
|
||||
const buf = try GpuBuffer.init(gloc, self.bytesSize(), .initMany(&.{ .CopyDst, .CopySrc }));
|
||||
|
||||
const enc = c.wgpuDeviceCreateCommandEncoder(gloc.device.device, null) orelse return error.Encoder;
|
||||
defer c.wgpuCommandEncoderRelease(enc);
|
||||
|
||||
const src_copy = c.WGPUTexelCopyTextureInfo{
|
||||
.texture = self.raw,
|
||||
.mipLevel = 0,
|
||||
.origin = .{ .x = 0, .y = 0, .z = 0 },
|
||||
.aspect = c.WGPUTextureAspect_All,
|
||||
};
|
||||
const dst_copy = c.WGPUTexelCopyBufferInfo{
|
||||
.buffer = buf.raw,
|
||||
.layout = .{
|
||||
.offset = 0,
|
||||
.bytesPerRow = self.bytesSizeRow(),
|
||||
.rowsPerImage = self.def.size.height,
|
||||
},
|
||||
};
|
||||
|
||||
c.wgpuCommandEncoderCopyTextureToBuffer(enc, &src_copy, &dst_copy, &self.def.size);
|
||||
|
||||
const cmd = c.wgpuCommandEncoderFinish(enc, null);
|
||||
defer c.wgpuCommandBufferRelease(cmd);
|
||||
c.wgpuQueueSubmit(gloc.device.queue, 1, &cmd);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
pub fn mapAsync(
|
||||
self: @This(),
|
||||
mode: c.WGPUMapMode,
|
||||
|
||||
-112
@@ -1,112 +0,0 @@
|
||||
const std = @import("std");
|
||||
const gpu = @import("lib.zig");
|
||||
const c = @import("utils.zig").c;
|
||||
const sv = @import("utils.zig").sv;
|
||||
const GpuDevice = gpu.GpuDevice;
|
||||
const GpuArena = gpu.GpuArena;
|
||||
const GpuBuffer = gpu.GpuBuffer;
|
||||
const GpuRender = gpu.GpuRender;
|
||||
const GpuTexture = gpu.GpuTexture;
|
||||
const GpuTextureView = gpu.GpuTextureView;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const allocator = init.gpa;
|
||||
|
||||
// 1. Open the raw headless GPU Device you shared
|
||||
const device = try GpuDevice.init(.{});
|
||||
defer device.deinit();
|
||||
|
||||
var grena = GpuArena.init(allocator, device);
|
||||
defer grena.deinit();
|
||||
const gloc = grena.gpuAllocator();
|
||||
|
||||
const width: u32 = 512;
|
||||
const height: u32 = 512;
|
||||
|
||||
// 2. Load our Render Pipeline (Procedural Triangle Strip)
|
||||
const circle_rp = try GpuRender.init(
|
||||
device,
|
||||
@embedFile("shaders/circle.wgsl"),
|
||||
.{
|
||||
.bindings = &.{},
|
||||
.texture_format = .RGBA8Unorm,
|
||||
.topology = .TriangleStrip,
|
||||
},
|
||||
);
|
||||
defer circle_rp.deinit();
|
||||
|
||||
// 3. Create the offscreen VRAM texture to render into
|
||||
const texture = try GpuTexture.init(gloc, .{
|
||||
.format = .RGBA8Unorm,
|
||||
.size = .{ .width = width, .height = height, .depthOrArrayLayers = 1 },
|
||||
.usage = .initMany(&.{ .RenderAttachment, .CopySrc }),
|
||||
});
|
||||
defer texture.deinit();
|
||||
|
||||
const view = try GpuTextureView.init(gloc, texture, .{});
|
||||
defer view.deinit();
|
||||
|
||||
// 4. Create a staging buffer to pull pixels from VRAM to CPU
|
||||
// 4 bytes per pixel (RGBA8)
|
||||
const row_bytes = width * 4;
|
||||
const buffer_bytes = row_bytes * height;
|
||||
|
||||
// Create a regular GpuBuffer set up to receive texture copy transfers
|
||||
const cpu_staging_buf = try GpuBuffer.init(gloc, buffer_bytes, .initMany(&.{ .CopyDst, .CopySrc }));
|
||||
|
||||
// 5. Draw the Circle Frame into the texture view!
|
||||
try circle_rp.draw(gloc, view, 4, .{});
|
||||
|
||||
// 6. Copy the texture data into our CPU staging buffer
|
||||
const enc = c.wgpuDeviceCreateCommandEncoder(device.device, null) orelse return error.Encoder;
|
||||
defer c.wgpuCommandEncoderRelease(enc);
|
||||
|
||||
const src_copy = c.WGPUTexelCopyTextureInfo{
|
||||
.texture = texture.raw,
|
||||
.mipLevel = 0,
|
||||
.origin = .{ .x = 0, .y = 0, .z = 0 },
|
||||
.aspect = c.WGPUTextureAspect_All,
|
||||
};
|
||||
const dst_copy = c.WGPUTexelCopyBufferInfo{
|
||||
.buffer = cpu_staging_buf.raw,
|
||||
.layout = .{
|
||||
.offset = 0,
|
||||
.bytesPerRow = row_bytes,
|
||||
.rowsPerImage = height,
|
||||
},
|
||||
};
|
||||
const copy_size = c.WGPUExtent3D{ .width = width, .height = height, .depthOrArrayLayers = 1 };
|
||||
|
||||
c.wgpuCommandEncoderCopyTextureToBuffer(enc, &src_copy, &dst_copy, ©_size);
|
||||
|
||||
const cmd = c.wgpuCommandEncoderFinish(enc, null);
|
||||
defer c.wgpuCommandBufferRelease(cmd);
|
||||
c.wgpuQueueSubmit(device.queue, 1, &cmd);
|
||||
|
||||
// 7. Map and read the raw image bytes back to CPU
|
||||
// (This uses whatever slice-reading helpers your `GpuBuffer` wrapper provides)
|
||||
const pixels = try cpu_staging_buf.read(allocator, u8);
|
||||
defer allocator.free(pixels);
|
||||
|
||||
// Now you have the raw binary image data! Let's output a simple Netpbm PPM image file
|
||||
// so you can actually open and look at your rendered circle.
|
||||
try savePpm(init.io, "circle.ppm", width, height, pixels);
|
||||
std.debug.print("Successfully rendered circle to circle.ppm!\n", .{});
|
||||
}
|
||||
|
||||
fn savePpm(io: std.Io, filename: []const u8, w: u32, h: u32, rgba_pixels: []const u8) !void {
|
||||
const file = try std.Io.Dir.cwd().createFile(io, filename, .{});
|
||||
defer file.close(io);
|
||||
|
||||
var buf: [255]u8 = undefined;
|
||||
var writer = file.writer(io, &buf);
|
||||
|
||||
// PPM Header: P6 format means raw RGB bytes
|
||||
try writer.interface.print("P6\n{d} {d}\n255\n", .{ w, h });
|
||||
|
||||
// Strip Alpha channel when writing out to standard RGB PPM format
|
||||
var i: usize = 0;
|
||||
while (i < rgba_pixels.len) : (i += 4) {
|
||||
try writer.interface.writeAll(rgba_pixels[i .. i + 3]);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
enable f16;
|
||||
|
||||
@group(0) @binding(0) var<storage, read> A: array<f16>;
|
||||
@group(0) @binding(1) var<storage, read> B: array<f16>;
|
||||
@group(0) @binding(2) var<storage, read_write> C: array<f16>;
|
||||
@group(0) @binding(3) var<uniform> size: u32;
|
||||
|
||||
@compute @workgroup_size(256)
|
||||
fn main(
|
||||
@builtin(global_invocation_id) global_id : vec3<u32>,
|
||||
@builtin(num_workgroups) num_workgroups: vec3<u32>
|
||||
) {
|
||||
// 1. Calculate the total number of threads across the entire grid
|
||||
let total_threads = num_workgroups.x * 256u;
|
||||
|
||||
// 2. Start at this thread's unique global ID
|
||||
var index = global_id.x;
|
||||
|
||||
// 3. Stride through the tensor elements
|
||||
while (index < size) {
|
||||
C[index] = A[index] + B[index];
|
||||
index += total_threads; // Jump forward by the total thread count
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4f,
|
||||
@location(0) uv: vec2f,
|
||||
};
|
||||
|
||||
@vertex
|
||||
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
|
||||
var output: VertexOutput;
|
||||
// Hardcoded fullscreen quad layout using 4 vertices (Triangle Strip)
|
||||
// Indexes: 0: Top-Left, 1: Bottom-Left, 2: Top-Right, 3: Bottom-Right
|
||||
var pos = array<vec2f, 4>(
|
||||
vec2f(-1.0, 1.0),
|
||||
vec2f(-1.0, -1.0),
|
||||
vec2f( 1.0, 1.0),
|
||||
vec2f( 1.0, -1.0)
|
||||
);
|
||||
|
||||
output.position = vec4f(pos[vertex_index], 0.0, 1.0);
|
||||
output.uv = pos[vertex_index]; // Ranges cleanly from -1.0 to 1.0
|
||||
return output;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_main(input: VertexOutput) -> @location(0) vec4f {
|
||||
// Distance from the center (0,0)
|
||||
let distance = length(input.uv);
|
||||
let radius = 0.5;
|
||||
|
||||
// Smooth out pixel edges (anti-aliasing)
|
||||
let edge_softness = 0.005;
|
||||
let alpha = 1.0 - smoothstep(radius - edge_softness, radius + edge_softness, distance);
|
||||
|
||||
if (alpha <= 0.0) {
|
||||
discard;
|
||||
}
|
||||
|
||||
// Draw a sharp/smooth red circle
|
||||
return vec4f(1.0, 0.3, 0.3, alpha);
|
||||
}
|
||||
Reference in New Issue
Block a user