Skip to content

Commit

Permalink
[mtl,vk] prepare api for host-visible device local memory
Browse files Browse the repository at this point in the history
  • Loading branch information
nicebyte committed Jul 27, 2024
1 parent 534866d commit 75abe48
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
18 changes: 16 additions & 2 deletions include/nicegraf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1938,11 +1938,15 @@ typedef enum ngf_buffer_storage_type {
/**
* \ingroup ngf
*
* Private memory that cannot be accessed by the host directly. The contents of a
* Memory that is local to the device (GPU). Depending on device capabilities,
* this memory may or may not be accessed by the host directly. See
* \ref ngf_device_capabilities::device_local_memory_is_host_visible.
*
* When the device-local memory isn't host-visible, the contents of a
* buffer backed by this type of memory can only be modified by executing a
* \ref ngf_cmd_copy_buffer.
*/
NGF_BUFFER_STORAGE_PRIVATE
NGF_BUFFER_STORAGE_DEVICE_LOCAL
} ngf_buffer_storage_type;

/**
Expand Down Expand Up @@ -2444,6 +2448,16 @@ typedef struct ngf_device_capabilities {
* This value is derived from \ref texture_depth_sample_counts.
*/
ngf_sample_count max_supported_texture_depth_sample_count;

/**
* Indicates whether the device-local storage is also host visible.
* Examples of cases where this may be supported are iGPU systems with unified memory,
* or discrete GPUs with ReBAR enabled.
* On systems with this capability, device-local storage can be mapped directly into
* the host address space, removing the need for host-visible staging buffers in certain
* cases.
*/
bool device_local_memory_is_host_visible;
} ngf_device_capabilities;

/**
Expand Down
4 changes: 2 additions & 2 deletions misc/common/mesh-loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mesh load_mesh_from_file(const char* mesh_file_name, ngf_xfer_encoder xfenc) {
*/
const ngf_buffer_info vertex_data_buffer_info = {
.size = vertex_data_staging_buffer_info.size,
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_VERTEX_BUFFER | NGF_BUFFER_USAGE_XFER_DST,
};
NGF_MISC_CHECK_NGF_ERROR(result.vertex_data.initialize(vertex_data_buffer_info));
Expand All @@ -122,7 +122,7 @@ mesh load_mesh_from_file(const char* mesh_file_name, ngf_xfer_encoder xfenc) {
if (result.num_indices > 0) {
const ngf_buffer_info index_data_buffer_info = {
.size = sizeof(uint32_t) * result.num_indices,
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_INDEX_BUFFER | NGF_BUFFER_USAGE_XFER_DST,
};
NGF_MISC_CHECK_NGF_ERROR(result.index_data.initialize(index_data_buffer_info));
Expand Down
6 changes: 3 additions & 3 deletions samples/06-vertex-attribs/vertex-attribs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ void* sample_initialize(
*/
const ngf_buffer_info vertex_buffer_info = {
.size = sizeof(vertex_attribs::vertex_data),
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_VERTEX_BUFFER | NGF_BUFFER_USAGE_XFER_DST,
};
const ngf_buffer_info index_buffer_info = {
.size = sizeof(vertex_attribs::index_data),
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_INDEX_BUFFER | NGF_BUFFER_USAGE_XFER_DST,
};
const ngf_buffer_info vertex_staging_buffer_info = {
Expand Down Expand Up @@ -269,7 +269,7 @@ void* sample_initialize(
*/
const ngf_buffer_info instance_data_buffer_info = {
.size = vertex_attribs::INSTANCE_DATA_SIZE,
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_TEXEL_BUFFER | NGF_BUFFER_USAGE_XFER_DST,
};
const ngf_buffer_info instance_data_staging_buffer_info = {
Expand Down
4 changes: 2 additions & 2 deletions samples/0b-compute-vertices/compute-vertices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void* sample_initialize(
.buffer_usage = NGF_BUFFER_USAGE_XFER_SRC};
const ngf_buffer_info index_buffer_info {
.size = compute_verts::ntotal_indices * sizeof(uint32_t),
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_XFER_DST | NGF_BUFFER_USAGE_INDEX_BUFFER};
ngf::buffer staging_index_buffer;
NGF_MISC_CHECK_NGF_ERROR(staging_index_buffer.initialize(staging_index_buffer_info));
Expand Down Expand Up @@ -176,7 +176,7 @@ void* sample_initialize(
*/
const ngf_buffer_info vertex_buffer_info {
.size = compute_verts::ntotal_verts * (4u * sizeof(float)) * 2,
.storage_type = NGF_BUFFER_STORAGE_PRIVATE,
.storage_type = NGF_BUFFER_STORAGE_DEVICE_LOCAL,
.buffer_usage = NGF_BUFFER_USAGE_VERTEX_BUFFER | NGF_BUFFER_USAGE_STORAGE_BUFFER};
NGF_MISC_CHECK_NGF_ERROR(state->vertex_buffer.initialize(vertex_buffer_info));
state->compute_buffer_slice.buffer = state->vertex_buffer.get();
Expand Down
3 changes: 2 additions & 1 deletion source/ngf-mtl/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ static void ngfmtl_populate_ngf_device(uint32_t handle, ngf_device& ngfdev, MTL:
caps.max_3d_image_dimension = 2048;
caps.max_image_layers = 2048;
caps.max_uniform_buffer_range = NGF_DEVICE_LIMIT_UNKNOWN;
caps.device_local_memory_is_host_visible = false; // TODO

if (gpu_family_idx >= ngfmtl_gpufam_idx(MTL::GPUFamilyApple6)) {
caps.max_sampled_images_per_stage = 128;
Expand Down Expand Up @@ -1655,7 +1656,7 @@ ngf_id<MTL::Buffer> ngfmtl_create_buffer(const ngf_buffer_info& info) {
case NGF_BUFFER_STORAGE_HOST_WRITEABLE:
options = MTL::ResourceCPUCacheModeWriteCombined | MTL::ResourceStorageModeShared;
break;
case NGF_BUFFER_STORAGE_PRIVATE:
case NGF_BUFFER_STORAGE_DEVICE_LOCAL:
options = MTL::ResourceStorageModePrivate;
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions source/ngf-vk/impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ static VkMemoryPropertyFlags get_vk_memory_flags(ngf_buffer_storage_type s) {
case NGF_BUFFER_STORAGE_HOST_WRITEABLE:
case NGF_BUFFER_STORAGE_HOST_READABLE_WRITEABLE:
return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
case NGF_BUFFER_STORAGE_PRIVATE:
case NGF_BUFFER_STORAGE_DEVICE_LOCAL:
return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
}
return 0;
Expand Down Expand Up @@ -3525,6 +3525,8 @@ ngf_error ngf_get_device_list(const ngf_device** devices, uint32_t* ndevices) {
ngfi_get_highest_sample_count(devcaps->texture_color_sample_counts);
devcaps->max_supported_texture_depth_sample_count =
ngfi_get_highest_sample_count(devcaps->texture_depth_sample_counts);

devcaps->device_local_memory_is_host_visible = false; // TODO
}
ngf_enumerate_devices_cleanup:
if (tmp_instance != VK_NULL_HANDLE) { destroy_vk_instance(tmp_instance, NULL); }
Expand Down Expand Up @@ -5702,7 +5704,7 @@ ngf_error ngf_create_buffer(const ngf_buffer_info* info, ngf_buffer* result) {
const VkBufferUsageFlags vk_usage_flags = get_vk_buffer_usage(info->buffer_usage);
const VkMemoryPropertyFlags vk_mem_flags = get_vk_memory_flags(info->storage_type);
const bool vk_mem_is_host_visible = vk_mem_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
const uint32_t vma_usage_flags = info->storage_type == NGF_BUFFER_STORAGE_PRIVATE
const uint32_t vma_usage_flags = info->storage_type == NGF_BUFFER_STORAGE_DEVICE_LOCAL
? VMA_MEMORY_USAGE_GPU_ONLY
: VMA_MEMORY_USAGE_CPU_ONLY;
ngf_error err = NGF_ERROR_OK;
Expand Down

0 comments on commit 75abe48

Please sign in to comment.