跳转到内容
标准库Zig 0.17.1官方原文
源码std/spirv.zig

第 1 行

std.spirv.builtin

常量

第 1 行

std.spirv.controlBarrier

函数

第 56 行

pub fn controlBarrier( comptime execution: Scope, comptime memory: Scope, comptime semantics: MemorySemantics, ) void

参数

execution: Scope
memory: Scope
semantics: MemorySemantics

std.spirv.frag_coord

常量

第 9 行

@Vector(4, f32)

std.spirv.frag_depth

变量

第 15 行

f32

std.spirv.global_invocation_id

常量

第 20 行

@Vector(3, u32)

std.spirv.ImageCoordinate

类型函数

第 118 行

获取指定 SPIR-V 图像或采样图像坐标的类型。

fn ImageCoordinate(Image: type, Element: type) type

参数

Image: type
Element: type

std.spirv.imageQuerySize

函数

第 186 行

查询 image 的维度,不指定细节层级。

pub fn imageQuerySize( image: anytype, ) ImageCoordinate(std.meta.Child(@TypeOf(image)), u32)

std.spirv.ImageSampledType

类型函数

第 136 行

对给定 SPIR-V 图像或采样图像类型进行采样或读取后,所得分量的类型。

fn ImageSampledType(Image: type) type

参数

Image: type

std.spirv.imageSampleImplicitLod

函数

第 151 行

sampled_image 的类型必须是指向 SPIR-V 采样图像的指针。

pub fn imageSampleImplicitLod( sampled_image: anytype, coordinate: ImageCoordinate(std.meta.Child(@TypeOf(sampled_image)), f32), ) @Vector(4, ImageSampledType(std.meta.Child(@TypeOf(sampled_image))))

参数

coordinate: ImageCoordinate(std.meta.Child(@TypeOf(sampled_image)), f32)

std.spirv.imageWrite

函数

第 220 行

不使用采样器向图像写入纹素。 image 的类型必须是指向 SPIR-V 图像的指针。

pub fn imageWrite( image: anytype, T: type, coordinate: ImageCoordinate(std.meta.Child(@TypeOf(image)), T), texel: @Vector(4, ImageSampledType(std.meta.Child(@TypeOf(image)))), ) void

参数

T: type
coordinate: ImageCoordinate(std.meta.Child(@TypeOf(image)), T)
texel: @Vector(4, ImageSampledType(std.meta.Child(@TypeOf(image))))

std.spirv.instance_index

常量

第 22 行

u32

std.spirv.invocation_id

常量

第 8 行

u32

std.spirv.local_invocation_id

常量

第 19 行

@Vector(3, u32)

std.spirv.memoryBarrier

函数

第 70 行

pub fn memoryBarrier(comptime memory: Scope, comptime semantics: MemorySemantics) void

参数

memory: Scope
semantics: MemorySemantics

std.spirv.MemorySemantics

容器

第 34 行

字段

_reserved_bit_0: bool = false
acquire: bool = false
release: bool = false
acquire_release: bool = false
sequentially_consistent: bool = false
_reserved_bit_5: bool = false
uniform_memory: bool = false
subgroup_memory: bool = false
workgroup_memory: bool = false
cross_workgroup_memory: bool = false
atomic_counter_memory: bool = false
image_memory: bool = false
output_memory: bool = false
make_available: bool = false
make_visible: bool = false
@"volatile": bool = false
_reserved: u16 = 0

std.spirv.MemorySemantics.none

常量

第 53 行

MemorySemantics

std.spirv.num_workgroups

常量

第 16 行

@Vector(3, u32)

std.spirv.point_coord

常量

第 10 行

@Vector(2, f32)

std.spirv.point_size_in

常量

第 6 行

std.spirv.point_size_out

常量

第 7 行

std.spirv.position_in

常量

第 4 行

std.spirv.position_out

常量

第 5 行

std.spirv.Scope

容器

第 24 行

字段

cross_device = 0
device = 1
workgroup = 2
subgroup = 3
invocation = 4
queue_family = 5
shader_call_khr = 6

std.spirv.setMeshOutputs

函数

第 265 行

pub fn setMeshOutputs(vertex_count: u32, primitive_count: u32) void

参数

vertex_count: u32
primitive_count: u32

std.spirv.specConst

函数

第 87 行

pub fn specConst(T: type, comptime default_value: T, comptime spec_id: u32) T

参数

T: type
default_value: T
spec_id: u32

std.spirv.std

别名

第 2 行

别名目标 std

std.spirv.vertex_index

常量

第 21 行

u32

std.spirv.workgroup_id

常量

第 18 行

@Vector(3, u32)

std.spirv.workgroup_size

常量

第 17 行

@Vector(3, u32)

std.spirv.workgroupBarrier

函数

第 79 行

pub fn workgroupBarrier() void
源码

std/spirv.zig

const builtin = @import("builtin");
const std = @import("std.zig");

pub const position_in = @extern(*addrspace(.input) @Vector(4, f32), .{ .name = "position" });
pub const position_out = @extern(*addrspace(.output) @Vector(4, f32), .{ .name = "position" });
pub const point_size_in = @extern(*addrspace(.input) f32, .{ .name = "point_size" });
pub const point_size_out = @extern(*addrspace(.output) f32, .{ .name = "point_size" });
pub extern const invocation_id: u32 addrspace(.input);
pub extern const frag_coord: @Vector(4, f32) addrspace(.input);
pub extern const point_coord: @Vector(2, f32) addrspace(.input);
// TODO: 直接/间接值
// pub extern const front_facing: bool addrspace(.input);
// TODO: 运行时数组
// pub extern const sample_mask;
pub extern var frag_depth: f32 addrspace(.output);
pub extern const num_workgroups: @Vector(3, u32) addrspace(.input);
pub extern const workgroup_size: @Vector(3, u32) addrspace(.input);
pub extern const workgroup_id: @Vector(3, u32) addrspace(.input);
pub extern const local_invocation_id: @Vector(3, u32) addrspace(.input);
pub extern const global_invocation_id: @Vector(3, u32) addrspace(.input);
pub extern const vertex_index: u32 addrspace(.input);
pub extern const instance_index: u32 addrspace(.input);

pub const Scope = enum(u32) {
    cross_device = 0,
    device = 1,
    workgroup = 2,
    subgroup = 3,
    invocation = 4,
    queue_family = 5,
    shader_call_khr = 6,
};

pub const MemorySemantics = packed struct(u32) {
    _reserved_bit_0: bool = false,
    acquire: bool = false,
    release: bool = false,
    acquire_release: bool = false,
    sequentially_consistent: bool = false,
    _reserved_bit_5: bool = false,
    uniform_memory: bool = false,
    subgroup_memory: bool = false,
    workgroup_memory: bool = false,
    cross_workgroup_memory: bool = false,
    atomic_counter_memory: bool = false,
    image_memory: bool = false,
    output_memory: bool = false,
    make_available: bool = false,
    make_visible: bool = false,
    @"volatile": bool = false,
    _reserved: u16 = 0,

    pub const none: MemorySemantics = .{};
};

pub fn controlBarrier(
    comptime execution: Scope,
    comptime memory: Scope,
    comptime semantics: MemorySemantics,
) void {
    asm volatile (
        \\OpControlBarrier %exec %mem %sem
        :
        : [exec] "" (@as(u32, @backingInt(execution))),
          [mem] "" (@as(u32, @backingInt(memory))),
          [sem] "" (@as(u32, @bitCast(semantics))),
    );
}

pub fn memoryBarrier(comptime memory: Scope, comptime semantics: MemorySemantics) void {
    asm volatile (
        \\OpMemoryBarrier %mem %sem
        :
        : [mem] "" (@as(u32, @backingInt(memory))),
          [sem] "" (@as(u32, @bitCast(semantics))),
    );
}

pub fn workgroupBarrier() void {
    controlBarrier(
        .workgroup,
        .workgroup,
        .{ .acquire_release = true, .workgroup_memory = true },
    );
}

pub fn specConst(T: type, comptime default_value: T, comptime spec_id: u32) T {
    switch (@typeInfo(T)) {
        .bool => {
            const op = if (default_value) "OpSpecConstantTrue" else "OpSpecConstantFalse";
            return asm ("%ret = " ++ op ++ " %ty\n" ++
                    "OpDecorate %ret SpecId $spec_id"
                : [ret] "" (-> T),
                : [ty] "t" (T),
                  [spec_id] "c" (spec_id),
            );
        },
        .int, .float => return asm (
            \\%ret = OpSpecConstant %ty $default_value
            \\       OpDecorate %ret SpecId $spec_id
            : [ret] "" (-> T),
            : [ty] "t" (T),
              [default_value] "c" (default_value),
              [spec_id] "c" (spec_id),
        ),
        .vector => return asm (
            \\%ret = OpSpecConstantComposite %ty %default_value %spec_id
            : [ret] "" (-> T),
            : [ty] "t" (T),
              [default_value] "c" (default_value),
              [spec_id] "c" (spec_id),
        ),
        else => @compileError("Invalid spec-constant type"),
    }
}

/// 获取指定 SPIR-V 图像或采样图像坐标的类型。
fn ImageCoordinate(Image: type, Element: type) type {
    const image_info = switch (@typeInfo(Image)) {
        .spirv => |spirv| switch (spirv) {
            .sampled_image => |sampled_image| @typeInfo(sampled_image).spirv.image,
            .image => |image| image,
            else => @compileError("Expected SPIR-V image or sampled image type, found '" ++ @typeName(Image) ++ "'"),
        },
        else => @compileError("Expected SPIR-V image or sampled image type, found '" ++ @typeName(Image) ++ "'"),
    };
    const dim = switch (image_info.dim) {
        .@"1d" => 1 + @as(u8, @intFromBool(image_info.arrayed)),
        .@"2d" => 2 + @as(u8, @intFromBool(image_info.arrayed)),
        .@"3d", .cube => 3 + @as(u8, @intFromBool(image_info.arrayed)),
    };
    if (dim == 1) return Element else return @Vector(dim, Element);
}

/// 对给定 SPIR-V 图像或采样图像类型进行采样或读取后,所得分量的类型。
fn ImageSampledType(Image: type) type {
    const image_info = switch (@typeInfo(Image)) {
        .spirv => |spirv| switch (spirv) {
            .sampled_image => |sampled_image| @typeInfo(sampled_image).spirv.image,
            .image => |image| image,
            else => @compileError("Expected SPIR-V image or sampled image type, found '" ++ @typeName(Image) ++ "'"),
        },
        else => @compileError("Expected SPIR-V image or sampled image type, found '" ++ @typeName(Image) ++ "'"),
    };
    return switch (image_info.usage) {
        inline else => |usage| usage,
    };
}

/// `sampled_image` 的类型必须是指向 SPIR-V 采样图像的指针。
pub fn imageSampleImplicitLod(
    sampled_image: anytype,
    coordinate: ImageCoordinate(std.meta.Child(@TypeOf(sampled_image)), f32),
) @Vector(4, ImageSampledType(std.meta.Child(@TypeOf(sampled_image)))) {
    const SampledImage = switch (@typeInfo(@TypeOf(sampled_image))) {
        .pointer => |pointer| pointer.child,
        else => @compileError("Expected a pointer to SPIR-V sampled image type, found '" ++ @typeName(@TypeOf(sampled_image)) ++ "'"),
    };
    const Result = @Vector(4, ImageSampledType(SampledImage));

    const image_info = switch (@typeInfo(SampledImage)) {
        .spirv => |spirv| switch (spirv) {
            .sampled_image => |sampled_image_info| @typeInfo(sampled_image_info).spirv.image,
            else => @compileError("Expected SPIR-V sampled image type, found '" ++ @typeName(SampledImage) ++ "'"),
        },
        else => @compileError("Expected SPIR-V sampled image type, found '" ++ @typeName(SampledImage) ++ "'"),
    };

    if (image_info.multisampled)
        @compileError("Can not implicitly sample a sampled image that was multisampled");

    // TOOD:若添加 buffer 维度,则在维度为缓冲区时发出编译错误。

    return asm volatile (
        \\%loaded_sampler = OpLoad %SampledImage %sampled_image
        \\%ret            = OpImageSampleImplicitLod %Result %loaded_sampler %coordinate
        : [ret] "" (-> Result),
        : [SampledImage] "t" (SampledImage),
          [sampled_image] "" (sampled_image),
          [Result] "t" (Result),
          [coordinate] "" (coordinate),
    );
}

/// 查询 `image` 的维度,不指定细节层级。
pub fn imageQuerySize(
    image: anytype,
) ImageCoordinate(std.meta.Child(@TypeOf(image)), u32) {
    const Image = switch (@typeInfo(@TypeOf(image))) {
        .pointer => |pointer| pointer.child,
        else => @compileError("Expected a pointer to SPIR-V image type, found '" ++ @typeName(@TypeOf(image)) ++ "'"),
    };

    const image_info = switch (@typeInfo(Image)) {
        .spirv => |spirv| switch (spirv) {
            .image => |info| info,
            else => @compileError("Expected SPIR-V image type, found '" ++ @typeName(Image) ++ "'"),
        },
        else => @compileError("Expected SPIR-V image type, found '" ++ @typeName(Image) ++ "'"),
    };

    // TODO: 若维度不是 1d、2d、3d 或 cube,则移除此检查(以防添加 buffer)。
    if (!image_info.multisampled and image_info.usage != .unknown and image_info.usage != .storage)
        @compileError("SPIR-V image must be either be multisampled or have an unknown or storage usage");

    const Result = ImageCoordinate(std.meta.Child(@TypeOf(image)), u32);

    return asm volatile (
        \\%loaded_image = OpLoad %Image %image
        \\%ret          = OpImageQuerySize %Result %loaded_image
        : [ret] "" (-> Result),
        : [Image] "t" (Image),
          [image] "" (image),
          [Result] "t" (Result),
    );
}

/// 不使用采样器向图像写入纹素。
/// `image` 的类型必须是指向 SPIR-V 图像的指针。
pub fn imageWrite(
    image: anytype,
    T: type,
    coordinate: ImageCoordinate(std.meta.Child(@TypeOf(image)), T),
    texel: @Vector(4, ImageSampledType(std.meta.Child(@TypeOf(image)))),
) void {
    switch (T) {
        u32, i32 => {},
        f32 => if (builtin.target.os.tag != .opencl) {
            @compileError("Floating point image coordinates only supported by OpenCL");
        },
        else => @compileError("Expected one of u32, i32 and f32 types. Found '" ++ @typeName(T) ++ "'"),
    }

    const Image = switch (@typeInfo(@TypeOf(image))) {
        .pointer => |pointer| pointer.child,
        else => @compileError("Expected a pointer to SPIR-V image type, found '" ++ @typeName(@TypeOf(image)) ++ "'"),
    };

    const image_info = switch (@typeInfo(Image)) {
        .spirv => |spirv| switch (spirv) {
            .image => |info| info,
            else => @compileError("Expected SPIR-V image type, found '" ++ @typeName(Image) ++ "'"),
        },
        else => @compileError("Expected SPIR-V image type, found '" ++ @typeName(Image) ++ "'"),
    };

    switch (image_info.usage) {
        .unknown, .storage => {},
        else => @compileError("SPIR-V image must have unknown or storage usage"),
    }

    // TODO: 若添加 SubpassData 维度,当图像为数组且具有 SubpassData 维度时,产生编译错误。

    return asm volatile (
        \\%loaded_image = OpLoad %Image %image
        \\                OpImageWrite %loaded_image %coordinate %texel
        :
        : [Image] "t" (Image),
          [image] "" (image),
          [coordinate] "" (coordinate),
          [texel] "" (texel),
    );
}

pub fn setMeshOutputs(vertex_count: u32, primitive_count: u32) void {
    asm volatile (
        \\OpSetMeshOutputsEXT %vertex_count %primitive_count
        :
        : [vertex_count] "" (vertex_count),
          [primitive_count] "" (primitive_count),
    );
}