Try export vulkan images with single memory objects

This commit is contained in:
mittorn 2024-10-02 23:01:11 +03:00
parent b82f5a9b9a
commit b1e6c99841
2 changed files with 52 additions and 18 deletions

View file

@ -1726,7 +1726,7 @@ encoder_encode(struct vaapi_recorder *r, VASurfaceID input)
VABufferID pic_buf =
encoder_update_pic_parameters(r, output_buf);
if (buffers[count - 1] == VA_INVALID_ID)
if (count && buffers[count - 1] == VA_INVALID_ID)
goto bail;
if (r->frame_count == 0)
count += encoder_prepare_headers(r, buffers + count);
@ -2047,6 +2047,7 @@ vaapi_recorder_create3(int drm_fd, int width, int height, const char *filename,
VAStatus status;
int major, minor;
int flags;
VADRMPRIMESurfaceDescriptor drmSurface = {0};
r = (vaapi_recorder*)calloc(sizeof *r,1);
if (r == NULL)
@ -2082,19 +2083,32 @@ vaapi_recorder_create3(int drm_fd, int width, int height, const char *filename,
goto err_vpp;
}
//create_surface_from_fd(r, dmabuf_fd, dmabuf_stride, &gInputRGBA);
VASurfaceAttrib va_attribs[2];
VASurfaceAttrib va_attribs[4];
//VASurfaceAttribExternalBuffers va_attrib_extbuf;
VADRMPRIMESurfaceDescriptor drmSurface;
/*
*
* {fourcc = 842094158, width = 1920, height = 1080, num_objects = 1, objects = {{fd = 15, size = 3133440, drm_format_modifier = 72057594037927938}, {fd = 0, size = 0,
drm_format_modifier = 0}, {fd = 0, size = 0, drm_format_modifier = 0}, {fd = 0, size = 0, drm_format_modifier = 0}}, num_layers = 2, layers = {{drm_format = 538982482,
num_planes = 1, object_index = {0, 0, 0, 0}, offset = {0, 0, 0, 0}, pitch = {1920, 0, 0, 0}}, {drm_format = 943215175, num_planes = 1, object_index = {0, 0, 0, 0}, offset = {
2088960, 0, 0, 0}, pitch = {1920, 0, 0, 0}}, {drm_format = 0, num_planes = 0, object_index = {0, 0, 0, 0}, offset = {0, 0, 0, 0}, pitch = {0, 0, 0, 0}}, {drm_format = 0,
num_planes = 0, object_index = {0, 0, 0, 0}, offset = {0, 0, 0, 0}, pitch = {0, 0, 0, 0}}}}
* {fourcc = 842094158, width = 1920, height = 1080, num_objects = 1, objects = {{fd = 13, size = 3133440, drm_format_modifier = 72057594037927938}, {fd = 0, size = 0,
drm_format_modifier = 0}, {fd = 0, size = 0, drm_format_modifier = 0}, {fd = 0, size = 0, drm_format_modifier = 0}}, num_layers = 2, layers = {{drm_format = 538982482,
num_planes = 1, object_index = {0, 0, 0, 0}, offset = {0, 0, 0, 0}, pitch = {1920, 0, 0, 0}}, {drm_format = 943215175, num_planes = 1, object_index = {0, 0, 0, 0}, offset = {
2088960, 0, 0, 0}, pitch = {1920, 0, 0, 0}}, {drm_format = 0, num_planes = 0, object_index = {0, 0, 0, 0}, offset = {0, 0, 0, 0}, pitch = {0, 0, 0, 0}}, {drm_format = 0,
num_planes = 0, object_index = {0, 0, 0, 0}, offset = {0, 0, 0, 0}, pitch = {0, 0, 0, 0}}}}
*/
drmSurface.fourcc = VA_FOURCC_NV12;
drmSurface.width = width;
drmSurface.height = height;
drmSurface.num_objects = 2;
drmSurface.num_objects = 1;
drmSurface.objects[0].fd = dmabuf_fd;
drmSurface.objects[0].drm_format_modifier = 0;//0x20000002096bb03;
drmSurface.objects[0].size = 1920 * height;
drmSurface.objects[1].fd = dmabuf_fd_uv;
drmSurface.objects[1].drm_format_modifier = 0;//0x20000002096bb03;
drmSurface.objects[1].size = 1920 * height/2;
drmSurface.objects[0].drm_format_modifier = 72057594037927938;//0x20000002096bb03;
drmSurface.objects[0].size = 3133440;
//drmSurface.objects[1].fd = dmabuf_fd_uv;
//drmSurface.objects[1].drm_format_modifier = 0;//0x20000002096bb03;
//drmSurface.objects[1].size = 1920 * height/2;
drmSurface.num_layers = 2;
drmSurface.layers[0].drm_format = DRM_FORMAT_R8;
drmSurface.layers[0].num_planes = 1;
@ -2103,8 +2117,8 @@ vaapi_recorder_create3(int drm_fd, int width, int height, const char *filename,
drmSurface.layers[0].pitch[0] = 1920;
drmSurface.layers[1].drm_format = DRM_FORMAT_GR88;
drmSurface.layers[1].num_planes = 1;
drmSurface.layers[1].object_index[0] = 1;
drmSurface.layers[1].offset[0] = 0;
drmSurface.layers[1].object_index[0] = 0;
drmSurface.layers[1].offset[0] = 2088960;
drmSurface.layers[1].pitch[0] = 1920;
/*unsigned long buffer_fd[2] = {dmabuf_fd, dmabuf_fd_uv};
@ -2130,6 +2144,14 @@ vaapi_recorder_create3(int drm_fd, int width, int height, const char *filename,
va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
va_attribs[1].value.type = VAGenericValueTypePointer;
va_attribs[1].value.value.p = &drmSurface;
va_attribs[2].type = VASurfaceAttribUsageHint;
va_attribs[2].flags = VA_SURFACE_ATTRIB_SETTABLE;
va_attribs[2].value.type = VAGenericValueTypeInteger;
va_attribs[2].value.value.i = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
va_attribs[3].type = VASurfaceAttribPixelFormat;
va_attribs[3].flags = VA_SURFACE_ATTRIB_SETTABLE;
va_attribs[3].value.type = VAGenericValueTypeInteger;
va_attribs[3].value.value.i = VA_FOURCC_NV12;
status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_YUV420,
r->width, r->height, &r->vpp.output, 1,
@ -2165,6 +2187,7 @@ vaapi_recorder_create4(int drm_fd, int width, int height, const char *filename,
int major, minor;
int flags;
VASurfaceAttribExternalBuffers va_attrib_extbuf = {0};
VADRMPRIMESurfaceDescriptor drmSurface = {0};
r = (vaapi_recorder*)calloc(sizeof *r,1);
if (r == NULL)
@ -2202,7 +2225,7 @@ vaapi_recorder_create4(int drm_fd, int width, int height, const char *filename,
//create_surface_from_fd(r, dmabuf_fd, dmabuf_stride, &gInputRGBA);
VASurfaceAttrib va_attribs[4];
VADRMPRIMESurfaceDescriptor drmSurface;
//unsigned long buffer_fd[2] = {dmabuf_fd, dmabuf_fd_uv};

View file

@ -787,7 +787,7 @@ public:
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, NULL, &image)); // create image.
imageCreateInfo.format = VK_FORMAT_R8G8_UNORM;
imageCreateInfo.extent = { WIDTH/2, HEIGHT/2, 1 };
layout.offset = 2088960;
layout.offset = 0;//2088960;
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, NULL, &image1)); // create image.
@ -837,7 +837,7 @@ public:
// Now associate that allocated memory with the buffer. With that, the buffer is backed by actual memory.
VK_CHECK_RESULT(vkBindImageMemory(device, image, imageMemory, 0));
VK_CHECK_RESULT(vkBindImageMemory(device, image1, imageMemory, 0));
VK_CHECK_RESULT(vkBindImageMemory(device, image1, imageMemory, 2088960));
VkImageViewCreateInfo view = {VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
view.image = image;
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
@ -938,7 +938,7 @@ public:
VkImageCreateInfo imageCreateInfo = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = format;
imageCreateInfo.extent = { width, height, 1 };
imageCreateInfo.extent = { (unsigned int)width, (unsigned int)height, 1 };
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
@ -977,10 +977,15 @@ public:
printf("mod %llx %d %d\n", modifiers[i].drmFormatModifier, modifiers[i].drmFormatModifierPlaneCount, (int)modifiers[i].drmFormatModifierTilingFeatures);
}
extInfo.pNext = &modifierList;
modifiers2[0] = 72057594037927938;
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, NULL, &image)); // create image.
imageCreateInfo.format = VK_FORMAT_R8G8_UNORM;
imageCreateInfo.extent = { WIDTH/2, HEIGHT/2, 1 };
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, NULL, &image1)); // create image.
/*
But the buffer doesn't allocate memory for itself, so we must do that manually.
*/
@ -1002,8 +1007,9 @@ public:
Now use obtained memory requirements info to allocate the memory for the buffer.
*/
VkMemoryAllocateInfo allocateInfo = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
allocateInfo.allocationSize = memoryRequirements.size; // specify required memory.
allocateInfo.pNext = &dedicatedAllocInfo;
allocateInfo.allocationSize = 3133440;//memoryRequirements.size; // specify required memory.
// dedicated info unneeded?
allocateInfo.pNext = &exportInfo;//&dedicatedAllocInfo;
/*
There are several types of memory that can be allocated, and we must choose a memory type that:
@ -1021,12 +1027,17 @@ public:
// Now associate that allocated memory with the buffer. With that, the buffer is backed by actual memory.
VK_CHECK_RESULT(vkBindImageMemory(device, image, imageMemory, 0));
VK_CHECK_RESULT(vkBindImageMemory(device, image1, imageMemory, 2088960));
VkImageViewCreateInfo view = {VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
view.image = image;
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
view.format = format;
view.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &imageView));
view.format = VK_FORMAT_R8G8_UNORM;
view.image = image1;
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &imageView1));
VkMemoryGetFdInfoKHR getFdInfo = { VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR};
getFdInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
getFdInfo.memory = imageMemory;
@ -1054,7 +1065,7 @@ public:
in the compute shader.
*/
VkDescriptorSetLayoutBinding descriptorSetLayoutBinding[2] = {};
VkDescriptorSetLayoutBinding descriptorSetLayoutBinding[3] = {};
descriptorSetLayoutBinding[0].binding = 0; // binding = 0
descriptorSetLayoutBinding[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
descriptorSetLayoutBinding[0].descriptorCount = 1;