diff options
author | quou <quou@disroot.org> | 2024-12-27 11:34:30 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-12-27 11:34:30 +1100 |
commit | 5f9100b935a27bbb6d5ac8ab2050b19ba2894087 (patch) | |
tree | 8f5527be4668dcf6541d2f6ad0cc7666b15d5b11 | |
parent | edc84213fadc9bbd30030a57600550bde562b24e (diff) |
expose the create texture function
-rw-r--r-- | video.cpp | 155 | ||||
-rw-r--r-- | video.hpp | 7 |
2 files changed, 85 insertions, 77 deletions
@@ -749,11 +749,6 @@ struct Texture_Loader : public Asset_Loader { void unload(Asset* a) override; Buffer_Id upload(void* buf, size_t size); - Texture_Id create_tex( - Texture_Format fmt, - int w, - int h - ); }; struct Terminator { @@ -2872,6 +2867,80 @@ Buffer& Device::get_buffer(Buffer_Id id) { return dev->buffers[id]; } +Texture_Id Device::create_texture( + Texture_Format fmt, + int w, + int h, + Buffer_Id init +) { + VkImageCreateInfo ii{}; + VkResult r; + Device_Vk* dev = (Device_Vk*)this; + Texture_Id id = dev->alloc_texture(); + Texture_Vk& tex = *(Texture_Vk*)&dev->get_texture(id); + VkMemoryRequirements req; + tex.state = Resource_State::undefined; + ii.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + ii.imageType = VK_IMAGE_TYPE_2D; + ii.extent.width = w; + ii.extent.height = h; + ii.extent.depth = 1; + ii.mipLevels = 1; + ii.arrayLayers = 1; + ii.format = get_vk_format(fmt); + ii.tiling = VK_IMAGE_TILING_OPTIMAL; + ii.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + ii.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; + ii.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + ii.samples = VK_SAMPLE_COUNT_1_BIT; + r = vkCreateImage(dev->dev, &ii, &dev->ac, &tex.image); + if (r != VK_SUCCESS) { + print_err("Failed to create an image.\n"); + } + vkGetImageMemoryRequirements(dev->dev, tex.image, &req); + { + VkMemoryPropertyFlags props = + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + int mt = dev->find_memory_type(req.memoryTypeBits, props); + VkMemoryAllocateInfo ai{}; + if (mt < 0) { + print("Failed to find a satisfying memory type index.\n"); + pbreak(mt); + } + ai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + ai.allocationSize = req.size; + ai.memoryTypeIndex = mt; + r = vkAllocateMemory(dev->dev, &ai, &dev->ac, &tex.memory); + if (r == VK_ERROR_OUT_OF_DEVICE_MEMORY) { + print_err("Out of video memory.\n"); + pbreak(r); + } + if (r != VK_SUCCESS) { + print_err("VRAM allocation failed.\n"); + pbreak(r); + } + } + vkBindImageMemory(dev->dev, tex.image, tex.memory, 0); + tex.w = w; + tex.h = h; + tex.alias = false; + tex.view = make_view( + dev, + tex.image, + ii.format, + VK_IMAGE_ASPECT_COLOR_BIT + ); + if (init) { + Context& ctx = dev->acquire(); + ctx.transition(id, Resource_State::copy_dst); + ctx.copy(id, init); + ctx.transition(id, Resource_State::shader_read); + dev->submit(ctx); + } + return id; +} + + Shader& Device::get_shader(Shader_Id id) { Device_Vk* dev = (Device_Vk*)this; assert(id.index); @@ -3029,6 +3098,8 @@ size_t Texture_Loader::calc_size( switch (fmt) { case texture_format_bc1: return (w / 4) * (h / 4) * 8; + case texture_format_r8i: + return w * h; default: print_err("Can't load this texture format.\n"); pbreak(45498); @@ -3049,69 +3120,6 @@ Buffer_Id Texture_Loader::upload(void* buf, size_t size) { return id; } -Texture_Id Texture_Loader::create_tex( - Texture_Format fmt, - int w, - int h -) { - VkImageCreateInfo ii{}; - VkResult r; - Texture_Id id = dev->alloc_texture(); - Texture_Vk& tex = *(Texture_Vk*)&dev->get_texture(id); - VkMemoryRequirements req; - ii.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - ii.imageType = VK_IMAGE_TYPE_2D; - ii.extent.width = w; - ii.extent.height = h; - ii.extent.depth = 1; - ii.mipLevels = 1; - ii.arrayLayers = 1; - ii.format = get_vk_format(fmt); - ii.tiling = VK_IMAGE_TILING_OPTIMAL; - ii.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - ii.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - ii.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - ii.samples = VK_SAMPLE_COUNT_1_BIT; - r = vkCreateImage(dev->dev, &ii, &dev->ac, &tex.image); - if (r != VK_SUCCESS) { - print_err("Failed to create an image.\n"); - } - vkGetImageMemoryRequirements(dev->dev, tex.image, &req); - { - VkMemoryPropertyFlags props = - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; - int mt = dev->find_memory_type(req.memoryTypeBits, props); - VkMemoryAllocateInfo ai{}; - if (mt < 0) { - print("Failed to find a satisfying memory type index.\n"); - pbreak(mt); - } - ai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - ai.allocationSize = req.size; - ai.memoryTypeIndex = mt; - r = vkAllocateMemory(dev->dev, &ai, &dev->ac, &tex.memory); - if (r == VK_ERROR_OUT_OF_DEVICE_MEMORY) { - print_err("Out of video memory.\n"); - pbreak(r); - } - if (r != VK_SUCCESS) { - print_err("VRAM allocation failed.\n"); - pbreak(r); - } - } - vkBindImageMemory(dev->dev, tex.image, tex.memory, 0); - tex.w = w; - tex.h = h; - tex.alias = false; - tex.view = make_view( - dev, - tex.image, - ii.format, - VK_IMAGE_ASPECT_COLOR_BIT - ); - return id; -} - Asset* Texture_Loader::load(Arena* a, Arena* s, Pack_File* f) { char magic[4]; void* data; @@ -3128,15 +3136,8 @@ Asset* Texture_Loader::load(Arena* a, Arena* s, Pack_File* f) { pack_read(f, data, size); { Buffer_Id buf = upload(data, size); - Texture_Id tex = create_tex(fmt, w, h); - { - Context& ctx = dev->acquire(); - ctx.transition(tex, Resource_State::copy_dst); - ctx.copy(tex, buf); - ctx.transition(tex, Resource_State::shader_read); - dev->submit(ctx); - dev->destroy_buffer(buf); - } + Texture_Id tex = dev->create_texture(fmt, w, h, buf); + dev->destroy_buffer(buf); return &dev->get_texture(tex); } } @@ -295,6 +295,13 @@ struct Device { Context& acquire(); Context& get_ctx(); + + Texture_Id create_texture( + Texture_Format fmt, + int w, + int h, + Buffer_Id init + ); Texture_Id get_backbuffer(); Texture& get_texture(Texture_Id id); void destroy_texture(Texture_Id id); |