diff options
Diffstat (limited to 'src/shader_recompiler/backend')
4 files changed, 74 insertions, 36 deletions
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index e5a78a914..feca5105f 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp | |||
| @@ -74,6 +74,11 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) { | |||
| 74 | case IR::Attribute::ClipDistance7: { | 74 | case IR::Attribute::ClipDistance7: { |
| 75 | const u32 base{static_cast<u32>(IR::Attribute::ClipDistance0)}; | 75 | const u32 base{static_cast<u32>(IR::Attribute::ClipDistance0)}; |
| 76 | const u32 index{static_cast<u32>(attr) - base}; | 76 | const u32 index{static_cast<u32>(attr) - base}; |
| 77 | if (index >= ctx.profile.max_user_clip_distances) { | ||
| 78 | LOG_WARNING(Shader, "Ignoring clip distance store {} >= {} supported", index, | ||
| 79 | ctx.profile.max_user_clip_distances); | ||
| 80 | return std::nullopt; | ||
| 81 | } | ||
| 77 | const Id clip_num{ctx.Const(index)}; | 82 | const Id clip_num{ctx.Const(index)}; |
| 78 | return OutputAccessChain(ctx, ctx.output_f32, ctx.clip_distances, clip_num); | 83 | return OutputAccessChain(ctx, ctx.output_f32, ctx.clip_distances, clip_num); |
| 79 | } | 84 | } |
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp index 22ceca19c..800754554 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp | |||
| @@ -214,16 +214,16 @@ Id TextureImage(EmitContext& ctx, IR::TextureInstInfo info, const IR::Value& ind | |||
| 214 | } | 214 | } |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | Id Image(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) { | 217 | std::pair<Id, bool> Image(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) { |
| 218 | if (!index.IsImmediate() || index.U32() != 0) { | 218 | if (!index.IsImmediate() || index.U32() != 0) { |
| 219 | throw NotImplementedException("Indirect image indexing"); | 219 | throw NotImplementedException("Indirect image indexing"); |
| 220 | } | 220 | } |
| 221 | if (info.type == TextureType::Buffer) { | 221 | if (info.type == TextureType::Buffer) { |
| 222 | const ImageBufferDefinition def{ctx.image_buffers.at(info.descriptor_index)}; | 222 | const ImageBufferDefinition def{ctx.image_buffers.at(info.descriptor_index)}; |
| 223 | return ctx.OpLoad(def.image_type, def.id); | 223 | return {ctx.OpLoad(def.image_type, def.id), def.is_integer}; |
| 224 | } else { | 224 | } else { |
| 225 | const ImageDefinition def{ctx.images.at(info.descriptor_index)}; | 225 | const ImageDefinition def{ctx.images.at(info.descriptor_index)}; |
| 226 | return ctx.OpLoad(def.image_type, def.id); | 226 | return {ctx.OpLoad(def.image_type, def.id), def.is_integer}; |
| 227 | } | 227 | } |
| 228 | } | 228 | } |
| 229 | 229 | ||
| @@ -566,13 +566,23 @@ Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id co | |||
| 566 | LOG_WARNING(Shader_SPIRV, "Typeless image read not supported by host"); | 566 | LOG_WARNING(Shader_SPIRV, "Typeless image read not supported by host"); |
| 567 | return ctx.ConstantNull(ctx.U32[4]); | 567 | return ctx.ConstantNull(ctx.U32[4]); |
| 568 | } | 568 | } |
| 569 | return Emit(&EmitContext::OpImageSparseRead, &EmitContext::OpImageRead, ctx, inst, ctx.U32[4], | 569 | const auto [image, is_integer] = Image(ctx, index, info); |
| 570 | Image(ctx, index, info), coords, std::nullopt, std::span<const Id>{}); | 570 | const Id result_type{is_integer ? ctx.U32[4] : ctx.F32[4]}; |
| 571 | Id color{Emit(&EmitContext::OpImageSparseRead, &EmitContext::OpImageRead, ctx, inst, | ||
| 572 | result_type, image, coords, std::nullopt, std::span<const Id>{})}; | ||
| 573 | if (!is_integer) { | ||
| 574 | color = ctx.OpBitcast(ctx.U32[4], color); | ||
| 575 | } | ||
| 576 | return color; | ||
| 571 | } | 577 | } |
| 572 | 578 | ||
| 573 | void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color) { | 579 | void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color) { |
| 574 | const auto info{inst->Flags<IR::TextureInstInfo>()}; | 580 | const auto info{inst->Flags<IR::TextureInstInfo>()}; |
| 575 | ctx.OpImageWrite(Image(ctx, index, info), coords, color); | 581 | const auto [image, is_integer] = Image(ctx, index, info); |
| 582 | if (!is_integer) { | ||
| 583 | color = ctx.OpBitcast(ctx.F32[4], color); | ||
| 584 | } | ||
| 585 | ctx.OpImageWrite(image, coords, color); | ||
| 576 | } | 586 | } |
| 577 | 587 | ||
| 578 | Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) { | 588 | Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) { |
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) { | |||
| 74 | throw InvalidArgument("Invalid image format {}", format); | 74 | throw InvalidArgument("Invalid image format {}", format); |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) { | 77 | Id ImageType(EmitContext& ctx, const ImageDescriptor& desc, Id sampled_type) { |
| 78 | const spv::ImageFormat format{GetImageFormat(desc.format)}; | 78 | const spv::ImageFormat format{GetImageFormat(desc.format)}; |
| 79 | const Id type{ctx.U32[1]}; | ||
| 80 | switch (desc.type) { | 79 | switch (desc.type) { |
| 81 | case TextureType::Color1D: | 80 | case TextureType::Color1D: |
| 82 | return ctx.TypeImage(type, spv::Dim::Dim1D, false, false, false, 2, format); | 81 | return ctx.TypeImage(sampled_type, spv::Dim::Dim1D, false, false, false, 2, format); |
| 83 | case TextureType::ColorArray1D: | 82 | case TextureType::ColorArray1D: |
| 84 | return ctx.TypeImage(type, spv::Dim::Dim1D, false, true, false, 2, format); | 83 | return ctx.TypeImage(sampled_type, spv::Dim::Dim1D, false, true, false, 2, format); |
| 85 | case TextureType::Color2D: | 84 | case TextureType::Color2D: |
| 86 | return ctx.TypeImage(type, spv::Dim::Dim2D, false, false, false, 2, format); | 85 | return ctx.TypeImage(sampled_type, spv::Dim::Dim2D, false, false, false, 2, format); |
| 87 | case TextureType::ColorArray2D: | 86 | case TextureType::ColorArray2D: |
| 88 | return ctx.TypeImage(type, spv::Dim::Dim2D, false, true, false, 2, format); | 87 | return ctx.TypeImage(sampled_type, spv::Dim::Dim2D, false, true, false, 2, format); |
| 89 | case TextureType::Color3D: | 88 | case TextureType::Color3D: |
| 90 | return ctx.TypeImage(type, spv::Dim::Dim3D, false, false, false, 2, format); | 89 | return ctx.TypeImage(sampled_type, spv::Dim::Dim3D, false, false, false, 2, format); |
| 91 | case TextureType::Buffer: | 90 | case TextureType::Buffer: |
| 92 | throw NotImplementedException("Image buffer"); | 91 | throw NotImplementedException("Image buffer"); |
| 93 | default: | 92 | default: |
| @@ -97,9 +96,9 @@ Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) { | |||
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | Id DefineVariable(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin, | 98 | Id DefineVariable(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin, |
| 100 | spv::StorageClass storage_class) { | 99 | spv::StorageClass storage_class, std::optional<Id> initializer = std::nullopt) { |
| 101 | const Id pointer_type{ctx.TypePointer(storage_class, type)}; | 100 | const Id pointer_type{ctx.TypePointer(storage_class, type)}; |
| 102 | const Id id{ctx.AddGlobalVariable(pointer_type, storage_class)}; | 101 | const Id id{ctx.AddGlobalVariable(pointer_type, storage_class, initializer)}; |
| 103 | if (builtin) { | 102 | if (builtin) { |
| 104 | ctx.Decorate(id, spv::Decoration::BuiltIn, *builtin); | 103 | ctx.Decorate(id, spv::Decoration::BuiltIn, *builtin); |
| 105 | } | 104 | } |
| @@ -145,11 +144,12 @@ Id DefineInput(EmitContext& ctx, Id type, bool per_invocation, | |||
| 145 | } | 144 | } |
| 146 | 145 | ||
| 147 | Id DefineOutput(EmitContext& ctx, Id type, std::optional<u32> invocations, | 146 | Id DefineOutput(EmitContext& ctx, Id type, std::optional<u32> invocations, |
| 148 | std::optional<spv::BuiltIn> builtin = std::nullopt) { | 147 | std::optional<spv::BuiltIn> builtin = std::nullopt, |
| 148 | std::optional<Id> initializer = std::nullopt) { | ||
| 149 | if (invocations && ctx.stage == Stage::TessellationControl) { | 149 | if (invocations && ctx.stage == Stage::TessellationControl) { |
| 150 | type = ctx.TypeArray(type, ctx.Const(*invocations)); | 150 | type = ctx.TypeArray(type, ctx.Const(*invocations)); |
| 151 | } | 151 | } |
| 152 | return DefineVariable(ctx, type, builtin, spv::StorageClass::Output); | 152 | return DefineVariable(ctx, type, builtin, spv::StorageClass::Output, initializer); |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | void DefineGenericOutput(EmitContext& ctx, size_t index, std::optional<u32> invocations) { | 155 | void DefineGenericOutput(EmitContext& ctx, size_t index, std::optional<u32> invocations) { |
| @@ -812,10 +812,14 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) { | |||
| 812 | labels.push_back(OpLabel()); | 812 | labels.push_back(OpLabel()); |
| 813 | } | 813 | } |
| 814 | if (info.stores.ClipDistances()) { | 814 | if (info.stores.ClipDistances()) { |
| 815 | literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2); | 815 | if (profile.max_user_clip_distances >= 4) { |
| 816 | labels.push_back(OpLabel()); | 816 | literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2); |
| 817 | literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2); | 817 | labels.push_back(OpLabel()); |
| 818 | labels.push_back(OpLabel()); | 818 | } |
| 819 | if (profile.max_user_clip_distances >= 8) { | ||
| 820 | literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2); | ||
| 821 | labels.push_back(OpLabel()); | ||
| 822 | } | ||
| 819 | } | 823 | } |
| 820 | OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone); | 824 | OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone); |
| 821 | OpSwitch(compare_index, default_label, literals, labels); | 825 | OpSwitch(compare_index, default_label, literals, labels); |
| @@ -844,17 +848,21 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) { | |||
| 844 | ++label_index; | 848 | ++label_index; |
| 845 | } | 849 | } |
| 846 | if (info.stores.ClipDistances()) { | 850 | if (info.stores.ClipDistances()) { |
| 847 | AddLabel(labels[label_index]); | 851 | if (profile.max_user_clip_distances >= 4) { |
| 848 | const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)}; | 852 | AddLabel(labels[label_index]); |
| 849 | OpStore(pointer, store_value); | 853 | const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)}; |
| 850 | OpReturn(); | 854 | OpStore(pointer, store_value); |
| 851 | ++label_index; | 855 | OpReturn(); |
| 852 | AddLabel(labels[label_index]); | 856 | ++label_index; |
| 853 | const Id fixed_index{OpIAdd(U32[1], masked_index, Const(4U))}; | 857 | } |
| 854 | const Id pointer2{OpAccessChain(output_f32, clip_distances, fixed_index)}; | 858 | if (profile.max_user_clip_distances >= 8) { |
| 855 | OpStore(pointer2, store_value); | 859 | AddLabel(labels[label_index]); |
| 856 | OpReturn(); | 860 | const Id fixed_index{OpIAdd(U32[1], masked_index, Const(4U))}; |
| 857 | ++label_index; | 861 | const Id pointer{OpAccessChain(output_f32, clip_distances, fixed_index)}; |
| 862 | OpStore(pointer, store_value); | ||
| 863 | OpReturn(); | ||
| 864 | ++label_index; | ||
| 865 | } | ||
| 858 | } | 866 | } |
| 859 | AddLabel(end_block); | 867 | AddLabel(end_block); |
| 860 | OpUnreachable(); | 868 | OpUnreachable(); |
| @@ -1273,7 +1281,9 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) { | |||
| 1273 | throw NotImplementedException("Array of image buffers"); | 1281 | throw NotImplementedException("Array of image buffers"); |
| 1274 | } | 1282 | } |
| 1275 | const spv::ImageFormat format{GetImageFormat(desc.format)}; | 1283 | const spv::ImageFormat format{GetImageFormat(desc.format)}; |
| 1276 | const Id image_type{TypeImage(U32[1], spv::Dim::Buffer, false, false, false, 2, format)}; | 1284 | const Id sampled_type{desc.is_integer ? U32[1] : F32[1]}; |
| 1285 | const Id image_type{ | ||
| 1286 | TypeImage(sampled_type, spv::Dim::Buffer, false, false, false, 2, format)}; | ||
| 1277 | const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)}; | 1287 | const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)}; |
| 1278 | const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)}; | 1288 | const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)}; |
| 1279 | Decorate(id, spv::Decoration::Binding, binding); | 1289 | Decorate(id, spv::Decoration::Binding, binding); |
| @@ -1283,6 +1293,7 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) { | |||
| 1283 | .id = id, | 1293 | .id = id, |
| 1284 | .image_type = image_type, | 1294 | .image_type = image_type, |
| 1285 | .count = desc.count, | 1295 | .count = desc.count, |
| 1296 | .is_integer = desc.is_integer, | ||
| 1286 | }); | 1297 | }); |
| 1287 | if (profile.supported_spirv >= 0x00010400) { | 1298 | if (profile.supported_spirv >= 0x00010400) { |
| 1288 | interfaces.push_back(id); | 1299 | interfaces.push_back(id); |
| @@ -1327,7 +1338,8 @@ void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_inde | |||
| 1327 | if (desc.count != 1) { | 1338 | if (desc.count != 1) { |
| 1328 | throw NotImplementedException("Array of images"); | 1339 | throw NotImplementedException("Array of images"); |
| 1329 | } | 1340 | } |
| 1330 | const Id image_type{ImageType(*this, desc)}; | 1341 | const Id sampled_type{desc.is_integer ? U32[1] : F32[1]}; |
| 1342 | const Id image_type{ImageType(*this, desc, sampled_type)}; | ||
| 1331 | const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)}; | 1343 | const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)}; |
| 1332 | const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)}; | 1344 | const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)}; |
| 1333 | Decorate(id, spv::Decoration::Binding, binding); | 1345 | Decorate(id, spv::Decoration::Binding, binding); |
| @@ -1337,6 +1349,7 @@ void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_inde | |||
| 1337 | .id = id, | 1349 | .id = id, |
| 1338 | .image_type = image_type, | 1350 | .image_type = image_type, |
| 1339 | .count = desc.count, | 1351 | .count = desc.count, |
| 1352 | .is_integer = desc.is_integer, | ||
| 1340 | }); | 1353 | }); |
| 1341 | if (profile.supported_spirv >= 0x00010400) { | 1354 | if (profile.supported_spirv >= 0x00010400) { |
| 1342 | interfaces.push_back(id); | 1355 | interfaces.push_back(id); |
| @@ -1528,8 +1541,16 @@ void EmitContext::DefineOutputs(const IR::Program& program) { | |||
| 1528 | if (stage == Stage::Fragment) { | 1541 | if (stage == Stage::Fragment) { |
| 1529 | throw NotImplementedException("Storing ClipDistance in fragment stage"); | 1542 | throw NotImplementedException("Storing ClipDistance in fragment stage"); |
| 1530 | } | 1543 | } |
| 1531 | const Id type{TypeArray(F32[1], Const(8U))}; | 1544 | if (profile.max_user_clip_distances > 0) { |
| 1532 | clip_distances = DefineOutput(*this, type, invocations, spv::BuiltIn::ClipDistance); | 1545 | const u32 used{std::min(profile.max_user_clip_distances, 8u)}; |
| 1546 | const std::array<Id, 8> zero{f32_zero_value, f32_zero_value, f32_zero_value, | ||
| 1547 | f32_zero_value, f32_zero_value, f32_zero_value, | ||
| 1548 | f32_zero_value, f32_zero_value}; | ||
| 1549 | const Id type{TypeArray(F32[1], Const(used))}; | ||
| 1550 | const Id initializer{ConstantComposite(type, std::span(zero).subspan(0, used))}; | ||
| 1551 | clip_distances = | ||
| 1552 | DefineOutput(*this, type, invocations, spv::BuiltIn::ClipDistance, initializer); | ||
| 1553 | } | ||
| 1533 | } | 1554 | } |
| 1534 | if (info.stores[IR::Attribute::Layer] && | 1555 | if (info.stores[IR::Attribute::Layer] && |
| 1535 | (profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) { | 1556 | (profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) { |
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h index 1aa79863d..56019ad89 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h | |||
| @@ -47,12 +47,14 @@ struct ImageBufferDefinition { | |||
| 47 | Id id; | 47 | Id id; |
| 48 | Id image_type; | 48 | Id image_type; |
| 49 | u32 count; | 49 | u32 count; |
| 50 | bool is_integer; | ||
| 50 | }; | 51 | }; |
| 51 | 52 | ||
| 52 | struct ImageDefinition { | 53 | struct ImageDefinition { |
| 53 | Id id; | 54 | Id id; |
| 54 | Id image_type; | 55 | Id image_type; |
| 55 | u32 count; | 56 | u32 count; |
| 57 | bool is_integer; | ||
| 56 | }; | 58 | }; |
| 57 | 59 | ||
| 58 | struct UniformDefinitions { | 60 | struct UniformDefinitions { |