adding builtin support
This commit is contained in:
@@ -3,12 +3,8 @@ const spv = @import("spv");
|
||||
|
||||
const shader_source = @embedFile("shader.spv");
|
||||
|
||||
const Input = struct {
|
||||
value: [4]i32 = [4]i32{ 0, 0, 0, 0 },
|
||||
};
|
||||
|
||||
const Output = struct {
|
||||
value: [4]i32 = [4]i32{ 0, 0, 0, 0 },
|
||||
const SSBO = struct {
|
||||
value: [256]i32 = [_]i32{0} ** 256,
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
@@ -28,19 +24,31 @@ pub fn main() !void {
|
||||
|
||||
const entry = try rt.getEntryPointByName("main");
|
||||
|
||||
var input: Input = .{};
|
||||
var output: Output = .{};
|
||||
var ssbo: SSBO = .{};
|
||||
|
||||
try rt.writeDescriptorSet(allocator, std.mem.asBytes(&input), 0, 0);
|
||||
try rt.writeDescriptorSet(allocator, std.mem.asBytes(&output), 0, 1);
|
||||
for (0..16) |i| {
|
||||
for (0..16) |x| {
|
||||
for (0..16) |y| {
|
||||
const global_invocation_indices = [3]i32{
|
||||
@as(i32, @intCast(i * 16 + x)),
|
||||
@as(i32, @intCast(y)),
|
||||
1,
|
||||
};
|
||||
|
||||
try rt.callEntryPoint(allocator, entry);
|
||||
try rt.writeBuiltIn(std.mem.asBytes(&global_invocation_indices), .GlobalInvocationId);
|
||||
try rt.writeDescriptorSet(allocator, std.mem.asBytes(&ssbo), 0, 0);
|
||||
rt.callEntryPoint(allocator, entry) catch |err| switch (err) {
|
||||
spv.Runtime.RuntimeError.OutOfBounds => continue,
|
||||
else => return err,
|
||||
};
|
||||
try rt.readDescriptorSet(std.mem.asBytes(&ssbo), 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try rt.readDescriptorSet(std.mem.asBytes(&output), 0, 1);
|
||||
std.log.info("Output: {any}", .{ssbo});
|
||||
|
||||
std.log.info("Output: {any}", .{output});
|
||||
|
||||
std.log.info("\nTotal memory used: {d:.3} KB\n", .{@as(f32, @floatFromInt(gpa.total_requested_bytes)) / 1000.0});
|
||||
std.log.info("Total memory used: {d:.3} KB\n", .{@as(f32, @floatFromInt(gpa.total_requested_bytes)) / 1000.0});
|
||||
}
|
||||
std.log.info("Successfully executed", .{});
|
||||
}
|
||||
|
||||
@@ -1,68 +1,25 @@
|
||||
[sudo mkswap /swapfilenzsl_version("1.1")]
|
||||
[nzsl_version("1.1")]
|
||||
module;
|
||||
|
||||
struct FragIn
|
||||
struct Input
|
||||
{
|
||||
[location(0)] time: f32,
|
||||
[location(1)] res: vec2[f32],
|
||||
[location(2)] pos: vec2[f32],
|
||||
[builtin(global_invocation_indices)] indices: vec3[u32]
|
||||
}
|
||||
|
||||
struct FragOut
|
||||
[layout(std430)]
|
||||
struct SSBO
|
||||
{
|
||||
[location(0)] color: vec4[f32]
|
||||
data: dyn_array[i32]
|
||||
}
|
||||
|
||||
[entry(frag)]
|
||||
fn main(input: FragIn) -> FragOut
|
||||
external
|
||||
{
|
||||
const I: i32 = 32;
|
||||
const A: f32 = 7.5;
|
||||
const MA: f32 = 2.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 = vec4[f32](0.0, 0.0, 0.0, 0.0);
|
||||
let ro = vec4[f32](0.0, 0.0, -2.0, 0.0);
|
||||
let rd = vec4[f32](uv.x, uv.y, 1.0, 0.0);
|
||||
let dt = 0.0;
|
||||
let ds = 0.0;
|
||||
let dm = -1.0;
|
||||
let p = ro;
|
||||
let c = vec4[f32](0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
let l = vec4[f32](0.0, sin(input.time * 0.2) * 4.0, cos(input.time * 0.2) * 4.0, 0.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.0);
|
||||
col = vec4[f32](value, value, value, 1.0);
|
||||
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.0) / 1.15 * max(1.0 - dm * A, 0.0);
|
||||
col = vec4[f32](value, value, value, 1.0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let output: FragOut;
|
||||
output.color = col;
|
||||
return output;
|
||||
[set(0), binding(0)] ssbo: storage[SSBO],
|
||||
}
|
||||
|
||||
[entry(compute)]
|
||||
[workgroup(16, 16, 1)]
|
||||
fn main(input: Input)
|
||||
{
|
||||
ssbo.data[input.indices.x * input.indices.y] = i32(input.indices.x * input.indices.y);
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,83 +1,68 @@
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint GLCompute %4 "main" %11 %20
|
||||
OpExecutionMode %4 LocalSize 1 1 1
|
||||
OpDecorate %11 BuiltIn NumWorkgroups
|
||||
OpDecorate %20 BuiltIn WorkgroupId
|
||||
OpMemberDecorate %37 0 Offset 0
|
||||
OpDecorate %38 ArrayStride 4
|
||||
OpDecorate %39 BufferBlock
|
||||
OpMemberDecorate %39 0 Offset 0
|
||||
OpDecorate %41 Binding 0
|
||||
OpDecorate %41 DescriptorSet 0
|
||||
OpMemberDecorate %50 0 Offset 0
|
||||
OpDecorate %51 ArrayStride 4
|
||||
OpDecorate %52 BufferBlock
|
||||
OpMemberDecorate %52 0 Offset 0
|
||||
OpDecorate %54 Binding 1
|
||||
OpDecorate %54 DescriptorSet 0
|
||||
OpDecorate %58 BuiltIn WorkgroupSize
|
||||
%2 = OpTypeVoid
|
||||
%3 = OpTypeFunction %2
|
||||
%6 = OpTypeInt 32 0
|
||||
%7 = OpTypePointer Function %6
|
||||
%9 = OpTypeVector %6 3
|
||||
%10 = OpTypePointer Input %9
|
||||
%11 = OpVariable %10 Input
|
||||
%12 = OpConstant %6 0
|
||||
%13 = OpTypePointer Input %6
|
||||
%16 = OpConstant %6 1
|
||||
%20 = OpVariable %10 Input
|
||||
%21 = OpConstant %6 2
|
||||
%34 = OpTypeInt 32 1
|
||||
%35 = OpTypePointer Function %34
|
||||
%37 = OpTypeStruct %34
|
||||
%38 = OpTypeRuntimeArray %37
|
||||
%39 = OpTypeStruct %38
|
||||
%40 = OpTypePointer Uniform %39
|
||||
%41 = OpVariable %40 Uniform
|
||||
%42 = OpConstant %34 0
|
||||
%44 = OpTypePointer Uniform %34
|
||||
%50 = OpTypeStruct %34
|
||||
%51 = OpTypeRuntimeArray %50
|
||||
%52 = OpTypeStruct %51
|
||||
%53 = OpTypePointer Uniform %52
|
||||
%54 = OpVariable %53 Uniform
|
||||
%58 = OpConstantComposite %9 %16 %16 %16
|
||||
%4 = OpFunction %2 None %3
|
||||
%5 = OpLabel
|
||||
%8 = OpVariable %7 Function
|
||||
%36 = OpVariable %35 Function
|
||||
%47 = OpVariable %35 Function
|
||||
%14 = OpAccessChain %13 %11 %12
|
||||
%15 = OpLoad %6 %14
|
||||
%17 = OpAccessChain %13 %11 %16
|
||||
%18 = OpLoad %6 %17
|
||||
%19 = OpIMul %6 %15 %18
|
||||
%22 = OpAccessChain %13 %20 %21
|
||||
%23 = OpLoad %6 %22
|
||||
%24 = OpIMul %6 %19 %23
|
||||
%25 = OpAccessChain %13 %11 %12
|
||||
%26 = OpLoad %6 %25
|
||||
%27 = OpAccessChain %13 %20 %16
|
||||
%28 = OpLoad %6 %27
|
||||
%29 = OpIMul %6 %26 %28
|
||||
%30 = OpIAdd %6 %24 %29
|
||||
%31 = OpAccessChain %13 %20 %12
|
||||
%32 = OpLoad %6 %31
|
||||
%33 = OpIAdd %6 %30 %32
|
||||
OpStore %8 %33
|
||||
%43 = OpLoad %6 %8
|
||||
%45 = OpAccessChain %44 %41 %42 %43 %42
|
||||
%46 = OpLoad %34 %45
|
||||
OpStore %36 %46
|
||||
%48 = OpLoad %34 %36
|
||||
%49 = OpExtInst %34 %1 SAbs %48
|
||||
OpStore %47 %49
|
||||
%55 = OpLoad %6 %8
|
||||
%56 = OpLoad %34 %47
|
||||
%57 = OpAccessChain %44 %54 %42 %55 %42
|
||||
OpStore %57 %56
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
Version 1.0
|
||||
Generator: 2560130
|
||||
Bound: 41
|
||||
Schema: 0
|
||||
OpCapability Capability(Shader)
|
||||
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
|
||||
OpEntryPoint ExecutionModel(GLCompute) %18 "main" %11
|
||||
OpExecutionMode %18 ExecutionMode(LocalSize) 16 16 1
|
||||
OpSource SourceLanguage(NZSL) 4198400
|
||||
OpSourceExtension "Version: 1.1"
|
||||
OpName %3 "SSBO"
|
||||
OpMemberName %3 0 "data"
|
||||
OpName %14 "Input"
|
||||
OpMemberName %14 0 "indices"
|
||||
OpName %5 "ssbo"
|
||||
OpName %11 "global_invocation_indices"
|
||||
OpName %18 "main"
|
||||
OpDecorate %5 Decoration(Binding) 0
|
||||
OpDecorate %5 Decoration(DescriptorSet) 0
|
||||
OpDecorate %11 Decoration(BuiltIn) BuiltIn(GlobalInvocationId)
|
||||
OpDecorate %2 Decoration(ArrayStride) 4
|
||||
OpDecorate %3 Decoration(BufferBlock)
|
||||
OpMemberDecorate %3 0 Decoration(Offset) 0
|
||||
OpMemberDecorate %14 0 Decoration(Offset) 0
|
||||
%1 = OpTypeInt 32 1
|
||||
%2 = OpTypeRuntimeArray %1
|
||||
%3 = OpTypeStruct %2
|
||||
%4 = OpTypePointer StorageClass(Uniform) %3
|
||||
%6 = OpTypeVoid
|
||||
%7 = OpTypeFunction %6
|
||||
%8 = OpTypeInt 32 0
|
||||
%9 = OpTypeVector %8 3
|
||||
%10 = OpTypePointer StorageClass(Input) %9
|
||||
%12 = OpConstant %1 i32(0)
|
||||
%13 = OpTypePointer StorageClass(Function) %9
|
||||
%14 = OpTypeStruct %9
|
||||
%15 = OpTypePointer StorageClass(Function) %14
|
||||
%16 = OpTypeRuntimeArray %1
|
||||
%17 = OpConstant %1 i32(1)
|
||||
%31 = OpTypePointer StorageClass(Uniform) %2
|
||||
%40 = OpTypePointer StorageClass(Uniform) %1
|
||||
%5 = OpVariable %4 StorageClass(Uniform)
|
||||
%11 = OpVariable %10 StorageClass(Input)
|
||||
%18 = OpFunction %6 FunctionControl(0) %7
|
||||
%19 = OpLabel
|
||||
%20 = OpVariable %15 StorageClass(Function)
|
||||
%21 = OpAccessChain %13 %20 %12
|
||||
OpCopyMemory %21 %11
|
||||
%22 = OpAccessChain %13 %20 %12
|
||||
%23 = OpLoad %9 %22
|
||||
%24 = OpCompositeExtract %8 %23 0
|
||||
%25 = OpAccessChain %13 %20 %12
|
||||
%26 = OpLoad %9 %25
|
||||
%27 = OpCompositeExtract %8 %26 1
|
||||
%28 = OpIMul %8 %24 %27
|
||||
%29 = OpBitcast %1 %28
|
||||
%30 = OpAccessChain %31 %5 %12
|
||||
%32 = OpAccessChain %13 %20 %12
|
||||
%33 = OpLoad %9 %32
|
||||
%34 = OpCompositeExtract %8 %33 0
|
||||
%35 = OpAccessChain %13 %20 %12
|
||||
%36 = OpLoad %9 %35
|
||||
%37 = OpCompositeExtract %8 %36 1
|
||||
%38 = OpIMul %8 %34 %37
|
||||
%39 = OpAccessChain %40 %30 %38
|
||||
OpStore %39 %29
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
Reference in New Issue
Block a user