| 27 | using namespace llvm_toolchain; |
| 28 | |
| 29 | optional<vulkan_descriptor_set_layout_t> vulkan_program::build_descriptor_set_layout(const compute_device& dev, |
| 30 | const string& func_name, |
| 31 | const llvm_toolchain::function_info& info, |
| 32 | const VkShaderStageFlagBits stage) { |
| 33 | const auto& vk_dev = (const vulkan_device&)dev; |
| 34 | |
| 35 | // handle implicit args which add to the total #args |
| 36 | const auto is_soft_printf = has_flag<FUNCTION_FLAGS::USES_SOFT_PRINTF>(info.flags); |
| 37 | const uint32_t implicit_arg_count = (is_soft_printf ? 1u : 0u); |
| 38 | const uint32_t explicit_arg_count = uint32_t(info.args.size()); |
| 39 | const uint32_t total_arg_count = explicit_arg_count + implicit_arg_count; |
| 40 | |
| 41 | // create function + device specific descriptor set layout |
| 42 | vulkan_descriptor_set_layout_t layout {}; |
| 43 | layout.bindings.reserve(total_arg_count); |
| 44 | bool valid_desc = true; |
| 45 | for (uint32_t i = 0, binding_idx = 0; i < (uint32_t)total_arg_count; ++i) { |
| 46 | // fully ignore argument buffer args, these are encoded as separate descriptor sets |
| 47 | if (i < explicit_arg_count && info.args[i].special_type == SPECIAL_TYPE::ARGUMENT_BUFFER) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | layout.bindings.push_back({}); |
| 52 | auto& binding = layout.bindings.back(); |
| 53 | |
| 54 | binding.binding = binding_idx; |
| 55 | binding.descriptorCount = 1; |
| 56 | binding.stageFlags = stage; |
| 57 | binding.pImmutableSamplers = nullptr; |
| 58 | |
| 59 | if (i < explicit_arg_count) { |
| 60 | const auto& arg = info.args[i]; |
| 61 | switch (arg.address_space) { |
| 62 | // image |
| 63 | case ARG_ADDRESS_SPACE::IMAGE: { |
| 64 | const bool is_image_array = (arg.special_type == SPECIAL_TYPE::IMAGE_ARRAY); |
| 65 | if (is_image_array) { |
| 66 | binding.descriptorCount = arg.size; |
| 67 | } |
| 68 | switch (arg.image_access) { |
| 69 | case ARG_IMAGE_ACCESS::READ: |
| 70 | binding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
| 71 | if (is_image_array) { |
| 72 | layout.read_image_desc += arg.size; |
| 73 | } else { |
| 74 | ++layout.read_image_desc; |
| 75 | } |
| 76 | break; |
| 77 | case ARG_IMAGE_ACCESS::WRITE: |
| 78 | binding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 79 | binding.descriptorCount *= dev.max_mip_levels; |
| 80 | if (is_image_array) { |
| 81 | layout.write_image_desc += arg.size * dev.max_mip_levels; |
| 82 | } else { |
| 83 | layout.write_image_desc += dev.max_mip_levels; |
| 84 | } |
| 85 | break; |
| 86 | case ARG_IMAGE_ACCESS::READ_WRITE: { |