Files
panvk-bifrost/mesa-panvk-bifrost/iter3/probe_triangle.c
T
marfrit a4e7d8ab90 initial seed: retrofit campaign lineage from local working trees
panvk-bifrost campaigns (r1..r4 Vulkan compositor + r5.video1 Vulkan
video decode) shipped before this repo existed; the deliverable
patches live in marfrit-packages, but the reasoning chain, phase docs,
and source-state evidence lived only in local working trees on the
development host.

This retrofit imports:
- mesa-panvk-bifrost/   — r1..r4 era phase docs (iter1..iter18)
                          (libmali stub blobs at iter18/blob/ excluded
                          — 109MB of RE artifacts replaced with a README
                          pointer)
- mesa-panvk-bifrost-video/ — sibling campaign phase docs + probe
- evidence/             — frozen .tgz source snapshots at each milestone
                          (basis for the 0005 patch diff generation)

Future iterations should branch off here from day one, so each iter is
a commit rather than a snapshot. See [[feedback-session-local-process-pins]]
for the process drift this retrofit closes.

Total: 1.9 MB across 124 files.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 05:25:37 +02:00

596 lines
23 KiB
C

/*
* iter3 fullscreen triangle probe for panvk-bifrost campaign.
*
* Tests the graphics pipeline path on PanVk-Bifrost (PineTab2 / Mali-G52 r1 MC1):
* vertex + fragment shaders, rasterizer, dynamic rendering, tile binning.
*
* Pipeline:
* 1. Vulkan 1.0 instance + VK_KHR_get_physical_device_properties2 extension.
* 2. Device with VK_KHR_dynamic_rendering + dependency chain
* (multiview, maintenance2, create_renderpass2, depth_stencil_resolve),
* dynamicRendering feature enabled.
* 3. Create 64x64 R8G8B8A8_UNORM image (COLOR_ATTACHMENT | TRANSFER_SRC),
* device-local memory, image view.
* 4. Create staging buffer (16 KiB, TRANSFER_DST, host-visible),
* pre-fill 0xDEADBEEF sentinel.
* 5. Build graphics pipeline:
* - vertex shader (probe_triangle.vert.spv): fullscreen triangle from
* gl_VertexIndex
* - fragment shader (probe_triangle.frag.spv): gl_FragCoord-encoded output
* - no vertex input bindings
* - viewport + scissor = 64x64 (static)
* - no blend, no depth, cull NONE
* - color attachment format chained via VkPipelineRenderingCreateInfoKHR
* 6. Cmd buffer:
* a. ImageBarrier UNDEFINED -> COLOR_ATTACHMENT_OPTIMAL
* b. vkCmdBeginRenderingKHR(loadOp=CLEAR black, storeOp=STORE)
* c. bind pipeline, vkCmdDraw(3, 1, 0, 0)
* d. vkCmdEndRenderingKHR
* e. ImageBarrier COLOR_ATTACHMENT_OPTIMAL -> TRANSFER_SRC_OPTIMAL
* f. vkCmdCopyImageToBuffer
* g. BufferBarrier TRANSFER_WRITE -> HOST_READ
* 7. Submit + fence-wait.
* 8. Verify pixel[row,col] == 0xff80(row)(col) for all 64x64 pixels.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <vulkan/vulkan.h>
#define IMG_W 64
#define IMG_H 64
#define PIXELS (IMG_W * IMG_H)
#define BYTES_PER_PIXEL 4
#define BUFFER_BYTES (PIXELS * BYTES_PER_PIXEL) /* 16384 */
#define VSPV_PATH "probe_triangle.vert.spv"
#define FSPV_PATH "probe_triangle.frag.spv"
/* Pixel encoding from the fragment shader:
* For pixel at (col, row): R=col, G=row, B=0x80, A=0xff
* RGBA8 LE uint32 = (A << 24) | (B << 16) | (G << 8) | R
* = 0xff80(row)(col)
*/
#define EXPECTED_PIXEL(col, row) (0xff800000u | ((uint32_t)(row) << 8) | (uint32_t)(col))
#define STEP(name) do { fprintf(stderr, "[step] " name "\n"); fflush(stderr); } while (0)
#define VK_CHECK(call) do { \
VkResult _r = (call); \
if (_r != VK_SUCCESS) { \
fprintf(stderr, "[fail] " #call " => %d at %s:%d\n", \
(int)_r, __FILE__, __LINE__); \
exit(2); \
} \
} while (0)
static uint32_t *read_spv(const char *path, size_t *out_bytes)
{
FILE *f = fopen(path, "rb");
if (!f) { fprintf(stderr, "[fail] open %s: %s\n", path, strerror(errno)); exit(3); }
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n <= 0 || (n & 3)) { fprintf(stderr, "[fail] bad SPV size %ld\n", n); exit(3); }
uint32_t *buf = malloc((size_t)n);
if (fread(buf, 1, (size_t)n, f) != (size_t)n) { fprintf(stderr, "[fail] short read\n"); exit(3); }
fclose(f);
*out_bytes = (size_t)n;
return buf;
}
static uint32_t pick_memtype(const VkPhysicalDeviceMemoryProperties *mp,
uint32_t type_bits, VkMemoryPropertyFlags want)
{
for (uint32_t i = 0; i < mp->memoryTypeCount; i++) {
if ((type_bits & (1u << i)) &&
(mp->memoryTypes[i].propertyFlags & want) == want)
return i;
}
fprintf(stderr, "[fail] no memory type matches type_bits=0x%x want=0x%x\n",
type_bits, want);
exit(4);
}
static uint32_t pick_host_visible(const VkPhysicalDeviceMemoryProperties *mp,
uint32_t type_bits)
{
VkMemoryPropertyFlags pref =
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
for (uint32_t i = 0; i < mp->memoryTypeCount; i++) {
if ((type_bits & (1u << i)) &&
(mp->memoryTypes[i].propertyFlags & pref) == pref)
return i;
}
for (uint32_t i = 0; i < mp->memoryTypeCount; i++) {
if ((type_bits & (1u << i)) &&
(mp->memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
return i;
}
fprintf(stderr, "[fail] no HOST_VISIBLE memory type\n");
exit(4);
}
static void image_barrier(VkCommandBuffer cb, VkImage img,
VkImageLayout old_layout, VkImageLayout new_layout,
VkAccessFlags src_access, VkAccessFlags dst_access,
VkPipelineStageFlags src_stage, VkPipelineStageFlags dst_stage)
{
VkImageMemoryBarrier ib = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = src_access,
.dstAccessMask = dst_access,
.oldLayout = old_layout, .newLayout = new_layout,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = img,
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0, .levelCount = 1,
.baseArrayLayer = 0, .layerCount = 1,
},
};
vkCmdPipelineBarrier(cb, src_stage, dst_stage, 0, 0, NULL, 0, NULL, 1, &ib);
}
static VkShaderModule make_shader(VkDevice dev, const char *spv_path)
{
size_t bytes = 0;
uint32_t *code = read_spv(spv_path, &bytes);
VkShaderModuleCreateInfo smci = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = bytes,
.pCode = code,
};
VkShaderModule sm;
VK_CHECK(vkCreateShaderModule(dev, &smci, NULL, &sm));
free(code);
return sm;
}
int main(void)
{
/* ---- instance --------------------------------------------------------- */
STEP("vkCreateInstance (+VK_KHR_get_physical_device_properties2)");
const char *inst_exts[] = { "VK_KHR_get_physical_device_properties2" };
VkApplicationInfo app = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "panvk-bifrost iter3 triangle probe",
.apiVersion = VK_API_VERSION_1_0,
};
VkInstanceCreateInfo ici = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app,
.enabledExtensionCount = 1,
.ppEnabledExtensionNames = inst_exts,
};
VkInstance inst;
VK_CHECK(vkCreateInstance(&ici, NULL, &inst));
/* ---- physical device -------------------------------------------------- */
STEP("vkEnumeratePhysicalDevices");
uint32_t n_phys = 0;
VK_CHECK(vkEnumeratePhysicalDevices(inst, &n_phys, NULL));
if (n_phys == 0) { fprintf(stderr, "[fail] no physical devices\n"); return 5; }
VkPhysicalDevice *phys = calloc(n_phys, sizeof(*phys));
VK_CHECK(vkEnumeratePhysicalDevices(inst, &n_phys, phys));
VkPhysicalDevice gpu = phys[0];
VkPhysicalDeviceProperties pp;
vkGetPhysicalDeviceProperties(gpu, &pp);
fprintf(stderr, "[info] gpu='%s' apiVersion=%u.%u.%u\n",
pp.deviceName,
VK_VERSION_MAJOR(pp.apiVersion),
VK_VERSION_MINOR(pp.apiVersion),
VK_VERSION_PATCH(pp.apiVersion));
VkPhysicalDeviceMemoryProperties mp;
vkGetPhysicalDeviceMemoryProperties(gpu, &mp);
/* ---- queue family (graphics) ----------------------------------------- */
uint32_t n_qf = 0;
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &n_qf, NULL);
VkQueueFamilyProperties *qfp = calloc(n_qf, sizeof(*qfp));
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &n_qf, qfp);
uint32_t qfam = UINT32_MAX;
for (uint32_t i = 0; i < n_qf; i++) {
if (qfp[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { qfam = i; break; }
}
if (qfam == UINT32_MAX) { fprintf(stderr, "[fail] no graphics queue\n"); return 6; }
/* ---- device + dynamic_rendering chain -------------------------------- */
STEP("vkCreateDevice (+VK_KHR_dynamic_rendering chain)");
const char *dev_exts[] = {
"VK_KHR_multiview",
"VK_KHR_maintenance2",
"VK_KHR_create_renderpass2",
"VK_KHR_depth_stencil_resolve",
"VK_KHR_dynamic_rendering",
};
VkPhysicalDeviceDynamicRenderingFeaturesKHR dyn_feat = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES_KHR,
.dynamicRendering = VK_TRUE,
};
float qprio = 1.0f;
VkDeviceQueueCreateInfo qci = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = qfam,
.queueCount = 1,
.pQueuePriorities = &qprio,
};
VkDeviceCreateInfo dci = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = &dyn_feat,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &qci,
.enabledExtensionCount = sizeof(dev_exts) / sizeof(dev_exts[0]),
.ppEnabledExtensionNames = dev_exts,
};
VkDevice dev;
VK_CHECK(vkCreateDevice(gpu, &dci, NULL, &dev));
VkQueue queue;
vkGetDeviceQueue(dev, qfam, 0, &queue);
/* Fetch the KHR-suffixed dynamic-rendering cmd functions. */
PFN_vkCmdBeginRenderingKHR pCmdBeginRendering =
(PFN_vkCmdBeginRenderingKHR)vkGetDeviceProcAddr(dev, "vkCmdBeginRenderingKHR");
PFN_vkCmdEndRenderingKHR pCmdEndRendering =
(PFN_vkCmdEndRenderingKHR)vkGetDeviceProcAddr(dev, "vkCmdEndRenderingKHR");
if (!pCmdBeginRendering || !pCmdEndRendering) {
fprintf(stderr, "[fail] could not load vkCmdBeginRenderingKHR / EndRenderingKHR\n");
return 10;
}
/* ---- color attachment image ------------------------------------------ */
STEP("vkCreateImage (64x64 R8G8B8A8_UNORM, COLOR_ATTACHMENT|TRANSFER_SRC)");
VkImageCreateInfo iciImg = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.imageType = VK_IMAGE_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.extent = { IMG_W, IMG_H, 1 },
.mipLevels = 1, .arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
VkImage img;
VK_CHECK(vkCreateImage(dev, &iciImg, NULL, &img));
VkMemoryRequirements imr;
vkGetImageMemoryRequirements(dev, img, &imr);
fprintf(stderr, "[info] image memReq size=%llu alignment=%llu typeBits=0x%x\n",
(unsigned long long)imr.size,
(unsigned long long)imr.alignment,
imr.memoryTypeBits);
VkMemoryAllocateInfo imai = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = imr.size,
.memoryTypeIndex = pick_memtype(&mp, imr.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
};
VkDeviceMemory img_mem;
VK_CHECK(vkAllocateMemory(dev, &imai, NULL, &img_mem));
VK_CHECK(vkBindImageMemory(dev, img, img_mem, 0));
/* ---- image view ------------------------------------------------------ */
STEP("vkCreateImageView");
VkImageViewCreateInfo ivci = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = img,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.components = {
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
},
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0, .levelCount = 1,
.baseArrayLayer = 0, .layerCount = 1,
},
};
VkImageView iv;
VK_CHECK(vkCreateImageView(dev, &ivci, NULL, &iv));
/* ---- staging buffer -------------------------------------------------- */
VkBufferCreateInfo bci = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = BUFFER_BYTES,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VkBuffer buf;
VK_CHECK(vkCreateBuffer(dev, &bci, NULL, &buf));
VkMemoryRequirements bmr;
vkGetBufferMemoryRequirements(dev, buf, &bmr);
VkMemoryAllocateInfo bmai = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = bmr.size,
.memoryTypeIndex = pick_host_visible(&mp, bmr.memoryTypeBits),
};
VkDeviceMemory buf_mem;
VK_CHECK(vkAllocateMemory(dev, &bmai, NULL, &buf_mem));
VK_CHECK(vkBindBufferMemory(dev, buf, buf_mem, 0));
void *mapped = NULL;
VK_CHECK(vkMapMemory(dev, buf_mem, 0, VK_WHOLE_SIZE, 0, &mapped));
uint32_t *u32 = (uint32_t *)mapped;
for (uint32_t i = 0; i < PIXELS; i++) u32[i] = 0xDEADBEEFu;
/* ---- graphics pipeline ----------------------------------------------- */
STEP("vkCreatePipelineLayout (empty)");
VkPipelineLayoutCreateInfo plci = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
};
VkPipelineLayout pl;
VK_CHECK(vkCreatePipelineLayout(dev, &plci, NULL, &pl));
STEP("vkCreateShaderModule vert + frag");
VkShaderModule vsm = make_shader(dev, VSPV_PATH);
VkShaderModule fsm = make_shader(dev, FSPV_PATH);
VkPipelineShaderStageCreateInfo stages[2] = {
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = vsm,
.pName = "main",
},
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = fsm,
.pName = "main",
},
};
VkPipelineVertexInputStateCreateInfo vi = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
};
VkPipelineInputAssemblyStateCreateInfo ia = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
};
VkViewport viewport = { 0, 0, IMG_W, IMG_H, 0.0f, 1.0f };
VkRect2D scissor = {{ 0, 0 }, { IMG_W, IMG_H }};
VkPipelineViewportStateCreateInfo vp = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.viewportCount = 1, .pViewports = &viewport,
.scissorCount = 1, .pScissors = &scissor,
};
VkPipelineRasterizationStateCreateInfo rs = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.polygonMode = VK_POLYGON_MODE_FILL,
.cullMode = VK_CULL_MODE_NONE,
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
.lineWidth = 1.0f,
};
VkPipelineMultisampleStateCreateInfo ms = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
};
VkPipelineColorBlendAttachmentState cba = {
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
};
VkPipelineColorBlendStateCreateInfo cb = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &cba,
};
VkFormat color_fmt = VK_FORMAT_R8G8B8A8_UNORM;
VkPipelineRenderingCreateInfoKHR pri = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &color_fmt,
};
VkGraphicsPipelineCreateInfo gpci = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.pNext = &pri,
.stageCount = 2, .pStages = stages,
.pVertexInputState = &vi,
.pInputAssemblyState = &ia,
.pViewportState = &vp,
.pRasterizationState = &rs,
.pMultisampleState = &ms,
.pColorBlendState = &cb,
.layout = pl,
/* renderPass = VK_NULL_HANDLE for dynamic rendering */
};
STEP("vkCreateGraphicsPipelines");
VkPipeline pipe;
VK_CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gpci, NULL, &pipe));
/* ---- command buffer --------------------------------------------------- */
VkCommandPoolCreateInfo cpoolci = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = qfam,
};
VkCommandPool cpool;
VK_CHECK(vkCreateCommandPool(dev, &cpoolci, NULL, &cpool));
VkCommandBufferAllocateInfo cbai = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = cpool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1,
};
VkCommandBuffer cb;
VK_CHECK(vkAllocateCommandBuffers(dev, &cbai, &cb));
STEP("record cmd buffer (dynamic rendering + draw + copy)");
VkCommandBufferBeginInfo cbbi = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
VK_CHECK(vkBeginCommandBuffer(cb, &cbbi));
/* UNDEFINED -> COLOR_ATTACHMENT_OPTIMAL */
image_barrier(cb, img,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
0, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
/* Dynamic rendering */
VkClearValue clear_black = {{{0.0f, 0.0f, 0.0f, 0.0f}}};
VkRenderingAttachmentInfoKHR color_attach = {
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,
.imageView = iv,
.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.clearValue = clear_black,
};
VkRenderingInfoKHR ri = {
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
.renderArea = {{ 0, 0 }, { IMG_W, IMG_H }},
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &color_attach,
};
pCmdBeginRendering(cb, &ri);
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe);
vkCmdDraw(cb, 3, 1, 0, 0);
pCmdEndRendering(cb);
/* COLOR_ATTACHMENT_OPTIMAL -> TRANSFER_SRC_OPTIMAL */
image_barrier(cb, img,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT);
/* Image -> staging buffer */
VkBufferImageCopy region = {
.imageSubresource = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.layerCount = 1,
},
.imageExtent = { IMG_W, IMG_H, 1 },
};
vkCmdCopyImageToBuffer(cb, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
buf, 1, &region);
/* Buffer transfer-write -> host-read */
VkBufferMemoryBarrier bb = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_HOST_READ_BIT,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.buffer = buf, .offset = 0, .size = VK_WHOLE_SIZE,
};
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_HOST_BIT,
0, 0, NULL, 1, &bb, 0, NULL);
VK_CHECK(vkEndCommandBuffer(cb));
/* ---- submit + wait --------------------------------------------------- */
VkFenceCreateInfo fci = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
VkFence fence;
VK_CHECK(vkCreateFence(dev, &fci, NULL, &fence));
STEP("vkQueueSubmit + vkWaitForFences (10s timeout)");
VkSubmitInfo si = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.commandBufferCount = 1,
.pCommandBuffers = &cb,
};
VK_CHECK(vkQueueSubmit(queue, 1, &si, fence));
VkResult wr = vkWaitForFences(dev, 1, &fence, VK_TRUE, 10ULL * 1000 * 1000 * 1000);
if (wr == VK_TIMEOUT) { fprintf(stderr, "[fail] fence TIMEOUT (10s)\n"); return 7; }
if (wr != VK_SUCCESS) { fprintf(stderr, "[fail] vkWaitForFences => %d\n", wr); return 8; }
/* ---- verify ---------------------------------------------------------- */
STEP("invalidate + verify");
VkMappedMemoryRange mmr = {
.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
.memory = buf_mem,
.offset = 0, .size = VK_WHOLE_SIZE,
};
vkInvalidateMappedMemoryRanges(dev, 1, &mmr);
uint32_t mismatches = 0;
uint32_t still_sentinel = 0;
uint32_t cleared_black = 0; /* 0xff000000 — clear with frag never running */
uint32_t first_diff_idx = UINT32_MAX;
for (uint32_t row = 0; row < IMG_H; row++) {
for (uint32_t col = 0; col < IMG_W; col++) {
uint32_t idx = row * IMG_W + col;
uint32_t got = u32[idx];
uint32_t want = EXPECTED_PIXEL(col, row);
if (got != want) {
if (first_diff_idx == UINT32_MAX) first_diff_idx = idx;
if (got == 0xDEADBEEFu) still_sentinel++;
else if (got == 0xff000000u || got == 0x00000000u) cleared_black++;
mismatches++;
}
}
}
fprintf(stderr, "[info] mismatches=%u/%u (sentinel=%u cleared_black=%u)\n",
mismatches, PIXELS, still_sentinel, cleared_black);
if (mismatches) {
uint32_t idx = first_diff_idx;
uint32_t row = idx / IMG_W, col = idx % IMG_W;
fprintf(stderr, "[diff] first mismatch at (col=%u, row=%u): got=0x%08x want=0x%08x\n",
col, row, u32[idx], EXPECTED_PIXEL(col, row));
/* Dump 4 corners + center for inspection. */
struct { uint32_t r, c; const char *name; } pts[] = {
{0, 0, "TL"}, {0, IMG_W-1, "TR"},
{IMG_H-1, 0, "BL"}, {IMG_H-1, IMG_W-1, "BR"},
{IMG_H/2, IMG_W/2, "center"},
};
for (size_t i = 0; i < sizeof(pts)/sizeof(pts[0]); i++) {
uint32_t k = pts[i].r * IMG_W + pts[i].c;
fprintf(stderr, "[diff] %s (%u,%u): got=0x%08x want=0x%08x\n",
pts[i].name, pts[i].c, pts[i].r,
u32[k], EXPECTED_PIXEL(pts[i].c, pts[i].r));
}
}
/* ---- teardown -------------------------------------------------------- */
vkUnmapMemory(dev, buf_mem);
vkDestroyFence(dev, fence, NULL);
vkDestroyCommandPool(dev, cpool, NULL);
vkDestroyPipeline(dev, pipe, NULL);
vkDestroyShaderModule(dev, vsm, NULL);
vkDestroyShaderModule(dev, fsm, NULL);
vkDestroyPipelineLayout(dev, pl, NULL);
vkDestroyBuffer(dev, buf, NULL);
vkFreeMemory(dev, buf_mem, NULL);
vkDestroyImageView(dev, iv, NULL);
vkDestroyImage(dev, img, NULL);
vkFreeMemory(dev, img_mem, NULL);
vkDestroyDevice(dev, NULL);
vkDestroyInstance(inst, NULL);
free(phys); free(qfp);
if (mismatches == 0) {
fprintf(stderr, "[PASS] PanVk-Bifrost triangle: all %u pixels match.\n", PIXELS);
return 0;
} else {
fprintf(stderr, "[FAIL] %u / %u pixels mismatched.\n", mismatches, PIXELS);
return 1;
}
}