Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/sysgpu/shader/codegen/hlsl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ fn emitExpr(hlsl: *Hlsl, inst_idx: InstIndex) error{OutOfMemory}!void {
.texture_dimension => |inst| try hlsl.emitTextureDimension(inst),
.texture_load => |inst| try hlsl.emitTextureLoad(inst),
.texture_store => |inst| try hlsl.emitTextureStore(inst),
.select => |inst| try hlsl.emitSelect(inst),
//else => |inst| std.debug.panic("TODO: implement Air tag {s}", .{@tagName(inst)}),
else => |inst| std.debug.panic("Expr: {}", .{inst}), // TODO
}
Expand Down Expand Up @@ -1157,6 +1158,17 @@ fn emitTextureStore(hlsl: *Hlsl, inst: Inst.TextureStore) !void {
try hlsl.emitExpr(inst.value);
}

fn emitSelect(hlsl: *Hlsl, inst: Inst.BuiltinSelect) !void {
// WGSL select(f, t, cond) maps to HLSL (cond ? t : f)
try hlsl.writeAll("(");
try hlsl.emitExpr(inst.cond);
try hlsl.writeAll(" ? ");
try hlsl.emitExpr(inst.false);
try hlsl.writeAll(" : ");
try hlsl.emitExpr(inst.true);
try hlsl.writeAll(")");
}

fn enterScope(hlsl: *Hlsl) void {
hlsl.indent += 4;
}
Expand Down
20 changes: 19 additions & 1 deletion src/sysgpu/shader/codegen/msl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ fn emitDiscard(msl: *Msl) !void {
fn emitExpr(msl: *Msl, inst_idx: InstIndex) error{ OutOfMemory, ConstExpr }!void {
switch (msl.air.getInst(inst_idx)) {
.var_ref => |inst| try msl.emitVarRef(inst),
//.bool => |inst| msl.emitBool(inst),
.bool => |inst| try msl.emitBool(inst),
.int => |inst| try msl.emitInt(inst),
.float => |inst| try msl.emitFloat(inst),
.vector => |inst| try msl.emitVector(inst),
Expand All @@ -675,6 +675,7 @@ fn emitExpr(msl: *Msl, inst_idx: InstIndex) error{ OutOfMemory, ConstExpr }!void
.texture_dimension => |inst| try msl.emitTextureDimension(inst),
.texture_load => |inst| try msl.emitTextureLoad(inst),
.texture_store => |inst| try msl.emitTextureStore(inst),
.select => |inst| try msl.emitSelect(inst),
//else => |inst| std.debug.panic("TODO: implement Air tag {s}", .{@tagName(inst)}),
else => |inst| try msl.print("Expr: {}", .{inst}), // TODO
}
Expand Down Expand Up @@ -1092,6 +1093,23 @@ fn emitTextureStore(msl: *Msl, inst: Inst.TextureStore) !void {
try msl.writeAll(")");
}

fn emitSelect(msl: *Msl, inst: Inst.BuiltinSelect) !void {
try msl.writeAll("select(");
try msl.emitExpr(inst.true);
try msl.writeAll(", ");
try msl.emitExpr(inst.false);
try msl.writeAll(", ");
try msl.emitExpr(inst.cond);
try msl.writeAll(")");
}

fn emitBool(msl: *Msl, inst: Inst.Bool) !void {
switch (inst.value.?) {
.literal => |lit| try msl.writeAll(if (lit) "true" else "false"),
.cast => @panic("TODO: implement bool cast for MSL"),
}
}

fn enterScope(msl: *Msl) void {
msl.indent += 4;
}
Expand Down
20 changes: 20 additions & 0 deletions src/sysgpu/shader/codegen/spirv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,7 @@ fn emitExpr(spv: *SpirV, section: *Section, inst: InstIndex) error{OutOfMemory}!
.texture_sample => |ts| spv.emitTextureSample(section, ts),
.texture_dimension => |td| spv.emitTextureDimension(section, td),
.texture_load => |tl| spv.emitTextureLoad(section, tl),
.select => |sel| spv.emitSelect(section, sel),
else => std.debug.panic("TODO: implement Air tag {s}", .{@tagName(spv.air.getInst(inst))}),
};
}
Expand Down Expand Up @@ -2460,6 +2461,25 @@ fn emitArray(spv: *SpirV, section: *Section, inst: Inst.Array) !IdRef {
return id;
}

fn emitSelect(spv: *SpirV, section: *Section, inst: Inst.BuiltinSelect) !IdRef {
const cond_id = try spv.emitExpr(section, inst.cond);
const true_id = try spv.emitExpr(section, inst.true);
const false_id = try spv.emitExpr(section, inst.false);

const result_type_id = try spv.emitType(inst.type);

const id = spv.allocId();
try section.emit(.OpSelect, .{
.id_result_type = result_type_id,
.id_result = id,
.condition = cond_id,
.object_1 = false_id,
.object_2 = true_id,
});

return id;
}

const Key = union(enum) {
void_type,
bool_type,
Expand Down
9 changes: 9 additions & 0 deletions src/sysgpu/shader/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ test "vertexWriteGBuffers" {
// try expectCodegen(vertexWriteGBuffers, "triangle.glsl", .glsl, false);
}

test "select" {
const src = @embedFile("test/select.wgsl");

try expectCodegen(src, "select.spv", .spirv, false);
try expectCodegen(src, "select.hlsl", .hlsl, false);
try expectCodegen(src, "select.msl", .msl, false);
//try expectCodegen(src, "select.glsl", .glsl, false); // I guess no glsl based on the other tests
}

fn expectCodegen(
source: [:0]const u8,
comptime file_name: []const u8,
Expand Down
8 changes: 8 additions & 0 deletions src/sysgpu/shader/test/select.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@fragment
fn main() -> @location(0) vec4<f32> {
var cond = true;
var t = vec4<f32>(1.0, 1.0, 1.0, 1.0);
var f = vec4<f32>(0.0, 0.0, 0.0, 0.0);

return select(f, t, cond);
}