working on example
Some checks failed
Build / build (push) Failing after 36s
Test / build (push) Failing after 55s

This commit is contained in:
2026-01-21 00:35:48 +01:00
parent df711a196a
commit c5225e3a45
8 changed files with 171 additions and 124 deletions

View File

@@ -17,7 +17,7 @@ pub fn main() !void {
try sdl3.init(init_flags);
defer sdl3.quit(init_flags);
const window = try sdl3.video.Window.init("Hello SDL3", screen_width, screen_height, .{});
const window = try sdl3.video.Window.init("Hello triangle", screen_width, screen_height, .{});
defer window.deinit();
const allocator = gpa.allocator();
@@ -32,19 +32,10 @@ pub fn main() !void {
try surface.lock();
defer surface.unlock();
const map = surface.getPixels() orelse return;
var pixel_map: [*]u32 = @as([*]u32, @ptrCast(@alignCast((surface.getPixels() orelse return).ptr)));
var pool: std.Thread.Pool = undefined;
try pool.init(.{
.allocator = allocator,
.n_jobs = 16384,
});
defer pool.deinit();
var wait_group: std.Thread.WaitGroup = .{};
const margin_x = @divTrunc(screen_width, 5);
const margin_y = @divTrunc(screen_height, 5);
const margin_x = @divTrunc(screen_width, 3);
const margin_y = @divTrunc(screen_height, 3);
const top_y = margin_y;
const bottom_y = (screen_height - 1) - margin_y;
const center_x = @divTrunc(screen_width, 2);
@@ -58,38 +49,37 @@ pub fn main() !void {
const x1 = std.math.clamp(center_x + half_w, 0, screen_width - 1);
for (x0..x1) |x| {
pool.spawnWg(&wait_group, run, .{ allocator, &module, map, surface, x, y });
var rt = try spv.Runtime.init(allocator, &module);
defer rt.deinit(allocator);
var output: [4]f32 = undefined;
const entry = try rt.getEntryPointByName("main");
const color = try rt.getResultByName("color");
const dim = try rt.getResultByName("dim");
const pos = try rt.getResultByName("pos");
try rt.writeInput(f32, &.{ @floatFromInt(x1 - x0), @floatFromInt(bottom_y - top_y) }, dim);
try rt.writeInput(f32, &.{ @floatFromInt(x), @floatFromInt(y) }, pos);
try rt.callEntryPoint(allocator, entry);
try rt.readOutput(f32, output[0..], color);
const rgba = surface.mapRgba(
@intFromFloat(output[0] * 255.0),
@intFromFloat(output[1] * 255.0),
@intFromFloat(output[2] * 255.0),
@intFromFloat(output[3] * 255.0),
);
pixel_map[(y * surface.getWidth()) + x] = rgba.value;
}
}
pool.waitAndWork(&wait_group);
}
try window.updateSurface();
std.Thread.sleep(5_000_000_000);
std.Thread.sleep(10_000_000_000);
}
std.log.info("Successfully executed", .{});
}
fn run(allocator: std.mem.Allocator, module: *spv.Module, map: []u8, surface: sdl3.surface.Surface, x: usize, y: usize) void {
var rt = spv.Runtime.init(allocator, module) catch |err| std.debug.panic("Failed with error {s}", .{@errorName(err)});
defer rt.deinit(allocator);
var output: [4]f32 = undefined;
const entry = rt.getEntryPointByName("main") catch |err| std.debug.panic("Failed with error {s}", .{@errorName(err)});
const color = rt.getResultByName("color") catch |err| std.debug.panic("Failed with error {s}", .{@errorName(err)});
rt.callEntryPoint(allocator, entry) catch |err| std.debug.panic("Failed with error {s}", .{@errorName(err)});
rt.readOutput(f32, output[0..], color) catch |err| std.debug.panic("Failed with error {s}", .{@errorName(err)});
const rgba = surface.mapRgba(
@intFromFloat(output[0] * 255.0),
@intFromFloat(output[1] * 255.0),
@intFromFloat(output[2] * 255.0),
@intFromFloat(output[3] * 255.0),
);
var pixel_map: [*]u32 = @as([*]u32, @ptrCast(@alignCast(map.ptr)));
pixel_map[(y * surface.getWidth()) + x] = rgba.value;
}