authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2024-02-02 17:48:40+03:30
committergravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2024-02-05 11:55:14+03:30
logbe32ae0534e62ff9f4c285d5301d869924223e1c
tree6f59bf44d1fcf894c9e693afd72f2956417aac22
parent739108c9f00b0f2532cb9bc3794f4ce1de821ab1

std: add gpu namespace


2 files changed, 169 insertions(+), 0 deletions(-)

lib/std/gpu.zig created+166
......@@ -0,0 +1,166 @@
1const std = @import("std.zig");
2const comptimePrint = std.fmt.comptimePrint;
3
4/// Will make `ptr` contain the location of the current invocation within the
5/// global workgroup. Each component is equal to the index of the local workgroup
6/// multiplied by the size of the local workgroup plus `localInvocationId`.
7/// `ptr` must be a reference to variable or struct field.
8pub fn globalInvocationId(comptime ptr: *addrspace(.input) @Vector(3, u32)) void {
9 asm volatile (
10 \\OpDecorate %ptr BuiltIn GlobalInvocationId
11 :
12 : [ptr] "" (ptr),
13 );
14}
15
16/// Will make that variable contain the location of the current cluster
17/// culling, task, mesh, or compute shader invocation within the local
18/// workgroup. Each component ranges from zero through to the size of the
19/// workgroup in that dimension minus one.
20/// `ptr` must be a reference to variable or struct field.
21pub fn localInvocationId(comptime ptr: *addrspace(.input) @Vector(3, u32)) void {
22 asm volatile (
23 \\OpDecorate %ptr BuiltIn LocalInvocationId
24 :
25 : [ptr] "" (ptr),
26 );
27}
28
29/// Output vertex position from a `Vertex` entrypoint
30/// `ptr` must be a reference to variable or struct field.
31pub fn position(comptime ptr: *addrspace(.output) @Vector(4, f32)) void {
32 asm volatile (
33 \\OpDecorate %ptr BuiltIn Position
34 :
35 : [ptr] "" (ptr),
36 );
37}
38
39/// Will make `ptr` contain the index of the vertex that is
40/// being processed by the current vertex shader invocation.
41/// `ptr` must be a reference to variable or struct field.
42pub fn vertexIndex(comptime ptr: *addrspace(.input) u32) void {
43 asm volatile (
44 \\OpDecorate %ptr BuiltIn VertexIndex
45 :
46 : [ptr] "" (ptr),
47 );
48}
49
50/// Output fragment depth from a `Fragment` entrypoint
51/// `ptr` must be a reference to variable or struct field.
52pub fn fragmentCoord(comptime ptr: *addrspace(.input) @Vector(4, f32)) void {
53 asm volatile (
54 \\OpDecorate %ptr BuiltIn FragCoord
55 :
56 : [ptr] "" (ptr),
57 );
58}
59
60/// Output fragment depth from a `Fragment` entrypoint
61/// `ptr` must be a reference to variable or struct field.
62pub fn fragmentDepth(comptime ptr: *addrspace(.output) f32) void {
63 asm volatile (
64 \\OpDecorate %ptr BuiltIn FragDepth
65 :
66 : [ptr] "" (ptr),
67 );
68}
69
70/// Forms the main linkage for `input` and `output` address spaces.
71/// `ptr` must be a reference to variable or struct field.
72pub fn location(comptime ptr: anytype, comptime loc: u32) void {
73 const code = comptimePrint("OpDecorate %ptr Location {}", .{loc});
74 asm volatile (code
75 :
76 : [ptr] "" (ptr),
77 );
78}
79
80/// Forms the main linkage for `input` and `output` address spaces.
81/// `ptr` must be a reference to variable or struct field.
82pub fn binding(comptime ptr: anytype, comptime group: u32, comptime bind: u32) void {
83 const code = comptimePrint(
84 \\OpDecorate %ptr DescriptorSet {}
85 \\OpDecorate %ptr Binding {}
86 , .{ group, bind });
87 asm volatile (code
88 :
89 : [ptr] "" (ptr),
90 );
91}
92
93pub const Origin = enum(u32) {
94 /// Increase toward the right and downward
95 upper_left = 7,
96 /// Increase toward the right and upward
97 lower_left = 8,
98};
99
100/// The coordinates appear to originate in the specified `origin`.
101/// Only valid with the `Fragment` calling convention.
102pub fn fragmentOrigin(comptime entry_point: anytype, comptime origin: Origin) void {
103 const origin_enum = switch (origin) {
104 .upper_left => .OriginUpperLeft,
105 .lower_left => .OriginLowerLeft,
106 };
107 asm volatile ("OpExecutionMode %entry_point " ++ @tagName(origin_enum)
108 :
109 : [entry_point] "" (entry_point),
110 );
111}
112
113pub const DepthMode = enum(u32) {
114 /// Declares that this entry point dynamically writes the
115 /// `fragmentDepth` built in-decorated variable.
116 replacing = 12,
117 /// Indicates that per-fragment tests may assume that
118 /// any `fragmentDepth` built in-decorated value written by the shader is
119 /// greater-than-or-equal to the fragment’s interpolated depth value
120 greater = 14,
121 /// Indicates that per-fragment tests may assume that
122 /// any `fragmentDepth` built in-decorated value written by the shader is
123 /// less-than-or-equal to the fragment’s interpolated depth value
124 less = 15,
125 /// Indicates that per-fragment tests may assume that
126 /// any `fragmentDepth` built in-decorated value written by the shader is
127 /// the same as the fragment’s interpolated depth value
128 unchanged = 16,
129};
130
131/// Only valid with the `Fragment` calling convention.
132pub fn depthMode(comptime entry_point: anytype, comptime mode: DepthMode) void {
133 const code = comptimePrint("OpExecutionMode %entry_point {}", .{@intFromEnum(mode)});
134 asm volatile (code
135 :
136 : [entry_point] "" (entry_point),
137 );
138}
139
140/// Indicates the workgroup size in the `x`, `y`, and `z` dimensions.
141/// Only valid with the `GLCompute` or `Kernel` calling conventions.
142pub fn workgroupSize(comptime entry_point: anytype, comptime size: @Vector(3, u32)) void {
143 const code = comptimePrint("OpExecutionMode %entry_point LocalSize {} {} {}", .{
144 size[0],
145 size[1],
146 size[2],
147 });
148 asm volatile (code
149 :
150 : [entry_point] "" (entry_point),
151 );
152}
153
154/// A hint to the client, which indicates the workgroup size in the `x`, `y`, and `z` dimensions.
155/// Only valid with the `GLCompute` or `Kernel` calling conventions.
156pub fn workgroupSizeHint(comptime entry_point: anytype, comptime size: @Vector(3, u32)) void {
157 const code = comptimePrint("OpExecutionMode %entry_point LocalSizeHint {} {} {}", .{
158 size[0],
159 size[1],
160 size[2],
161 });
162 asm volatile (code
163 :
164 : [entry_point] "" (entry_point),
165 );
166}
lib/std/std.zig+3
......@@ -104,6 +104,9 @@ pub const fmt = @import("fmt.zig");
104104/// File system-related functionality.
105105pub const fs = @import("fs.zig");
106106
107/// GPU programming helpers.
108pub const gpu = @import("gpu.zig");
109
107110/// Fast hashing functions (i.e. not cryptographically secure).
108111pub const hash = @import("hash.zig");
109112pub const hash_map = @import("hash_map.zig");