Skip to content

SPIRV Target #24645

Description

@mozbeel

Zig Version

0.14.1

Steps to Reproduce and Observed Behavior

I'm trying to compile for SPIR-V to pair with spirv-cross:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const spirv_target = b.resolveTargetQuery(.{
        .cpu_arch = .spirv32,
        .os_tag = .vulkan,
        .cpu_model = .{ .explicit = &std.Target.spirv.cpu.vulkan_v1_2 },
        .ofmt = .spirv,
    });

    // CHANGE: Use b.addObject instead of b.addExecutable
    
    const spirv_shader_mod = b.addModule("vert_shader", .{
        .target = spirv_target,
        .optimize = .ReleaseFast,
        .root_source_file = b.path("src/shaders/vertex.zig"),
    });

    const spirv_shader_object = b.addObject(.{
        .name = "vert_shader",
        .root_module = spirv_shader_mod, 
        .use_llvm = false, // Critical for the self-hosted SPIR-V backend
    });

    // Install the generated SPIR-V file.
    // The .getEmittedBin() method retrieves the path to the compiled binary.
    const install_shader_step = b.addInstallFile(spirv_shader_object.getEmittedBin(), "vert_shader.spv");

    install_shader_step.step.dependOn(&spirv_shader_object.step);

    b.getInstallStep().dependOn(&install_shader_step.step);
}

And I'm getting this error:

error: error: compiler backend unavailable for the specified target

Is there a fix for this?

Expected Behavior

I expected to see a vert_shader.spv in zig-out/ compiled from this shader:

const std = @import("std");
const gpu = std.gpu;

extern const a_pos: @Vector(2, f32) addrspace(.input);
extern const a_color: @Vector(3, f32) addrspace(.input);

extern var v_color: @Vector(3, f32) addrspace(.output);

export fn main() callconv(.spirv_vertex) @Vector(4, f32) {
    gpu.location(&a_pos, 0);
    gpu.location(&a_color, 1);
    gpu.location(&v_color, 0);

    v_color = a_color;

    return @Vector(4, f32){ a_pos[0], a_pos[1], a_pos[2] };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions