|
|
|
@ -7,13 +7,18 @@ |
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV { |
|
|
|
namespace { |
|
|
|
Id Image(EmitContext& ctx, IR::TextureInstInfo info) { |
|
|
|
struct ImageInfo { |
|
|
|
Id id; |
|
|
|
bool is_signed; |
|
|
|
}; |
|
|
|
|
|
|
|
ImageInfo Image(EmitContext& ctx, IR::TextureInstInfo info) { |
|
|
|
if (info.type == TextureType::Buffer) { |
|
|
|
const ImageBufferDefinition def{ctx.image_buffers.at(info.descriptor_index)}; |
|
|
|
return def.id; |
|
|
|
return {def.id, def.is_signed}; |
|
|
|
} else { |
|
|
|
const ImageDefinition def{ctx.images.at(info.descriptor_index)}; |
|
|
|
return def.id; |
|
|
|
return {def.id, def.is_signed}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -24,42 +29,66 @@ std::pair<Id, Id> AtomicArgs(EmitContext& ctx) { |
|
|
|
} |
|
|
|
|
|
|
|
Id ImageAtomicU32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id value, |
|
|
|
Id (Sirit::Module::*atomic_func)(Id, Id, Id, Id, Id)) { |
|
|
|
Id (Sirit::Module::*atomic_func)(Id, Id, Id, Id, Id), bool value_signed) { |
|
|
|
if (!index.IsImmediate() || index.U32() != 0) { |
|
|
|
// TODO: handle layers
|
|
|
|
throw NotImplementedException("Image indexing"); |
|
|
|
} |
|
|
|
const auto info{inst->Flags<IR::TextureInstInfo>()}; |
|
|
|
const Id image{Image(ctx, info)}; |
|
|
|
const Id pointer{ctx.OpImageTexelPointer(ctx.image_u32, image, coords, ctx.Const(0U))}; |
|
|
|
const auto image_info{Image(ctx, info)}; |
|
|
|
Id pointer_type{image_info.is_signed ? ctx.image_s32 : ctx.image_u32}; |
|
|
|
if (!Sirit::ValidId(pointer_type)) { |
|
|
|
const Id element_type{image_info.is_signed ? ctx.S32[1] : ctx.U32[1]}; |
|
|
|
pointer_type = ctx.TypePointer(spv::StorageClass::Image, element_type); |
|
|
|
} |
|
|
|
const Id image{image_info.id}; |
|
|
|
const Id pointer{ctx.OpImageTexelPointer(pointer_type, image, coords, ctx.Const(0U))}; |
|
|
|
const auto [scope, semantics]{AtomicArgs(ctx)}; |
|
|
|
return (ctx.*atomic_func)(ctx.U32[1], pointer, scope, semantics, value); |
|
|
|
const Id result_type{image_info.is_signed ? ctx.S32[1] : ctx.U32[1]}; |
|
|
|
|
|
|
|
// Ensure value type matches result_type's pointee type
|
|
|
|
Id cast_value{value}; |
|
|
|
if (image_info.is_signed) { |
|
|
|
// Result type is signed s32, ensure value is also s32
|
|
|
|
cast_value = ctx.OpBitcast(ctx.S32[1], value); |
|
|
|
} else { |
|
|
|
// Result type is unsigned u32, ensure value is also u32
|
|
|
|
cast_value = ctx.OpBitcast(ctx.U32[1], value); |
|
|
|
} |
|
|
|
|
|
|
|
Id result{(ctx.*atomic_func)(result_type, pointer, scope, semantics, cast_value)}; |
|
|
|
|
|
|
|
// Convert result back to u32 for IR compatibility
|
|
|
|
if (image_info.is_signed) { |
|
|
|
result = ctx.OpBitcast(ctx.U32[1], result); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
Id EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicIAdd); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicIAdd, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicSMin32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicSMin); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicSMin, true); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicUMin32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicUMin); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicUMin, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicSMax32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicSMax); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicSMax, true); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicUMax32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicUMax); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicUMax, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicInc32(EmitContext&, IR::Inst*, const IR::Value&, Id, Id) { |
|
|
|
@ -74,22 +103,22 @@ Id EmitImageAtomicDec32(EmitContext&, IR::Inst*, const IR::Value&, Id, Id) { |
|
|
|
|
|
|
|
Id EmitImageAtomicAnd32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicAnd); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicAnd, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicOr32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicOr); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicOr, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicXor32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicXor); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicXor, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, |
|
|
|
Id value) { |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicExchange); |
|
|
|
return ImageAtomicU32(ctx, inst, index, coords, value, &Sirit::Module::OpAtomicExchange, false); |
|
|
|
} |
|
|
|
|
|
|
|
Id EmitBindlessImageAtomicIAdd32(EmitContext&) { |
|
|
|
|