MCPcopy Create free account
hub / github.com/KhronosGroup/Vulkan-Tools / demo_prepare_texture_buffer

Function demo_prepare_texture_buffer

cube/cube.c:1733–1792  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1731}
1732
1733static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1734 int32_t tex_width;
1735 int32_t tex_height;
1736 VkResult U_ASSERT_ONLY err;
1737 bool U_ASSERT_ONLY pass;
1738
1739 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1740 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1741 }
1742
1743 tex_obj->tex_width = tex_width;
1744 tex_obj->tex_height = tex_height;
1745
1746 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1747 .pNext = NULL,
1748 .flags = 0,
1749 .size = tex_width * tex_height * 4,
1750 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1751 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1752 .queueFamilyIndexCount = 0,
1753 .pQueueFamilyIndices = NULL};
1754
1755 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1756 assert(!err);
1757 demo_name_object(demo, VK_OBJECT_TYPE_BUFFER, (uint64_t)tex_obj->buffer, "TexBuffer(%s)", filename);
1758
1759 VkMemoryRequirements mem_reqs;
1760 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1761
1762 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1763 tex_obj->mem_alloc.pNext = NULL;
1764 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1765 tex_obj->mem_alloc.memoryTypeIndex = 0;
1766
1767 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1768 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1769 assert(pass);
1770
1771 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1772 assert(!err);
1773 demo_name_object(demo, VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)tex_obj->mem, "TexBufMemory(%s)", filename);
1774
1775 /* bind memory */
1776 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1777 assert(!err);
1778
1779 VkSubresourceLayout layout;
1780 memset(&layout, 0, sizeof(layout));
1781 layout.rowPitch = tex_width * 4;
1782
1783 void *data;
1784 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1785 assert(!err);
1786
1787 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1788 fprintf(stderr, "Error loading texture: %s\n", filename);
1789 }
1790

Callers 1

demo_prepare_texturesFunction · 0.85

Calls 3

loadTextureFunction · 0.85
demo_name_objectFunction · 0.85

Tested by

no test coverage detected