summaryrefslogtreecommitdiff
path: root/unused code/rend_vk_memory.c
blob: 4782b4675e0cc11b12e60f5f001cb892316cbb8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once

#include "rend_internal.h"
#include "rend_vk_internal.h"
#include <vulkan/vulkan_core.h>


#if 0
static RendVkMemory
rend_vk_memory_malloc(size_t size, VkDevice logical_device, VkPhysicalDevice physical_device, uint32_t heap_index, VkAllocationCallbacks *allocator)
{
    RendVkMemory memory = {
        .host_mapped_memory = NULL,
        .device_memory = 0,
        .logical_device = logical_device,
        .heap_index = heap_index
    };

    VkMemoryAllocateFlagsInfo flags_info = {
        .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO,
        .pNext = NULL,
        .flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT, /* REQUIRED for BDA buffers */
        .deviceMask = 0
    };

    VkMemoryAllocateInfo alloc_info = {
        .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
        .allocationSize = size,
        .pNext = &flags_info,
        .memoryTypeIndex = heap_index
    };

    vkAllocateMemory(logical_device, &alloc_info, allocator, &memory.device_memory);
    return memory;
}

static void
rend_vk_memory_free(RendVkMemory *memory)
{
    if (memory->host_mapped_memory) {
        rend_vk_memory_unmap(memory);
    }
    if (memory->offset == 0) {
        vkFreeMemory(memory->logical_device, memory->device_memory, memory->allocator);
        memset(memory, 0, sizeof *memory);
    } else {
        PWARN("[REND_VK] Attempted to free memory with an offset!");
    }
}

static void*
rend_vk_memory_map(RendVkMemory *memory)
{
    // SPEC: memory must have been created with a memory type that reports VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
    // vkMapMemory will fail if the implementation is unable to allocate an appropriately sized contiguous virtual address range,
    // e.g. due to virtual address space fragmentation or platform limits.
    // In such cases, vkMapMemory must return VK_ERROR_MEMORY_MAP_FAILED.
    // The application can improve the likelihood of success by reducing the size of the mapped range and/or removing unneeded mappings using vkUnmapMemory.

    vkMapMemory(memory->logical_device, memory->device_memory, memory->offset, memory->size, 0, &memory->host_mapped_memory);
    return memory->host_mapped_memory;
}

static void
rend_vk_memory_unmap(RendVkMemory *memory) 
{
    if (memory->offset == 0) {
        vkUnmapMemory(memory->logical_device, memory->device_memory);
        memory->host_mapped_memory = 0;
    } else {
        PWARN("[REND_VK] Attempted to unmap memory with an offset!");
    }
}

static void
rend_vk_memory_copy(RendVkMemory *memory, size_t offset, const void *data, size_t size)
{
    /* bounds check */
    if (offset + size > memory->size) {
        REND__WARN("Memory write out of bounds!");
        return;
    }

    /* important to cast this */
    uint8_t *dest = memory->host_mapped_memory;
    memcpy(dest + offset, data, size);

    /* flush host writes if memory is non-coherent */
    // VkMappedMemoryRange range = {
    //     .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
    //     .memory = memory->device_memory,
    //     .offset = offset,
    //     .size = size
    // };
    //
    // vkFlushMappedMemoryRanges(memory->logical_device, 1, &range);

}

#endif