adding some operators, working on example
Some checks failed
Build / build (push) Failing after 33s
Test / build (push) Failing after 3m0s

This commit is contained in:
2026-01-20 21:52:42 +01:00
parent 35099b33e1
commit df711a196a
11 changed files with 170 additions and 68 deletions

View File

@@ -1,25 +1,95 @@
const std = @import("std");
const sdl3 = @import("sdl3");
const spv = @import("spv");
const shader_source = @embedFile("shader.spv");
const screen_width = 640;
const screen_height = 480;
pub fn main() !void {
{
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
defer sdl3.shutdown();
const init_flags = sdl3.InitFlags{ .video = true };
try sdl3.init(init_flags);
defer sdl3.quit(init_flags);
const window = try sdl3.video.Window.init("Hello SDL3", screen_width, screen_height, .{});
defer window.deinit();
const allocator = gpa.allocator();
var module = try spv.Module.init(allocator, @ptrCast(@alignCast(shader_source)));
defer module.deinit(allocator);
var rt = try spv.Runtime.init(allocator, &module);
defer rt.deinit(allocator);
const surface = try window.getSurface();
try surface.clear(.{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 });
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
var output: [4]i32 = undefined;
try rt.readOutput(i32, output[0..], try rt.getResultByName("color"));
std.log.info("Output: Vec4{any}", .{output});
{
try surface.lock();
defer surface.unlock();
const map = surface.getPixels() orelse return;
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 top_y = margin_y;
const bottom_y = (screen_height - 1) - margin_y;
const center_x = @divTrunc(screen_width, 2);
const tri_h = bottom_y - top_y;
const max_half_w = @divTrunc(screen_width, 2) - margin_x;
for (top_y..bottom_y) |y| {
const t: f32 = @as(f32, @floatFromInt(y - top_y)) / @as(f32, @floatFromInt(tri_h));
const half_w: usize = @intFromFloat((t * @as(f32, @floatFromInt(max_half_w))) + 0.5);
const x0 = std.math.clamp(center_x - half_w, 0, screen_width - 1);
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 });
}
}
pool.waitAndWork(&wait_group);
}
try window.updateSurface();
std.Thread.sleep(5_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;
}

View File

@@ -1,17 +1,20 @@
[nzsl_version("1.1")]
module;
struct FragIn
{
[location(0)] pos: vec2[f32]
}
struct FragOut
{
[location(0)] color: vec4[i32]
[location(0)] color: vec4[f32]
}
[entry(frag)]
fn main() -> FragOut
fn main(input: FragIn) -> FragOut
{
let base: i32 = 4;
let value: i32 = base >> 3;
let output: FragOut;
output.color = vec4[i32](value, value, value, value);
output.color = vec4[f32](1.0, 0.0, 0.0, 1.0);
return output;
}

Binary file not shown.

View File

@@ -1,6 +1,6 @@
Version 1.0
Generator: 2560130
Bound: 29
Bound: 21
Schema: 0
OpCapability Capability(Shader)
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
@@ -16,35 +16,25 @@ Schema: 0
OpMemberDecorate %7 0 Decoration(Offset) 0
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeInt 32 1
%3 = OpTypeFloat 32
%4 = OpTypeVector %3 4
%5 = OpTypePointer StorageClass(Output) %4
%7 = OpTypeStruct %4
%8 = OpConstant %3 i32(4)
%9 = OpTypePointer StorageClass(Function) %3
%10 = OpConstant %3 i32(3)
%11 = OpTypePointer StorageClass(Function) %7
%12 = OpConstant %3 i32(0)
%26 = OpTypePointer StorageClass(Function) %4
%8 = OpTypePointer StorageClass(Function) %7
%9 = OpTypeInt 32 1
%10 = OpConstant %9 i32(0)
%11 = OpConstant %3 f32(1)
%12 = OpConstant %3 f32(0)
%18 = OpTypePointer StorageClass(Function) %4
%6 = OpVariable %5 StorageClass(Output)
%13 = OpFunction %1 FunctionControl(0) %2
%14 = OpLabel
%15 = OpVariable %9 StorageClass(Function)
%16 = OpVariable %9 StorageClass(Function)
%17 = OpVariable %11 StorageClass(Function)
OpStore %15 %8
%18 = OpLoad %3 %15
%19 = OpShiftRightArithmetic %3 %18 %10
OpStore %16 %19
%20 = OpLoad %3 %16
%21 = OpLoad %3 %16
%22 = OpLoad %3 %16
%23 = OpLoad %3 %16
%24 = OpCompositeConstruct %4 %20 %21 %22 %23
%25 = OpAccessChain %26 %17 %12
OpStore %25 %24
%27 = OpLoad %7 %17
%28 = OpCompositeExtract %4 %27 0
OpStore %6 %28
%15 = OpVariable %8 StorageClass(Function)
%16 = OpCompositeConstruct %4 %11 %12 %12 %11
%17 = OpAccessChain %18 %15 %10
OpStore %17 %16
%19 = OpLoad %7 %15
%20 = OpCompositeExtract %4 %19 0
OpStore %6 %20
OpReturn
OpFunctionEnd