| 32 | namespace fl { |
| 33 | |
| 34 | device_buffer::device_buffer(const device_queue& cqueue, |
| 35 | const size_t& size_, |
| 36 | std::span<uint8_t> host_data_, |
| 37 | const MEMORY_FLAG flags_, |
| 38 | device_buffer* shared_buffer_) : |
| 39 | device_memory(cqueue, host_data_, flags_), size(align_size(size_)), shared_buffer(shared_buffer_) { |
| 40 | if (size == 0) { |
| 41 | throw std::runtime_error("can't allocate a buffer of size 0!"); |
| 42 | } |
| 43 | |
| 44 | if (host_data.data() != nullptr && host_data.size_bytes() != size) { |
| 45 | throw std::runtime_error("host data size " + std::to_string(host_data.size_bytes()) + " does not match buffer size " + std::to_string(size)); |
| 46 | } |
| 47 | |
| 48 | if (size_ != size) { |
| 49 | log_error("buffer size must always be a multiple of $! - using size of $ instead of $ now", |
| 50 | min_multiple(), size, size_); |
| 51 | } |
| 52 | |
| 53 | if (shared_buffer != nullptr) { |
| 54 | if (!has_flag<MEMORY_FLAG::VULKAN_SHARING>(flags) && !has_flag<MEMORY_FLAG::METAL_SHARING>(flags)) { |
| 55 | log_warn("provided a shared buffer, but no sharing flag is set"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // if there is host data, it must have at least the same size as the buffer |
| 60 | if (host_data.data() != nullptr && host_data.size_bytes() < size) { |
| 61 | throw std::runtime_error("image host data size " + std::to_string(host_data.size_bytes()) + |
| 62 | " is smaller than the expected buffer size " + std::to_string(size)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | std::shared_ptr<device_buffer> device_buffer::clone(const device_queue& cqueue, const bool copy_contents, |
| 67 | const MEMORY_FLAG flags_override) { |