Skip to content

Commit

Permalink
start images
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNumbat committed Jan 13, 2024
1 parent 2720249 commit bb30acd
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rvk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ set(SOURCES_RVK
"instance.h"
"device.cpp"
"device.h"
"swapchain.h"
"swapchain.cpp"
"memory.cpp"
"memory.h"
)

if(UNIX AND NOT APPLE)
Expand Down
10 changes: 10 additions & 0 deletions rvk/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ Device::Device(Arc<Physical_Device, Alloc> P, Slice<String_View> extensions,
reinterpret_cast<const char*>(ext.terminate<Mregion<R>>().data()));
}

vk_extensions.push(VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME);
vk_extensions.push(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
vk_extensions.push(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME);
vk_extensions.push(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME);
#ifdef RPP_OS_WINDOWS
vk_extensions.push(VK_KHR_EXTERNAL_FENCE_WIN32_EXTENSION_NAME);
#else
vk_extensions.push(VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME);
#endif

VkDeviceCreateInfo dev_info = {};
dev_info.pNext = &features;
features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
Expand Down
8 changes: 8 additions & 0 deletions rvk/fwd.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

#pragma once

#ifdef RPP_OS_WINDOWS
#define VK_USE_PLATFORM_WIN32_KHR
#endif

#include <rpp/base.h>
#include <volk/volk.h>

Expand All @@ -27,6 +31,10 @@ struct Instance;
struct Debug_Callback;
struct Physical_Device;
struct Device;

struct Image;
struct Image_View;

struct Swapchain;

String_View describe(VkResult result);
Expand Down
54 changes: 54 additions & 0 deletions rvk/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#include <imgui/imgui.h>

#include "memory.h"

namespace rvk::impl {

using namespace rpp;

Device_Memory::Device_Memory(const Arc<Physical_Device, Alloc>& physical_device,
Arc<Device, Alloc> D, Heap location, u64 heap_size)
: device(move(D)), allocator(heap_size), location(location) {

VkMemoryAllocateFlagsInfo flags = {};
flags.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO;
flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;

VkMemoryAllocateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
info.pNext = &flags;
info.allocationSize = heap_size;
info.memoryTypeIndex = device->heap_index(location);

RVK_CHECK(vkAllocateMemory(*device, &info, null, &device_memory));

vkSetDeviceMemoryPriorityEXT(*device, device_memory, 1.0f);

buffer_image_granularity =
physical_device->properties().device.properties.limits.bufferImageGranularity;

if(location == Heap::host) {
RVK_CHECK(vkMapMemory(*device, device_memory, 0, VK_WHOLE_SIZE, 0,
reinterpret_cast<void**>(&persistent_map)));
}
}

Device_Memory::~Device_Memory() {
if(persistent_map) {
vkUnmapMemory(*device, device_memory);
}
if(device_memory) {
vkFreeMemory(*device, device_memory, null);
allocator.statistics().assert_clear();
info("[rvk] Freed GPU heap.");
}
persistent_map = null;
device_memory = null;
}

typename Heap_Allocator::Stats Device_Memory::stats() {
return allocator.statistics();
}

} // namespace rvk::impl
46 changes: 46 additions & 0 deletions rvk/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#pragma once

#include <rpp/base.h>
#include <rpp/range_allocator.h>
#include <rpp/rc.h>

#include "fwd.h"

#include "device.h"

namespace rvk::impl {

using namespace rpp;

using Heap_Allocator = Range_Allocator<Alloc, 32, 10>;
using Buffer_Allocator = Range_Allocator<Alloc, 24, 6>;

struct Device_Memory {

explicit Device_Memory(const Arc<Physical_Device, Alloc>& physical_device,
Arc<Device, Alloc> device, Heap location, u64 size);
~Device_Memory();

Device_Memory(const Device_Memory&) = delete;
Device_Memory& operator=(const Device_Memory&) = delete;
Device_Memory(Device_Memory&&) = delete;
Device_Memory& operator=(Device_Memory&&) = delete;

// Opt<Buffer> allocate(u64 size, VkBufferUsageFlags usage);
// Opt<Image> allocate(u32 width, u32 height, VkFormat format, VkImageUsageFlags usage);

Heap_Allocator::Stats stats();

private:
Arc<Device, Alloc> device;

VkDeviceMemory device_memory = null;

Heap location = Heap::device;
u8* persistent_map = null;
u64 buffer_image_granularity = 0;
Heap_Allocator allocator;
};

} // namespace rvk::impl
11 changes: 11 additions & 0 deletions rvk/swapchain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include <imgui/imgui.h>

#include "device.h"
#include "swapchain.h"

namespace rvk::impl {

using namespace rpp;

} // namespace rvk::impl
65 changes: 65 additions & 0 deletions rvk/swapchain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

#pragma once

#include <rpp/base.h>
#include <rpp/rc.h>

#include "fwd.h"

namespace rvk::impl {

using namespace rpp;

struct Swapchain {

explicit Swapchain(const Arc<Physical_Device, Alloc>& physical_device,
Arc<Device, Alloc> device, VkSurfaceKHR surface);
~Swapchain();

Swapchain(const Swapchain&) = delete;
Swapchain& operator=(const Swapchain&) = delete;
Swapchain(Swapchain&&) = delete;
Swapchain& operator=(Swapchain&&) = delete;

VkFormat format() {
return surface_format.format;
}
VkExtent2D dimensions() {
return extent;
}
u64 slot_count() {
return slots.length();
}
u32 min_image_count() {
return min_images;
}
// Image_View& slot(u64 i) {
// return slots[i].view;
// }

operator VkSwapchainKHR() {
return swapchain;
}

private:
static VkExtent2D choose_extent(VkSurfaceCapabilitiesKHR capabilities);
static VkSurfaceFormatKHR choose_format(Slice<VkSurfaceFormatKHR> formats);
static VkPresentModeKHR choose_present_mode(Slice<VkPresentModeKHR> modes);

Arc<Device, Alloc> device;

struct Slot {
// Image image;
// Image_View view;
};
Vec<Slot, Alloc> slots;

VkSwapchainKHR swapchain = null;

u32 min_images = 0;
VkExtent2D extent = {};
VkPresentModeKHR present_mode = {};
VkSurfaceFormatKHR surface_format = {};
};

} // namespace rvk::impl

0 comments on commit bb30acd

Please sign in to comment.