summaryrefslogtreecommitdiff
path: root/video.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'video.cpp')
-rw-r--r--video.cpp155
1 files changed, 78 insertions, 77 deletions
diff --git a/video.cpp b/video.cpp
index 6dc0ef7..d7678f3 100644
--- a/video.cpp
+++ b/video.cpp
@@ -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);
}
}