رفع صورة Vulkan الكلاسيكي هو طقس ثلاثي الخطوات: مخزن staging ← vkCmdCopyBufferToImage ← انتقال تخطيط، كله داخل مخزن أوامر على طابور. VK_EXT_host_image_copy (أساسي في Vulkan 1.4) يضيف بابًا من جانب المضيف: vkCopyMemoryToImageEXT يكتب البكسلات من ذاكرة الـ CPU مباشرةً إلى صورة — بلا staging، بلا مخزن أوامر، بلا إرسال طابور.
المسار الكلاسيكي مقابل مسار المضيف
paths.txt
classic: cpu buf → staging VkBuffer → cmd copy → image (queue submit!)
host: cpu buf ──────────────────► image (immediate)upload.cpp
// 1. Image needs the HOST_TRANSFER flag at creation:
vk::ImageCreateInfo ici{};
ici.format = vk::Format::eR8G8B8A8Unorm;
ici.extent = {w, h, 1};
ici.usage = vk::ImageUsageFlagBits::eSampled
| vk::ImageUsageFlagBits::eHostTransferEXT;
ici.initialLayout = vk::ImageLayout::eUndefined;
// … vkCreateImage, bind memory, then:
// 2. Transition once via host — vkTransitionImageLayoutEXT:
vk::HostImageLayoutTransitionInfoEXT tr{};
tr.image = img;
tr.oldLayout = vk::ImageLayout::eUndefined;
tr.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
tr.subresourceRange = {vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1};
dev.transitionImageLayoutEXT(1, &tr);
// 3. Copy pixels — synchronous, host-visible side:
vk::MemoryToImageCopyEXT cpy{};
cpy.pHostPointer = pixels.data();
cpy.memoryRowLength = w * 4; // bytes per row, 0 = tight
cpy.imageSubresource = {vk::ImageAspectFlagBits::eColor, 0, 0, 1};
cpy.imageExtent = {w, h, 1};
dev.copyMemoryToImageEXT(vk::CopyMemoryToImageInfoEXT{
vk::CopyMemoryToImageFlagBitsEXT::eNoInternalFormatConversion,
img, vk::ImageLayout::eShaderReadOnlyOptimal, 1, &cpy});vkCopyImageToMemoryEXT هو المرآة — اقرأ صورة عودة إلى الـ CPU دون مخزن إعادة قراءة + رقصة fence.
لماذا تكلف
- رفع صغير غير متكرر (خطوط، LUTs، أيقونات): بلا تجمع أوامر، بلا fence، بلا حسابات staging — استدعاء واحد، انتهى.
- كتابات بتخطيط مبلَّط/مثلى: التعريف يخلط (swizzle) داخليًا، أنت فقط تسلّم صفوفًا خطية.
- موارد وقت التهيئة: ارفع قبل وجود أي طابور؛ يبسط تشغيل المحرك بشكل كبير.
أين لا يناسب
- البث لكل إطار — النسخة عمل مضيف شبه متزامن وغالبًا أبطأ من نسخة staging غير متزامنة عبر DMA؛ للبيانات لكل إطار (إطارات فيديو، atlases ديناميكية) يبقى مسار staging الكلاسيكي أو مخزن
HOST_COHERENTdevice-local أسرع. - ليست كل الصيغ/التخطيطات مؤهلة — افحص
vkGetPhysicalDeviceFormatProperties2←VkFormatProperties3::hostImageCopyوvkGetPhysicalDeviceImageFormatProperties2لـhostImageCopySupport.
probe.cpp
vk::FormatProperties3 fp3{};
dev.getPhysicalDeviceFormatProperties2(
vk::PhysicalDeviceFormatInfo2{vk::Format::eR8G8B8A8Unorm})
.get<vk::FormatProperties2, vk::FormatProperties3>();
// fp3.optimalTilingFeatures & eHostImageCopy?
قواعد عملية
- اضبط دائمًا استخدام
eHostTransfer+ أبقِ الصورة في تخطيط متوافق مع نقل المضيف بين الكتابات (eGeneralيعمل في كل مكان). - لا يزيل الحاجة لتتبع التخطيط الصحيح — فقط ينقل الانتقال إلى استدعاء مضيف.
- فكر فيه كـ راحة + تمديدات أقل، لا ترقية أداء.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow