Working self contained rendering (simple circle)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
const std = @import("std");
|
||||
const gpu = @import("gpu");
|
||||
const GpuDevice = gpu.GpuDevice;
|
||||
const GpuArena = gpu.GpuArena;
|
||||
const GpuBuffer = gpu.GpuBuffer;
|
||||
const GpuRender = gpu.GpuRender;
|
||||
const GpuTexture = gpu.GpuTexture;
|
||||
const GpuTextureView = gpu.GpuTextureView;
|
||||
|
||||
const width: u32 = 512;
|
||||
const height: u32 = 512;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const allocator = init.gpa;
|
||||
|
||||
// 1. Open GPU Device
|
||||
const device = try GpuDevice.init(.{});
|
||||
defer device.deinit();
|
||||
|
||||
// 2. Init VRAM Arena
|
||||
var grena = GpuArena.init(allocator, device);
|
||||
defer grena.deinit();
|
||||
const gloc = grena.gpuAllocator();
|
||||
|
||||
// 3. Load Render Pipeline
|
||||
const circle_rp = try GpuRender.init(
|
||||
device, // Change to gloc + track them
|
||||
@embedFile("shaders/circle.wgsl"),
|
||||
.{ .bindings = &.{}, .texture_format = .RGBA8Unorm, .topology = .TriangleStrip },
|
||||
);
|
||||
defer circle_rp.deinit();
|
||||
|
||||
// 4. Create 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();
|
||||
|
||||
// 5. Create a view from texture
|
||||
const view = try GpuTextureView.init(gloc, texture, .{});
|
||||
defer view.deinit();
|
||||
|
||||
// 6. Run the rendering pipeline
|
||||
try circle_rp.draw(gloc, view, 4, .{});
|
||||
|
||||
// 7. Load Texture into GpuBuffer
|
||||
const cpu_staging_cpu = try texture.buffCopy(gloc);
|
||||
defer cpu_staging_cpu.deinit();
|
||||
|
||||
// 8. Read GpuBuffer to CPU
|
||||
const pixels = try cpu_staging_cpu.read(allocator, u8);
|
||||
defer allocator.free(pixels);
|
||||
|
||||
// 9. Write a simple ppm image
|
||||
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,77 +0,0 @@
|
||||
// I am using this mnist reduced dataset https://www.kaggle.com/datasets/mohamedgamal07/reduced-mnist
|
||||
|
||||
const std = @import("std");
|
||||
const gpu = @import("gpu");
|
||||
const GpuDevice = gpu.GpuDevice;
|
||||
const GpuArena = gpu.GpuArena;
|
||||
const GpuBuffer = gpu.GpuBuffer;
|
||||
const GpuProcess = gpu.GpuProcess;
|
||||
|
||||
const BATCHSIZE = 10;
|
||||
const EPOCH = 10;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const allocator = init.gpa;
|
||||
const io = init.io;
|
||||
|
||||
// 1. Open GPU Device
|
||||
const device = try GpuDevice.init(.{});
|
||||
defer device.deinit();
|
||||
|
||||
// 2. Create a GPU Arena to manage VRAM
|
||||
var grena = GpuArena.init(allocator, device);
|
||||
defer grena.deinit();
|
||||
const gloc = grena.gpuAllocator();
|
||||
|
||||
// 3. Load the WGSL compute pipeline
|
||||
const add_process = try GpuProcess.init(device, @embedFile("shaders/add.wgsl"));
|
||||
defer add_process.deinit();
|
||||
|
||||
var train_dir = try std.Io.Dir.cwd().openDir(io, "mnist/train", .{});
|
||||
|
||||
var images: [BATCHSIZE * 28 * 28]f16 = undefined;
|
||||
for (EPOCH) |epoch| {
|
||||
// Load random images from train dir
|
||||
train_dir.openDir(io, "0", .{});
|
||||
for (BATCHSIZE) |i| {
|
||||
const file = try train_dir.openFile(io, "0.jpg", .{});
|
||||
images[28 * 28 * i .. 28 * 28 * (i + 1)] = file.read
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Setup CPU data
|
||||
const len: usize = 16;
|
||||
const data_a = try allocator.alloc(f16, len);
|
||||
defer allocator.free(data_a);
|
||||
const data_b = try allocator.alloc(f16, len);
|
||||
defer allocator.free(data_b);
|
||||
|
||||
for (0..len) |i| {
|
||||
data_a[i] = @floatFromInt(i);
|
||||
data_b[i] = @floatFromInt(len - 1 - i);
|
||||
}
|
||||
|
||||
// 5. Initialize raw GPU Buffers
|
||||
// We pass the EnumSet inline using `.initMany` since the Enum itself isn't exported
|
||||
const byte_size = len * @sizeOf(f16);
|
||||
const buf_a = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc }));
|
||||
const buf_b = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc }));
|
||||
const buf_out = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc }));
|
||||
|
||||
// Note: The buffers are safely tied to the GpuArena which will automatically
|
||||
// release them at the end. You can also manually call buf_x.deinit() if desired.
|
||||
|
||||
// 6. Transfer data from CPU slices to GPU Buffers
|
||||
try buf_a.load(f16, data_a);
|
||||
try buf_b.load(f16, data_b);
|
||||
|
||||
// 7. Dispatch the Compute Process
|
||||
// We pass the data type (f16) to allow GpuProcess to calculate chunks correctly
|
||||
try add_process.run(gloc, f16, buf_a, buf_b, buf_out);
|
||||
|
||||
// 8. Map and copy the resulting buffer back to the CPU
|
||||
const out = try buf_out.read(allocator, f16);
|
||||
defer allocator.free(out);
|
||||
|
||||
std.debug.print("Result: {any}\n", .{out});
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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