working on example and adding opcodes
All checks were successful
Build / build (push) Successful in 2m6s
Test / build (push) Successful in 6m5s

This commit is contained in:
2026-01-23 02:09:30 +01:00
parent bb866f1312
commit 27172539e5
13 changed files with 736 additions and 266 deletions

View File

@@ -16,9 +16,21 @@ pub fn main() !void {
var rt = try spv.Runtime.init(allocator, &module);
defer rt.deinit(allocator);
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
const entry = try rt.getEntryPointByName("main");
const color = try rt.getResultByName("color");
const time = try rt.getResultByName("time");
const pos = try rt.getResultByName("pos");
const res = try rt.getResultByName("res");
var output: [4]f32 = undefined;
try rt.readOutput(f32, output[0..output.len], try rt.getResultByName("color"));
try rt.writeInput(f32, &.{@as(f32, @floatFromInt(std.time.milliTimestamp()))}, time);
try rt.writeInput(f32, &.{ 1250.0, 720.0 }, res);
try rt.writeInput(f32, &.{ 0.0, 0.0 }, pos);
try rt.callEntryPoint(allocator, entry);
try rt.readOutput(f32, output[0..output.len], color);
std.log.info("Output: Vec4{any}", .{output});
}
std.log.info("Successfully executed", .{});

View File

@@ -1,15 +1,71 @@
[nzsl_version("1.1")]
module;
struct FragIn
{
[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() -> FragOut
fn main(input: FragIn) -> FragOut
{
let output: FragOut;
output.color = vec4[f32](1.0, 1.0, 1.0, 1.0);
return output;
const I: i32 = 128;
const A: f32 = 7.5;
const MA: f32 = 100.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;
}
}
if (col == vec3[f32](0.0, 0.0, 0.0))
discard;
let output: FragOut;
output.color = vec4[f32](col.x, col.y, col.z, 1.0);
return output;
}

Binary file not shown.

View File

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