adding GLSL std 450 base
All checks were successful
Build / build (push) Successful in 1m44s
Test / build (push) Successful in 6m46s

This commit is contained in:
2026-01-24 02:46:02 +01:00
parent 37da19ed43
commit 96ad7f12f9
14 changed files with 1501 additions and 740 deletions

View File

@@ -4,8 +4,8 @@ const spv = @import("spv");
const shader_source = @embedFile("shader.spv");
const screen_width = 1250;
const screen_height = 720;
const screen_width = 200;
const screen_height = 200;
pub fn main() !void {
{
@@ -36,10 +36,16 @@ pub fn main() !void {
}
for (0..screen_height) |_| {
var rt = try spv.Runtime.init(allocator, &module);
(try runner_cache.addOne(allocator)).* = .{
.allocator = allocator,
.surface = surface,
.rt = try spv.Runtime.init(allocator, &module),
.rt = rt,
.entry = try rt.getEntryPointByName("main"),
.color = try rt.getResultByName("color"),
.time = try rt.getResultByName("time"),
.pos = try rt.getResultByName("pos"),
.res = try rt.getResultByName("res"),
};
}
@@ -48,9 +54,11 @@ pub fn main() !void {
.allocator = allocator,
});
var timer = try std.time.Timer.start();
var quit = false;
while (!quit) {
try surface.clear(.{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 });
try surface.clear(.{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0 });
while (sdl3.events.poll()) |event|
switch (event) {
@@ -65,17 +73,19 @@ pub fn main() !void {
const pixel_map: [*]u32 = @as([*]u32, @ptrCast(@alignCast((surface.getPixels() orelse return).ptr)));
var timer = try std.time.Timer.start();
var frame_timer = try std.time.Timer.start();
defer {
const ns = timer.lap();
const ns = frame_timer.lap();
const ms = @as(f32, @floatFromInt(ns)) / std.time.ns_per_s;
std.log.info("Took {d:.3}s - {d:.3}fps to render", .{ ms, 1.0 / ms });
}
const delta: f32 = @as(f32, @floatFromInt(timer.read())) / std.time.ns_per_s;
var wait_group: std.Thread.WaitGroup = .{};
for (0..screen_height) |y| {
const runner = &runner_cache.items[y];
thread_pool.spawnWg(&wait_group, Runner.run, .{ runner, y, pixel_map });
thread_pool.spawnWg(&wait_group, Runner.runWrapper, .{ runner, y, pixel_map, delta });
}
thread_pool.waitAndWork(&wait_group);
}
@@ -92,23 +102,33 @@ const Runner = struct {
allocator: std.mem.Allocator,
surface: sdl3.surface.Surface,
rt: spv.Runtime,
entry: spv.SpvWord,
color: spv.SpvWord,
time: spv.SpvWord,
pos: spv.SpvWord,
res: spv.SpvWord,
fn run(self: *Self, y: usize, pixel_map: [*]u32) void {
fn runWrapper(self: *Self, y: usize, pixel_map: [*]u32, timer: f32) void {
@call(.always_inline, Self.run, .{ self, y, pixel_map, timer }) catch |err| {
std.log.err("{s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
std.process.abort();
};
}
fn run(self: *Self, y: usize, pixel_map: [*]u32, timer: f32) !void {
var rt = self.rt; // Copy to avoid pointer access of `self` at runtime. Okay as Runtime contains only pointers and trivially copyable fields
const entry = rt.getEntryPointByName("main") catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
const color = rt.getResultByName("color") catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
const time = rt.getResultByName("time") catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
const pos = rt.getResultByName("pos") catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
const res = rt.getResultByName("res") catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
var output: [4]f32 = undefined;
for (0..screen_width) |x| {
rt.writeInput(f32, &.{@as(f32, @floatFromInt(std.time.milliTimestamp()))}, time) catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
rt.writeInput(f32, &.{ @floatFromInt(screen_width), @floatFromInt(screen_height) }, res) catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
rt.writeInput(f32, &.{ @floatFromInt(x), @floatFromInt(y) }, pos) catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
rt.callEntryPoint(self.allocator, entry) catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
rt.readOutput(f32, output[0..], color) catch |err| std.debug.panic("Catch error {s}", .{@errorName(err)});
try rt.writeInput(f32, &.{timer}, self.time);
try rt.writeInput(f32, &.{ @floatFromInt(screen_width), @floatFromInt(screen_height) }, self.res);
try rt.writeInput(f32, &.{ @floatFromInt(x), @floatFromInt(y) }, self.pos);
try rt.callEntryPoint(self.allocator, self.entry);
try rt.readOutput(f32, output[0..], self.color);
const rgba = self.surface.mapRgba(
@truncate(@as(u32, @intFromFloat(output[0] * 255.0))),

View File

@@ -3,25 +3,66 @@ module;
struct FragIn
{
[location(0)] time: f32,
[location(1)] res: vec2[f32],
[location(2)] pos: vec2[f32],
[location(0)] time: f32,
[location(1)] res: vec2[f32],
[location(2)] pos: vec2[f32],
}
struct FragOut
{
[location(0)] color: vec4[f32]
[location(0)] color: vec4[f32]
}
[entry(frag)]
fn main(input: FragIn) -> FragOut
{
let output: FragOut;
output.color = vec4[f32](
input.pos.x / input.res.x,
input.pos.y / input.res.y,
1.0,
1.0
);
return output;
const I: i32 = 32;
const A: f32 = 7.5;
const MA: f32 = 20.0;
const MI: f32 = 0.001;
let uv0 = input.pos / input.res * 2.0 - vec2[f32](1.0, 1.0);
let uv = vec2[f32](uv0.x * (input.res.x / input.res.y), uv0.y);
let col = vec3[f32](0.0, 0.0, 0.0);
let ro = vec3[f32](0.0, 0.0, -2.0);
let rd = vec3[f32](uv.x, uv.y, 1.0);
let dt = 0.0;
let ds = 0.0;
let dm = -1.0;
let p = ro;
let c = vec3[f32](0.0, 0.0, 0.0);
let l = vec3[f32](0.0, sin(input.time * 0.2) * 4.0, cos(input.time * 0.2) * 4.0);
for i in 0 -> I
{
p = ro + rd * dt;
ds = length(c - p) - 1.0;
dt += ds;
if (dm == -1.0 || ds < dm)
dm = ds;
if (ds <= MI)
{
let value = max(dot(normalize(c - p), normalize(p - l)) - 0.35, 0.0);
col = vec3[f32](value, value, value);
break;
}
if (ds >= MA)
{
if (dot(normalize(rd), normalize(l - ro)) <= 1.0)
{
let value = max(dot(normalize(rd), normalize(l - ro)) + 0.15, 0.05)/ 1.15 * (1.0 - dm * A);
col = vec3[f32](value, value, value);
}
break;
}
}
let output: FragOut;
output.color = vec4[f32](col.x, col.y, col.z, 1.0);
return output;
}

Binary file not shown.

View File

@@ -1,85 +1,281 @@
Version 1.0
Generator: 2560130
Bound: 50
Bound: 203
Schema: 0
OpCapability Capability(Shader)
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
OpEntryPoint ExecutionModel(Fragment) %24 "main" %5 %11 %14 %20
OpExecutionMode %24 ExecutionMode(OriginUpperLeft)
OpSource SourceLanguage(NZSL) 4198400
OpSourceExtension "Version: 1.1"
OpName %16 "FragIn"
OpMemberName %16 0 "time"
OpMemberName %16 1 "res"
OpMemberName %16 2 "pos"
OpName %21 "FragOut"
OpMemberName %21 0 "color"
OpName %5 "time"
OpName %11 "res"
OpName %14 "pos"
OpName %20 "color"
OpName %24 "main"
OpDecorate %5 Decoration(Location) 0
OpDecorate %11 Decoration(Location) 1
OpDecorate %14 Decoration(Location) 2
OpDecorate %20 Decoration(Location) 0
OpMemberDecorate %16 0 Decoration(Offset) 0
OpMemberDecorate %16 1 Decoration(Offset) 8
OpMemberDecorate %16 2 Decoration(Offset) 16
OpMemberDecorate %21 0 Decoration(Offset) 0
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeFloat 32
%4 = OpTypePointer StorageClass(Input) %3
%6 = OpTypeInt 32 1
%7 = OpConstant %6 i32(0)
%8 = OpTypePointer StorageClass(Function) %3
%9 = OpTypeVector %3 2
%10 = OpTypePointer StorageClass(Input) %9
%12 = OpConstant %6 i32(1)
%13 = OpTypePointer StorageClass(Function) %9
%15 = OpConstant %6 i32(2)
%16 = OpTypeStruct %3 %9 %9
%17 = OpTypePointer StorageClass(Function) %16
%18 = OpTypeVector %3 4
%19 = OpTypePointer StorageClass(Output) %18
%21 = OpTypeStruct %18
%22 = OpTypePointer StorageClass(Function) %21
%23 = OpConstant %3 f32(1)
%47 = OpTypePointer StorageClass(Function) %18
%5 = OpVariable %4 StorageClass(Input)
%11 = OpVariable %10 StorageClass(Input)
%14 = OpVariable %10 StorageClass(Input)
%20 = OpVariable %19 StorageClass(Output)
%24 = OpFunction %1 FunctionControl(0) %2
%25 = OpLabel
%26 = OpVariable %22 StorageClass(Function)
%27 = OpVariable %17 StorageClass(Function)
%28 = OpAccessChain %8 %27 %7
OpCopyMemory %28 %5
%29 = OpAccessChain %13 %27 %12
OpCopyMemory %29 %11
%30 = OpAccessChain %13 %27 %15
OpCopyMemory %30 %14
%31 = OpAccessChain %13 %27 %15
%32 = OpLoad %9 %31
%33 = OpCompositeExtract %3 %32 0
%34 = OpAccessChain %13 %27 %12
%35 = OpLoad %9 %34
%36 = OpCompositeExtract %3 %35 0
%37 = OpFDiv %3 %33 %36
%38 = OpAccessChain %13 %27 %15
%39 = OpLoad %9 %38
%40 = OpCompositeExtract %3 %39 1
%41 = OpAccessChain %13 %27 %12
%42 = OpLoad %9 %41
%43 = OpCompositeExtract %3 %42 1
%44 = OpFDiv %3 %40 %43
%45 = OpCompositeConstruct %18 %37 %44 %23 %23
%46 = OpAccessChain %47 %26 %7
OpStore %46 %45
%48 = OpLoad %21 %26
%49 = OpCompositeExtract %18 %48 0
OpStore %20 %49
OpReturn
OpFunctionEnd
OpCapability Capability(Shader)
%42 = OpExtInstImport "GLSL.std.450"
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
OpEntryPoint ExecutionModel(Fragment) %43 "main" %5 %11 %14 %20
OpExecutionMode %43 ExecutionMode(OriginUpperLeft)
OpSource SourceLanguage(NZSL) 4198400
OpSourceExtension "Version: 1.1"
OpName %16 "FragIn"
OpMemberName %16 0 "time"
OpMemberName %16 1 "res"
OpMemberName %16 2 "pos"
OpName %21 "FragOut"
OpMemberName %21 0 "color"
OpName %5 "time"
OpName %11 "res"
OpName %14 "pos"
OpName %20 "color"
OpName %43 "main"
OpDecorate %5 Decoration(Location) 0
OpDecorate %11 Decoration(Location) 1
OpDecorate %14 Decoration(Location) 2
OpDecorate %20 Decoration(Location) 0
OpMemberDecorate %16 0 Decoration(Offset) 0
OpMemberDecorate %16 1 Decoration(Offset) 8
OpMemberDecorate %16 2 Decoration(Offset) 16
OpMemberDecorate %21 0 Decoration(Offset) 0
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeFloat 32
%4 = OpTypePointer StorageClass(Input) %3
%6 = OpTypeInt 32 1
%7 = OpConstant %6 i32(0)
%8 = OpTypePointer StorageClass(Function) %3
%9 = OpTypeVector %3 2
%10 = OpTypePointer StorageClass(Input) %9
%12 = OpConstant %6 i32(1)
%13 = OpTypePointer StorageClass(Function) %9
%15 = OpConstant %6 i32(2)
%16 = OpTypeStruct %3 %9 %9
%17 = OpTypePointer StorageClass(Function) %16
%18 = OpTypeVector %3 4
%19 = OpTypePointer StorageClass(Output) %18
%21 = OpTypeStruct %18
%22 = OpConstant %3 f32(2)
%23 = OpConstant %3 f32(1)
%24 = OpConstant %3 f32(0)
%25 = OpTypeVector %3 3
%26 = OpTypePointer StorageClass(Function) %25
%27 = OpConstant %3 f32(-2)
%28 = OpConstant %3 f32(-1)
%29 = OpConstant %3 f32(0.2)
%30 = OpConstant %3 f32(4)
%31 = OpTypePointer StorageClass(Function) %6
%32 = OpConstant %6 i32(32)
%33 = OpTypeBool
%34 = OpConstant %3 f32(0.001)
%35 = OpConstant %3 f32(0.35)
%36 = OpConstant %3 f32(20)
%37 = OpConstant %3 f32(0.15)
%38 = OpConstant %3 f32(0.05)
%39 = OpConstant %3 f32(1.15)
%40 = OpConstant %3 f32(7.5)
%41 = OpTypePointer StorageClass(Function) %21
%200 = OpTypePointer StorageClass(Function) %18
%5 = OpVariable %4 StorageClass(Input)
%11 = OpVariable %10 StorageClass(Input)
%14 = OpVariable %10 StorageClass(Input)
%20 = OpVariable %19 StorageClass(Output)
%43 = OpFunction %1 FunctionControl(0) %2
%44 = OpLabel
%45 = OpVariable %13 StorageClass(Function)
%46 = OpVariable %13 StorageClass(Function)
%47 = OpVariable %26 StorageClass(Function)
%48 = OpVariable %26 StorageClass(Function)
%49 = OpVariable %26 StorageClass(Function)
%50 = OpVariable %8 StorageClass(Function)
%51 = OpVariable %8 StorageClass(Function)
%52 = OpVariable %8 StorageClass(Function)
%53 = OpVariable %26 StorageClass(Function)
%54 = OpVariable %26 StorageClass(Function)
%55 = OpVariable %26 StorageClass(Function)
%56 = OpVariable %31 StorageClass(Function)
%57 = OpVariable %31 StorageClass(Function)
%58 = OpVariable %8 StorageClass(Function)
%59 = OpVariable %8 StorageClass(Function)
%60 = OpVariable %41 StorageClass(Function)
%61 = OpVariable %17 StorageClass(Function)
%62 = OpAccessChain %8 %61 %7
OpCopyMemory %62 %5
%63 = OpAccessChain %13 %61 %12
OpCopyMemory %63 %11
%64 = OpAccessChain %13 %61 %15
OpCopyMemory %64 %14
%65 = OpAccessChain %13 %61 %15
%66 = OpLoad %9 %65
%67 = OpAccessChain %13 %61 %12
%68 = OpLoad %9 %67
%69 = OpFDiv %9 %66 %68
%70 = OpVectorTimesScalar %9 %69 %22
%71 = OpCompositeConstruct %9 %23 %23
%72 = OpFSub %9 %70 %71
OpStore %45 %72
%73 = OpLoad %9 %45
%74 = OpCompositeExtract %3 %73 0
%75 = OpAccessChain %13 %61 %12
%76 = OpLoad %9 %75
%77 = OpCompositeExtract %3 %76 0
%78 = OpAccessChain %13 %61 %12
%79 = OpLoad %9 %78
%80 = OpCompositeExtract %3 %79 1
%81 = OpFDiv %3 %77 %80
%82 = OpFMul %3 %74 %81
%83 = OpLoad %9 %45
%84 = OpCompositeExtract %3 %83 1
%85 = OpCompositeConstruct %9 %82 %84
OpStore %46 %85
%86 = OpCompositeConstruct %25 %24 %24 %24
OpStore %47 %86
%87 = OpCompositeConstruct %25 %24 %24 %27
OpStore %48 %87
%88 = OpLoad %9 %46
%89 = OpCompositeExtract %3 %88 0
%90 = OpLoad %9 %46
%91 = OpCompositeExtract %3 %90 1
%92 = OpCompositeConstruct %25 %89 %91 %23
OpStore %49 %92
OpStore %50 %24
OpStore %51 %24
OpStore %52 %28
%93 = OpLoad %25 %48
OpStore %53 %93
%94 = OpCompositeConstruct %25 %24 %24 %24
OpStore %54 %94
%95 = OpAccessChain %8 %61 %7
%96 = OpLoad %3 %95
%97 = OpFMul %3 %96 %29
%98 = OpExtInst %3 GLSLstd450 Sin %97
%99 = OpFMul %3 %98 %30
%100 = OpAccessChain %8 %61 %7
%101 = OpLoad %3 %100
%102 = OpFMul %3 %101 %29
%103 = OpExtInst %3 GLSLstd450 Cos %102
%104 = OpFMul %3 %103 %30
%105 = OpCompositeConstruct %25 %24 %99 %104
OpStore %55 %105
OpStore %56 %7
OpStore %57 %32
OpBranch %106
%106 = OpLabel
%110 = OpLoad %6 %56
%111 = OpLoad %6 %57
%112 = OpSLessThan %33 %110 %111
OpLoopMerge %108 %109 LoopControl(0)
OpBranchConditional %112 %107 %108
%107 = OpLabel
%113 = OpLoad %25 %48
%114 = OpLoad %25 %49
%115 = OpLoad %3 %50
%116 = OpVectorTimesScalar %25 %114 %115
%117 = OpFAdd %25 %113 %116
OpStore %53 %117
%118 = OpLoad %25 %54
%119 = OpLoad %25 %53
%120 = OpFSub %25 %118 %119
%121 = OpExtInst %3 GLSLstd450 Length %120
%122 = OpFSub %3 %121 %23
OpStore %51 %122
%123 = OpLoad %3 %50
%124 = OpLoad %3 %51
%125 = OpFAdd %3 %123 %124
OpStore %50 %125
%129 = OpLoad %3 %52
%130 = OpFOrdEqual %33 %129 %28
%131 = OpLoad %3 %51
%132 = OpLoad %3 %52
%133 = OpFOrdLessThan %33 %131 %132
%134 = OpLogicalOr %33 %130 %133
OpSelectionMerge %126 SelectionControl(0)
OpBranchConditional %134 %127 %128
%127 = OpLabel
%135 = OpLoad %3 %51
OpStore %52 %135
OpBranch %126
%128 = OpLabel
OpBranch %126
%126 = OpLabel
%139 = OpLoad %3 %51
%140 = OpFOrdLessThanEqual %33 %139 %34
OpSelectionMerge %136 SelectionControl(0)
OpBranchConditional %140 %137 %138
%137 = OpLabel
%141 = OpLoad %25 %54
%142 = OpLoad %25 %53
%143 = OpFSub %25 %141 %142
%144 = OpExtInst %25 GLSLstd450 Normalize %143
%145 = OpLoad %25 %53
%146 = OpLoad %25 %55
%147 = OpFSub %25 %145 %146
%148 = OpExtInst %25 GLSLstd450 Normalize %147
%149 = OpDot %3 %144 %148
%150 = OpFSub %3 %149 %35
%151 = OpExtInst %3 GLSLstd450 FMax %150 %24
OpStore %58 %151
%152 = OpLoad %3 %58
%153 = OpLoad %3 %58
%154 = OpLoad %3 %58
%155 = OpCompositeConstruct %25 %152 %153 %154
OpStore %47 %155
OpBranch %108
%138 = OpLabel
OpBranch %136
%136 = OpLabel
%159 = OpLoad %3 %51
%160 = OpFOrdGreaterThanEqual %33 %159 %36
OpSelectionMerge %156 SelectionControl(0)
OpBranchConditional %160 %157 %158
%157 = OpLabel
%164 = OpLoad %25 %49
%165 = OpExtInst %25 GLSLstd450 Normalize %164
%166 = OpLoad %25 %55
%167 = OpLoad %25 %48
%168 = OpFSub %25 %166 %167
%169 = OpExtInst %25 GLSLstd450 Normalize %168
%170 = OpDot %3 %165 %169
%171 = OpFOrdLessThanEqual %33 %170 %23
OpSelectionMerge %161 SelectionControl(0)
OpBranchConditional %171 %162 %163
%162 = OpLabel
%172 = OpLoad %25 %49
%173 = OpExtInst %25 GLSLstd450 Normalize %172
%174 = OpLoad %25 %55
%175 = OpLoad %25 %48
%176 = OpFSub %25 %174 %175
%177 = OpExtInst %25 GLSLstd450 Normalize %176
%178 = OpDot %3 %173 %177
%179 = OpFAdd %3 %178 %37
%180 = OpExtInst %3 GLSLstd450 FMax %179 %38
%181 = OpFDiv %3 %180 %39
%182 = OpLoad %3 %52
%183 = OpFMul %3 %182 %40
%184 = OpFSub %3 %23 %183
%185 = OpFMul %3 %181 %184
OpStore %59 %185
%186 = OpLoad %3 %59
%187 = OpLoad %3 %59
%188 = OpLoad %3 %59
%189 = OpCompositeConstruct %25 %186 %187 %188
OpStore %47 %189
OpBranch %161
%163 = OpLabel
OpBranch %161
%161 = OpLabel
OpBranch %108
%158 = OpLabel
OpBranch %156
%156 = OpLabel
%190 = OpLoad %6 %56
%191 = OpIAdd %6 %190 %12
OpStore %56 %191
OpBranch %109
%109 = OpLabel
OpBranch %106
%108 = OpLabel
%192 = OpLoad %25 %47
%193 = OpCompositeExtract %3 %192 0
%194 = OpLoad %25 %47
%195 = OpCompositeExtract %3 %194 1
%196 = OpLoad %25 %47
%197 = OpCompositeExtract %3 %196 2
%198 = OpCompositeConstruct %18 %193 %195 %197 %23
%199 = OpAccessChain %200 %60 %7
OpStore %199 %198
%201 = OpLoad %21 %60
%202 = OpCompositeExtract %18 %201 0
OpStore %20 %202
OpReturn
OpFunctionEnd