summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/backend/spirv/spirv_emit_context.cpp')
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.cpp81
1 files changed, 51 insertions, 30 deletions
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
index eb3cc23cc..0442adc83 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
@@ -74,20 +74,19 @@ spv::ImageFormat GetImageFormat(ImageFormat format) {
throw InvalidArgument("Invalid image format {}", format);
}
-Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) {
+Id ImageType(EmitContext& ctx, const ImageDescriptor& desc, Id sampled_type) {
const spv::ImageFormat format{GetImageFormat(desc.format)};
- const Id type{ctx.U32[1]};
switch (desc.type) {
case TextureType::Color1D:
- return ctx.TypeImage(type, spv::Dim::Dim1D, false, false, false, 2, format);
+ return ctx.TypeImage(sampled_type, spv::Dim::Dim1D, false, false, false, 2, format);
case TextureType::ColorArray1D:
- return ctx.TypeImage(type, spv::Dim::Dim1D, false, true, false, 2, format);
+ return ctx.TypeImage(sampled_type, spv::Dim::Dim1D, false, true, false, 2, format);
case TextureType::Color2D:
- return ctx.TypeImage(type, spv::Dim::Dim2D, false, false, false, 2, format);
+ return ctx.TypeImage(sampled_type, spv::Dim::Dim2D, false, false, false, 2, format);
case TextureType::ColorArray2D:
- return ctx.TypeImage(type, spv::Dim::Dim2D, false, true, false, 2, format);
+ return ctx.TypeImage(sampled_type, spv::Dim::Dim2D, false, true, false, 2, format);
case TextureType::Color3D:
- return ctx.TypeImage(type, spv::Dim::Dim3D, false, false, false, 2, format);
+ return ctx.TypeImage(sampled_type, spv::Dim::Dim3D, false, false, false, 2, format);
case TextureType::Buffer:
throw NotImplementedException("Image buffer");
default:
@@ -97,9 +96,9 @@ Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) {
}
Id DefineVariable(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin,
- spv::StorageClass storage_class) {
+ spv::StorageClass storage_class, std::optional<Id> initializer = std::nullopt) {
const Id pointer_type{ctx.TypePointer(storage_class, type)};
- const Id id{ctx.AddGlobalVariable(pointer_type, storage_class)};
+ const Id id{ctx.AddGlobalVariable(pointer_type, storage_class, initializer)};
if (builtin) {
ctx.Decorate(id, spv::Decoration::BuiltIn, *builtin);
}
@@ -145,11 +144,12 @@ Id DefineInput(EmitContext& ctx, Id type, bool per_invocation,
}
Id DefineOutput(EmitContext& ctx, Id type, std::optional<u32> invocations,
- std::optional<spv::BuiltIn> builtin = std::nullopt) {
+ std::optional<spv::BuiltIn> builtin = std::nullopt,
+ std::optional<Id> initializer = std::nullopt) {
if (invocations && ctx.stage == Stage::TessellationControl) {
type = ctx.TypeArray(type, ctx.Const(*invocations));
}
- return DefineVariable(ctx, type, builtin, spv::StorageClass::Output);
+ return DefineVariable(ctx, type, builtin, spv::StorageClass::Output, initializer);
}
void DefineGenericOutput(EmitContext& ctx, size_t index, std::optional<u32> invocations) {
@@ -812,10 +812,14 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
labels.push_back(OpLabel());
}
if (info.stores.ClipDistances()) {
- literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2);
- labels.push_back(OpLabel());
- literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2);
- labels.push_back(OpLabel());
+ if (profile.max_user_clip_distances >= 4) {
+ literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2);
+ labels.push_back(OpLabel());
+ }
+ if (profile.max_user_clip_distances >= 8) {
+ literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2);
+ labels.push_back(OpLabel());
+ }
}
OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
OpSwitch(compare_index, default_label, literals, labels);
@@ -844,17 +848,21 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
++label_index;
}
if (info.stores.ClipDistances()) {
- AddLabel(labels[label_index]);
- const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)};
- OpStore(pointer, store_value);
- OpReturn();
- ++label_index;
- AddLabel(labels[label_index]);
- const Id fixed_index{OpIAdd(U32[1], masked_index, Const(4U))};
- const Id pointer2{OpAccessChain(output_f32, clip_distances, fixed_index)};
- OpStore(pointer2, store_value);
- OpReturn();
- ++label_index;
+ if (profile.max_user_clip_distances >= 4) {
+ AddLabel(labels[label_index]);
+ const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)};
+ OpStore(pointer, store_value);
+ OpReturn();
+ ++label_index;
+ }
+ if (profile.max_user_clip_distances >= 8) {
+ AddLabel(labels[label_index]);
+ const Id fixed_index{OpIAdd(U32[1], masked_index, Const(4U))};
+ const Id pointer{OpAccessChain(output_f32, clip_distances, fixed_index)};
+ OpStore(pointer, store_value);
+ OpReturn();
+ ++label_index;
+ }
}
AddLabel(end_block);
OpUnreachable();
@@ -1273,7 +1281,9 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
throw NotImplementedException("Array of image buffers");
}
const spv::ImageFormat format{GetImageFormat(desc.format)};
- const Id image_type{TypeImage(U32[1], spv::Dim::Buffer, false, false, false, 2, format)};
+ const Id sampled_type{desc.is_integer ? U32[1] : F32[1]};
+ const Id image_type{
+ TypeImage(sampled_type, spv::Dim::Buffer, false, false, false, 2, format)};
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
Decorate(id, spv::Decoration::Binding, binding);
@@ -1283,6 +1293,7 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
.id = id,
.image_type = image_type,
.count = desc.count,
+ .is_integer = desc.is_integer,
});
if (profile.supported_spirv >= 0x00010400) {
interfaces.push_back(id);
@@ -1327,7 +1338,8 @@ void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_inde
if (desc.count != 1) {
throw NotImplementedException("Array of images");
}
- const Id image_type{ImageType(*this, desc)};
+ const Id sampled_type{desc.is_integer ? U32[1] : F32[1]};
+ const Id image_type{ImageType(*this, desc, sampled_type)};
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
Decorate(id, spv::Decoration::Binding, binding);
@@ -1337,6 +1349,7 @@ void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_inde
.id = id,
.image_type = image_type,
.count = desc.count,
+ .is_integer = desc.is_integer,
});
if (profile.supported_spirv >= 0x00010400) {
interfaces.push_back(id);
@@ -1528,8 +1541,16 @@ void EmitContext::DefineOutputs(const IR::Program& program) {
if (stage == Stage::Fragment) {
throw NotImplementedException("Storing ClipDistance in fragment stage");
}
- const Id type{TypeArray(F32[1], Const(8U))};
- clip_distances = DefineOutput(*this, type, invocations, spv::BuiltIn::ClipDistance);
+ if (profile.max_user_clip_distances > 0) {
+ const u32 used{std::min(profile.max_user_clip_distances, 8u)};
+ const std::array<Id, 8> zero{f32_zero_value, f32_zero_value, f32_zero_value,
+ f32_zero_value, f32_zero_value, f32_zero_value,
+ f32_zero_value, f32_zero_value};
+ const Id type{TypeArray(F32[1], Const(used))};
+ const Id initializer{ConstantComposite(type, std::span(zero).subspan(0, used))};
+ clip_distances =
+ DefineOutput(*this, type, invocations, spv::BuiltIn::ClipDistance, initializer);
+ }
}
if (info.stores[IR::Attribute::Layer] &&
(profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) {