-/*
- Copyright 2017-2018 Google Inc.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-*/
-
-#include "spirv_reflect.h"
-#include <assert.h>
-#include <stdbool.h>
-#include <string.h>
-
-#if defined(WIN32)
- #define _CRTDBG_MAP_ALLOC
- #include <stdlib.h>
- #include <crtdbg.h>
-#else
- #include <stdlib.h>
-#endif
-
-// Temporary enums until these make it into SPIR-V/Vulkan
-// clang-format off
-enum {
- SpvReflectOpDecorateId = 332,
- SpvReflectOpDecorateStringGOOGLE = 5632,
- SpvReflectOpMemberDecorateStringGOOGLE = 5633,
- SpvReflectDecorationHlslCounterBufferGOOGLE = 5634,
- SpvReflectDecorationHlslSemanticGOOGLE = 5635
-};
-// clang-format on
-
-// clang-format off
-enum {
- SPIRV_STARTING_WORD_INDEX = 5,
- SPIRV_WORD_SIZE = sizeof(uint32_t),
- SPIRV_BYTE_WIDTH = 8,
- SPIRV_MINIMUM_FILE_SIZE = SPIRV_STARTING_WORD_INDEX * SPIRV_WORD_SIZE,
- SPIRV_DATA_ALIGNMENT = 4 * SPIRV_WORD_SIZE, // 16
- SPIRV_ACCESS_CHAIN_INDEX_OFFSET = 4,
-};
-// clang-format on
-
-// clang-format off
-enum {
- INVALID_VALUE = 0xFFFFFFFF,
-};
-// clang-format on
-
-// clang-format off
-enum {
- MAX_NODE_NAME_LENGTH = 1024,
-};
-// clang-format on
-
-// clang-format off
-enum {
- IMAGE_SAMPLED = 1,
- IMAGE_STORAGE = 2
-};
-// clang-format on
-
-// clang-format off
-typedef struct ArrayTraits {
- uint32_t element_type_id;
- uint32_t length_id;
-} ArrayTraits;
-// clang-format on
-
-// clang-format off
-typedef struct ImageTraits {
- uint32_t sampled_type_id;
- SpvDim dim;
- uint32_t depth;
- uint32_t arrayed;
- uint32_t ms;
- uint32_t sampled;
- SpvImageFormat image_format;
-} ImageTraits;
-// clang-format on
-
-// clang-format off
-typedef struct NumberDecoration {
- uint32_t word_offset;
- uint32_t value;
-} NumberDecoration;
-// clang-format on
-
-// clang-format off
-typedef struct StringDecoration {
- uint32_t word_offset;
- const char* value;
-} StringDecoration;
-// clang-format on
-
-// clang-format off
-typedef struct Decorations {
- bool is_block;
- bool is_buffer_block;
- bool is_row_major;
- bool is_column_major;
- bool is_built_in;
- bool is_noperspective;
- bool is_flat;
- bool is_non_writable;
- NumberDecoration set;
- NumberDecoration binding;
- NumberDecoration input_attachment_index;
- NumberDecoration location;
- NumberDecoration offset;
- NumberDecoration uav_counter_buffer;
- StringDecoration semantic;
- uint32_t array_stride;
- uint32_t matrix_stride;
- SpvBuiltIn built_in;
-} Decorations;
-// clang-format on
-
-// clang-format off
-typedef struct Node {
- uint32_t result_id;
- SpvOp op;
- uint32_t result_type_id;
- uint32_t type_id;
- SpvStorageClass storage_class;
- uint32_t word_offset;
- uint32_t word_count;
- bool is_type;
-
- ArrayTraits array_traits;
- ImageTraits image_traits;
- uint32_t image_type_id;
-
- const char* name;
- Decorations decorations;
- uint32_t member_count;
- const char** member_names;
- Decorations* member_decorations;
-} Node;
-// clang-format on
-
-// clang-format off
-typedef struct String {
- uint32_t result_id;
- const char* string;
-} String;
-// clang-format on
-
-// clang-format off
-typedef struct Function {
- uint32_t id;
- uint32_t callee_count;
- uint32_t* callees;
- struct Function** callee_ptrs;
- uint32_t accessed_ptr_count;
- uint32_t* accessed_ptrs;
-} Function;
-// clang-format on
-
-// clang-format off
-typedef struct AccessChain {
- uint32_t result_id;
- uint32_t result_type_id;
- //
- // Pointing to the base of a composite object.
- // Generally the id of descriptor block variable
- uint32_t base_id;
- //
- // From spec:
- // The first index in Indexes will select the
- // top-level member/element/component/element
- // of the base composite
- uint32_t index_count;
- uint32_t* indexes;
-} AccessChain;
-// clang-format on
-
-// clang-format off
-typedef struct Parser {
- size_t spirv_word_count;
- uint32_t* spirv_code;
- uint32_t string_count;
- String* strings;
- SpvSourceLanguage source_language;
- uint32_t source_language_version;
- uint32_t source_file_id;
- String source_embedded;
- size_t node_count;
- Node* nodes;
- uint32_t entry_point_count;
- uint32_t function_count;
- Function* functions;
- uint32_t access_chain_count;
- AccessChain* access_chains;
-
- uint32_t type_count;
- uint32_t descriptor_count;
- uint32_t push_constant_count;
-} Parser;
-// clang-format on
-
-static uint32_t Max(uint32_t a, uint32_t b)
-{
- return a > b ? a : b;
-}
-
-static uint32_t RoundUp(uint32_t value, uint32_t multiple)
-{
- assert(multiple && ((multiple & (multiple - 1)) == 0));
- return (value + multiple - 1) & ~(multiple - 1);
-}
-
-#define IsNull(ptr) \
- (ptr == NULL)
-
-#define IsNotNull(ptr) \
- (ptr != NULL)
-
-#define SafeFree(ptr) \
- { \
- if (ptr != NULL) { \
- free((void*)ptr); \
- ptr = NULL; \
- } \
- }
-
-static int SortCompareUint32(const void* a, const void* b)
-{
- const uint32_t* p_a = (const uint32_t*)a;
- const uint32_t* p_b = (const uint32_t*)b;
-
- return (int)*p_a - (int)*p_b;
-}
-
//
-// De-duplicates a sorted array and returns the new size.
+// Copyright 2023 Pixar
//
-// Note: The array doesn't actually need to be sorted, just
-// arranged into "runs" so that all the entries with one
-// value are adjacent.
+// Licensed under the Apache License, Version 2.0 (the "Apache License")
+// with the following modification; you may not use this file except in
+// compliance with the Apache License and the following modification to it:
+// Section 6. Trademarks. is deleted and replaced with:
//
-static size_t DedupSortedUint32(uint32_t* arr, size_t size)
-{
- if (size == 0) {
- return 0;
- }
- size_t dedup_idx = 0;
- for (size_t i = 0; i < size; ++i) {
- if (arr[dedup_idx] != arr[i]) {
- ++dedup_idx;
- arr[dedup_idx] = arr[i];
- }
- }
- return dedup_idx+1;
-}
-
-static bool SearchSortedUint32(const uint32_t* arr, size_t size, uint32_t target)
-{
- size_t lo = 0;
- size_t hi = size;
- while (lo < hi) {
- size_t mid = (hi - lo) / 2 + lo;
- if (arr[mid] == target) {
- return true;
- } else if (arr[mid] < target) {
- lo = mid+1;
- } else {
- hi = mid;
- }
- }
- return false;
-}
-
-static SpvReflectResult IntersectSortedUint32(
- const uint32_t* p_arr0,
- size_t arr0_size,
- const uint32_t* p_arr1,
- size_t arr1_size,
- uint32_t** pp_res,
- size_t* res_size
-)
-{
- *res_size = 0;
- const uint32_t* arr0_end = p_arr0 + arr0_size;
- const uint32_t* arr1_end = p_arr1 + arr1_size;
-
- const uint32_t* idx0 = p_arr0;
- const uint32_t* idx1 = p_arr1;
- while (idx0 != arr0_end && idx1 != arr1_end) {
- if (*idx0 < *idx1) {
- ++idx0;
- } else if (*idx0 > *idx1) {
- ++idx1;
- } else {
- ++*res_size;
- ++idx0;
- ++idx1;
- }
- }
-
- *pp_res = NULL;
- if (*res_size > 0) {
- *pp_res = (uint32_t*)calloc(*res_size, sizeof(**pp_res));
- if (IsNull(*pp_res)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- uint32_t* idxr = *pp_res;
- idx0 = p_arr0;
- idx1 = p_arr1;
- while (idx0 != arr0_end && idx1 != arr1_end) {
- if (*idx0 < *idx1) {
- ++idx0;
- } else if (*idx0 > *idx1) {
- ++idx1;
- } else {
- *(idxr++) = *idx0;
- ++idx0;
- ++idx1;
- }
- }
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-
-static bool InRange(const Parser* p_parser, uint32_t index)
-{
- bool in_range = false;
- if (IsNotNull(p_parser)) {
- in_range = (index < p_parser->spirv_word_count);
- }
- return in_range;
-}
-
-static SpvReflectResult ReadU32(Parser* p_parser, uint32_t word_offset, uint32_t* p_value)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
- assert(InRange(p_parser, word_offset));
- SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF;
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && InRange(p_parser, word_offset)) {
- *p_value = *(p_parser->spirv_code + word_offset);
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- return result;
-}
-
-#define CHECKED_READU32(parser, word_offset, value) \
- { \
- SpvReflectResult checked_readu32_result = ReadU32(parser, \
- word_offset, (uint32_t*)&(value)); \
- if (checked_readu32_result != SPV_REFLECT_RESULT_SUCCESS) { \
- return checked_readu32_result; \
- } \
- }
-
-#define CHECKED_READU32_CAST(parser, word_offset, cast_to_type, value) \
- { \
- uint32_t checked_readu32_cast_u32 = UINT32_MAX; \
- SpvReflectResult checked_readu32_cast_result = ReadU32(parser, \
- word_offset, \
- (uint32_t*)&(checked_readu32_cast_u32)); \
- if (checked_readu32_cast_result != SPV_REFLECT_RESULT_SUCCESS) { \
- return checked_readu32_cast_result; \
- } \
- value = (cast_to_type)checked_readu32_cast_u32; \
- }
-
-#define IF_READU32(result, parser, word_offset, value) \
- if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \
- result = ReadU32(parser, word_offset, (uint32_t*)&(value)); \
- }
-
-#define IF_READU32_CAST(result, parser, word_offset, cast_to_type, value) \
- if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \
- uint32_t if_readu32_cast_u32 = UINT32_MAX; \
- result = ReadU32(parser, word_offset, &if_readu32_cast_u32); \
- if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \
- value = (cast_to_type)if_readu32_cast_u32; \
- } \
- }
-
-static SpvReflectResult ReadStr(
- Parser* p_parser,
- uint32_t word_offset,
- uint32_t word_index,
- uint32_t word_count,
- uint32_t* p_buf_size,
- char* p_buf
-)
-{
- uint32_t limit = (word_offset + word_count);
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
- assert(InRange(p_parser, limit));
- SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF;
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && InRange(p_parser, limit)) {
- const char* c_str = (const char*)(p_parser->spirv_code + word_offset + word_index);
- uint32_t n = word_count * SPIRV_WORD_SIZE;
- uint32_t length_with_terminator = 0;
- for (uint32_t i = 0; i < n; ++i) {
- char c = *(c_str + i);
- if (c == 0) {
- length_with_terminator = i + 1;
- break;
- }
- }
-
- if (length_with_terminator > 0) {
- result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- if (IsNotNull(p_buf_size) && IsNotNull(p_buf)) {
- result = SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED;
- if (length_with_terminator <= *p_buf_size) {
- memset(p_buf, 0, *p_buf_size);
- memcpy(p_buf, c_str, length_with_terminator);
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- }
- else {
- if (IsNotNull(p_buf_size)) {
- *p_buf_size = length_with_terminator;
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- }
- }
- }
- return result;
-}
-
-static SpvReflectDecorationFlags ApplyDecorations(const Decorations* p_decoration_fields)
-{
- SpvReflectDecorationFlags decorations = SPV_REFLECT_DECORATION_NONE;
- if (p_decoration_fields->is_block) {
- decorations |= SPV_REFLECT_DECORATION_BLOCK;
- }
- if (p_decoration_fields->is_buffer_block) {
- decorations |= SPV_REFLECT_DECORATION_BUFFER_BLOCK;
- }
- if (p_decoration_fields->is_row_major) {
- decorations |= SPV_REFLECT_DECORATION_ROW_MAJOR;
- }
- if (p_decoration_fields->is_column_major) {
- decorations |= SPV_REFLECT_DECORATION_COLUMN_MAJOR;
- }
- if (p_decoration_fields->is_built_in) {
- decorations |= SPV_REFLECT_DECORATION_BUILT_IN;
- }
- if (p_decoration_fields->is_noperspective) {
- decorations |= SPV_REFLECT_DECORATION_NOPERSPECTIVE;
- }
- if (p_decoration_fields->is_flat) {
- decorations |= SPV_REFLECT_DECORATION_FLAT;
- }
- if (p_decoration_fields->is_non_writable) {
- decorations |= SPV_REFLECT_DECORATION_NON_WRITABLE;
- }
- return decorations;
-}
-
-static void ApplyNumericTraits(const SpvReflectTypeDescription* p_type, SpvReflectNumericTraits* p_numeric_traits)
-{
- memcpy(p_numeric_traits, &p_type->traits.numeric, sizeof(p_type->traits.numeric));
-}
-
-static void ApplyArrayTraits(const SpvReflectTypeDescription* p_type, SpvReflectArrayTraits* p_array_traits)
-{
- memcpy(p_array_traits, &p_type->traits.array, sizeof(p_type->traits.array));
-}
-
-static Node* FindNode(Parser* p_parser, uint32_t result_id)
-{
- Node* p_node = NULL;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_elem = &(p_parser->nodes[i]);
- if (p_elem->result_id == result_id) {
- p_node = p_elem;
- break;
- }
- }
- return p_node;
-}
-
-static SpvReflectTypeDescription* FindType(SpvReflectShaderModule* p_module, uint32_t type_id)
-{
- SpvReflectTypeDescription* p_type = NULL;
- for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) {
- SpvReflectTypeDescription* p_elem = &(p_module->_internal->type_descriptions[i]);
- if (p_elem->id == type_id) {
- p_type = p_elem;
- break;
- }
- }
- return p_type;
-}
-
-static SpvReflectResult CreateParser(size_t size, void* p_code, Parser* p_parser)
-{
- if (p_code == NULL) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- if (size < SPIRV_MINIMUM_FILE_SIZE) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_CODE_SIZE;
- }
- if ((size % 4) != 0) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_CODE_SIZE;
- }
-
- p_parser->spirv_word_count = size / SPIRV_WORD_SIZE;
- p_parser->spirv_code = (uint32_t*)p_code;
-
- if (p_parser->spirv_code[0] != SpvMagicNumber) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_MAGIC_NUMBER;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static void DestroyParser(Parser* p_parser)
-{
- if (!IsNull(p_parser->nodes)) {
- // Free nodes
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (IsNotNull(p_node->member_names)) {
- SafeFree(p_node->member_names);
- }
- if (IsNotNull(p_node->member_decorations)) {
- SafeFree(p_node->member_decorations);
- }
- }
-
- // Free functions
- for (size_t i = 0; i < p_parser->function_count; ++i) {
- SafeFree(p_parser->functions[i].callees);
- SafeFree(p_parser->functions[i].callee_ptrs);
- SafeFree(p_parser->functions[i].accessed_ptrs);
- }
-
- // Free access chains
- for (uint32_t i = 0; i < p_parser->access_chain_count; ++i) {
- SafeFree(p_parser->access_chains[i].indexes);
- }
-
- SafeFree(p_parser->nodes);
- SafeFree(p_parser->strings);
- SafeFree(p_parser->functions);
- SafeFree(p_parser->access_chains);
- p_parser->node_count = 0;
- }
-}
-
-static SpvReflectResult ParseNodes(Parser* p_parser)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
-
- uint32_t* p_spirv = p_parser->spirv_code;
- uint32_t spirv_word_index = SPIRV_STARTING_WORD_INDEX;
-
- // Count nodes
- uint32_t node_count = 0;
- while (spirv_word_index < p_parser->spirv_word_count) {
- uint32_t word = p_spirv[spirv_word_index];
- SpvOp op = (SpvOp)(word & 0xFFFF);
- uint32_t node_word_count = (word >> 16) & 0xFFFF;
- if (node_word_count == 0) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_INSTRUCTION;
- }
- if (op == SpvOpAccessChain) {
- ++(p_parser->access_chain_count);
- }
- spirv_word_index += node_word_count;
- ++node_count;
- }
-
- if (node_count == 0) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF;
- }
-
- // Allocate nodes
- p_parser->node_count = node_count;
- p_parser->nodes = (Node*)calloc(p_parser->node_count, sizeof(*(p_parser->nodes)));
- if (IsNull(p_parser->nodes)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- // Mark all nodes with an invalid state
- for (uint32_t i = 0; i < node_count; ++i) {
- p_parser->nodes[i].op = (SpvOp)INVALID_VALUE;
- p_parser->nodes[i].storage_class = (SpvStorageClass)INVALID_VALUE;
- p_parser->nodes[i].decorations.set.value = (uint32_t)INVALID_VALUE;
- p_parser->nodes[i].decorations.binding.value = (uint32_t)INVALID_VALUE;
- p_parser->nodes[i].decorations.location.value = (uint32_t)INVALID_VALUE;
- p_parser->nodes[i].decorations.offset.value = (uint32_t)INVALID_VALUE;
- p_parser->nodes[i].decorations.uav_counter_buffer.value = (uint32_t)INVALID_VALUE;
- p_parser->nodes[i].decorations.built_in = (SpvBuiltIn)INVALID_VALUE;
- }
- // Mark source file id node
- p_parser->source_file_id = (uint32_t)INVALID_VALUE;
-
- // Function node
- uint32_t function_node = (uint32_t)INVALID_VALUE;
-
- // Allocate access chain
- if (p_parser->access_chain_count > 0) {
- p_parser->access_chains = (AccessChain*)calloc(p_parser->access_chain_count, sizeof(*(p_parser->access_chains)));
- if (IsNull(p_parser->access_chains)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- // Parse nodes
- uint32_t node_index = 0;
- uint32_t access_chain_index = 0;
- spirv_word_index = SPIRV_STARTING_WORD_INDEX;
- while (spirv_word_index < p_parser->spirv_word_count) {
- uint32_t word = p_spirv[spirv_word_index];
- SpvOp op = (SpvOp)(word & 0xFFFF);
- uint32_t node_word_count = (word >> 16) & 0xFFFF;
-
- Node* p_node = &(p_parser->nodes[node_index]);
- p_node->op = op;
- p_node->word_offset = spirv_word_index;
- p_node->word_count = node_word_count;
-
- switch (p_node->op) {
- default: break;
-
- case SpvOpString: {
- ++(p_parser->string_count);
- }
- break;
-
- case SpvOpSource: {
- CHECKED_READU32_CAST(p_parser, p_node->word_offset + 1, SpvSourceLanguage, p_parser->source_language);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_parser->source_language_version);
- if (p_node->word_count >= 4) {
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_parser->source_file_id);
- }
- }
- break;
-
- case SpvOpEntryPoint: {
- ++(p_parser->entry_point_count);
- }
- break;
-
- case SpvOpName:
- case SpvOpMemberName:
- {
- uint32_t member_offset = (p_node->op == SpvOpMemberName) ? 1 : 0;
- uint32_t name_start = p_node->word_offset + member_offset + 2;
- p_node->name = (const char*)(p_parser->spirv_code + name_start);
- }
- break;
-
- case SpvOpTypeStruct:
- {
- p_node->member_count = p_node->word_count - 2;
- } // Fall through
- case SpvOpTypeVoid:
- case SpvOpTypeBool:
- case SpvOpTypeInt:
- case SpvOpTypeFloat:
- case SpvOpTypeVector:
- case SpvOpTypeMatrix:
- case SpvOpTypeSampler:
- case SpvOpTypeOpaque:
- case SpvOpTypeFunction:
- case SpvOpTypeEvent:
- case SpvOpTypeDeviceEvent:
- case SpvOpTypeReserveId:
- case SpvOpTypeQueue:
- case SpvOpTypePipe:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpTypeImage: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->image_traits.sampled_type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->image_traits.dim);
- CHECKED_READU32(p_parser, p_node->word_offset + 4, p_node->image_traits.depth);
- CHECKED_READU32(p_parser, p_node->word_offset + 5, p_node->image_traits.arrayed);
- CHECKED_READU32(p_parser, p_node->word_offset + 6, p_node->image_traits.ms);
- CHECKED_READU32(p_parser, p_node->word_offset + 7, p_node->image_traits.sampled);
- CHECKED_READU32(p_parser, p_node->word_offset + 8, p_node->image_traits.image_format);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpTypeSampledImage: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->image_type_id);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpTypeArray: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->array_traits.element_type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->array_traits.length_id);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpTypeRuntimeArray: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->array_traits.element_type_id);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpTypePointer: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->storage_class);
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->type_id);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpTypeForwardPointer:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->storage_class);
- p_node->is_type = true;
- }
- break;
-
- case SpvOpConstantTrue:
- case SpvOpConstantFalse:
- case SpvOpConstant:
- case SpvOpConstantComposite:
- case SpvOpConstantSampler:
- case SpvOpConstantNull: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
- }
- break;
-
- case SpvOpSpecConstantTrue:
- case SpvOpSpecConstantFalse:
- case SpvOpSpecConstant:
- case SpvOpSpecConstantComposite:
- case SpvOpSpecConstantOp: {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
- }
- break;
-
- case SpvOpVariable:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->storage_class);
- }
- break;
-
- case SpvOpLoad:
- {
- // Only load enough so OpDecorate can reference the node, skip the remaining operands.
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
- }
- break;
-
- case SpvOpAccessChain:
- {
- AccessChain* p_access_chain = &(p_parser->access_chains[access_chain_index]);
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_access_chain->result_type_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_access_chain->result_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_access_chain->base_id);
- //
- // SPIRV_ACCESS_CHAIN_INDEX_OFFSET (4) is the number of words up until the first index:
- // [Node, Result Type Id, Result Id, Base Id, <Indexes>]
- //
- p_access_chain->index_count = (node_word_count - SPIRV_ACCESS_CHAIN_INDEX_OFFSET);
- if (p_access_chain->index_count > 0) {
- p_access_chain->indexes = (uint32_t*)calloc(p_access_chain->index_count, sizeof(*(p_access_chain->indexes)));
- if (IsNull( p_access_chain->indexes)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- // Parse any index values for access chain
- for (uint32_t index_index = 0; index_index < p_access_chain->index_count; ++index_index) {
- // Read index id
- uint32_t index_id = 0;
- CHECKED_READU32(p_parser, p_node->word_offset + SPIRV_ACCESS_CHAIN_INDEX_OFFSET + index_index, index_id);
- // Find OpConstant node that contains index value
- Node* p_index_value_node = FindNode(p_parser, index_id);
- if ((p_index_value_node != NULL) && (p_index_value_node->op == SpvOpConstant)) {
- // Read index value
- uint32_t index_value = UINT32_MAX;
- CHECKED_READU32(p_parser, p_index_value_node->word_offset + 3, index_value);
- assert(index_value != UINT32_MAX);
- // Write index value to array
- p_access_chain->indexes[index_index] = index_value;
- }
- }
- }
- ++access_chain_index;
- }
- break;
-
- case SpvOpFunction:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
- // Count function definitions, not function declarations. To determine
- // the difference, set an in-function variable, and then if an OpLabel
- // is reached before the end of the function increment the function
- // count.
- function_node = node_index;
- }
- break;
-
- case SpvOpLabel:
- {
- if (function_node != (uint32_t)INVALID_VALUE) {
- Node* p_func_node = &(p_parser->nodes[function_node]);
- CHECKED_READU32(p_parser, p_func_node->word_offset + 2, p_func_node->result_id);
- ++(p_parser->function_count);
- }
- } // Fall through
-
- case SpvOpFunctionEnd:
- {
- function_node = (uint32_t)INVALID_VALUE;
- }
- break;
- }
-
- if (p_node->is_type) {
- ++(p_parser->type_count);
- }
-
- spirv_word_index += node_word_count;
- ++node_index;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseStrings(Parser* p_parser)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
- assert(IsNotNull(p_parser->nodes));
-
- // Early out
- if (p_parser->string_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
- // Allocate string storage
- p_parser->strings = (String*)calloc(p_parser->string_count, sizeof(*(p_parser->strings)));
-
- uint32_t string_index = 0;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (p_node->op != SpvOpString) {
- continue;
- }
-
- // Paranoid check against string count
- assert(string_index < p_parser->string_count);
- if (string_index >= p_parser->string_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- // Result id
- String* p_string = &(p_parser->strings[string_index]);
- CHECKED_READU32(p_parser, p_node->word_offset + 1, p_string->result_id);
-
- // String
- uint32_t string_start = p_node->word_offset + 2;
- p_string->string = (const char*)(p_parser->spirv_code + string_start);
-
- // Increment string index
- ++string_index;
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseSource(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
-
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code)) {
- // Source file
- if (IsNotNull(p_parser->strings)) {
- for (uint32_t i = 0; i < p_parser->string_count; ++i) {
- String* p_string = &(p_parser->strings[i]);
- if (p_string->result_id == p_parser->source_file_id) {
- p_module->source_file = p_string->string;
- break;
- }
- }
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseFunction(Parser* p_parser, Node* p_func_node, Function* p_func, size_t first_label_index)
-{
- p_func->id = p_func_node->result_id;
-
- p_func->callee_count = 0;
- p_func->accessed_ptr_count = 0;
-
- for (size_t i = first_label_index; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (p_node->op == SpvOpFunctionEnd) {
- break;
- }
- switch (p_node->op) {
- case SpvOpFunctionCall: {
- ++(p_func->callee_count);
- }
- break;
- case SpvOpLoad:
- case SpvOpAccessChain:
- case SpvOpInBoundsAccessChain:
- case SpvOpPtrAccessChain:
- case SpvOpArrayLength:
- case SpvOpGenericPtrMemSemantics:
- case SpvOpInBoundsPtrAccessChain:
- case SpvOpStore:
- {
- ++(p_func->accessed_ptr_count);
- }
- break;
- case SpvOpCopyMemory:
- case SpvOpCopyMemorySized:
- {
- p_func->accessed_ptr_count += 2;
- }
- break;
- default: break;
- }
- }
-
- if (p_func->callee_count > 0) {
- p_func->callees = (uint32_t*)calloc(p_func->callee_count,
- sizeof(*(p_func->callees)));
- if (IsNull(p_func->callees)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- if (p_func->accessed_ptr_count > 0) {
- p_func->accessed_ptrs = (uint32_t*)calloc(p_func->accessed_ptr_count,
- sizeof(*(p_func->accessed_ptrs)));
- if (IsNull(p_func->accessed_ptrs)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- p_func->callee_count = 0;
- p_func->accessed_ptr_count = 0;
- for (size_t i = first_label_index; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (p_node->op == SpvOpFunctionEnd) {
- break;
- }
- switch (p_node->op) {
- case SpvOpFunctionCall: {
- CHECKED_READU32(p_parser, p_node->word_offset + 3,
- p_func->callees[p_func->callee_count]);
- (++p_func->callee_count);
- }
- break;
- case SpvOpLoad:
- case SpvOpAccessChain:
- case SpvOpInBoundsAccessChain:
- case SpvOpPtrAccessChain:
- case SpvOpArrayLength:
- case SpvOpGenericPtrMemSemantics:
- case SpvOpInBoundsPtrAccessChain:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 3,
- p_func->accessed_ptrs[p_func->accessed_ptr_count]);
- (++p_func->accessed_ptr_count);
- }
- break;
- case SpvOpStore:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 2,
- p_func->accessed_ptrs[p_func->accessed_ptr_count]);
- (++p_func->accessed_ptr_count);
- }
- break;
- case SpvOpCopyMemory:
- case SpvOpCopyMemorySized:
- {
- CHECKED_READU32(p_parser, p_node->word_offset + 2,
- p_func->accessed_ptrs[p_func->accessed_ptr_count]);
- (++p_func->accessed_ptr_count);
- CHECKED_READU32(p_parser, p_node->word_offset + 3,
- p_func->accessed_ptrs[p_func->accessed_ptr_count]);
- (++p_func->accessed_ptr_count);
- }
- break;
- default: break;
- }
- }
-
- if (p_func->callee_count > 0) {
- qsort(p_func->callees, p_func->callee_count,
- sizeof(*(p_func->callees)), SortCompareUint32);
- }
- p_func->callee_count = (uint32_t)DedupSortedUint32(p_func->callees,
- p_func->callee_count);
-
- if (p_func->accessed_ptr_count > 0) {
- qsort(p_func->accessed_ptrs, p_func->accessed_ptr_count,
- sizeof(*(p_func->accessed_ptrs)), SortCompareUint32);
- }
- p_func->accessed_ptr_count = (uint32_t)DedupSortedUint32(p_func->accessed_ptrs,
- p_func->accessed_ptr_count);
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static int SortCompareFunctions(const void* a, const void* b)
-{
- const Function* af = (const Function*)a;
- const Function* bf = (const Function*)b;
- return (int)af->id - (int)bf->id;
-}
-
-static SpvReflectResult ParseFunctions(Parser* p_parser)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
- assert(IsNotNull(p_parser->nodes));
-
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
- if (p_parser->function_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- p_parser->functions = (Function*)calloc(p_parser->function_count,
- sizeof(*(p_parser->functions)));
- if (IsNull(p_parser->functions)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- size_t function_index = 0;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (p_node->op != SpvOpFunction) {
- continue;
- }
-
- // Skip over function declarations that aren't definitions
- bool func_definition = false;
- // Intentionally reuse i to avoid iterating over these nodes more than
- // once
- for (; i < p_parser->node_count; ++i) {
- if (p_parser->nodes[i].op == SpvOpLabel) {
- func_definition = true;
- break;
- }
- if (p_parser->nodes[i].op == SpvOpFunctionEnd) {
- break;
- }
- }
- if (!func_definition) {
- continue;
- }
-
- Function* p_function = &(p_parser->functions[function_index]);
-
- SpvReflectResult result = ParseFunction(p_parser, p_node, p_function, i);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- ++function_index;
- }
-
- qsort(p_parser->functions, p_parser->function_count,
- sizeof(*(p_parser->functions)), SortCompareFunctions);
-
- // Once they're sorted, link the functions with pointers to improve graph
- // traversal efficiency
- for (size_t i = 0; i < p_parser->function_count; ++i) {
- Function* p_func = &(p_parser->functions[i]);
- if (p_func->callee_count == 0) {
- continue;
- }
- p_func->callee_ptrs = (Function**)calloc(p_func->callee_count,
- sizeof(*(p_func->callee_ptrs)));
- for (size_t j = 0, k = 0; j < p_func->callee_count; ++j) {
- while (p_parser->functions[k].id != p_func->callees[j]) {
- ++k;
- if (k >= p_parser->function_count) {
- // Invalid called function ID somewhere
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- p_func->callee_ptrs[j] = &(p_parser->functions[k]);
- }
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseMemberCounts(Parser* p_parser)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
- assert(IsNotNull(p_parser->nodes));
-
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if ((p_node->op != SpvOpMemberName) && (p_node->op != SpvOpMemberDecorate)) {
- continue;
- }
-
- uint32_t target_id = 0;
- uint32_t member_index = (uint32_t)INVALID_VALUE;
- CHECKED_READU32(p_parser, p_node->word_offset + 1, target_id);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, member_index);
- Node* p_target_node = FindNode(p_parser, target_id);
- // Not all nodes get parsed, so FindNode returning NULL is expected.
- if (IsNull(p_target_node)) {
- continue;
- }
-
- if (member_index == INVALID_VALUE) {
- return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED;
- }
-
- p_target_node->member_count = Max(p_target_node->member_count, member_index + 1);
- }
-
- for (uint32_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (p_node->member_count == 0) {
- continue;
- }
-
- p_node->member_names = (const char **)calloc(p_node->member_count, sizeof(*(p_node->member_names)));
- if (IsNull(p_node->member_names)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- p_node->member_decorations = (Decorations*)calloc(p_node->member_count, sizeof(*(p_node->member_decorations)));
- if (IsNull(p_node->member_decorations)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseNames(Parser* p_parser)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->spirv_code));
- assert(IsNotNull(p_parser->nodes));
-
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if ((p_node->op != SpvOpName) && (p_node->op != SpvOpMemberName)) {
- continue;
- }
-
- uint32_t target_id = 0;
- CHECKED_READU32(p_parser, p_node->word_offset + 1, target_id);
- Node* p_target_node = FindNode(p_parser, target_id);
- // Not all nodes get parsed, so FindNode returning NULL is expected.
- if (IsNull(p_target_node)) {
- continue;
- }
-
- const char** pp_target_name = &(p_target_node->name);
- if (p_node->op == SpvOpMemberName) {
- uint32_t member_index = UINT32_MAX;
- CHECKED_READU32(p_parser, p_node->word_offset + 2, member_index);
- pp_target_name = &(p_target_node->member_names[member_index]);
- }
-
- *pp_target_name = p_node->name;
- }
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDecorations(Parser* p_parser)
-{
- for (uint32_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
-
- if (((uint32_t)p_node->op != (uint32_t)SpvOpDecorate) &&
- ((uint32_t)p_node->op != (uint32_t)SpvOpMemberDecorate) &&
- ((uint32_t)p_node->op != (uint32_t)SpvReflectOpDecorateId) &&
- ((uint32_t)p_node->op != (uint32_t)SpvReflectOpDecorateStringGOOGLE) &&
- ((uint32_t)p_node->op != (uint32_t)SpvReflectOpMemberDecorateStringGOOGLE))
- {
- continue;
- }
-
- // Need to adjust the read offset if this is a member decoration
- uint32_t member_offset = 0;
- if (p_node->op == SpvOpMemberDecorate) {
- member_offset = 1;
- }
-
- // Get decoration
- uint32_t decoration = (uint32_t)INVALID_VALUE;
- CHECKED_READU32(p_parser, p_node->word_offset + member_offset + 2, decoration);
-
- // Filter out the decoration that do not affect reflection, otherwise
- // there will be random crashes because the nodes aren't found.
- bool skip = false;
- switch (decoration) {
- default: {
- skip = true;
- }
- break;
- case SpvDecorationBlock:
- case SpvDecorationBufferBlock:
- case SpvDecorationColMajor:
- case SpvDecorationRowMajor:
- case SpvDecorationArrayStride:
- case SpvDecorationMatrixStride:
- case SpvDecorationBuiltIn:
- case SpvDecorationNoPerspective:
- case SpvDecorationFlat:
- case SpvDecorationNonWritable:
- case SpvDecorationLocation:
- case SpvDecorationBinding:
- case SpvDecorationDescriptorSet:
- case SpvDecorationOffset:
- case SpvDecorationInputAttachmentIndex:
- case SpvReflectDecorationHlslCounterBufferGOOGLE:
- case SpvReflectDecorationHlslSemanticGOOGLE: {
- skip = false;
- }
- break;
- }
- if (skip) {
- continue;
- }
-
- // Find target target node
- uint32_t target_id = 0;
- CHECKED_READU32(p_parser, p_node->word_offset + 1, target_id);
- Node* p_target_node = FindNode(p_parser, target_id);
- if (IsNull(p_target_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Get decorations
- Decorations* p_target_decorations = &(p_target_node->decorations);
- // Update pointer if this is a member member decoration
- if (p_node->op == SpvOpMemberDecorate) {
- uint32_t member_index = (uint32_t)INVALID_VALUE;
- CHECKED_READU32(p_parser, p_node->word_offset + 2, member_index);
- p_target_decorations = &(p_target_node->member_decorations[member_index]);
- }
-
- switch (decoration) {
- default: break;
-
- case SpvDecorationBlock: {
- p_target_decorations->is_block = true;
- }
- break;
-
- case SpvDecorationBufferBlock: {
- p_target_decorations->is_buffer_block = true;
- }
- break;
-
- case SpvDecorationColMajor: {
- p_target_decorations->is_column_major = true;
- }
- break;
-
- case SpvDecorationRowMajor: {
- p_target_decorations->is_row_major = true;
- }
- break;
-
- case SpvDecorationArrayStride: {
- uint32_t word_offset = p_node->word_offset + member_offset + 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->array_stride);
- }
- break;
-
- case SpvDecorationMatrixStride: {
- uint32_t word_offset = p_node->word_offset + member_offset + 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->matrix_stride);
- }
- break;
-
- case SpvDecorationBuiltIn: {
- p_target_decorations->is_built_in = true;
- uint32_t word_offset = p_node->word_offset + member_offset + 3;
- CHECKED_READU32_CAST(p_parser, word_offset, SpvBuiltIn, p_target_decorations->built_in);
- }
- break;
-
- case SpvDecorationNoPerspective: {
- p_target_decorations->is_noperspective = true;
- }
- break;
-
- case SpvDecorationFlat: {
- p_target_decorations->is_flat = true;
- }
- break;
-
- case SpvDecorationNonWritable: {
- p_target_decorations->is_non_writable = true;
- }
- break;
-
- case SpvDecorationLocation: {
- uint32_t word_offset = p_node->word_offset + member_offset + 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->location.value);
- p_target_decorations->location.word_offset = word_offset;
- }
- break;
-
- case SpvDecorationBinding: {
- uint32_t word_offset = p_node->word_offset + member_offset+ 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->binding.value);
- p_target_decorations->binding.word_offset = word_offset;
- }
- break;
-
- case SpvDecorationDescriptorSet: {
- uint32_t word_offset = p_node->word_offset + member_offset+ 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->set.value);
- p_target_decorations->set.word_offset = word_offset;
- }
- break;
-
- case SpvDecorationOffset: {
- uint32_t word_offset = p_node->word_offset + member_offset+ 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->offset.value);
- p_target_decorations->offset.word_offset = word_offset;
- }
- break;
-
- case SpvDecorationInputAttachmentIndex: {
- uint32_t word_offset = p_node->word_offset + member_offset+ 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->input_attachment_index.value);
- p_target_decorations->input_attachment_index.word_offset = word_offset;
- }
- break;
-
- case SpvReflectDecorationHlslCounterBufferGOOGLE: {
- uint32_t word_offset = p_node->word_offset + member_offset+ 3;
- CHECKED_READU32(p_parser, word_offset, p_target_decorations->uav_counter_buffer.value);
- p_target_decorations->uav_counter_buffer.word_offset = word_offset;
- }
- break;
-
- case SpvReflectDecorationHlslSemanticGOOGLE: {
- uint32_t word_offset = p_node->word_offset + member_offset + 3;
- p_target_decorations->semantic.value = (const char*)(p_parser->spirv_code + word_offset);
- p_target_decorations->semantic.word_offset = word_offset;
- }
- break;
- }
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult EnumerateAllUniforms(
- SpvReflectShaderModule* p_module,
- size_t* p_uniform_count,
- uint32_t** pp_uniforms
-)
-{
- *p_uniform_count = p_module->descriptor_binding_count;
- if (*p_uniform_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
- *pp_uniforms = (uint32_t*)calloc(*p_uniform_count, sizeof(**pp_uniforms));
-
- if (IsNull(*pp_uniforms)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- for (size_t i = 0; i < *p_uniform_count; ++i) {
- (*pp_uniforms)[i] = p_module->descriptor_bindings[i].spirv_id;
- }
- qsort(*pp_uniforms, *p_uniform_count, sizeof(**pp_uniforms),
- SortCompareUint32);
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseType(
- Parser* p_parser,
- Node* p_node,
- Decorations* p_struct_member_decorations,
- SpvReflectShaderModule* p_module,
- SpvReflectTypeDescription* p_type
-)
-{
- SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS;
-
- if (p_node->member_count > 0) {
- p_type->member_count = p_node->member_count;
- p_type->members = (SpvReflectTypeDescription*)calloc(p_type->member_count, sizeof(*(p_type->members)));
- if (IsNotNull(p_type->members)) {
- // Mark all members types with an invalid state
- for (size_t i = 0; i < p_type->members->member_count; ++i) {
- SpvReflectTypeDescription* p_member_type = &(p_type->members[i]);
- p_member_type->id = (uint32_t)INVALID_VALUE;
- p_member_type->op = (SpvOp)INVALID_VALUE;
- p_member_type->storage_class = (SpvStorageClass)INVALID_VALUE;
- }
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- // Since the parse descends on type information, these will get overwritten
- // if not guarded against assignment. Only assign if the id is invalid.
- if (p_type->id == INVALID_VALUE) {
- p_type->id = p_node->result_id;
- p_type->op = p_node->op;
- p_type->decoration_flags = 0;
- }
- // Top level types need to pick up decorations from all types below it.
- // Issue and fix here: https://github.com/chaoticbob/SPIRV-Reflect/issues/64
- p_type->decoration_flags = ApplyDecorations(&p_node->decorations);
-
- switch (p_node->op) {
- default: break;
- case SpvOpTypeVoid:
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_VOID;
- break;
-
- case SpvOpTypeBool:
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_BOOL;
- break;
-
- case SpvOpTypeInt: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_INT;
- IF_READU32(result, p_parser, p_node->word_offset + 2, p_type->traits.numeric.scalar.width);
- IF_READU32(result, p_parser, p_node->word_offset + 3, p_type->traits.numeric.scalar.signedness);
- }
- break;
-
- case SpvOpTypeFloat: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_FLOAT;
- IF_READU32(result, p_parser, p_node->word_offset + 2, p_type->traits.numeric.scalar.width);
- }
- break;
-
- case SpvOpTypeVector: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_VECTOR;
- uint32_t component_type_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + 2, component_type_id);
- IF_READU32(result, p_parser, p_node->word_offset + 3, p_type->traits.numeric.vector.component_count);
- // Parse component type
- Node* p_next_node = FindNode(p_parser, component_type_id);
- if (IsNotNull(p_next_node)) {
- result = ParseType(p_parser, p_next_node, NULL, p_module, p_type);
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- break;
-
- case SpvOpTypeMatrix: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_MATRIX;
- uint32_t column_type_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + 2, column_type_id);
- IF_READU32(result, p_parser, p_node->word_offset + 3, p_type->traits.numeric.matrix.column_count);
- Node* p_next_node = FindNode(p_parser, column_type_id);
- if (IsNotNull(p_next_node)) {
- result = ParseType(p_parser, p_next_node, NULL, p_module, p_type);
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- p_type->traits.numeric.matrix.row_count = p_type->traits.numeric.vector.component_count;
- p_type->traits.numeric.matrix.stride = p_node->decorations.matrix_stride;
- // NOTE: Matrix stride is decorated using OpMemberDecoreate - not OpDecoreate.
- if (IsNotNull(p_struct_member_decorations)) {
- p_type->traits.numeric.matrix.stride = p_struct_member_decorations->matrix_stride;
- }
- }
- break;
-
- case SpvOpTypeImage: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE;
- IF_READU32_CAST(result, p_parser, p_node->word_offset + 3, SpvDim, p_type->traits.image.dim);
- IF_READU32(result, p_parser, p_node->word_offset + 4, p_type->traits.image.depth);
- IF_READU32(result, p_parser, p_node->word_offset + 5, p_type->traits.image.arrayed);
- IF_READU32(result, p_parser, p_node->word_offset + 6, p_type->traits.image.ms);
- IF_READU32(result, p_parser, p_node->word_offset + 7, p_type->traits.image.sampled);
- IF_READU32_CAST(result, p_parser, p_node->word_offset + 8, SpvImageFormat, p_type->traits.image.image_format);
- }
- break;
-
- case SpvOpTypeSampler: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLER;
- }
- break;
-
- case SpvOpTypeSampledImage: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLED_IMAGE;
- uint32_t image_type_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + 2, image_type_id);
- Node* p_next_node = FindNode(p_parser, image_type_id);
- if (IsNotNull(p_next_node)) {
- result = ParseType(p_parser, p_next_node, NULL, p_module, p_type);
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- break;
-
- case SpvOpTypeArray: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_ARRAY;
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- uint32_t element_type_id = (uint32_t)INVALID_VALUE;
- uint32_t length_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + 2, element_type_id);
- IF_READU32(result, p_parser, p_node->word_offset + 3, length_id);
- // NOTE: Array stride is decorated using OpDecorate instead of
- // OpMemberDecorate, even if the array is apart of a struct.
- p_type->traits.array.stride = p_node->decorations.array_stride;
- // Get length for current dimension
- Node* p_length_node = FindNode(p_parser, length_id);
- if (IsNotNull(p_length_node)) {
- if (p_length_node->op == SpvOpSpecConstant ||
- p_length_node->op == SpvOpSpecConstantOp) {
- p_type->traits.array.dims[p_type->traits.array.dims_count] = 0xFFFFFFFF;
- p_type->traits.array.dims_count += 1;
- } else {
- uint32_t length = 0;
- IF_READU32(result, p_parser, p_length_node->word_offset + 3, length);
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- // Write the array dim and increment the count and offset
- p_type->traits.array.dims[p_type->traits.array.dims_count] = length;
- p_type->traits.array.dims_count += 1;
- } else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- // Parse next dimension or element type
- Node* p_next_node = FindNode(p_parser, element_type_id);
- if (IsNotNull(p_next_node)) {
- result = ParseType(p_parser, p_next_node, NULL, p_module, p_type);
- }
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- }
- break;
-
- case SpvOpTypeRuntimeArray: {
- uint32_t element_type_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + 2, element_type_id);
- // Parse next dimension or element type
- Node* p_next_node = FindNode(p_parser, element_type_id);
- if (IsNotNull(p_next_node)) {
- result = ParseType(p_parser, p_next_node, NULL, p_module, p_type);
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- break;
-
- case SpvOpTypeStruct: {
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_STRUCT;
- p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_BLOCK;
- uint32_t word_index = 2;
- uint32_t member_index = 0;
- for (; word_index < p_node->word_count; ++word_index, ++member_index) {
- uint32_t member_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + word_index, member_id);
- // Find member node
- Node* p_member_node = FindNode(p_parser, member_id);
- if (IsNull(p_member_node)) {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- break;
- }
-
- // Member decorations
- Decorations* p_member_decorations = &p_node->member_decorations[member_index];
-
- assert(member_index < p_type->member_count);
- // Parse member type
- SpvReflectTypeDescription* p_member_type = &(p_type->members[member_index]);
- p_member_type->id = member_id;
- p_member_type->op = p_member_node->op;
- result = ParseType(p_parser, p_member_node, p_member_decorations, p_module, p_member_type);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- break;
- }
- // This looks wrong
- //p_member_type->type_name = p_member_node->name;
- p_member_type->struct_member_name = p_node->member_names[member_index];
- }
- }
- break;
-
- case SpvOpTypeOpaque: break;
-
- case SpvOpTypePointer: {
- IF_READU32_CAST(result, p_parser, p_node->word_offset + 2, SpvStorageClass, p_type->storage_class);
- uint32_t type_id = (uint32_t)INVALID_VALUE;
- IF_READU32(result, p_parser, p_node->word_offset + 3, type_id);
- // Parse type
- Node* p_next_node = FindNode(p_parser, type_id);
- if (IsNotNull(p_next_node)) {
- result = ParseType(p_parser, p_next_node, NULL, p_module, p_type);
- }
- else {
- result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- break;
- }
-
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- // Names get assigned on the way down. Guard against names
- // get overwritten on the way up.
- if (IsNull(p_type->type_name)) {
- p_type->type_name = p_node->name;
- }
- }
- }
-
- return result;
-}
-
-static SpvReflectResult ParseTypes(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- if (p_parser->type_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- p_module->_internal->type_description_count = p_parser->type_count;
- p_module->_internal->type_descriptions = (SpvReflectTypeDescription*)calloc(p_module->_internal->type_description_count,
- sizeof(*(p_module->_internal->type_descriptions)));
- if (IsNull(p_module->_internal->type_descriptions)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- // Mark all types with an invalid state
- for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) {
- SpvReflectTypeDescription* p_type = &(p_module->_internal->type_descriptions[i]);
- p_type->id = (uint32_t)INVALID_VALUE;
- p_type->op = (SpvOp)INVALID_VALUE;
- p_type->storage_class = (SpvStorageClass)INVALID_VALUE;
- }
-
- size_t type_index = 0;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (! p_node->is_type) {
- continue;
- }
-
- SpvReflectTypeDescription* p_type = &(p_module->_internal->type_descriptions[type_index]);
- SpvReflectResult result = ParseType(p_parser, p_node, NULL, p_module, p_type);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- ++type_index;
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static int SortCompareDescriptorBinding(const void* a, const void* b)
-{
- const SpvReflectDescriptorBinding* p_elem_a = (const SpvReflectDescriptorBinding*)a;
- const SpvReflectDescriptorBinding* p_elem_b = (const SpvReflectDescriptorBinding*)b;
- int value = (int)(p_elem_a->binding) - (int)(p_elem_b->binding);
- if (value == 0) {
- // use spirv-id as a tiebreaker to ensure a stable ordering, as they're guaranteed
- // unique.
- assert(p_elem_a->spirv_id != p_elem_b->spirv_id);
- value = (int)(p_elem_a->spirv_id) - (int)(p_elem_b->spirv_id);
- }
- return value;
-}
-
-static SpvReflectResult ParseDescriptorBindings(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- p_module->descriptor_binding_count = 0;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if ((p_node->op != SpvOpVariable) ||
- ((p_node->storage_class != SpvStorageClassUniform) && (p_node->storage_class != SpvStorageClassUniformConstant)))
- {
- continue;
- }
- if ((p_node->decorations.set.value == INVALID_VALUE) || (p_node->decorations.binding.value == INVALID_VALUE)) {
- continue;
- }
-
- p_module->descriptor_binding_count += 1;
- }
-
- if (p_module->descriptor_binding_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- p_module->descriptor_bindings = (SpvReflectDescriptorBinding*)calloc(p_module->descriptor_binding_count, sizeof(*(p_module->descriptor_bindings)));
- if (IsNull(p_module->descriptor_bindings)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- // Mark all types with an invalid state
- for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]);
- p_descriptor->binding = (uint32_t)INVALID_VALUE;
- p_descriptor->input_attachment_index = (uint32_t)INVALID_VALUE;
- p_descriptor->set = (uint32_t)INVALID_VALUE;
- p_descriptor->descriptor_type = (SpvReflectDescriptorType)INVALID_VALUE;
- p_descriptor->uav_counter_id = (uint32_t)INVALID_VALUE;
- }
-
- size_t descriptor_index = 0;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if ((p_node->op != SpvOpVariable) ||
- ((p_node->storage_class != SpvStorageClassUniform) && (p_node->storage_class != SpvStorageClassUniformConstant)))\
- {
- continue;
- }
- if ((p_node->decorations.set.value == INVALID_VALUE) || (p_node->decorations.binding.value == INVALID_VALUE)) {
- continue;
- }
-
- SpvReflectTypeDescription* p_type = FindType(p_module, p_node->type_id);
- if (IsNull(p_type)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // If the type is a pointer, resolve it
- if (p_type->op == SpvOpTypePointer) {
- // Find the type's node
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Should be the resolved type
- p_type = FindType(p_module, p_type_node->type_id);
- if (IsNull(p_type)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
-
- SpvReflectDescriptorBinding* p_descriptor = &p_module->descriptor_bindings[descriptor_index];
- p_descriptor->spirv_id = p_node->result_id;
- p_descriptor->name = p_node->name;
- p_descriptor->binding = p_node->decorations.binding.value;
- p_descriptor->input_attachment_index = p_node->decorations.input_attachment_index.value;
- p_descriptor->set = p_node->decorations.set.value;
- p_descriptor->count = 1;
- p_descriptor->uav_counter_id = p_node->decorations.uav_counter_buffer.value;
- p_descriptor->type_description = p_type;
-
- // Copy image traits
- if ((p_type->type_flags & SPV_REFLECT_TYPE_FLAG_EXTERNAL_MASK) == SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE) {
- memcpy(&p_descriptor->image, &p_type->traits.image, sizeof(p_descriptor->image));
- }
-
- // This is a workaround for: https://github.com/KhronosGroup/glslang/issues/1096
- {
- const uint32_t resource_mask = SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLED_IMAGE | SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE;
- if ((p_type->type_flags & resource_mask) == resource_mask) {
- memcpy(&p_descriptor->image, &p_type->traits.image, sizeof(p_descriptor->image));
- }
- }
-
- // Copy array traits
- if (p_type->traits.array.dims_count > 0) {
- p_descriptor->array.dims_count = p_type->traits.array.dims_count;
- for (uint32_t dim_index = 0; dim_index < p_type->traits.array.dims_count; ++dim_index) {
- uint32_t dim_value = p_type->traits.array.dims[dim_index];
- p_descriptor->array.dims[dim_index] = dim_value;
- p_descriptor->count *= dim_value;
- }
- }
-
- // Count
-
-
- p_descriptor->word_offset.binding = p_node->decorations.binding.word_offset;
- p_descriptor->word_offset.set = p_node->decorations.set.word_offset;
-
- ++descriptor_index;
- }
-
- if (p_module->descriptor_binding_count > 0) {
- qsort(p_module->descriptor_bindings,
- p_module->descriptor_binding_count,
- sizeof(*(p_module->descriptor_bindings)),
- SortCompareDescriptorBinding);
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDescriptorType(SpvReflectShaderModule* p_module)
-{
- if (p_module->descriptor_binding_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]);
- SpvReflectTypeDescription* p_type = p_descriptor->type_description;
-
- switch (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_EXTERNAL_MASK) {
- default: assert(false && "unknown type flag"); break;
-
- case SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE: {
- if (p_descriptor->image.dim == SpvDimBuffer) {
- switch (p_descriptor->image.sampled) {
- default: assert(false && "unknown texel buffer sampled value"); break;
- case IMAGE_SAMPLED: p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; break;
- case IMAGE_STORAGE: p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; break;
- }
- }
- else if(p_descriptor->image.dim == SpvDimSubpassData) {
- p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
- }
- else {
- switch (p_descriptor->image.sampled) {
- default: assert(false && "unknown image sampled value"); break;
- case IMAGE_SAMPLED: p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE; break;
- case IMAGE_STORAGE: p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE; break;
- }
- }
- }
- break;
-
- case SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLER: {
- p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER;
- }
- break;
-
- case (SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLED_IMAGE | SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE): {
- // This is a workaround for: https://github.com/KhronosGroup/glslang/issues/1096
- if (p_descriptor->image.dim == SpvDimBuffer) {
- switch (p_descriptor->image.sampled) {
- default: assert(false && "unknown texel buffer sampled value"); break;
- case IMAGE_SAMPLED: p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; break;
- case IMAGE_STORAGE: p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; break;
- }
- }
- else {
- p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
- }
- }
- break;
-
- case SPV_REFLECT_TYPE_FLAG_EXTERNAL_BLOCK: {
- if (p_type->decoration_flags & SPV_REFLECT_DECORATION_BLOCK) {
- p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
- }
- else if (p_type->decoration_flags & SPV_REFLECT_DECORATION_BUFFER_BLOCK) {
- p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER;
- }
- else {
- assert(false && "unknown struct");
- }
- }
- break;
- }
-
- switch (p_descriptor->descriptor_type) {
- case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SAMPLER; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER : p_descriptor->resource_type = (SpvReflectResourceType)(SPV_REFLECT_RESOURCE_FLAG_SAMPLER | SPV_REFLECT_RESOURCE_FLAG_SRV); break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_CBV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_CBV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; break;
- case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC : p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; break;
-
- case SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
- break;
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseUAVCounterBindings(SpvReflectShaderModule* p_module)
-{
- char name[MAX_NODE_NAME_LENGTH];
- const char* k_count_tag = "@count";
-
- for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]);
-
- if (p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) {
- continue;
- }
-
- SpvReflectDescriptorBinding* p_counter_descriptor = NULL;
- // Use UAV counter buffer id if present...
- if (p_descriptor->uav_counter_id != UINT32_MAX) {
- for (uint32_t counter_descriptor_index = 0; counter_descriptor_index < p_module->descriptor_binding_count; ++counter_descriptor_index) {
- SpvReflectDescriptorBinding* p_test_counter_descriptor = &(p_module->descriptor_bindings[counter_descriptor_index]);
- if (p_test_counter_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) {
- continue;
- }
- if (p_descriptor->uav_counter_id == p_test_counter_descriptor->spirv_id) {
- p_counter_descriptor = p_test_counter_descriptor;
- break;
- }
- }
- }
- // ...otherwise use old @count convention.
- else {
- const size_t descriptor_name_length = p_descriptor->name? strlen(p_descriptor->name): 0;
-
- memset(name, 0, MAX_NODE_NAME_LENGTH);
- memcpy(name, p_descriptor->name, descriptor_name_length);
-#if defined(WIN32)
- strcat_s(name, MAX_NODE_NAME_LENGTH, k_count_tag);
-#else
- strcat(name, k_count_tag);
-#endif
-
- for (uint32_t counter_descriptor_index = 0; counter_descriptor_index < p_module->descriptor_binding_count; ++counter_descriptor_index) {
- SpvReflectDescriptorBinding* p_test_counter_descriptor = &(p_module->descriptor_bindings[counter_descriptor_index]);
- if (p_test_counter_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) {
- continue;
- }
- if (p_test_counter_descriptor->name && strcmp(name, p_test_counter_descriptor->name) == 0) {
- p_counter_descriptor = p_test_counter_descriptor;
- break;
- }
- }
- }
-
- if (p_counter_descriptor != NULL) {
- p_descriptor->uav_counter_binding = p_counter_descriptor;
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDescriptorBlockVariable(
- Parser* p_parser,
- SpvReflectShaderModule* p_module,
- SpvReflectTypeDescription* p_type,
- SpvReflectBlockVariable* p_var
-)
-{
- bool has_non_writable = false;
-
- if (IsNotNull(p_type->members) && (p_type->member_count > 0)) {
- p_var->member_count = p_type->member_count;
- p_var->members = (SpvReflectBlockVariable*)calloc(p_var->member_count, sizeof(*p_var->members));
- if (IsNull(p_var->members)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Resolve to element type if current type is array or run time array
- if (p_type_node->op == SpvOpTypeArray) {
- while (p_type_node->op == SpvOpTypeArray) {
- p_type_node = FindNode(p_parser, p_type_node->array_traits.element_type_id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
- }
- else if(p_type_node->op == SpvOpTypeRuntimeArray) {
- // Element type description
- p_type = FindType(p_module, p_type_node->array_traits.element_type_id);
- if (IsNull(p_type)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Element type node
- p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
-
- // Parse members
- for (uint32_t member_index = 0; member_index < p_type->member_count; ++member_index) {
- SpvReflectTypeDescription* p_member_type = &p_type->members[member_index];
- SpvReflectBlockVariable* p_member_var = &p_var->members[member_index];
- bool is_struct = (p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) == SPV_REFLECT_TYPE_FLAG_STRUCT;
- if (is_struct) {
- SpvReflectResult result = ParseDescriptorBlockVariable(p_parser, p_module, p_member_type, p_member_var);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
-
- p_member_var->name = p_type_node->member_names[member_index];
- p_member_var->offset = p_type_node->member_decorations[member_index].offset.value;
- p_member_var->decoration_flags = ApplyDecorations(&p_type_node->member_decorations[member_index]);
- p_member_var->flags |= SPV_REFLECT_VARIABLE_FLAGS_UNUSED;
- if (!has_non_writable && (p_member_var->decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE)) {
- has_non_writable = true;
- }
- ApplyNumericTraits(p_member_type, &p_member_var->numeric);
- if (p_member_type->op == SpvOpTypeArray) {
- ApplyArrayTraits(p_member_type, &p_member_var->array);
- }
-
- p_member_var->type_description = p_member_type;
- }
- }
-
- p_var->name = p_type->type_name;
- p_var->type_description = p_type;
- if (has_non_writable) {
- p_var->decoration_flags |= SPV_REFLECT_DECORATION_NON_WRITABLE;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDescriptorBlockVariableSizes(
- Parser* p_parser,
- SpvReflectShaderModule* p_module,
- bool is_parent_root,
- bool is_parent_aos,
- bool is_parent_rta,
- SpvReflectBlockVariable* p_var
-)
-{
- if (p_var->member_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- // Absolute offsets
- for (uint32_t member_index = 0; member_index < p_var->member_count; ++member_index) {
- SpvReflectBlockVariable* p_member_var = &p_var->members[member_index];
- if (is_parent_root) {
- p_member_var->absolute_offset = p_member_var->offset;
- }
- else {
- p_member_var->absolute_offset = is_parent_aos ? 0 : p_member_var->offset + p_var->absolute_offset;
- }
- }
-
- // Size
- for (uint32_t member_index = 0; member_index < p_var->member_count; ++member_index) {
- SpvReflectBlockVariable* p_member_var = &p_var->members[member_index];
- SpvReflectTypeDescription* p_member_type = p_member_var->type_description;
-
- switch (p_member_type->op) {
- case SpvOpTypeBool: {
- p_member_var->size = SPIRV_WORD_SIZE;
- }
- break;
-
- case SpvOpTypeInt:
- case SpvOpTypeFloat: {
- p_member_var->size = p_member_type->traits.numeric.scalar.width / SPIRV_BYTE_WIDTH;
- }
- break;
-
- case SpvOpTypeVector: {
- uint32_t size = p_member_type->traits.numeric.vector.component_count *
- (p_member_type->traits.numeric.scalar.width / SPIRV_BYTE_WIDTH);
- p_member_var->size = size;
- }
- break;
-
- case SpvOpTypeMatrix: {
- if (p_member_var->decoration_flags & SPV_REFLECT_DECORATION_COLUMN_MAJOR) {
- p_member_var->size = p_member_var->numeric.matrix.column_count * p_member_var->numeric.matrix.stride;
- }
- else if (p_member_var->decoration_flags & SPV_REFLECT_DECORATION_ROW_MAJOR) {
- p_member_var->size = p_member_var->numeric.matrix.row_count * p_member_var->numeric.matrix.stride;
- }
- }
- break;
-
- case SpvOpTypeArray: {
- // If array of structs, parse members first...
- bool is_struct = (p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) == SPV_REFLECT_TYPE_FLAG_STRUCT;
- if (is_struct) {
- SpvReflectResult result = ParseDescriptorBlockVariableSizes(p_parser, p_module, false, true, is_parent_rta, p_member_var);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- // ...then array
- uint32_t element_count = (p_member_var->array.dims_count > 0 ? 1 : 0);
- for (uint32_t i = 0; i < p_member_var->array.dims_count; ++i) {
- element_count *= p_member_var->array.dims[i];
- }
- p_member_var->size = element_count * p_member_var->array.stride;
- }
- break;
-
- case SpvOpTypeRuntimeArray: {
- bool is_struct = (p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) == SPV_REFLECT_TYPE_FLAG_STRUCT;
- if (is_struct) {
- SpvReflectResult result = ParseDescriptorBlockVariableSizes(p_parser, p_module, false, true, true, p_member_var);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- }
- break;
-
- case SpvOpTypeStruct: {
- SpvReflectResult result = ParseDescriptorBlockVariableSizes(p_parser, p_module, false, is_parent_aos, is_parent_rta, p_member_var);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- break;
-
- default:
- break;
- }
- }
-
- // Parse padded size using offset difference for all member except for the last entry...
- for (uint32_t member_index = 0; member_index < (p_var->member_count - 1); ++member_index) {
- SpvReflectBlockVariable* p_member_var = &p_var->members[member_index];
- SpvReflectBlockVariable* p_next_member_var = &p_var->members[member_index + 1];
- p_member_var->padded_size = p_next_member_var->offset - p_member_var->offset;
- if (p_member_var->size > p_member_var->padded_size) {
- p_member_var->size = p_member_var->padded_size;
- }
- if (is_parent_rta) {
- p_member_var->padded_size = p_member_var->size;
- }
- }
- // ...last entry just gets rounded up to near multiple of SPIRV_DATA_ALIGNMENT, which is 16 and
- // subtract the offset.
- if (p_var->member_count > 0) {
- SpvReflectBlockVariable* p_member_var = &p_var->members[p_var->member_count - 1];
- p_member_var->padded_size = RoundUp(p_member_var->offset + p_member_var->size, SPIRV_DATA_ALIGNMENT) - p_member_var->offset;
- if (p_member_var->size > p_member_var->padded_size) {
- p_member_var->size = p_member_var->padded_size;
- }
- if (is_parent_rta) {
- p_member_var->padded_size = p_member_var->size;
- }
- }
-
- // @TODO validate this with assertion
- p_var->size = p_var->members[p_var->member_count - 1].offset +
- p_var->members[p_var->member_count - 1].padded_size;
- p_var->padded_size = p_var->size;
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDescriptorBlockVariableUsage(
- Parser* p_parser,
- SpvReflectShaderModule* p_module,
- AccessChain* p_access_chain,
- uint32_t index_index,
- SpvOp override_op_type,
- SpvReflectBlockVariable* p_var
-)
-{
- (void)p_parser;
- (void)p_access_chain;
- (void)p_var;
-
- // Clear the current variable's USED flag
- p_var->flags &= ~SPV_REFLECT_VARIABLE_FLAGS_UNUSED;
-
- // Parsing arrays requires overriding the op type for
- // for the lowest dim's element type.
- SpvOp op_type = p_var->type_description->op;
- if (override_op_type != (SpvOp)INVALID_VALUE) {
- op_type = override_op_type;
- }
-
- switch (op_type) {
- default: break;
-
- case SpvOpTypeArray: {
- // Parse through array's type hierarchy to find the actual/non-array element type
- SpvReflectTypeDescription* p_type = p_var->type_description;
- while ((p_type->op == SpvOpTypeArray) && (index_index < p_access_chain->index_count)) {
- // Find the array element type id
- Node* p_node = FindNode(p_parser, p_type->id);
- if (p_node == NULL) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- uint32_t element_type_id = p_node->array_traits.element_type_id;
- // Get the array element type
- p_type = FindType(p_module, element_type_id);
- if (p_type == NULL) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Next access index
- index_index += 1;
- }
- // Parse current var again with a type override and advanced index index
- SpvReflectResult result = ParseDescriptorBlockVariableUsage(
- p_parser,
- p_module,
- p_access_chain,
- index_index,
- p_type->op,
- p_var);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- break;
-
- case SpvOpTypeStruct: {
- assert(p_var->member_count > 0);
- if (p_var->member_count == 0) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_BLOCK_DATA;
- }
-
- uint32_t index = p_access_chain->indexes[index_index];
-
- if (index >= p_var->member_count) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_BLOCK_MEMBER_REFERENCE;
- }
-
- SpvReflectBlockVariable* p_member_var = &p_var->members[index];
- if (index_index < p_access_chain->index_count) {
- SpvReflectResult result = ParseDescriptorBlockVariableUsage(
- p_parser,
- p_module,
- p_access_chain,
- index_index + 1,
- (SpvOp)INVALID_VALUE,
- p_member_var);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- }
- break;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDescriptorBlocks(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- if (p_module->descriptor_binding_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]);
- SpvReflectTypeDescription* p_type = p_descriptor->type_description;
- if ((p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER) &&
- (p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) )
- {
- continue;
- }
-
- // Mark UNUSED
- p_descriptor->block.flags |= SPV_REFLECT_VARIABLE_FLAGS_UNUSED;
- // Parse descriptor block
- SpvReflectResult result = ParseDescriptorBlockVariable(p_parser, p_module, p_type, &p_descriptor->block);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- for (uint32_t access_chain_index = 0; access_chain_index < p_parser->access_chain_count; ++access_chain_index) {
- AccessChain* p_access_chain = &(p_parser->access_chains[access_chain_index]);
- // Skip any access chains that aren't touching this descriptor block
- if (p_descriptor->spirv_id != p_access_chain->base_id) {
- continue;
- }
- result = ParseDescriptorBlockVariableUsage(
- p_parser,
- p_module,
- p_access_chain,
- 0,
- (SpvOp)INVALID_VALUE,
- &p_descriptor->block);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
-
- p_descriptor->block.name = p_descriptor->name;
-
- bool is_parent_rta = (p_descriptor->descriptor_type == SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER);
- result = ParseDescriptorBlockVariableSizes(p_parser, p_module, true, false, is_parent_rta, &p_descriptor->block);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- if (is_parent_rta) {
- p_descriptor->block.size = 0;
- p_descriptor->block.padded_size = 0;
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseFormat(
- const SpvReflectTypeDescription* p_type,
- SpvReflectFormat* p_format
-)
-{
- SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_INTERNAL_ERROR;
- bool signedness = (p_type->traits.numeric.scalar.signedness != 0);
- if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_VECTOR) {
- uint32_t component_count = p_type->traits.numeric.vector.component_count;
- if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_FLOAT) {
- switch (component_count) {
- case 2: *p_format = SPV_REFLECT_FORMAT_R32G32_SFLOAT; break;
- case 3: *p_format = SPV_REFLECT_FORMAT_R32G32B32_SFLOAT; break;
- case 4: *p_format = SPV_REFLECT_FORMAT_R32G32B32A32_SFLOAT; break;
- }
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- else if (p_type->type_flags & (SPV_REFLECT_TYPE_FLAG_INT | SPV_REFLECT_TYPE_FLAG_BOOL)) {
- switch (component_count) {
- case 2: *p_format = signedness ? SPV_REFLECT_FORMAT_R32G32_SINT : SPV_REFLECT_FORMAT_R32G32_UINT; break;
- case 3: *p_format = signedness ? SPV_REFLECT_FORMAT_R32G32B32_SINT : SPV_REFLECT_FORMAT_R32G32B32_UINT; break;
- case 4: *p_format = signedness ? SPV_REFLECT_FORMAT_R32G32B32A32_SINT : SPV_REFLECT_FORMAT_R32G32B32A32_UINT; break;
- }
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- }
- else if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_FLOAT) {
- *p_format = SPV_REFLECT_FORMAT_R32_SFLOAT;
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- else if (p_type->type_flags & (SPV_REFLECT_TYPE_FLAG_INT | SPV_REFLECT_TYPE_FLAG_BOOL)) {
- if (signedness) {
- *p_format = SPV_REFLECT_FORMAT_R32_SINT;
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- else {
- *p_format = SPV_REFLECT_FORMAT_R32_UINT;
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- }
- else if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) {
- *p_format = SPV_REFLECT_FORMAT_UNDEFINED;
- result = SPV_REFLECT_RESULT_SUCCESS;
- }
- return result;
-}
-
-static SpvReflectResult ParseInterfaceVariable(
- Parser* p_parser,
- const Decorations* p_type_node_decorations,
- SpvReflectShaderModule* p_module,
- SpvReflectTypeDescription* p_type,
- SpvReflectInterfaceVariable* p_var,
- bool* p_has_built_in
-)
-{
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
-
- if (p_type->member_count > 0) {
- p_var->member_count = p_type->member_count;
- p_var->members = (SpvReflectInterfaceVariable*)calloc(p_var->member_count, sizeof(*p_var->members));
- if (IsNull(p_var->members)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- for (uint32_t member_index = 0; member_index < p_type_node->member_count; ++member_index) {
- Decorations* p_member_decorations = &p_type_node->member_decorations[member_index];
- SpvReflectTypeDescription* p_member_type = &p_type->members[member_index];
- SpvReflectInterfaceVariable* p_member_var = &p_var->members[member_index];
- SpvReflectResult result = ParseInterfaceVariable(p_parser, p_member_decorations, p_module, p_member_type, p_member_var, p_has_built_in);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- }
-
- p_var->name = p_type_node->name;
- p_var->decoration_flags = ApplyDecorations(p_type_node_decorations);
- p_var->built_in = p_type_node_decorations->built_in;
- ApplyNumericTraits(p_type, &p_var->numeric);
- if (p_type->op == SpvOpTypeArray) {
- ApplyArrayTraits(p_type, &p_var->array);
- }
-
- p_var->type_description = p_type;
-
- *p_has_built_in |= p_type_node_decorations->is_built_in;
-
- SpvReflectResult result = ParseFormat(p_var->type_description, &p_var->format);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseInterfaceVariables(
- Parser* p_parser,
- SpvReflectShaderModule* p_module,
- SpvReflectEntryPoint* p_entry,
- size_t io_var_count,
- uint32_t* io_vars
-)
-{
- if (io_var_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- p_entry->input_variable_count = 0;
- p_entry->output_variable_count = 0;
- for (size_t i = 0; i < io_var_count; ++i) {
- uint32_t var_result_id = *(io_vars + i);
- Node* p_node = FindNode(p_parser, var_result_id);
- if (IsNull(p_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
-
- if (p_node->storage_class == SpvStorageClassInput) {
- p_entry->input_variable_count += 1;
- }
- else if (p_node->storage_class == SpvStorageClassOutput) {
- p_entry->output_variable_count += 1;
- }
- }
-
- if (p_entry->input_variable_count > 0) {
- p_entry->input_variables = (SpvReflectInterfaceVariable*)calloc(p_entry->input_variable_count, sizeof(*(p_entry->input_variables)));
- if (IsNull(p_entry->input_variables)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
-
- if (p_entry->output_variable_count > 0) {
- p_entry->output_variables = (SpvReflectInterfaceVariable*)calloc(p_entry->output_variable_count, sizeof(*(p_entry->output_variables)));
- if (IsNull(p_entry->output_variables)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- size_t input_index = 0;
- size_t output_index = 0;
- for (size_t i = 0; i < io_var_count; ++i) {
- uint32_t var_result_id = *(io_vars + i);
- Node* p_node = FindNode(p_parser, var_result_id);
- if (IsNull(p_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
-
- SpvReflectTypeDescription* p_type = FindType(p_module, p_node->type_id);
- if (IsNull(p_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // If the type is a pointer, resolve it
- if (p_type->op == SpvOpTypePointer) {
- // Find the type's node
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Should be the resolved type
- p_type = FindType(p_module, p_type_node->type_id);
- if (IsNull(p_type)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
-
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
-
- SpvReflectInterfaceVariable* p_var = NULL;
- if (p_node->storage_class == SpvStorageClassInput) {
- p_var = &(p_entry->input_variables[input_index]);
- p_var->storage_class = SpvStorageClassInput;
- ++input_index;
- }
- else if (p_node->storage_class == SpvStorageClassOutput) {
- p_var = &(p_entry->output_variables[output_index]);
- p_var->storage_class = SpvStorageClassOutput;
- ++output_index;
- } else {
- // interface variables can only have input or output storage classes;
- // anything else is either a new addition or an error.
- assert(false && "Unsupported storage class for interface variable");
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_STORAGE_CLASS;
- }
-
- bool has_built_in = p_node->decorations.is_built_in;
- SpvReflectResult result = ParseInterfaceVariable(
- p_parser,
- &p_type_node->decorations,
- p_module,
- p_type,
- p_var,
- &has_built_in);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- // SPIR-V result id
- p_var->spirv_id = p_node->result_id;
- // Name
- p_var->name = p_node->name;
- // Semantic
- p_var->semantic = p_node->decorations.semantic.value;
-
- // Decorate with built-in if any member is built-in
- if (has_built_in) {
- p_var->decoration_flags |= SPV_REFLECT_DECORATION_BUILT_IN;
- }
-
- // Location is decorated on OpVariable node, not the type node.
- p_var->location = p_node->decorations.location.value;
- p_var->word_offset.location = p_node->decorations.location.word_offset;
-
- // Built in
- if (p_node->decorations.is_built_in) {
- p_var->built_in = p_node->decorations.built_in;
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult EnumerateAllPushConstants(
- SpvReflectShaderModule* p_module,
- size_t* p_push_constant_count,
- uint32_t** p_push_constants
-)
-{
- *p_push_constant_count = p_module->push_constant_block_count;
- if (*p_push_constant_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
- *p_push_constants = (uint32_t*)calloc(*p_push_constant_count, sizeof(**p_push_constants));
-
- if (IsNull(*p_push_constants)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- for (size_t i = 0; i < *p_push_constant_count; ++i) {
- (*p_push_constants)[i] = p_module->push_constant_blocks[i].spirv_id;
- }
- qsort(*p_push_constants, *p_push_constant_count, sizeof(**p_push_constants),
- SortCompareUint32);
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult TraverseCallGraph(
- Parser* p_parser,
- Function* p_func,
- size_t* p_func_count,
- uint32_t* p_func_ids,
- uint32_t depth
-)
-{
- if (depth > p_parser->function_count) {
- // Vulkan does not permit recursion (Vulkan spec Appendix A):
- // "Recursion: The static function-call graph for an entry point must not
- // contain cycles."
- return SPV_REFLECT_RESULT_ERROR_SPIRV_RECURSION;
- }
- if (IsNotNull(p_func_ids)) {
- p_func_ids[(*p_func_count)++] = p_func->id;
- } else {
- ++*p_func_count;
- }
- for (size_t i = 0; i < p_func->callee_count; ++i) {
- SpvReflectResult result = TraverseCallGraph(
- p_parser, p_func->callee_ptrs[i], p_func_count, p_func_ids, depth + 1);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseStaticallyUsedResources(
- Parser* p_parser,
- SpvReflectShaderModule* p_module,
- SpvReflectEntryPoint* p_entry,
- size_t uniform_count,
- uint32_t* uniforms,
- size_t push_constant_count,
- uint32_t* push_constants
-)
-{
- // Find function with the right id
- Function* p_func = NULL;
- for (size_t i = 0; i < p_parser->function_count; ++i) {
- if (p_parser->functions[i].id == p_entry->id) {
- p_func = &(p_parser->functions[i]);
- break;
- }
- }
- if (p_func == NULL) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
-
- size_t called_function_count = 0;
- SpvReflectResult result = TraverseCallGraph(
- p_parser,
- p_func,
- &called_function_count,
- NULL,
- 0);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- uint32_t* p_called_functions = NULL;
- if (called_function_count > 0) {
- p_called_functions = (uint32_t*)calloc(called_function_count, sizeof(*p_called_functions));
- if (IsNull(p_called_functions)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- called_function_count = 0;
- result = TraverseCallGraph(
- p_parser,
- p_func,
- &called_function_count,
- p_called_functions,
- 0);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- if (called_function_count > 0) {
- qsort(
- p_called_functions,
- called_function_count,
- sizeof(*p_called_functions),
- SortCompareUint32);
- }
- called_function_count = DedupSortedUint32(p_called_functions, called_function_count);
-
- uint32_t used_variable_count = 0;
- for (size_t i = 0, j = 0; i < called_function_count; ++i) {
- // No need to bounds check j because a missing ID issue would have been
- // found during TraverseCallGraph
- while (p_parser->functions[j].id != p_called_functions[i]) {
- ++j;
- }
- used_variable_count += p_parser->functions[j].accessed_ptr_count;
- }
- uint32_t* used_variables = NULL;
- if (used_variable_count > 0) {
- used_variables = (uint32_t*)calloc(used_variable_count,
- sizeof(*used_variables));
- if (IsNull(used_variables)) {
- SafeFree(p_called_functions);
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
- used_variable_count = 0;
- for (size_t i = 0, j = 0; i < called_function_count; ++i) {
- while (p_parser->functions[j].id != p_called_functions[i]) {
- ++j;
- }
-
- memcpy(&used_variables[used_variable_count],
- p_parser->functions[j].accessed_ptrs,
- p_parser->functions[j].accessed_ptr_count * sizeof(*used_variables));
- used_variable_count += p_parser->functions[j].accessed_ptr_count;
- }
- SafeFree(p_called_functions);
-
- if (used_variable_count > 0) {
- qsort(used_variables, used_variable_count, sizeof(*used_variables),
- SortCompareUint32);
- }
- used_variable_count = (uint32_t)DedupSortedUint32(used_variables,
- used_variable_count);
-
- // Do set intersection to find the used uniform and push constants
- size_t used_uniform_count = 0;
- //
- SpvReflectResult result0 = IntersectSortedUint32(
- used_variables,
- used_variable_count,
- uniforms,
- uniform_count,
- &p_entry->used_uniforms,
- &used_uniform_count);
-
- size_t used_push_constant_count = 0;
- //
- SpvReflectResult result1 = IntersectSortedUint32(
- used_variables,
- used_variable_count,
- push_constants,
- push_constant_count,
- &p_entry->used_push_constants,
- &used_push_constant_count);
-
- for (uint32_t j = 0; j < p_module->descriptor_binding_count; ++j) {
- SpvReflectDescriptorBinding* p_binding = &p_module->descriptor_bindings[j];
- bool found = SearchSortedUint32(
- used_variables,
- used_variable_count,
- p_binding->spirv_id);
- if (found) {
- p_binding->accessed = 1;
- }
- }
-
- SafeFree(used_variables);
- if (result0 != SPV_REFLECT_RESULT_SUCCESS) {
- return result0;
- }
- if (result1 != SPV_REFLECT_RESULT_SUCCESS) {
- return result1;
- }
-
- p_entry->used_uniform_count = (uint32_t)used_uniform_count;
- p_entry->used_push_constant_count = (uint32_t)used_push_constant_count;
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseEntryPoints(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- if (p_parser->entry_point_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- p_module->entry_point_count = p_parser->entry_point_count;
- p_module->entry_points = (SpvReflectEntryPoint*)calloc(p_module->entry_point_count,
- sizeof(*(p_module->entry_points)));
- if (IsNull(p_module->entry_points)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- SpvReflectResult result;
- size_t uniform_count = 0;
- uint32_t* uniforms = NULL;
- if ((result = EnumerateAllUniforms(p_module, &uniform_count, &uniforms)) !=
- SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- size_t push_constant_count = 0;
- uint32_t* push_constants = NULL;
- if ((result = EnumerateAllPushConstants(p_module, &push_constant_count, &push_constants)) !=
- SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- size_t entry_point_index = 0;
- for (size_t i = 0; entry_point_index < p_parser->entry_point_count && i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if (p_node->op != SpvOpEntryPoint) {
- continue;
- }
-
- SpvReflectEntryPoint* p_entry_point = &(p_module->entry_points[entry_point_index]);
- CHECKED_READU32_CAST(p_parser, p_node->word_offset + 1, SpvExecutionModel, p_entry_point->spirv_execution_model);
- CHECKED_READU32(p_parser, p_node->word_offset + 2, p_entry_point->id);
-
- switch (p_entry_point->spirv_execution_model) {
- default: break;
- case SpvExecutionModelVertex : p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_VERTEX_BIT; break;
- case SpvExecutionModelTessellationControl : p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_TESSELLATION_CONTROL_BIT; break;
- case SpvExecutionModelTessellationEvaluation : p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; break;
- case SpvExecutionModelGeometry : p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_GEOMETRY_BIT; break;
- case SpvExecutionModelFragment : p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_FRAGMENT_BIT; break;
- case SpvExecutionModelGLCompute : p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_COMPUTE_BIT; break;
- }
-
- ++entry_point_index;
-
- // Name length is required to calculate next operand
- uint32_t name_start_word_offset = 3;
- uint32_t name_length_with_terminator = 0;
- result = ReadStr(p_parser, p_node->word_offset + name_start_word_offset, 0, p_node->word_count, &name_length_with_terminator, NULL);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- p_entry_point->name = (const char*)(p_parser->spirv_code + p_node->word_offset + name_start_word_offset);
-
- uint32_t name_word_count = RoundUp(name_length_with_terminator, SPIRV_WORD_SIZE) / SPIRV_WORD_SIZE;
- size_t interface_variable_count = (p_node->word_count - (name_start_word_offset + name_word_count));
- uint32_t* interface_variables = NULL;
- if (interface_variable_count > 0) {
- interface_variables = (uint32_t*)calloc(interface_variable_count, sizeof(*(interface_variables)));
- if (IsNull(interface_variables)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
-
- for (uint32_t var_index = 0; var_index < interface_variable_count; ++var_index) {
- uint32_t var_result_id = (uint32_t)INVALID_VALUE;
- uint32_t offset = name_start_word_offset + name_word_count + var_index;
- CHECKED_READU32(p_parser, p_node->word_offset + offset, var_result_id);
- interface_variables[var_index] = var_result_id;
- }
-
- result = ParseInterfaceVariables(
- p_parser,
- p_module,
- p_entry_point,
- interface_variable_count,
- interface_variables);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- SafeFree(interface_variables);
-
- result = ParseStaticallyUsedResources(
- p_parser,
- p_module,
- p_entry_point,
- uniform_count,
- uniforms,
- push_constant_count,
- push_constants);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- }
-
- SafeFree(uniforms);
- SafeFree(push_constants);
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseExecutionModes(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- assert(IsNotNull(p_parser));
- assert(IsNotNull(p_parser->nodes));
- assert(IsNotNull(p_module));
-
- if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
- for (size_t node_idx = 0; node_idx < p_parser->node_count; ++node_idx) {
- Node* p_node = &(p_parser->nodes[node_idx]);
- if (p_node->op != SpvOpExecutionMode) {
- continue;
- }
-
- // Read entry point id
- uint32_t entry_point_id = 0;
- CHECKED_READU32(p_parser, p_node->word_offset + 1, entry_point_id);
-
- // Find entry point
- SpvReflectEntryPoint* p_entry_point = NULL;
- for (size_t entry_point_idx = 0; entry_point_idx < p_module->entry_point_count; ++entry_point_idx) {
- if (p_module->entry_points[entry_point_idx].id == entry_point_id) {
- p_entry_point = &p_module->entry_points[entry_point_idx];
- break;
- }
- }
- // Bail if entry point is null
- if (IsNull(p_entry_point)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ENTRY_POINT;
- }
-
- // Read execution mode
- uint32_t execution_mode = (uint32_t)INVALID_VALUE;
- CHECKED_READU32(p_parser, p_node->word_offset + 2, execution_mode);
-
- // Parse execution mode
- switch (execution_mode) {
- default: {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_EXECUTION_MODE;
- }
- break;
-
- case SpvExecutionModeInvocations:
- case SpvExecutionModeSpacingEqual:
- case SpvExecutionModeSpacingFractionalEven:
- case SpvExecutionModeSpacingFractionalOdd:
- case SpvExecutionModeVertexOrderCw:
- case SpvExecutionModeVertexOrderCcw:
- case SpvExecutionModePixelCenterInteger:
- case SpvExecutionModeOriginUpperLeft:
- case SpvExecutionModeOriginLowerLeft:
- case SpvExecutionModeEarlyFragmentTests:
- case SpvExecutionModePointMode:
- case SpvExecutionModeXfb:
- case SpvExecutionModeDepthReplacing:
- case SpvExecutionModeDepthGreater:
- case SpvExecutionModeDepthLess:
- case SpvExecutionModeDepthUnchanged:
- break;
-
- case SpvExecutionModeLocalSize: {
- CHECKED_READU32(p_parser, p_node->word_offset + 3, p_entry_point->local_size.x);
- CHECKED_READU32(p_parser, p_node->word_offset + 4, p_entry_point->local_size.y);
- CHECKED_READU32(p_parser, p_node->word_offset + 5, p_entry_point->local_size.z);
- }
- break;
-
- case SpvExecutionModeLocalSizeHint:
- case SpvExecutionModeInputPoints:
- case SpvExecutionModeInputLines:
- case SpvExecutionModeInputLinesAdjacency:
- case SpvExecutionModeTriangles:
- case SpvExecutionModeInputTrianglesAdjacency:
- case SpvExecutionModeQuads:
- case SpvExecutionModeIsolines:
- case SpvExecutionModeOutputVertices:
- case SpvExecutionModeOutputPoints:
- case SpvExecutionModeOutputLineStrip:
- case SpvExecutionModeOutputTriangleStrip:
- case SpvExecutionModeVecTypeHint:
- case SpvExecutionModeContractionOff:
- case SpvExecutionModeInitializer:
- case SpvExecutionModeFinalizer:
- case SpvExecutionModeSubgroupSize:
- case SpvExecutionModeSubgroupsPerWorkgroup:
- case SpvExecutionModeSubgroupsPerWorkgroupId:
- case SpvExecutionModeLocalSizeId:
- case SpvExecutionModeLocalSizeHintId:
- case SpvExecutionModePostDepthCoverage:
- case SpvExecutionModeStencilRefReplacingEXT:
- break;
- }
- }
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParsePushConstantBlocks(Parser* p_parser, SpvReflectShaderModule* p_module)
-{
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if ((p_node->op != SpvOpVariable) || (p_node->storage_class != SpvStorageClassPushConstant)) {
- continue;
- }
-
- p_module->push_constant_block_count += 1;
- }
-
- if (p_module->push_constant_block_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- p_module->push_constant_blocks = (SpvReflectBlockVariable*)calloc(p_module->push_constant_block_count, sizeof(*p_module->push_constant_blocks));
- if (IsNull(p_module->push_constant_blocks)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
-
- uint32_t push_constant_index = 0;
- for (size_t i = 0; i < p_parser->node_count; ++i) {
- Node* p_node = &(p_parser->nodes[i]);
- if ((p_node->op != SpvOpVariable) || (p_node->storage_class != SpvStorageClassPushConstant)) {
- continue;
- }
-
- SpvReflectTypeDescription* p_type = FindType(p_module, p_node->type_id);
- if (IsNull(p_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // If the type is a pointer, resolve it
- if (p_type->op == SpvOpTypePointer) {
- // Find the type's node
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- // Should be the resolved type
- p_type = FindType(p_module, p_type_node->type_id);
- if (IsNull(p_type)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
- }
-
- Node* p_type_node = FindNode(p_parser, p_type->id);
- if (IsNull(p_type_node)) {
- return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
- }
-
- SpvReflectBlockVariable* p_push_constant = &p_module->push_constant_blocks[push_constant_index];
- p_push_constant->spirv_id = p_node->result_id;
- SpvReflectResult result = ParseDescriptorBlockVariable(p_parser, p_module, p_type, p_push_constant);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
- result = ParseDescriptorBlockVariableSizes(p_parser, p_module, true, false, false, p_push_constant);
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- return result;
- }
-
- ++push_constant_index;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static int SortCompareDescriptorSet(const void* a, const void* b)
-{
- const SpvReflectDescriptorSet* p_elem_a = (const SpvReflectDescriptorSet*)a;
- const SpvReflectDescriptorSet* p_elem_b = (const SpvReflectDescriptorSet*)b;
- int value = (int)(p_elem_a->set) - (int)(p_elem_b->set);
- // We should never see duplicate descriptor set numbers in a shader; if so, a tiebreaker
- // would be needed here.
- assert(value != 0);
- return value;
-}
-
-static SpvReflectResult ParseEntrypointDescriptorSets(SpvReflectShaderModule* p_module) {
- // Update the entry point's sets
- for (uint32_t i = 0; i < p_module->entry_point_count; ++i) {
- SpvReflectEntryPoint* p_entry = &p_module->entry_points[i];
- for (uint32_t j = 0; j < p_entry->descriptor_set_count; ++j) {
- SafeFree(p_entry->descriptor_sets[j].bindings);
- }
- SafeFree(p_entry->descriptor_sets);
- p_entry->descriptor_set_count = 0;
- for (uint32_t j = 0; j < p_module->descriptor_set_count; ++j) {
- const SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j];
- for (uint32_t k = 0; k < p_set->binding_count; ++k) {
- bool found = SearchSortedUint32(
- p_entry->used_uniforms,
- p_entry->used_uniform_count,
- p_set->bindings[k]->spirv_id);
- if (found) {
- ++p_entry->descriptor_set_count;
- break;
- }
- }
- }
-
- p_entry->descriptor_sets = NULL;
- if (p_entry->descriptor_set_count > 0) {
- p_entry->descriptor_sets = (SpvReflectDescriptorSet*)calloc(p_entry->descriptor_set_count,
- sizeof(*p_entry->descriptor_sets));
- if (IsNull(p_entry->descriptor_sets)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- }
- p_entry->descriptor_set_count = 0;
- for (uint32_t j = 0; j < p_module->descriptor_set_count; ++j) {
- const SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j];
- uint32_t count = 0;
- for (uint32_t k = 0; k < p_set->binding_count; ++k) {
- bool found = SearchSortedUint32(
- p_entry->used_uniforms,
- p_entry->used_uniform_count,
- p_set->bindings[k]->spirv_id);
- if (found) {
- ++count;
- }
- }
- if (count == 0) {
- continue;
- }
- SpvReflectDescriptorSet* p_entry_set = &p_entry->descriptor_sets[
- p_entry->descriptor_set_count++];
- p_entry_set->set = p_set->set;
- p_entry_set->bindings = (SpvReflectDescriptorBinding**)calloc(count,
- sizeof(*p_entry_set->bindings));
- if (IsNull(p_entry_set->bindings)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- for (uint32_t k = 0; k < p_set->binding_count; ++k) {
- bool found = SearchSortedUint32(
- p_entry->used_uniforms,
- p_entry->used_uniform_count,
- p_set->bindings[k]->spirv_id);
- if (found) {
- p_entry_set->bindings[p_entry_set->binding_count++] = p_set->bindings[k];
- }
- }
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult ParseDescriptorSets(SpvReflectShaderModule* p_module)
-{
- // Count the descriptors in each set
- for (uint32_t i = 0; i < p_module->descriptor_binding_count; ++i) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[i]);
-
- // Look for a target set using the descriptor's set number
- SpvReflectDescriptorSet* p_target_set = NULL;
- for (uint32_t j = 0; j < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++j) {
- SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j];
- if (p_set->set == p_descriptor->set) {
- p_target_set = p_set;
- break;
- }
- }
-
- // If a target set isn't found, find the first available one.
- if (IsNull(p_target_set)) {
- for (uint32_t j = 0; j < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++j) {
- SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j];
- if (p_set->set == (uint32_t)INVALID_VALUE) {
- p_target_set = p_set;
- p_target_set->set = p_descriptor->set;
- break;
- }
- }
- }
-
- if (IsNull(p_target_set)) {
- return SPV_REFLECT_RESULT_ERROR_INTERNAL_ERROR;
- }
-
- p_target_set->binding_count += 1;
- }
-
- // Count the descriptor sets
- for (uint32_t i = 0; i < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++i) {
- const SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[i];
- if (p_set->set != (uint32_t)INVALID_VALUE) {
- p_module->descriptor_set_count += 1;
- }
- }
-
- // Sort the descriptor sets based on numbers
- if (p_module->descriptor_set_count > 0) {
- qsort(p_module->descriptor_sets,
- p_module->descriptor_set_count,
- sizeof(*(p_module->descriptor_sets)),
- SortCompareDescriptorSet);
- }
-
- // Build descriptor pointer array
- for (uint32_t i = 0; i <p_module->descriptor_set_count; ++i) {
- SpvReflectDescriptorSet* p_set = &(p_module->descriptor_sets[i]);
- p_set->bindings = (SpvReflectDescriptorBinding **)calloc(p_set->binding_count, sizeof(*(p_set->bindings)));
-
- uint32_t descriptor_index = 0;
- for (uint32_t j = 0; j < p_module->descriptor_binding_count; ++j) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[j]);
- if (p_descriptor->set == p_set->set) {
- assert(descriptor_index < p_set->binding_count);
- p_set->bindings[descriptor_index] = p_descriptor;
- ++descriptor_index;
- }
- }
- }
-
- return ParseEntrypointDescriptorSets(p_module);
-}
-
-static SpvReflectResult DisambiguateStorageBufferSrvUav(SpvReflectShaderModule* p_module)
-{
- if (p_module->descriptor_binding_count == 0) {
- return SPV_REFLECT_RESULT_SUCCESS;
- }
-
- for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) {
- SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]);
- // Skip everything that isn't a STORAGE_BUFFER descriptor
- if (p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) {
- continue;
- }
-
- //
- // Vulkan doesn't disambiguate between SRVs and UAVs so they
- // come back as STORAGE_BUFFER. The block parsing process will
- // mark a block as non-writable should any member of the block
- // or its descendants are non-writable.
- //
- if (p_descriptor->block.decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE) {
- p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV;
- }
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-static SpvReflectResult SynchronizeDescriptorSets(SpvReflectShaderModule* p_module)
-{
- // Free and reset all descriptor set numbers
- for (uint32_t i = 0; i < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++i) {
- SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[i];
- SafeFree(p_set->bindings);
- p_set->binding_count = 0;
- p_set->set = (uint32_t)INVALID_VALUE;
- }
- // Set descriptor set count to zero
- p_module->descriptor_set_count = 0;
-
- SpvReflectResult result = ParseDescriptorSets(p_module);
- return result;
-}
-
-SpvReflectResult spvReflectGetShaderModule(
- size_t size,
- const void* p_code,
- SpvReflectShaderModule* p_module
-)
-{
- return spvReflectCreateShaderModule(size, p_code, p_module);
-}
-
-SpvReflectResult spvReflectCreateShaderModule(
- size_t size,
- const void* p_code,
- SpvReflectShaderModule* p_module
-)
-{
- // Initialize all module fields to zero
- memset(p_module, 0, sizeof(*p_module));
-
- // Allocate module internals
-#ifdef __cplusplus
- p_module->_internal = (SpvReflectShaderModule::Internal*)calloc(1, sizeof(*(p_module->_internal)));
-#else
- p_module->_internal = calloc(1, sizeof(*(p_module->_internal)));
-#endif
- if (IsNull(p_module->_internal)) {
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- // Allocate SPIR-V code storage
- p_module->_internal->spirv_size = size;
- p_module->_internal->spirv_code = (uint32_t*)calloc(1, p_module->_internal->spirv_size);
- p_module->_internal->spirv_word_count = (uint32_t)(size / SPIRV_WORD_SIZE);
- if (IsNull(p_module->_internal->spirv_code)) {
- SafeFree(p_module->_internal);
- return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
- }
- memcpy(p_module->_internal->spirv_code, p_code, size);
-
- Parser parser = { 0 };
- SpvReflectResult result = CreateParser(p_module->_internal->spirv_size,
- p_module->_internal->spirv_code,
- &parser);
-
- // Generator
- {
- const uint32_t* p_ptr = (const uint32_t*)p_module->_internal->spirv_code;
- p_module->generator = (SpvReflectGenerator)((*(p_ptr + 2) & 0xFFFF0000) >> 16);
- }
-
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseNodes(&parser);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseStrings(&parser);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseSource(&parser, p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseFunctions(&parser);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseMemberCounts(&parser);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseNames(&parser);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseDecorations(&parser);
- }
-
- // Start of reflection data parsing
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- p_module->source_language = parser.source_language;
- p_module->source_language_version = parser.source_language_version;
-
- // Zero out descriptor set data
- p_module->descriptor_set_count = 0;
- memset(p_module->descriptor_sets, 0, SPV_REFLECT_MAX_DESCRIPTOR_SETS * sizeof(*p_module->descriptor_sets));
- // Initialize descriptor set numbers
- for (uint32_t set_number = 0; set_number < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++set_number) {
- p_module->descriptor_sets[set_number].set = (uint32_t)INVALID_VALUE;
- }
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseTypes(&parser, p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseDescriptorBindings(&parser, p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseDescriptorType(p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseUAVCounterBindings(p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseDescriptorBlocks(&parser, p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParsePushConstantBlocks(&parser, p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseEntryPoints(&parser, p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS && p_module->entry_point_count > 0) {
- SpvReflectEntryPoint* p_entry = &(p_module->entry_points[0]);
- p_module->entry_point_name = p_entry->name;
- p_module->entry_point_id = p_entry->id;
- p_module->spirv_execution_model = p_entry->spirv_execution_model;
- p_module->shader_stage = p_entry->shader_stage;
- p_module->input_variable_count = p_entry->input_variable_count;
- p_module->input_variables = p_entry->input_variables;
- p_module->output_variable_count = p_entry->output_variable_count;
- p_module->output_variables = p_entry->output_variables;
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = DisambiguateStorageBufferSrvUav(p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = SynchronizeDescriptorSets(p_module);
- }
- if (result == SPV_REFLECT_RESULT_SUCCESS) {
- result = ParseExecutionModes(&parser, p_module);
- }
-
- // Destroy module if parse was not successful
- if (result != SPV_REFLECT_RESULT_SUCCESS) {
- spvReflectDestroyShaderModule(p_module);
- }
-
- DestroyParser(&parser);
-
- return result;
-}
-
-static void SafeFreeTypes(SpvReflectTypeDescription* p_type)
-{
- if (IsNull(p_type)) {
- return;
- }
-
- if (IsNotNull(p_type->members)) {
- for (size_t i = 0; i < p_type->member_count; ++i) {
- SpvReflectTypeDescription* p_member = &p_type->members[i];
- SafeFreeTypes(p_member);
- }
-
- SafeFree(p_type->members);
- p_type->members = NULL;
- }
-}
-
-static void SafeFreeBlockVariables(SpvReflectBlockVariable* p_block)
-{
- if (IsNull(p_block)) {
- return;
- }
-
- if (IsNotNull(p_block->members)) {
- for (size_t i = 0; i < p_block->member_count; ++i) {
- SpvReflectBlockVariable* p_member = &p_block->members[i];
- SafeFreeBlockVariables(p_member);
- }
-
- SafeFree(p_block->members);
- p_block->members = NULL;
- }
-}
-
-static void SafeFreeInterfaceVariable(SpvReflectInterfaceVariable* p_interface)
-{
- if (IsNull(p_interface)) {
- return;
- }
-
- if (IsNotNull(p_interface->members)) {
- for (size_t i = 0; i < p_interface->member_count; ++i) {
- SpvReflectInterfaceVariable* p_member = &p_interface->members[i];
- SafeFreeInterfaceVariable(p_member);
- }
-
- SafeFree(p_interface->members);
- p_interface->members = NULL;
- }
-}
-
-void spvReflectDestroyShaderModule(SpvReflectShaderModule* p_module)
-{
- if (IsNull(p_module->_internal)) {
- return;
- }
-
- // Descriptor set bindings
- for (size_t i = 0; i < p_module->descriptor_set_count; ++i) {
- SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[i];
- free(p_set->bindings);
- }
-
- // Descriptor binding blocks
- for (size_t i = 0; i < p_module->descriptor_binding_count; ++i) {
- SpvReflectDescriptorBinding* p_descriptor = &p_module->descriptor_bindings[i];
- SafeFreeBlockVariables(&p_descriptor->block);
- }
- SafeFree(p_module->descriptor_bindings);
-
- // Entry points
- for (size_t i = 0; i < p_module->entry_point_count; ++i) {
- SpvReflectEntryPoint* p_entry = &p_module->entry_points[i];
- for (size_t j = 0; j < p_entry->input_variable_count; j++) {
- SafeFreeInterfaceVariable(&p_entry->input_variables[j]);
- }
- for (size_t j = 0; j < p_entry->output_variable_count; j++) {
- SafeFreeInterfaceVariable(&p_entry->output_variables[j]);
- }
- for (uint32_t j = 0; j < p_entry->descriptor_set_count; ++j) {
- SafeFree(p_entry->descriptor_sets[j].bindings);
- }
- SafeFree(p_entry->descriptor_sets);
- SafeFree(p_entry->input_variables);
- SafeFree(p_entry->output_variables);
- SafeFree(p_entry->used_uniforms);
- SafeFree(p_entry->used_push_constants);
- }
- SafeFree(p_module->entry_points);
-
- // Push constants
- for (size_t i = 0; i < p_module->push_constant_block_count; ++i) {
- SafeFreeBlockVariables(&p_module->push_constant_blocks[i]);
- }
- SafeFree(p_module->push_constant_blocks);
-
- // Type infos
- for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) {
- SpvReflectTypeDescription* p_type = &p_module->_internal->type_descriptions[i];
- if (IsNotNull(p_type->members)) {
- SafeFreeTypes(p_type);
- }
- SafeFree(p_type->members);
- }
- SafeFree(p_module->_internal->type_descriptions);
-
- // Free SPIR-V code
- SafeFree(p_module->_internal->spirv_code);
- // Free internal
- SafeFree(p_module->_internal);
-}
-
-uint32_t spvReflectGetCodeSize(const SpvReflectShaderModule* p_module)
-{
- if (IsNull(p_module)) {
- return 0;
- }
-
- return (uint32_t)(p_module->_internal->spirv_size);
-}
-
-const uint32_t* spvReflectGetCode(const SpvReflectShaderModule* p_module)
-{
- if (IsNull(p_module)) {
- return NULL;
- }
-
- return p_module->_internal->spirv_code;
-}
-
-const SpvReflectEntryPoint* spvReflectGetEntryPoint(
- const SpvReflectShaderModule* p_module,
- const char* entry_point
-) {
- if (IsNull(p_module) || IsNull(entry_point)) {
- return NULL;
- }
-
- for (uint32_t i = 0; i < p_module->entry_point_count; ++i) {
- if (strcmp(p_module->entry_points[i].name, entry_point) == 0) {
- return &p_module->entry_points[i];
- }
- }
- return NULL;
-}
-
-SpvReflectResult spvReflectEnumerateDescriptorBindings(
- const SpvReflectShaderModule* p_module,
- uint32_t* p_count,
- SpvReflectDescriptorBinding** pp_bindings
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- if (IsNotNull(pp_bindings)) {
- if (*p_count != p_module->descriptor_binding_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectDescriptorBinding* p_bindings = (SpvReflectDescriptorBinding*)&p_module->descriptor_bindings[index];
- pp_bindings[index] = p_bindings;
- }
- }
- else {
- *p_count = p_module->descriptor_binding_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateEntryPointDescriptorBindings(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t* p_count,
- SpvReflectDescriptorBinding** pp_bindings
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
-
- uint32_t count = 0;
- for (uint32_t i = 0; i < p_module->descriptor_binding_count; ++i) {
- bool found = SearchSortedUint32(
- p_entry->used_uniforms,
- p_entry->used_uniform_count,
- p_module->descriptor_bindings[i].spirv_id);
- if (found) {
- if (IsNotNull(pp_bindings)) {
- if (count >= *p_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
- pp_bindings[count++] = (SpvReflectDescriptorBinding*)&p_module->descriptor_bindings[i];
- } else {
- ++count;
- }
- }
- }
- if (IsNotNull(pp_bindings)) {
- if (count != *p_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
- } else {
- *p_count = count;
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateDescriptorSets(
- const SpvReflectShaderModule* p_module,
- uint32_t* p_count,
- SpvReflectDescriptorSet** pp_sets
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- if (IsNotNull(pp_sets)) {
- if (*p_count != p_module->descriptor_set_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectDescriptorSet* p_set = (SpvReflectDescriptorSet*)&p_module->descriptor_sets[index];
- pp_sets[index] = p_set;
- }
- }
- else {
- *p_count = p_module->descriptor_set_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateEntryPointDescriptorSets(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t* p_count,
- SpvReflectDescriptorSet** pp_sets
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
-
- if (IsNotNull(pp_sets)) {
- if (*p_count != p_entry->descriptor_set_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectDescriptorSet* p_set = (SpvReflectDescriptorSet*)&p_entry->descriptor_sets[index];
- pp_sets[index] = p_set;
- }
- }
- else {
- *p_count = p_entry->descriptor_set_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateInputVariables(
- const SpvReflectShaderModule* p_module,
- uint32_t* p_count,
- SpvReflectInterfaceVariable** pp_variables
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- if (IsNotNull(pp_variables)) {
- if (*p_count != p_module->input_variable_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectInterfaceVariable* p_var = (SpvReflectInterfaceVariable*)&p_module->input_variables[index];
- pp_variables[index] = p_var;
- }
- }
- else {
- *p_count = p_module->input_variable_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateEntryPointInputVariables(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t* p_count,
- SpvReflectInterfaceVariable** pp_variables
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
-
- if (IsNotNull(pp_variables)) {
- if (*p_count != p_entry->input_variable_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectInterfaceVariable* p_var = (SpvReflectInterfaceVariable*)&p_entry->input_variables[index];
- pp_variables[index] = p_var;
- }
- }
- else {
- *p_count = p_entry->input_variable_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateOutputVariables(
- const SpvReflectShaderModule* p_module,
- uint32_t* p_count,
- SpvReflectInterfaceVariable** pp_variables
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- if (IsNotNull(pp_variables)) {
- if (*p_count != p_module->output_variable_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectInterfaceVariable* p_var = (SpvReflectInterfaceVariable*)&p_module->output_variables[index];
- pp_variables[index] = p_var;
- }
- }
- else {
- *p_count = p_module->output_variable_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumerateEntryPointOutputVariables(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t* p_count,
- SpvReflectInterfaceVariable** pp_variables
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
-
- if (IsNotNull(pp_variables)) {
- if (*p_count != p_entry->output_variable_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectInterfaceVariable* p_var = (SpvReflectInterfaceVariable*)&p_entry->output_variables[index];
- pp_variables[index] = p_var;
- }
- }
- else {
- *p_count = p_entry->output_variable_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectEnumeratePushConstantBlocks(
- const SpvReflectShaderModule* p_module,
- uint32_t* p_count,
- SpvReflectBlockVariable** pp_blocks
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- if (pp_blocks != NULL) {
- if (*p_count != p_module->push_constant_block_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
-
- for (uint32_t index = 0; index < *p_count; ++index) {
- SpvReflectBlockVariable* p_push_constant_blocks = (SpvReflectBlockVariable*)&p_module->push_constant_blocks[index];
- pp_blocks[index] = p_push_constant_blocks;
- }
- }
- else {
- *p_count = p_module->push_constant_block_count;
- }
-
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-SpvReflectResult spvReflectEnumeratePushConstants(
- const SpvReflectShaderModule* p_module,
- uint32_t* p_count,
- SpvReflectBlockVariable** pp_blocks
-)
-{
- return spvReflectEnumeratePushConstantBlocks(p_module, p_count, pp_blocks);
-}
-
-SpvReflectResult spvReflectEnumerateEntryPointPushConstantBlocks(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t* p_count,
- SpvReflectBlockVariable** pp_blocks
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_count)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
-
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
-
- uint32_t count = 0;
- for (uint32_t i = 0; i < p_module->push_constant_block_count; ++i) {
- bool found = SearchSortedUint32(p_entry->used_push_constants,
- p_entry->used_push_constant_count,
- p_module->push_constant_blocks[i].spirv_id);
- if (found) {
- if (IsNotNull(pp_blocks)) {
- if (count >= *p_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
- pp_blocks[count++] = (SpvReflectBlockVariable*)&p_module->push_constant_blocks[i];
- } else {
- ++count;
- }
- }
- }
- if (IsNotNull(pp_blocks)) {
- if (count != *p_count) {
- return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
- }
- } else {
- *p_count = count;
- }
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-const SpvReflectDescriptorBinding* spvReflectGetDescriptorBinding(
- const SpvReflectShaderModule* p_module,
- uint32_t binding_number,
- uint32_t set_number,
- SpvReflectResult* p_result
-)
-{
- const SpvReflectDescriptorBinding* p_descriptor = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->descriptor_binding_count; ++index) {
- const SpvReflectDescriptorBinding* p_potential = &p_module->descriptor_bindings[index];
- if ((p_potential->binding == binding_number) && (p_potential->set == set_number)) {
- p_descriptor = p_potential;
- break;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_descriptor)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_descriptor;
-}
-
-const SpvReflectDescriptorBinding* spvReflectGetEntryPointDescriptorBinding(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t binding_number,
- uint32_t set_number,
- SpvReflectResult* p_result
-)
-{
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectDescriptorBinding* p_descriptor = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->descriptor_binding_count; ++index) {
- const SpvReflectDescriptorBinding* p_potential = &p_module->descriptor_bindings[index];
- bool found = SearchSortedUint32(
- p_entry->used_uniforms,
- p_entry->used_uniform_count,
- p_potential->spirv_id);
- if ((p_potential->binding == binding_number) && (p_potential->set == set_number) && found) {
- p_descriptor = p_potential;
- break;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_descriptor)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_descriptor;
-}
-
-const SpvReflectDescriptorSet* spvReflectGetDescriptorSet(
- const SpvReflectShaderModule* p_module,
- uint32_t set_number,
- SpvReflectResult* p_result
-)
-{
- const SpvReflectDescriptorSet* p_set = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->descriptor_set_count; ++index) {
- const SpvReflectDescriptorSet* p_potential = &p_module->descriptor_sets[index];
- if (p_potential->set == set_number) {
- p_set = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_set)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_set;
-}
-
-const SpvReflectDescriptorSet* spvReflectGetEntryPointDescriptorSet(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t set_number,
- SpvReflectResult* p_result)
-{
- const SpvReflectDescriptorSet* p_set = NULL;
- if (IsNotNull(p_module)) {
- const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- for (uint32_t index = 0; index < p_entry->descriptor_set_count; ++index) {
- const SpvReflectDescriptorSet* p_potential = &p_entry->descriptor_sets[index];
- if (p_potential->set == set_number) {
- p_set = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_set)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_set;
-}
-
-
-const SpvReflectInterfaceVariable* spvReflectGetInputVariableByLocation(
- const SpvReflectShaderModule* p_module,
- uint32_t location,
- SpvReflectResult* p_result
-)
-{
- if (location == INVALID_VALUE) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->input_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_module->input_variables[index];
- if (p_potential->location == location) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-const SpvReflectInterfaceVariable* spvReflectGetInputVariable(
- const SpvReflectShaderModule* p_module,
- uint32_t location,
- SpvReflectResult* p_result
-)
-{
- return spvReflectGetInputVariableByLocation(p_module, location, p_result);
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetEntryPointInputVariableByLocation(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t location,
- SpvReflectResult* p_result
-)
-{
- if (location == INVALID_VALUE) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
-
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- for (uint32_t index = 0; index < p_entry->input_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_entry->input_variables[index];
- if (p_potential->location == location) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetInputVariableBySemantic(
- const SpvReflectShaderModule* p_module,
- const char* semantic,
- SpvReflectResult* p_result
-)
-{
- if (IsNull(semantic)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- return NULL;
- }
- if (semantic[0] == '\0') {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->input_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_module->input_variables[index];
- if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetEntryPointInputVariableBySemantic(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- const char* semantic,
- SpvReflectResult* p_result
-)
-{
- if (IsNull(semantic)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- return NULL;
- }
- if (semantic[0] == '\0') {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- for (uint32_t index = 0; index < p_entry->input_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_entry->input_variables[index];
- if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetOutputVariableByLocation(
- const SpvReflectShaderModule* p_module,
- uint32_t location,
- SpvReflectResult* p_result
-)
-{
- if (location == INVALID_VALUE) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->output_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_module->output_variables[index];
- if (p_potential->location == location) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-const SpvReflectInterfaceVariable* spvReflectGetOutputVariable(
- const SpvReflectShaderModule* p_module,
- uint32_t location,
- SpvReflectResult* p_result
-)
-{
- return spvReflectGetOutputVariableByLocation(p_module, location, p_result);
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetEntryPointOutputVariableByLocation(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- uint32_t location,
- SpvReflectResult* p_result
-)
-{
- if (location == INVALID_VALUE) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
-
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- for (uint32_t index = 0; index < p_entry->output_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_entry->output_variables[index];
- if (p_potential->location == location) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetOutputVariableBySemantic(
- const SpvReflectShaderModule* p_module,
- const char* semantic,
- SpvReflectResult* p_result
-)
-{
- if (IsNull(semantic)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- return NULL;
- }
- if (semantic[0] == '\0') {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- for (uint32_t index = 0; index < p_module->output_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_module->output_variables[index];
- if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-
-const SpvReflectInterfaceVariable* spvReflectGetEntryPointOutputVariableBySemantic(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- const char* semantic,
- SpvReflectResult* p_result)
-{
- if (IsNull(semantic)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- return NULL;
- }
- if (semantic[0] == '\0') {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- const SpvReflectInterfaceVariable* p_var = NULL;
- if (IsNotNull(p_module)) {
- const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- for (uint32_t index = 0; index < p_entry->output_variable_count; ++index) {
- const SpvReflectInterfaceVariable* p_potential = &p_entry->output_variables[index];
- if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) {
- p_var = p_potential;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_var)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_var;
-}
-
-const SpvReflectBlockVariable* spvReflectGetPushConstantBlock(
- const SpvReflectShaderModule* p_module,
- uint32_t index,
- SpvReflectResult* p_result
-)
-{
- const SpvReflectBlockVariable* p_push_constant = NULL;
- if (IsNotNull(p_module)) {
- if (index < p_module->push_constant_block_count) {
- p_push_constant = &p_module->push_constant_blocks[index];
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_push_constant)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_push_constant;
-}
-const SpvReflectBlockVariable* spvReflectGetPushConstant(
- const SpvReflectShaderModule* p_module,
- uint32_t index,
- SpvReflectResult* p_result
-)
-{
- return spvReflectGetPushConstantBlock(p_module, index, p_result);
-}
-
-const SpvReflectBlockVariable* spvReflectGetEntryPointPushConstantBlock(
- const SpvReflectShaderModule* p_module,
- const char* entry_point,
- SpvReflectResult* p_result)
-{
- const SpvReflectBlockVariable* p_push_constant = NULL;
- if (IsNotNull(p_module)) {
- const SpvReflectEntryPoint* p_entry =
- spvReflectGetEntryPoint(p_module, entry_point);
- if (IsNull(p_entry)) {
- if (IsNotNull(p_result)) {
- *p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
- }
- return NULL;
- }
- for (uint32_t i = 0; i < p_module->push_constant_block_count; ++i) {
- bool found = SearchSortedUint32(
- p_entry->used_push_constants,
- p_entry->used_push_constant_count,
- p_module->push_constant_blocks[i].spirv_id);
- if (found) {
- p_push_constant = &p_module->push_constant_blocks[i];
- break;
- }
- }
- }
- if (IsNotNull(p_result)) {
- *p_result = IsNotNull(p_push_constant)
- ? SPV_REFLECT_RESULT_SUCCESS
- : (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER
- : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND);
- }
- return p_push_constant;
-}
-
-SpvReflectResult spvReflectChangeDescriptorBindingNumbers(
- SpvReflectShaderModule* p_module,
- const SpvReflectDescriptorBinding* p_binding,
- uint32_t new_binding_number,
- uint32_t new_set_binding
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_binding)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
-
- SpvReflectDescriptorBinding* p_target_descriptor = NULL;
- for (uint32_t index = 0; index < p_module->descriptor_binding_count; ++index) {
- if(&p_module->descriptor_bindings[index] == p_binding) {
- p_target_descriptor = &p_module->descriptor_bindings[index];
- break;
- }
- }
-
- if (IsNotNull(p_target_descriptor)) {
- if (p_target_descriptor->word_offset.binding > (p_module->_internal->spirv_word_count - 1)) {
- return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED;
- }
- // Binding number
- if (new_binding_number != (uint32_t)SPV_REFLECT_BINDING_NUMBER_DONT_CHANGE) {
- uint32_t* p_code = p_module->_internal->spirv_code + p_target_descriptor->word_offset.binding;
- *p_code = new_binding_number;
- p_target_descriptor->binding = new_binding_number;
- }
- // Set number
- if (new_set_binding != (uint32_t)SPV_REFLECT_SET_NUMBER_DONT_CHANGE) {
- uint32_t* p_code = p_module->_internal->spirv_code + p_target_descriptor->word_offset.set;
- *p_code = new_set_binding;
- p_target_descriptor->set = new_set_binding;
- }
- }
-
- SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS;
- if (new_set_binding != (uint32_t)SPV_REFLECT_SET_NUMBER_DONT_CHANGE) {
- result = SynchronizeDescriptorSets(p_module);
- }
- return result;
-}
-SpvReflectResult spvReflectChangeDescriptorBindingNumber(
- SpvReflectShaderModule* p_module,
- const SpvReflectDescriptorBinding* p_descriptor_binding,
- uint32_t new_binding_number,
- uint32_t optional_new_set_number
-)
-{
- return spvReflectChangeDescriptorBindingNumbers(
- p_module,p_descriptor_binding,
- new_binding_number,
- optional_new_set_number);
-}
-
-SpvReflectResult spvReflectChangeDescriptorSetNumber(
- SpvReflectShaderModule* p_module,
- const SpvReflectDescriptorSet* p_set,
- uint32_t new_set_number
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_set)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- SpvReflectDescriptorSet* p_target_set = NULL;
- for (uint32_t index = 0; index < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++index) {
- // The descriptor sets for specific entry points might not be in this set,
- // so just match on set index.
- if (p_module->descriptor_sets[index].set == p_set->set) {
- p_target_set = (SpvReflectDescriptorSet*)p_set;
- break;
- }
- }
-
- SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS;
- if (IsNotNull(p_target_set) && new_set_number != (uint32_t)SPV_REFLECT_SET_NUMBER_DONT_CHANGE) {
- for (uint32_t index = 0; index < p_target_set->binding_count; ++index) {
- SpvReflectDescriptorBinding* p_descriptor = p_target_set->bindings[index];
- if (p_descriptor->word_offset.set > (p_module->_internal->spirv_word_count - 1)) {
- return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED;
- }
-
- uint32_t* p_code = p_module->_internal->spirv_code + p_descriptor->word_offset.set;
- *p_code = new_set_number;
- p_descriptor->set = new_set_number;
- }
-
- result = SynchronizeDescriptorSets(p_module);
- }
-
- return result;
-}
-
-static SpvReflectResult ChangeVariableLocation(
- SpvReflectShaderModule* p_module,
- SpvReflectInterfaceVariable* p_variable,
- uint32_t new_location
-)
-{
- if (p_variable->word_offset.location > (p_module->_internal->spirv_word_count - 1)) {
- return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED;
- }
- uint32_t* p_code = p_module->_internal->spirv_code + p_variable->word_offset.location;
- *p_code = new_location;
- p_variable->location = new_location;
- return SPV_REFLECT_RESULT_SUCCESS;
-}
-
-SpvReflectResult spvReflectChangeInputVariableLocation(
- SpvReflectShaderModule* p_module,
- const SpvReflectInterfaceVariable* p_input_variable,
- uint32_t new_location
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_input_variable)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- for (uint32_t index = 0; index < p_module->input_variable_count; ++index) {
- if(&p_module->input_variables[index] == p_input_variable) {
- return ChangeVariableLocation(p_module, &p_module->input_variables[index], new_location);
- }
- }
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
-}
-
-SpvReflectResult spvReflectChangeOutputVariableLocation(
- SpvReflectShaderModule* p_module,
- const SpvReflectInterfaceVariable* p_output_variable,
- uint32_t new_location
-)
-{
- if (IsNull(p_module)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- if (IsNull(p_output_variable)) {
- return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
- }
- for (uint32_t index = 0; index < p_module->output_variable_count; ++index) {
- if(&p_module->output_variables[index] == p_output_variable) {
- return ChangeVariableLocation(p_module, &p_module->output_variables[index], new_location);
- }
- }
- return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND;
-}
-
-const char* spvReflectSourceLanguage(SpvSourceLanguage source_lang)
-{
- switch (source_lang) {
- case SpvSourceLanguageUnknown : return "Unknown";
- case SpvSourceLanguageESSL : return "ESSL";
- case SpvSourceLanguageGLSL : return "GLSL";
- case SpvSourceLanguageOpenCL_C : return "OpenCL_C";
- case SpvSourceLanguageOpenCL_CPP : return "OpenCL_CPP";
- case SpvSourceLanguageHLSL : return "HLSL";
-#if SPV_VERSION == 0x10600
- case SpvSourceLanguageCPP_for_OpenCL : return "CPP_for_OpenCL";
- case SpvSourceLanguageSYCL : return "SYCL";
-#endif
+// 6. Trademarks. This License does not grant permission to use the trade
+// names, trademarks, service marks, or product names of the Licensor
+// and its affiliates, except as required to comply with Section 4(c) of
+// the License and to reproduce the content of the NOTICE file.
+//
+// You may obtain a copy of the Apache License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the Apache License with the above modification is
+// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the Apache License for the specific
+// language governing permissions and limitations under the Apache License.
+//
+#include "pxr/imaging/hgiVulkan/spirv_reflect.h"
- case SpvSourceLanguageMax:
- break;
- }
- return "";
-}
+#include <SPIRV-Reflect/spirv_reflect.c>
-//\r
-// Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved.\r
-//\r
-// Permission is hereby granted, free of charge, to any person obtaining a copy\r
-// of this software and associated documentation files (the "Software"), to deal\r
-// in the Software without restriction, including without limitation the rights\r
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-// copies of the Software, and to permit persons to whom the Software is\r
-// furnished to do so, subject to the following conditions:\r
-//\r
-// The above copyright notice and this permission notice shall be included in\r
-// all copies or substantial portions of the Software.\r
-//\r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-// THE SOFTWARE.\r
-//\r
-\r
-#ifndef AMD_VULKAN_MEMORY_ALLOCATOR_H\r
-#define AMD_VULKAN_MEMORY_ALLOCATOR_H\r
-\r
-/** \mainpage Vulkan Memory Allocator\r
-\r
-<b>Version 3.0.0-development</b> (2020-03-23)\r
-\r
-Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved. \n\r
-License: MIT\r
-\r
-Documentation of all members: vk_mem_alloc.h\r
-\r
-\section main_table_of_contents Table of contents\r
-\r
-- <b>User guide</b>\r
- - \subpage quick_start\r
- - [Project setup](@ref quick_start_project_setup)\r
- - [Initialization](@ref quick_start_initialization)\r
- - [Resource allocation](@ref quick_start_resource_allocation)\r
- - \subpage choosing_memory_type\r
- - [Usage](@ref choosing_memory_type_usage)\r
- - [Required and preferred flags](@ref choosing_memory_type_required_preferred_flags)\r
- - [Explicit memory types](@ref choosing_memory_type_explicit_memory_types)\r
- - [Custom memory pools](@ref choosing_memory_type_custom_memory_pools)\r
- - [Dedicated allocations](@ref choosing_memory_type_dedicated_allocations)\r
- - \subpage memory_mapping\r
- - [Mapping functions](@ref memory_mapping_mapping_functions)\r
- - [Persistently mapped memory](@ref memory_mapping_persistently_mapped_memory)\r
- - [Cache flush and invalidate](@ref memory_mapping_cache_control)\r
- - [Finding out if memory is mappable](@ref memory_mapping_finding_if_memory_mappable)\r
- - \subpage staying_within_budget\r
- - [Querying for budget](@ref staying_within_budget_querying_for_budget)\r
- - [Controlling memory usage](@ref staying_within_budget_controlling_memory_usage)\r
- - \subpage custom_memory_pools\r
- - [Choosing memory type index](@ref custom_memory_pools_MemTypeIndex)\r
- - [Linear allocation algorithm](@ref linear_algorithm)\r
- - [Free-at-once](@ref linear_algorithm_free_at_once)\r
- - [Stack](@ref linear_algorithm_stack)\r
- - [Double stack](@ref linear_algorithm_double_stack)\r
- - [Ring buffer](@ref linear_algorithm_ring_buffer)\r
- - [Buddy allocation algorithm](@ref buddy_algorithm)\r
- - \subpage defragmentation\r
- - [Defragmenting CPU memory](@ref defragmentation_cpu)\r
- - [Defragmenting GPU memory](@ref defragmentation_gpu)\r
- - [Additional notes](@ref defragmentation_additional_notes)\r
- - [Writing custom allocation algorithm](@ref defragmentation_custom_algorithm)\r
- - \subpage lost_allocations\r
- - \subpage statistics\r
- - [Numeric statistics](@ref statistics_numeric_statistics)\r
- - [JSON dump](@ref statistics_json_dump)\r
- - \subpage allocation_annotation\r
- - [Allocation user data](@ref allocation_user_data)\r
- - [Allocation names](@ref allocation_names)\r
- - \subpage debugging_memory_usage\r
- - [Memory initialization](@ref debugging_memory_usage_initialization)\r
- - [Margins](@ref debugging_memory_usage_margins)\r
- - [Corruption detection](@ref debugging_memory_usage_corruption_detection)\r
- - \subpage record_and_replay\r
-- \subpage usage_patterns\r
- - [Common mistakes](@ref usage_patterns_common_mistakes)\r
- - [Simple patterns](@ref usage_patterns_simple)\r
- - [Advanced patterns](@ref usage_patterns_advanced)\r
-- \subpage configuration\r
- - [Pointers to Vulkan functions](@ref config_Vulkan_functions)\r
- - [Custom host memory allocator](@ref custom_memory_allocator)\r
- - [Device memory allocation callbacks](@ref allocation_callbacks)\r
- - [Device heap memory limit](@ref heap_memory_limit)\r
- - \subpage vk_khr_dedicated_allocation\r
- - \subpage enabling_buffer_device_address\r
- - \subpage vk_amd_device_coherent_memory\r
-- \subpage general_considerations\r
- - [Thread safety](@ref general_considerations_thread_safety)\r
- - [Validation layer warnings](@ref general_considerations_validation_layer_warnings)\r
- - [Allocation algorithm](@ref general_considerations_allocation_algorithm)\r
- - [Features not supported](@ref general_considerations_features_not_supported)\r
-\r
-\section main_see_also See also\r
-\r
-- [Product page on GPUOpen](https://gpuopen.com/gaming-product/vulkan-memory-allocator/)\r
-- [Source repository on GitHub](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator)\r
-\r
-\r
-\r
-\r
-\page quick_start Quick start\r
-\r
-\section quick_start_project_setup Project setup\r
-\r
-Vulkan Memory Allocator comes in form of a "stb-style" single header file.\r
-You don't need to build it as a separate library project.\r
-You can add this file directly to your project and submit it to code repository next to your other source files.\r
-\r
-"Single header" doesn't mean that everything is contained in C/C++ declarations,\r
-like it tends to be in case of inline functions or C++ templates.\r
-It means that implementation is bundled with interface in a single file and needs to be extracted using preprocessor macro.\r
-If you don't do it properly, you will get linker errors.\r
-\r
-To do it properly:\r
-\r
--# Include "vk_mem_alloc.h" file in each CPP file where you want to use the library.\r
- This includes declarations of all members of the library.\r
--# In exacly one CPP file define following macro before this include.\r
- It enables also internal definitions.\r
-\r
-\code\r
-#define VMA_IMPLEMENTATION\r
-#include "vk_mem_alloc.h"\r
-\endcode\r
-\r
-It may be a good idea to create dedicated CPP file just for this purpose.\r
-\r
-Note on language: This library is written in C++, but has C-compatible interface.\r
-Thus you can include and use vk_mem_alloc.h in C or C++ code, but full\r
-implementation with `VMA_IMPLEMENTATION` macro must be compiled as C++, NOT as C.\r
-\r
-Please note that this library includes header `<vulkan/vulkan.h>`, which in turn\r
-includes `<windows.h>` on Windows. If you need some specific macros defined\r
-before including these headers (like `WIN32_LEAN_AND_MEAN` or\r
-`WINVER` for Windows, `VK_USE_PLATFORM_WIN32_KHR` for Vulkan), you must define\r
-them before every `#include` of this library.\r
-\r
-\r
-\section quick_start_initialization Initialization\r
-\r
-At program startup:\r
-\r
--# Initialize Vulkan to have `VkPhysicalDevice`, `VkDevice` and `VkInstance` object.\r
--# Fill VmaAllocatorCreateInfo structure and create #VmaAllocator object by\r
- calling vmaCreateAllocator().\r
-\r
-\code\r
-VmaAllocatorCreateInfo allocatorInfo = {};\r
-allocatorInfo.physicalDevice = physicalDevice;\r
-allocatorInfo.device = device;\r
-allocatorInfo.instance = instance;\r
-\r
-VmaAllocator allocator;\r
-vmaCreateAllocator(&allocatorInfo, &allocator);\r
-\endcode\r
-\r
-\section quick_start_resource_allocation Resource allocation\r
-\r
-When you want to create a buffer or image:\r
-\r
--# Fill `VkBufferCreateInfo` / `VkImageCreateInfo` structure.\r
--# Fill VmaAllocationCreateInfo structure.\r
--# Call vmaCreateBuffer() / vmaCreateImage() to get `VkBuffer`/`VkImage` with memory\r
- already allocated and bound to it.\r
-\r
-\code\r
-VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-bufferInfo.size = 65536;\r
-bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
-\r
-VmaAllocationCreateInfo allocInfo = {};\r
-allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
-\r
-VkBuffer buffer;\r
-VmaAllocation allocation;\r
-vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);\r
-\endcode\r
-\r
-Don't forget to destroy your objects when no longer needed:\r
-\r
-\code\r
-vmaDestroyBuffer(allocator, buffer, allocation);\r
-vmaDestroyAllocator(allocator);\r
-\endcode\r
-\r
-\r
-\page choosing_memory_type Choosing memory type\r
-\r
-Physical devices in Vulkan support various combinations of memory heaps and\r
-types. Help with choosing correct and optimal memory type for your specific\r
-resource is one of the key features of this library. You can use it by filling\r
-appropriate members of VmaAllocationCreateInfo structure, as described below.\r
-You can also combine multiple methods.\r
-\r
--# If you just want to find memory type index that meets your requirements, you\r
- can use function: vmaFindMemoryTypeIndex(), vmaFindMemoryTypeIndexForBufferInfo(),\r
- vmaFindMemoryTypeIndexForImageInfo().\r
--# If you want to allocate a region of device memory without association with any\r
- specific image or buffer, you can use function vmaAllocateMemory(). Usage of\r
- this function is not recommended and usually not needed.\r
- vmaAllocateMemoryPages() function is also provided for creating multiple allocations at once,\r
- which may be useful for sparse binding.\r
--# If you already have a buffer or an image created, you want to allocate memory\r
- for it and then you will bind it yourself, you can use function\r
- vmaAllocateMemoryForBuffer(), vmaAllocateMemoryForImage().\r
- For binding you should use functions: vmaBindBufferMemory(), vmaBindImageMemory()\r
- or their extended versions: vmaBindBufferMemory2(), vmaBindImageMemory2().\r
--# If you want to create a buffer or an image, allocate memory for it and bind\r
- them together, all in one call, you can use function vmaCreateBuffer(),\r
- vmaCreateImage(). This is the easiest and recommended way to use this library.\r
-\r
-When using 3. or 4., the library internally queries Vulkan for memory types\r
-supported for that buffer or image (function `vkGetBufferMemoryRequirements()`)\r
-and uses only one of these types.\r
-\r
-If no memory type can be found that meets all the requirements, these functions\r
-return `VK_ERROR_FEATURE_NOT_PRESENT`.\r
-\r
-You can leave VmaAllocationCreateInfo structure completely filled with zeros.\r
-It means no requirements are specified for memory type.\r
-It is valid, although not very useful.\r
-\r
-\section choosing_memory_type_usage Usage\r
-\r
-The easiest way to specify memory requirements is to fill member\r
-VmaAllocationCreateInfo::usage using one of the values of enum #VmaMemoryUsage.\r
-It defines high level, common usage types.\r
-For more details, see description of this enum.\r
-\r
-For example, if you want to create a uniform buffer that will be filled using\r
-transfer only once or infrequently and used for rendering every frame, you can\r
-do it using following code:\r
-\r
-\code\r
-VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-bufferInfo.size = 65536;\r
-bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
-\r
-VmaAllocationCreateInfo allocInfo = {};\r
-allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
-\r
-VkBuffer buffer;\r
-VmaAllocation allocation;\r
-vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);\r
-\endcode\r
-\r
-\section choosing_memory_type_required_preferred_flags Required and preferred flags\r
-\r
-You can specify more detailed requirements by filling members\r
-VmaAllocationCreateInfo::requiredFlags and VmaAllocationCreateInfo::preferredFlags\r
-with a combination of bits from enum `VkMemoryPropertyFlags`. For example,\r
-if you want to create a buffer that will be persistently mapped on host (so it\r
-must be `HOST_VISIBLE`) and preferably will also be `HOST_COHERENT` and `HOST_CACHED`,\r
-use following code:\r
-\r
-\code\r
-VmaAllocationCreateInfo allocInfo = {};\r
-allocInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;\r
-allocInfo.preferredFlags = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT;\r
-allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;\r
-\r
-VkBuffer buffer;\r
-VmaAllocation allocation;\r
-vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);\r
-\endcode\r
-\r
-A memory type is chosen that has all the required flags and as many preferred\r
-flags set as possible.\r
-\r
-If you use VmaAllocationCreateInfo::usage, it is just internally converted to\r
-a set of required and preferred flags.\r
-\r
-\section choosing_memory_type_explicit_memory_types Explicit memory types\r
-\r
-If you inspected memory types available on the physical device and you have\r
-a preference for memory types that you want to use, you can fill member\r
-VmaAllocationCreateInfo::memoryTypeBits. It is a bit mask, where each bit set\r
-means that a memory type with that index is allowed to be used for the\r
-allocation. Special value 0, just like `UINT32_MAX`, means there are no\r
-restrictions to memory type index.\r
-\r
-Please note that this member is NOT just a memory type index.\r
-Still you can use it to choose just one, specific memory type.\r
-For example, if you already determined that your buffer should be created in\r
-memory type 2, use following code:\r
-\r
-\code\r
-uint32_t memoryTypeIndex = 2;\r
-\r
-VmaAllocationCreateInfo allocInfo = {};\r
-allocInfo.memoryTypeBits = 1u << memoryTypeIndex;\r
-\r
-VkBuffer buffer;\r
-VmaAllocation allocation;\r
-vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);\r
-\endcode\r
-\r
-\section choosing_memory_type_custom_memory_pools Custom memory pools\r
-\r
-If you allocate from custom memory pool, all the ways of specifying memory\r
-requirements described above are not applicable and the aforementioned members\r
-of VmaAllocationCreateInfo structure are ignored. Memory type is selected\r
-explicitly when creating the pool and then used to make all the allocations from\r
-that pool. For further details, see \ref custom_memory_pools.\r
-\r
-\section choosing_memory_type_dedicated_allocations Dedicated allocations\r
-\r
-Memory for allocations is reserved out of larger block of `VkDeviceMemory`\r
-allocated from Vulkan internally. That's the main feature of this whole library.\r
-You can still request a separate memory block to be created for an allocation,\r
-just like you would do in a trivial solution without using any allocator.\r
-In that case, a buffer or image is always bound to that memory at offset 0.\r
-This is called a "dedicated allocation".\r
-You can explicitly request it by using flag #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.\r
-The library can also internally decide to use dedicated allocation in some cases, e.g.:\r
-\r
-- When the size of the allocation is large.\r
-- When [VK_KHR_dedicated_allocation](@ref vk_khr_dedicated_allocation) extension is enabled\r
- and it reports that dedicated allocation is required or recommended for the resource.\r
-- When allocation of next big memory block fails due to not enough device memory,\r
- but allocation with the exact requested size succeeds.\r
-\r
-\r
-\page memory_mapping Memory mapping\r
-\r
-To "map memory" in Vulkan means to obtain a CPU pointer to `VkDeviceMemory`,\r
-to be able to read from it or write to it in CPU code.\r
-Mapping is possible only of memory allocated from a memory type that has\r
-`VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT` flag.\r
-Functions `vkMapMemory()`, `vkUnmapMemory()` are designed for this purpose.\r
-You can use them directly with memory allocated by this library,\r
-but it is not recommended because of following issue:\r
-Mapping the same `VkDeviceMemory` block multiple times is illegal - only one mapping at a time is allowed.\r
-This includes mapping disjoint regions. Mapping is not reference-counted internally by Vulkan.\r
-Because of this, Vulkan Memory Allocator provides following facilities:\r
-\r
-\section memory_mapping_mapping_functions Mapping functions\r
-\r
-The library provides following functions for mapping of a specific #VmaAllocation: vmaMapMemory(), vmaUnmapMemory().\r
-They are safer and more convenient to use than standard Vulkan functions.\r
-You can map an allocation multiple times simultaneously - mapping is reference-counted internally.\r
-You can also map different allocations simultaneously regardless of whether they use the same `VkDeviceMemory` block.\r
-The way it's implemented is that the library always maps entire memory block, not just region of the allocation.\r
-For further details, see description of vmaMapMemory() function.\r
-Example:\r
-\r
-\code\r
-// Having these objects initialized:\r
-\r
-struct ConstantBuffer\r
-{\r
- ...\r
-};\r
-ConstantBuffer constantBufferData;\r
-\r
-VmaAllocator allocator;\r
-VkBuffer constantBuffer;\r
-VmaAllocation constantBufferAllocation;\r
-\r
-// You can map and fill your buffer using following code:\r
-\r
-void* mappedData;\r
-vmaMapMemory(allocator, constantBufferAllocation, &mappedData);\r
-memcpy(mappedData, &constantBufferData, sizeof(constantBufferData));\r
-vmaUnmapMemory(allocator, constantBufferAllocation);\r
-\endcode\r
-\r
-When mapping, you may see a warning from Vulkan validation layer similar to this one:\r
-\r
-<i>Mapping an image with layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL can result in undefined behavior if this memory is used by the device. Only GENERAL or PREINITIALIZED should be used.</i>\r
-\r
-It happens because the library maps entire `VkDeviceMemory` block, where different\r
-types of images and buffers may end up together, especially on GPUs with unified memory like Intel.\r
-You can safely ignore it if you are sure you access only memory of the intended\r
-object that you wanted to map.\r
-\r
-\r
-\section memory_mapping_persistently_mapped_memory Persistently mapped memory\r
-\r
-Kepping your memory persistently mapped is generally OK in Vulkan.\r
-You don't need to unmap it before using its data on the GPU.\r
-The library provides a special feature designed for that:\r
-Allocations made with #VMA_ALLOCATION_CREATE_MAPPED_BIT flag set in\r
-VmaAllocationCreateInfo::flags stay mapped all the time,\r
-so you can just access CPU pointer to it any time\r
-without a need to call any "map" or "unmap" function.\r
-Example:\r
-\r
-\code\r
-VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-bufCreateInfo.size = sizeof(ConstantBuffer);\r
-bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;\r
-allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;\r
-\r
-VkBuffer buf;\r
-VmaAllocation alloc;\r
-VmaAllocationInfo allocInfo;\r
-vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);\r
-\r
-// Buffer is already mapped. You can access its memory.\r
-memcpy(allocInfo.pMappedData, &constantBufferData, sizeof(constantBufferData));\r
-\endcode\r
-\r
-There are some exceptions though, when you should consider mapping memory only for a short period of time:\r
-\r
-- When operating system is Windows 7 or 8.x (Windows 10 is not affected because it uses WDDM2),\r
- device is discrete AMD GPU,\r
- and memory type is the special 256 MiB pool of `DEVICE_LOCAL + HOST_VISIBLE` memory\r
- (selected when you use #VMA_MEMORY_USAGE_CPU_TO_GPU),\r
- then whenever a memory block allocated from this memory type stays mapped\r
- for the time of any call to `vkQueueSubmit()` or `vkQueuePresentKHR()`, this\r
- block is migrated by WDDM to system RAM, which degrades performance. It doesn't\r
- matter if that particular memory block is actually used by the command buffer\r
- being submitted.\r
-- On Mac/MoltenVK there is a known bug - [Issue #175](https://github.com/KhronosGroup/MoltenVK/issues/175)\r
- which requires unmapping before GPU can see updated texture.\r
-- Keeping many large memory blocks mapped may impact performance or stability of some debugging tools.\r
-\r
-\section memory_mapping_cache_control Cache flush and invalidate\r
- \r
-Memory in Vulkan doesn't need to be unmapped before using it on GPU,\r
-but unless a memory types has `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT` flag set,\r
-you need to manually **invalidate** cache before reading of mapped pointer\r
-and **flush** cache after writing to mapped pointer.\r
-Map/unmap operations don't do that automatically.\r
-Vulkan provides following functions for this purpose `vkFlushMappedMemoryRanges()`,\r
-`vkInvalidateMappedMemoryRanges()`, but this library provides more convenient\r
-functions that refer to given allocation object: vmaFlushAllocation(),\r
-vmaInvalidateAllocation(),\r
-or multiple objects at once: vmaFlushAllocations(), vmaInvalidateAllocations().\r
-\r
-Regions of memory specified for flush/invalidate must be aligned to\r
-`VkPhysicalDeviceLimits::nonCoherentAtomSize`. This is automatically ensured by the library.\r
-In any memory type that is `HOST_VISIBLE` but not `HOST_COHERENT`, all allocations\r
-within blocks are aligned to this value, so their offsets are always multiply of\r
-`nonCoherentAtomSize` and two different allocations never share same "line" of this size.\r
-\r
-Please note that memory allocated with #VMA_MEMORY_USAGE_CPU_ONLY is guaranteed to be `HOST_COHERENT`.\r
-\r
-Also, Windows drivers from all 3 **PC** GPU vendors (AMD, Intel, NVIDIA)\r
-currently provide `HOST_COHERENT` flag on all memory types that are\r
-`HOST_VISIBLE`, so on this platform you may not need to bother.\r
-\r
-\section memory_mapping_finding_if_memory_mappable Finding out if memory is mappable\r
-\r
-It may happen that your allocation ends up in memory that is `HOST_VISIBLE` (available for mapping)\r
-despite it wasn't explicitly requested.\r
-For example, application may work on integrated graphics with unified memory (like Intel) or\r
-allocation from video memory might have failed, so the library chose system memory as fallback.\r
-\r
-You can detect this case and map such allocation to access its memory on CPU directly,\r
-instead of launching a transfer operation.\r
-In order to do that: inspect `allocInfo.memoryType`, call vmaGetMemoryTypeProperties(),\r
-and look for `VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT` flag in properties of that memory type.\r
-\r
-\code\r
-VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-bufCreateInfo.size = sizeof(ConstantBuffer);\r
-bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
-allocCreateInfo.preferredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;\r
-\r
-VkBuffer buf;\r
-VmaAllocation alloc;\r
-VmaAllocationInfo allocInfo;\r
-vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);\r
-\r
-VkMemoryPropertyFlags memFlags;\r
-vmaGetMemoryTypeProperties(allocator, allocInfo.memoryType, &memFlags);\r
-if((memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)\r
-{\r
- // Allocation ended up in mappable memory. You can map it and access it directly.\r
- void* mappedData;\r
- vmaMapMemory(allocator, alloc, &mappedData);\r
- memcpy(mappedData, &constantBufferData, sizeof(constantBufferData));\r
- vmaUnmapMemory(allocator, alloc);\r
-}\r
-else\r
-{\r
- // Allocation ended up in non-mappable memory.\r
- // You need to create CPU-side buffer in VMA_MEMORY_USAGE_CPU_ONLY and make a transfer.\r
-}\r
-\endcode\r
-\r
-You can even use #VMA_ALLOCATION_CREATE_MAPPED_BIT flag while creating allocations\r
-that are not necessarily `HOST_VISIBLE` (e.g. using #VMA_MEMORY_USAGE_GPU_ONLY).\r
-If the allocation ends up in memory type that is `HOST_VISIBLE`, it will be persistently mapped and you can use it directly.\r
-If not, the flag is just ignored.\r
-Example:\r
-\r
-\code\r
-VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-bufCreateInfo.size = sizeof(ConstantBuffer);\r
-bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
-allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;\r
-\r
-VkBuffer buf;\r
-VmaAllocation alloc;\r
-VmaAllocationInfo allocInfo;\r
-vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);\r
-\r
-if(allocInfo.pUserData != nullptr)\r
-{\r
- // Allocation ended up in mappable memory.\r
- // It's persistently mapped. You can access it directly.\r
- memcpy(allocInfo.pMappedData, &constantBufferData, sizeof(constantBufferData));\r
-}\r
-else\r
-{\r
- // Allocation ended up in non-mappable memory.\r
- // You need to create CPU-side buffer in VMA_MEMORY_USAGE_CPU_ONLY and make a transfer.\r
-}\r
-\endcode\r
-\r
-\r
-\page staying_within_budget Staying within budget\r
-\r
-When developing a graphics-intensive game or program, it is important to avoid allocating\r
-more GPU memory than it's physically available. When the memory is over-committed,\r
-various bad things can happen, depending on the specific GPU, graphics driver, and\r
-operating system:\r
-\r
-- It may just work without any problems.\r
-- The application may slow down because some memory blocks are moved to system RAM\r
- and the GPU has to access them through PCI Express bus.\r
-- A new allocation may take very long time to complete, even few seconds, and possibly\r
- freeze entire system.\r
-- The new allocation may fail with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.\r
-- It may even result in GPU crash (TDR), observed as `VK_ERROR_DEVICE_LOST`\r
- returned somewhere later.\r
-\r
-\section staying_within_budget_querying_for_budget Querying for budget\r
-\r
-To query for current memory usage and available budget, use function vmaGetBudget().\r
-Returned structure #VmaBudget contains quantities expressed in bytes, per Vulkan memory heap.\r
-\r
-Please note that this function returns different information and works faster than\r
-vmaCalculateStats(). vmaGetBudget() can be called every frame or even before every\r
-allocation, while vmaCalculateStats() is intended to be used rarely,\r
-only to obtain statistical information, e.g. for debugging purposes.\r
-\r
-It is recommended to use <b>VK_EXT_memory_budget</b> device extension to obtain information\r
-about the budget from Vulkan device. VMA is able to use this extension automatically.\r
-When not enabled, the allocator behaves same way, but then it estimates current usage\r
-and available budget based on its internal information and Vulkan memory heap sizes,\r
-which may be less precise. In order to use this extension:\r
-\r
-1. Make sure extensions VK_EXT_memory_budget and VK_KHR_get_physical_device_properties2\r
- required by it are available and enable them. Please note that the first is a device\r
- extension and the second is instance extension!\r
-2. Use flag #VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT when creating #VmaAllocator object.\r
-3. Make sure to call vmaSetCurrentFrameIndex() every frame. Budget is queried from\r
- Vulkan inside of it to avoid overhead of querying it with every allocation.\r
-\r
-\section staying_within_budget_controlling_memory_usage Controlling memory usage\r
-\r
-There are many ways in which you can try to stay within the budget.\r
-\r
-First, when making new allocation requires allocating a new memory block, the library\r
-tries not to exceed the budget automatically. If a block with default recommended size\r
-(e.g. 256 MB) would go over budget, a smaller block is allocated, possibly even\r
-dedicated memory for just this resource.\r
-\r
-If the size of the requested resource plus current memory usage is more than the\r
-budget, by default the library still tries to create it, leaving it to the Vulkan\r
-implementation whether the allocation succeeds or fails. You can change this behavior\r
-by using #VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT flag. With it, the allocation is\r
-not made if it would exceed the budget or if the budget is already exceeded.\r
-Some other allocations become lost instead to make room for it, if the mechanism of\r
-[lost allocations](@ref lost_allocations) is used.\r
-If that is not possible, the allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.\r
-Example usage pattern may be to pass the #VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT flag\r
-when creating resources that are not essential for the application (e.g. the texture\r
-of a specific object) and not to pass it when creating critically important resources\r
-(e.g. render targets).\r
-\r
-Finally, you can also use #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT flag to make sure\r
-a new allocation is created only when it fits inside one of the existing memory blocks.\r
-If it would require to allocate a new block, if fails instead with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.\r
-This also ensures that the function call is very fast because it never goes to Vulkan\r
-to obtain a new block.\r
-\r
-Please note that creating \ref custom_memory_pools with VmaPoolCreateInfo::minBlockCount\r
-set to more than 0 will try to allocate memory blocks without checking whether they\r
-fit within budget.\r
-\r
-\r
-\page custom_memory_pools Custom memory pools\r
-\r
-A memory pool contains a number of `VkDeviceMemory` blocks.\r
-The library automatically creates and manages default pool for each memory type available on the device.\r
-Default memory pool automatically grows in size.\r
-Size of allocated blocks is also variable and managed automatically.\r
-\r
-You can create custom pool and allocate memory out of it.\r
-It can be useful if you want to:\r
-\r
-- Keep certain kind of allocations separate from others.\r
-- Enforce particular, fixed size of Vulkan memory blocks.\r
-- Limit maximum amount of Vulkan memory allocated for that pool.\r
-- Reserve minimum or fixed amount of Vulkan memory always preallocated for that pool.\r
-\r
-To use custom memory pools:\r
-\r
--# Fill VmaPoolCreateInfo structure.\r
--# Call vmaCreatePool() to obtain #VmaPool handle.\r
--# When making an allocation, set VmaAllocationCreateInfo::pool to this handle.\r
- You don't need to specify any other parameters of this structure, like `usage`.\r
-\r
-Example:\r
-\r
-\code\r
-// Create a pool that can have at most 2 blocks, 128 MiB each.\r
-VmaPoolCreateInfo poolCreateInfo = {};\r
-poolCreateInfo.memoryTypeIndex = ...\r
-poolCreateInfo.blockSize = 128ull * 1024 * 1024;\r
-poolCreateInfo.maxBlockCount = 2;\r
-\r
-VmaPool pool;\r
-vmaCreatePool(allocator, &poolCreateInfo, &pool);\r
-\r
-// Allocate a buffer out of it.\r
-VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-bufCreateInfo.size = 1024;\r
-bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.pool = pool;\r
-\r
-VkBuffer buf;\r
-VmaAllocation alloc;\r
-VmaAllocationInfo allocInfo;\r
-vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);\r
-\endcode\r
-\r
-You have to free all allocations made from this pool before destroying it.\r
-\r
-\code\r
-vmaDestroyBuffer(allocator, buf, alloc);\r
-vmaDestroyPool(allocator, pool);\r
-\endcode\r
-\r
-\section custom_memory_pools_MemTypeIndex Choosing memory type index\r
-\r
-When creating a pool, you must explicitly specify memory type index.\r
-To find the one suitable for your buffers or images, you can use helper functions\r
-vmaFindMemoryTypeIndexForBufferInfo(), vmaFindMemoryTypeIndexForImageInfo().\r
-You need to provide structures with example parameters of buffers or images\r
-that you are going to create in that pool.\r
-\r
-\code\r
-VkBufferCreateInfo exampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-exampleBufCreateInfo.size = 1024; // Whatever.\r
-exampleBufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; // Change if needed.\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; // Change if needed.\r
-\r
-uint32_t memTypeIndex;\r
-vmaFindMemoryTypeIndexForBufferInfo(allocator, &exampleBufCreateInfo, &allocCreateInfo, &memTypeIndex);\r
-\r
-VmaPoolCreateInfo poolCreateInfo = {};\r
-poolCreateInfo.memoryTypeIndex = memTypeIndex;\r
-// ...\r
-\endcode\r
-\r
-When creating buffers/images allocated in that pool, provide following parameters:\r
-\r
-- `VkBufferCreateInfo`: Prefer to pass same parameters as above.\r
- Otherwise you risk creating resources in a memory type that is not suitable for them, which may result in undefined behavior.\r
- Using different `VK_BUFFER_USAGE_` flags may work, but you shouldn't create images in a pool intended for buffers\r
- or the other way around.\r
-- VmaAllocationCreateInfo: You don't need to pass same parameters. Fill only `pool` member.\r
- Other members are ignored anyway.\r
-\r
-\section linear_algorithm Linear allocation algorithm\r
-\r
-Each Vulkan memory block managed by this library has accompanying metadata that\r
-keeps track of used and unused regions. By default, the metadata structure and\r
-algorithm tries to find best place for new allocations among free regions to\r
-optimize memory usage. This way you can allocate and free objects in any order.\r
-\r
-\r
-\r
-Sometimes there is a need to use simpler, linear allocation algorithm. You can\r
-create custom pool that uses such algorithm by adding flag\r
-#VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT to VmaPoolCreateInfo::flags while creating\r
-#VmaPool object. Then an alternative metadata management is used. It always\r
-creates new allocations after last one and doesn't reuse free regions after\r
-allocations freed in the middle. It results in better allocation performance and\r
-less memory consumed by metadata.\r
-\r
-\r
-\r
-With this one flag, you can create a custom pool that can be used in many ways:\r
-free-at-once, stack, double stack, and ring buffer. See below for details.\r
-\r
-\subsection linear_algorithm_free_at_once Free-at-once\r
-\r
-In a pool that uses linear algorithm, you still need to free all the allocations\r
-individually, e.g. by using vmaFreeMemory() or vmaDestroyBuffer(). You can free\r
-them in any order. New allocations are always made after last one - free space\r
-in the middle is not reused. However, when you release all the allocation and\r
-the pool becomes empty, allocation starts from the beginning again. This way you\r
-can use linear algorithm to speed up creation of allocations that you are going\r
-to release all at once.\r
-\r
-\r
-\r
-This mode is also available for pools created with VmaPoolCreateInfo::maxBlockCount\r
-value that allows multiple memory blocks.\r
-\r
-\subsection linear_algorithm_stack Stack\r
-\r
-When you free an allocation that was created last, its space can be reused.\r
-Thanks to this, if you always release allocations in the order opposite to their\r
-creation (LIFO - Last In First Out), you can achieve behavior of a stack.\r
-\r
-\r
-\r
-This mode is also available for pools created with VmaPoolCreateInfo::maxBlockCount\r
-value that allows multiple memory blocks.\r
-\r
-\subsection linear_algorithm_double_stack Double stack\r
-\r
-The space reserved by a custom pool with linear algorithm may be used by two\r
-stacks:\r
-\r
-- First, default one, growing up from offset 0.\r
-- Second, "upper" one, growing down from the end towards lower offsets.\r
-\r
-To make allocation from upper stack, add flag #VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT\r
-to VmaAllocationCreateInfo::flags.\r
-\r
-\r
-\r
-Double stack is available only in pools with one memory block -\r
-VmaPoolCreateInfo::maxBlockCount must be 1. Otherwise behavior is undefined.\r
-\r
-When the two stacks' ends meet so there is not enough space between them for a\r
-new allocation, such allocation fails with usual\r
-`VK_ERROR_OUT_OF_DEVICE_MEMORY` error.\r
-\r
-\subsection linear_algorithm_ring_buffer Ring buffer\r
-\r
-When you free some allocations from the beginning and there is not enough free space\r
-for a new one at the end of a pool, allocator's "cursor" wraps around to the\r
-beginning and starts allocation there. Thanks to this, if you always release\r
-allocations in the same order as you created them (FIFO - First In First Out),\r
-you can achieve behavior of a ring buffer / queue.\r
-\r
-\r
-\r
-Pools with linear algorithm support [lost allocations](@ref lost_allocations) when used as ring buffer.\r
-If there is not enough free space for a new allocation, but existing allocations\r
-from the front of the queue can become lost, they become lost and the allocation\r
-succeeds.\r
-\r
-\r
-\r
-Ring buffer is available only in pools with one memory block -\r
-VmaPoolCreateInfo::maxBlockCount must be 1. Otherwise behavior is undefined.\r
-\r
-\section buddy_algorithm Buddy allocation algorithm\r
-\r
-There is another allocation algorithm that can be used with custom pools, called\r
-"buddy". Its internal data structure is based on a tree of blocks, each having\r
-size that is a power of two and a half of its parent's size. When you want to\r
-allocate memory of certain size, a free node in the tree is located. If it's too\r
-large, it is recursively split into two halves (called "buddies"). However, if\r
-requested allocation size is not a power of two, the size of a tree node is\r
-aligned up to the nearest power of two and the remaining space is wasted. When\r
-two buddy nodes become free, they are merged back into one larger node.\r
-\r
-\r
-\r
-The advantage of buddy allocation algorithm over default algorithm is faster\r
-allocation and deallocation, as well as smaller external fragmentation. The\r
-disadvantage is more wasted space (internal fragmentation).\r
-\r
-For more information, please read ["Buddy memory allocation" on Wikipedia](https://en.wikipedia.org/wiki/Buddy_memory_allocation)\r
-or other sources that describe this concept in general.\r
-\r
-To use buddy allocation algorithm with a custom pool, add flag\r
-#VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT to VmaPoolCreateInfo::flags while creating\r
-#VmaPool object.\r
-\r
-Several limitations apply to pools that use buddy algorithm:\r
-\r
-- It is recommended to use VmaPoolCreateInfo::blockSize that is a power of two.\r
- Otherwise, only largest power of two smaller than the size is used for\r
- allocations. The remaining space always stays unused.\r
-- [Margins](@ref debugging_memory_usage_margins) and\r
- [corruption detection](@ref debugging_memory_usage_corruption_detection)\r
- don't work in such pools.\r
-- [Lost allocations](@ref lost_allocations) don't work in such pools. You can\r
- use them, but they never become lost. Support may be added in the future.\r
-- [Defragmentation](@ref defragmentation) doesn't work with allocations made from\r
- such pool.\r
-\r
-\page defragmentation Defragmentation\r
-\r
-Interleaved allocations and deallocations of many objects of varying size can\r
-cause fragmentation over time, which can lead to a situation where the library is unable\r
-to find a continuous range of free memory for a new allocation despite there is\r
-enough free space, just scattered across many small free ranges between existing\r
-allocations.\r
-\r
-To mitigate this problem, you can use defragmentation feature:\r
-structure #VmaDefragmentationInfo2, function vmaDefragmentationBegin(), vmaDefragmentationEnd().\r
-Given set of allocations, \r
-this function can move them to compact used memory, ensure more continuous free\r
-space and possibly also free some `VkDeviceMemory` blocks.\r
-\r
-What the defragmentation does is:\r
-\r
-- Updates #VmaAllocation objects to point to new `VkDeviceMemory` and offset.\r
- After allocation has been moved, its VmaAllocationInfo::deviceMemory and/or\r
- VmaAllocationInfo::offset changes. You must query them again using\r
- vmaGetAllocationInfo() if you need them.\r
-- Moves actual data in memory.\r
-\r
-What it doesn't do, so you need to do it yourself:\r
-\r
-- Recreate buffers and images that were bound to allocations that were defragmented and\r
- bind them with their new places in memory.\r
- You must use `vkDestroyBuffer()`, `vkDestroyImage()`,\r
- `vkCreateBuffer()`, `vkCreateImage()`, vmaBindBufferMemory(), vmaBindImageMemory()\r
- for that purpose and NOT vmaDestroyBuffer(),\r
- vmaDestroyImage(), vmaCreateBuffer(), vmaCreateImage(), because you don't need to\r
- destroy or create allocation objects!\r
-- Recreate views and update descriptors that point to these buffers and images.\r
-\r
-\section defragmentation_cpu Defragmenting CPU memory\r
-\r
-Following example demonstrates how you can run defragmentation on CPU.\r
-Only allocations created in memory types that are `HOST_VISIBLE` can be defragmented.\r
-Others are ignored.\r
-\r
-The way it works is:\r
-\r
-- It temporarily maps entire memory blocks when necessary.\r
-- It moves data using `memmove()` function.\r
-\r
-\code\r
-// Given following variables already initialized:\r
-VkDevice device;\r
-VmaAllocator allocator;\r
-std::vector<VkBuffer> buffers;\r
-std::vector<VmaAllocation> allocations;\r
-\r
-\r
-const uint32_t allocCount = (uint32_t)allocations.size();\r
-std::vector<VkBool32> allocationsChanged(allocCount);\r
-\r
-VmaDefragmentationInfo2 defragInfo = {};\r
-defragInfo.allocationCount = allocCount;\r
-defragInfo.pAllocations = allocations.data();\r
-defragInfo.pAllocationsChanged = allocationsChanged.data();\r
-defragInfo.maxCpuBytesToMove = VK_WHOLE_SIZE; // No limit.\r
-defragInfo.maxCpuAllocationsToMove = UINT32_MAX; // No limit.\r
-\r
-VmaDefragmentationContext defragCtx;\r
-vmaDefragmentationBegin(allocator, &defragInfo, nullptr, &defragCtx);\r
-vmaDefragmentationEnd(allocator, defragCtx);\r
-\r
-for(uint32_t i = 0; i < allocCount; ++i)\r
-{\r
- if(allocationsChanged[i])\r
- {\r
- // Destroy buffer that is immutably bound to memory region which is no longer valid.\r
- vkDestroyBuffer(device, buffers[i], nullptr);\r
-\r
- // Create new buffer with same parameters.\r
- VkBufferCreateInfo bufferInfo = ...;\r
- vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);\r
- \r
- // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.\r
- \r
- // Bind new buffer to new memory region. Data contained in it is already moved.\r
- VmaAllocationInfo allocInfo;\r
- vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);\r
- vmaBindBufferMemory(allocator, allocations[i], buffers[i]);\r
- }\r
-}\r
-\endcode\r
-\r
-Setting VmaDefragmentationInfo2::pAllocationsChanged is optional.\r
-This output array tells whether particular allocation in VmaDefragmentationInfo2::pAllocations at the same index\r
-has been modified during defragmentation.\r
-You can pass null, but you then need to query every allocation passed to defragmentation\r
-for new parameters using vmaGetAllocationInfo() if you might need to recreate and rebind a buffer or image associated with it.\r
-\r
-If you use [Custom memory pools](@ref choosing_memory_type_custom_memory_pools),\r
-you can fill VmaDefragmentationInfo2::poolCount and VmaDefragmentationInfo2::pPools\r
-instead of VmaDefragmentationInfo2::allocationCount and VmaDefragmentationInfo2::pAllocations\r
-to defragment all allocations in given pools.\r
-You cannot use VmaDefragmentationInfo2::pAllocationsChanged in that case.\r
-You can also combine both methods.\r
-\r
-\section defragmentation_gpu Defragmenting GPU memory\r
-\r
-It is also possible to defragment allocations created in memory types that are not `HOST_VISIBLE`.\r
-To do that, you need to pass a command buffer that meets requirements as described in\r
-VmaDefragmentationInfo2::commandBuffer. The way it works is:\r
-\r
-- It creates temporary buffers and binds them to entire memory blocks when necessary.\r
-- It issues `vkCmdCopyBuffer()` to passed command buffer.\r
-\r
-Example:\r
-\r
-\code\r
-// Given following variables already initialized:\r
-VkDevice device;\r
-VmaAllocator allocator;\r
-VkCommandBuffer commandBuffer;\r
-std::vector<VkBuffer> buffers;\r
-std::vector<VmaAllocation> allocations;\r
-\r
-\r
-const uint32_t allocCount = (uint32_t)allocations.size();\r
-std::vector<VkBool32> allocationsChanged(allocCount);\r
-\r
-VkCommandBufferBeginInfo cmdBufBeginInfo = ...;\r
-vkBeginCommandBuffer(commandBuffer, &cmdBufBeginInfo);\r
-\r
-VmaDefragmentationInfo2 defragInfo = {};\r
-defragInfo.allocationCount = allocCount;\r
-defragInfo.pAllocations = allocations.data();\r
-defragInfo.pAllocationsChanged = allocationsChanged.data();\r
-defragInfo.maxGpuBytesToMove = VK_WHOLE_SIZE; // Notice it's "GPU" this time.\r
-defragInfo.maxGpuAllocationsToMove = UINT32_MAX; // Notice it's "GPU" this time.\r
-defragInfo.commandBuffer = commandBuffer;\r
-\r
-VmaDefragmentationContext defragCtx;\r
-vmaDefragmentationBegin(allocator, &defragInfo, nullptr, &defragCtx);\r
-\r
-vkEndCommandBuffer(commandBuffer);\r
-\r
-// Submit commandBuffer.\r
-// Wait for a fence that ensures commandBuffer execution finished.\r
-\r
-vmaDefragmentationEnd(allocator, defragCtx);\r
-\r
-for(uint32_t i = 0; i < allocCount; ++i)\r
-{\r
- if(allocationsChanged[i])\r
- {\r
- // Destroy buffer that is immutably bound to memory region which is no longer valid.\r
- vkDestroyBuffer(device, buffers[i], nullptr);\r
-\r
- // Create new buffer with same parameters.\r
- VkBufferCreateInfo bufferInfo = ...;\r
- vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);\r
- \r
- // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.\r
- \r
- // Bind new buffer to new memory region. Data contained in it is already moved.\r
- VmaAllocationInfo allocInfo;\r
- vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);\r
- vmaBindBufferMemory(allocator, allocations[i], buffers[i]);\r
- }\r
-}\r
-\endcode\r
-\r
-You can combine these two methods by specifying non-zero `maxGpu*` as well as `maxCpu*` parameters.\r
-The library automatically chooses best method to defragment each memory pool.\r
-\r
-You may try not to block your entire program to wait until defragmentation finishes,\r
-but do it in the background, as long as you carefully fullfill requirements described\r
-in function vmaDefragmentationBegin().\r
-\r
-\section defragmentation_additional_notes Additional notes\r
-\r
-It is only legal to defragment allocations bound to:\r
-\r
-- buffers\r
-- images created with `VK_IMAGE_CREATE_ALIAS_BIT`, `VK_IMAGE_TILING_LINEAR`, and\r
- being currently in `VK_IMAGE_LAYOUT_GENERAL` or `VK_IMAGE_LAYOUT_PREINITIALIZED`.\r
-\r
-Defragmentation of images created with `VK_IMAGE_TILING_OPTIMAL` or in any other\r
-layout may give undefined results.\r
-\r
-If you defragment allocations bound to images, new images to be bound to new\r
-memory region after defragmentation should be created with `VK_IMAGE_LAYOUT_PREINITIALIZED`\r
-and then transitioned to their original layout from before defragmentation if\r
-needed using an image memory barrier.\r
-\r
-While using defragmentation, you may experience validation layer warnings, which you just need to ignore.\r
-See [Validation layer warnings](@ref general_considerations_validation_layer_warnings).\r
-\r
-Please don't expect memory to be fully compacted after defragmentation.\r
-Algorithms inside are based on some heuristics that try to maximize number of Vulkan\r
-memory blocks to make totally empty to release them, as well as to maximimze continuous\r
-empty space inside remaining blocks, while minimizing the number and size of allocations that\r
-need to be moved. Some fragmentation may still remain - this is normal.\r
-\r
-\section defragmentation_custom_algorithm Writing custom defragmentation algorithm\r
-\r
-If you want to implement your own, custom defragmentation algorithm,\r
-there is infrastructure prepared for that,\r
-but it is not exposed through the library API - you need to hack its source code.\r
-Here are steps needed to do this:\r
-\r
--# Main thing you need to do is to define your own class derived from base abstract\r
- class `VmaDefragmentationAlgorithm` and implement your version of its pure virtual methods.\r
- See definition and comments of this class for details.\r
--# Your code needs to interact with device memory block metadata.\r
- If you need more access to its data than it's provided by its public interface,\r
- declare your new class as a friend class e.g. in class `VmaBlockMetadata_Generic`.\r
--# If you want to create a flag that would enable your algorithm or pass some additional\r
- flags to configure it, add them to `VmaDefragmentationFlagBits` and use them in\r
- VmaDefragmentationInfo2::flags.\r
--# Modify function `VmaBlockVectorDefragmentationContext::Begin` to create object\r
- of your new class whenever needed.\r
-\r
-\r
-\page lost_allocations Lost allocations\r
-\r
-If your game oversubscribes video memory, if may work OK in previous-generation\r
-graphics APIs (DirectX 9, 10, 11, OpenGL) because resources are automatically\r
-paged to system RAM. In Vulkan you can't do it because when you run out of\r
-memory, an allocation just fails. If you have more data (e.g. textures) that can\r
-fit into VRAM and you don't need it all at once, you may want to upload them to\r
-GPU on demand and "push out" ones that are not used for a long time to make room\r
-for the new ones, effectively using VRAM (or a cartain memory pool) as a form of\r
-cache. Vulkan Memory Allocator can help you with that by supporting a concept of\r
-"lost allocations".\r
-\r
-To create an allocation that can become lost, include #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT\r
-flag in VmaAllocationCreateInfo::flags. Before using a buffer or image bound to\r
-such allocation in every new frame, you need to query it if it's not lost.\r
-To check it, call vmaTouchAllocation().\r
-If the allocation is lost, you should not use it or buffer/image bound to it.\r
-You mustn't forget to destroy this allocation and this buffer/image.\r
-vmaGetAllocationInfo() can also be used for checking status of the allocation.\r
-Allocation is lost when returned VmaAllocationInfo::deviceMemory == `VK_NULL_HANDLE`.\r
-\r
-To create an allocation that can make some other allocations lost to make room\r
-for it, use #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flag. You will\r
-usually use both flags #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT and\r
-#VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT at the same time.\r
-\r
-Warning! Current implementation uses quite naive, brute force algorithm,\r
-which can make allocation calls that use #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT\r
-flag quite slow. A new, more optimal algorithm and data structure to speed this\r
-up is planned for the future.\r
-\r
-<b>Q: When interleaving creation of new allocations with usage of existing ones,\r
-how do you make sure that an allocation won't become lost while it's used in the\r
-current frame?</b>\r
-\r
-It is ensured because vmaTouchAllocation() / vmaGetAllocationInfo() not only returns allocation\r
-status/parameters and checks whether it's not lost, but when it's not, it also\r
-atomically marks it as used in the current frame, which makes it impossible to\r
-become lost in that frame. It uses lockless algorithm, so it works fast and\r
-doesn't involve locking any internal mutex.\r
-\r
-<b>Q: What if my allocation may still be in use by the GPU when it's rendering a\r
-previous frame while I already submit new frame on the CPU?</b>\r
-\r
-You can make sure that allocations "touched" by vmaTouchAllocation() / vmaGetAllocationInfo() will not\r
-become lost for a number of additional frames back from the current one by\r
-specifying this number as VmaAllocatorCreateInfo::frameInUseCount (for default\r
-memory pool) and VmaPoolCreateInfo::frameInUseCount (for custom pool).\r
-\r
-<b>Q: How do you inform the library when new frame starts?</b>\r
-\r
-You need to call function vmaSetCurrentFrameIndex().\r
-\r
-Example code:\r
-\r
-\code\r
-struct MyBuffer\r
-{\r
- VkBuffer m_Buf = nullptr;\r
- VmaAllocation m_Alloc = nullptr;\r
-\r
- // Called when the buffer is really needed in the current frame.\r
- void EnsureBuffer();\r
-};\r
-\r
-void MyBuffer::EnsureBuffer()\r
-{\r
- // Buffer has been created.\r
- if(m_Buf != VK_NULL_HANDLE)\r
- {\r
- // Check if its allocation is not lost + mark it as used in current frame.\r
- if(vmaTouchAllocation(allocator, m_Alloc))\r
- {\r
- // It's all OK - safe to use m_Buf.\r
- return;\r
- }\r
- }\r
-\r
- // Buffer not yet exists or lost - destroy and recreate it.\r
-\r
- vmaDestroyBuffer(allocator, m_Buf, m_Alloc);\r
-\r
- VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
- bufCreateInfo.size = 1024;\r
- bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
-\r
- VmaAllocationCreateInfo allocCreateInfo = {};\r
- allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
- allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT |\r
- VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;\r
-\r
- vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &m_Buf, &m_Alloc, nullptr);\r
-}\r
-\endcode\r
-\r
-When using lost allocations, you may see some Vulkan validation layer warnings\r
-about overlapping regions of memory bound to different kinds of buffers and\r
-images. This is still valid as long as you implement proper handling of lost\r
-allocations (like in the example above) and don't use them.\r
-\r
-You can create an allocation that is already in lost state from the beginning using function\r
-vmaCreateLostAllocation(). It may be useful if you need a "dummy" allocation that is not null.\r
-\r
-You can call function vmaMakePoolAllocationsLost() to set all eligible allocations\r
-in a specified custom pool to lost state.\r
-Allocations that have been "touched" in current frame or VmaPoolCreateInfo::frameInUseCount frames back\r
-cannot become lost.\r
-\r
-<b>Q: Can I touch allocation that cannot become lost?</b>\r
-\r
-Yes, although it has no visible effect.\r
-Calls to vmaGetAllocationInfo() and vmaTouchAllocation() update last use frame index\r
-also for allocations that cannot become lost, but the only way to observe it is to dump\r
-internal allocator state using vmaBuildStatsString().\r
-You can use this feature for debugging purposes to explicitly mark allocations that you use\r
-in current frame and then analyze JSON dump to see for how long each allocation stays unused.\r
-\r
-\r
-\page statistics Statistics\r
-\r
-This library contains functions that return information about its internal state,\r
-especially the amount of memory allocated from Vulkan.\r
-Please keep in mind that these functions need to traverse all internal data structures\r
-to gather these information, so they may be quite time-consuming.\r
-Don't call them too often.\r
-\r
-\section statistics_numeric_statistics Numeric statistics\r
-\r
-You can query for overall statistics of the allocator using function vmaCalculateStats().\r
-Information are returned using structure #VmaStats.\r
-It contains #VmaStatInfo - number of allocated blocks, number of allocations\r
-(occupied ranges in these blocks), number of unused (free) ranges in these blocks,\r
-number of bytes used and unused (but still allocated from Vulkan) and other information.\r
-They are summed across memory heaps, memory types and total for whole allocator.\r
-\r
-You can query for statistics of a custom pool using function vmaGetPoolStats().\r
-Information are returned using structure #VmaPoolStats.\r
-\r
-You can query for information about specific allocation using function vmaGetAllocationInfo().\r
-It fill structure #VmaAllocationInfo.\r
-\r
-\section statistics_json_dump JSON dump\r
-\r
-You can dump internal state of the allocator to a string in JSON format using function vmaBuildStatsString().\r
-The result is guaranteed to be correct JSON.\r
-It uses ANSI encoding.\r
-Any strings provided by user (see [Allocation names](@ref allocation_names))\r
-are copied as-is and properly escaped for JSON, so if they use UTF-8, ISO-8859-2 or any other encoding,\r
-this JSON string can be treated as using this encoding.\r
-It must be freed using function vmaFreeStatsString().\r
-\r
-The format of this JSON string is not part of official documentation of the library,\r
-but it will not change in backward-incompatible way without increasing library major version number\r
-and appropriate mention in changelog.\r
-\r
-The JSON string contains all the data that can be obtained using vmaCalculateStats().\r
-It can also contain detailed map of allocated memory blocks and their regions -\r
-free and occupied by allocations.\r
-This allows e.g. to visualize the memory or assess fragmentation.\r
-\r
-\r
-\page allocation_annotation Allocation names and user data\r
-\r
-\section allocation_user_data Allocation user data\r
-\r
-You can annotate allocations with your own information, e.g. for debugging purposes.\r
-To do that, fill VmaAllocationCreateInfo::pUserData field when creating\r
-an allocation. It's an opaque `void*` pointer. You can use it e.g. as a pointer,\r
-some handle, index, key, ordinal number or any other value that would associate\r
-the allocation with your custom metadata.\r
-\r
-\code\r
-VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };\r
-// Fill bufferInfo...\r
-\r
-MyBufferMetadata* pMetadata = CreateBufferMetadata();\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
-allocCreateInfo.pUserData = pMetadata;\r
-\r
-VkBuffer buffer;\r
-VmaAllocation allocation;\r
-vmaCreateBuffer(allocator, &bufferInfo, &allocCreateInfo, &buffer, &allocation, nullptr);\r
-\endcode\r
-\r
-The pointer may be later retrieved as VmaAllocationInfo::pUserData:\r
-\r
-\code\r
-VmaAllocationInfo allocInfo;\r
-vmaGetAllocationInfo(allocator, allocation, &allocInfo);\r
-MyBufferMetadata* pMetadata = (MyBufferMetadata*)allocInfo.pUserData;\r
-\endcode\r
-\r
-It can also be changed using function vmaSetAllocationUserData().\r
-\r
-Values of (non-zero) allocations' `pUserData` are printed in JSON report created by\r
-vmaBuildStatsString(), in hexadecimal form.\r
-\r
-\section allocation_names Allocation names\r
-\r
-There is alternative mode available where `pUserData` pointer is used to point to\r
-a null-terminated string, giving a name to the allocation. To use this mode,\r
-set #VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT flag in VmaAllocationCreateInfo::flags.\r
-Then `pUserData` passed as VmaAllocationCreateInfo::pUserData or argument to\r
-vmaSetAllocationUserData() must be either null or pointer to a null-terminated string.\r
-The library creates internal copy of the string, so the pointer you pass doesn't need\r
-to be valid for whole lifetime of the allocation. You can free it after the call.\r
-\r
-\code\r
-VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };\r
-// Fill imageInfo...\r
-\r
-std::string imageName = "Texture: ";\r
-imageName += fileName;\r
-\r
-VmaAllocationCreateInfo allocCreateInfo = {};\r
-allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;\r
-allocCreateInfo.flags = VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;\r
-allocCreateInfo.pUserData = imageName.c_str();\r
-\r
-VkImage image;\r
-VmaAllocation allocation;\r
-vmaCreateImage(allocator, &imageInfo, &allocCreateInfo, &image, &allocation, nullptr);\r
-\endcode\r
-\r
-The value of `pUserData` pointer of the allocation will be different than the one\r
-you passed when setting allocation's name - pointing to a buffer managed\r
-internally that holds copy of the string.\r
-\r
-\code\r
-VmaAllocationInfo allocInfo;\r
-vmaGetAllocationInfo(allocator, allocation, &allocInfo);\r
-const char* imageName = (const char*)allocInfo.pUserData;\r
-printf("Image name: %s\n", imageName);\r
-\endcode\r
-\r
-That string is also printed in JSON report created by vmaBuildStatsString().\r
-\r
-\note Passing string name to VMA allocation doesn't automatically set it to the Vulkan buffer or image created with it.\r
-You must do it manually using an extension like VK_EXT_debug_utils, which is independent of this library.\r
-\r
-\r
-\page debugging_memory_usage Debugging incorrect memory usage\r
-\r
-If you suspect a bug with memory usage, like usage of uninitialized memory or\r
-memory being overwritten out of bounds of an allocation,\r
-you can use debug features of this library to verify this.\r
-\r
-\section debugging_memory_usage_initialization Memory initialization\r
-\r
-If you experience a bug with incorrect and nondeterministic data in your program and you suspect uninitialized memory to be used,\r
-you can enable automatic memory initialization to verify this.\r
-To do it, define macro `VMA_DEBUG_INITIALIZE_ALLOCATIONS` to 1.\r
-\r
-\code\r
-#define VMA_DEBUG_INITIALIZE_ALLOCATIONS 1\r
-#include "vk_mem_alloc.h"\r
-\endcode\r
-\r
-It makes memory of all new allocations initialized to bit pattern `0xDCDCDCDC`.\r
-Before an allocation is destroyed, its memory is filled with bit pattern `0xEFEFEFEF`.\r
-Memory is automatically mapped and unmapped if necessary.\r
-\r
-If you find these values while debugging your program, good chances are that you incorrectly\r
-read Vulkan memory that is allocated but not initialized, or already freed, respectively.\r
-\r
-Memory initialization works only with memory types that are `HOST_VISIBLE`.\r
-It works also with dedicated allocations.\r
-It doesn't work with allocations created with #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag,\r
-as they cannot be mapped.\r
-\r
-\section debugging_memory_usage_margins Margins\r
-\r
-By default, allocations are laid out in memory blocks next to each other if possible\r
-(considering required alignment, `bufferImageGranularity`, and `nonCoherentAtomSize`).\r
-\r
-\r
-\r
-Define macro `VMA_DEBUG_MARGIN` to some non-zero value (e.g. 16) to enforce specified\r
-number of bytes as a margin before and after every allocation.\r
-\r
-\code\r
-#define VMA_DEBUG_MARGIN 16\r
-#include "vk_mem_alloc.h"\r
-\endcode\r
-\r
-\r
-\r
-If your bug goes away after enabling margins, it means it may be caused by memory\r
-being overwritten outside of allocation boundaries. It is not 100% certain though.\r
-Change in application behavior may also be caused by different order and distribution\r
-of allocations across memory blocks after margins are applied.\r
-\r
-The margin is applied also before first and after last allocation in a block.\r
-It may occur only once between two adjacent allocations.\r
-\r
-Margins work with all types of memory.\r
-\r
-Margin is applied only to allocations made out of memory blocks and not to dedicated\r
-allocations, which have their own memory block of specific size.\r
-It is thus not applied to allocations made using #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT flag\r
-or those automatically decided to put into dedicated allocations, e.g. due to its\r
-large size or recommended by VK_KHR_dedicated_allocation extension.\r
-Margins are also not active in custom pools created with #VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT flag.\r
-\r
-Margins appear in [JSON dump](@ref statistics_json_dump) as part of free space.\r
-\r
-Note that enabling margins increases memory usage and fragmentation.\r
-\r
-\section debugging_memory_usage_corruption_detection Corruption detection\r
-\r
-You can additionally define macro `VMA_DEBUG_DETECT_CORRUPTION` to 1 to enable validation\r
-of contents of the margins.\r
-\r
-\code\r
-#define VMA_DEBUG_MARGIN 16\r
-#define VMA_DEBUG_DETECT_CORRUPTION 1\r
-#include "vk_mem_alloc.h"\r
-\endcode\r
-\r
-When this feature is enabled, number of bytes specified as `VMA_DEBUG_MARGIN`\r
-(it must be multiply of 4) before and after every allocation is filled with a magic number.\r
-This idea is also know as "canary".\r
-Memory is automatically mapped and unmapped if necessary.\r
-\r
-This number is validated automatically when the allocation is destroyed.\r
-If it's not equal to the expected value, `VMA_ASSERT()` is executed.\r
-It clearly means that either CPU or GPU overwritten the memory outside of boundaries of the allocation,\r
-which indicates a serious bug.\r
-\r
-You can also explicitly request checking margins of all allocations in all memory blocks\r
-that belong to specified memory types by using function vmaCheckCorruption(),\r
-or in memory blocks that belong to specified custom pool, by using function \r
-vmaCheckPoolCorruption().\r
-\r
-Margin validation (corruption detection) works only for memory types that are\r
-`HOST_VISIBLE` and `HOST_COHERENT`.\r
-\r
-\r
-\page record_and_replay Record and replay\r
-\r
-\section record_and_replay_introduction Introduction\r
-\r
-While using the library, sequence of calls to its functions together with their\r
-parameters can be recorded to a file and later replayed using standalone player\r
-application. It can be useful to:\r
-\r
-- Test correctness - check if same sequence of calls will not cause crash or\r
- failures on a target platform.\r
-- Gather statistics - see number of allocations, peak memory usage, number of\r
- calls etc.\r
-- Benchmark performance - see how much time it takes to replay the whole\r
- sequence.\r
-\r
-\section record_and_replay_usage Usage\r
-\r
-Recording functionality is disabled by default.\r
-To enable it, define following macro before every include of this library:\r
-\r
-\code\r
-#define VMA_RECORDING_ENABLED 1\r
-\endcode\r
-\r
-<b>To record sequence of calls to a file:</b> Fill in\r
-VmaAllocatorCreateInfo::pRecordSettings member while creating #VmaAllocator\r
-object. File is opened and written during whole lifetime of the allocator.\r
-\r
-<b>To replay file:</b> Use VmaReplay - standalone command-line program.\r
-Precompiled binary can be found in "bin" directory.\r
-Its source can be found in "src/VmaReplay" directory.\r
-Its project is generated by Premake.\r
-Command line syntax is printed when the program is launched without parameters.\r
-Basic usage:\r
-\r
- VmaReplay.exe MyRecording.csv\r
-\r
-<b>Documentation of file format</b> can be found in file: "docs/Recording file format.md".\r
-It's a human-readable, text file in CSV format (Comma Separated Values).\r
-\r
-\section record_and_replay_additional_considerations Additional considerations\r
-\r
-- Replaying file that was recorded on a different GPU (with different parameters\r
- like `bufferImageGranularity`, `nonCoherentAtomSize`, and especially different\r
- set of memory heaps and types) may give different performance and memory usage\r
- results, as well as issue some warnings and errors.\r
-- Current implementation of recording in VMA, as well as VmaReplay application, is\r
- coded and tested only on Windows. Inclusion of recording code is driven by\r
- `VMA_RECORDING_ENABLED` macro. Support for other platforms should be easy to\r
- add. Contributions are welcomed.\r
-\r
-\r
-\page usage_patterns Recommended usage patterns\r
-\r
-See also slides from talk:\r
-[Sawicki, Adam. Advanced Graphics Techniques Tutorial: Memory management in Vulkan and DX12. Game Developers Conference, 2018](https://www.gdcvault.com/play/1025458/Advanced-Graphics-Techniques-Tutorial-New)\r
-\r
-\r
-\section usage_patterns_common_mistakes Common mistakes\r
-\r
-<b>Use of CPU_TO_GPU instead of CPU_ONLY memory</b>\r
-\r
-#VMA_MEMORY_USAGE_CPU_TO_GPU is recommended only for resources that will be\r
-mapped and written by the CPU, as well as read directly by the GPU - like some\r
-buffers or textures updated every frame (dynamic). If you create a staging copy\r
-of a resource to be written by CPU and then used as a source of transfer to\r
-another resource placed in the GPU memory, that staging resource should be\r
-created with #VMA_MEMORY_USAGE_CPU_ONLY. Please read the descriptions of these\r
-enums carefully for details.\r
-\r
-<b>Unnecessary use of custom pools</b>\r
-\r
-\ref custom_memory_pools may be useful for special purposes - when you want to\r
-keep certain type of resources separate e.g. to reserve minimum amount of memory\r
-for them, limit maximum amount of memory they can occupy, or make some of them\r
-push out the other through the mechanism of \ref lost_allocations. For most\r
-resources this is not needed and so it is not recommended to create #VmaPool\r
-objects and allocations out of them. Allocating from the default pool is sufficient.\r
-\r
-\section usage_patterns_simple Simple patterns\r
-\r
-\subsection usage_patterns_simple_render_targets Render targets\r
-\r
-<b>When:</b>\r
-Any resources that you frequently write and read on GPU,\r
-e.g. images used as color attachments (aka "render targets"), depth-stencil attachments,\r
-images/buffers used as storage image/buffer (aka "Unordered Access View (UAV)").\r
-\r
-<b>What to do:</b>\r
-Create them in video memory that is fastest to access from GPU using\r
-#VMA_MEMORY_USAGE_GPU_ONLY.\r
-\r
-Consider using [VK_KHR_dedicated_allocation](@ref vk_khr_dedicated_allocation) extension\r
-and/or manually creating them as dedicated allocations using #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,\r
-especially if they are large or if you plan to destroy and recreate them e.g. when\r
-display resolution changes.\r
-Prefer to create such resources first and all other GPU resources (like textures and vertex buffers) later.\r
-\r
-\subsection usage_patterns_simple_immutable_resources Immutable resources\r
-\r
-<b>When:</b>\r
-Any resources that you fill on CPU only once (aka "immutable") or infrequently\r
-and then read frequently on GPU,\r
-e.g. textures, vertex and index buffers, constant buffers that don't change often.\r
-\r
-<b>What to do:</b>\r
-Create them in video memory that is fastest to access from GPU using\r
-#VMA_MEMORY_USAGE_GPU_ONLY.\r
-\r
-To initialize content of such resource, create a CPU-side (aka "staging") copy of it\r
-in system memory - #VMA_MEMORY_USAGE_CPU_ONLY, map it, fill it,\r
-and submit a transfer from it to the GPU resource.\r
-You can keep the staging copy if you need it for another upload transfer in the future.\r
-If you don't, you can destroy it or reuse this buffer for uploading different resource\r
-after the transfer finishes.\r
-\r
-Prefer to create just buffers in system memory rather than images, even for uploading textures.\r
-Use `vkCmdCopyBufferToImage()`.\r
-Dont use images with `VK_IMAGE_TILING_LINEAR`.\r
-\r
-\subsection usage_patterns_dynamic_resources Dynamic resources\r
-\r
-<b>When:</b>\r
-Any resources that change frequently (aka "dynamic"), e.g. every frame or every draw call,\r
-written on CPU, read on GPU.\r
-\r
-<b>What to do:</b>\r
-Create them using #VMA_MEMORY_USAGE_CPU_TO_GPU.\r
-You can map it and write to it directly on CPU, as well as read from it on GPU.\r
-\r
-This is a more complex situation. Different solutions are possible,\r
-and the best one depends on specific GPU type, but you can use this simple approach for the start.\r
-Prefer to write to such resource sequentially (e.g. using `memcpy`).\r
-Don't perform random access or any reads from it on CPU, as it may be very slow.\r
-Also note that textures written directly from the host through a mapped pointer need to be in LINEAR not OPTIMAL layout.\r
-\r
-\subsection usage_patterns_readback Readback\r
-\r
-<b>When:</b>\r
-Resources that contain data written by GPU that you want to read back on CPU,\r
-e.g. results of some computations.\r
-\r
-<b>What to do:</b>\r
-Create them using #VMA_MEMORY_USAGE_GPU_TO_CPU.\r
-You can write to them directly on GPU, as well as map and read them on CPU.\r
-\r
-\section usage_patterns_advanced Advanced patterns\r
-\r
-\subsection usage_patterns_integrated_graphics Detecting integrated graphics\r
-\r
-You can support integrated graphics (like Intel HD Graphics, AMD APU) better\r
-by detecting it in Vulkan.\r
-To do it, call `vkGetPhysicalDeviceProperties()`, inspect\r
-`VkPhysicalDeviceProperties::deviceType` and look for `VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU`.\r
-When you find it, you can assume that memory is unified and all memory types are comparably fast\r
-to access from GPU, regardless of `VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT`.\r
-\r
-You can then sum up sizes of all available memory heaps and treat them as useful for\r
-your GPU resources, instead of only `DEVICE_LOCAL` ones.\r
-You can also prefer to create your resources in memory types that are `HOST_VISIBLE` to map them\r
-directly instead of submitting explicit transfer (see below).\r
-\r
-\subsection usage_patterns_direct_vs_transfer Direct access versus transfer\r
-\r
-For resources that you frequently write on CPU and read on GPU, many solutions are possible:\r
-\r
--# Create one copy in video memory using #VMA_MEMORY_USAGE_GPU_ONLY,\r
- second copy in system memory using #VMA_MEMORY_USAGE_CPU_ONLY and submit explicit transfer each time.\r
--# Create just a single copy using #VMA_MEMORY_USAGE_CPU_TO_GPU, map it and fill it on CPU,\r
- read it directly on GPU.\r
--# Create just a single copy using #VMA_MEMORY_USAGE_CPU_ONLY, map it and fill it on CPU,\r
- read it directly on GPU.\r
-\r
-Which solution is the most efficient depends on your resource and especially on the GPU.\r
-It is best to measure it and then make the decision.\r
-Some general recommendations:\r
-\r
-- On integrated graphics use (2) or (3) to avoid unnecesary time and memory overhead\r
- related to using a second copy and making transfer.\r
-- For small resources (e.g. constant buffers) use (2).\r
- Discrete AMD cards have special 256 MiB pool of video memory that is directly mappable.\r
- Even if the resource ends up in system memory, its data may be cached on GPU after first\r
- fetch over PCIe bus.\r
-- For larger resources (e.g. textures), decide between (1) and (2).\r
- You may want to differentiate NVIDIA and AMD, e.g. by looking for memory type that is\r
- both `DEVICE_LOCAL` and `HOST_VISIBLE`. When you find it, use (2), otherwise use (1).\r
-\r
-Similarly, for resources that you frequently write on GPU and read on CPU, multiple\r
-solutions are possible:\r
-\r
--# Create one copy in video memory using #VMA_MEMORY_USAGE_GPU_ONLY,\r
- second copy in system memory using #VMA_MEMORY_USAGE_GPU_TO_CPU and submit explicit tranfer each time.\r
--# Create just single copy using #VMA_MEMORY_USAGE_GPU_TO_CPU, write to it directly on GPU,\r
- map it and read it on CPU.\r
-\r
-You should take some measurements to decide which option is faster in case of your specific\r
-resource.\r
-\r
-Note that textures accessed directly from the host through a mapped pointer need to be in LINEAR layout,\r
-which may slow down their usage on the device.\r
-Textures accessed only by the device and transfer operations can use OPTIMAL layout.\r
-\r
-If you don't want to specialize your code for specific types of GPUs, you can still make\r
-an simple optimization for cases when your resource ends up in mappable memory to use it\r
-directly in this case instead of creating CPU-side staging copy.\r
-For details see [Finding out if memory is mappable](@ref memory_mapping_finding_if_memory_mappable).\r
-\r
-\r
-\page configuration Configuration\r
-\r
-Please check "CONFIGURATION SECTION" in the code to find macros that you can define\r
-before each include of this file or change directly in this file to provide\r
-your own implementation of basic facilities like assert, `min()` and `max()` functions,\r
-mutex, atomic etc.\r
-The library uses its own implementation of containers by default, but you can switch to using\r
-STL containers instead.\r
-\r
-For example, define `VMA_ASSERT(expr)` before including the library to provide\r
-custom implementation of the assertion, compatible with your project.\r
-By default it is defined to standard C `assert(expr)` in `_DEBUG` configuration\r
-and empty otherwise.\r
-\r
-\section config_Vulkan_functions Pointers to Vulkan functions\r
-\r
-There are multiple ways to import pointers to Vulkan functions in the library.\r
-In the simplest case you don't need to do anything.\r
-If the compilation or linking of your program or the initialization of the #VmaAllocator\r
-doesn't work for you, you can try to reconfigure it.\r
-\r
-First, the allocator tries to fetch pointers to Vulkan functions linked statically,\r
-like this:\r
-\r
-\code\r
-m_VulkanFunctions.vkAllocateMemory = (PFN_vkAllocateMemory)vkAllocateMemory;\r
-\endcode\r
-\r
-If you want to disable this feature, set configuration macro: `#define VMA_STATIC_VULKAN_FUNCTIONS 0`.\r
-\r
-Second, you can provide the pointers yourself by setting member VmaAllocatorCreateInfo::pVulkanFunctions.\r
-You can fetch them e.g. using functions `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` or\r
-by using a helper library like [volk](https://github.com/zeux/volk).\r
-\r
-Third, VMA tries to fetch remaining pointers that are still null by calling\r
-`vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` on its own.\r
-If you want to disable this feature, set configuration macro: `#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0`.\r
-\r
-Finally, all the function pointers required by the library (considering selected\r
-Vulkan version and enabled extensions) are checked with `VMA_ASSERT` if they are not null.\r
-\r
-\r
-\section custom_memory_allocator Custom host memory allocator\r
-\r
-If you use custom allocator for CPU memory rather than default operator `new`\r
-and `delete` from C++, you can make this library using your allocator as well\r
-by filling optional member VmaAllocatorCreateInfo::pAllocationCallbacks. These\r
-functions will be passed to Vulkan, as well as used by the library itself to\r
-make any CPU-side allocations.\r
-\r
-\section allocation_callbacks Device memory allocation callbacks\r
-\r
-The library makes calls to `vkAllocateMemory()` and `vkFreeMemory()` internally.\r
-You can setup callbacks to be informed about these calls, e.g. for the purpose\r
-of gathering some statistics. To do it, fill optional member\r
-VmaAllocatorCreateInfo::pDeviceMemoryCallbacks.\r
-\r
-\section heap_memory_limit Device heap memory limit\r
-\r
-When device memory of certain heap runs out of free space, new allocations may\r
-fail (returning error code) or they may succeed, silently pushing some existing\r
-memory blocks from GPU VRAM to system RAM (which degrades performance). This\r
-behavior is implementation-dependant - it depends on GPU vendor and graphics\r
-driver.\r
-\r
-On AMD cards it can be controlled while creating Vulkan device object by using\r
-VK_AMD_memory_overallocation_behavior extension, if available.\r
-\r
-Alternatively, if you want to test how your program behaves with limited amount of Vulkan device\r
-memory available without switching your graphics card to one that really has\r
-smaller VRAM, you can use a feature of this library intended for this purpose.\r
-To do it, fill optional member VmaAllocatorCreateInfo::pHeapSizeLimit.\r
-\r
-\r
-\r
-\page vk_khr_dedicated_allocation VK_KHR_dedicated_allocation\r
-\r
-VK_KHR_dedicated_allocation is a Vulkan extension which can be used to improve\r
-performance on some GPUs. It augments Vulkan API with possibility to query\r
-driver whether it prefers particular buffer or image to have its own, dedicated\r
-allocation (separate `VkDeviceMemory` block) for better efficiency - to be able\r
-to do some internal optimizations.\r
-\r
-The extension is supported by this library. It will be used automatically when\r
-enabled. To enable it:\r
-\r
-1 . When creating Vulkan device, check if following 2 device extensions are\r
-supported (call `vkEnumerateDeviceExtensionProperties()`).\r
-If yes, enable them (fill `VkDeviceCreateInfo::ppEnabledExtensionNames`).\r
-\r
-- VK_KHR_get_memory_requirements2\r
-- VK_KHR_dedicated_allocation\r
-\r
-If you enabled these extensions:\r
-\r
-2 . Use #VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT flag when creating\r
-your #VmaAllocator`to inform the library that you enabled required extensions\r
-and you want the library to use them.\r
-\r
-\code\r
-allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;\r
-\r
-vmaCreateAllocator(&allocatorInfo, &allocator);\r
-\endcode\r
-\r
-That's all. The extension will be automatically used whenever you create a\r
-buffer using vmaCreateBuffer() or image using vmaCreateImage().\r
-\r
-When using the extension together with Vulkan Validation Layer, you will receive\r
-warnings like this:\r
-\r
- vkBindBufferMemory(): Binding memory to buffer 0x33 but vkGetBufferMemoryRequirements() has not been called on that buffer.\r
-\r
-It is OK, you should just ignore it. It happens because you use function\r
-`vkGetBufferMemoryRequirements2KHR()` instead of standard\r
-`vkGetBufferMemoryRequirements()`, while the validation layer seems to be\r
-unaware of it.\r
-\r
-To learn more about this extension, see:\r
-\r
-- [VK_KHR_dedicated_allocation in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap44.html#VK_KHR_dedicated_allocation)\r
-- [VK_KHR_dedicated_allocation unofficial manual](http://asawicki.info/articles/VK_KHR_dedicated_allocation.php5)\r
-\r
-\r
-\r
-\page vk_amd_device_coherent_memory VK_AMD_device_coherent_memory\r
-\r
-VK_AMD_device_coherent_memory is a device extension that enables access to\r
-additional memory types with `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` and\r
-`VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD` flag. It is useful mostly for\r
-allocation of buffers intended for writing "breadcrumb markers" in between passes\r
-or draw calls, which in turn are useful for debugging GPU crash/hang/TDR cases.\r
-\r
-When the extension is available but has not been enabled, Vulkan physical device\r
-still exposes those memory types, but their usage is forbidden. VMA automatically\r
-takes care of that - it returns `VK_ERROR_FEATURE_NOT_PRESENT` when an attempt\r
-to allocate memory of such type is made.\r
-\r
-If you want to use this extension in connection with VMA, follow these steps:\r
-\r
-\section vk_amd_device_coherent_memory_initialization Initialization\r
-\r
-1) Call `vkEnumerateDeviceExtensionProperties` for the physical device.\r
-Check if the extension is supported - if returned array of `VkExtensionProperties` contains "VK_AMD_device_coherent_memory".\r
-\r
-2) Call `vkGetPhysicalDeviceFeatures2` for the physical device instead of old `vkGetPhysicalDeviceFeatures`.\r
-Attach additional structure `VkPhysicalDeviceCoherentMemoryFeaturesAMD` to `VkPhysicalDeviceFeatures2::pNext` to be returned.\r
-Check if the device feature is really supported - check if `VkPhysicalDeviceCoherentMemoryFeaturesAMD::deviceCoherentMemory` is true.\r
-\r
-3) While creating device with `vkCreateDevice`, enable this extension - add "VK_AMD_device_coherent_memory"\r
-to the list passed as `VkDeviceCreateInfo::ppEnabledExtensionNames`.\r
-\r
-4) While creating the device, also don't set `VkDeviceCreateInfo::pEnabledFeatures`.\r
-Fill in `VkPhysicalDeviceFeatures2` structure instead and pass it as `VkDeviceCreateInfo::pNext`.\r
-Enable this device feature - attach additional structure `VkPhysicalDeviceCoherentMemoryFeaturesAMD` to\r
-`VkPhysicalDeviceFeatures2::pNext` and set its member `deviceCoherentMemory` to `VK_TRUE`.\r
-\r
-5) While creating #VmaAllocator with vmaCreateAllocator() inform VMA that you\r
-have enabled this extension and feature - add #VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT\r
-to VmaAllocatorCreateInfo::flags.\r
-\r
-\section vk_amd_device_coherent_memory_usage Usage\r
-\r
-After following steps described above, you can create VMA allocations and custom pools\r
-out of the special `DEVICE_COHERENT` and `DEVICE_UNCACHED` memory types on eligible\r
-devices. There are multiple ways to do it, for example:\r
-\r
-- You can request or prefer to allocate out of such memory types by adding\r
- `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` to VmaAllocationCreateInfo::requiredFlags\r
- or VmaAllocationCreateInfo::preferredFlags. Those flags can be freely mixed with\r
- other ways of \ref choosing_memory_type, like setting VmaAllocationCreateInfo::usage.\r
-- If you manually found memory type index to use for this purpose, force allocation\r
- from this specific index by setting VmaAllocationCreateInfo::memoryTypeBits `= 1u << index`.\r
-\r
-\section vk_amd_device_coherent_memory_more_information More information\r
-\r
-To learn more about this extension, see [VK_AMD_device_coherent_memory in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap44.html#VK_AMD_device_coherent_memory)\r
-\r
-Example use of this extension can be found in the code of the sample and test suite\r
-accompanying this library.\r
-\r
-\r
-\page enabling_buffer_device_address Enabling buffer device address\r
-\r
-Device extension VK_KHR_buffer_device_address\r
-allow to fetch raw GPU pointer to a buffer and pass it for usage in a shader code.\r
-It is promoted to core Vulkan 1.2.\r
-\r
-If you want to use this feature in connection with VMA, follow these steps:\r
-\r
-\section enabling_buffer_device_address_initialization Initialization\r
-\r
-1) (For Vulkan version < 1.2) Call `vkEnumerateDeviceExtensionProperties` for the physical device.\r
-Check if the extension is supported - if returned array of `VkExtensionProperties` contains\r
-"VK_KHR_buffer_device_address".\r
-\r
-2) Call `vkGetPhysicalDeviceFeatures2` for the physical device instead of old `vkGetPhysicalDeviceFeatures`.\r
-Attach additional structure `VkPhysicalDeviceBufferDeviceAddressFeatures*` to `VkPhysicalDeviceFeatures2::pNext` to be returned.\r
-Check if the device feature is really supported - check if `VkPhysicalDeviceBufferDeviceAddressFeatures*::bufferDeviceAddress` is true.\r
-\r
-3) (For Vulkan version < 1.2) While creating device with `vkCreateDevice`, enable this extension - add\r
-"VK_KHR_buffer_device_address" to the list passed as `VkDeviceCreateInfo::ppEnabledExtensionNames`.\r
-\r
-4) While creating the device, also don't set `VkDeviceCreateInfo::pEnabledFeatures`.\r
-Fill in `VkPhysicalDeviceFeatures2` structure instead and pass it as `VkDeviceCreateInfo::pNext`.\r
-Enable this device feature - attach additional structure `VkPhysicalDeviceBufferDeviceAddressFeatures*` to\r
-`VkPhysicalDeviceFeatures2::pNext` and set its member `bufferDeviceAddress` to `VK_TRUE`.\r
-\r
-5) While creating #VmaAllocator with vmaCreateAllocator() inform VMA that you\r
-have enabled this feature - add #VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT\r
-to VmaAllocatorCreateInfo::flags.\r
-\r
-\section enabling_buffer_device_address_usage Usage\r
-\r
-After following steps described above, you can create buffers with `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT*` using VMA.\r
-The library automatically adds `VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT*` to\r
-allocated memory blocks wherever it might be needed.\r
-\r
-Please note that the library supports only `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT*`.\r
-The second part of this functionality related to "capture and replay" is not supported,\r
-as it is intended for usage in debugging tools like RenderDoc, not in everyday Vulkan usage.\r
-\r
-\section enabling_buffer_device_address_more_information More information\r
-\r
-To learn more about this extension, see [VK_KHR_buffer_device_address in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap46.html#VK_KHR_buffer_device_address)\r
-\r
-Example use of this extension can be found in the code of the sample and test suite\r
-accompanying this library.\r
-\r
-\page general_considerations General considerations\r
-\r
-\section general_considerations_thread_safety Thread safety\r
-\r
-- The library has no global state, so separate #VmaAllocator objects can be used\r
- independently.\r
- There should be no need to create multiple such objects though - one per `VkDevice` is enough.\r
-- By default, all calls to functions that take #VmaAllocator as first parameter\r
- are safe to call from multiple threads simultaneously because they are\r
- synchronized internally when needed.\r
-- When the allocator is created with #VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT\r
- flag, calls to functions that take such #VmaAllocator object must be\r
- synchronized externally.\r
-- Access to a #VmaAllocation object must be externally synchronized. For example,\r
- you must not call vmaGetAllocationInfo() and vmaMapMemory() from different\r
- threads at the same time if you pass the same #VmaAllocation object to these\r
- functions.\r
-\r
-\section general_considerations_validation_layer_warnings Validation layer warnings\r
-\r
-When using this library, you can meet following types of warnings issued by\r
-Vulkan validation layer. They don't necessarily indicate a bug, so you may need\r
-to just ignore them.\r
-\r
-- *vkBindBufferMemory(): Binding memory to buffer 0xeb8e4 but vkGetBufferMemoryRequirements() has not been called on that buffer.*\r
- - It happens when VK_KHR_dedicated_allocation extension is enabled.\r
- `vkGetBufferMemoryRequirements2KHR` function is used instead, while validation layer seems to be unaware of it.\r
-- *Mapping an image with layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL can result in undefined behavior if this memory is used by the device. Only GENERAL or PREINITIALIZED should be used.*\r
- - It happens when you map a buffer or image, because the library maps entire\r
- `VkDeviceMemory` block, where different types of images and buffers may end\r
- up together, especially on GPUs with unified memory like Intel.\r
-- *Non-linear image 0xebc91 is aliased with linear buffer 0xeb8e4 which may indicate a bug.*\r
- - It happens when you use lost allocations, and a new image or buffer is\r
- created in place of an existing object that bacame lost.\r
- - It may happen also when you use [defragmentation](@ref defragmentation).\r
-\r
-\section general_considerations_allocation_algorithm Allocation algorithm\r
-\r
-The library uses following algorithm for allocation, in order:\r
-\r
--# Try to find free range of memory in existing blocks.\r
--# If failed, try to create a new block of `VkDeviceMemory`, with preferred block size.\r
--# If failed, try to create such block with size/2, size/4, size/8.\r
--# If failed and #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flag was\r
- specified, try to find space in existing blocks, possilby making some other\r
- allocations lost.\r
--# If failed, try to allocate separate `VkDeviceMemory` for this allocation,\r
- just like when you use #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.\r
--# If failed, choose other memory type that meets the requirements specified in\r
- VmaAllocationCreateInfo and go to point 1.\r
--# If failed, return `VK_ERROR_OUT_OF_DEVICE_MEMORY`.\r
-\r
-\section general_considerations_features_not_supported Features not supported\r
-\r
-Features deliberately excluded from the scope of this library:\r
-\r
-- Data transfer. Uploading (straming) and downloading data of buffers and images\r
- between CPU and GPU memory and related synchronization is responsibility of the user.\r
- Defining some "texture" object that would automatically stream its data from a\r
- staging copy in CPU memory to GPU memory would rather be a feature of another,\r
- higher-level library implemented on top of VMA.\r
-- Allocations for imported/exported external memory. They tend to require\r
- explicit memory type index and dedicated allocation anyway, so they don't\r
- interact with main features of this library. Such special purpose allocations\r
- should be made manually, using `vkCreateBuffer()` and `vkAllocateMemory()`.\r
-- Recreation of buffers and images. Although the library has functions for\r
- buffer and image creation (vmaCreateBuffer(), vmaCreateImage()), you need to\r
- recreate these objects yourself after defragmentation. That's because the big\r
- structures `VkBufferCreateInfo`, `VkImageCreateInfo` are not stored in\r
- #VmaAllocation object.\r
-- Handling CPU memory allocation failures. When dynamically creating small C++\r
- objects in CPU memory (not Vulkan memory), allocation failures are not checked\r
- and handled gracefully, because that would complicate code significantly and\r
- is usually not needed in desktop PC applications anyway.\r
-- Code free of any compiler warnings. Maintaining the library to compile and\r
- work correctly on so many different platforms is hard enough. Being free of \r
- any warnings, on any version of any compiler, is simply not feasible.\r
-- This is a C++ library with C interface.\r
- Bindings or ports to any other programming languages are welcomed as external projects and\r
- are not going to be included into this repository.\r
-\r
-*/\r
-\r
-#if VMA_RECORDING_ENABLED\r
- #include <chrono>\r
- #if defined(_WIN32)\r
- #include <windows.h>\r
- #else\r
- #include <sstream>\r
- #include <thread>\r
- #endif\r
-#endif\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-/*\r
-Define this macro to 0/1 to disable/enable support for recording functionality,\r
-available through VmaAllocatorCreateInfo::pRecordSettings.\r
-*/\r
-#ifndef VMA_RECORDING_ENABLED\r
- #define VMA_RECORDING_ENABLED 0\r
-#endif\r
-\r
-#ifndef NOMINMAX\r
- #define NOMINMAX // For windows.h\r
-#endif\r
-\r
-#if defined(__ANDROID__) && defined(VK_NO_PROTOTYPES) && VMA_STATIC_VULKAN_FUNCTIONS\r
- extern PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;\r
- extern PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;\r
- extern PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties;\r
- extern PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties;\r
- extern PFN_vkAllocateMemory vkAllocateMemory;\r
- extern PFN_vkFreeMemory vkFreeMemory;\r
- extern PFN_vkMapMemory vkMapMemory;\r
- extern PFN_vkUnmapMemory vkUnmapMemory;\r
- extern PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges;\r
- extern PFN_vkInvalidateMappedMemoryRanges vkInvalidateMappedMemoryRanges;\r
- extern PFN_vkBindBufferMemory vkBindBufferMemory;\r
- extern PFN_vkBindImageMemory vkBindImageMemory;\r
- extern PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements;\r
- extern PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements;\r
- extern PFN_vkCreateBuffer vkCreateBuffer;\r
- extern PFN_vkDestroyBuffer vkDestroyBuffer;\r
- extern PFN_vkCreateImage vkCreateImage;\r
- extern PFN_vkDestroyImage vkDestroyImage;\r
- extern PFN_vkCmdCopyBuffer vkCmdCopyBuffer;\r
- #if VMA_VULKAN_VERSION >= 1001000\r
- extern PFN_vkGetBufferMemoryRequirements2 vkGetBufferMemoryRequirements2;\r
- extern PFN_vkGetImageMemoryRequirements2 vkGetImageMemoryRequirements2;\r
- extern PFN_vkBindBufferMemory2 vkBindBufferMemory2;\r
- extern PFN_vkBindImageMemory2 vkBindImageMemory2;\r
- extern PFN_vkGetPhysicalDeviceMemoryProperties2 vkGetPhysicalDeviceMemoryProperties2;\r
- #endif // #if VMA_VULKAN_VERSION >= 1001000\r
-#endif // #if defined(__ANDROID__) && VMA_STATIC_VULKAN_FUNCTIONS && VK_NO_PROTOTYPES\r
-\r
-#ifndef VULKAN_H_\r
- #include <vulkan/vulkan.h>\r
-#endif\r
-\r
-// Define this macro to declare maximum supported Vulkan version in format AAABBBCCC,\r
-// where AAA = major, BBB = minor, CCC = patch.\r
-// If you want to use version > 1.0, it still needs to be enabled via VmaAllocatorCreateInfo::vulkanApiVersion.\r
-#if !defined(VMA_VULKAN_VERSION)\r
- #if defined(VK_VERSION_1_2)\r
- #define VMA_VULKAN_VERSION 1002000\r
- #elif defined(VK_VERSION_1_1)\r
- #define VMA_VULKAN_VERSION 1001000\r
- #else\r
- #define VMA_VULKAN_VERSION 1000000\r
- #endif\r
-#endif\r
-\r
-#if !defined(VMA_DEDICATED_ALLOCATION)\r
- #if VK_KHR_get_memory_requirements2 && VK_KHR_dedicated_allocation\r
- #define VMA_DEDICATED_ALLOCATION 1\r
- #else\r
- #define VMA_DEDICATED_ALLOCATION 0\r
- #endif\r
-#endif\r
-\r
-#if !defined(VMA_BIND_MEMORY2)\r
- #if VK_KHR_bind_memory2\r
- #define VMA_BIND_MEMORY2 1\r
- #else\r
- #define VMA_BIND_MEMORY2 0\r
- #endif\r
-#endif\r
-\r
-#if !defined(VMA_MEMORY_BUDGET)\r
- #if VK_EXT_memory_budget && (VK_KHR_get_physical_device_properties2 || VMA_VULKAN_VERSION >= 1001000)\r
- #define VMA_MEMORY_BUDGET 1\r
- #else\r
- #define VMA_MEMORY_BUDGET 0\r
- #endif\r
-#endif\r
-\r
-// Defined to 1 when VK_KHR_buffer_device_address device extension or equivalent core Vulkan 1.2 feature is defined in its headers.\r
-#if !defined(VMA_BUFFER_DEVICE_ADDRESS)\r
- #if VK_KHR_buffer_device_address || VMA_VULKAN_VERSION >= 1002000\r
- #define VMA_BUFFER_DEVICE_ADDRESS 1\r
- #else\r
- #define VMA_BUFFER_DEVICE_ADDRESS 0\r
- #endif\r
-#endif\r
-\r
-// Define these macros to decorate all public functions with additional code,\r
-// before and after returned type, appropriately. This may be useful for\r
-// exporing the functions when compiling VMA as a separate library. Example:\r
-// #define VMA_CALL_PRE __declspec(dllexport)\r
-// #define VMA_CALL_POST __cdecl\r
-#ifndef VMA_CALL_PRE\r
- #define VMA_CALL_PRE\r
-#endif\r
-#ifndef VMA_CALL_POST\r
- #define VMA_CALL_POST\r
-#endif\r
-\r
-// Define this macro to decorate pointers with an attribute specifying the\r
-// length of the array they point to if they are not null.\r
-//\r
-// The length may be one of\r
-// - The name of another parameter in the argument list where the pointer is declared\r
-// - The name of another member in the struct where the pointer is declared\r
-// - The name of a member of a struct type, meaning the value of that member in\r
-// the context of the call. For example\r
-// VMA_LEN_IF_NOT_NULL("VkPhysicalDeviceMemoryProperties::memoryHeapCount"),\r
-// this means the number of memory heaps available in the device associated\r
-// with the VmaAllocator being dealt with.\r
-#ifndef VMA_LEN_IF_NOT_NULL\r
- #define VMA_LEN_IF_NOT_NULL(len)\r
-#endif\r
-\r
-// The VMA_NULLABLE macro is defined to be _Nullable when compiling with Clang.\r
-// see: https://clang.llvm.org/docs/AttributeReference.html#nullable\r
-#ifndef VMA_NULLABLE\r
- #ifdef __clang__\r
- #define VMA_NULLABLE _Nullable\r
- #else\r
- #define VMA_NULLABLE\r
- #endif\r
-#endif\r
-\r
-// The VMA_NOT_NULL macro is defined to be _Nonnull when compiling with Clang.\r
-// see: https://clang.llvm.org/docs/AttributeReference.html#nonnull\r
-#ifndef VMA_NOT_NULL\r
- #ifdef __clang__\r
- #define VMA_NOT_NULL _Nonnull\r
- #else\r
- #define VMA_NOT_NULL\r
- #endif\r
-#endif\r
-\r
-// If non-dispatchable handles are represented as pointers then we can give\r
-// then nullability annotations\r
-#ifndef VMA_NOT_NULL_NON_DISPATCHABLE\r
- #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)\r
- #define VMA_NOT_NULL_NON_DISPATCHABLE VMA_NOT_NULL\r
- #else\r
- #define VMA_NOT_NULL_NON_DISPATCHABLE\r
- #endif\r
-#endif\r
-\r
-#ifndef VMA_NULLABLE_NON_DISPATCHABLE\r
- #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)\r
- #define VMA_NULLABLE_NON_DISPATCHABLE VMA_NULLABLE\r
- #else\r
- #define VMA_NULLABLE_NON_DISPATCHABLE\r
- #endif\r
-#endif\r
-\r
-/** \struct VmaAllocator\r
-\brief Represents main object of this library initialized.\r
-\r
-Fill structure #VmaAllocatorCreateInfo and call function vmaCreateAllocator() to create it.\r
-Call function vmaDestroyAllocator() to destroy it.\r
-\r
-It is recommended to create just one object of this type per `VkDevice` object,\r
-right after Vulkan is initialized and keep it alive until before Vulkan device is destroyed.\r
-*/\r
-VK_DEFINE_HANDLE(VmaAllocator)\r
-\r
-/// Callback function called after successful vkAllocateMemory.\r
-typedef void (VKAPI_PTR *PFN_vmaAllocateDeviceMemoryFunction)(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t memoryType,\r
- VkDeviceMemory VMA_NOT_NULL_NON_DISPATCHABLE memory,\r
- VkDeviceSize size,\r
- void* VMA_NULLABLE pUserData);\r
-/// Callback function called before vkFreeMemory.\r
-typedef void (VKAPI_PTR *PFN_vmaFreeDeviceMemoryFunction)(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t memoryType,\r
- VkDeviceMemory VMA_NOT_NULL_NON_DISPATCHABLE memory,\r
- VkDeviceSize size,\r
- void* VMA_NULLABLE pUserData);\r
-\r
-/** \brief Set of callbacks that the library will call for `vkAllocateMemory` and `vkFreeMemory`.\r
-\r
-Provided for informative purpose, e.g. to gather statistics about number of\r
-allocations or total amount of memory allocated in Vulkan.\r
-\r
-Used in VmaAllocatorCreateInfo::pDeviceMemoryCallbacks.\r
-*/\r
-typedef struct VmaDeviceMemoryCallbacks {\r
- /// Optional, can be null.\r
- PFN_vmaAllocateDeviceMemoryFunction VMA_NULLABLE pfnAllocate;\r
- /// Optional, can be null.\r
- PFN_vmaFreeDeviceMemoryFunction VMA_NULLABLE pfnFree;\r
- /// Optional, can be null.\r
- void* VMA_NULLABLE pUserData;\r
-} VmaDeviceMemoryCallbacks;\r
-\r
-/// Flags for created #VmaAllocator.\r
-typedef enum VmaAllocatorCreateFlagBits {\r
- /** \brief Allocator and all objects created from it will not be synchronized internally, so you must guarantee they are used from only one thread at a time or synchronized externally by you.\r
-\r
- Using this flag may increase performance because internal mutexes are not used.\r
- */\r
- VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT = 0x00000001,\r
- /** \brief Enables usage of VK_KHR_dedicated_allocation extension.\r
-\r
- The flag works only if VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_0`.\r
- When it's `VK_API_VERSION_1_1`, the flag is ignored because the extension has been promoted to Vulkan 1.1.\r
-\r
- Using this extenion will automatically allocate dedicated blocks of memory for\r
- some buffers and images instead of suballocating place for them out of bigger\r
- memory blocks (as if you explicitly used #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT\r
- flag) when it is recommended by the driver. It may improve performance on some\r
- GPUs.\r
-\r
- You may set this flag only if you found out that following device extensions are\r
- supported, you enabled them while creating Vulkan device passed as\r
- VmaAllocatorCreateInfo::device, and you want them to be used internally by this\r
- library:\r
-\r
- - VK_KHR_get_memory_requirements2 (device extension)\r
- - VK_KHR_dedicated_allocation (device extension)\r
-\r
- When this flag is set, you can experience following warnings reported by Vulkan\r
- validation layer. You can ignore them.\r
-\r
- > vkBindBufferMemory(): Binding memory to buffer 0x2d but vkGetBufferMemoryRequirements() has not been called on that buffer.\r
- */\r
- VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT = 0x00000002,\r
- /**\r
- Enables usage of VK_KHR_bind_memory2 extension.\r
-\r
- The flag works only if VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_0`.\r
- When it's `VK_API_VERSION_1_1`, the flag is ignored because the extension has been promoted to Vulkan 1.1.\r
-\r
- You may set this flag only if you found out that this device extension is supported,\r
- you enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device,\r
- and you want it to be used internally by this library.\r
-\r
- The extension provides functions `vkBindBufferMemory2KHR` and `vkBindImageMemory2KHR`,\r
- which allow to pass a chain of `pNext` structures while binding.\r
- This flag is required if you use `pNext` parameter in vmaBindBufferMemory2() or vmaBindImageMemory2().\r
- */\r
- VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT = 0x00000004,\r
- /**\r
- Enables usage of VK_EXT_memory_budget extension.\r
-\r
- You may set this flag only if you found out that this device extension is supported,\r
- you enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device,\r
- and you want it to be used internally by this library, along with another instance extension\r
- VK_KHR_get_physical_device_properties2, which is required by it (or Vulkan 1.1, where this extension is promoted).\r
-\r
- The extension provides query for current memory usage and budget, which will probably\r
- be more accurate than an estimation used by the library otherwise.\r
- */\r
- VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT = 0x00000008,\r
- /**\r
- Enables usage of VK_AMD_device_coherent_memory extension.\r
- \r
- You may set this flag only if you:\r
-\r
- - found out that this device extension is supported and enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device,\r
- - checked that `VkPhysicalDeviceCoherentMemoryFeaturesAMD::deviceCoherentMemory` is true and set it while creating the Vulkan device,\r
- - want it to be used internally by this library.\r
-\r
- The extension and accompanying device feature provide access to memory types with\r
- `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` and `VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD` flags.\r
- They are useful mostly for writing breadcrumb markers - a common method for debugging GPU crash/hang/TDR.\r
- \r
- When the extension is not enabled, such memory types are still enumerated, but their usage is illegal.\r
- To protect from this error, if you don't create the allocator with this flag, it will refuse to allocate any memory or create a custom pool in such memory type,\r
- returning `VK_ERROR_FEATURE_NOT_PRESENT`.\r
- */\r
- VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT = 0x00000010,\r
- /**\r
- Enables usage of "buffer device address" feature, which allows you to use function\r
- `vkGetBufferDeviceAddress*` to get raw GPU pointer to a buffer and pass it for usage inside a shader.\r
-\r
- You may set this flag only if you:\r
-\r
- 1. (For Vulkan version < 1.2) Found as available and enabled device extension\r
- VK_KHR_buffer_device_address.\r
- This extension is promoted to core Vulkan 1.2.\r
- 2. Found as available and enabled device feature `VkPhysicalDeviceBufferDeviceAddressFeatures*::bufferDeviceAddress`.\r
-\r
- When this flag is set, you can create buffers with `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT*` using VMA.\r
- The library automatically adds `VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT*` to\r
- allocated memory blocks wherever it might be needed.\r
-\r
- For more information, see documentation chapter \ref enabling_buffer_device_address.\r
- */\r
- VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT = 0x00000020,\r
-\r
- VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF\r
-} VmaAllocatorCreateFlagBits;\r
-typedef VkFlags VmaAllocatorCreateFlags;\r
-\r
-/** \brief Pointers to some Vulkan functions - a subset used by the library.\r
-\r
-Used in VmaAllocatorCreateInfo::pVulkanFunctions.\r
-*/\r
-typedef struct VmaVulkanFunctions {\r
- PFN_vkGetPhysicalDeviceProperties VMA_NULLABLE vkGetPhysicalDeviceProperties;\r
- PFN_vkGetPhysicalDeviceMemoryProperties VMA_NULLABLE vkGetPhysicalDeviceMemoryProperties;\r
- PFN_vkAllocateMemory VMA_NULLABLE vkAllocateMemory;\r
- PFN_vkFreeMemory VMA_NULLABLE vkFreeMemory;\r
- PFN_vkMapMemory VMA_NULLABLE vkMapMemory;\r
- PFN_vkUnmapMemory VMA_NULLABLE vkUnmapMemory;\r
- PFN_vkFlushMappedMemoryRanges VMA_NULLABLE vkFlushMappedMemoryRanges;\r
- PFN_vkInvalidateMappedMemoryRanges VMA_NULLABLE vkInvalidateMappedMemoryRanges;\r
- PFN_vkBindBufferMemory VMA_NULLABLE vkBindBufferMemory;\r
- PFN_vkBindImageMemory VMA_NULLABLE vkBindImageMemory;\r
- PFN_vkGetBufferMemoryRequirements VMA_NULLABLE vkGetBufferMemoryRequirements;\r
- PFN_vkGetImageMemoryRequirements VMA_NULLABLE vkGetImageMemoryRequirements;\r
- PFN_vkCreateBuffer VMA_NULLABLE vkCreateBuffer;\r
- PFN_vkDestroyBuffer VMA_NULLABLE vkDestroyBuffer;\r
- PFN_vkCreateImage VMA_NULLABLE vkCreateImage;\r
- PFN_vkDestroyImage VMA_NULLABLE vkDestroyImage;\r
- PFN_vkCmdCopyBuffer VMA_NULLABLE vkCmdCopyBuffer;\r
-#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- PFN_vkGetBufferMemoryRequirements2KHR VMA_NULLABLE vkGetBufferMemoryRequirements2KHR;\r
- PFN_vkGetImageMemoryRequirements2KHR VMA_NULLABLE vkGetImageMemoryRequirements2KHR;\r
-#endif\r
-#if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000\r
- PFN_vkBindBufferMemory2KHR VMA_NULLABLE vkBindBufferMemory2KHR;\r
- PFN_vkBindImageMemory2KHR VMA_NULLABLE vkBindImageMemory2KHR;\r
-#endif\r
-#if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000\r
- PFN_vkGetPhysicalDeviceMemoryProperties2KHR VMA_NULLABLE vkGetPhysicalDeviceMemoryProperties2KHR;\r
-#endif\r
-} VmaVulkanFunctions;\r
-\r
-/// Flags to be used in VmaRecordSettings::flags.\r
-typedef enum VmaRecordFlagBits {\r
- /** \brief Enables flush after recording every function call.\r
-\r
- Enable it if you expect your application to crash, which may leave recording file truncated.\r
- It may degrade performance though.\r
- */\r
- VMA_RECORD_FLUSH_AFTER_CALL_BIT = 0x00000001,\r
- \r
- VMA_RECORD_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF\r
-} VmaRecordFlagBits;\r
-typedef VkFlags VmaRecordFlags;\r
-\r
-/// Parameters for recording calls to VMA functions. To be used in VmaAllocatorCreateInfo::pRecordSettings.\r
-typedef struct VmaRecordSettings\r
-{\r
- /// Flags for recording. Use #VmaRecordFlagBits enum.\r
- VmaRecordFlags flags;\r
- /** \brief Path to the file that should be written by the recording.\r
-\r
- Suggested extension: "csv".\r
- If the file already exists, it will be overwritten.\r
- It will be opened for the whole time #VmaAllocator object is alive.\r
- If opening this file fails, creation of the whole allocator object fails.\r
- */\r
- const char* VMA_NOT_NULL pFilePath;\r
-} VmaRecordSettings;\r
-\r
-/// Description of a Allocator to be created.\r
-typedef struct VmaAllocatorCreateInfo\r
-{\r
- /// Flags for created allocator. Use #VmaAllocatorCreateFlagBits enum.\r
- VmaAllocatorCreateFlags flags;\r
- /// Vulkan physical device.\r
- /** It must be valid throughout whole lifetime of created allocator. */\r
- VkPhysicalDevice VMA_NOT_NULL physicalDevice;\r
- /// Vulkan device.\r
- /** It must be valid throughout whole lifetime of created allocator. */\r
- VkDevice VMA_NOT_NULL device;\r
- /// Preferred size of a single `VkDeviceMemory` block to be allocated from large heaps > 1 GiB. Optional.\r
- /** Set to 0 to use default, which is currently 256 MiB. */\r
- VkDeviceSize preferredLargeHeapBlockSize;\r
- /// Custom CPU memory allocation callbacks. Optional.\r
- /** Optional, can be null. When specified, will also be used for all CPU-side memory allocations. */\r
- const VkAllocationCallbacks* VMA_NULLABLE pAllocationCallbacks;\r
- /// Informative callbacks for `vkAllocateMemory`, `vkFreeMemory`. Optional.\r
- /** Optional, can be null. */\r
- const VmaDeviceMemoryCallbacks* VMA_NULLABLE pDeviceMemoryCallbacks;\r
- /** \brief Maximum number of additional frames that are in use at the same time as current frame.\r
-\r
- This value is used only when you make allocations with\r
- VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocation cannot become\r
- lost if allocation.lastUseFrameIndex >= allocator.currentFrameIndex - frameInUseCount.\r
-\r
- For example, if you double-buffer your command buffers, so resources used for\r
- rendering in previous frame may still be in use by the GPU at the moment you\r
- allocate resources needed for the current frame, set this value to 1.\r
-\r
- If you want to allow any allocations other than used in the current frame to\r
- become lost, set this value to 0.\r
- */\r
- uint32_t frameInUseCount;\r
- /** \brief Either null or a pointer to an array of limits on maximum number of bytes that can be allocated out of particular Vulkan memory heap.\r
-\r
- If not NULL, it must be a pointer to an array of\r
- `VkPhysicalDeviceMemoryProperties::memoryHeapCount` elements, defining limit on\r
- maximum number of bytes that can be allocated out of particular Vulkan memory\r
- heap.\r
-\r
- Any of the elements may be equal to `VK_WHOLE_SIZE`, which means no limit on that\r
- heap. This is also the default in case of `pHeapSizeLimit` = NULL.\r
-\r
- If there is a limit defined for a heap:\r
-\r
- - If user tries to allocate more memory from that heap using this allocator,\r
- the allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.\r
- - If the limit is smaller than heap size reported in `VkMemoryHeap::size`, the\r
- value of this limit will be reported instead when using vmaGetMemoryProperties().\r
-\r
- Warning! Using this feature may not be equivalent to installing a GPU with\r
- smaller amount of memory, because graphics driver doesn't necessary fail new\r
- allocations with `VK_ERROR_OUT_OF_DEVICE_MEMORY` result when memory capacity is\r
- exceeded. It may return success and just silently migrate some device memory\r
- blocks to system RAM. This driver behavior can also be controlled using\r
- VK_AMD_memory_overallocation_behavior extension.\r
- */\r
- const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL("VkPhysicalDeviceMemoryProperties::memoryHeapCount") pHeapSizeLimit;\r
-\r
- /** \brief Pointers to Vulkan functions. Can be null.\r
-\r
- For details see [Pointers to Vulkan functions](@ref config_Vulkan_functions).\r
- */\r
- const VmaVulkanFunctions* VMA_NULLABLE pVulkanFunctions;\r
- /** \brief Parameters for recording of VMA calls. Can be null.\r
-\r
- If not null, it enables recording of calls to VMA functions to a file.\r
- If support for recording is not enabled using `VMA_RECORDING_ENABLED` macro,\r
- creation of the allocator object fails with `VK_ERROR_FEATURE_NOT_PRESENT`.\r
- */\r
- const VmaRecordSettings* VMA_NULLABLE pRecordSettings;\r
- /** \brief Handle to Vulkan instance object.\r
-\r
- Starting from version 3.0.0 this member is no longer optional, it must be set!\r
- */\r
- VkInstance VMA_NOT_NULL instance;\r
- /** \brief Optional. The highest version of Vulkan that the application is designed to use.\r
- \r
- It must be a value in the format as created by macro `VK_MAKE_VERSION` or a constant like: `VK_API_VERSION_1_1`, `VK_API_VERSION_1_0`.\r
- The patch version number specified is ignored. Only the major and minor versions are considered.\r
- It must be less or equal (preferably equal) to value as passed to `vkCreateInstance` as `VkApplicationInfo::apiVersion`.\r
- Only versions 1.0 and 1.1 are supported by the current implementation.\r
- Leaving it initialized to zero is equivalent to `VK_API_VERSION_1_0`.\r
- */\r
- uint32_t vulkanApiVersion;\r
-} VmaAllocatorCreateInfo;\r
-\r
-/// Creates Allocator object.\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateAllocator(\r
- const VmaAllocatorCreateInfo* VMA_NOT_NULL pCreateInfo,\r
- VmaAllocator VMA_NULLABLE * VMA_NOT_NULL pAllocator);\r
-\r
-/// Destroys allocator object.\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyAllocator(\r
- VmaAllocator VMA_NULLABLE allocator);\r
-\r
-/** \brief Information about existing #VmaAllocator object.\r
-*/\r
-typedef struct VmaAllocatorInfo\r
-{\r
- /** \brief Handle to Vulkan instance object.\r
-\r
- This is the same value as has been passed through VmaAllocatorCreateInfo::instance.\r
- */\r
- VkInstance VMA_NOT_NULL instance;\r
- /** \brief Handle to Vulkan physical device object.\r
-\r
- This is the same value as has been passed through VmaAllocatorCreateInfo::physicalDevice.\r
- */\r
- VkPhysicalDevice VMA_NOT_NULL physicalDevice;\r
- /** \brief Handle to Vulkan device object.\r
-\r
- This is the same value as has been passed through VmaAllocatorCreateInfo::device.\r
- */\r
- VkDevice VMA_NOT_NULL device;\r
-} VmaAllocatorInfo;\r
-\r
-/** \brief Returns information about existing #VmaAllocator object - handle to Vulkan device etc.\r
-\r
-It might be useful if you want to keep just the #VmaAllocator handle and fetch other required handles to\r
-`VkPhysicalDevice`, `VkDevice` etc. every time using this function.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocatorInfo(VmaAllocator VMA_NOT_NULL allocator, VmaAllocatorInfo* VMA_NOT_NULL pAllocatorInfo);\r
-\r
-/**\r
-PhysicalDeviceProperties are fetched from physicalDevice by the allocator.\r
-You can access it here, without fetching it again on your own.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetPhysicalDeviceProperties(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkPhysicalDeviceProperties* VMA_NULLABLE * VMA_NOT_NULL ppPhysicalDeviceProperties);\r
-\r
-/**\r
-PhysicalDeviceMemoryProperties are fetched from physicalDevice by the allocator.\r
-You can access it here, without fetching it again on your own.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryProperties(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkPhysicalDeviceMemoryProperties* VMA_NULLABLE * VMA_NOT_NULL ppPhysicalDeviceMemoryProperties);\r
-\r
-/**\r
-\brief Given Memory Type Index, returns Property Flags of this memory type.\r
-\r
-This is just a convenience function. Same information can be obtained using\r
-vmaGetMemoryProperties().\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryTypeProperties(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t memoryTypeIndex,\r
- VkMemoryPropertyFlags* VMA_NOT_NULL pFlags);\r
-\r
-/** \brief Sets index of the current frame.\r
-\r
-This function must be used if you make allocations with\r
-#VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT and\r
-#VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flags to inform the allocator\r
-when a new frame begins. Allocations queried using vmaGetAllocationInfo() cannot\r
-become lost in the current frame.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaSetCurrentFrameIndex(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t frameIndex);\r
-\r
-/** \brief Calculated statistics of memory usage in entire allocator.\r
-*/\r
-typedef struct VmaStatInfo\r
-{\r
- /// Number of `VkDeviceMemory` Vulkan memory blocks allocated.\r
- uint32_t blockCount;\r
- /// Number of #VmaAllocation allocation objects allocated.\r
- uint32_t allocationCount;\r
- /// Number of free ranges of memory between allocations.\r
- uint32_t unusedRangeCount;\r
- /// Total number of bytes occupied by all allocations.\r
- VkDeviceSize usedBytes;\r
- /// Total number of bytes occupied by unused ranges.\r
- VkDeviceSize unusedBytes;\r
- VkDeviceSize allocationSizeMin, allocationSizeAvg, allocationSizeMax;\r
- VkDeviceSize unusedRangeSizeMin, unusedRangeSizeAvg, unusedRangeSizeMax;\r
-} VmaStatInfo;\r
-\r
-/// General statistics from current state of Allocator.\r
-typedef struct VmaStats\r
-{\r
- VmaStatInfo memoryType[VK_MAX_MEMORY_TYPES];\r
- VmaStatInfo memoryHeap[VK_MAX_MEMORY_HEAPS];\r
- VmaStatInfo total;\r
-} VmaStats;\r
-\r
-/** \brief Retrieves statistics from current state of the Allocator.\r
-\r
-This function is called "calculate" not "get" because it has to traverse all\r
-internal data structures, so it may be quite slow. For faster but more brief statistics\r
-suitable to be called every frame or every allocation, use vmaGetBudget().\r
-\r
-Note that when using allocator from multiple threads, returned information may immediately\r
-become outdated.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaCalculateStats(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaStats* VMA_NOT_NULL pStats);\r
-\r
-/** \brief Statistics of current memory usage and available budget, in bytes, for specific memory heap.\r
-*/\r
-typedef struct VmaBudget\r
-{\r
- /** \brief Sum size of all `VkDeviceMemory` blocks allocated from particular heap, in bytes.\r
- */\r
- VkDeviceSize blockBytes;\r
- \r
- /** \brief Sum size of all allocations created in particular heap, in bytes.\r
- \r
- Usually less or equal than `blockBytes`.\r
- Difference `blockBytes - allocationBytes` is the amount of memory allocated but unused -\r
- available for new allocations or wasted due to fragmentation.\r
- \r
- It might be greater than `blockBytes` if there are some allocations in lost state, as they account\r
- to this value as well.\r
- */\r
- VkDeviceSize allocationBytes;\r
- \r
- /** \brief Estimated current memory usage of the program, in bytes.\r
- \r
- Fetched from system using `VK_EXT_memory_budget` extension if enabled.\r
- \r
- It might be different than `blockBytes` (usually higher) due to additional implicit objects\r
- also occupying the memory, like swapchain, pipelines, descriptor heaps, command buffers, or\r
- `VkDeviceMemory` blocks allocated outside of this library, if any.\r
- */\r
- VkDeviceSize usage;\r
- \r
- /** \brief Estimated amount of memory available to the program, in bytes.\r
- \r
- Fetched from system using `VK_EXT_memory_budget` extension if enabled.\r
- \r
- It might be different (most probably smaller) than `VkMemoryHeap::size[heapIndex]` due to factors\r
- external to the program, like other programs also consuming system resources.\r
- Difference `budget - usage` is the amount of additional memory that can probably\r
- be allocated without problems. Exceeding the budget may result in various problems.\r
- */\r
- VkDeviceSize budget;\r
-} VmaBudget;\r
-\r
-/** \brief Retrieves information about current memory budget for all memory heaps.\r
-\r
-\param[out] pBudget Must point to array with number of elements at least equal to number of memory heaps in physical device used.\r
-\r
-This function is called "get" not "calculate" because it is very fast, suitable to be called\r
-every frame or every allocation. For more detailed statistics use vmaCalculateStats().\r
-\r
-Note that when using allocator from multiple threads, returned information may immediately\r
-become outdated.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetBudget(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaBudget* VMA_NOT_NULL pBudget);\r
-\r
-#ifndef VMA_STATS_STRING_ENABLED\r
-#define VMA_STATS_STRING_ENABLED 1\r
-#endif\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-/// Builds and returns statistics as string in JSON format.\r
-/** @param[out] ppStatsString Must be freed using vmaFreeStatsString() function.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaBuildStatsString(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- char* VMA_NULLABLE * VMA_NOT_NULL ppStatsString,\r
- VkBool32 detailedMap);\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- char* VMA_NULLABLE pStatsString);\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-/** \struct VmaPool\r
-\brief Represents custom memory pool\r
-\r
-Fill structure VmaPoolCreateInfo and call function vmaCreatePool() to create it.\r
-Call function vmaDestroyPool() to destroy it.\r
-\r
-For more information see [Custom memory pools](@ref choosing_memory_type_custom_memory_pools).\r
-*/\r
-VK_DEFINE_HANDLE(VmaPool)\r
-\r
-typedef enum VmaMemoryUsage\r
-{\r
- /** No intended memory usage specified.\r
- Use other members of VmaAllocationCreateInfo to specify your requirements.\r
- */\r
- VMA_MEMORY_USAGE_UNKNOWN = 0,\r
- /** Memory will be used on device only, so fast access from the device is preferred.\r
- It usually means device-local GPU (video) memory.\r
- No need to be mappable on host.\r
- It is roughly equivalent of `D3D12_HEAP_TYPE_DEFAULT`.\r
-\r
- Usage:\r
- \r
- - Resources written and read by device, e.g. images used as attachments.\r
- - Resources transferred from host once (immutable) or infrequently and read by\r
- device multiple times, e.g. textures to be sampled, vertex buffers, uniform\r
- (constant) buffers, and majority of other types of resources used on GPU.\r
-\r
- Allocation may still end up in `HOST_VISIBLE` memory on some implementations.\r
- In such case, you are free to map it.\r
- You can use #VMA_ALLOCATION_CREATE_MAPPED_BIT with this usage type.\r
- */\r
- VMA_MEMORY_USAGE_GPU_ONLY = 1,\r
- /** Memory will be mappable on host.\r
- It usually means CPU (system) memory.\r
- Guarantees to be `HOST_VISIBLE` and `HOST_COHERENT`.\r
- CPU access is typically uncached. Writes may be write-combined.\r
- Resources created in this pool may still be accessible to the device, but access to them can be slow.\r
- It is roughly equivalent of `D3D12_HEAP_TYPE_UPLOAD`.\r
-\r
- Usage: Staging copy of resources used as transfer source.\r
- */\r
- VMA_MEMORY_USAGE_CPU_ONLY = 2,\r
- /**\r
- Memory that is both mappable on host (guarantees to be `HOST_VISIBLE`) and preferably fast to access by GPU.\r
- CPU access is typically uncached. Writes may be write-combined.\r
-\r
- Usage: Resources written frequently by host (dynamic), read by device. E.g. textures (with LINEAR layout), vertex buffers, uniform buffers updated every frame or every draw call.\r
- */\r
- VMA_MEMORY_USAGE_CPU_TO_GPU = 3,\r
- /** Memory mappable on host (guarantees to be `HOST_VISIBLE`) and cached.\r
- It is roughly equivalent of `D3D12_HEAP_TYPE_READBACK`.\r
-\r
- Usage:\r
-\r
- - Resources written by device, read by host - results of some computations, e.g. screen capture, average scene luminance for HDR tone mapping.\r
- - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection.\r
- */\r
- VMA_MEMORY_USAGE_GPU_TO_CPU = 4,\r
- /** CPU memory - memory that is preferably not `DEVICE_LOCAL`, but also not guaranteed to be `HOST_VISIBLE`.\r
-\r
- Usage: Staging copy of resources moved from GPU memory to CPU memory as part\r
- of custom paging/residency mechanism, to be moved back to GPU memory when needed.\r
- */\r
- VMA_MEMORY_USAGE_CPU_COPY = 5,\r
- /** Lazily allocated GPU memory having `VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT`.\r
- Exists mostly on mobile platforms. Using it on desktop PC or other GPUs with no such memory type present will fail the allocation.\r
- \r
- Usage: Memory for transient attachment images (color attachments, depth attachments etc.), created with `VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT`.\r
-\r
- Allocations with this usage are always created as dedicated - it implies #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.\r
- */\r
- VMA_MEMORY_USAGE_GPU_LAZILY_ALLOCATED = 6,\r
-\r
- VMA_MEMORY_USAGE_MAX_ENUM = 0x7FFFFFFF\r
-} VmaMemoryUsage;\r
-\r
-/// Flags to be passed as VmaAllocationCreateInfo::flags.\r
-typedef enum VmaAllocationCreateFlagBits {\r
- /** \brief Set this flag if the allocation should have its own memory block.\r
- \r
- Use it for special, big resources, like fullscreen images used as attachments.\r
- \r
- You should not use this flag if VmaAllocationCreateInfo::pool is not null.\r
- */\r
- VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT = 0x00000001,\r
-\r
- /** \brief Set this flag to only try to allocate from existing `VkDeviceMemory` blocks and never create new such block.\r
- \r
- If new allocation cannot be placed in any of the existing blocks, allocation\r
- fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY` error.\r
- \r
- You should not use #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT and\r
- #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT at the same time. It makes no sense.\r
- \r
- If VmaAllocationCreateInfo::pool is not null, this flag is implied and ignored. */\r
- VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT = 0x00000002,\r
- /** \brief Set this flag to use a memory that will be persistently mapped and retrieve pointer to it.\r
- \r
- Pointer to mapped memory will be returned through VmaAllocationInfo::pMappedData.\r
-\r
- Is it valid to use this flag for allocation made from memory type that is not\r
- `HOST_VISIBLE`. This flag is then ignored and memory is not mapped. This is\r
- useful if you need an allocation that is efficient to use on GPU\r
- (`DEVICE_LOCAL`) and still want to map it directly if possible on platforms that\r
- support it (e.g. Intel GPU).\r
-\r
- You should not use this flag together with #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT.\r
- */\r
- VMA_ALLOCATION_CREATE_MAPPED_BIT = 0x00000004,\r
- /** Allocation created with this flag can become lost as a result of another\r
- allocation with #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flag, so you\r
- must check it before use.\r
-\r
- To check if allocation is not lost, call vmaGetAllocationInfo() and check if\r
- VmaAllocationInfo::deviceMemory is not `VK_NULL_HANDLE`.\r
-\r
- For details about supporting lost allocations, see Lost Allocations\r
- chapter of User Guide on Main Page.\r
-\r
- You should not use this flag together with #VMA_ALLOCATION_CREATE_MAPPED_BIT.\r
- */\r
- VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT = 0x00000008,\r
- /** While creating allocation using this flag, other allocations that were\r
- created with flag #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT can become lost.\r
-\r
- For details about supporting lost allocations, see Lost Allocations\r
- chapter of User Guide on Main Page.\r
- */\r
- VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT = 0x00000010,\r
- /** Set this flag to treat VmaAllocationCreateInfo::pUserData as pointer to a\r
- null-terminated string. Instead of copying pointer value, a local copy of the\r
- string is made and stored in allocation's `pUserData`. The string is automatically\r
- freed together with the allocation. It is also used in vmaBuildStatsString().\r
- */\r
- VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT = 0x00000020,\r
- /** Allocation will be created from upper stack in a double stack pool.\r
-\r
- This flag is only allowed for custom pools created with #VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT flag.\r
- */\r
- VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT = 0x00000040,\r
- /** Create both buffer/image and allocation, but don't bind them together.\r
- It is useful when you want to bind yourself to do some more advanced binding, e.g. using some extensions.\r
- The flag is meaningful only with functions that bind by default: vmaCreateBuffer(), vmaCreateImage().\r
- Otherwise it is ignored.\r
- */\r
- VMA_ALLOCATION_CREATE_DONT_BIND_BIT = 0x00000080,\r
- /** Create allocation only if additional device memory required for it, if any, won't exceed\r
- memory budget. Otherwise return `VK_ERROR_OUT_OF_DEVICE_MEMORY`.\r
- */\r
- VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT = 0x00000100,\r
-\r
- /** Allocation strategy that chooses smallest possible free range for the\r
- allocation.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT = 0x00010000,\r
- /** Allocation strategy that chooses biggest possible free range for the\r
- allocation.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT = 0x00020000,\r
- /** Allocation strategy that chooses first suitable free range for the\r
- allocation.\r
-\r
- "First" doesn't necessarily means the one with smallest offset in memory,\r
- but rather the one that is easiest and fastest to find.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT = 0x00040000,\r
-\r
- /** Allocation strategy that tries to minimize memory usage.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_MIN_MEMORY_BIT = VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT,\r
- /** Allocation strategy that tries to minimize allocation time.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_MIN_TIME_BIT = VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT,\r
- /** Allocation strategy that tries to minimize memory fragmentation.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_MIN_FRAGMENTATION_BIT = VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT,\r
-\r
- /** A bit mask to extract only `STRATEGY` bits from entire set of flags.\r
- */\r
- VMA_ALLOCATION_CREATE_STRATEGY_MASK =\r
- VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT |\r
- VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT |\r
- VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT,\r
-\r
- VMA_ALLOCATION_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF\r
-} VmaAllocationCreateFlagBits;\r
-typedef VkFlags VmaAllocationCreateFlags;\r
-\r
-typedef struct VmaAllocationCreateInfo\r
-{\r
- /// Use #VmaAllocationCreateFlagBits enum.\r
- VmaAllocationCreateFlags flags;\r
- /** \brief Intended usage of memory.\r
- \r
- You can leave #VMA_MEMORY_USAGE_UNKNOWN if you specify memory requirements in other way. \n\r
- If `pool` is not null, this member is ignored.\r
- */\r
- VmaMemoryUsage usage;\r
- /** \brief Flags that must be set in a Memory Type chosen for an allocation.\r
- \r
- Leave 0 if you specify memory requirements in other way. \n\r
- If `pool` is not null, this member is ignored.*/\r
- VkMemoryPropertyFlags requiredFlags;\r
- /** \brief Flags that preferably should be set in a memory type chosen for an allocation.\r
- \r
- Set to 0 if no additional flags are prefered. \n\r
- If `pool` is not null, this member is ignored. */\r
- VkMemoryPropertyFlags preferredFlags;\r
- /** \brief Bitmask containing one bit set for every memory type acceptable for this allocation.\r
-\r
- Value 0 is equivalent to `UINT32_MAX` - it means any memory type is accepted if\r
- it meets other requirements specified by this structure, with no further\r
- restrictions on memory type index. \n\r
- If `pool` is not null, this member is ignored.\r
- */\r
- uint32_t memoryTypeBits;\r
- /** \brief Pool that this allocation should be created in.\r
-\r
- Leave `VK_NULL_HANDLE` to allocate from default pool. If not null, members:\r
- `usage`, `requiredFlags`, `preferredFlags`, `memoryTypeBits` are ignored.\r
- */\r
- VmaPool VMA_NULLABLE pool;\r
- /** \brief Custom general-purpose pointer that will be stored in #VmaAllocation, can be read as VmaAllocationInfo::pUserData and changed using vmaSetAllocationUserData().\r
- \r
- If #VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is used, it must be either\r
- null or pointer to a null-terminated string. The string will be then copied to\r
- internal buffer, so it doesn't need to be valid after allocation call.\r
- */\r
- void* VMA_NULLABLE pUserData;\r
-} VmaAllocationCreateInfo;\r
-\r
-/**\r
-\brief Helps to find memoryTypeIndex, given memoryTypeBits and VmaAllocationCreateInfo.\r
-\r
-This algorithm tries to find a memory type that:\r
-\r
-- Is allowed by memoryTypeBits.\r
-- Contains all the flags from pAllocationCreateInfo->requiredFlags.\r
-- Matches intended usage.\r
-- Has as many flags from pAllocationCreateInfo->preferredFlags as possible.\r
-\r
-\return Returns VK_ERROR_FEATURE_NOT_PRESENT if not found. Receiving such result\r
-from this function or any other allocating function probably means that your\r
-device doesn't support any memory type with requested features for the specific\r
-type of resource you want to use it for. Please check parameters of your\r
-resource, like image layout (OPTIMAL versus LINEAR) or mip level count.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndex(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t memoryTypeBits,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo,\r
- uint32_t* VMA_NOT_NULL pMemoryTypeIndex);\r
-\r
-/**\r
-\brief Helps to find memoryTypeIndex, given VkBufferCreateInfo and VmaAllocationCreateInfo.\r
-\r
-It can be useful e.g. to determine value to be used as VmaPoolCreateInfo::memoryTypeIndex.\r
-It internally creates a temporary, dummy buffer that never has memory bound.\r
-It is just a convenience function, equivalent to calling:\r
-\r
-- `vkCreateBuffer`\r
-- `vkGetBufferMemoryRequirements`\r
-- `vmaFindMemoryTypeIndex`\r
-- `vkDestroyBuffer`\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkBufferCreateInfo* VMA_NOT_NULL pBufferCreateInfo,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo,\r
- uint32_t* VMA_NOT_NULL pMemoryTypeIndex);\r
-\r
-/**\r
-\brief Helps to find memoryTypeIndex, given VkImageCreateInfo and VmaAllocationCreateInfo.\r
-\r
-It can be useful e.g. to determine value to be used as VmaPoolCreateInfo::memoryTypeIndex.\r
-It internally creates a temporary, dummy image that never has memory bound.\r
-It is just a convenience function, equivalent to calling:\r
-\r
-- `vkCreateImage`\r
-- `vkGetImageMemoryRequirements`\r
-- `vmaFindMemoryTypeIndex`\r
-- `vkDestroyImage`\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkImageCreateInfo* VMA_NOT_NULL pImageCreateInfo,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo,\r
- uint32_t* VMA_NOT_NULL pMemoryTypeIndex);\r
-\r
-/// Flags to be passed as VmaPoolCreateInfo::flags.\r
-typedef enum VmaPoolCreateFlagBits {\r
- /** \brief Use this flag if you always allocate only buffers and linear images or only optimal images out of this pool and so Buffer-Image Granularity can be ignored.\r
-\r
- This is an optional optimization flag.\r
-\r
- If you always allocate using vmaCreateBuffer(), vmaCreateImage(),\r
- vmaAllocateMemoryForBuffer(), then you don't need to use it because allocator\r
- knows exact type of your allocations so it can handle Buffer-Image Granularity\r
- in the optimal way.\r
-\r
- If you also allocate using vmaAllocateMemoryForImage() or vmaAllocateMemory(),\r
- exact type of such allocations is not known, so allocator must be conservative\r
- in handling Buffer-Image Granularity, which can lead to suboptimal allocation\r
- (wasted memory). In that case, if you can make sure you always allocate only\r
- buffers and linear images or only optimal images out of this pool, use this flag\r
- to make allocator disregard Buffer-Image Granularity and so make allocations\r
- faster and more optimal.\r
- */\r
- VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT = 0x00000002,\r
-\r
- /** \brief Enables alternative, linear allocation algorithm in this pool.\r
-\r
- Specify this flag to enable linear allocation algorithm, which always creates\r
- new allocations after last one and doesn't reuse space from allocations freed in\r
- between. It trades memory consumption for simplified algorithm and data\r
- structure, which has better performance and uses less memory for metadata.\r
-\r
- By using this flag, you can achieve behavior of free-at-once, stack,\r
- ring buffer, and double stack. For details, see documentation chapter\r
- \ref linear_algorithm.\r
-\r
- When using this flag, you must specify VmaPoolCreateInfo::maxBlockCount == 1 (or 0 for default).\r
-\r
- For more details, see [Linear allocation algorithm](@ref linear_algorithm).\r
- */\r
- VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT = 0x00000004,\r
-\r
- /** \brief Enables alternative, buddy allocation algorithm in this pool.\r
-\r
- It operates on a tree of blocks, each having size that is a power of two and\r
- a half of its parent's size. Comparing to default algorithm, this one provides\r
- faster allocation and deallocation and decreased external fragmentation,\r
- at the expense of more memory wasted (internal fragmentation).\r
-\r
- For more details, see [Buddy allocation algorithm](@ref buddy_algorithm).\r
- */\r
- VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT = 0x00000008,\r
-\r
- /** Bit mask to extract only `ALGORITHM` bits from entire set of flags.\r
- */\r
- VMA_POOL_CREATE_ALGORITHM_MASK =\r
- VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT |\r
- VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT,\r
-\r
- VMA_POOL_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF\r
-} VmaPoolCreateFlagBits;\r
-typedef VkFlags VmaPoolCreateFlags;\r
-\r
-/** \brief Describes parameter of created #VmaPool.\r
-*/\r
-typedef struct VmaPoolCreateInfo {\r
- /** \brief Vulkan memory type index to allocate this pool from.\r
- */\r
- uint32_t memoryTypeIndex;\r
- /** \brief Use combination of #VmaPoolCreateFlagBits.\r
- */\r
- VmaPoolCreateFlags flags;\r
- /** \brief Size of a single `VkDeviceMemory` block to be allocated as part of this pool, in bytes. Optional.\r
-\r
- Specify nonzero to set explicit, constant size of memory blocks used by this\r
- pool.\r
-\r
- Leave 0 to use default and let the library manage block sizes automatically.\r
- Sizes of particular blocks may vary.\r
- */\r
- VkDeviceSize blockSize;\r
- /** \brief Minimum number of blocks to be always allocated in this pool, even if they stay empty.\r
-\r
- Set to 0 to have no preallocated blocks and allow the pool be completely empty.\r
- */\r
- size_t minBlockCount;\r
- /** \brief Maximum number of blocks that can be allocated in this pool. Optional.\r
-\r
- Set to 0 to use default, which is `SIZE_MAX`, which means no limit.\r
- \r
- Set to same value as VmaPoolCreateInfo::minBlockCount to have fixed amount of memory allocated\r
- throughout whole lifetime of this pool.\r
- */\r
- size_t maxBlockCount;\r
- /** \brief Maximum number of additional frames that are in use at the same time as current frame.\r
-\r
- This value is used only when you make allocations with\r
- #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocation cannot become\r
- lost if allocation.lastUseFrameIndex >= allocator.currentFrameIndex - frameInUseCount.\r
-\r
- For example, if you double-buffer your command buffers, so resources used for\r
- rendering in previous frame may still be in use by the GPU at the moment you\r
- allocate resources needed for the current frame, set this value to 1.\r
-\r
- If you want to allow any allocations other than used in the current frame to\r
- become lost, set this value to 0.\r
- */\r
- uint32_t frameInUseCount;\r
-} VmaPoolCreateInfo;\r
-\r
-/** \brief Describes parameter of existing #VmaPool.\r
-*/\r
-typedef struct VmaPoolStats {\r
- /** \brief Total amount of `VkDeviceMemory` allocated from Vulkan for this pool, in bytes.\r
- */\r
- VkDeviceSize size;\r
- /** \brief Total number of bytes in the pool not used by any #VmaAllocation.\r
- */\r
- VkDeviceSize unusedSize;\r
- /** \brief Number of #VmaAllocation objects created from this pool that were not destroyed or lost.\r
- */\r
- size_t allocationCount;\r
- /** \brief Number of continuous memory ranges in the pool not used by any #VmaAllocation.\r
- */\r
- size_t unusedRangeCount;\r
- /** \brief Size of the largest continuous free memory region available for new allocation.\r
-\r
- Making a new allocation of that size is not guaranteed to succeed because of\r
- possible additional margin required to respect alignment and buffer/image\r
- granularity.\r
- */\r
- VkDeviceSize unusedRangeSizeMax;\r
- /** \brief Number of `VkDeviceMemory` blocks allocated for this pool.\r
- */\r
- size_t blockCount;\r
-} VmaPoolStats;\r
-\r
-/** \brief Allocates Vulkan device memory and creates #VmaPool object.\r
-\r
-@param allocator Allocator object.\r
-@param pCreateInfo Parameters of pool to create.\r
-@param[out] pPool Handle to created pool.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreatePool(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VmaPoolCreateInfo* VMA_NOT_NULL pCreateInfo,\r
- VmaPool VMA_NULLABLE * VMA_NOT_NULL pPool);\r
-\r
-/** \brief Destroys #VmaPool object and frees Vulkan device memory.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyPool(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaPool VMA_NULLABLE pool);\r
-\r
-/** \brief Retrieves statistics of existing #VmaPool object.\r
-\r
-@param allocator Allocator object.\r
-@param pool Pool object.\r
-@param[out] pPoolStats Statistics of specified pool.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolStats(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaPool VMA_NOT_NULL pool,\r
- VmaPoolStats* VMA_NOT_NULL pPoolStats);\r
-\r
-/** \brief Marks all allocations in given pool as lost if they are not used in current frame or VmaPoolCreateInfo::frameInUseCount back from now.\r
-\r
-@param allocator Allocator object.\r
-@param pool Pool.\r
-@param[out] pLostAllocationCount Number of allocations marked as lost. Optional - pass null if you don't need this information.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaMakePoolAllocationsLost(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaPool VMA_NOT_NULL pool,\r
- size_t* VMA_NULLABLE pLostAllocationCount);\r
-\r
-/** \brief Checks magic number in margins around all allocations in given memory pool in search for corruptions.\r
-\r
-Corruption detection is enabled only when `VMA_DEBUG_DETECT_CORRUPTION` macro is defined to nonzero,\r
-`VMA_DEBUG_MARGIN` is defined to nonzero and the pool is created in memory type that is\r
-`HOST_VISIBLE` and `HOST_COHERENT`. For more information, see [Corruption detection](@ref debugging_memory_usage_corruption_detection).\r
-\r
-Possible return values:\r
-\r
-- `VK_ERROR_FEATURE_NOT_PRESENT` - corruption detection is not enabled for specified pool.\r
-- `VK_SUCCESS` - corruption detection has been performed and succeeded.\r
-- `VK_ERROR_VALIDATION_FAILED_EXT` - corruption detection has been performed and found memory corruptions around one of the allocations.\r
- `VMA_ASSERT` is also fired in that case.\r
-- Other value: Error returned by Vulkan, e.g. memory mapping failure.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator VMA_NOT_NULL allocator, VmaPool VMA_NOT_NULL pool);\r
-\r
-/** \brief Retrieves name of a custom pool.\r
-\r
-After the call `ppName` is either null or points to an internally-owned null-terminated string\r
-containing name of the pool that was previously set. The pointer becomes invalid when the pool is\r
-destroyed or its name is changed using vmaSetPoolName().\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaPool VMA_NOT_NULL pool,\r
- const char* VMA_NULLABLE * VMA_NOT_NULL ppName);\r
-\r
-/** \brief Sets name of a custom pool.\r
-\r
-`pName` can be either null or pointer to a null-terminated string with new name for the pool.\r
-Function makes internal copy of the string, so it can be changed or freed immediately after this call.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaSetPoolName(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaPool VMA_NOT_NULL pool,\r
- const char* VMA_NULLABLE pName);\r
-\r
-/** \struct VmaAllocation\r
-\brief Represents single memory allocation.\r
-\r
-It may be either dedicated block of `VkDeviceMemory` or a specific region of a bigger block of this type\r
-plus unique offset.\r
-\r
-There are multiple ways to create such object.\r
-You need to fill structure VmaAllocationCreateInfo.\r
-For more information see [Choosing memory type](@ref choosing_memory_type).\r
-\r
-Although the library provides convenience functions that create Vulkan buffer or image,\r
-allocate memory for it and bind them together,\r
-binding of the allocation to a buffer or an image is out of scope of the allocation itself.\r
-Allocation object can exist without buffer/image bound,\r
-binding can be done manually by the user, and destruction of it can be done\r
-independently of destruction of the allocation.\r
-\r
-The object also remembers its size and some other information.\r
-To retrieve this information, use function vmaGetAllocationInfo() and inspect\r
-returned structure VmaAllocationInfo.\r
-\r
-Some kinds allocations can be in lost state.\r
-For more information, see [Lost allocations](@ref lost_allocations).\r
-*/\r
-VK_DEFINE_HANDLE(VmaAllocation)\r
-\r
-/** \brief Parameters of #VmaAllocation objects, that can be retrieved using function vmaGetAllocationInfo().\r
-*/\r
-typedef struct VmaAllocationInfo {\r
- /** \brief Memory type index that this allocation was allocated from.\r
- \r
- It never changes.\r
- */\r
- uint32_t memoryType;\r
- /** \brief Handle to Vulkan memory object.\r
-\r
- Same memory object can be shared by multiple allocations.\r
- \r
- It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost.\r
-\r
- If the allocation is lost, it is equal to `VK_NULL_HANDLE`.\r
- */\r
- VkDeviceMemory VMA_NULLABLE_NON_DISPATCHABLE deviceMemory;\r
- /** \brief Offset into deviceMemory object to the beginning of this allocation, in bytes. (deviceMemory, offset) pair is unique to this allocation.\r
-\r
- It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost.\r
- */\r
- VkDeviceSize offset;\r
- /** \brief Size of this allocation, in bytes.\r
-\r
- It never changes, unless allocation is lost.\r
- */\r
- VkDeviceSize size;\r
- /** \brief Pointer to the beginning of this allocation as mapped data.\r
-\r
- If the allocation hasn't been mapped using vmaMapMemory() and hasn't been\r
- created with #VMA_ALLOCATION_CREATE_MAPPED_BIT flag, this value is null.\r
-\r
- It can change after call to vmaMapMemory(), vmaUnmapMemory().\r
- It can also change after call to vmaDefragment() if this allocation is passed to the function.\r
- */\r
- void* VMA_NULLABLE pMappedData;\r
- /** \brief Custom general-purpose pointer that was passed as VmaAllocationCreateInfo::pUserData or set using vmaSetAllocationUserData().\r
-\r
- It can change after call to vmaSetAllocationUserData() for this allocation.\r
- */\r
- void* VMA_NULLABLE pUserData;\r
-} VmaAllocationInfo;\r
-\r
-/** \brief General purpose memory allocation.\r
-\r
-@param[out] pAllocation Handle to allocated memory.\r
-@param[out] pAllocationInfo Optional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo().\r
-\r
-You should free the memory using vmaFreeMemory() or vmaFreeMemoryPages().\r
-\r
-It is recommended to use vmaAllocateMemoryForBuffer(), vmaAllocateMemoryForImage(),\r
-vmaCreateBuffer(), vmaCreateImage() instead whenever possible.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkMemoryRequirements* VMA_NOT_NULL pVkMemoryRequirements,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation,\r
- VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);\r
-\r
-/** \brief General purpose memory allocation for multiple allocation objects at once.\r
-\r
-@param allocator Allocator object.\r
-@param pVkMemoryRequirements Memory requirements for each allocation.\r
-@param pCreateInfo Creation parameters for each alloction.\r
-@param allocationCount Number of allocations to make.\r
-@param[out] pAllocations Pointer to array that will be filled with handles to created allocations.\r
-@param[out] pAllocationInfo Optional. Pointer to array that will be filled with parameters of created allocations.\r
-\r
-You should free the memory using vmaFreeMemory() or vmaFreeMemoryPages().\r
-\r
-Word "pages" is just a suggestion to use this function to allocate pieces of memory needed for sparse binding.\r
-It is just a general purpose allocation function able to make multiple allocations at once.\r
-It may be internally optimized to be more efficient than calling vmaAllocateMemory() `allocationCount` times.\r
-\r
-All allocations are made using same parameters. All of them are created out of the same memory pool and type.\r
-If any allocation fails, all allocations already made within this function call are also freed, so that when\r
-returned result is not `VK_SUCCESS`, `pAllocation` array is always entirely filled with `VK_NULL_HANDLE`.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkMemoryRequirements* VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pVkMemoryRequirements,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pCreateInfo,\r
- size_t allocationCount,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations,\r
- VmaAllocationInfo* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocationInfo);\r
-\r
-/**\r
-@param[out] pAllocation Handle to allocated memory.\r
-@param[out] pAllocationInfo Optional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo().\r
-\r
-You should free the memory using vmaFreeMemory().\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation,\r
- VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);\r
-\r
-/// Function similar to vmaAllocateMemoryForBuffer().\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VkImage VMA_NOT_NULL_NON_DISPATCHABLE image,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation,\r
- VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);\r
-\r
-/** \brief Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage().\r
-\r
-Passing `VK_NULL_HANDLE` as `allocation` is valid. Such function call is just skipped.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VmaAllocation VMA_NULLABLE allocation);\r
-\r
-/** \brief Frees memory and destroys multiple allocations.\r
-\r
-Word "pages" is just a suggestion to use this function to free pieces of memory used for sparse binding.\r
-It is just a general purpose function to free memory and destroy allocations made using e.g. vmaAllocateMemory(),\r
-vmaAllocateMemoryPages() and other functions.\r
-It may be internally optimized to be more efficient than calling vmaFreeMemory() `allocationCount` times.\r
-\r
-Allocations in `pAllocations` array can come from any memory pools and types.\r
-Passing `VK_NULL_HANDLE` as elements of `pAllocations` array is valid. Such entries are just skipped.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- size_t allocationCount,\r
- const VmaAllocation VMA_NULLABLE * VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations);\r
-\r
-/** \brief Deprecated.\r
-\r
-\deprecated\r
-In version 2.2.0 it used to try to change allocation's size without moving or reallocating it.\r
-In current version it returns `VK_SUCCESS` only if `newSize` equals current allocation's size.\r
-Otherwise returns `VK_ERROR_OUT_OF_POOL_MEMORY`, indicating that allocation's size could not be changed.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaResizeAllocation(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkDeviceSize newSize);\r
-\r
-/** \brief Returns current information about specified allocation and atomically marks it as used in current frame.\r
-\r
-Current paramters of given allocation are returned in `pAllocationInfo`.\r
-\r
-This function also atomically "touches" allocation - marks it as used in current frame,\r
-just like vmaTouchAllocation().\r
-If the allocation is in lost state, `pAllocationInfo->deviceMemory == VK_NULL_HANDLE`.\r
-\r
-Although this function uses atomics and doesn't lock any mutex, so it should be quite efficient,\r
-you can avoid calling it too often.\r
-\r
-- You can retrieve same VmaAllocationInfo structure while creating your resource, from function\r
- vmaCreateBuffer(), vmaCreateImage(). You can remember it if you are sure parameters don't change\r
- (e.g. due to defragmentation or allocation becoming lost).\r
-- If you just want to check if allocation is not lost, vmaTouchAllocation() will work faster.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationInfo(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VmaAllocationInfo* VMA_NOT_NULL pAllocationInfo);\r
-\r
-/** \brief Returns `VK_TRUE` if allocation is not lost and atomically marks it as used in current frame.\r
-\r
-If the allocation has been created with #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag,\r
-this function returns `VK_TRUE` if it's not in lost state, so it can still be used.\r
-It then also atomically "touches" the allocation - marks it as used in current frame,\r
-so that you can be sure it won't become lost in current frame or next `frameInUseCount` frames.\r
-\r
-If the allocation is in lost state, the function returns `VK_FALSE`.\r
-Memory of such allocation, as well as buffer or image bound to it, should not be used.\r
-Lost allocation and the buffer/image still need to be destroyed.\r
-\r
-If the allocation has been created without #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag,\r
-this function always returns `VK_TRUE`.\r
-*/\r
-VMA_CALL_PRE VkBool32 VMA_CALL_POST vmaTouchAllocation(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation);\r
-\r
-/** \brief Sets pUserData in given allocation to new value.\r
-\r
-If the allocation was created with VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT,\r
-pUserData must be either null, or pointer to a null-terminated string. The function\r
-makes local copy of the string and sets it as allocation's `pUserData`. String\r
-passed as pUserData doesn't need to be valid for whole lifetime of the allocation -\r
-you can free it after this call. String previously pointed by allocation's\r
-pUserData is freed from memory.\r
-\r
-If the flag was not used, the value of pointer `pUserData` is just copied to\r
-allocation's `pUserData`. It is opaque, so you can use it however you want - e.g.\r
-as a pointer, ordinal number or some handle to you own data.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- void* VMA_NULLABLE pUserData);\r
-\r
-/** \brief Creates new allocation that is in lost state from the beginning.\r
-\r
-It can be useful if you need a dummy, non-null allocation.\r
-\r
-You still need to destroy created object using vmaFreeMemory().\r
-\r
-Returned allocation is not tied to any specific memory pool or memory type and\r
-not bound to any image or buffer. It has size = 0. It cannot be turned into\r
-a real, non-empty allocation.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaCreateLostAllocation(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation);\r
-\r
-/** \brief Maps memory represented by given allocation and returns pointer to it.\r
-\r
-Maps memory represented by given allocation to make it accessible to CPU code.\r
-When succeeded, `*ppData` contains pointer to first byte of this memory.\r
-If the allocation is part of bigger `VkDeviceMemory` block, the pointer is\r
-correctly offseted to the beginning of region assigned to this particular\r
-allocation.\r
-\r
-Mapping is internally reference-counted and synchronized, so despite raw Vulkan\r
-function `vkMapMemory()` cannot be used to map same block of `VkDeviceMemory`\r
-multiple times simultaneously, it is safe to call this function on allocations\r
-assigned to the same memory block. Actual Vulkan memory will be mapped on first\r
-mapping and unmapped on last unmapping.\r
-\r
-If the function succeeded, you must call vmaUnmapMemory() to unmap the\r
-allocation when mapping is no longer needed or before freeing the allocation, at\r
-the latest.\r
-\r
-It also safe to call this function multiple times on the same allocation. You\r
-must call vmaUnmapMemory() same number of times as you called vmaMapMemory().\r
-\r
-It is also safe to call this function on allocation created with\r
-#VMA_ALLOCATION_CREATE_MAPPED_BIT flag. Its memory stays mapped all the time.\r
-You must still call vmaUnmapMemory() same number of times as you called\r
-vmaMapMemory(). You must not call vmaUnmapMemory() additional time to free the\r
-"0-th" mapping made automatically due to #VMA_ALLOCATION_CREATE_MAPPED_BIT flag.\r
-\r
-This function fails when used on allocation made in memory type that is not\r
-`HOST_VISIBLE`.\r
-\r
-This function always fails when called for allocation that was created with\r
-#VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocations cannot be\r
-mapped.\r
-\r
-This function doesn't automatically flush or invalidate caches.\r
-If the allocation is made from a memory types that is not `HOST_COHERENT`,\r
-you also need to use vmaInvalidateAllocation() / vmaFlushAllocation(), as required by Vulkan specification.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaMapMemory(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- void* VMA_NULLABLE * VMA_NOT_NULL ppData);\r
-\r
-/** \brief Unmaps memory represented by given allocation, mapped previously using vmaMapMemory().\r
-\r
-For details, see description of vmaMapMemory().\r
-\r
-This function doesn't automatically flush or invalidate caches.\r
-If the allocation is made from a memory types that is not `HOST_COHERENT`,\r
-you also need to use vmaInvalidateAllocation() / vmaFlushAllocation(), as required by Vulkan specification.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaUnmapMemory(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation);\r
-\r
-/** \brief Flushes memory of given allocation.\r
-\r
-Calls `vkFlushMappedMemoryRanges()` for memory associated with given range of given allocation.\r
-It needs to be called after writing to a mapped memory for memory types that are not `HOST_COHERENT`.\r
-Unmap operation doesn't do that automatically.\r
-\r
-- `offset` must be relative to the beginning of allocation.\r
-- `size` can be `VK_WHOLE_SIZE`. It means all memory from `offset` the the end of given allocation.\r
-- `offset` and `size` don't have to be aligned.\r
- They are internally rounded down/up to multiply of `nonCoherentAtomSize`.\r
-- If `size` is 0, this call is ignored.\r
-- If memory type that the `allocation` belongs to is not `HOST_VISIBLE` or it is `HOST_COHERENT`,\r
- this call is ignored.\r
-\r
-Warning! `offset` and `size` are relative to the contents of given `allocation`.\r
-If you mean whole allocation, you can pass 0 and `VK_WHOLE_SIZE`, respectively.\r
-Do not pass allocation's offset as `offset`!!!\r
-\r
-This function returns the `VkResult` from `vkFlushMappedMemoryRanges` if it is\r
-called, otherwise `VK_SUCCESS`.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocation(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkDeviceSize offset,\r
- VkDeviceSize size);\r
-\r
-/** \brief Invalidates memory of given allocation.\r
-\r
-Calls `vkInvalidateMappedMemoryRanges()` for memory associated with given range of given allocation.\r
-It needs to be called before reading from a mapped memory for memory types that are not `HOST_COHERENT`.\r
-Map operation doesn't do that automatically.\r
-\r
-- `offset` must be relative to the beginning of allocation.\r
-- `size` can be `VK_WHOLE_SIZE`. It means all memory from `offset` the the end of given allocation.\r
-- `offset` and `size` don't have to be aligned.\r
- They are internally rounded down/up to multiply of `nonCoherentAtomSize`.\r
-- If `size` is 0, this call is ignored.\r
-- If memory type that the `allocation` belongs to is not `HOST_VISIBLE` or it is `HOST_COHERENT`,\r
- this call is ignored.\r
-\r
-Warning! `offset` and `size` are relative to the contents of given `allocation`.\r
-If you mean whole allocation, you can pass 0 and `VK_WHOLE_SIZE`, respectively.\r
-Do not pass allocation's offset as `offset`!!!\r
-\r
-This function returns the `VkResult` from `vkInvalidateMappedMemoryRanges` if\r
-it is called, otherwise `VK_SUCCESS`.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocation(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkDeviceSize offset,\r
- VkDeviceSize size);\r
-\r
-/** \brief Flushes memory of given set of allocations.\r
-\r
-Calls `vkFlushMappedMemoryRanges()` for memory associated with given ranges of given allocations.\r
-For more information, see documentation of vmaFlushAllocation().\r
-\r
-\param allocator\r
-\param allocationCount\r
-\param allocations\r
-\param offsets If not null, it must point to an array of offsets of regions to flush, relative to the beginning of respective allocations. Null means all ofsets are zero.\r
-\param sizes If not null, it must point to an array of sizes of regions to flush in respective allocations. Null means `VK_WHOLE_SIZE` for all allocations.\r
-\r
-This function returns the `VkResult` from `vkFlushMappedMemoryRanges` if it is\r
-called, otherwise `VK_SUCCESS`.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocations(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t allocationCount,\r
- const VmaAllocation VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) allocations,\r
- const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) offsets,\r
- const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) sizes);\r
-\r
-/** \brief Invalidates memory of given set of allocations.\r
-\r
-Calls `vkInvalidateMappedMemoryRanges()` for memory associated with given ranges of given allocations.\r
-For more information, see documentation of vmaInvalidateAllocation().\r
-\r
-\param allocator\r
-\param allocationCount\r
-\param allocations\r
-\param offsets If not null, it must point to an array of offsets of regions to flush, relative to the beginning of respective allocations. Null means all ofsets are zero.\r
-\param sizes If not null, it must point to an array of sizes of regions to flush in respective allocations. Null means `VK_WHOLE_SIZE` for all allocations.\r
-\r
-This function returns the `VkResult` from `vkInvalidateMappedMemoryRanges` if it is\r
-called, otherwise `VK_SUCCESS`.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocations(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- uint32_t allocationCount,\r
- const VmaAllocation VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) allocations,\r
- const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) offsets,\r
- const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) sizes);\r
-\r
-/** \brief Checks magic number in margins around all allocations in given memory types (in both default and custom pools) in search for corruptions.\r
-\r
-@param memoryTypeBits Bit mask, where each bit set means that a memory type with that index should be checked.\r
-\r
-Corruption detection is enabled only when `VMA_DEBUG_DETECT_CORRUPTION` macro is defined to nonzero,\r
-`VMA_DEBUG_MARGIN` is defined to nonzero and only for memory types that are\r
-`HOST_VISIBLE` and `HOST_COHERENT`. For more information, see [Corruption detection](@ref debugging_memory_usage_corruption_detection).\r
-\r
-Possible return values:\r
-\r
-- `VK_ERROR_FEATURE_NOT_PRESENT` - corruption detection is not enabled for any of specified memory types.\r
-- `VK_SUCCESS` - corruption detection has been performed and succeeded.\r
-- `VK_ERROR_VALIDATION_FAILED_EXT` - corruption detection has been performed and found memory corruptions around one of the allocations.\r
- `VMA_ASSERT` is also fired in that case.\r
-- Other value: Error returned by Vulkan, e.g. memory mapping failure.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator VMA_NOT_NULL allocator, uint32_t memoryTypeBits);\r
-\r
-/** \struct VmaDefragmentationContext\r
-\brief Represents Opaque object that represents started defragmentation process.\r
-\r
-Fill structure #VmaDefragmentationInfo2 and call function vmaDefragmentationBegin() to create it.\r
-Call function vmaDefragmentationEnd() to destroy it.\r
-*/\r
-VK_DEFINE_HANDLE(VmaDefragmentationContext)\r
-\r
-/// Flags to be used in vmaDefragmentationBegin(). None at the moment. Reserved for future use.\r
-typedef enum VmaDefragmentationFlagBits {\r
- VMA_DEFRAGMENTATION_FLAG_INCREMENTAL = 0x1,\r
- VMA_DEFRAGMENTATION_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF\r
-} VmaDefragmentationFlagBits;\r
-typedef VkFlags VmaDefragmentationFlags;\r
-\r
-/** \brief Parameters for defragmentation.\r
-\r
-To be used with function vmaDefragmentationBegin().\r
-*/\r
-typedef struct VmaDefragmentationInfo2 {\r
- /** \brief Reserved for future use. Should be 0.\r
- */\r
- VmaDefragmentationFlags flags;\r
- /** \brief Number of allocations in `pAllocations` array.\r
- */\r
- uint32_t allocationCount;\r
- /** \brief Pointer to array of allocations that can be defragmented.\r
-\r
- The array should have `allocationCount` elements.\r
- The array should not contain nulls.\r
- Elements in the array should be unique - same allocation cannot occur twice.\r
- It is safe to pass allocations that are in the lost state - they are ignored.\r
- All allocations not present in this array are considered non-moveable during this defragmentation.\r
- */\r
- const VmaAllocation VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations;\r
- /** \brief Optional, output. Pointer to array that will be filled with information whether the allocation at certain index has been changed during defragmentation.\r
-\r
- The array should have `allocationCount` elements.\r
- You can pass null if you are not interested in this information.\r
- */\r
- VkBool32* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocationsChanged;\r
- /** \brief Numer of pools in `pPools` array.\r
- */\r
- uint32_t poolCount;\r
- /** \brief Either null or pointer to array of pools to be defragmented.\r
-\r
- All the allocations in the specified pools can be moved during defragmentation\r
- and there is no way to check if they were really moved as in `pAllocationsChanged`,\r
- so you must query all the allocations in all these pools for new `VkDeviceMemory`\r
- and offset using vmaGetAllocationInfo() if you might need to recreate buffers\r
- and images bound to them.\r
-\r
- The array should have `poolCount` elements.\r
- The array should not contain nulls.\r
- Elements in the array should be unique - same pool cannot occur twice.\r
-\r
- Using this array is equivalent to specifying all allocations from the pools in `pAllocations`.\r
- It might be more efficient.\r
- */\r
- const VmaPool VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(poolCount) pPools;\r
- /** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places using transfers on CPU side, like `memcpy()`, `memmove()`.\r
- \r
- `VK_WHOLE_SIZE` means no limit.\r
- */\r
- VkDeviceSize maxCpuBytesToMove;\r
- /** \brief Maximum number of allocations that can be moved to a different place using transfers on CPU side, like `memcpy()`, `memmove()`.\r
-\r
- `UINT32_MAX` means no limit.\r
- */\r
- uint32_t maxCpuAllocationsToMove;\r
- /** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places using transfers on GPU side, posted to `commandBuffer`.\r
- \r
- `VK_WHOLE_SIZE` means no limit.\r
- */\r
- VkDeviceSize maxGpuBytesToMove;\r
- /** \brief Maximum number of allocations that can be moved to a different place using transfers on GPU side, posted to `commandBuffer`.\r
-\r
- `UINT32_MAX` means no limit.\r
- */\r
- uint32_t maxGpuAllocationsToMove;\r
- /** \brief Optional. Command buffer where GPU copy commands will be posted.\r
-\r
- If not null, it must be a valid command buffer handle that supports Transfer queue type.\r
- It must be in the recording state and outside of a render pass instance.\r
- You need to submit it and make sure it finished execution before calling vmaDefragmentationEnd().\r
-\r
- Passing null means that only CPU defragmentation will be performed.\r
- */\r
- VkCommandBuffer VMA_NULLABLE commandBuffer;\r
-} VmaDefragmentationInfo2;\r
-\r
-typedef struct VmaDefragmentationPassMoveInfo {\r
- VmaAllocation VMA_NOT_NULL allocation;\r
- VkDeviceMemory VMA_NOT_NULL_NON_DISPATCHABLE memory;\r
- VkDeviceSize offset;\r
-} VmaDefragmentationPassMoveInfo;\r
-\r
-/** \brief Parameters for incremental defragmentation steps.\r
-\r
-To be used with function vmaBeginDefragmentationPass().\r
-*/\r
-typedef struct VmaDefragmentationPassInfo {\r
- uint32_t moveCount;\r
- VmaDefragmentationPassMoveInfo* VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(moveCount) pMoves;\r
-} VmaDefragmentationPassInfo;\r
-\r
-/** \brief Deprecated. Optional configuration parameters to be passed to function vmaDefragment().\r
-\r
-\deprecated This is a part of the old interface. It is recommended to use structure #VmaDefragmentationInfo2 and function vmaDefragmentationBegin() instead.\r
-*/\r
-typedef struct VmaDefragmentationInfo {\r
- /** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places.\r
- \r
- Default is `VK_WHOLE_SIZE`, which means no limit.\r
- */\r
- VkDeviceSize maxBytesToMove;\r
- /** \brief Maximum number of allocations that can be moved to different place.\r
-\r
- Default is `UINT32_MAX`, which means no limit.\r
- */\r
- uint32_t maxAllocationsToMove;\r
-} VmaDefragmentationInfo;\r
-\r
-/** \brief Statistics returned by function vmaDefragment(). */\r
-typedef struct VmaDefragmentationStats {\r
- /// Total number of bytes that have been copied while moving allocations to different places.\r
- VkDeviceSize bytesMoved;\r
- /// Total number of bytes that have been released to the system by freeing empty `VkDeviceMemory` objects.\r
- VkDeviceSize bytesFreed;\r
- /// Number of allocations that have been moved to different places.\r
- uint32_t allocationsMoved;\r
- /// Number of empty `VkDeviceMemory` objects that have been released to the system.\r
- uint32_t deviceMemoryBlocksFreed;\r
-} VmaDefragmentationStats;\r
-\r
-/** \brief Begins defragmentation process.\r
-\r
-@param allocator Allocator object.\r
-@param pInfo Structure filled with parameters of defragmentation.\r
-@param[out] pStats Optional. Statistics of defragmentation. You can pass null if you are not interested in this information.\r
-@param[out] pContext Context object that must be passed to vmaDefragmentationEnd() to finish defragmentation.\r
-@return `VK_SUCCESS` and `*pContext == null` if defragmentation finished within this function call. `VK_NOT_READY` and `*pContext != null` if defragmentation has been started and you need to call vmaDefragmentationEnd() to finish it. Negative value in case of error.\r
-\r
-Use this function instead of old, deprecated vmaDefragment().\r
-\r
-Warning! Between the call to vmaDefragmentationBegin() and vmaDefragmentationEnd():\r
-\r
-- You should not use any of allocations passed as `pInfo->pAllocations` or\r
- any allocations that belong to pools passed as `pInfo->pPools`,\r
- including calling vmaGetAllocationInfo(), vmaTouchAllocation(), or access\r
- their data.\r
-- Some mutexes protecting internal data structures may be locked, so trying to\r
- make or free any allocations, bind buffers or images, map memory, or launch\r
- another simultaneous defragmentation in between may cause stall (when done on\r
- another thread) or deadlock (when done on the same thread), unless you are\r
- 100% sure that defragmented allocations are in different pools.\r
-- Information returned via `pStats` and `pInfo->pAllocationsChanged` are undefined.\r
- They become valid after call to vmaDefragmentationEnd().\r
-- If `pInfo->commandBuffer` is not null, you must submit that command buffer\r
- and make sure it finished execution before calling vmaDefragmentationEnd().\r
-\r
-For more information and important limitations regarding defragmentation, see documentation chapter:\r
-[Defragmentation](@ref defragmentation).\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationBegin(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VmaDefragmentationInfo2* VMA_NOT_NULL pInfo,\r
- VmaDefragmentationStats* VMA_NULLABLE pStats,\r
- VmaDefragmentationContext VMA_NULLABLE * VMA_NOT_NULL pContext);\r
-\r
-/** \brief Ends defragmentation process.\r
-\r
-Use this function to finish defragmentation started by vmaDefragmentationBegin().\r
-It is safe to pass `context == null`. The function then does nothing.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationEnd(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaDefragmentationContext VMA_NULLABLE context);\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBeginDefragmentationPass(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaDefragmentationContext VMA_NULLABLE context,\r
- VmaDefragmentationPassInfo* VMA_NOT_NULL pInfo\r
-);\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaEndDefragmentationPass(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaDefragmentationContext VMA_NULLABLE context\r
-);\r
-\r
-/** \brief Deprecated. Compacts memory by moving allocations.\r
-\r
-@param pAllocations Array of allocations that can be moved during this compation.\r
-@param allocationCount Number of elements in pAllocations and pAllocationsChanged arrays.\r
-@param[out] pAllocationsChanged Array of boolean values that will indicate whether matching allocation in pAllocations array has been moved. This parameter is optional. Pass null if you don't need this information.\r
-@param pDefragmentationInfo Configuration parameters. Optional - pass null to use default values.\r
-@param[out] pDefragmentationStats Statistics returned by the function. Optional - pass null if you don't need this information.\r
-@return `VK_SUCCESS` if completed, negative error code in case of error.\r
-\r
-\deprecated This is a part of the old interface. It is recommended to use structure #VmaDefragmentationInfo2 and function vmaDefragmentationBegin() instead.\r
-\r
-This function works by moving allocations to different places (different\r
-`VkDeviceMemory` objects and/or different offsets) in order to optimize memory\r
-usage. Only allocations that are in `pAllocations` array can be moved. All other\r
-allocations are considered nonmovable in this call. Basic rules:\r
-\r
-- Only allocations made in memory types that have\r
- `VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT` and `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT`\r
- flags can be compacted. You may pass other allocations but it makes no sense -\r
- these will never be moved.\r
-- Custom pools created with #VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT or\r
- #VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT flag are not defragmented. Allocations\r
- passed to this function that come from such pools are ignored.\r
-- Allocations created with #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT or\r
- created as dedicated allocations for any other reason are also ignored.\r
-- Both allocations made with or without #VMA_ALLOCATION_CREATE_MAPPED_BIT\r
- flag can be compacted. If not persistently mapped, memory will be mapped\r
- temporarily inside this function if needed.\r
-- You must not pass same #VmaAllocation object multiple times in `pAllocations` array.\r
-\r
-The function also frees empty `VkDeviceMemory` blocks.\r
-\r
-Warning: This function may be time-consuming, so you shouldn't call it too often\r
-(like after every resource creation/destruction).\r
-You can call it on special occasions (like when reloading a game level or\r
-when you just destroyed a lot of objects). Calling it every frame may be OK, but\r
-you should measure that on your platform.\r
-\r
-For more information, see [Defragmentation](@ref defragmentation) chapter.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragment(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VmaAllocation VMA_NOT_NULL * VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations,\r
- size_t allocationCount,\r
- VkBool32* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocationsChanged,\r
- const VmaDefragmentationInfo* VMA_NULLABLE pDefragmentationInfo,\r
- VmaDefragmentationStats* VMA_NULLABLE pDefragmentationStats);\r
-\r
-/** \brief Binds buffer to allocation.\r
-\r
-Binds specified buffer to region of memory represented by specified allocation.\r
-Gets `VkDeviceMemory` handle and offset from the allocation.\r
-If you want to create a buffer, allocate memory for it and bind them together separately,\r
-you should use this function for binding instead of standard `vkBindBufferMemory()`,\r
-because it ensures proper synchronization so that when a `VkDeviceMemory` object is used by multiple\r
-allocations, calls to `vkBind*Memory()` or `vkMapMemory()` won't happen from multiple threads simultaneously\r
-(which is illegal in Vulkan).\r
-\r
-It is recommended to use function vmaCreateBuffer() instead of this one.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer);\r
-\r
-/** \brief Binds buffer to allocation with additional parameters.\r
-\r
-@param allocationLocalOffset Additional offset to be added while binding, relative to the beginnig of the `allocation`. Normally it should be 0.\r
-@param pNext A chain of structures to be attached to `VkBindBufferMemoryInfoKHR` structure used internally. Normally it should be null.\r
-\r
-This function is similar to vmaBindBufferMemory(), but it provides additional parameters.\r
-\r
-If `pNext` is not null, #VmaAllocator object must have been created with #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT flag\r
-or with VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_1`. Otherwise the call fails.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory2(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer,\r
- const void* VMA_NULLABLE pNext);\r
-\r
-/** \brief Binds image to allocation.\r
-\r
-Binds specified image to region of memory represented by specified allocation.\r
-Gets `VkDeviceMemory` handle and offset from the allocation.\r
-If you want to create an image, allocate memory for it and bind them together separately,\r
-you should use this function for binding instead of standard `vkBindImageMemory()`,\r
-because it ensures proper synchronization so that when a `VkDeviceMemory` object is used by multiple\r
-allocations, calls to `vkBind*Memory()` or `vkMapMemory()` won't happen from multiple threads simultaneously\r
-(which is illegal in Vulkan).\r
-\r
-It is recommended to use function vmaCreateImage() instead of this one.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkImage VMA_NOT_NULL_NON_DISPATCHABLE image);\r
-\r
-/** \brief Binds image to allocation with additional parameters.\r
-\r
-@param allocationLocalOffset Additional offset to be added while binding, relative to the beginnig of the `allocation`. Normally it should be 0.\r
-@param pNext A chain of structures to be attached to `VkBindImageMemoryInfoKHR` structure used internally. Normally it should be null.\r
-\r
-This function is similar to vmaBindImageMemory(), but it provides additional parameters.\r
-\r
-If `pNext` is not null, #VmaAllocator object must have been created with #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT flag\r
-or with VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_1`. Otherwise the call fails.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory2(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VmaAllocation VMA_NOT_NULL allocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkImage VMA_NOT_NULL_NON_DISPATCHABLE image,\r
- const void* VMA_NULLABLE pNext);\r
-\r
-/**\r
-@param[out] pBuffer Buffer that was created.\r
-@param[out] pAllocation Allocation that was created.\r
-@param[out] pAllocationInfo Optional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo().\r
-\r
-This function automatically:\r
-\r
--# Creates buffer.\r
--# Allocates appropriate memory for it.\r
--# Binds the buffer with the memory.\r
-\r
-If any of these operations fail, buffer and allocation are not created,\r
-returned value is negative error code, *pBuffer and *pAllocation are null.\r
-\r
-If the function succeeded, you must destroy both buffer and allocation when you\r
-no longer need them using either convenience function vmaDestroyBuffer() or\r
-separately, using `vkDestroyBuffer()` and vmaFreeMemory().\r
-\r
-If VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT flag was used,\r
-VK_KHR_dedicated_allocation extension is used internally to query driver whether\r
-it requires or prefers the new buffer to have dedicated allocation. If yes,\r
-and if dedicated allocation is possible (VmaAllocationCreateInfo::pool is null\r
-and VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT is not used), it creates dedicated\r
-allocation for this buffer, just like when using\r
-VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBuffer(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkBufferCreateInfo* VMA_NOT_NULL pBufferCreateInfo,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo,\r
- VkBuffer VMA_NULLABLE_NON_DISPATCHABLE * VMA_NOT_NULL pBuffer,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation,\r
- VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);\r
-\r
-/** \brief Destroys Vulkan buffer and frees allocated memory.\r
-\r
-This is just a convenience function equivalent to:\r
-\r
-\code\r
-vkDestroyBuffer(device, buffer, allocationCallbacks);\r
-vmaFreeMemory(allocator, allocation);\r
-\endcode\r
-\r
-It it safe to pass null as buffer and/or allocation.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyBuffer(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VkBuffer VMA_NULLABLE_NON_DISPATCHABLE buffer,\r
- VmaAllocation VMA_NULLABLE allocation);\r
-\r
-/// Function similar to vmaCreateBuffer().\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateImage(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- const VkImageCreateInfo* VMA_NOT_NULL pImageCreateInfo,\r
- const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo,\r
- VkImage VMA_NULLABLE_NON_DISPATCHABLE * VMA_NOT_NULL pImage,\r
- VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation,\r
- VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);\r
-\r
-/** \brief Destroys Vulkan image and frees allocated memory.\r
-\r
-This is just a convenience function equivalent to:\r
-\r
-\code\r
-vkDestroyImage(device, image, allocationCallbacks);\r
-vmaFreeMemory(allocator, allocation);\r
-\endcode\r
-\r
-It it safe to pass null as image and/or allocation.\r
-*/\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyImage(\r
- VmaAllocator VMA_NOT_NULL allocator,\r
- VkImage VMA_NULLABLE_NON_DISPATCHABLE image,\r
- VmaAllocation VMA_NULLABLE allocation);\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-#endif // AMD_VULKAN_MEMORY_ALLOCATOR_H\r
-\r
-// For Visual Studio IntelliSense.\r
-#if defined(__cplusplus) && defined(__INTELLISENSE__)\r
-#define VMA_IMPLEMENTATION\r
-#endif\r
-\r
-#ifdef VMA_IMPLEMENTATION\r
-#undef VMA_IMPLEMENTATION\r
-\r
-#include <cstdint>\r
-#include <cstdlib>\r
-#include <cstring>\r
-#include <utility>\r
-\r
-/*******************************************************************************\r
-CONFIGURATION SECTION\r
-\r
-Define some of these macros before each #include of this header or change them\r
-here if you need other then default behavior depending on your environment.\r
-*/\r
-\r
-/*\r
-Define this macro to 1 to make the library fetch pointers to Vulkan functions\r
-internally, like:\r
-\r
- vulkanFunctions.vkAllocateMemory = &vkAllocateMemory;\r
-*/\r
-#if !defined(VMA_STATIC_VULKAN_FUNCTIONS) && !defined(VK_NO_PROTOTYPES)\r
- #define VMA_STATIC_VULKAN_FUNCTIONS 1\r
-#endif\r
-\r
-/*\r
-Define this macro to 1 to make the library fetch pointers to Vulkan functions\r
-internally, like:\r
-\r
- vulkanFunctions.vkAllocateMemory = (PFN_vkAllocateMemory)vkGetDeviceProcAddr(m_hDevice, vkAllocateMemory);\r
-*/\r
-#if !defined(VMA_DYNAMIC_VULKAN_FUNCTIONS)\r
- #define VMA_DYNAMIC_VULKAN_FUNCTIONS 1\r
-#endif\r
-\r
-// Define this macro to 1 to make the library use STL containers instead of its own implementation.\r
-//#define VMA_USE_STL_CONTAINERS 1\r
-\r
-/* Set this macro to 1 to make the library including and using STL containers:\r
-std::pair, std::vector, std::list, std::unordered_map.\r
-\r
-Set it to 0 or undefined to make the library using its own implementation of\r
-the containers.\r
-*/\r
-#if VMA_USE_STL_CONTAINERS\r
- #define VMA_USE_STL_VECTOR 1\r
- #define VMA_USE_STL_UNORDERED_MAP 1\r
- #define VMA_USE_STL_LIST 1\r
-#endif\r
-\r
-#ifndef VMA_USE_STL_SHARED_MUTEX\r
- // Compiler conforms to C++17.\r
- #if __cplusplus >= 201703L\r
- #define VMA_USE_STL_SHARED_MUTEX 1\r
- // Visual studio defines __cplusplus properly only when passed additional parameter: /Zc:__cplusplus\r
- // Otherwise it's always 199711L, despite shared_mutex works since Visual Studio 2015 Update 2.\r
- // See: https://blogs.msdn.microsoft.com/vcblog/2018/04/09/msvc-now-correctly-reports-__cplusplus/\r
- #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && __cplusplus == 199711L && _MSVC_LANG >= 201703L\r
- #define VMA_USE_STL_SHARED_MUTEX 1\r
- #else\r
- #define VMA_USE_STL_SHARED_MUTEX 0\r
- #endif\r
-#endif\r
-\r
-/*\r
-THESE INCLUDES ARE NOT ENABLED BY DEFAULT.\r
-Library has its own container implementation.\r
-*/\r
-#if VMA_USE_STL_VECTOR\r
- #include <vector>\r
-#endif\r
-\r
-#if VMA_USE_STL_UNORDERED_MAP\r
- #include <unordered_map>\r
-#endif\r
-\r
-#if VMA_USE_STL_LIST\r
- #include <list>\r
-#endif\r
-\r
-/*\r
-Following headers are used in this CONFIGURATION section only, so feel free to\r
-remove them if not needed.\r
-*/\r
-#include <cassert> // for assert\r
-#include <algorithm> // for min, max\r
-#include <mutex>\r
-\r
-#ifndef VMA_NULL\r
- // Value used as null pointer. Define it to e.g.: nullptr, NULL, 0, (void*)0.\r
- #define VMA_NULL nullptr\r
-#endif\r
-\r
-#if defined(__ANDROID_API__) && (__ANDROID_API__ < 16)\r
-#include <cstdlib>\r
-void *aligned_alloc(size_t alignment, size_t size)\r
-{\r
- // alignment must be >= sizeof(void*)\r
- if(alignment < sizeof(void*))\r
- {\r
- alignment = sizeof(void*);\r
- }\r
-\r
- return memalign(alignment, size);\r
-}\r
-#elif defined(__APPLE__) || defined(__ANDROID__) || (defined(__linux__) && defined(__GLIBCXX__) && !defined(_GLIBCXX_HAVE_ALIGNED_ALLOC))\r
-#include <cstdlib>\r
-void *aligned_alloc(size_t alignment, size_t size)\r
-{\r
- // alignment must be >= sizeof(void*)\r
- if(alignment < sizeof(void*))\r
- {\r
- alignment = sizeof(void*);\r
- }\r
-\r
- void *pointer;\r
- if(posix_memalign(&pointer, alignment, size) == 0)\r
- return pointer;\r
- return VMA_NULL;\r
-}\r
-#endif\r
-\r
-// If your compiler is not compatible with C++11 and definition of\r
-// aligned_alloc() function is missing, uncommeting following line may help:\r
-\r
-//#include <malloc.h>\r
-\r
-// Normal assert to check for programmer's errors, especially in Debug configuration.\r
-#ifndef VMA_ASSERT\r
- #ifdef NDEBUG\r
- #define VMA_ASSERT(expr)\r
- #else\r
- #define VMA_ASSERT(expr) assert(expr)\r
- #endif\r
-#endif\r
-\r
-// Assert that will be called very often, like inside data structures e.g. operator[].\r
-// Making it non-empty can make program slow.\r
-#ifndef VMA_HEAVY_ASSERT\r
- #ifdef NDEBUG\r
- #define VMA_HEAVY_ASSERT(expr)\r
- #else\r
- #define VMA_HEAVY_ASSERT(expr) //VMA_ASSERT(expr)\r
- #endif\r
-#endif\r
-\r
-#ifndef VMA_ALIGN_OF\r
- #define VMA_ALIGN_OF(type) (__alignof(type))\r
-#endif\r
-\r
-#ifndef VMA_SYSTEM_ALIGNED_MALLOC\r
- #if defined(_WIN32)\r
- #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (_aligned_malloc((size), (alignment)))\r
- #else\r
- #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (aligned_alloc((alignment), (size) ))\r
- #endif\r
-#endif\r
-\r
-#ifndef VMA_SYSTEM_FREE\r
- #if defined(_WIN32)\r
- #define VMA_SYSTEM_FREE(ptr) _aligned_free(ptr)\r
- #else\r
- #define VMA_SYSTEM_FREE(ptr) free(ptr)\r
- #endif\r
-#endif\r
-\r
-#ifndef VMA_MIN\r
- #define VMA_MIN(v1, v2) (std::min((v1), (v2)))\r
-#endif\r
-\r
-#ifndef VMA_MAX\r
- #define VMA_MAX(v1, v2) (std::max((v1), (v2)))\r
-#endif\r
-\r
-#ifndef VMA_SWAP\r
- #define VMA_SWAP(v1, v2) std::swap((v1), (v2))\r
-#endif\r
-\r
-#ifndef VMA_SORT\r
- #define VMA_SORT(beg, end, cmp) std::sort(beg, end, cmp)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_LOG\r
- #define VMA_DEBUG_LOG(format, ...)\r
- /*\r
- #define VMA_DEBUG_LOG(format, ...) do { \\r
- printf(format, __VA_ARGS__); \\r
- printf("\n"); \\r
- } while(false)\r
- */\r
-#endif\r
-\r
-// Define this macro to 1 to enable functions: vmaBuildStatsString, vmaFreeStatsString.\r
-#if VMA_STATS_STRING_ENABLED\r
- static inline void VmaUint32ToStr(char* outStr, size_t strLen, uint32_t num)\r
- {\r
- snprintf(outStr, strLen, "%u", static_cast<unsigned int>(num));\r
- }\r
- static inline void VmaUint64ToStr(char* outStr, size_t strLen, uint64_t num)\r
- {\r
- snprintf(outStr, strLen, "%llu", static_cast<unsigned long long>(num));\r
- }\r
- static inline void VmaPtrToStr(char* outStr, size_t strLen, const void* ptr)\r
- {\r
- snprintf(outStr, strLen, "%p", ptr);\r
- }\r
-#endif\r
-\r
-#ifndef VMA_MUTEX\r
- class VmaMutex\r
- {\r
- public:\r
- void Lock() { m_Mutex.lock(); }\r
- void Unlock() { m_Mutex.unlock(); }\r
- bool TryLock() { return m_Mutex.try_lock(); }\r
- private:\r
- std::mutex m_Mutex;\r
- };\r
- #define VMA_MUTEX VmaMutex\r
-#endif\r
-\r
-// Read-write mutex, where "read" is shared access, "write" is exclusive access.\r
-#ifndef VMA_RW_MUTEX\r
- #if VMA_USE_STL_SHARED_MUTEX\r
- // Use std::shared_mutex from C++17.\r
- #include <shared_mutex>\r
- class VmaRWMutex\r
- {\r
- public:\r
- void LockRead() { m_Mutex.lock_shared(); }\r
- void UnlockRead() { m_Mutex.unlock_shared(); }\r
- bool TryLockRead() { return m_Mutex.try_lock_shared(); }\r
- void LockWrite() { m_Mutex.lock(); }\r
- void UnlockWrite() { m_Mutex.unlock(); }\r
- bool TryLockWrite() { return m_Mutex.try_lock(); }\r
- private:\r
- std::shared_mutex m_Mutex;\r
- };\r
- #define VMA_RW_MUTEX VmaRWMutex\r
- #elif defined(_WIN32) && defined(WINVER) && WINVER >= 0x0600\r
- // Use SRWLOCK from WinAPI.\r
- // Minimum supported client = Windows Vista, server = Windows Server 2008.\r
- class VmaRWMutex\r
- {\r
- public:\r
- VmaRWMutex() { InitializeSRWLock(&m_Lock); }\r
- void LockRead() { AcquireSRWLockShared(&m_Lock); }\r
- void UnlockRead() { ReleaseSRWLockShared(&m_Lock); }\r
- bool TryLockRead() { return TryAcquireSRWLockShared(&m_Lock) != FALSE; }\r
- void LockWrite() { AcquireSRWLockExclusive(&m_Lock); }\r
- void UnlockWrite() { ReleaseSRWLockExclusive(&m_Lock); }\r
- bool TryLockWrite() { return TryAcquireSRWLockExclusive(&m_Lock) != FALSE; }\r
- private:\r
- SRWLOCK m_Lock;\r
- };\r
- #define VMA_RW_MUTEX VmaRWMutex\r
- #else\r
- // Less efficient fallback: Use normal mutex.\r
- class VmaRWMutex\r
- {\r
- public:\r
- void LockRead() { m_Mutex.Lock(); }\r
- void UnlockRead() { m_Mutex.Unlock(); }\r
- bool TryLockRead() { return m_Mutex.TryLock(); }\r
- void LockWrite() { m_Mutex.Lock(); }\r
- void UnlockWrite() { m_Mutex.Unlock(); }\r
- bool TryLockWrite() { return m_Mutex.TryLock(); }\r
- private:\r
- VMA_MUTEX m_Mutex;\r
- };\r
- #define VMA_RW_MUTEX VmaRWMutex\r
- #endif // #if VMA_USE_STL_SHARED_MUTEX\r
-#endif // #ifndef VMA_RW_MUTEX\r
-\r
-/*\r
-If providing your own implementation, you need to implement a subset of std::atomic.\r
-*/\r
-#ifndef VMA_ATOMIC_UINT32\r
- #include <atomic>\r
- #define VMA_ATOMIC_UINT32 std::atomic<uint32_t>\r
-#endif\r
-\r
-#ifndef VMA_ATOMIC_UINT64\r
- #include <atomic>\r
- #define VMA_ATOMIC_UINT64 std::atomic<uint64_t>\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_ALWAYS_DEDICATED_MEMORY\r
- /**\r
- Every allocation will have its own memory block.\r
- Define to 1 for debugging purposes only.\r
- */\r
- #define VMA_DEBUG_ALWAYS_DEDICATED_MEMORY (0)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_ALIGNMENT\r
- /**\r
- Minimum alignment of all allocations, in bytes.\r
- Set to more than 1 for debugging purposes only. Must be power of two.\r
- */\r
- #define VMA_DEBUG_ALIGNMENT (1)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_MARGIN\r
- /**\r
- Minimum margin before and after every allocation, in bytes.\r
- Set nonzero for debugging purposes only.\r
- */\r
- #define VMA_DEBUG_MARGIN (0)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_INITIALIZE_ALLOCATIONS\r
- /**\r
- Define this macro to 1 to automatically fill new allocations and destroyed\r
- allocations with some bit pattern.\r
- */\r
- #define VMA_DEBUG_INITIALIZE_ALLOCATIONS (0)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_DETECT_CORRUPTION\r
- /**\r
- Define this macro to 1 together with non-zero value of VMA_DEBUG_MARGIN to\r
- enable writing magic value to the margin before and after every allocation and\r
- validating it, so that memory corruptions (out-of-bounds writes) are detected.\r
- */\r
- #define VMA_DEBUG_DETECT_CORRUPTION (0)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_GLOBAL_MUTEX\r
- /**\r
- Set this to 1 for debugging purposes only, to enable single mutex protecting all\r
- entry calls to the library. Can be useful for debugging multithreading issues.\r
- */\r
- #define VMA_DEBUG_GLOBAL_MUTEX (0)\r
-#endif\r
-\r
-#ifndef VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY\r
- /**\r
- Minimum value for VkPhysicalDeviceLimits::bufferImageGranularity.\r
- Set to more than 1 for debugging purposes only. Must be power of two.\r
- */\r
- #define VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY (1)\r
-#endif\r
-\r
-#ifndef VMA_SMALL_HEAP_MAX_SIZE\r
- /// Maximum size of a memory heap in Vulkan to consider it "small".\r
- #define VMA_SMALL_HEAP_MAX_SIZE (1024ull * 1024 * 1024)\r
-#endif\r
-\r
-#ifndef VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE\r
- /// Default size of a block allocated as single VkDeviceMemory from a "large" heap.\r
- #define VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE (256ull * 1024 * 1024)\r
-#endif\r
-\r
-#ifndef VMA_CLASS_NO_COPY\r
- #define VMA_CLASS_NO_COPY(className) \\r
- private: \\r
- className(const className&) = delete; \\r
- className& operator=(const className&) = delete;\r
-#endif\r
-\r
-static const uint32_t VMA_FRAME_INDEX_LOST = UINT32_MAX;\r
-\r
-// Decimal 2139416166, float NaN, little-endian binary 66 E6 84 7F.\r
-static const uint32_t VMA_CORRUPTION_DETECTION_MAGIC_VALUE = 0x7F84E666;\r
-\r
-static const uint8_t VMA_ALLOCATION_FILL_PATTERN_CREATED = 0xDC;\r
-static const uint8_t VMA_ALLOCATION_FILL_PATTERN_DESTROYED = 0xEF;\r
-\r
-/*******************************************************************************\r
-END OF CONFIGURATION\r
-*/\r
-\r
-// # Copy of some Vulkan definitions so we don't need to check their existence just to handle few constants.\r
-\r
-static const uint32_t VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY = 0x00000040;\r
-static const uint32_t VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY = 0x00000080;\r
-static const uint32_t VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_COPY = 0x00020000;\r
-\r
-static const uint32_t VMA_ALLOCATION_INTERNAL_STRATEGY_MIN_OFFSET = 0x10000000u;\r
-\r
-static VkAllocationCallbacks VmaEmptyAllocationCallbacks = {\r
- VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL };\r
-\r
-// Returns number of bits set to 1 in (v).\r
-static inline uint32_t VmaCountBitsSet(uint32_t v)\r
-{\r
- uint32_t c = v - ((v >> 1) & 0x55555555);\r
- c = ((c >> 2) & 0x33333333) + (c & 0x33333333);\r
- c = ((c >> 4) + c) & 0x0F0F0F0F;\r
- c = ((c >> 8) + c) & 0x00FF00FF;\r
- c = ((c >> 16) + c) & 0x0000FFFF;\r
- return c;\r
-}\r
-\r
-// Aligns given value up to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 16.\r
-// Use types like uint32_t, uint64_t as T.\r
-template <typename T>\r
-static inline T VmaAlignUp(T val, T align)\r
-{\r
- return (val + align - 1) / align * align;\r
-}\r
-// Aligns given value down to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 8.\r
-// Use types like uint32_t, uint64_t as T.\r
-template <typename T>\r
-static inline T VmaAlignDown(T val, T align)\r
-{\r
- return val / align * align;\r
-}\r
-\r
-// Division with mathematical rounding to nearest number.\r
-template <typename T>\r
-static inline T VmaRoundDiv(T x, T y)\r
-{\r
- return (x + (y / (T)2)) / y;\r
-}\r
-\r
-/*\r
-Returns true if given number is a power of two.\r
-T must be unsigned integer number or signed integer but always nonnegative.\r
-For 0 returns true.\r
-*/\r
-template <typename T>\r
-inline bool VmaIsPow2(T x)\r
-{\r
- return (x & (x-1)) == 0;\r
-}\r
-\r
-// Returns smallest power of 2 greater or equal to v.\r
-static inline uint32_t VmaNextPow2(uint32_t v)\r
-{\r
- v--;\r
- v |= v >> 1;\r
- v |= v >> 2;\r
- v |= v >> 4;\r
- v |= v >> 8;\r
- v |= v >> 16;\r
- v++;\r
- return v;\r
-}\r
-static inline uint64_t VmaNextPow2(uint64_t v)\r
-{\r
- v--;\r
- v |= v >> 1;\r
- v |= v >> 2;\r
- v |= v >> 4;\r
- v |= v >> 8;\r
- v |= v >> 16;\r
- v |= v >> 32;\r
- v++;\r
- return v;\r
-}\r
-\r
-// Returns largest power of 2 less or equal to v.\r
-static inline uint32_t VmaPrevPow2(uint32_t v)\r
-{\r
- v |= v >> 1;\r
- v |= v >> 2;\r
- v |= v >> 4;\r
- v |= v >> 8;\r
- v |= v >> 16;\r
- v = v ^ (v >> 1);\r
- return v;\r
-}\r
-static inline uint64_t VmaPrevPow2(uint64_t v)\r
-{\r
- v |= v >> 1;\r
- v |= v >> 2;\r
- v |= v >> 4;\r
- v |= v >> 8;\r
- v |= v >> 16;\r
- v |= v >> 32;\r
- v = v ^ (v >> 1);\r
- return v;\r
-}\r
-\r
-static inline bool VmaStrIsEmpty(const char* pStr)\r
-{\r
- return pStr == VMA_NULL || *pStr == '\0';\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-static const char* VmaAlgorithmToStr(uint32_t algorithm)\r
-{\r
- switch(algorithm)\r
- {\r
- case VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT:\r
- return "Linear";\r
- case VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT:\r
- return "Buddy";\r
- case 0:\r
- return "Default";\r
- default:\r
- VMA_ASSERT(0);\r
- return "";\r
- }\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-#ifndef VMA_SORT\r
-\r
-template<typename Iterator, typename Compare>\r
-Iterator VmaQuickSortPartition(Iterator beg, Iterator end, Compare cmp)\r
-{\r
- Iterator centerValue = end; --centerValue;\r
- Iterator insertIndex = beg;\r
- for(Iterator memTypeIndex = beg; memTypeIndex < centerValue; ++memTypeIndex)\r
- {\r
- if(cmp(*memTypeIndex, *centerValue))\r
- {\r
- if(insertIndex != memTypeIndex)\r
- {\r
- VMA_SWAP(*memTypeIndex, *insertIndex);\r
- }\r
- ++insertIndex;\r
- }\r
- }\r
- if(insertIndex != centerValue)\r
- {\r
- VMA_SWAP(*insertIndex, *centerValue);\r
- }\r
- return insertIndex;\r
-}\r
-\r
-template<typename Iterator, typename Compare>\r
-void VmaQuickSort(Iterator beg, Iterator end, Compare cmp)\r
-{\r
- if(beg < end)\r
- {\r
- Iterator it = VmaQuickSortPartition<Iterator, Compare>(beg, end, cmp);\r
- VmaQuickSort<Iterator, Compare>(beg, it, cmp);\r
- VmaQuickSort<Iterator, Compare>(it + 1, end, cmp);\r
- }\r
-}\r
-\r
-#define VMA_SORT(beg, end, cmp) VmaQuickSort(beg, end, cmp)\r
-\r
-#endif // #ifndef VMA_SORT\r
-\r
-/*\r
-Returns true if two memory blocks occupy overlapping pages.\r
-ResourceA must be in less memory offset than ResourceB.\r
-\r
-Algorithm is based on "Vulkan 1.0.39 - A Specification (with all registered Vulkan extensions)"\r
-chapter 11.6 "Resource Memory Association", paragraph "Buffer-Image Granularity".\r
-*/\r
-static inline bool VmaBlocksOnSamePage(\r
- VkDeviceSize resourceAOffset,\r
- VkDeviceSize resourceASize,\r
- VkDeviceSize resourceBOffset,\r
- VkDeviceSize pageSize)\r
-{\r
- VMA_ASSERT(resourceAOffset + resourceASize <= resourceBOffset && resourceASize > 0 && pageSize > 0);\r
- VkDeviceSize resourceAEnd = resourceAOffset + resourceASize - 1;\r
- VkDeviceSize resourceAEndPage = resourceAEnd & ~(pageSize - 1);\r
- VkDeviceSize resourceBStart = resourceBOffset;\r
- VkDeviceSize resourceBStartPage = resourceBStart & ~(pageSize - 1);\r
- return resourceAEndPage == resourceBStartPage;\r
-}\r
-\r
-enum VmaSuballocationType\r
-{\r
- VMA_SUBALLOCATION_TYPE_FREE = 0,\r
- VMA_SUBALLOCATION_TYPE_UNKNOWN = 1,\r
- VMA_SUBALLOCATION_TYPE_BUFFER = 2,\r
- VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN = 3,\r
- VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR = 4,\r
- VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL = 5,\r
- VMA_SUBALLOCATION_TYPE_MAX_ENUM = 0x7FFFFFFF\r
-};\r
-\r
-/*\r
-Returns true if given suballocation types could conflict and must respect\r
-VkPhysicalDeviceLimits::bufferImageGranularity. They conflict if one is buffer\r
-or linear image and another one is optimal image. If type is unknown, behave\r
-conservatively.\r
-*/\r
-static inline bool VmaIsBufferImageGranularityConflict(\r
- VmaSuballocationType suballocType1,\r
- VmaSuballocationType suballocType2)\r
-{\r
- if(suballocType1 > suballocType2)\r
- {\r
- VMA_SWAP(suballocType1, suballocType2);\r
- }\r
- \r
- switch(suballocType1)\r
- {\r
- case VMA_SUBALLOCATION_TYPE_FREE:\r
- return false;\r
- case VMA_SUBALLOCATION_TYPE_UNKNOWN:\r
- return true;\r
- case VMA_SUBALLOCATION_TYPE_BUFFER:\r
- return\r
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||\r
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;\r
- case VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN:\r
- return\r
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||\r
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR ||\r
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;\r
- case VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR:\r
- return\r
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;\r
- case VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL:\r
- return false;\r
- default:\r
- VMA_ASSERT(0);\r
- return true;\r
- }\r
-}\r
-\r
-static void VmaWriteMagicValue(void* pData, VkDeviceSize offset)\r
-{\r
-#if VMA_DEBUG_MARGIN > 0 && VMA_DEBUG_DETECT_CORRUPTION\r
- uint32_t* pDst = (uint32_t*)((char*)pData + offset);\r
- const size_t numberCount = VMA_DEBUG_MARGIN / sizeof(uint32_t);\r
- for(size_t i = 0; i < numberCount; ++i, ++pDst)\r
- {\r
- *pDst = VMA_CORRUPTION_DETECTION_MAGIC_VALUE;\r
- }\r
-#else\r
- // no-op\r
-#endif\r
-}\r
-\r
-static bool VmaValidateMagicValue(const void* pData, VkDeviceSize offset)\r
-{\r
-#if VMA_DEBUG_MARGIN > 0 && VMA_DEBUG_DETECT_CORRUPTION\r
- const uint32_t* pSrc = (const uint32_t*)((const char*)pData + offset);\r
- const size_t numberCount = VMA_DEBUG_MARGIN / sizeof(uint32_t);\r
- for(size_t i = 0; i < numberCount; ++i, ++pSrc)\r
- {\r
- if(*pSrc != VMA_CORRUPTION_DETECTION_MAGIC_VALUE)\r
- {\r
- return false;\r
- }\r
- }\r
-#endif\r
- return true;\r
-}\r
-\r
-/*\r
-Fills structure with parameters of an example buffer to be used for transfers\r
-during GPU memory defragmentation.\r
-*/\r
-static void VmaFillGpuDefragmentationBufferCreateInfo(VkBufferCreateInfo& outBufCreateInfo)\r
-{\r
- memset(&outBufCreateInfo, 0, sizeof(outBufCreateInfo));\r
- outBufCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;\r
- outBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;\r
- outBufCreateInfo.size = (VkDeviceSize)VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE; // Example size.\r
-}\r
-\r
-// Helper RAII class to lock a mutex in constructor and unlock it in destructor (at the end of scope).\r
-struct VmaMutexLock\r
-{\r
- VMA_CLASS_NO_COPY(VmaMutexLock)\r
-public:\r
- VmaMutexLock(VMA_MUTEX& mutex, bool useMutex = true) :\r
- m_pMutex(useMutex ? &mutex : VMA_NULL)\r
- { if(m_pMutex) { m_pMutex->Lock(); } }\r
- ~VmaMutexLock()\r
- { if(m_pMutex) { m_pMutex->Unlock(); } }\r
-private:\r
- VMA_MUTEX* m_pMutex;\r
-};\r
-\r
-// Helper RAII class to lock a RW mutex in constructor and unlock it in destructor (at the end of scope), for reading.\r
-struct VmaMutexLockRead\r
-{\r
- VMA_CLASS_NO_COPY(VmaMutexLockRead)\r
-public:\r
- VmaMutexLockRead(VMA_RW_MUTEX& mutex, bool useMutex) :\r
- m_pMutex(useMutex ? &mutex : VMA_NULL)\r
- { if(m_pMutex) { m_pMutex->LockRead(); } }\r
- ~VmaMutexLockRead() { if(m_pMutex) { m_pMutex->UnlockRead(); } }\r
-private:\r
- VMA_RW_MUTEX* m_pMutex;\r
-};\r
-\r
-// Helper RAII class to lock a RW mutex in constructor and unlock it in destructor (at the end of scope), for writing.\r
-struct VmaMutexLockWrite\r
-{\r
- VMA_CLASS_NO_COPY(VmaMutexLockWrite)\r
-public:\r
- VmaMutexLockWrite(VMA_RW_MUTEX& mutex, bool useMutex) :\r
- m_pMutex(useMutex ? &mutex : VMA_NULL)\r
- { if(m_pMutex) { m_pMutex->LockWrite(); } }\r
- ~VmaMutexLockWrite() { if(m_pMutex) { m_pMutex->UnlockWrite(); } }\r
-private:\r
- VMA_RW_MUTEX* m_pMutex;\r
-};\r
-\r
-#if VMA_DEBUG_GLOBAL_MUTEX\r
- static VMA_MUTEX gDebugGlobalMutex;\r
- #define VMA_DEBUG_GLOBAL_MUTEX_LOCK VmaMutexLock debugGlobalMutexLock(gDebugGlobalMutex, true);\r
-#else\r
- #define VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-#endif\r
-\r
-// Minimum size of a free suballocation to register it in the free suballocation collection.\r
-static const VkDeviceSize VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER = 16;\r
-\r
-/*\r
-Performs binary search and returns iterator to first element that is greater or\r
-equal to (key), according to comparison (cmp).\r
-\r
-Cmp should return true if first argument is less than second argument.\r
-\r
-Returned value is the found element, if present in the collection or place where\r
-new element with value (key) should be inserted.\r
-*/\r
-template <typename CmpLess, typename IterT, typename KeyT>\r
-static IterT VmaBinaryFindFirstNotLess(IterT beg, IterT end, const KeyT &key, const CmpLess& cmp)\r
-{\r
- size_t down = 0, up = (end - beg);\r
- while(down < up)\r
- {\r
- const size_t mid = (down + up) / 2;\r
- if(cmp(*(beg+mid), key))\r
- {\r
- down = mid + 1;\r
- }\r
- else\r
- {\r
- up = mid;\r
- }\r
- }\r
- return beg + down;\r
-}\r
-\r
-template<typename CmpLess, typename IterT, typename KeyT>\r
-IterT VmaBinaryFindSorted(const IterT& beg, const IterT& end, const KeyT& value, const CmpLess& cmp)\r
-{\r
- IterT it = VmaBinaryFindFirstNotLess<CmpLess, IterT, KeyT>(\r
- beg, end, value, cmp);\r
- if(it == end ||\r
- (!cmp(*it, value) && !cmp(value, *it)))\r
- {\r
- return it;\r
- }\r
- return end;\r
-}\r
-\r
-/*\r
-Returns true if all pointers in the array are not-null and unique.\r
-Warning! O(n^2) complexity. Use only inside VMA_HEAVY_ASSERT.\r
-T must be pointer type, e.g. VmaAllocation, VmaPool.\r
-*/\r
-template<typename T>\r
-static bool VmaValidatePointerArray(uint32_t count, const T* arr)\r
-{\r
- for(uint32_t i = 0; i < count; ++i)\r
- {\r
- const T iPtr = arr[i];\r
- if(iPtr == VMA_NULL)\r
- {\r
- return false;\r
- }\r
- for(uint32_t j = i + 1; j < count; ++j)\r
- {\r
- if(iPtr == arr[j])\r
- {\r
- return false;\r
- }\r
- }\r
- }\r
- return true;\r
-}\r
-\r
-template<typename MainT, typename NewT>\r
-static inline void VmaPnextChainPushFront(MainT* mainStruct, NewT* newStruct)\r
-{\r
- newStruct->pNext = mainStruct->pNext;\r
- mainStruct->pNext = newStruct;\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// Memory allocation\r
-\r
-static void* VmaMalloc(const VkAllocationCallbacks* pAllocationCallbacks, size_t size, size_t alignment)\r
-{\r
- if((pAllocationCallbacks != VMA_NULL) &&\r
- (pAllocationCallbacks->pfnAllocation != VMA_NULL))\r
- {\r
- return (*pAllocationCallbacks->pfnAllocation)(\r
- pAllocationCallbacks->pUserData,\r
- size,\r
- alignment,\r
- VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);\r
- }\r
- else\r
- {\r
- return VMA_SYSTEM_ALIGNED_MALLOC(size, alignment);\r
- }\r
-}\r
-\r
-static void VmaFree(const VkAllocationCallbacks* pAllocationCallbacks, void* ptr)\r
-{\r
- if((pAllocationCallbacks != VMA_NULL) &&\r
- (pAllocationCallbacks->pfnFree != VMA_NULL))\r
- {\r
- (*pAllocationCallbacks->pfnFree)(pAllocationCallbacks->pUserData, ptr);\r
- }\r
- else\r
- {\r
- VMA_SYSTEM_FREE(ptr);\r
- }\r
-}\r
-\r
-template<typename T>\r
-static T* VmaAllocate(const VkAllocationCallbacks* pAllocationCallbacks)\r
-{\r
- return (T*)VmaMalloc(pAllocationCallbacks, sizeof(T), VMA_ALIGN_OF(T));\r
-}\r
-\r
-template<typename T>\r
-static T* VmaAllocateArray(const VkAllocationCallbacks* pAllocationCallbacks, size_t count)\r
-{\r
- return (T*)VmaMalloc(pAllocationCallbacks, sizeof(T) * count, VMA_ALIGN_OF(T));\r
-}\r
-\r
-#define vma_new(allocator, type) new(VmaAllocate<type>(allocator))(type)\r
-\r
-#define vma_new_array(allocator, type, count) new(VmaAllocateArray<type>((allocator), (count)))(type)\r
-\r
-template<typename T>\r
-static void vma_delete(const VkAllocationCallbacks* pAllocationCallbacks, T* ptr)\r
-{\r
- ptr->~T();\r
- VmaFree(pAllocationCallbacks, ptr);\r
-}\r
-\r
-template<typename T>\r
-static void vma_delete_array(const VkAllocationCallbacks* pAllocationCallbacks, T* ptr, size_t count)\r
-{\r
- if(ptr != VMA_NULL)\r
- {\r
- for(size_t i = count; i--; )\r
- {\r
- ptr[i].~T();\r
- }\r
- VmaFree(pAllocationCallbacks, ptr);\r
- }\r
-}\r
-\r
-static char* VmaCreateStringCopy(const VkAllocationCallbacks* allocs, const char* srcStr)\r
-{\r
- if(srcStr != VMA_NULL)\r
- {\r
- const size_t len = strlen(srcStr);\r
- char* const result = vma_new_array(allocs, char, len + 1);\r
- memcpy(result, srcStr, len + 1);\r
- return result;\r
- }\r
- else\r
- {\r
- return VMA_NULL;\r
- }\r
-}\r
-\r
-static void VmaFreeString(const VkAllocationCallbacks* allocs, char* str)\r
-{\r
- if(str != VMA_NULL)\r
- {\r
- const size_t len = strlen(str);\r
- vma_delete_array(allocs, str, len + 1);\r
- }\r
-}\r
-\r
-// STL-compatible allocator.\r
-template<typename T>\r
-class VmaStlAllocator\r
-{\r
-public:\r
- const VkAllocationCallbacks* const m_pCallbacks;\r
- typedef T value_type;\r
- \r
- VmaStlAllocator(const VkAllocationCallbacks* pCallbacks) : m_pCallbacks(pCallbacks) { }\r
- template<typename U> VmaStlAllocator(const VmaStlAllocator<U>& src) : m_pCallbacks(src.m_pCallbacks) { }\r
-\r
- T* allocate(size_t n) { return VmaAllocateArray<T>(m_pCallbacks, n); }\r
- void deallocate(T* p, size_t n) { VmaFree(m_pCallbacks, p); }\r
-\r
- template<typename U>\r
- bool operator==(const VmaStlAllocator<U>& rhs) const\r
- {\r
- return m_pCallbacks == rhs.m_pCallbacks;\r
- }\r
- template<typename U>\r
- bool operator!=(const VmaStlAllocator<U>& rhs) const\r
- {\r
- return m_pCallbacks != rhs.m_pCallbacks;\r
- }\r
-\r
- VmaStlAllocator& operator=(const VmaStlAllocator& x) = delete;\r
-};\r
-\r
-#if VMA_USE_STL_VECTOR\r
-\r
-#define VmaVector std::vector\r
-\r
-template<typename T, typename allocatorT>\r
-static void VmaVectorInsert(std::vector<T, allocatorT>& vec, size_t index, const T& item)\r
-{\r
- vec.insert(vec.begin() + index, item);\r
-}\r
-\r
-template<typename T, typename allocatorT>\r
-static void VmaVectorRemove(std::vector<T, allocatorT>& vec, size_t index)\r
-{\r
- vec.erase(vec.begin() + index);\r
-}\r
-\r
-#else // #if VMA_USE_STL_VECTOR\r
-\r
-/* Class with interface compatible with subset of std::vector.\r
-T must be POD because constructors and destructors are not called and memcpy is\r
-used for these objects. */\r
-template<typename T, typename AllocatorT>\r
-class VmaVector\r
-{\r
-public:\r
- typedef T value_type;\r
-\r
- VmaVector(const AllocatorT& allocator) :\r
- m_Allocator(allocator),\r
- m_pArray(VMA_NULL),\r
- m_Count(0),\r
- m_Capacity(0)\r
- {\r
- }\r
-\r
- VmaVector(size_t count, const AllocatorT& allocator) :\r
- m_Allocator(allocator),\r
- m_pArray(count ? (T*)VmaAllocateArray<T>(allocator.m_pCallbacks, count) : VMA_NULL),\r
- m_Count(count),\r
- m_Capacity(count)\r
- {\r
- }\r
- \r
- // This version of the constructor is here for compatibility with pre-C++14 std::vector.\r
- // value is unused.\r
- VmaVector(size_t count, const T& value, const AllocatorT& allocator)\r
- : VmaVector(count, allocator) {}\r
- \r
- VmaVector(const VmaVector<T, AllocatorT>& src) :\r
- m_Allocator(src.m_Allocator),\r
- m_pArray(src.m_Count ? (T*)VmaAllocateArray<T>(src.m_Allocator.m_pCallbacks, src.m_Count) : VMA_NULL),\r
- m_Count(src.m_Count),\r
- m_Capacity(src.m_Count)\r
- {\r
- if(m_Count != 0)\r
- {\r
- memcpy(m_pArray, src.m_pArray, m_Count * sizeof(T));\r
- }\r
- }\r
- \r
- ~VmaVector()\r
- {\r
- VmaFree(m_Allocator.m_pCallbacks, m_pArray);\r
- }\r
-\r
- VmaVector& operator=(const VmaVector<T, AllocatorT>& rhs)\r
- {\r
- if(&rhs != this)\r
- {\r
- resize(rhs.m_Count);\r
- if(m_Count != 0)\r
- {\r
- memcpy(m_pArray, rhs.m_pArray, m_Count * sizeof(T));\r
- }\r
- }\r
- return *this;\r
- }\r
- \r
- bool empty() const { return m_Count == 0; }\r
- size_t size() const { return m_Count; }\r
- T* data() { return m_pArray; }\r
- const T* data() const { return m_pArray; }\r
- \r
- T& operator[](size_t index)\r
- {\r
- VMA_HEAVY_ASSERT(index < m_Count);\r
- return m_pArray[index];\r
- }\r
- const T& operator[](size_t index) const\r
- {\r
- VMA_HEAVY_ASSERT(index < m_Count);\r
- return m_pArray[index];\r
- }\r
-\r
- T& front()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return m_pArray[0];\r
- }\r
- const T& front() const\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return m_pArray[0];\r
- }\r
- T& back()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return m_pArray[m_Count - 1];\r
- }\r
- const T& back() const\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return m_pArray[m_Count - 1];\r
- }\r
-\r
- void reserve(size_t newCapacity, bool freeMemory = false)\r
- {\r
- newCapacity = VMA_MAX(newCapacity, m_Count);\r
- \r
- if((newCapacity < m_Capacity) && !freeMemory)\r
- {\r
- newCapacity = m_Capacity;\r
- }\r
- \r
- if(newCapacity != m_Capacity)\r
- {\r
- T* const newArray = newCapacity ? VmaAllocateArray<T>(m_Allocator, newCapacity) : VMA_NULL;\r
- if(m_Count != 0)\r
- {\r
- memcpy(newArray, m_pArray, m_Count * sizeof(T));\r
- }\r
- VmaFree(m_Allocator.m_pCallbacks, m_pArray);\r
- m_Capacity = newCapacity;\r
- m_pArray = newArray;\r
- }\r
- }\r
-\r
- void resize(size_t newCount, bool freeMemory = false)\r
- {\r
- size_t newCapacity = m_Capacity;\r
- if(newCount > m_Capacity)\r
- {\r
- newCapacity = VMA_MAX(newCount, VMA_MAX(m_Capacity * 3 / 2, (size_t)8));\r
- }\r
- else if(freeMemory)\r
- {\r
- newCapacity = newCount;\r
- }\r
-\r
- if(newCapacity != m_Capacity)\r
- {\r
- T* const newArray = newCapacity ? VmaAllocateArray<T>(m_Allocator.m_pCallbacks, newCapacity) : VMA_NULL;\r
- const size_t elementsToCopy = VMA_MIN(m_Count, newCount);\r
- if(elementsToCopy != 0)\r
- {\r
- memcpy(newArray, m_pArray, elementsToCopy * sizeof(T));\r
- }\r
- VmaFree(m_Allocator.m_pCallbacks, m_pArray);\r
- m_Capacity = newCapacity;\r
- m_pArray = newArray;\r
- }\r
-\r
- m_Count = newCount;\r
- }\r
-\r
- void clear(bool freeMemory = false)\r
- {\r
- resize(0, freeMemory);\r
- }\r
-\r
- void insert(size_t index, const T& src)\r
- {\r
- VMA_HEAVY_ASSERT(index <= m_Count);\r
- const size_t oldCount = size();\r
- resize(oldCount + 1);\r
- if(index < oldCount)\r
- {\r
- memmove(m_pArray + (index + 1), m_pArray + index, (oldCount - index) * sizeof(T));\r
- }\r
- m_pArray[index] = src;\r
- }\r
-\r
- void remove(size_t index)\r
- {\r
- VMA_HEAVY_ASSERT(index < m_Count);\r
- const size_t oldCount = size();\r
- if(index < oldCount - 1)\r
- {\r
- memmove(m_pArray + index, m_pArray + (index + 1), (oldCount - index - 1) * sizeof(T));\r
- }\r
- resize(oldCount - 1);\r
- }\r
-\r
- void push_back(const T& src)\r
- {\r
- const size_t newIndex = size();\r
- resize(newIndex + 1);\r
- m_pArray[newIndex] = src;\r
- }\r
-\r
- void pop_back()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- resize(size() - 1);\r
- }\r
-\r
- void push_front(const T& src)\r
- {\r
- insert(0, src);\r
- }\r
-\r
- void pop_front()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- remove(0);\r
- }\r
-\r
- typedef T* iterator;\r
-\r
- iterator begin() { return m_pArray; }\r
- iterator end() { return m_pArray + m_Count; }\r
-\r
-private:\r
- AllocatorT m_Allocator;\r
- T* m_pArray;\r
- size_t m_Count;\r
- size_t m_Capacity;\r
-};\r
-\r
-template<typename T, typename allocatorT>\r
-static void VmaVectorInsert(VmaVector<T, allocatorT>& vec, size_t index, const T& item)\r
-{\r
- vec.insert(index, item);\r
-}\r
-\r
-template<typename T, typename allocatorT>\r
-static void VmaVectorRemove(VmaVector<T, allocatorT>& vec, size_t index)\r
-{\r
- vec.remove(index);\r
-}\r
-\r
-#endif // #if VMA_USE_STL_VECTOR\r
-\r
-template<typename CmpLess, typename VectorT>\r
-size_t VmaVectorInsertSorted(VectorT& vector, const typename VectorT::value_type& value)\r
-{\r
- const size_t indexToInsert = VmaBinaryFindFirstNotLess(\r
- vector.data(),\r
- vector.data() + vector.size(),\r
- value,\r
- CmpLess()) - vector.data();\r
- VmaVectorInsert(vector, indexToInsert, value);\r
- return indexToInsert;\r
-}\r
-\r
-template<typename CmpLess, typename VectorT>\r
-bool VmaVectorRemoveSorted(VectorT& vector, const typename VectorT::value_type& value)\r
-{\r
- CmpLess comparator;\r
- typename VectorT::iterator it = VmaBinaryFindFirstNotLess(\r
- vector.begin(),\r
- vector.end(),\r
- value,\r
- comparator);\r
- if((it != vector.end()) && !comparator(*it, value) && !comparator(value, *it))\r
- {\r
- size_t indexToRemove = it - vector.begin();\r
- VmaVectorRemove(vector, indexToRemove);\r
- return true;\r
- }\r
- return false;\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaSmallVector\r
-\r
-/*\r
-This is a vector (a variable-sized array), optimized for the case when the array is small.\r
-\r
-It contains some number of elements in-place, which allows it to avoid heap allocation\r
-when the actual number of elements is below that threshold. This allows normal "small"\r
-cases to be fast without losing generality for large inputs.\r
-*/\r
-\r
-template<typename T, typename AllocatorT, size_t N>\r
-class VmaSmallVector\r
-{\r
-public:\r
- typedef T value_type;\r
-\r
- VmaSmallVector(const AllocatorT& allocator) :\r
- m_Count(0),\r
- m_DynamicArray(allocator)\r
- {\r
- }\r
- VmaSmallVector(size_t count, const AllocatorT& allocator) :\r
- m_Count(count),\r
- m_DynamicArray(count > N ? count : 0, allocator)\r
- {\r
- }\r
- template<typename SrcT, typename SrcAllocatorT, size_t SrcN>\r
- VmaSmallVector(const VmaSmallVector<SrcT, SrcAllocatorT, SrcN>& src) = delete;\r
- template<typename SrcT, typename SrcAllocatorT, size_t SrcN>\r
- VmaSmallVector<T, AllocatorT, N>& operator=(const VmaSmallVector<SrcT, SrcAllocatorT, SrcN>& rhs) = delete;\r
-\r
- bool empty() const { return m_Count == 0; }\r
- size_t size() const { return m_Count; }\r
- T* data() { return m_Count > N ? m_DynamicArray.data() : m_StaticArray; }\r
- const T* data() const { return m_Count > N ? m_DynamicArray.data() : m_StaticArray; }\r
-\r
- T& operator[](size_t index)\r
- {\r
- VMA_HEAVY_ASSERT(index < m_Count);\r
- return data()[index];\r
- }\r
- const T& operator[](size_t index) const\r
- {\r
- VMA_HEAVY_ASSERT(index < m_Count);\r
- return data()[index];\r
- }\r
-\r
- T& front()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return data()[0];\r
- }\r
- const T& front() const\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return data()[0];\r
- }\r
- T& back()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return data()[m_Count - 1];\r
- }\r
- const T& back() const\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- return data()[m_Count - 1];\r
- }\r
-\r
- void resize(size_t newCount, bool freeMemory = false)\r
- {\r
- if(newCount > N && m_Count > N)\r
- {\r
- // Any direction, staying in m_DynamicArray\r
- m_DynamicArray.resize(newCount, freeMemory);\r
- }\r
- else if(newCount > N && m_Count <= N)\r
- {\r
- // Growing, moving from m_StaticArray to m_DynamicArray\r
- m_DynamicArray.resize(newCount, freeMemory);\r
- if(m_Count > 0)\r
- {\r
- memcpy(m_DynamicArray.data(), m_StaticArray, m_Count * sizeof(T));\r
- }\r
- }\r
- else if(newCount <= N && m_Count > N)\r
- {\r
- // Shrinking, moving from m_DynamicArray to m_StaticArray\r
- if(newCount > 0)\r
- {\r
- memcpy(m_StaticArray, m_DynamicArray.data(), newCount * sizeof(T));\r
- }\r
- m_DynamicArray.resize(0, freeMemory);\r
- }\r
- else\r
- {\r
- // Any direction, staying in m_StaticArray - nothing to do here\r
- }\r
- m_Count = newCount;\r
- }\r
-\r
- void clear(bool freeMemory = false)\r
- {\r
- m_DynamicArray.clear(freeMemory);\r
- m_Count = 0;\r
- }\r
-\r
- void insert(size_t index, const T& src)\r
- {\r
- VMA_HEAVY_ASSERT(index <= m_Count);\r
- const size_t oldCount = size();\r
- resize(oldCount + 1);\r
- T* const dataPtr = data();\r
- if(index < oldCount)\r
- {\r
- // I know, this could be more optimal for case where memmove can be memcpy directly from m_StaticArray to m_DynamicArray.\r
- memmove(dataPtr + (index + 1), dataPtr + index, (oldCount - index) * sizeof(T));\r
- }\r
- dataPtr[index] = src;\r
- }\r
-\r
- void remove(size_t index)\r
- {\r
- VMA_HEAVY_ASSERT(index < m_Count);\r
- const size_t oldCount = size();\r
- if(index < oldCount - 1)\r
- {\r
- // I know, this could be more optimal for case where memmove can be memcpy directly from m_DynamicArray to m_StaticArray.\r
- T* const dataPtr = data();\r
- memmove(dataPtr + index, dataPtr + (index + 1), (oldCount - index - 1) * sizeof(T));\r
- }\r
- resize(oldCount - 1);\r
- }\r
-\r
- void push_back(const T& src)\r
- {\r
- const size_t newIndex = size();\r
- resize(newIndex + 1);\r
- data()[newIndex] = src;\r
- }\r
-\r
- void pop_back()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- resize(size() - 1);\r
- }\r
-\r
- void push_front(const T& src)\r
- {\r
- insert(0, src);\r
- }\r
-\r
- void pop_front()\r
- {\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- remove(0);\r
- }\r
-\r
- typedef T* iterator;\r
-\r
- iterator begin() { return data(); }\r
- iterator end() { return data() + m_Count; }\r
-\r
-private:\r
- size_t m_Count;\r
- T m_StaticArray[N]; // Used when m_Size <= N\r
- VmaVector<T, AllocatorT> m_DynamicArray; // Used when m_Size > N\r
-};\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaPoolAllocator\r
-\r
-/*\r
-Allocator for objects of type T using a list of arrays (pools) to speed up\r
-allocation. Number of elements that can be allocated is not bounded because\r
-allocator can create multiple blocks.\r
-*/\r
-template<typename T>\r
-class VmaPoolAllocator\r
-{\r
- VMA_CLASS_NO_COPY(VmaPoolAllocator)\r
-public:\r
- VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity);\r
- ~VmaPoolAllocator();\r
- template<typename... Types> T* Alloc(Types... args);\r
- void Free(T* ptr);\r
-\r
-private:\r
- union Item\r
- {\r
- uint32_t NextFreeIndex;\r
- alignas(T) char Value[sizeof(T)];\r
- };\r
-\r
- struct ItemBlock\r
- {\r
- Item* pItems;\r
- uint32_t Capacity;\r
- uint32_t FirstFreeIndex;\r
- };\r
- \r
- const VkAllocationCallbacks* m_pAllocationCallbacks;\r
- const uint32_t m_FirstBlockCapacity;\r
- VmaVector< ItemBlock, VmaStlAllocator<ItemBlock> > m_ItemBlocks;\r
-\r
- ItemBlock& CreateNewBlock();\r
-};\r
-\r
-template<typename T>\r
-VmaPoolAllocator<T>::VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity) :\r
- m_pAllocationCallbacks(pAllocationCallbacks),\r
- m_FirstBlockCapacity(firstBlockCapacity),\r
- m_ItemBlocks(VmaStlAllocator<ItemBlock>(pAllocationCallbacks))\r
-{\r
- VMA_ASSERT(m_FirstBlockCapacity > 1);\r
-}\r
-\r
-template<typename T>\r
-VmaPoolAllocator<T>::~VmaPoolAllocator()\r
-{\r
- for(size_t i = m_ItemBlocks.size(); i--; )\r
- vma_delete_array(m_pAllocationCallbacks, m_ItemBlocks[i].pItems, m_ItemBlocks[i].Capacity);\r
- m_ItemBlocks.clear();\r
-}\r
-\r
-template<typename T>\r
-template<typename... Types> T* VmaPoolAllocator<T>::Alloc(Types... args)\r
-{\r
- for(size_t i = m_ItemBlocks.size(); i--; )\r
- {\r
- ItemBlock& block = m_ItemBlocks[i];\r
- // This block has some free items: Use first one.\r
- if(block.FirstFreeIndex != UINT32_MAX)\r
- {\r
- Item* const pItem = &block.pItems[block.FirstFreeIndex];\r
- block.FirstFreeIndex = pItem->NextFreeIndex;\r
- T* result = (T*)&pItem->Value;\r
- new(result)T(std::forward<Types>(args)...); // Explicit constructor call.\r
- return result;\r
- }\r
- }\r
-\r
- // No block has free item: Create new one and use it.\r
- ItemBlock& newBlock = CreateNewBlock();\r
- Item* const pItem = &newBlock.pItems[0];\r
- newBlock.FirstFreeIndex = pItem->NextFreeIndex;\r
- T* result = (T*)&pItem->Value;\r
- new(result)T(std::forward<Types>(args)...); // Explicit constructor call.\r
- return result;\r
-}\r
-\r
-template<typename T>\r
-void VmaPoolAllocator<T>::Free(T* ptr)\r
-{\r
- // Search all memory blocks to find ptr.\r
- for(size_t i = m_ItemBlocks.size(); i--; )\r
- {\r
- ItemBlock& block = m_ItemBlocks[i];\r
- \r
- // Casting to union.\r
- Item* pItemPtr;\r
- memcpy(&pItemPtr, &ptr, sizeof(pItemPtr));\r
- \r
- // Check if pItemPtr is in address range of this block.\r
- if((pItemPtr >= block.pItems) && (pItemPtr < block.pItems + block.Capacity))\r
- {\r
- ptr->~T(); // Explicit destructor call.\r
- const uint32_t index = static_cast<uint32_t>(pItemPtr - block.pItems);\r
- pItemPtr->NextFreeIndex = block.FirstFreeIndex;\r
- block.FirstFreeIndex = index;\r
- return;\r
- }\r
- }\r
- VMA_ASSERT(0 && "Pointer doesn't belong to this memory pool.");\r
-}\r
-\r
-template<typename T>\r
-typename VmaPoolAllocator<T>::ItemBlock& VmaPoolAllocator<T>::CreateNewBlock()\r
-{\r
- const uint32_t newBlockCapacity = m_ItemBlocks.empty() ?\r
- m_FirstBlockCapacity : m_ItemBlocks.back().Capacity * 3 / 2;\r
-\r
- const ItemBlock newBlock = {\r
- vma_new_array(m_pAllocationCallbacks, Item, newBlockCapacity),\r
- newBlockCapacity,\r
- 0 };\r
-\r
- m_ItemBlocks.push_back(newBlock);\r
-\r
- // Setup singly-linked list of all free items in this block.\r
- for(uint32_t i = 0; i < newBlockCapacity - 1; ++i)\r
- newBlock.pItems[i].NextFreeIndex = i + 1;\r
- newBlock.pItems[newBlockCapacity - 1].NextFreeIndex = UINT32_MAX;\r
- return m_ItemBlocks.back();\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaRawList, VmaList\r
-\r
-#if VMA_USE_STL_LIST\r
-\r
-#define VmaList std::list\r
-\r
-#else // #if VMA_USE_STL_LIST\r
-\r
-template<typename T>\r
-struct VmaListItem\r
-{\r
- VmaListItem* pPrev;\r
- VmaListItem* pNext;\r
- T Value;\r
-};\r
-\r
-// Doubly linked list.\r
-template<typename T>\r
-class VmaRawList\r
-{\r
- VMA_CLASS_NO_COPY(VmaRawList)\r
-public:\r
- typedef VmaListItem<T> ItemType;\r
-\r
- VmaRawList(const VkAllocationCallbacks* pAllocationCallbacks);\r
- ~VmaRawList();\r
- void Clear();\r
-\r
- size_t GetCount() const { return m_Count; }\r
- bool IsEmpty() const { return m_Count == 0; }\r
-\r
- ItemType* Front() { return m_pFront; }\r
- const ItemType* Front() const { return m_pFront; }\r
- ItemType* Back() { return m_pBack; }\r
- const ItemType* Back() const { return m_pBack; }\r
-\r
- ItemType* PushBack();\r
- ItemType* PushFront();\r
- ItemType* PushBack(const T& value);\r
- ItemType* PushFront(const T& value);\r
- void PopBack();\r
- void PopFront();\r
- \r
- // Item can be null - it means PushBack.\r
- ItemType* InsertBefore(ItemType* pItem);\r
- // Item can be null - it means PushFront.\r
- ItemType* InsertAfter(ItemType* pItem);\r
-\r
- ItemType* InsertBefore(ItemType* pItem, const T& value);\r
- ItemType* InsertAfter(ItemType* pItem, const T& value);\r
-\r
- void Remove(ItemType* pItem);\r
-\r
-private:\r
- const VkAllocationCallbacks* const m_pAllocationCallbacks;\r
- VmaPoolAllocator<ItemType> m_ItemAllocator;\r
- ItemType* m_pFront;\r
- ItemType* m_pBack;\r
- size_t m_Count;\r
-};\r
-\r
-template<typename T>\r
-VmaRawList<T>::VmaRawList(const VkAllocationCallbacks* pAllocationCallbacks) :\r
- m_pAllocationCallbacks(pAllocationCallbacks),\r
- m_ItemAllocator(pAllocationCallbacks, 128),\r
- m_pFront(VMA_NULL),\r
- m_pBack(VMA_NULL),\r
- m_Count(0)\r
-{\r
-}\r
-\r
-template<typename T>\r
-VmaRawList<T>::~VmaRawList()\r
-{\r
- // Intentionally not calling Clear, because that would be unnecessary\r
- // computations to return all items to m_ItemAllocator as free.\r
-}\r
-\r
-template<typename T>\r
-void VmaRawList<T>::Clear()\r
-{\r
- if(IsEmpty() == false)\r
- {\r
- ItemType* pItem = m_pBack;\r
- while(pItem != VMA_NULL)\r
- {\r
- ItemType* const pPrevItem = pItem->pPrev;\r
- m_ItemAllocator.Free(pItem);\r
- pItem = pPrevItem;\r
- }\r
- m_pFront = VMA_NULL;\r
- m_pBack = VMA_NULL;\r
- m_Count = 0;\r
- }\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::PushBack()\r
-{\r
- ItemType* const pNewItem = m_ItemAllocator.Alloc();\r
- pNewItem->pNext = VMA_NULL;\r
- if(IsEmpty())\r
- {\r
- pNewItem->pPrev = VMA_NULL;\r
- m_pFront = pNewItem;\r
- m_pBack = pNewItem;\r
- m_Count = 1;\r
- }\r
- else\r
- {\r
- pNewItem->pPrev = m_pBack;\r
- m_pBack->pNext = pNewItem;\r
- m_pBack = pNewItem;\r
- ++m_Count;\r
- }\r
- return pNewItem;\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::PushFront()\r
-{\r
- ItemType* const pNewItem = m_ItemAllocator.Alloc();\r
- pNewItem->pPrev = VMA_NULL;\r
- if(IsEmpty())\r
- {\r
- pNewItem->pNext = VMA_NULL;\r
- m_pFront = pNewItem;\r
- m_pBack = pNewItem;\r
- m_Count = 1;\r
- }\r
- else\r
- {\r
- pNewItem->pNext = m_pFront;\r
- m_pFront->pPrev = pNewItem;\r
- m_pFront = pNewItem;\r
- ++m_Count;\r
- }\r
- return pNewItem;\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::PushBack(const T& value)\r
-{\r
- ItemType* const pNewItem = PushBack();\r
- pNewItem->Value = value;\r
- return pNewItem;\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::PushFront(const T& value)\r
-{\r
- ItemType* const pNewItem = PushFront();\r
- pNewItem->Value = value;\r
- return pNewItem;\r
-}\r
-\r
-template<typename T>\r
-void VmaRawList<T>::PopBack()\r
-{\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- ItemType* const pBackItem = m_pBack;\r
- ItemType* const pPrevItem = pBackItem->pPrev;\r
- if(pPrevItem != VMA_NULL)\r
- {\r
- pPrevItem->pNext = VMA_NULL;\r
- }\r
- m_pBack = pPrevItem;\r
- m_ItemAllocator.Free(pBackItem);\r
- --m_Count;\r
-}\r
-\r
-template<typename T>\r
-void VmaRawList<T>::PopFront()\r
-{\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
- ItemType* const pFrontItem = m_pFront;\r
- ItemType* const pNextItem = pFrontItem->pNext;\r
- if(pNextItem != VMA_NULL)\r
- {\r
- pNextItem->pPrev = VMA_NULL;\r
- }\r
- m_pFront = pNextItem;\r
- m_ItemAllocator.Free(pFrontItem);\r
- --m_Count;\r
-}\r
-\r
-template<typename T>\r
-void VmaRawList<T>::Remove(ItemType* pItem)\r
-{\r
- VMA_HEAVY_ASSERT(pItem != VMA_NULL);\r
- VMA_HEAVY_ASSERT(m_Count > 0);\r
-\r
- if(pItem->pPrev != VMA_NULL)\r
- {\r
- pItem->pPrev->pNext = pItem->pNext;\r
- }\r
- else\r
- {\r
- VMA_HEAVY_ASSERT(m_pFront == pItem);\r
- m_pFront = pItem->pNext;\r
- }\r
-\r
- if(pItem->pNext != VMA_NULL)\r
- {\r
- pItem->pNext->pPrev = pItem->pPrev;\r
- }\r
- else\r
- {\r
- VMA_HEAVY_ASSERT(m_pBack == pItem);\r
- m_pBack = pItem->pPrev;\r
- }\r
-\r
- m_ItemAllocator.Free(pItem);\r
- --m_Count;\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::InsertBefore(ItemType* pItem)\r
-{\r
- if(pItem != VMA_NULL)\r
- {\r
- ItemType* const prevItem = pItem->pPrev;\r
- ItemType* const newItem = m_ItemAllocator.Alloc();\r
- newItem->pPrev = prevItem;\r
- newItem->pNext = pItem;\r
- pItem->pPrev = newItem;\r
- if(prevItem != VMA_NULL)\r
- {\r
- prevItem->pNext = newItem;\r
- }\r
- else\r
- {\r
- VMA_HEAVY_ASSERT(m_pFront == pItem);\r
- m_pFront = newItem;\r
- }\r
- ++m_Count;\r
- return newItem;\r
- }\r
- else\r
- return PushBack();\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::InsertAfter(ItemType* pItem)\r
-{\r
- if(pItem != VMA_NULL)\r
- {\r
- ItemType* const nextItem = pItem->pNext;\r
- ItemType* const newItem = m_ItemAllocator.Alloc();\r
- newItem->pNext = nextItem;\r
- newItem->pPrev = pItem;\r
- pItem->pNext = newItem;\r
- if(nextItem != VMA_NULL)\r
- {\r
- nextItem->pPrev = newItem;\r
- }\r
- else\r
- {\r
- VMA_HEAVY_ASSERT(m_pBack == pItem);\r
- m_pBack = newItem;\r
- }\r
- ++m_Count;\r
- return newItem;\r
- }\r
- else\r
- return PushFront();\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::InsertBefore(ItemType* pItem, const T& value)\r
-{\r
- ItemType* const newItem = InsertBefore(pItem);\r
- newItem->Value = value;\r
- return newItem;\r
-}\r
-\r
-template<typename T>\r
-VmaListItem<T>* VmaRawList<T>::InsertAfter(ItemType* pItem, const T& value)\r
-{\r
- ItemType* const newItem = InsertAfter(pItem);\r
- newItem->Value = value;\r
- return newItem;\r
-}\r
-\r
-template<typename T, typename AllocatorT>\r
-class VmaList\r
-{\r
- VMA_CLASS_NO_COPY(VmaList)\r
-public:\r
- class iterator\r
- {\r
- public:\r
- iterator() :\r
- m_pList(VMA_NULL),\r
- m_pItem(VMA_NULL)\r
- {\r
- }\r
-\r
- T& operator*() const\r
- {\r
- VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);\r
- return m_pItem->Value;\r
- }\r
- T* operator->() const\r
- {\r
- VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);\r
- return &m_pItem->Value;\r
- }\r
-\r
- iterator& operator++()\r
- {\r
- VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);\r
- m_pItem = m_pItem->pNext;\r
- return *this;\r
- }\r
- iterator& operator--()\r
- {\r
- if(m_pItem != VMA_NULL)\r
- {\r
- m_pItem = m_pItem->pPrev;\r
- }\r
- else\r
- {\r
- VMA_HEAVY_ASSERT(!m_pList->IsEmpty());\r
- m_pItem = m_pList->Back();\r
- }\r
- return *this;\r
- }\r
-\r
- iterator operator++(int)\r
- {\r
- iterator result = *this;\r
- ++*this;\r
- return result;\r
- }\r
- iterator operator--(int)\r
- {\r
- iterator result = *this;\r
- --*this;\r
- return result;\r
- }\r
-\r
- bool operator==(const iterator& rhs) const\r
- {\r
- VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);\r
- return m_pItem == rhs.m_pItem;\r
- }\r
- bool operator!=(const iterator& rhs) const\r
- {\r
- VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);\r
- return m_pItem != rhs.m_pItem;\r
- }\r
- \r
- private:\r
- VmaRawList<T>* m_pList;\r
- VmaListItem<T>* m_pItem;\r
-\r
- iterator(VmaRawList<T>* pList, VmaListItem<T>* pItem) :\r
- m_pList(pList),\r
- m_pItem(pItem)\r
- {\r
- }\r
-\r
- friend class VmaList<T, AllocatorT>;\r
- };\r
-\r
- class const_iterator\r
- {\r
- public:\r
- const_iterator() :\r
- m_pList(VMA_NULL),\r
- m_pItem(VMA_NULL)\r
- {\r
- }\r
-\r
- const_iterator(const iterator& src) :\r
- m_pList(src.m_pList),\r
- m_pItem(src.m_pItem)\r
- {\r
- }\r
- \r
- const T& operator*() const\r
- {\r
- VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);\r
- return m_pItem->Value;\r
- }\r
- const T* operator->() const\r
- {\r
- VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);\r
- return &m_pItem->Value;\r
- }\r
-\r
- const_iterator& operator++()\r
- {\r
- VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);\r
- m_pItem = m_pItem->pNext;\r
- return *this;\r
- }\r
- const_iterator& operator--()\r
- {\r
- if(m_pItem != VMA_NULL)\r
- {\r
- m_pItem = m_pItem->pPrev;\r
- }\r
- else\r
- {\r
- VMA_HEAVY_ASSERT(!m_pList->IsEmpty());\r
- m_pItem = m_pList->Back();\r
- }\r
- return *this;\r
- }\r
-\r
- const_iterator operator++(int)\r
- {\r
- const_iterator result = *this;\r
- ++*this;\r
- return result;\r
- }\r
- const_iterator operator--(int)\r
- {\r
- const_iterator result = *this;\r
- --*this;\r
- return result;\r
- }\r
-\r
- bool operator==(const const_iterator& rhs) const\r
- {\r
- VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);\r
- return m_pItem == rhs.m_pItem;\r
- }\r
- bool operator!=(const const_iterator& rhs) const\r
- {\r
- VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);\r
- return m_pItem != rhs.m_pItem;\r
- }\r
- \r
- private:\r
- const_iterator(const VmaRawList<T>* pList, const VmaListItem<T>* pItem) :\r
- m_pList(pList),\r
- m_pItem(pItem)\r
- {\r
- }\r
-\r
- const VmaRawList<T>* m_pList;\r
- const VmaListItem<T>* m_pItem;\r
-\r
- friend class VmaList<T, AllocatorT>;\r
- };\r
-\r
- VmaList(const AllocatorT& allocator) : m_RawList(allocator.m_pCallbacks) { }\r
-\r
- bool empty() const { return m_RawList.IsEmpty(); }\r
- size_t size() const { return m_RawList.GetCount(); }\r
-\r
- iterator begin() { return iterator(&m_RawList, m_RawList.Front()); }\r
- iterator end() { return iterator(&m_RawList, VMA_NULL); }\r
-\r
- const_iterator cbegin() const { return const_iterator(&m_RawList, m_RawList.Front()); }\r
- const_iterator cend() const { return const_iterator(&m_RawList, VMA_NULL); }\r
-\r
- void clear() { m_RawList.Clear(); }\r
- void push_back(const T& value) { m_RawList.PushBack(value); }\r
- void erase(iterator it) { m_RawList.Remove(it.m_pItem); }\r
- iterator insert(iterator it, const T& value) { return iterator(&m_RawList, m_RawList.InsertBefore(it.m_pItem, value)); }\r
-\r
-private:\r
- VmaRawList<T> m_RawList;\r
-};\r
-\r
-#endif // #if VMA_USE_STL_LIST\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaMap\r
-\r
-// Unused in this version.\r
-#if 0\r
-\r
-#if VMA_USE_STL_UNORDERED_MAP\r
-\r
-#define VmaPair std::pair\r
-\r
-#define VMA_MAP_TYPE(KeyT, ValueT) \\r
- std::unordered_map< KeyT, ValueT, std::hash<KeyT>, std::equal_to<KeyT>, VmaStlAllocator< std::pair<KeyT, ValueT> > >\r
-\r
-#else // #if VMA_USE_STL_UNORDERED_MAP\r
-\r
-template<typename T1, typename T2>\r
-struct VmaPair\r
-{\r
- T1 first;\r
- T2 second;\r
-\r
- VmaPair() : first(), second() { }\r
- VmaPair(const T1& firstSrc, const T2& secondSrc) : first(firstSrc), second(secondSrc) { }\r
-};\r
-\r
-/* Class compatible with subset of interface of std::unordered_map.\r
-KeyT, ValueT must be POD because they will be stored in VmaVector.\r
-*/\r
-template<typename KeyT, typename ValueT>\r
-class VmaMap\r
-{\r
-public:\r
- typedef VmaPair<KeyT, ValueT> PairType;\r
- typedef PairType* iterator;\r
-\r
- VmaMap(const VmaStlAllocator<PairType>& allocator) : m_Vector(allocator) { }\r
-\r
- iterator begin() { return m_Vector.begin(); }\r
- iterator end() { return m_Vector.end(); }\r
-\r
- void insert(const PairType& pair);\r
- iterator find(const KeyT& key);\r
- void erase(iterator it);\r
- \r
-private:\r
- VmaVector< PairType, VmaStlAllocator<PairType> > m_Vector;\r
-};\r
-\r
-#define VMA_MAP_TYPE(KeyT, ValueT) VmaMap<KeyT, ValueT>\r
-\r
-template<typename FirstT, typename SecondT>\r
-struct VmaPairFirstLess\r
-{\r
- bool operator()(const VmaPair<FirstT, SecondT>& lhs, const VmaPair<FirstT, SecondT>& rhs) const\r
- {\r
- return lhs.first < rhs.first;\r
- }\r
- bool operator()(const VmaPair<FirstT, SecondT>& lhs, const FirstT& rhsFirst) const\r
- {\r
- return lhs.first < rhsFirst;\r
- }\r
-};\r
-\r
-template<typename KeyT, typename ValueT>\r
-void VmaMap<KeyT, ValueT>::insert(const PairType& pair)\r
-{\r
- const size_t indexToInsert = VmaBinaryFindFirstNotLess(\r
- m_Vector.data(),\r
- m_Vector.data() + m_Vector.size(),\r
- pair,\r
- VmaPairFirstLess<KeyT, ValueT>()) - m_Vector.data();\r
- VmaVectorInsert(m_Vector, indexToInsert, pair);\r
-}\r
-\r
-template<typename KeyT, typename ValueT>\r
-VmaPair<KeyT, ValueT>* VmaMap<KeyT, ValueT>::find(const KeyT& key)\r
-{\r
- PairType* it = VmaBinaryFindFirstNotLess(\r
- m_Vector.data(),\r
- m_Vector.data() + m_Vector.size(),\r
- key,\r
- VmaPairFirstLess<KeyT, ValueT>());\r
- if((it != m_Vector.end()) && (it->first == key))\r
- {\r
- return it;\r
- }\r
- else\r
- {\r
- return m_Vector.end();\r
- }\r
-}\r
-\r
-template<typename KeyT, typename ValueT>\r
-void VmaMap<KeyT, ValueT>::erase(iterator it)\r
-{\r
- VmaVectorRemove(m_Vector, it - m_Vector.begin());\r
-}\r
-\r
-#endif // #if VMA_USE_STL_UNORDERED_MAP\r
-\r
-#endif // #if 0\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-\r
-class VmaDeviceMemoryBlock;\r
-\r
-enum VMA_CACHE_OPERATION { VMA_CACHE_FLUSH, VMA_CACHE_INVALIDATE };\r
-\r
-struct VmaAllocation_T\r
-{\r
-private:\r
- static const uint8_t MAP_COUNT_FLAG_PERSISTENT_MAP = 0x80;\r
-\r
- enum FLAGS\r
- {\r
- FLAG_USER_DATA_STRING = 0x01,\r
- };\r
-\r
-public:\r
- enum ALLOCATION_TYPE\r
- {\r
- ALLOCATION_TYPE_NONE,\r
- ALLOCATION_TYPE_BLOCK,\r
- ALLOCATION_TYPE_DEDICATED,\r
- };\r
-\r
- /*\r
- This struct is allocated using VmaPoolAllocator.\r
- */\r
-\r
- VmaAllocation_T(uint32_t currentFrameIndex, bool userDataString) :\r
- m_Alignment{1},\r
- m_Size{0},\r
- m_pUserData{VMA_NULL},\r
- m_LastUseFrameIndex{currentFrameIndex},\r
- m_MemoryTypeIndex{0},\r
- m_Type{(uint8_t)ALLOCATION_TYPE_NONE},\r
- m_SuballocationType{(uint8_t)VMA_SUBALLOCATION_TYPE_UNKNOWN},\r
- m_MapCount{0},\r
- m_Flags{userDataString ? (uint8_t)FLAG_USER_DATA_STRING : (uint8_t)0}\r
- {\r
-#if VMA_STATS_STRING_ENABLED\r
- m_CreationFrameIndex = currentFrameIndex;\r
- m_BufferImageUsage = 0;\r
-#endif\r
- }\r
-\r
- ~VmaAllocation_T()\r
- {\r
- VMA_ASSERT((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) == 0 && "Allocation was not unmapped before destruction.");\r
-\r
- // Check if owned string was freed.\r
- VMA_ASSERT(m_pUserData == VMA_NULL);\r
- }\r
-\r
- void InitBlockAllocation(\r
- VmaDeviceMemoryBlock* block,\r
- VkDeviceSize offset,\r
- VkDeviceSize alignment,\r
- VkDeviceSize size,\r
- uint32_t memoryTypeIndex,\r
- VmaSuballocationType suballocationType,\r
- bool mapped,\r
- bool canBecomeLost)\r
- {\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_NONE);\r
- VMA_ASSERT(block != VMA_NULL);\r
- m_Type = (uint8_t)ALLOCATION_TYPE_BLOCK;\r
- m_Alignment = alignment;\r
- m_Size = size;\r
- m_MemoryTypeIndex = memoryTypeIndex;\r
- m_MapCount = mapped ? MAP_COUNT_FLAG_PERSISTENT_MAP : 0;\r
- m_SuballocationType = (uint8_t)suballocationType;\r
- m_BlockAllocation.m_Block = block;\r
- m_BlockAllocation.m_Offset = offset;\r
- m_BlockAllocation.m_CanBecomeLost = canBecomeLost;\r
- }\r
-\r
- void InitLost()\r
- {\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_NONE);\r
- VMA_ASSERT(m_LastUseFrameIndex.load() == VMA_FRAME_INDEX_LOST);\r
- m_Type = (uint8_t)ALLOCATION_TYPE_BLOCK;\r
- m_MemoryTypeIndex = 0;\r
- m_BlockAllocation.m_Block = VMA_NULL;\r
- m_BlockAllocation.m_Offset = 0;\r
- m_BlockAllocation.m_CanBecomeLost = true;\r
- }\r
-\r
- void ChangeBlockAllocation(\r
- VmaAllocator hAllocator,\r
- VmaDeviceMemoryBlock* block,\r
- VkDeviceSize offset); \r
-\r
- void ChangeOffset(VkDeviceSize newOffset);\r
-\r
- // pMappedData not null means allocation is created with MAPPED flag.\r
- void InitDedicatedAllocation(\r
- uint32_t memoryTypeIndex,\r
- VkDeviceMemory hMemory,\r
- VmaSuballocationType suballocationType,\r
- void* pMappedData,\r
- VkDeviceSize size)\r
- {\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_NONE);\r
- VMA_ASSERT(hMemory != VK_NULL_HANDLE);\r
- m_Type = (uint8_t)ALLOCATION_TYPE_DEDICATED;\r
- m_Alignment = 0;\r
- m_Size = size;\r
- m_MemoryTypeIndex = memoryTypeIndex;\r
- m_SuballocationType = (uint8_t)suballocationType;\r
- m_MapCount = (pMappedData != VMA_NULL) ? MAP_COUNT_FLAG_PERSISTENT_MAP : 0;\r
- m_DedicatedAllocation.m_hMemory = hMemory;\r
- m_DedicatedAllocation.m_pMappedData = pMappedData;\r
- }\r
-\r
- ALLOCATION_TYPE GetType() const { return (ALLOCATION_TYPE)m_Type; }\r
- VkDeviceSize GetAlignment() const { return m_Alignment; }\r
- VkDeviceSize GetSize() const { return m_Size; }\r
- bool IsUserDataString() const { return (m_Flags & FLAG_USER_DATA_STRING) != 0; }\r
- void* GetUserData() const { return m_pUserData; }\r
- void SetUserData(VmaAllocator hAllocator, void* pUserData);\r
- VmaSuballocationType GetSuballocationType() const { return (VmaSuballocationType)m_SuballocationType; }\r
-\r
- VmaDeviceMemoryBlock* GetBlock() const\r
- {\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_BLOCK);\r
- return m_BlockAllocation.m_Block;\r
- }\r
- VkDeviceSize GetOffset() const;\r
- VkDeviceMemory GetMemory() const;\r
- uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; }\r
- bool IsPersistentMap() const { return (m_MapCount & MAP_COUNT_FLAG_PERSISTENT_MAP) != 0; }\r
- void* GetMappedData() const;\r
- bool CanBecomeLost() const;\r
- \r
- uint32_t GetLastUseFrameIndex() const\r
- {\r
- return m_LastUseFrameIndex.load();\r
- }\r
- bool CompareExchangeLastUseFrameIndex(uint32_t& expected, uint32_t desired)\r
- {\r
- return m_LastUseFrameIndex.compare_exchange_weak(expected, desired);\r
- }\r
- /*\r
- - If hAllocation.LastUseFrameIndex + frameInUseCount < allocator.CurrentFrameIndex,\r
- makes it lost by setting LastUseFrameIndex = VMA_FRAME_INDEX_LOST and returns true.\r
- - Else, returns false.\r
- \r
- If hAllocation is already lost, assert - you should not call it then.\r
- If hAllocation was not created with CAN_BECOME_LOST_BIT, assert.\r
- */\r
- bool MakeLost(uint32_t currentFrameIndex, uint32_t frameInUseCount);\r
-\r
- void DedicatedAllocCalcStatsInfo(VmaStatInfo& outInfo)\r
- {\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_DEDICATED);\r
- outInfo.blockCount = 1;\r
- outInfo.allocationCount = 1;\r
- outInfo.unusedRangeCount = 0;\r
- outInfo.usedBytes = m_Size;\r
- outInfo.unusedBytes = 0;\r
- outInfo.allocationSizeMin = outInfo.allocationSizeMax = m_Size;\r
- outInfo.unusedRangeSizeMin = UINT64_MAX;\r
- outInfo.unusedRangeSizeMax = 0;\r
- }\r
-\r
- void BlockAllocMap();\r
- void BlockAllocUnmap();\r
- VkResult DedicatedAllocMap(VmaAllocator hAllocator, void** ppData);\r
- void DedicatedAllocUnmap(VmaAllocator hAllocator);\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- uint32_t GetCreationFrameIndex() const { return m_CreationFrameIndex; }\r
- uint32_t GetBufferImageUsage() const { return m_BufferImageUsage; }\r
-\r
- void InitBufferImageUsage(uint32_t bufferImageUsage)\r
- {\r
- VMA_ASSERT(m_BufferImageUsage == 0);\r
- m_BufferImageUsage = bufferImageUsage;\r
- }\r
-\r
- void PrintParameters(class VmaJsonWriter& json) const;\r
-#endif\r
-\r
-private:\r
- VkDeviceSize m_Alignment;\r
- VkDeviceSize m_Size;\r
- void* m_pUserData;\r
- VMA_ATOMIC_UINT32 m_LastUseFrameIndex;\r
- uint32_t m_MemoryTypeIndex;\r
- uint8_t m_Type; // ALLOCATION_TYPE\r
- uint8_t m_SuballocationType; // VmaSuballocationType\r
- // Bit 0x80 is set when allocation was created with VMA_ALLOCATION_CREATE_MAPPED_BIT.\r
- // Bits with mask 0x7F are reference counter for vmaMapMemory()/vmaUnmapMemory().\r
- uint8_t m_MapCount;\r
- uint8_t m_Flags; // enum FLAGS\r
-\r
- // Allocation out of VmaDeviceMemoryBlock.\r
- struct BlockAllocation\r
- {\r
- VmaDeviceMemoryBlock* m_Block;\r
- VkDeviceSize m_Offset;\r
- bool m_CanBecomeLost;\r
- };\r
-\r
- // Allocation for an object that has its own private VkDeviceMemory.\r
- struct DedicatedAllocation\r
- {\r
- VkDeviceMemory m_hMemory;\r
- void* m_pMappedData; // Not null means memory is mapped.\r
- };\r
-\r
- union\r
- {\r
- // Allocation out of VmaDeviceMemoryBlock.\r
- BlockAllocation m_BlockAllocation;\r
- // Allocation for an object that has its own private VkDeviceMemory.\r
- DedicatedAllocation m_DedicatedAllocation;\r
- };\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- uint32_t m_CreationFrameIndex;\r
- uint32_t m_BufferImageUsage; // 0 if unknown.\r
-#endif\r
-\r
- void FreeUserDataString(VmaAllocator hAllocator);\r
-};\r
-\r
-/*\r
-Represents a region of VmaDeviceMemoryBlock that is either assigned and returned as\r
-allocated memory block or free.\r
-*/\r
-struct VmaSuballocation\r
-{\r
- VkDeviceSize offset;\r
- VkDeviceSize size;\r
- VmaAllocation hAllocation;\r
- VmaSuballocationType type;\r
-};\r
-\r
-// Comparator for offsets.\r
-struct VmaSuballocationOffsetLess\r
-{\r
- bool operator()(const VmaSuballocation& lhs, const VmaSuballocation& rhs) const\r
- {\r
- return lhs.offset < rhs.offset;\r
- }\r
-};\r
-struct VmaSuballocationOffsetGreater\r
-{\r
- bool operator()(const VmaSuballocation& lhs, const VmaSuballocation& rhs) const\r
- {\r
- return lhs.offset > rhs.offset;\r
- }\r
-};\r
-\r
-typedef VmaList< VmaSuballocation, VmaStlAllocator<VmaSuballocation> > VmaSuballocationList;\r
-\r
-// Cost of one additional allocation lost, as equivalent in bytes.\r
-static const VkDeviceSize VMA_LOST_ALLOCATION_COST = 1048576;\r
-\r
-enum class VmaAllocationRequestType\r
-{\r
- Normal,\r
- // Used by "Linear" algorithm.\r
- UpperAddress,\r
- EndOf1st,\r
- EndOf2nd,\r
-};\r
-\r
-/*\r
-Parameters of planned allocation inside a VmaDeviceMemoryBlock.\r
-\r
-If canMakeOtherLost was false:\r
-- item points to a FREE suballocation.\r
-- itemsToMakeLostCount is 0.\r
-\r
-If canMakeOtherLost was true:\r
-- item points to first of sequence of suballocations, which are either FREE,\r
- or point to VmaAllocations that can become lost.\r
-- itemsToMakeLostCount is the number of VmaAllocations that need to be made lost for\r
- the requested allocation to succeed.\r
-*/\r
-struct VmaAllocationRequest\r
-{\r
- VkDeviceSize offset;\r
- VkDeviceSize sumFreeSize; // Sum size of free items that overlap with proposed allocation.\r
- VkDeviceSize sumItemSize; // Sum size of items to make lost that overlap with proposed allocation.\r
- VmaSuballocationList::iterator item;\r
- size_t itemsToMakeLostCount;\r
- void* customData;\r
- VmaAllocationRequestType type;\r
-\r
- VkDeviceSize CalcCost() const\r
- {\r
- return sumItemSize + itemsToMakeLostCount * VMA_LOST_ALLOCATION_COST;\r
- }\r
-};\r
-\r
-/*\r
-Data structure used for bookkeeping of allocations and unused ranges of memory\r
-in a single VkDeviceMemory block.\r
-*/\r
-class VmaBlockMetadata\r
-{\r
-public:\r
- VmaBlockMetadata(VmaAllocator hAllocator);\r
- virtual ~VmaBlockMetadata() { }\r
- virtual void Init(VkDeviceSize size) { m_Size = size; }\r
-\r
- // Validates all data structures inside this object. If not valid, returns false.\r
- virtual bool Validate() const = 0;\r
- VkDeviceSize GetSize() const { return m_Size; }\r
- virtual size_t GetAllocationCount() const = 0;\r
- virtual VkDeviceSize GetSumFreeSize() const = 0;\r
- virtual VkDeviceSize GetUnusedRangeSizeMax() const = 0;\r
- // Returns true if this block is empty - contains only single free suballocation.\r
- virtual bool IsEmpty() const = 0;\r
-\r
- virtual void CalcAllocationStatInfo(VmaStatInfo& outInfo) const = 0;\r
- // Shouldn't modify blockCount.\r
- virtual void AddPoolStats(VmaPoolStats& inoutStats) const = 0;\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- virtual void PrintDetailedMap(class VmaJsonWriter& json) const = 0;\r
-#endif\r
-\r
- // Tries to find a place for suballocation with given parameters inside this block.\r
- // If succeeded, fills pAllocationRequest and returns true.\r
- // If failed, returns false.\r
- virtual bool CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- // Always one of VMA_ALLOCATION_CREATE_STRATEGY_* or VMA_ALLOCATION_INTERNAL_STRATEGY_* flags.\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest) = 0;\r
-\r
- virtual bool MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest) = 0;\r
-\r
- virtual uint32_t MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount) = 0;\r
-\r
- virtual VkResult CheckCorruption(const void* pBlockData) = 0;\r
-\r
- // Makes actual allocation based on request. Request must already be checked and valid.\r
- virtual void Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation) = 0;\r
-\r
- // Frees suballocation assigned to given memory region.\r
- virtual void Free(const VmaAllocation allocation) = 0;\r
- virtual void FreeAtOffset(VkDeviceSize offset) = 0;\r
-\r
-protected:\r
- const VkAllocationCallbacks* GetAllocationCallbacks() const { return m_pAllocationCallbacks; }\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- void PrintDetailedMap_Begin(class VmaJsonWriter& json,\r
- VkDeviceSize unusedBytes,\r
- size_t allocationCount,\r
- size_t unusedRangeCount) const;\r
- void PrintDetailedMap_Allocation(class VmaJsonWriter& json,\r
- VkDeviceSize offset,\r
- VmaAllocation hAllocation) const;\r
- void PrintDetailedMap_UnusedRange(class VmaJsonWriter& json,\r
- VkDeviceSize offset,\r
- VkDeviceSize size) const;\r
- void PrintDetailedMap_End(class VmaJsonWriter& json) const;\r
-#endif\r
-\r
-private:\r
- VkDeviceSize m_Size;\r
- const VkAllocationCallbacks* m_pAllocationCallbacks;\r
-};\r
-\r
-#define VMA_VALIDATE(cond) do { if(!(cond)) { \\r
- VMA_ASSERT(0 && "Validation failed: " #cond); \\r
- return false; \\r
- } } while(false)\r
-\r
-class VmaBlockMetadata_Generic : public VmaBlockMetadata\r
-{\r
- VMA_CLASS_NO_COPY(VmaBlockMetadata_Generic)\r
-public:\r
- VmaBlockMetadata_Generic(VmaAllocator hAllocator);\r
- virtual ~VmaBlockMetadata_Generic();\r
- virtual void Init(VkDeviceSize size);\r
-\r
- virtual bool Validate() const;\r
- virtual size_t GetAllocationCount() const { return m_Suballocations.size() - m_FreeCount; }\r
- virtual VkDeviceSize GetSumFreeSize() const { return m_SumFreeSize; }\r
- virtual VkDeviceSize GetUnusedRangeSizeMax() const;\r
- virtual bool IsEmpty() const;\r
-\r
- virtual void CalcAllocationStatInfo(VmaStatInfo& outInfo) const;\r
- virtual void AddPoolStats(VmaPoolStats& inoutStats) const;\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- virtual void PrintDetailedMap(class VmaJsonWriter& json) const;\r
-#endif\r
-\r
- virtual bool CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest);\r
-\r
- virtual bool MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest);\r
-\r
- virtual uint32_t MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount);\r
-\r
- virtual VkResult CheckCorruption(const void* pBlockData);\r
-\r
- virtual void Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation);\r
-\r
- virtual void Free(const VmaAllocation allocation);\r
- virtual void FreeAtOffset(VkDeviceSize offset);\r
-\r
- ////////////////////////////////////////////////////////////////////////////////\r
- // For defragmentation\r
- \r
- bool IsBufferImageGranularityConflictPossible(\r
- VkDeviceSize bufferImageGranularity,\r
- VmaSuballocationType& inOutPrevSuballocType) const;\r
-\r
-private:\r
- friend class VmaDefragmentationAlgorithm_Generic;\r
- friend class VmaDefragmentationAlgorithm_Fast;\r
-\r
- uint32_t m_FreeCount;\r
- VkDeviceSize m_SumFreeSize;\r
- VmaSuballocationList m_Suballocations;\r
- // Suballocations that are free and have size greater than certain threshold.\r
- // Sorted by size, ascending.\r
- VmaVector< VmaSuballocationList::iterator, VmaStlAllocator< VmaSuballocationList::iterator > > m_FreeSuballocationsBySize;\r
-\r
- bool ValidateFreeSuballocationList() const;\r
-\r
- // Checks if requested suballocation with given parameters can be placed in given pFreeSuballocItem.\r
- // If yes, fills pOffset and returns true. If no, returns false.\r
- bool CheckAllocation(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- VmaSuballocationType allocType,\r
- VmaSuballocationList::const_iterator suballocItem,\r
- bool canMakeOtherLost,\r
- VkDeviceSize* pOffset,\r
- size_t* itemsToMakeLostCount,\r
- VkDeviceSize* pSumFreeSize,\r
- VkDeviceSize* pSumItemSize) const;\r
- // Given free suballocation, it merges it with following one, which must also be free.\r
- void MergeFreeWithNext(VmaSuballocationList::iterator item);\r
- // Releases given suballocation, making it free.\r
- // Merges it with adjacent free suballocations if applicable.\r
- // Returns iterator to new free suballocation at this place.\r
- VmaSuballocationList::iterator FreeSuballocation(VmaSuballocationList::iterator suballocItem);\r
- // Given free suballocation, it inserts it into sorted list of\r
- // m_FreeSuballocationsBySize if it's suitable.\r
- void RegisterFreeSuballocation(VmaSuballocationList::iterator item);\r
- // Given free suballocation, it removes it from sorted list of\r
- // m_FreeSuballocationsBySize if it's suitable.\r
- void UnregisterFreeSuballocation(VmaSuballocationList::iterator item);\r
-};\r
-\r
-/*\r
-Allocations and their references in internal data structure look like this:\r
-\r
-if(m_2ndVectorMode == SECOND_VECTOR_EMPTY):\r
-\r
- 0 +-------+\r
- | |\r
- | |\r
- | |\r
- +-------+\r
- | Alloc | 1st[m_1stNullItemsBeginCount]\r
- +-------+\r
- | Alloc | 1st[m_1stNullItemsBeginCount + 1]\r
- +-------+\r
- | ... |\r
- +-------+\r
- | Alloc | 1st[1st.size() - 1]\r
- +-------+\r
- | |\r
- | |\r
- | |\r
-GetSize() +-------+\r
-\r
-if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER):\r
-\r
- 0 +-------+\r
- | Alloc | 2nd[0]\r
- +-------+\r
- | Alloc | 2nd[1]\r
- +-------+\r
- | ... |\r
- +-------+\r
- | Alloc | 2nd[2nd.size() - 1]\r
- +-------+\r
- | |\r
- | |\r
- | |\r
- +-------+\r
- | Alloc | 1st[m_1stNullItemsBeginCount]\r
- +-------+\r
- | Alloc | 1st[m_1stNullItemsBeginCount + 1]\r
- +-------+\r
- | ... |\r
- +-------+\r
- | Alloc | 1st[1st.size() - 1]\r
- +-------+\r
- | |\r
-GetSize() +-------+\r
-\r
-if(m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK):\r
-\r
- 0 +-------+\r
- | |\r
- | |\r
- | |\r
- +-------+\r
- | Alloc | 1st[m_1stNullItemsBeginCount]\r
- +-------+\r
- | Alloc | 1st[m_1stNullItemsBeginCount + 1]\r
- +-------+\r
- | ... |\r
- +-------+\r
- | Alloc | 1st[1st.size() - 1]\r
- +-------+\r
- | |\r
- | |\r
- | |\r
- +-------+\r
- | Alloc | 2nd[2nd.size() - 1]\r
- +-------+\r
- | ... |\r
- +-------+\r
- | Alloc | 2nd[1]\r
- +-------+\r
- | Alloc | 2nd[0]\r
-GetSize() +-------+\r
-\r
-*/\r
-class VmaBlockMetadata_Linear : public VmaBlockMetadata\r
-{\r
- VMA_CLASS_NO_COPY(VmaBlockMetadata_Linear)\r
-public:\r
- VmaBlockMetadata_Linear(VmaAllocator hAllocator);\r
- virtual ~VmaBlockMetadata_Linear();\r
- virtual void Init(VkDeviceSize size);\r
-\r
- virtual bool Validate() const;\r
- virtual size_t GetAllocationCount() const;\r
- virtual VkDeviceSize GetSumFreeSize() const { return m_SumFreeSize; }\r
- virtual VkDeviceSize GetUnusedRangeSizeMax() const;\r
- virtual bool IsEmpty() const { return GetAllocationCount() == 0; }\r
-\r
- virtual void CalcAllocationStatInfo(VmaStatInfo& outInfo) const;\r
- virtual void AddPoolStats(VmaPoolStats& inoutStats) const;\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- virtual void PrintDetailedMap(class VmaJsonWriter& json) const;\r
-#endif\r
-\r
- virtual bool CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest);\r
-\r
- virtual bool MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest);\r
-\r
- virtual uint32_t MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount);\r
-\r
- virtual VkResult CheckCorruption(const void* pBlockData);\r
-\r
- virtual void Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation);\r
-\r
- virtual void Free(const VmaAllocation allocation);\r
- virtual void FreeAtOffset(VkDeviceSize offset);\r
-\r
-private:\r
- /*\r
- There are two suballocation vectors, used in ping-pong way.\r
- The one with index m_1stVectorIndex is called 1st.\r
- The one with index (m_1stVectorIndex ^ 1) is called 2nd.\r
- 2nd can be non-empty only when 1st is not empty.\r
- When 2nd is not empty, m_2ndVectorMode indicates its mode of operation.\r
- */\r
- typedef VmaVector< VmaSuballocation, VmaStlAllocator<VmaSuballocation> > SuballocationVectorType;\r
-\r
- enum SECOND_VECTOR_MODE\r
- {\r
- SECOND_VECTOR_EMPTY,\r
- /*\r
- Suballocations in 2nd vector are created later than the ones in 1st, but they\r
- all have smaller offset.\r
- */\r
- SECOND_VECTOR_RING_BUFFER,\r
- /*\r
- Suballocations in 2nd vector are upper side of double stack.\r
- They all have offsets higher than those in 1st vector.\r
- Top of this stack means smaller offsets, but higher indices in this vector.\r
- */\r
- SECOND_VECTOR_DOUBLE_STACK,\r
- };\r
-\r
- VkDeviceSize m_SumFreeSize;\r
- SuballocationVectorType m_Suballocations0, m_Suballocations1;\r
- uint32_t m_1stVectorIndex;\r
- SECOND_VECTOR_MODE m_2ndVectorMode;\r
-\r
- SuballocationVectorType& AccessSuballocations1st() { return m_1stVectorIndex ? m_Suballocations1 : m_Suballocations0; }\r
- SuballocationVectorType& AccessSuballocations2nd() { return m_1stVectorIndex ? m_Suballocations0 : m_Suballocations1; }\r
- const SuballocationVectorType& AccessSuballocations1st() const { return m_1stVectorIndex ? m_Suballocations1 : m_Suballocations0; }\r
- const SuballocationVectorType& AccessSuballocations2nd() const { return m_1stVectorIndex ? m_Suballocations0 : m_Suballocations1; }\r
- \r
- // Number of items in 1st vector with hAllocation = null at the beginning.\r
- size_t m_1stNullItemsBeginCount;\r
- // Number of other items in 1st vector with hAllocation = null somewhere in the middle.\r
- size_t m_1stNullItemsMiddleCount;\r
- // Number of items in 2nd vector with hAllocation = null.\r
- size_t m_2ndNullItemsCount;\r
-\r
- bool ShouldCompact1st() const;\r
- void CleanupAfterFree();\r
-\r
- bool CreateAllocationRequest_LowerAddress(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest);\r
- bool CreateAllocationRequest_UpperAddress(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest);\r
-};\r
-\r
-/*\r
-- GetSize() is the original size of allocated memory block.\r
-- m_UsableSize is this size aligned down to a power of two.\r
- All allocations and calculations happen relative to m_UsableSize.\r
-- GetUnusableSize() is the difference between them.\r
- It is repoted as separate, unused range, not available for allocations.\r
-\r
-Node at level 0 has size = m_UsableSize.\r
-Each next level contains nodes with size 2 times smaller than current level.\r
-m_LevelCount is the maximum number of levels to use in the current object.\r
-*/\r
-class VmaBlockMetadata_Buddy : public VmaBlockMetadata\r
-{\r
- VMA_CLASS_NO_COPY(VmaBlockMetadata_Buddy)\r
-public:\r
- VmaBlockMetadata_Buddy(VmaAllocator hAllocator);\r
- virtual ~VmaBlockMetadata_Buddy();\r
- virtual void Init(VkDeviceSize size);\r
-\r
- virtual bool Validate() const;\r
- virtual size_t GetAllocationCount() const { return m_AllocationCount; }\r
- virtual VkDeviceSize GetSumFreeSize() const { return m_SumFreeSize + GetUnusableSize(); }\r
- virtual VkDeviceSize GetUnusedRangeSizeMax() const;\r
- virtual bool IsEmpty() const { return m_Root->type == Node::TYPE_FREE; }\r
-\r
- virtual void CalcAllocationStatInfo(VmaStatInfo& outInfo) const;\r
- virtual void AddPoolStats(VmaPoolStats& inoutStats) const;\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- virtual void PrintDetailedMap(class VmaJsonWriter& json) const;\r
-#endif\r
-\r
- virtual bool CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest);\r
-\r
- virtual bool MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest);\r
-\r
- virtual uint32_t MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount);\r
-\r
- virtual VkResult CheckCorruption(const void* pBlockData) { return VK_ERROR_FEATURE_NOT_PRESENT; }\r
-\r
- virtual void Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation);\r
-\r
- virtual void Free(const VmaAllocation allocation) { FreeAtOffset(allocation, allocation->GetOffset()); }\r
- virtual void FreeAtOffset(VkDeviceSize offset) { FreeAtOffset(VMA_NULL, offset); }\r
-\r
-private:\r
- static const VkDeviceSize MIN_NODE_SIZE = 32;\r
- static const size_t MAX_LEVELS = 30;\r
-\r
- struct ValidationContext\r
- {\r
- size_t calculatedAllocationCount;\r
- size_t calculatedFreeCount;\r
- VkDeviceSize calculatedSumFreeSize;\r
-\r
- ValidationContext() :\r
- calculatedAllocationCount(0),\r
- calculatedFreeCount(0),\r
- calculatedSumFreeSize(0) { }\r
- };\r
-\r
- struct Node\r
- {\r
- VkDeviceSize offset;\r
- enum TYPE\r
- {\r
- TYPE_FREE,\r
- TYPE_ALLOCATION,\r
- TYPE_SPLIT,\r
- TYPE_COUNT\r
- } type;\r
- Node* parent;\r
- Node* buddy;\r
-\r
- union\r
- {\r
- struct\r
- {\r
- Node* prev;\r
- Node* next;\r
- } free;\r
- struct\r
- {\r
- VmaAllocation alloc;\r
- } allocation;\r
- struct\r
- {\r
- Node* leftChild;\r
- } split;\r
- };\r
- };\r
-\r
- // Size of the memory block aligned down to a power of two.\r
- VkDeviceSize m_UsableSize;\r
- uint32_t m_LevelCount;\r
-\r
- Node* m_Root;\r
- struct {\r
- Node* front;\r
- Node* back;\r
- } m_FreeList[MAX_LEVELS];\r
- // Number of nodes in the tree with type == TYPE_ALLOCATION.\r
- size_t m_AllocationCount;\r
- // Number of nodes in the tree with type == TYPE_FREE.\r
- size_t m_FreeCount;\r
- // This includes space wasted due to internal fragmentation. Doesn't include unusable size.\r
- VkDeviceSize m_SumFreeSize;\r
-\r
- VkDeviceSize GetUnusableSize() const { return GetSize() - m_UsableSize; }\r
- void DeleteNode(Node* node);\r
- bool ValidateNode(ValidationContext& ctx, const Node* parent, const Node* curr, uint32_t level, VkDeviceSize levelNodeSize) const;\r
- uint32_t AllocSizeToLevel(VkDeviceSize allocSize) const;\r
- inline VkDeviceSize LevelToNodeSize(uint32_t level) const { return m_UsableSize >> level; }\r
- // Alloc passed just for validation. Can be null.\r
- void FreeAtOffset(VmaAllocation alloc, VkDeviceSize offset);\r
- void CalcAllocationStatInfoNode(VmaStatInfo& outInfo, const Node* node, VkDeviceSize levelNodeSize) const;\r
- // Adds node to the front of FreeList at given level.\r
- // node->type must be FREE.\r
- // node->free.prev, next can be undefined.\r
- void AddToFreeListFront(uint32_t level, Node* node);\r
- // Removes node from FreeList at given level.\r
- // node->type must be FREE.\r
- // node->free.prev, next stay untouched.\r
- void RemoveFromFreeList(uint32_t level, Node* node);\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- void PrintDetailedMapNode(class VmaJsonWriter& json, const Node* node, VkDeviceSize levelNodeSize) const;\r
-#endif\r
-};\r
-\r
-/*\r
-Represents a single block of device memory (`VkDeviceMemory`) with all the\r
-data about its regions (aka suballocations, #VmaAllocation), assigned and free.\r
-\r
-Thread-safety: This class must be externally synchronized.\r
-*/\r
-class VmaDeviceMemoryBlock\r
-{\r
- VMA_CLASS_NO_COPY(VmaDeviceMemoryBlock)\r
-public:\r
- VmaBlockMetadata* m_pMetadata;\r
-\r
- VmaDeviceMemoryBlock(VmaAllocator hAllocator);\r
-\r
- ~VmaDeviceMemoryBlock()\r
- {\r
- VMA_ASSERT(m_MapCount == 0 && "VkDeviceMemory block is being destroyed while it is still mapped.");\r
- VMA_ASSERT(m_hMemory == VK_NULL_HANDLE);\r
- }\r
-\r
- // Always call after construction.\r
- void Init(\r
- VmaAllocator hAllocator,\r
- VmaPool hParentPool,\r
- uint32_t newMemoryTypeIndex,\r
- VkDeviceMemory newMemory,\r
- VkDeviceSize newSize,\r
- uint32_t id,\r
- uint32_t algorithm);\r
- // Always call before destruction.\r
- void Destroy(VmaAllocator allocator);\r
- \r
- VmaPool GetParentPool() const { return m_hParentPool; }\r
- VkDeviceMemory GetDeviceMemory() const { return m_hMemory; }\r
- uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; }\r
- uint32_t GetId() const { return m_Id; }\r
- void* GetMappedData() const { return m_pMappedData; }\r
-\r
- // Validates all data structures inside this object. If not valid, returns false.\r
- bool Validate() const;\r
-\r
- VkResult CheckCorruption(VmaAllocator hAllocator);\r
-\r
- // ppData can be null.\r
- VkResult Map(VmaAllocator hAllocator, uint32_t count, void** ppData);\r
- void Unmap(VmaAllocator hAllocator, uint32_t count);\r
-\r
- VkResult WriteMagicValueAroundAllocation(VmaAllocator hAllocator, VkDeviceSize allocOffset, VkDeviceSize allocSize);\r
- VkResult ValidateMagicValueAroundAllocation(VmaAllocator hAllocator, VkDeviceSize allocOffset, VkDeviceSize allocSize);\r
-\r
- VkResult BindBufferMemory(\r
- const VmaAllocator hAllocator,\r
- const VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkBuffer hBuffer,\r
- const void* pNext);\r
- VkResult BindImageMemory(\r
- const VmaAllocator hAllocator,\r
- const VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkImage hImage,\r
- const void* pNext);\r
-\r
-private:\r
- VmaPool m_hParentPool; // VK_NULL_HANDLE if not belongs to custom pool.\r
- uint32_t m_MemoryTypeIndex;\r
- uint32_t m_Id;\r
- VkDeviceMemory m_hMemory;\r
-\r
- /*\r
- Protects access to m_hMemory so it's not used by multiple threads simultaneously, e.g. vkMapMemory, vkBindBufferMemory.\r
- Also protects m_MapCount, m_pMappedData.\r
- Allocations, deallocations, any change in m_pMetadata is protected by parent's VmaBlockVector::m_Mutex.\r
- */\r
- VMA_MUTEX m_Mutex;\r
- uint32_t m_MapCount;\r
- void* m_pMappedData;\r
-};\r
-\r
-struct VmaPointerLess\r
-{\r
- bool operator()(const void* lhs, const void* rhs) const\r
- {\r
- return lhs < rhs;\r
- }\r
-};\r
-\r
-struct VmaDefragmentationMove\r
-{\r
- size_t srcBlockIndex;\r
- size_t dstBlockIndex;\r
- VkDeviceSize srcOffset;\r
- VkDeviceSize dstOffset;\r
- VkDeviceSize size;\r
- VmaAllocation hAllocation;\r
- VmaDeviceMemoryBlock* pSrcBlock;\r
- VmaDeviceMemoryBlock* pDstBlock;\r
-};\r
-\r
-class VmaDefragmentationAlgorithm;\r
-\r
-/*\r
-Sequence of VmaDeviceMemoryBlock. Represents memory blocks allocated for a specific\r
-Vulkan memory type.\r
-\r
-Synchronized internally with a mutex.\r
-*/\r
-struct VmaBlockVector\r
-{\r
- VMA_CLASS_NO_COPY(VmaBlockVector)\r
-public:\r
- VmaBlockVector(\r
- VmaAllocator hAllocator,\r
- VmaPool hParentPool,\r
- uint32_t memoryTypeIndex,\r
- VkDeviceSize preferredBlockSize,\r
- size_t minBlockCount,\r
- size_t maxBlockCount,\r
- VkDeviceSize bufferImageGranularity,\r
- uint32_t frameInUseCount,\r
- bool explicitBlockSize,\r
- uint32_t algorithm);\r
- ~VmaBlockVector();\r
-\r
- VkResult CreateMinBlocks();\r
-\r
- VmaAllocator GetAllocator() const { return m_hAllocator; }\r
- VmaPool GetParentPool() const { return m_hParentPool; }\r
- bool IsCustomPool() const { return m_hParentPool != VMA_NULL; }\r
- uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; }\r
- VkDeviceSize GetPreferredBlockSize() const { return m_PreferredBlockSize; }\r
- VkDeviceSize GetBufferImageGranularity() const { return m_BufferImageGranularity; }\r
- uint32_t GetFrameInUseCount() const { return m_FrameInUseCount; }\r
- uint32_t GetAlgorithm() const { return m_Algorithm; }\r
-\r
- void GetPoolStats(VmaPoolStats* pStats);\r
-\r
- bool IsEmpty();\r
- bool IsCorruptionDetectionEnabled() const;\r
-\r
- VkResult Allocate(\r
- uint32_t currentFrameIndex,\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaSuballocationType suballocType,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations);\r
-\r
- void Free(const VmaAllocation hAllocation);\r
-\r
- // Adds statistics of this BlockVector to pStats.\r
- void AddStats(VmaStats* pStats);\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- void PrintDetailedMap(class VmaJsonWriter& json);\r
-#endif\r
-\r
- void MakePoolAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- size_t* pLostAllocationCount);\r
- VkResult CheckCorruption();\r
-\r
- // Saves results in pCtx->res.\r
- void Defragment(\r
- class VmaBlockVectorDefragmentationContext* pCtx,\r
- VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags,\r
- VkDeviceSize& maxCpuBytesToMove, uint32_t& maxCpuAllocationsToMove,\r
- VkDeviceSize& maxGpuBytesToMove, uint32_t& maxGpuAllocationsToMove,\r
- VkCommandBuffer commandBuffer);\r
- void DefragmentationEnd(\r
- class VmaBlockVectorDefragmentationContext* pCtx,\r
- uint32_t flags,\r
- VmaDefragmentationStats* pStats);\r
-\r
- uint32_t ProcessDefragmentations(\r
- class VmaBlockVectorDefragmentationContext *pCtx,\r
- VmaDefragmentationPassMoveInfo* pMove, uint32_t maxMoves);\r
-\r
- void CommitDefragmentations(\r
- class VmaBlockVectorDefragmentationContext *pCtx,\r
- VmaDefragmentationStats* pStats);\r
-\r
- ////////////////////////////////////////////////////////////////////////////////\r
- // To be used only while the m_Mutex is locked. Used during defragmentation.\r
-\r
- size_t GetBlockCount() const { return m_Blocks.size(); }\r
- VmaDeviceMemoryBlock* GetBlock(size_t index) const { return m_Blocks[index]; }\r
- size_t CalcAllocationCount() const;\r
- bool IsBufferImageGranularityConflictPossible() const;\r
-\r
-private:\r
- friend class VmaDefragmentationAlgorithm_Generic;\r
-\r
- const VmaAllocator m_hAllocator;\r
- const VmaPool m_hParentPool;\r
- const uint32_t m_MemoryTypeIndex;\r
- const VkDeviceSize m_PreferredBlockSize;\r
- const size_t m_MinBlockCount;\r
- const size_t m_MaxBlockCount;\r
- const VkDeviceSize m_BufferImageGranularity;\r
- const uint32_t m_FrameInUseCount;\r
- const bool m_ExplicitBlockSize;\r
- const uint32_t m_Algorithm;\r
- VMA_RW_MUTEX m_Mutex;\r
-\r
- /* There can be at most one allocation that is completely empty (except when minBlockCount > 0) -\r
- a hysteresis to avoid pessimistic case of alternating creation and destruction of a VkDeviceMemory. */\r
- bool m_HasEmptyBlock;\r
- // Incrementally sorted by sumFreeSize, ascending.\r
- VmaVector< VmaDeviceMemoryBlock*, VmaStlAllocator<VmaDeviceMemoryBlock*> > m_Blocks;\r
- uint32_t m_NextBlockId;\r
-\r
- VkDeviceSize CalcMaxBlockSize() const;\r
-\r
- // Finds and removes given block from vector.\r
- void Remove(VmaDeviceMemoryBlock* pBlock);\r
-\r
- // Performs single step in sorting m_Blocks. They may not be fully sorted\r
- // after this call.\r
- void IncrementallySortBlocks();\r
-\r
- VkResult AllocatePage(\r
- uint32_t currentFrameIndex,\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaSuballocationType suballocType,\r
- VmaAllocation* pAllocation);\r
-\r
- // To be used only without CAN_MAKE_OTHER_LOST flag.\r
- VkResult AllocateFromBlock(\r
- VmaDeviceMemoryBlock* pBlock,\r
- uint32_t currentFrameIndex,\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- VmaAllocationCreateFlags allocFlags,\r
- void* pUserData,\r
- VmaSuballocationType suballocType,\r
- uint32_t strategy,\r
- VmaAllocation* pAllocation);\r
-\r
- VkResult CreateBlock(VkDeviceSize blockSize, size_t* pNewBlockIndex);\r
-\r
- // Saves result to pCtx->res.\r
- void ApplyDefragmentationMovesCpu(\r
- class VmaBlockVectorDefragmentationContext* pDefragCtx,\r
- const VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves);\r
- // Saves result to pCtx->res.\r
- void ApplyDefragmentationMovesGpu(\r
- class VmaBlockVectorDefragmentationContext* pDefragCtx,\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkCommandBuffer commandBuffer);\r
-\r
- /*\r
- Used during defragmentation. pDefragmentationStats is optional. It's in/out\r
- - updated with new data.\r
- */\r
- void FreeEmptyBlocks(VmaDefragmentationStats* pDefragmentationStats);\r
-\r
- void UpdateHasEmptyBlock();\r
-};\r
-\r
-struct VmaPool_T\r
-{\r
- VMA_CLASS_NO_COPY(VmaPool_T)\r
-public:\r
- VmaBlockVector m_BlockVector;\r
-\r
- VmaPool_T(\r
- VmaAllocator hAllocator,\r
- const VmaPoolCreateInfo& createInfo,\r
- VkDeviceSize preferredBlockSize);\r
- ~VmaPool_T();\r
-\r
- uint32_t GetId() const { return m_Id; }\r
- void SetId(uint32_t id) { VMA_ASSERT(m_Id == 0); m_Id = id; }\r
-\r
- const char* GetName() const { return m_Name; }\r
- void SetName(const char* pName);\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- //void PrintDetailedMap(class VmaStringBuilder& sb);\r
-#endif\r
-\r
-private:\r
- uint32_t m_Id;\r
- char* m_Name;\r
-};\r
-\r
-/*\r
-Performs defragmentation:\r
-\r
-- Updates `pBlockVector->m_pMetadata`.\r
-- Updates allocations by calling ChangeBlockAllocation() or ChangeOffset().\r
-- Does not move actual data, only returns requested moves as `moves`.\r
-*/\r
-class VmaDefragmentationAlgorithm\r
-{\r
- VMA_CLASS_NO_COPY(VmaDefragmentationAlgorithm)\r
-public:\r
- VmaDefragmentationAlgorithm(\r
- VmaAllocator hAllocator,\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currentFrameIndex) :\r
- m_hAllocator(hAllocator),\r
- m_pBlockVector(pBlockVector),\r
- m_CurrentFrameIndex(currentFrameIndex)\r
- {\r
- }\r
- virtual ~VmaDefragmentationAlgorithm()\r
- {\r
- }\r
-\r
- virtual void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged) = 0;\r
- virtual void AddAll() = 0;\r
-\r
- virtual VkResult Defragment(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- VmaDefragmentationFlags flags) = 0;\r
-\r
- virtual VkDeviceSize GetBytesMoved() const = 0;\r
- virtual uint32_t GetAllocationsMoved() const = 0;\r
-\r
-protected:\r
- VmaAllocator const m_hAllocator;\r
- VmaBlockVector* const m_pBlockVector;\r
- const uint32_t m_CurrentFrameIndex;\r
-\r
- struct AllocationInfo\r
- {\r
- VmaAllocation m_hAllocation;\r
- VkBool32* m_pChanged;\r
-\r
- AllocationInfo() :\r
- m_hAllocation(VK_NULL_HANDLE),\r
- m_pChanged(VMA_NULL)\r
- {\r
- }\r
- AllocationInfo(VmaAllocation hAlloc, VkBool32* pChanged) :\r
- m_hAllocation(hAlloc),\r
- m_pChanged(pChanged)\r
- {\r
- }\r
- };\r
-};\r
-\r
-class VmaDefragmentationAlgorithm_Generic : public VmaDefragmentationAlgorithm\r
-{\r
- VMA_CLASS_NO_COPY(VmaDefragmentationAlgorithm_Generic)\r
-public:\r
- VmaDefragmentationAlgorithm_Generic(\r
- VmaAllocator hAllocator,\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currentFrameIndex,\r
- bool overlappingMoveSupported);\r
- virtual ~VmaDefragmentationAlgorithm_Generic();\r
-\r
- virtual void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged);\r
- virtual void AddAll() { m_AllAllocations = true; }\r
-\r
- virtual VkResult Defragment(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- VmaDefragmentationFlags flags);\r
-\r
- virtual VkDeviceSize GetBytesMoved() const { return m_BytesMoved; }\r
- virtual uint32_t GetAllocationsMoved() const { return m_AllocationsMoved; }\r
-\r
-private:\r
- uint32_t m_AllocationCount;\r
- bool m_AllAllocations;\r
-\r
- VkDeviceSize m_BytesMoved;\r
- uint32_t m_AllocationsMoved;\r
-\r
- struct AllocationInfoSizeGreater\r
- {\r
- bool operator()(const AllocationInfo& lhs, const AllocationInfo& rhs) const\r
- {\r
- return lhs.m_hAllocation->GetSize() > rhs.m_hAllocation->GetSize();\r
- }\r
- };\r
-\r
- struct AllocationInfoOffsetGreater\r
- {\r
- bool operator()(const AllocationInfo& lhs, const AllocationInfo& rhs) const\r
- {\r
- return lhs.m_hAllocation->GetOffset() > rhs.m_hAllocation->GetOffset();\r
- }\r
- };\r
-\r
- struct BlockInfo\r
- {\r
- size_t m_OriginalBlockIndex;\r
- VmaDeviceMemoryBlock* m_pBlock;\r
- bool m_HasNonMovableAllocations;\r
- VmaVector< AllocationInfo, VmaStlAllocator<AllocationInfo> > m_Allocations;\r
-\r
- BlockInfo(const VkAllocationCallbacks* pAllocationCallbacks) :\r
- m_OriginalBlockIndex(SIZE_MAX),\r
- m_pBlock(VMA_NULL),\r
- m_HasNonMovableAllocations(true),\r
- m_Allocations(pAllocationCallbacks)\r
- {\r
- }\r
-\r
- void CalcHasNonMovableAllocations()\r
- {\r
- const size_t blockAllocCount = m_pBlock->m_pMetadata->GetAllocationCount();\r
- const size_t defragmentAllocCount = m_Allocations.size();\r
- m_HasNonMovableAllocations = blockAllocCount != defragmentAllocCount;\r
- }\r
-\r
- void SortAllocationsBySizeDescending()\r
- {\r
- VMA_SORT(m_Allocations.begin(), m_Allocations.end(), AllocationInfoSizeGreater());\r
- }\r
-\r
- void SortAllocationsByOffsetDescending()\r
- {\r
- VMA_SORT(m_Allocations.begin(), m_Allocations.end(), AllocationInfoOffsetGreater());\r
- }\r
- };\r
-\r
- struct BlockPointerLess\r
- {\r
- bool operator()(const BlockInfo* pLhsBlockInfo, const VmaDeviceMemoryBlock* pRhsBlock) const\r
- {\r
- return pLhsBlockInfo->m_pBlock < pRhsBlock;\r
- }\r
- bool operator()(const BlockInfo* pLhsBlockInfo, const BlockInfo* pRhsBlockInfo) const\r
- {\r
- return pLhsBlockInfo->m_pBlock < pRhsBlockInfo->m_pBlock;\r
- }\r
- };\r
-\r
- // 1. Blocks with some non-movable allocations go first.\r
- // 2. Blocks with smaller sumFreeSize go first.\r
- struct BlockInfoCompareMoveDestination\r
- {\r
- bool operator()(const BlockInfo* pLhsBlockInfo, const BlockInfo* pRhsBlockInfo) const\r
- {\r
- if(pLhsBlockInfo->m_HasNonMovableAllocations && !pRhsBlockInfo->m_HasNonMovableAllocations)\r
- {\r
- return true;\r
- }\r
- if(!pLhsBlockInfo->m_HasNonMovableAllocations && pRhsBlockInfo->m_HasNonMovableAllocations)\r
- {\r
- return false;\r
- }\r
- if(pLhsBlockInfo->m_pBlock->m_pMetadata->GetSumFreeSize() < pRhsBlockInfo->m_pBlock->m_pMetadata->GetSumFreeSize())\r
- {\r
- return true;\r
- }\r
- return false;\r
- }\r
- };\r
-\r
- typedef VmaVector< BlockInfo*, VmaStlAllocator<BlockInfo*> > BlockInfoVector;\r
- BlockInfoVector m_Blocks;\r
-\r
- VkResult DefragmentRound(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- bool freeOldAllocations);\r
-\r
- size_t CalcBlocksWithNonMovableCount() const;\r
-\r
- static bool MoveMakesSense(\r
- size_t dstBlockIndex, VkDeviceSize dstOffset,\r
- size_t srcBlockIndex, VkDeviceSize srcOffset);\r
-};\r
-\r
-class VmaDefragmentationAlgorithm_Fast : public VmaDefragmentationAlgorithm\r
-{\r
- VMA_CLASS_NO_COPY(VmaDefragmentationAlgorithm_Fast)\r
-public:\r
- VmaDefragmentationAlgorithm_Fast(\r
- VmaAllocator hAllocator,\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currentFrameIndex,\r
- bool overlappingMoveSupported);\r
- virtual ~VmaDefragmentationAlgorithm_Fast();\r
-\r
- virtual void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged) { ++m_AllocationCount; }\r
- virtual void AddAll() { m_AllAllocations = true; }\r
-\r
- virtual VkResult Defragment(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- VmaDefragmentationFlags flags);\r
-\r
- virtual VkDeviceSize GetBytesMoved() const { return m_BytesMoved; }\r
- virtual uint32_t GetAllocationsMoved() const { return m_AllocationsMoved; }\r
-\r
-private:\r
- struct BlockInfo\r
- {\r
- size_t origBlockIndex;\r
- };\r
-\r
- class FreeSpaceDatabase\r
- {\r
- public:\r
- FreeSpaceDatabase()\r
- {\r
- FreeSpace s = {};\r
- s.blockInfoIndex = SIZE_MAX;\r
- for(size_t i = 0; i < MAX_COUNT; ++i)\r
- {\r
- m_FreeSpaces[i] = s;\r
- }\r
- }\r
-\r
- void Register(size_t blockInfoIndex, VkDeviceSize offset, VkDeviceSize size)\r
- {\r
- if(size < VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- return;\r
- }\r
-\r
- // Find first invalid or the smallest structure.\r
- size_t bestIndex = SIZE_MAX;\r
- for(size_t i = 0; i < MAX_COUNT; ++i)\r
- {\r
- // Empty structure.\r
- if(m_FreeSpaces[i].blockInfoIndex == SIZE_MAX)\r
- {\r
- bestIndex = i;\r
- break;\r
- }\r
- if(m_FreeSpaces[i].size < size &&\r
- (bestIndex == SIZE_MAX || m_FreeSpaces[bestIndex].size > m_FreeSpaces[i].size))\r
- {\r
- bestIndex = i;\r
- }\r
- }\r
-\r
- if(bestIndex != SIZE_MAX)\r
- {\r
- m_FreeSpaces[bestIndex].blockInfoIndex = blockInfoIndex;\r
- m_FreeSpaces[bestIndex].offset = offset;\r
- m_FreeSpaces[bestIndex].size = size;\r
- }\r
- }\r
-\r
- bool Fetch(VkDeviceSize alignment, VkDeviceSize size,\r
- size_t& outBlockInfoIndex, VkDeviceSize& outDstOffset)\r
- {\r
- size_t bestIndex = SIZE_MAX;\r
- VkDeviceSize bestFreeSpaceAfter = 0;\r
- for(size_t i = 0; i < MAX_COUNT; ++i)\r
- {\r
- // Structure is valid.\r
- if(m_FreeSpaces[i].blockInfoIndex != SIZE_MAX)\r
- {\r
- const VkDeviceSize dstOffset = VmaAlignUp(m_FreeSpaces[i].offset, alignment);\r
- // Allocation fits into this structure.\r
- if(dstOffset + size <= m_FreeSpaces[i].offset + m_FreeSpaces[i].size)\r
- {\r
- const VkDeviceSize freeSpaceAfter = (m_FreeSpaces[i].offset + m_FreeSpaces[i].size) -\r
- (dstOffset + size);\r
- if(bestIndex == SIZE_MAX || freeSpaceAfter > bestFreeSpaceAfter)\r
- {\r
- bestIndex = i;\r
- bestFreeSpaceAfter = freeSpaceAfter;\r
- }\r
- }\r
- }\r
- }\r
- \r
- if(bestIndex != SIZE_MAX)\r
- {\r
- outBlockInfoIndex = m_FreeSpaces[bestIndex].blockInfoIndex;\r
- outDstOffset = VmaAlignUp(m_FreeSpaces[bestIndex].offset, alignment);\r
-\r
- if(bestFreeSpaceAfter >= VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- // Leave this structure for remaining empty space.\r
- const VkDeviceSize alignmentPlusSize = (outDstOffset - m_FreeSpaces[bestIndex].offset) + size;\r
- m_FreeSpaces[bestIndex].offset += alignmentPlusSize;\r
- m_FreeSpaces[bestIndex].size -= alignmentPlusSize;\r
- }\r
- else\r
- {\r
- // This structure becomes invalid.\r
- m_FreeSpaces[bestIndex].blockInfoIndex = SIZE_MAX;\r
- }\r
-\r
- return true;\r
- }\r
-\r
- return false;\r
- }\r
-\r
- private:\r
- static const size_t MAX_COUNT = 4;\r
-\r
- struct FreeSpace\r
- {\r
- size_t blockInfoIndex; // SIZE_MAX means this structure is invalid.\r
- VkDeviceSize offset;\r
- VkDeviceSize size;\r
- } m_FreeSpaces[MAX_COUNT];\r
- };\r
-\r
- const bool m_OverlappingMoveSupported;\r
-\r
- uint32_t m_AllocationCount;\r
- bool m_AllAllocations;\r
-\r
- VkDeviceSize m_BytesMoved;\r
- uint32_t m_AllocationsMoved;\r
-\r
- VmaVector< BlockInfo, VmaStlAllocator<BlockInfo> > m_BlockInfos;\r
-\r
- void PreprocessMetadata();\r
- void PostprocessMetadata();\r
- void InsertSuballoc(VmaBlockMetadata_Generic* pMetadata, const VmaSuballocation& suballoc);\r
-};\r
-\r
-struct VmaBlockDefragmentationContext\r
-{\r
- enum BLOCK_FLAG\r
- {\r
- BLOCK_FLAG_USED = 0x00000001,\r
- };\r
- uint32_t flags;\r
- VkBuffer hBuffer;\r
-};\r
-\r
-class VmaBlockVectorDefragmentationContext\r
-{\r
- VMA_CLASS_NO_COPY(VmaBlockVectorDefragmentationContext)\r
-public:\r
- VkResult res;\r
- bool mutexLocked;\r
- VmaVector< VmaBlockDefragmentationContext, VmaStlAllocator<VmaBlockDefragmentationContext> > blockContexts;\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> > defragmentationMoves;\r
- uint32_t defragmentationMovesProcessed;\r
- uint32_t defragmentationMovesCommitted;\r
- bool hasDefragmentationPlan;\r
-\r
- VmaBlockVectorDefragmentationContext(\r
- VmaAllocator hAllocator,\r
- VmaPool hCustomPool, // Optional.\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currFrameIndex);\r
- ~VmaBlockVectorDefragmentationContext();\r
-\r
- VmaPool GetCustomPool() const { return m_hCustomPool; }\r
- VmaBlockVector* GetBlockVector() const { return m_pBlockVector; }\r
- VmaDefragmentationAlgorithm* GetAlgorithm() const { return m_pAlgorithm; }\r
-\r
- void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged);\r
- void AddAll() { m_AllAllocations = true; }\r
-\r
- void Begin(bool overlappingMoveSupported, VmaDefragmentationFlags flags);\r
-\r
-private:\r
- const VmaAllocator m_hAllocator;\r
- // Null if not from custom pool.\r
- const VmaPool m_hCustomPool;\r
- // Redundant, for convenience not to fetch from m_hCustomPool->m_BlockVector or m_hAllocator->m_pBlockVectors.\r
- VmaBlockVector* const m_pBlockVector;\r
- const uint32_t m_CurrFrameIndex;\r
- // Owner of this object.\r
- VmaDefragmentationAlgorithm* m_pAlgorithm;\r
-\r
- struct AllocInfo\r
- {\r
- VmaAllocation hAlloc;\r
- VkBool32* pChanged;\r
- };\r
- // Used between constructor and Begin.\r
- VmaVector< AllocInfo, VmaStlAllocator<AllocInfo> > m_Allocations;\r
- bool m_AllAllocations;\r
-};\r
-\r
-struct VmaDefragmentationContext_T\r
-{\r
-private:\r
- VMA_CLASS_NO_COPY(VmaDefragmentationContext_T)\r
-public:\r
- VmaDefragmentationContext_T(\r
- VmaAllocator hAllocator,\r
- uint32_t currFrameIndex,\r
- uint32_t flags,\r
- VmaDefragmentationStats* pStats);\r
- ~VmaDefragmentationContext_T();\r
-\r
- void AddPools(uint32_t poolCount, const VmaPool* pPools);\r
- void AddAllocations(\r
- uint32_t allocationCount,\r
- const VmaAllocation* pAllocations,\r
- VkBool32* pAllocationsChanged);\r
-\r
- /*\r
- Returns:\r
- - `VK_SUCCESS` if succeeded and object can be destroyed immediately.\r
- - `VK_NOT_READY` if succeeded but the object must remain alive until vmaDefragmentationEnd().\r
- - Negative value if error occured and object can be destroyed immediately.\r
- */\r
- VkResult Defragment(\r
- VkDeviceSize maxCpuBytesToMove, uint32_t maxCpuAllocationsToMove,\r
- VkDeviceSize maxGpuBytesToMove, uint32_t maxGpuAllocationsToMove,\r
- VkCommandBuffer commandBuffer, VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags);\r
-\r
- VkResult DefragmentPassBegin(VmaDefragmentationPassInfo* pInfo);\r
- VkResult DefragmentPassEnd();\r
-\r
-private:\r
- const VmaAllocator m_hAllocator;\r
- const uint32_t m_CurrFrameIndex;\r
- const uint32_t m_Flags;\r
- VmaDefragmentationStats* const m_pStats;\r
-\r
- VkDeviceSize m_MaxCpuBytesToMove;\r
- uint32_t m_MaxCpuAllocationsToMove;\r
- VkDeviceSize m_MaxGpuBytesToMove;\r
- uint32_t m_MaxGpuAllocationsToMove;\r
-\r
- // Owner of these objects.\r
- VmaBlockVectorDefragmentationContext* m_DefaultPoolContexts[VK_MAX_MEMORY_TYPES];\r
- // Owner of these objects.\r
- VmaVector< VmaBlockVectorDefragmentationContext*, VmaStlAllocator<VmaBlockVectorDefragmentationContext*> > m_CustomPoolContexts;\r
-};\r
-\r
-#if VMA_RECORDING_ENABLED\r
-\r
-class VmaRecorder\r
-{\r
-public:\r
- VmaRecorder();\r
- VkResult Init(const VmaRecordSettings& settings, bool useMutex);\r
- void WriteConfiguration(\r
- const VkPhysicalDeviceProperties& devProps,\r
- const VkPhysicalDeviceMemoryProperties& memProps,\r
- uint32_t vulkanApiVersion,\r
- bool dedicatedAllocationExtensionEnabled,\r
- bool bindMemory2ExtensionEnabled,\r
- bool memoryBudgetExtensionEnabled,\r
- bool deviceCoherentMemoryExtensionEnabled);\r
- ~VmaRecorder();\r
-\r
- void RecordCreateAllocator(uint32_t frameIndex);\r
- void RecordDestroyAllocator(uint32_t frameIndex);\r
- void RecordCreatePool(uint32_t frameIndex,\r
- const VmaPoolCreateInfo& createInfo,\r
- VmaPool pool);\r
- void RecordDestroyPool(uint32_t frameIndex, VmaPool pool);\r
- void RecordAllocateMemory(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaAllocation allocation);\r
- void RecordAllocateMemoryPages(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- const VmaAllocationCreateInfo& createInfo,\r
- uint64_t allocationCount,\r
- const VmaAllocation* pAllocations);\r
- void RecordAllocateMemoryForBuffer(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- bool requiresDedicatedAllocation,\r
- bool prefersDedicatedAllocation,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaAllocation allocation);\r
- void RecordAllocateMemoryForImage(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- bool requiresDedicatedAllocation,\r
- bool prefersDedicatedAllocation,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaAllocation allocation);\r
- void RecordFreeMemory(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordFreeMemoryPages(uint32_t frameIndex,\r
- uint64_t allocationCount,\r
- const VmaAllocation* pAllocations);\r
- void RecordSetAllocationUserData(uint32_t frameIndex,\r
- VmaAllocation allocation,\r
- const void* pUserData);\r
- void RecordCreateLostAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordMapMemory(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordUnmapMemory(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordFlushAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size);\r
- void RecordInvalidateAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size);\r
- void RecordCreateBuffer(uint32_t frameIndex,\r
- const VkBufferCreateInfo& bufCreateInfo,\r
- const VmaAllocationCreateInfo& allocCreateInfo,\r
- VmaAllocation allocation);\r
- void RecordCreateImage(uint32_t frameIndex,\r
- const VkImageCreateInfo& imageCreateInfo,\r
- const VmaAllocationCreateInfo& allocCreateInfo,\r
- VmaAllocation allocation);\r
- void RecordDestroyBuffer(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordDestroyImage(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordTouchAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordGetAllocationInfo(uint32_t frameIndex,\r
- VmaAllocation allocation);\r
- void RecordMakePoolAllocationsLost(uint32_t frameIndex,\r
- VmaPool pool);\r
- void RecordDefragmentationBegin(uint32_t frameIndex,\r
- const VmaDefragmentationInfo2& info,\r
- VmaDefragmentationContext ctx);\r
- void RecordDefragmentationEnd(uint32_t frameIndex,\r
- VmaDefragmentationContext ctx);\r
- void RecordSetPoolName(uint32_t frameIndex,\r
- VmaPool pool,\r
- const char* name);\r
-\r
-private:\r
- struct CallParams\r
- {\r
- uint32_t threadId;\r
- double time;\r
- };\r
-\r
- class UserDataString\r
- {\r
- public:\r
- UserDataString(VmaAllocationCreateFlags allocFlags, const void* pUserData);\r
- const char* GetString() const { return m_Str; }\r
-\r
- private:\r
- char m_PtrStr[17];\r
- const char* m_Str;\r
- };\r
-\r
- bool m_UseMutex;\r
- VmaRecordFlags m_Flags;\r
- FILE* m_File;\r
- VMA_MUTEX m_FileMutex;\r
- std::chrono::time_point<std::chrono::high_resolution_clock> m_RecordingStartTime;\r
-\r
- void GetBasicParams(CallParams& outParams);\r
-\r
- // T must be a pointer type, e.g. VmaAllocation, VmaPool.\r
- template<typename T>\r
- void PrintPointerList(uint64_t count, const T* pItems)\r
- {\r
- if(count)\r
- {\r
- fprintf(m_File, "%p", pItems[0]);\r
- for(uint64_t i = 1; i < count; ++i)\r
- {\r
- fprintf(m_File, " %p", pItems[i]);\r
- }\r
- }\r
- }\r
-\r
- void PrintPointerList(uint64_t count, const VmaAllocation* pItems);\r
- void Flush();\r
-};\r
-\r
-#endif // #if VMA_RECORDING_ENABLED\r
-\r
-/*\r
-Thread-safe wrapper over VmaPoolAllocator free list, for allocation of VmaAllocation_T objects.\r
-*/\r
-class VmaAllocationObjectAllocator\r
-{\r
- VMA_CLASS_NO_COPY(VmaAllocationObjectAllocator)\r
-public:\r
- VmaAllocationObjectAllocator(const VkAllocationCallbacks* pAllocationCallbacks);\r
-\r
- template<typename... Types> VmaAllocation Allocate(Types... args);\r
- void Free(VmaAllocation hAlloc);\r
-\r
-private:\r
- VMA_MUTEX m_Mutex;\r
- VmaPoolAllocator<VmaAllocation_T> m_Allocator;\r
-};\r
-\r
-struct VmaCurrentBudgetData\r
-{\r
- VMA_ATOMIC_UINT64 m_BlockBytes[VK_MAX_MEMORY_HEAPS];\r
- VMA_ATOMIC_UINT64 m_AllocationBytes[VK_MAX_MEMORY_HEAPS];\r
-\r
-#if VMA_MEMORY_BUDGET\r
- VMA_ATOMIC_UINT32 m_OperationsSinceBudgetFetch;\r
- VMA_RW_MUTEX m_BudgetMutex;\r
- uint64_t m_VulkanUsage[VK_MAX_MEMORY_HEAPS];\r
- uint64_t m_VulkanBudget[VK_MAX_MEMORY_HEAPS];\r
- uint64_t m_BlockBytesAtBudgetFetch[VK_MAX_MEMORY_HEAPS];\r
-#endif // #if VMA_MEMORY_BUDGET\r
-\r
- VmaCurrentBudgetData()\r
- {\r
- for(uint32_t heapIndex = 0; heapIndex < VK_MAX_MEMORY_HEAPS; ++heapIndex)\r
- {\r
- m_BlockBytes[heapIndex] = 0;\r
- m_AllocationBytes[heapIndex] = 0;\r
-#if VMA_MEMORY_BUDGET\r
- m_VulkanUsage[heapIndex] = 0;\r
- m_VulkanBudget[heapIndex] = 0;\r
- m_BlockBytesAtBudgetFetch[heapIndex] = 0;\r
-#endif\r
- }\r
-\r
-#if VMA_MEMORY_BUDGET\r
- m_OperationsSinceBudgetFetch = 0;\r
-#endif\r
- }\r
-\r
- void AddAllocation(uint32_t heapIndex, VkDeviceSize allocationSize)\r
- {\r
- m_AllocationBytes[heapIndex] += allocationSize;\r
-#if VMA_MEMORY_BUDGET\r
- ++m_OperationsSinceBudgetFetch;\r
-#endif\r
- }\r
-\r
- void RemoveAllocation(uint32_t heapIndex, VkDeviceSize allocationSize)\r
- {\r
- VMA_ASSERT(m_AllocationBytes[heapIndex] >= allocationSize); // DELME\r
- m_AllocationBytes[heapIndex] -= allocationSize;\r
-#if VMA_MEMORY_BUDGET\r
- ++m_OperationsSinceBudgetFetch;\r
-#endif\r
- }\r
-};\r
-\r
-// Main allocator object.\r
-struct VmaAllocator_T\r
-{\r
- VMA_CLASS_NO_COPY(VmaAllocator_T)\r
-public:\r
- bool m_UseMutex;\r
- uint32_t m_VulkanApiVersion;\r
- bool m_UseKhrDedicatedAllocation; // Can be set only if m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0).\r
- bool m_UseKhrBindMemory2; // Can be set only if m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0).\r
- bool m_UseExtMemoryBudget;\r
- bool m_UseAmdDeviceCoherentMemory;\r
- bool m_UseKhrBufferDeviceAddress;\r
- VkDevice m_hDevice;\r
- VkInstance m_hInstance;\r
- bool m_AllocationCallbacksSpecified;\r
- VkAllocationCallbacks m_AllocationCallbacks;\r
- VmaDeviceMemoryCallbacks m_DeviceMemoryCallbacks;\r
- VmaAllocationObjectAllocator m_AllocationObjectAllocator;\r
- \r
- // Each bit (1 << i) is set if HeapSizeLimit is enabled for that heap, so cannot allocate more than the heap size.\r
- uint32_t m_HeapSizeLimitMask;\r
-\r
- VkPhysicalDeviceProperties m_PhysicalDeviceProperties;\r
- VkPhysicalDeviceMemoryProperties m_MemProps;\r
-\r
- // Default pools.\r
- VmaBlockVector* m_pBlockVectors[VK_MAX_MEMORY_TYPES];\r
-\r
- // Each vector is sorted by memory (handle value).\r
- typedef VmaVector< VmaAllocation, VmaStlAllocator<VmaAllocation> > AllocationVectorType;\r
- AllocationVectorType* m_pDedicatedAllocations[VK_MAX_MEMORY_TYPES];\r
- VMA_RW_MUTEX m_DedicatedAllocationsMutex[VK_MAX_MEMORY_TYPES];\r
-\r
- VmaCurrentBudgetData m_Budget;\r
-\r
- VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo);\r
- VkResult Init(const VmaAllocatorCreateInfo* pCreateInfo);\r
- ~VmaAllocator_T();\r
-\r
- const VkAllocationCallbacks* GetAllocationCallbacks() const\r
- {\r
- return m_AllocationCallbacksSpecified ? &m_AllocationCallbacks : 0;\r
- }\r
- const VmaVulkanFunctions& GetVulkanFunctions() const\r
- {\r
- return m_VulkanFunctions;\r
- }\r
-\r
- VkPhysicalDevice GetPhysicalDevice() const { return m_PhysicalDevice; }\r
-\r
- VkDeviceSize GetBufferImageGranularity() const\r
- {\r
- return VMA_MAX(\r
- static_cast<VkDeviceSize>(VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY),\r
- m_PhysicalDeviceProperties.limits.bufferImageGranularity);\r
- }\r
-\r
- uint32_t GetMemoryHeapCount() const { return m_MemProps.memoryHeapCount; }\r
- uint32_t GetMemoryTypeCount() const { return m_MemProps.memoryTypeCount; }\r
-\r
- uint32_t MemoryTypeIndexToHeapIndex(uint32_t memTypeIndex) const\r
- {\r
- VMA_ASSERT(memTypeIndex < m_MemProps.memoryTypeCount);\r
- return m_MemProps.memoryTypes[memTypeIndex].heapIndex;\r
- }\r
- // True when specific memory type is HOST_VISIBLE but not HOST_COHERENT.\r
- bool IsMemoryTypeNonCoherent(uint32_t memTypeIndex) const\r
- {\r
- return (m_MemProps.memoryTypes[memTypeIndex].propertyFlags & (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) ==\r
- VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;\r
- }\r
- // Minimum alignment for all allocations in specific memory type.\r
- VkDeviceSize GetMemoryTypeMinAlignment(uint32_t memTypeIndex) const\r
- {\r
- return IsMemoryTypeNonCoherent(memTypeIndex) ?\r
- VMA_MAX((VkDeviceSize)VMA_DEBUG_ALIGNMENT, m_PhysicalDeviceProperties.limits.nonCoherentAtomSize) :\r
- (VkDeviceSize)VMA_DEBUG_ALIGNMENT;\r
- }\r
-\r
- bool IsIntegratedGpu() const\r
- {\r
- return m_PhysicalDeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;\r
- }\r
-\r
- uint32_t GetGlobalMemoryTypeBits() const { return m_GlobalMemoryTypeBits; }\r
-\r
-#if VMA_RECORDING_ENABLED\r
- VmaRecorder* GetRecorder() const { return m_pRecorder; }\r
-#endif\r
-\r
- void GetBufferMemoryRequirements(\r
- VkBuffer hBuffer,\r
- VkMemoryRequirements& memReq,\r
- bool& requiresDedicatedAllocation,\r
- bool& prefersDedicatedAllocation) const;\r
- void GetImageMemoryRequirements(\r
- VkImage hImage,\r
- VkMemoryRequirements& memReq,\r
- bool& requiresDedicatedAllocation,\r
- bool& prefersDedicatedAllocation) const;\r
-\r
- // Main allocation function.\r
- VkResult AllocateMemory(\r
- const VkMemoryRequirements& vkMemReq,\r
- bool requiresDedicatedAllocation,\r
- bool prefersDedicatedAllocation,\r
- VkBuffer dedicatedBuffer,\r
- VkBufferUsageFlags dedicatedBufferUsage, // UINT32_MAX when unknown.\r
- VkImage dedicatedImage,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaSuballocationType suballocType,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations);\r
-\r
- // Main deallocation function.\r
- void FreeMemory(\r
- size_t allocationCount,\r
- const VmaAllocation* pAllocations);\r
-\r
- VkResult ResizeAllocation(\r
- const VmaAllocation alloc,\r
- VkDeviceSize newSize);\r
-\r
- void CalculateStats(VmaStats* pStats);\r
-\r
- void GetBudget(\r
- VmaBudget* outBudget, uint32_t firstHeap, uint32_t heapCount);\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
- void PrintDetailedMap(class VmaJsonWriter& json);\r
-#endif\r
-\r
- VkResult DefragmentationBegin(\r
- const VmaDefragmentationInfo2& info,\r
- VmaDefragmentationStats* pStats,\r
- VmaDefragmentationContext* pContext);\r
- VkResult DefragmentationEnd(\r
- VmaDefragmentationContext context);\r
-\r
- VkResult DefragmentationPassBegin(\r
- VmaDefragmentationPassInfo* pInfo,\r
- VmaDefragmentationContext context);\r
- VkResult DefragmentationPassEnd(\r
- VmaDefragmentationContext context);\r
-\r
- void GetAllocationInfo(VmaAllocation hAllocation, VmaAllocationInfo* pAllocationInfo);\r
- bool TouchAllocation(VmaAllocation hAllocation);\r
-\r
- VkResult CreatePool(const VmaPoolCreateInfo* pCreateInfo, VmaPool* pPool);\r
- void DestroyPool(VmaPool pool);\r
- void GetPoolStats(VmaPool pool, VmaPoolStats* pPoolStats);\r
-\r
- void SetCurrentFrameIndex(uint32_t frameIndex);\r
- uint32_t GetCurrentFrameIndex() const { return m_CurrentFrameIndex.load(); }\r
-\r
- void MakePoolAllocationsLost(\r
- VmaPool hPool,\r
- size_t* pLostAllocationCount);\r
- VkResult CheckPoolCorruption(VmaPool hPool);\r
- VkResult CheckCorruption(uint32_t memoryTypeBits);\r
-\r
- void CreateLostAllocation(VmaAllocation* pAllocation);\r
-\r
- // Call to Vulkan function vkAllocateMemory with accompanying bookkeeping.\r
- VkResult AllocateVulkanMemory(const VkMemoryAllocateInfo* pAllocateInfo, VkDeviceMemory* pMemory);\r
- // Call to Vulkan function vkFreeMemory with accompanying bookkeeping.\r
- void FreeVulkanMemory(uint32_t memoryType, VkDeviceSize size, VkDeviceMemory hMemory);\r
- // Call to Vulkan function vkBindBufferMemory or vkBindBufferMemory2KHR.\r
- VkResult BindVulkanBuffer(\r
- VkDeviceMemory memory,\r
- VkDeviceSize memoryOffset,\r
- VkBuffer buffer,\r
- const void* pNext);\r
- // Call to Vulkan function vkBindImageMemory or vkBindImageMemory2KHR.\r
- VkResult BindVulkanImage(\r
- VkDeviceMemory memory,\r
- VkDeviceSize memoryOffset,\r
- VkImage image,\r
- const void* pNext);\r
-\r
- VkResult Map(VmaAllocation hAllocation, void** ppData);\r
- void Unmap(VmaAllocation hAllocation);\r
-\r
- VkResult BindBufferMemory(\r
- VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkBuffer hBuffer,\r
- const void* pNext);\r
- VkResult BindImageMemory(\r
- VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkImage hImage,\r
- const void* pNext);\r
-\r
- VkResult FlushOrInvalidateAllocation(\r
- VmaAllocation hAllocation,\r
- VkDeviceSize offset, VkDeviceSize size,\r
- VMA_CACHE_OPERATION op);\r
- VkResult FlushOrInvalidateAllocations(\r
- uint32_t allocationCount,\r
- const VmaAllocation* allocations,\r
- const VkDeviceSize* offsets, const VkDeviceSize* sizes,\r
- VMA_CACHE_OPERATION op);\r
-\r
- void FillAllocation(const VmaAllocation hAllocation, uint8_t pattern);\r
-\r
- /*\r
- Returns bit mask of memory types that can support defragmentation on GPU as\r
- they support creation of required buffer for copy operations.\r
- */\r
- uint32_t GetGpuDefragmentationMemoryTypeBits();\r
-\r
-private:\r
- VkDeviceSize m_PreferredLargeHeapBlockSize;\r
-\r
- VkPhysicalDevice m_PhysicalDevice;\r
- VMA_ATOMIC_UINT32 m_CurrentFrameIndex;\r
- VMA_ATOMIC_UINT32 m_GpuDefragmentationMemoryTypeBits; // UINT32_MAX means uninitialized.\r
- \r
- VMA_RW_MUTEX m_PoolsMutex;\r
- // Protected by m_PoolsMutex. Sorted by pointer value.\r
- VmaVector<VmaPool, VmaStlAllocator<VmaPool> > m_Pools;\r
- uint32_t m_NextPoolId;\r
-\r
- VmaVulkanFunctions m_VulkanFunctions;\r
-\r
- // Global bit mask AND-ed with any memoryTypeBits to disallow certain memory types.\r
- uint32_t m_GlobalMemoryTypeBits;\r
-\r
-#if VMA_RECORDING_ENABLED\r
- VmaRecorder* m_pRecorder;\r
-#endif\r
-\r
- void ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunctions);\r
-\r
-#if VMA_STATIC_VULKAN_FUNCTIONS == 1\r
- void ImportVulkanFunctions_Static();\r
-#endif\r
-\r
- void ImportVulkanFunctions_Custom(const VmaVulkanFunctions* pVulkanFunctions);\r
-\r
-#if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1\r
- void ImportVulkanFunctions_Dynamic();\r
-#endif\r
-\r
- void ValidateVulkanFunctions();\r
-\r
- VkDeviceSize CalcPreferredBlockSize(uint32_t memTypeIndex);\r
-\r
- VkResult AllocateMemoryOfType(\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- bool dedicatedAllocation,\r
- VkBuffer dedicatedBuffer,\r
- VkBufferUsageFlags dedicatedBufferUsage,\r
- VkImage dedicatedImage,\r
- const VmaAllocationCreateInfo& createInfo,\r
- uint32_t memTypeIndex,\r
- VmaSuballocationType suballocType,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations);\r
-\r
- // Helper function only to be used inside AllocateDedicatedMemory.\r
- VkResult AllocateDedicatedMemoryPage(\r
- VkDeviceSize size,\r
- VmaSuballocationType suballocType,\r
- uint32_t memTypeIndex,\r
- const VkMemoryAllocateInfo& allocInfo,\r
- bool map,\r
- bool isUserDataString,\r
- void* pUserData,\r
- VmaAllocation* pAllocation);\r
-\r
- // Allocates and registers new VkDeviceMemory specifically for dedicated allocations.\r
- VkResult AllocateDedicatedMemory(\r
- VkDeviceSize size,\r
- VmaSuballocationType suballocType,\r
- uint32_t memTypeIndex,\r
- bool withinBudget,\r
- bool map,\r
- bool isUserDataString,\r
- void* pUserData,\r
- VkBuffer dedicatedBuffer,\r
- VkBufferUsageFlags dedicatedBufferUsage,\r
- VkImage dedicatedImage,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations);\r
-\r
- void FreeDedicatedMemory(const VmaAllocation allocation);\r
-\r
- /*\r
- Calculates and returns bit mask of memory types that can support defragmentation\r
- on GPU as they support creation of required buffer for copy operations.\r
- */\r
- uint32_t CalculateGpuDefragmentationMemoryTypeBits() const;\r
-\r
- uint32_t CalculateGlobalMemoryTypeBits() const;\r
-\r
- bool GetFlushOrInvalidateRange(\r
- VmaAllocation allocation,\r
- VkDeviceSize offset, VkDeviceSize size,\r
- VkMappedMemoryRange& outRange) const;\r
-\r
-#if VMA_MEMORY_BUDGET\r
- void UpdateVulkanBudget();\r
-#endif // #if VMA_MEMORY_BUDGET\r
-};\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// Memory allocation #2 after VmaAllocator_T definition\r
-\r
-static void* VmaMalloc(VmaAllocator hAllocator, size_t size, size_t alignment)\r
-{\r
- return VmaMalloc(&hAllocator->m_AllocationCallbacks, size, alignment);\r
-}\r
-\r
-static void VmaFree(VmaAllocator hAllocator, void* ptr)\r
-{\r
- VmaFree(&hAllocator->m_AllocationCallbacks, ptr);\r
-}\r
-\r
-template<typename T>\r
-static T* VmaAllocate(VmaAllocator hAllocator)\r
-{\r
- return (T*)VmaMalloc(hAllocator, sizeof(T), VMA_ALIGN_OF(T));\r
-}\r
-\r
-template<typename T>\r
-static T* VmaAllocateArray(VmaAllocator hAllocator, size_t count)\r
-{\r
- return (T*)VmaMalloc(hAllocator, sizeof(T) * count, VMA_ALIGN_OF(T));\r
-}\r
-\r
-template<typename T>\r
-static void vma_delete(VmaAllocator hAllocator, T* ptr)\r
-{\r
- if(ptr != VMA_NULL)\r
- {\r
- ptr->~T();\r
- VmaFree(hAllocator, ptr);\r
- }\r
-}\r
-\r
-template<typename T>\r
-static void vma_delete_array(VmaAllocator hAllocator, T* ptr, size_t count)\r
-{\r
- if(ptr != VMA_NULL)\r
- {\r
- for(size_t i = count; i--; )\r
- ptr[i].~T();\r
- VmaFree(hAllocator, ptr);\r
- }\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaStringBuilder\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-class VmaStringBuilder\r
-{\r
-public:\r
- VmaStringBuilder(VmaAllocator alloc) : m_Data(VmaStlAllocator<char>(alloc->GetAllocationCallbacks())) { }\r
- size_t GetLength() const { return m_Data.size(); }\r
- const char* GetData() const { return m_Data.data(); }\r
-\r
- void Add(char ch) { m_Data.push_back(ch); }\r
- void Add(const char* pStr);\r
- void AddNewLine() { Add('\n'); }\r
- void AddNumber(uint32_t num);\r
- void AddNumber(uint64_t num);\r
- void AddPointer(const void* ptr);\r
-\r
-private:\r
- VmaVector< char, VmaStlAllocator<char> > m_Data;\r
-};\r
-\r
-void VmaStringBuilder::Add(const char* pStr)\r
-{\r
- const size_t strLen = strlen(pStr);\r
- if(strLen > 0)\r
- {\r
- const size_t oldCount = m_Data.size();\r
- m_Data.resize(oldCount + strLen);\r
- memcpy(m_Data.data() + oldCount, pStr, strLen);\r
- }\r
-}\r
-\r
-void VmaStringBuilder::AddNumber(uint32_t num)\r
-{\r
- char buf[11];\r
- buf[10] = '\0';\r
- char *p = &buf[10];\r
- do\r
- {\r
- *--p = '0' + (num % 10);\r
- num /= 10;\r
- }\r
- while(num);\r
- Add(p);\r
-}\r
-\r
-void VmaStringBuilder::AddNumber(uint64_t num)\r
-{\r
- char buf[21];\r
- buf[20] = '\0';\r
- char *p = &buf[20];\r
- do\r
- {\r
- *--p = '0' + (num % 10);\r
- num /= 10;\r
- }\r
- while(num);\r
- Add(p);\r
-}\r
-\r
-void VmaStringBuilder::AddPointer(const void* ptr)\r
-{\r
- char buf[21];\r
- VmaPtrToStr(buf, sizeof(buf), ptr);\r
- Add(buf);\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaJsonWriter\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-class VmaJsonWriter\r
-{\r
- VMA_CLASS_NO_COPY(VmaJsonWriter)\r
-public:\r
- VmaJsonWriter(const VkAllocationCallbacks* pAllocationCallbacks, VmaStringBuilder& sb);\r
- ~VmaJsonWriter();\r
-\r
- void BeginObject(bool singleLine = false);\r
- void EndObject();\r
- \r
- void BeginArray(bool singleLine = false);\r
- void EndArray();\r
- \r
- void WriteString(const char* pStr);\r
- void BeginString(const char* pStr = VMA_NULL);\r
- void ContinueString(const char* pStr);\r
- void ContinueString(uint32_t n);\r
- void ContinueString(uint64_t n);\r
- void ContinueString_Pointer(const void* ptr);\r
- void EndString(const char* pStr = VMA_NULL);\r
- \r
- void WriteNumber(uint32_t n);\r
- void WriteNumber(uint64_t n);\r
- void WriteBool(bool b);\r
- void WriteNull();\r
-\r
-private:\r
- static const char* const INDENT;\r
-\r
- enum COLLECTION_TYPE\r
- {\r
- COLLECTION_TYPE_OBJECT,\r
- COLLECTION_TYPE_ARRAY,\r
- };\r
- struct StackItem\r
- {\r
- COLLECTION_TYPE type;\r
- uint32_t valueCount;\r
- bool singleLineMode;\r
- };\r
-\r
- VmaStringBuilder& m_SB;\r
- VmaVector< StackItem, VmaStlAllocator<StackItem> > m_Stack;\r
- bool m_InsideString;\r
-\r
- void BeginValue(bool isString);\r
- void WriteIndent(bool oneLess = false);\r
-};\r
-\r
-const char* const VmaJsonWriter::INDENT = " ";\r
-\r
-VmaJsonWriter::VmaJsonWriter(const VkAllocationCallbacks* pAllocationCallbacks, VmaStringBuilder& sb) :\r
- m_SB(sb),\r
- m_Stack(VmaStlAllocator<StackItem>(pAllocationCallbacks)),\r
- m_InsideString(false)\r
-{\r
-}\r
-\r
-VmaJsonWriter::~VmaJsonWriter()\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
- VMA_ASSERT(m_Stack.empty());\r
-}\r
-\r
-void VmaJsonWriter::BeginObject(bool singleLine)\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
-\r
- BeginValue(false);\r
- m_SB.Add('{');\r
-\r
- StackItem item;\r
- item.type = COLLECTION_TYPE_OBJECT;\r
- item.valueCount = 0;\r
- item.singleLineMode = singleLine;\r
- m_Stack.push_back(item);\r
-}\r
-\r
-void VmaJsonWriter::EndObject()\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
-\r
- WriteIndent(true);\r
- m_SB.Add('}');\r
-\r
- VMA_ASSERT(!m_Stack.empty() && m_Stack.back().type == COLLECTION_TYPE_OBJECT);\r
- m_Stack.pop_back();\r
-}\r
-\r
-void VmaJsonWriter::BeginArray(bool singleLine)\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
-\r
- BeginValue(false);\r
- m_SB.Add('[');\r
-\r
- StackItem item;\r
- item.type = COLLECTION_TYPE_ARRAY;\r
- item.valueCount = 0;\r
- item.singleLineMode = singleLine;\r
- m_Stack.push_back(item);\r
-}\r
-\r
-void VmaJsonWriter::EndArray()\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
-\r
- WriteIndent(true);\r
- m_SB.Add(']');\r
-\r
- VMA_ASSERT(!m_Stack.empty() && m_Stack.back().type == COLLECTION_TYPE_ARRAY);\r
- m_Stack.pop_back();\r
-}\r
-\r
-void VmaJsonWriter::WriteString(const char* pStr)\r
-{\r
- BeginString(pStr);\r
- EndString();\r
-}\r
-\r
-void VmaJsonWriter::BeginString(const char* pStr)\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
-\r
- BeginValue(true);\r
- m_SB.Add('"');\r
- m_InsideString = true;\r
- if(pStr != VMA_NULL && pStr[0] != '\0')\r
- {\r
- ContinueString(pStr);\r
- }\r
-}\r
-\r
-void VmaJsonWriter::ContinueString(const char* pStr)\r
-{\r
- VMA_ASSERT(m_InsideString);\r
-\r
- const size_t strLen = strlen(pStr);\r
- for(size_t i = 0; i < strLen; ++i)\r
- {\r
- char ch = pStr[i];\r
- if(ch == '\\')\r
- {\r
- m_SB.Add("\\\\");\r
- }\r
- else if(ch == '"')\r
- {\r
- m_SB.Add("\\\"");\r
- }\r
- else if(ch >= 32)\r
- {\r
- m_SB.Add(ch);\r
- }\r
- else switch(ch)\r
- {\r
- case '\b':\r
- m_SB.Add("\\b");\r
- break;\r
- case '\f':\r
- m_SB.Add("\\f");\r
- break;\r
- case '\n':\r
- m_SB.Add("\\n");\r
- break;\r
- case '\r':\r
- m_SB.Add("\\r");\r
- break;\r
- case '\t':\r
- m_SB.Add("\\t");\r
- break;\r
- default:\r
- VMA_ASSERT(0 && "Character not currently supported.");\r
- break;\r
- }\r
- }\r
-}\r
-\r
-void VmaJsonWriter::ContinueString(uint32_t n)\r
-{\r
- VMA_ASSERT(m_InsideString);\r
- m_SB.AddNumber(n);\r
-}\r
-\r
-void VmaJsonWriter::ContinueString(uint64_t n)\r
-{\r
- VMA_ASSERT(m_InsideString);\r
- m_SB.AddNumber(n);\r
-}\r
-\r
-void VmaJsonWriter::ContinueString_Pointer(const void* ptr)\r
-{\r
- VMA_ASSERT(m_InsideString);\r
- m_SB.AddPointer(ptr);\r
-}\r
-\r
-void VmaJsonWriter::EndString(const char* pStr)\r
-{\r
- VMA_ASSERT(m_InsideString);\r
- if(pStr != VMA_NULL && pStr[0] != '\0')\r
- {\r
- ContinueString(pStr);\r
- }\r
- m_SB.Add('"');\r
- m_InsideString = false;\r
-}\r
-\r
-void VmaJsonWriter::WriteNumber(uint32_t n)\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
- BeginValue(false);\r
- m_SB.AddNumber(n);\r
-}\r
-\r
-void VmaJsonWriter::WriteNumber(uint64_t n)\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
- BeginValue(false);\r
- m_SB.AddNumber(n);\r
-}\r
-\r
-void VmaJsonWriter::WriteBool(bool b)\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
- BeginValue(false);\r
- m_SB.Add(b ? "true" : "false");\r
-}\r
-\r
-void VmaJsonWriter::WriteNull()\r
-{\r
- VMA_ASSERT(!m_InsideString);\r
- BeginValue(false);\r
- m_SB.Add("null");\r
-}\r
-\r
-void VmaJsonWriter::BeginValue(bool isString)\r
-{\r
- if(!m_Stack.empty())\r
- {\r
- StackItem& currItem = m_Stack.back();\r
- if(currItem.type == COLLECTION_TYPE_OBJECT &&\r
- currItem.valueCount % 2 == 0)\r
- {\r
- VMA_ASSERT(isString);\r
- }\r
-\r
- if(currItem.type == COLLECTION_TYPE_OBJECT &&\r
- currItem.valueCount % 2 != 0)\r
- {\r
- m_SB.Add(": ");\r
- }\r
- else if(currItem.valueCount > 0)\r
- {\r
- m_SB.Add(", ");\r
- WriteIndent();\r
- }\r
- else\r
- {\r
- WriteIndent();\r
- }\r
- ++currItem.valueCount;\r
- }\r
-}\r
-\r
-void VmaJsonWriter::WriteIndent(bool oneLess)\r
-{\r
- if(!m_Stack.empty() && !m_Stack.back().singleLineMode)\r
- {\r
- m_SB.AddNewLine();\r
- \r
- size_t count = m_Stack.size();\r
- if(count > 0 && oneLess)\r
- {\r
- --count;\r
- }\r
- for(size_t i = 0; i < count; ++i)\r
- {\r
- m_SB.Add(INDENT);\r
- }\r
- }\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-\r
-void VmaAllocation_T::SetUserData(VmaAllocator hAllocator, void* pUserData)\r
-{\r
- if(IsUserDataString())\r
- {\r
- VMA_ASSERT(pUserData == VMA_NULL || pUserData != m_pUserData);\r
-\r
- FreeUserDataString(hAllocator);\r
-\r
- if(pUserData != VMA_NULL)\r
- {\r
- m_pUserData = VmaCreateStringCopy(hAllocator->GetAllocationCallbacks(), (const char*)pUserData);\r
- }\r
- }\r
- else\r
- {\r
- m_pUserData = pUserData;\r
- }\r
-}\r
-\r
-void VmaAllocation_T::ChangeBlockAllocation(\r
- VmaAllocator hAllocator,\r
- VmaDeviceMemoryBlock* block,\r
- VkDeviceSize offset)\r
-{\r
- VMA_ASSERT(block != VMA_NULL);\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_BLOCK);\r
-\r
- // Move mapping reference counter from old block to new block.\r
- if(block != m_BlockAllocation.m_Block)\r
- {\r
- uint32_t mapRefCount = m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP;\r
- if(IsPersistentMap())\r
- ++mapRefCount;\r
- m_BlockAllocation.m_Block->Unmap(hAllocator, mapRefCount);\r
- block->Map(hAllocator, mapRefCount, VMA_NULL);\r
- }\r
-\r
- m_BlockAllocation.m_Block = block;\r
- m_BlockAllocation.m_Offset = offset;\r
-}\r
-\r
-void VmaAllocation_T::ChangeOffset(VkDeviceSize newOffset)\r
-{\r
- VMA_ASSERT(m_Type == ALLOCATION_TYPE_BLOCK);\r
- m_BlockAllocation.m_Offset = newOffset;\r
-}\r
-\r
-VkDeviceSize VmaAllocation_T::GetOffset() const\r
-{\r
- switch(m_Type)\r
- {\r
- case ALLOCATION_TYPE_BLOCK:\r
- return m_BlockAllocation.m_Offset;\r
- case ALLOCATION_TYPE_DEDICATED:\r
- return 0;\r
- default:\r
- VMA_ASSERT(0);\r
- return 0;\r
- }\r
-}\r
-\r
-VkDeviceMemory VmaAllocation_T::GetMemory() const\r
-{\r
- switch(m_Type)\r
- {\r
- case ALLOCATION_TYPE_BLOCK:\r
- return m_BlockAllocation.m_Block->GetDeviceMemory();\r
- case ALLOCATION_TYPE_DEDICATED:\r
- return m_DedicatedAllocation.m_hMemory;\r
- default:\r
- VMA_ASSERT(0);\r
- return VK_NULL_HANDLE;\r
- }\r
-}\r
-\r
-void* VmaAllocation_T::GetMappedData() const\r
-{\r
- switch(m_Type)\r
- {\r
- case ALLOCATION_TYPE_BLOCK:\r
- if(m_MapCount != 0)\r
- {\r
- void* pBlockData = m_BlockAllocation.m_Block->GetMappedData();\r
- VMA_ASSERT(pBlockData != VMA_NULL);\r
- return (char*)pBlockData + m_BlockAllocation.m_Offset;\r
- }\r
- else\r
- {\r
- return VMA_NULL;\r
- }\r
- break;\r
- case ALLOCATION_TYPE_DEDICATED:\r
- VMA_ASSERT((m_DedicatedAllocation.m_pMappedData != VMA_NULL) == (m_MapCount != 0));\r
- return m_DedicatedAllocation.m_pMappedData;\r
- default:\r
- VMA_ASSERT(0);\r
- return VMA_NULL;\r
- }\r
-}\r
-\r
-bool VmaAllocation_T::CanBecomeLost() const\r
-{\r
- switch(m_Type)\r
- {\r
- case ALLOCATION_TYPE_BLOCK:\r
- return m_BlockAllocation.m_CanBecomeLost;\r
- case ALLOCATION_TYPE_DEDICATED:\r
- return false;\r
- default:\r
- VMA_ASSERT(0);\r
- return false;\r
- }\r
-}\r
-\r
-bool VmaAllocation_T::MakeLost(uint32_t currentFrameIndex, uint32_t frameInUseCount)\r
-{\r
- VMA_ASSERT(CanBecomeLost());\r
-\r
- /*\r
- Warning: This is a carefully designed algorithm.\r
- Do not modify unless you really know what you're doing :)\r
- */\r
- uint32_t localLastUseFrameIndex = GetLastUseFrameIndex();\r
- for(;;)\r
- {\r
- if(localLastUseFrameIndex == VMA_FRAME_INDEX_LOST)\r
- {\r
- VMA_ASSERT(0);\r
- return false;\r
- }\r
- else if(localLastUseFrameIndex + frameInUseCount >= currentFrameIndex)\r
- {\r
- return false;\r
- }\r
- else // Last use time earlier than current time.\r
- {\r
- if(CompareExchangeLastUseFrameIndex(localLastUseFrameIndex, VMA_FRAME_INDEX_LOST))\r
- {\r
- // Setting hAllocation.LastUseFrameIndex atomic to VMA_FRAME_INDEX_LOST is enough to mark it as LOST.\r
- // Calling code just needs to unregister this allocation in owning VmaDeviceMemoryBlock.\r
- return true;\r
- }\r
- }\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-// Correspond to values of enum VmaSuballocationType.\r
-static const char* VMA_SUBALLOCATION_TYPE_NAMES[] = {\r
- "FREE",\r
- "UNKNOWN",\r
- "BUFFER",\r
- "IMAGE_UNKNOWN",\r
- "IMAGE_LINEAR",\r
- "IMAGE_OPTIMAL",\r
-};\r
-\r
-void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const\r
-{\r
- json.WriteString("Type");\r
- json.WriteString(VMA_SUBALLOCATION_TYPE_NAMES[m_SuballocationType]);\r
-\r
- json.WriteString("Size");\r
- json.WriteNumber(m_Size);\r
-\r
- if(m_pUserData != VMA_NULL)\r
- {\r
- json.WriteString("UserData");\r
- if(IsUserDataString())\r
- {\r
- json.WriteString((const char*)m_pUserData);\r
- }\r
- else\r
- {\r
- json.BeginString();\r
- json.ContinueString_Pointer(m_pUserData);\r
- json.EndString();\r
- }\r
- }\r
-\r
- json.WriteString("CreationFrameIndex");\r
- json.WriteNumber(m_CreationFrameIndex);\r
-\r
- json.WriteString("LastUseFrameIndex");\r
- json.WriteNumber(GetLastUseFrameIndex());\r
-\r
- if(m_BufferImageUsage != 0)\r
- {\r
- json.WriteString("Usage");\r
- json.WriteNumber(m_BufferImageUsage);\r
- }\r
-}\r
-\r
-#endif\r
-\r
-void VmaAllocation_T::FreeUserDataString(VmaAllocator hAllocator)\r
-{\r
- VMA_ASSERT(IsUserDataString());\r
- VmaFreeString(hAllocator->GetAllocationCallbacks(), (char*)m_pUserData);\r
- m_pUserData = VMA_NULL;\r
-}\r
-\r
-void VmaAllocation_T::BlockAllocMap()\r
-{\r
- VMA_ASSERT(GetType() == ALLOCATION_TYPE_BLOCK);\r
-\r
- if((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) < 0x7F)\r
- {\r
- ++m_MapCount;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0 && "Allocation mapped too many times simultaneously.");\r
- }\r
-}\r
-\r
-void VmaAllocation_T::BlockAllocUnmap()\r
-{\r
- VMA_ASSERT(GetType() == ALLOCATION_TYPE_BLOCK);\r
-\r
- if((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) != 0)\r
- {\r
- --m_MapCount;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0 && "Unmapping allocation not previously mapped.");\r
- }\r
-}\r
-\r
-VkResult VmaAllocation_T::DedicatedAllocMap(VmaAllocator hAllocator, void** ppData)\r
-{\r
- VMA_ASSERT(GetType() == ALLOCATION_TYPE_DEDICATED);\r
-\r
- if(m_MapCount != 0)\r
- {\r
- if((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) < 0x7F)\r
- {\r
- VMA_ASSERT(m_DedicatedAllocation.m_pMappedData != VMA_NULL);\r
- *ppData = m_DedicatedAllocation.m_pMappedData;\r
- ++m_MapCount;\r
- return VK_SUCCESS;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0 && "Dedicated allocation mapped too many times simultaneously.");\r
- return VK_ERROR_MEMORY_MAP_FAILED;\r
- }\r
- }\r
- else\r
- {\r
- VkResult result = (*hAllocator->GetVulkanFunctions().vkMapMemory)(\r
- hAllocator->m_hDevice,\r
- m_DedicatedAllocation.m_hMemory,\r
- 0, // offset\r
- VK_WHOLE_SIZE,\r
- 0, // flags\r
- ppData);\r
- if(result == VK_SUCCESS)\r
- {\r
- m_DedicatedAllocation.m_pMappedData = *ppData;\r
- m_MapCount = 1;\r
- }\r
- return result;\r
- }\r
-}\r
-\r
-void VmaAllocation_T::DedicatedAllocUnmap(VmaAllocator hAllocator)\r
-{\r
- VMA_ASSERT(GetType() == ALLOCATION_TYPE_DEDICATED);\r
-\r
- if((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) != 0)\r
- {\r
- --m_MapCount;\r
- if(m_MapCount == 0)\r
- {\r
- m_DedicatedAllocation.m_pMappedData = VMA_NULL;\r
- (*hAllocator->GetVulkanFunctions().vkUnmapMemory)(\r
- hAllocator->m_hDevice,\r
- m_DedicatedAllocation.m_hMemory);\r
- }\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0 && "Unmapping dedicated allocation not previously mapped.");\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-static void VmaPrintStatInfo(VmaJsonWriter& json, const VmaStatInfo& stat)\r
-{\r
- json.BeginObject();\r
-\r
- json.WriteString("Blocks");\r
- json.WriteNumber(stat.blockCount);\r
-\r
- json.WriteString("Allocations");\r
- json.WriteNumber(stat.allocationCount);\r
-\r
- json.WriteString("UnusedRanges");\r
- json.WriteNumber(stat.unusedRangeCount);\r
-\r
- json.WriteString("UsedBytes");\r
- json.WriteNumber(stat.usedBytes);\r
-\r
- json.WriteString("UnusedBytes");\r
- json.WriteNumber(stat.unusedBytes);\r
-\r
- if(stat.allocationCount > 1)\r
- {\r
- json.WriteString("AllocationSize");\r
- json.BeginObject(true);\r
- json.WriteString("Min");\r
- json.WriteNumber(stat.allocationSizeMin);\r
- json.WriteString("Avg");\r
- json.WriteNumber(stat.allocationSizeAvg);\r
- json.WriteString("Max");\r
- json.WriteNumber(stat.allocationSizeMax);\r
- json.EndObject();\r
- }\r
-\r
- if(stat.unusedRangeCount > 1)\r
- {\r
- json.WriteString("UnusedRangeSize");\r
- json.BeginObject(true);\r
- json.WriteString("Min");\r
- json.WriteNumber(stat.unusedRangeSizeMin);\r
- json.WriteString("Avg");\r
- json.WriteNumber(stat.unusedRangeSizeAvg);\r
- json.WriteString("Max");\r
- json.WriteNumber(stat.unusedRangeSizeMax);\r
- json.EndObject();\r
- }\r
-\r
- json.EndObject();\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-struct VmaSuballocationItemSizeLess\r
-{\r
- bool operator()(\r
- const VmaSuballocationList::iterator lhs,\r
- const VmaSuballocationList::iterator rhs) const\r
- {\r
- return lhs->size < rhs->size;\r
- }\r
- bool operator()(\r
- const VmaSuballocationList::iterator lhs,\r
- VkDeviceSize rhsSize) const\r
- {\r
- return lhs->size < rhsSize;\r
- }\r
-};\r
-\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaBlockMetadata\r
-\r
-VmaBlockMetadata::VmaBlockMetadata(VmaAllocator hAllocator) :\r
- m_Size(0),\r
- m_pAllocationCallbacks(hAllocator->GetAllocationCallbacks())\r
-{\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-void VmaBlockMetadata::PrintDetailedMap_Begin(class VmaJsonWriter& json,\r
- VkDeviceSize unusedBytes,\r
- size_t allocationCount,\r
- size_t unusedRangeCount) const\r
-{\r
- json.BeginObject();\r
-\r
- json.WriteString("TotalBytes");\r
- json.WriteNumber(GetSize());\r
-\r
- json.WriteString("UnusedBytes");\r
- json.WriteNumber(unusedBytes);\r
-\r
- json.WriteString("Allocations");\r
- json.WriteNumber((uint64_t)allocationCount);\r
-\r
- json.WriteString("UnusedRanges");\r
- json.WriteNumber((uint64_t)unusedRangeCount);\r
-\r
- json.WriteString("Suballocations");\r
- json.BeginArray();\r
-}\r
-\r
-void VmaBlockMetadata::PrintDetailedMap_Allocation(class VmaJsonWriter& json,\r
- VkDeviceSize offset,\r
- VmaAllocation hAllocation) const\r
-{\r
- json.BeginObject(true);\r
- \r
- json.WriteString("Offset");\r
- json.WriteNumber(offset);\r
-\r
- hAllocation->PrintParameters(json);\r
-\r
- json.EndObject();\r
-}\r
-\r
-void VmaBlockMetadata::PrintDetailedMap_UnusedRange(class VmaJsonWriter& json,\r
- VkDeviceSize offset,\r
- VkDeviceSize size) const\r
-{\r
- json.BeginObject(true);\r
- \r
- json.WriteString("Offset");\r
- json.WriteNumber(offset);\r
-\r
- json.WriteString("Type");\r
- json.WriteString(VMA_SUBALLOCATION_TYPE_NAMES[VMA_SUBALLOCATION_TYPE_FREE]);\r
-\r
- json.WriteString("Size");\r
- json.WriteNumber(size);\r
-\r
- json.EndObject();\r
-}\r
-\r
-void VmaBlockMetadata::PrintDetailedMap_End(class VmaJsonWriter& json) const\r
-{\r
- json.EndArray();\r
- json.EndObject();\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaBlockMetadata_Generic\r
-\r
-VmaBlockMetadata_Generic::VmaBlockMetadata_Generic(VmaAllocator hAllocator) :\r
- VmaBlockMetadata(hAllocator),\r
- m_FreeCount(0),\r
- m_SumFreeSize(0),\r
- m_Suballocations(VmaStlAllocator<VmaSuballocation>(hAllocator->GetAllocationCallbacks())),\r
- m_FreeSuballocationsBySize(VmaStlAllocator<VmaSuballocationList::iterator>(hAllocator->GetAllocationCallbacks()))\r
-{\r
-}\r
-\r
-VmaBlockMetadata_Generic::~VmaBlockMetadata_Generic()\r
-{\r
-}\r
-\r
-void VmaBlockMetadata_Generic::Init(VkDeviceSize size)\r
-{\r
- VmaBlockMetadata::Init(size);\r
-\r
- m_FreeCount = 1;\r
- m_SumFreeSize = size;\r
-\r
- VmaSuballocation suballoc = {};\r
- suballoc.offset = 0;\r
- suballoc.size = size;\r
- suballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- suballoc.hAllocation = VK_NULL_HANDLE;\r
-\r
- VMA_ASSERT(size > VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER);\r
- m_Suballocations.push_back(suballoc);\r
- VmaSuballocationList::iterator suballocItem = m_Suballocations.end();\r
- --suballocItem;\r
- m_FreeSuballocationsBySize.push_back(suballocItem);\r
-}\r
-\r
-bool VmaBlockMetadata_Generic::Validate() const\r
-{\r
- VMA_VALIDATE(!m_Suballocations.empty());\r
- \r
- // Expected offset of new suballocation as calculated from previous ones.\r
- VkDeviceSize calculatedOffset = 0;\r
- // Expected number of free suballocations as calculated from traversing their list.\r
- uint32_t calculatedFreeCount = 0;\r
- // Expected sum size of free suballocations as calculated from traversing their list.\r
- VkDeviceSize calculatedSumFreeSize = 0;\r
- // Expected number of free suballocations that should be registered in\r
- // m_FreeSuballocationsBySize calculated from traversing their list.\r
- size_t freeSuballocationsToRegister = 0;\r
- // True if previous visited suballocation was free.\r
- bool prevFree = false;\r
-\r
- for(VmaSuballocationList::const_iterator suballocItem = m_Suballocations.cbegin();\r
- suballocItem != m_Suballocations.cend();\r
- ++suballocItem)\r
- {\r
- const VmaSuballocation& subAlloc = *suballocItem;\r
- \r
- // Actual offset of this suballocation doesn't match expected one.\r
- VMA_VALIDATE(subAlloc.offset == calculatedOffset);\r
-\r
- const bool currFree = (subAlloc.type == VMA_SUBALLOCATION_TYPE_FREE);\r
- // Two adjacent free suballocations are invalid. They should be merged.\r
- VMA_VALIDATE(!prevFree || !currFree);\r
-\r
- VMA_VALIDATE(currFree == (subAlloc.hAllocation == VK_NULL_HANDLE));\r
-\r
- if(currFree)\r
- {\r
- calculatedSumFreeSize += subAlloc.size;\r
- ++calculatedFreeCount;\r
- if(subAlloc.size >= VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- ++freeSuballocationsToRegister;\r
- }\r
-\r
- // Margin required between allocations - every free space must be at least that large.\r
- VMA_VALIDATE(subAlloc.size >= VMA_DEBUG_MARGIN);\r
- }\r
- else\r
- {\r
- VMA_VALIDATE(subAlloc.hAllocation->GetOffset() == subAlloc.offset);\r
- VMA_VALIDATE(subAlloc.hAllocation->GetSize() == subAlloc.size);\r
-\r
- // Margin required between allocations - previous allocation must be free.\r
- VMA_VALIDATE(VMA_DEBUG_MARGIN == 0 || prevFree);\r
- }\r
-\r
- calculatedOffset += subAlloc.size;\r
- prevFree = currFree;\r
- }\r
-\r
- // Number of free suballocations registered in m_FreeSuballocationsBySize doesn't\r
- // match expected one.\r
- VMA_VALIDATE(m_FreeSuballocationsBySize.size() == freeSuballocationsToRegister);\r
-\r
- VkDeviceSize lastSize = 0;\r
- for(size_t i = 0; i < m_FreeSuballocationsBySize.size(); ++i)\r
- {\r
- VmaSuballocationList::iterator suballocItem = m_FreeSuballocationsBySize[i];\r
- \r
- // Only free suballocations can be registered in m_FreeSuballocationsBySize.\r
- VMA_VALIDATE(suballocItem->type == VMA_SUBALLOCATION_TYPE_FREE);\r
- // They must be sorted by size ascending.\r
- VMA_VALIDATE(suballocItem->size >= lastSize);\r
-\r
- lastSize = suballocItem->size;\r
- }\r
-\r
- // Check if totals match calculacted values.\r
- VMA_VALIDATE(ValidateFreeSuballocationList());\r
- VMA_VALIDATE(calculatedOffset == GetSize());\r
- VMA_VALIDATE(calculatedSumFreeSize == m_SumFreeSize);\r
- VMA_VALIDATE(calculatedFreeCount == m_FreeCount);\r
-\r
- return true;\r
-}\r
-\r
-VkDeviceSize VmaBlockMetadata_Generic::GetUnusedRangeSizeMax() const\r
-{\r
- if(!m_FreeSuballocationsBySize.empty())\r
- {\r
- return m_FreeSuballocationsBySize.back()->size;\r
- }\r
- else\r
- {\r
- return 0;\r
- }\r
-}\r
-\r
-bool VmaBlockMetadata_Generic::IsEmpty() const\r
-{\r
- return (m_Suballocations.size() == 1) && (m_FreeCount == 1);\r
-}\r
-\r
-void VmaBlockMetadata_Generic::CalcAllocationStatInfo(VmaStatInfo& outInfo) const\r
-{\r
- outInfo.blockCount = 1;\r
-\r
- const uint32_t rangeCount = (uint32_t)m_Suballocations.size();\r
- outInfo.allocationCount = rangeCount - m_FreeCount;\r
- outInfo.unusedRangeCount = m_FreeCount;\r
- \r
- outInfo.unusedBytes = m_SumFreeSize;\r
- outInfo.usedBytes = GetSize() - outInfo.unusedBytes;\r
-\r
- outInfo.allocationSizeMin = UINT64_MAX;\r
- outInfo.allocationSizeMax = 0;\r
- outInfo.unusedRangeSizeMin = UINT64_MAX;\r
- outInfo.unusedRangeSizeMax = 0;\r
-\r
- for(VmaSuballocationList::const_iterator suballocItem = m_Suballocations.cbegin();\r
- suballocItem != m_Suballocations.cend();\r
- ++suballocItem)\r
- {\r
- const VmaSuballocation& suballoc = *suballocItem;\r
- if(suballoc.type != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size);\r
- outInfo.allocationSizeMax = VMA_MAX(outInfo.allocationSizeMax, suballoc.size);\r
- }\r
- else\r
- {\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, suballoc.size);\r
- outInfo.unusedRangeSizeMax = VMA_MAX(outInfo.unusedRangeSizeMax, suballoc.size);\r
- }\r
- }\r
-}\r
-\r
-void VmaBlockMetadata_Generic::AddPoolStats(VmaPoolStats& inoutStats) const\r
-{\r
- const uint32_t rangeCount = (uint32_t)m_Suballocations.size();\r
-\r
- inoutStats.size += GetSize();\r
- inoutStats.unusedSize += m_SumFreeSize;\r
- inoutStats.allocationCount += rangeCount - m_FreeCount;\r
- inoutStats.unusedRangeCount += m_FreeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, GetUnusedRangeSizeMax());\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-void VmaBlockMetadata_Generic::PrintDetailedMap(class VmaJsonWriter& json) const\r
-{\r
- PrintDetailedMap_Begin(json,\r
- m_SumFreeSize, // unusedBytes\r
- m_Suballocations.size() - (size_t)m_FreeCount, // allocationCount\r
- m_FreeCount); // unusedRangeCount\r
-\r
- size_t i = 0;\r
- for(VmaSuballocationList::const_iterator suballocItem = m_Suballocations.cbegin();\r
- suballocItem != m_Suballocations.cend();\r
- ++suballocItem, ++i)\r
- {\r
- if(suballocItem->type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- PrintDetailedMap_UnusedRange(json, suballocItem->offset, suballocItem->size);\r
- }\r
- else\r
- {\r
- PrintDetailedMap_Allocation(json, suballocItem->offset, suballocItem->hAllocation);\r
- }\r
- }\r
-\r
- PrintDetailedMap_End(json);\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-bool VmaBlockMetadata_Generic::CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- VMA_ASSERT(allocSize > 0);\r
- VMA_ASSERT(!upperAddress);\r
- VMA_ASSERT(allocType != VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_ASSERT(pAllocationRequest != VMA_NULL);\r
- VMA_HEAVY_ASSERT(Validate());\r
-\r
- pAllocationRequest->type = VmaAllocationRequestType::Normal;\r
-\r
- // There is not enough total free space in this block to fullfill the request: Early return.\r
- if(canMakeOtherLost == false &&\r
- m_SumFreeSize < allocSize + 2 * VMA_DEBUG_MARGIN)\r
- {\r
- return false;\r
- }\r
-\r
- // New algorithm, efficiently searching freeSuballocationsBySize.\r
- const size_t freeSuballocCount = m_FreeSuballocationsBySize.size();\r
- if(freeSuballocCount > 0)\r
- {\r
- if(strategy == VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT)\r
- {\r
- // Find first free suballocation with size not less than allocSize + 2 * VMA_DEBUG_MARGIN.\r
- VmaSuballocationList::iterator* const it = VmaBinaryFindFirstNotLess(\r
- m_FreeSuballocationsBySize.data(),\r
- m_FreeSuballocationsBySize.data() + freeSuballocCount,\r
- allocSize + 2 * VMA_DEBUG_MARGIN,\r
- VmaSuballocationItemSizeLess());\r
- size_t index = it - m_FreeSuballocationsBySize.data();\r
- for(; index < freeSuballocCount; ++index)\r
- {\r
- if(CheckAllocation(\r
- currentFrameIndex,\r
- frameInUseCount,\r
- bufferImageGranularity,\r
- allocSize,\r
- allocAlignment,\r
- allocType,\r
- m_FreeSuballocationsBySize[index],\r
- false, // canMakeOtherLost\r
- &pAllocationRequest->offset,\r
- &pAllocationRequest->itemsToMakeLostCount,\r
- &pAllocationRequest->sumFreeSize,\r
- &pAllocationRequest->sumItemSize))\r
- {\r
- pAllocationRequest->item = m_FreeSuballocationsBySize[index];\r
- return true;\r
- }\r
- }\r
- }\r
- else if(strategy == VMA_ALLOCATION_INTERNAL_STRATEGY_MIN_OFFSET)\r
- {\r
- for(VmaSuballocationList::iterator it = m_Suballocations.begin();\r
- it != m_Suballocations.end();\r
- ++it)\r
- {\r
- if(it->type == VMA_SUBALLOCATION_TYPE_FREE && CheckAllocation(\r
- currentFrameIndex,\r
- frameInUseCount,\r
- bufferImageGranularity,\r
- allocSize,\r
- allocAlignment,\r
- allocType,\r
- it,\r
- false, // canMakeOtherLost\r
- &pAllocationRequest->offset,\r
- &pAllocationRequest->itemsToMakeLostCount,\r
- &pAllocationRequest->sumFreeSize,\r
- &pAllocationRequest->sumItemSize))\r
- {\r
- pAllocationRequest->item = it;\r
- return true;\r
- }\r
- }\r
- }\r
- else // WORST_FIT, FIRST_FIT\r
- {\r
- // Search staring from biggest suballocations.\r
- for(size_t index = freeSuballocCount; index--; )\r
- {\r
- if(CheckAllocation(\r
- currentFrameIndex,\r
- frameInUseCount,\r
- bufferImageGranularity,\r
- allocSize,\r
- allocAlignment,\r
- allocType,\r
- m_FreeSuballocationsBySize[index],\r
- false, // canMakeOtherLost\r
- &pAllocationRequest->offset,\r
- &pAllocationRequest->itemsToMakeLostCount,\r
- &pAllocationRequest->sumFreeSize,\r
- &pAllocationRequest->sumItemSize))\r
- {\r
- pAllocationRequest->item = m_FreeSuballocationsBySize[index];\r
- return true;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if(canMakeOtherLost)\r
- {\r
- // Brute-force algorithm. TODO: Come up with something better.\r
-\r
- bool found = false;\r
- VmaAllocationRequest tmpAllocRequest = {};\r
- tmpAllocRequest.type = VmaAllocationRequestType::Normal;\r
- for(VmaSuballocationList::iterator suballocIt = m_Suballocations.begin();\r
- suballocIt != m_Suballocations.end();\r
- ++suballocIt)\r
- {\r
- if(suballocIt->type == VMA_SUBALLOCATION_TYPE_FREE ||\r
- suballocIt->hAllocation->CanBecomeLost())\r
- {\r
- if(CheckAllocation(\r
- currentFrameIndex,\r
- frameInUseCount,\r
- bufferImageGranularity,\r
- allocSize,\r
- allocAlignment,\r
- allocType,\r
- suballocIt,\r
- canMakeOtherLost,\r
- &tmpAllocRequest.offset,\r
- &tmpAllocRequest.itemsToMakeLostCount,\r
- &tmpAllocRequest.sumFreeSize,\r
- &tmpAllocRequest.sumItemSize))\r
- {\r
- if(strategy == VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT)\r
- {\r
- *pAllocationRequest = tmpAllocRequest;\r
- pAllocationRequest->item = suballocIt;\r
- break;\r
- }\r
- if(!found || tmpAllocRequest.CalcCost() < pAllocationRequest->CalcCost())\r
- {\r
- *pAllocationRequest = tmpAllocRequest;\r
- pAllocationRequest->item = suballocIt;\r
- found = true;\r
- }\r
- }\r
- }\r
- }\r
-\r
- return found;\r
- }\r
-\r
- return false;\r
-}\r
-\r
-bool VmaBlockMetadata_Generic::MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- VMA_ASSERT(pAllocationRequest && pAllocationRequest->type == VmaAllocationRequestType::Normal);\r
-\r
- while(pAllocationRequest->itemsToMakeLostCount > 0)\r
- {\r
- if(pAllocationRequest->item->type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- ++pAllocationRequest->item;\r
- }\r
- VMA_ASSERT(pAllocationRequest->item != m_Suballocations.end());\r
- VMA_ASSERT(pAllocationRequest->item->hAllocation != VK_NULL_HANDLE);\r
- VMA_ASSERT(pAllocationRequest->item->hAllocation->CanBecomeLost());\r
- if(pAllocationRequest->item->hAllocation->MakeLost(currentFrameIndex, frameInUseCount))\r
- {\r
- pAllocationRequest->item = FreeSuballocation(pAllocationRequest->item);\r
- --pAllocationRequest->itemsToMakeLostCount;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
-\r
- VMA_HEAVY_ASSERT(Validate());\r
- VMA_ASSERT(pAllocationRequest->item != m_Suballocations.end());\r
- VMA_ASSERT(pAllocationRequest->item->type == VMA_SUBALLOCATION_TYPE_FREE);\r
- \r
- return true;\r
-}\r
-\r
-uint32_t VmaBlockMetadata_Generic::MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount)\r
-{\r
- uint32_t lostAllocationCount = 0;\r
- for(VmaSuballocationList::iterator it = m_Suballocations.begin();\r
- it != m_Suballocations.end();\r
- ++it)\r
- {\r
- if(it->type != VMA_SUBALLOCATION_TYPE_FREE &&\r
- it->hAllocation->CanBecomeLost() &&\r
- it->hAllocation->MakeLost(currentFrameIndex, frameInUseCount))\r
- {\r
- it = FreeSuballocation(it);\r
- ++lostAllocationCount;\r
- }\r
- }\r
- return lostAllocationCount;\r
-}\r
-\r
-VkResult VmaBlockMetadata_Generic::CheckCorruption(const void* pBlockData)\r
-{\r
- for(VmaSuballocationList::iterator it = m_Suballocations.begin();\r
- it != m_Suballocations.end();\r
- ++it)\r
- {\r
- if(it->type != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- if(!VmaValidateMagicValue(pBlockData, it->offset - VMA_DEBUG_MARGIN))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED BEFORE VALIDATED ALLOCATION!");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- if(!VmaValidateMagicValue(pBlockData, it->offset + it->size))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED AFTER VALIDATED ALLOCATION!");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- }\r
- }\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaBlockMetadata_Generic::Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation)\r
-{\r
- VMA_ASSERT(request.type == VmaAllocationRequestType::Normal);\r
- VMA_ASSERT(request.item != m_Suballocations.end());\r
- VmaSuballocation& suballoc = *request.item;\r
- // Given suballocation is a free block.\r
- VMA_ASSERT(suballoc.type == VMA_SUBALLOCATION_TYPE_FREE);\r
- // Given offset is inside this suballocation.\r
- VMA_ASSERT(request.offset >= suballoc.offset);\r
- const VkDeviceSize paddingBegin = request.offset - suballoc.offset;\r
- VMA_ASSERT(suballoc.size >= paddingBegin + allocSize);\r
- const VkDeviceSize paddingEnd = suballoc.size - paddingBegin - allocSize;\r
-\r
- // Unregister this free suballocation from m_FreeSuballocationsBySize and update\r
- // it to become used.\r
- UnregisterFreeSuballocation(request.item);\r
-\r
- suballoc.offset = request.offset;\r
- suballoc.size = allocSize;\r
- suballoc.type = type;\r
- suballoc.hAllocation = hAllocation;\r
-\r
- // If there are any free bytes remaining at the end, insert new free suballocation after current one.\r
- if(paddingEnd)\r
- {\r
- VmaSuballocation paddingSuballoc = {};\r
- paddingSuballoc.offset = request.offset + allocSize;\r
- paddingSuballoc.size = paddingEnd;\r
- paddingSuballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- VmaSuballocationList::iterator next = request.item;\r
- ++next;\r
- const VmaSuballocationList::iterator paddingEndItem =\r
- m_Suballocations.insert(next, paddingSuballoc);\r
- RegisterFreeSuballocation(paddingEndItem);\r
- }\r
-\r
- // If there are any free bytes remaining at the beginning, insert new free suballocation before current one.\r
- if(paddingBegin)\r
- {\r
- VmaSuballocation paddingSuballoc = {};\r
- paddingSuballoc.offset = request.offset - paddingBegin;\r
- paddingSuballoc.size = paddingBegin;\r
- paddingSuballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- const VmaSuballocationList::iterator paddingBeginItem =\r
- m_Suballocations.insert(request.item, paddingSuballoc);\r
- RegisterFreeSuballocation(paddingBeginItem);\r
- }\r
-\r
- // Update totals.\r
- m_FreeCount = m_FreeCount - 1;\r
- if(paddingBegin > 0)\r
- {\r
- ++m_FreeCount;\r
- }\r
- if(paddingEnd > 0)\r
- {\r
- ++m_FreeCount;\r
- }\r
- m_SumFreeSize -= allocSize;\r
-}\r
-\r
-void VmaBlockMetadata_Generic::Free(const VmaAllocation allocation)\r
-{\r
- for(VmaSuballocationList::iterator suballocItem = m_Suballocations.begin();\r
- suballocItem != m_Suballocations.end();\r
- ++suballocItem)\r
- {\r
- VmaSuballocation& suballoc = *suballocItem;\r
- if(suballoc.hAllocation == allocation)\r
- {\r
- FreeSuballocation(suballocItem);\r
- VMA_HEAVY_ASSERT(Validate());\r
- return;\r
- }\r
- }\r
- VMA_ASSERT(0 && "Not found!");\r
-}\r
-\r
-void VmaBlockMetadata_Generic::FreeAtOffset(VkDeviceSize offset)\r
-{\r
- for(VmaSuballocationList::iterator suballocItem = m_Suballocations.begin();\r
- suballocItem != m_Suballocations.end();\r
- ++suballocItem)\r
- {\r
- VmaSuballocation& suballoc = *suballocItem;\r
- if(suballoc.offset == offset)\r
- {\r
- FreeSuballocation(suballocItem);\r
- return;\r
- }\r
- }\r
- VMA_ASSERT(0 && "Not found!");\r
-}\r
-\r
-bool VmaBlockMetadata_Generic::ValidateFreeSuballocationList() const\r
-{\r
- VkDeviceSize lastSize = 0;\r
- for(size_t i = 0, count = m_FreeSuballocationsBySize.size(); i < count; ++i)\r
- {\r
- const VmaSuballocationList::iterator it = m_FreeSuballocationsBySize[i];\r
-\r
- VMA_VALIDATE(it->type == VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_VALIDATE(it->size >= VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER);\r
- VMA_VALIDATE(it->size >= lastSize);\r
- lastSize = it->size;\r
- }\r
- return true;\r
-}\r
-\r
-bool VmaBlockMetadata_Generic::CheckAllocation(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- VmaSuballocationType allocType,\r
- VmaSuballocationList::const_iterator suballocItem,\r
- bool canMakeOtherLost,\r
- VkDeviceSize* pOffset,\r
- size_t* itemsToMakeLostCount,\r
- VkDeviceSize* pSumFreeSize,\r
- VkDeviceSize* pSumItemSize) const\r
-{\r
- VMA_ASSERT(allocSize > 0);\r
- VMA_ASSERT(allocType != VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_ASSERT(suballocItem != m_Suballocations.cend());\r
- VMA_ASSERT(pOffset != VMA_NULL);\r
- \r
- *itemsToMakeLostCount = 0;\r
- *pSumFreeSize = 0;\r
- *pSumItemSize = 0;\r
-\r
- if(canMakeOtherLost)\r
- {\r
- if(suballocItem->type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- *pSumFreeSize = suballocItem->size;\r
- }\r
- else\r
- {\r
- if(suballocItem->hAllocation->CanBecomeLost() &&\r
- suballocItem->hAllocation->GetLastUseFrameIndex() + frameInUseCount < currentFrameIndex)\r
- {\r
- ++*itemsToMakeLostCount;\r
- *pSumItemSize = suballocItem->size;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
-\r
- // Remaining size is too small for this request: Early return.\r
- if(GetSize() - suballocItem->offset < allocSize)\r
- {\r
- return false;\r
- }\r
-\r
- // Start from offset equal to beginning of this suballocation.\r
- *pOffset = suballocItem->offset;\r
- \r
- // Apply VMA_DEBUG_MARGIN at the beginning.\r
- if(VMA_DEBUG_MARGIN > 0)\r
- {\r
- *pOffset += VMA_DEBUG_MARGIN;\r
- }\r
- \r
- // Apply alignment.\r
- *pOffset = VmaAlignUp(*pOffset, allocAlignment);\r
-\r
- // Check previous suballocations for BufferImageGranularity conflicts.\r
- // Make bigger alignment if necessary.\r
- if(bufferImageGranularity > 1)\r
- {\r
- bool bufferImageGranularityConflict = false;\r
- VmaSuballocationList::const_iterator prevSuballocItem = suballocItem;\r
- while(prevSuballocItem != m_Suballocations.cbegin())\r
- {\r
- --prevSuballocItem;\r
- const VmaSuballocation& prevSuballoc = *prevSuballocItem;\r
- if(VmaBlocksOnSamePage(prevSuballoc.offset, prevSuballoc.size, *pOffset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(prevSuballoc.type, allocType))\r
- {\r
- bufferImageGranularityConflict = true;\r
- break;\r
- }\r
- }\r
- else\r
- // Already on previous page.\r
- break;\r
- }\r
- if(bufferImageGranularityConflict)\r
- {\r
- *pOffset = VmaAlignUp(*pOffset, bufferImageGranularity);\r
- }\r
- }\r
- \r
- // Now that we have final *pOffset, check if we are past suballocItem.\r
- // If yes, return false - this function should be called for another suballocItem as starting point.\r
- if(*pOffset >= suballocItem->offset + suballocItem->size)\r
- {\r
- return false;\r
- }\r
- \r
- // Calculate padding at the beginning based on current offset.\r
- const VkDeviceSize paddingBegin = *pOffset - suballocItem->offset;\r
-\r
- // Calculate required margin at the end.\r
- const VkDeviceSize requiredEndMargin = VMA_DEBUG_MARGIN;\r
-\r
- const VkDeviceSize totalSize = paddingBegin + allocSize + requiredEndMargin;\r
- // Another early return check.\r
- if(suballocItem->offset + totalSize > GetSize())\r
- {\r
- return false;\r
- }\r
-\r
- // Advance lastSuballocItem until desired size is reached.\r
- // Update itemsToMakeLostCount.\r
- VmaSuballocationList::const_iterator lastSuballocItem = suballocItem;\r
- if(totalSize > suballocItem->size)\r
- {\r
- VkDeviceSize remainingSize = totalSize - suballocItem->size;\r
- while(remainingSize > 0)\r
- {\r
- ++lastSuballocItem;\r
- if(lastSuballocItem == m_Suballocations.cend())\r
- {\r
- return false;\r
- }\r
- if(lastSuballocItem->type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- *pSumFreeSize += lastSuballocItem->size;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(lastSuballocItem->hAllocation != VK_NULL_HANDLE);\r
- if(lastSuballocItem->hAllocation->CanBecomeLost() &&\r
- lastSuballocItem->hAllocation->GetLastUseFrameIndex() + frameInUseCount < currentFrameIndex)\r
- {\r
- ++*itemsToMakeLostCount;\r
- *pSumItemSize += lastSuballocItem->size;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
- remainingSize = (lastSuballocItem->size < remainingSize) ?\r
- remainingSize - lastSuballocItem->size : 0;\r
- }\r
- }\r
-\r
- // Check next suballocations for BufferImageGranularity conflicts.\r
- // If conflict exists, we must mark more allocations lost or fail.\r
- if(bufferImageGranularity > 1)\r
- {\r
- VmaSuballocationList::const_iterator nextSuballocItem = lastSuballocItem;\r
- ++nextSuballocItem;\r
- while(nextSuballocItem != m_Suballocations.cend())\r
- {\r
- const VmaSuballocation& nextSuballoc = *nextSuballocItem;\r
- if(VmaBlocksOnSamePage(*pOffset, allocSize, nextSuballoc.offset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(allocType, nextSuballoc.type))\r
- {\r
- VMA_ASSERT(nextSuballoc.hAllocation != VK_NULL_HANDLE);\r
- if(nextSuballoc.hAllocation->CanBecomeLost() &&\r
- nextSuballoc.hAllocation->GetLastUseFrameIndex() + frameInUseCount < currentFrameIndex)\r
- {\r
- ++*itemsToMakeLostCount;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
- }\r
- else\r
- {\r
- // Already on next page.\r
- break;\r
- }\r
- ++nextSuballocItem;\r
- }\r
- }\r
- }\r
- else\r
- {\r
- const VmaSuballocation& suballoc = *suballocItem;\r
- VMA_ASSERT(suballoc.type == VMA_SUBALLOCATION_TYPE_FREE);\r
-\r
- *pSumFreeSize = suballoc.size;\r
-\r
- // Size of this suballocation is too small for this request: Early return.\r
- if(suballoc.size < allocSize)\r
- {\r
- return false;\r
- }\r
-\r
- // Start from offset equal to beginning of this suballocation.\r
- *pOffset = suballoc.offset;\r
- \r
- // Apply VMA_DEBUG_MARGIN at the beginning.\r
- if(VMA_DEBUG_MARGIN > 0)\r
- {\r
- *pOffset += VMA_DEBUG_MARGIN;\r
- }\r
- \r
- // Apply alignment.\r
- *pOffset = VmaAlignUp(*pOffset, allocAlignment);\r
- \r
- // Check previous suballocations for BufferImageGranularity conflicts.\r
- // Make bigger alignment if necessary.\r
- if(bufferImageGranularity > 1)\r
- {\r
- bool bufferImageGranularityConflict = false;\r
- VmaSuballocationList::const_iterator prevSuballocItem = suballocItem;\r
- while(prevSuballocItem != m_Suballocations.cbegin())\r
- {\r
- --prevSuballocItem;\r
- const VmaSuballocation& prevSuballoc = *prevSuballocItem;\r
- if(VmaBlocksOnSamePage(prevSuballoc.offset, prevSuballoc.size, *pOffset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(prevSuballoc.type, allocType))\r
- {\r
- bufferImageGranularityConflict = true;\r
- break;\r
- }\r
- }\r
- else\r
- // Already on previous page.\r
- break;\r
- }\r
- if(bufferImageGranularityConflict)\r
- {\r
- *pOffset = VmaAlignUp(*pOffset, bufferImageGranularity);\r
- }\r
- }\r
- \r
- // Calculate padding at the beginning based on current offset.\r
- const VkDeviceSize paddingBegin = *pOffset - suballoc.offset;\r
-\r
- // Calculate required margin at the end.\r
- const VkDeviceSize requiredEndMargin = VMA_DEBUG_MARGIN;\r
-\r
- // Fail if requested size plus margin before and after is bigger than size of this suballocation.\r
- if(paddingBegin + allocSize + requiredEndMargin > suballoc.size)\r
- {\r
- return false;\r
- }\r
-\r
- // Check next suballocations for BufferImageGranularity conflicts.\r
- // If conflict exists, allocation cannot be made here.\r
- if(bufferImageGranularity > 1)\r
- {\r
- VmaSuballocationList::const_iterator nextSuballocItem = suballocItem;\r
- ++nextSuballocItem;\r
- while(nextSuballocItem != m_Suballocations.cend())\r
- {\r
- const VmaSuballocation& nextSuballoc = *nextSuballocItem;\r
- if(VmaBlocksOnSamePage(*pOffset, allocSize, nextSuballoc.offset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(allocType, nextSuballoc.type))\r
- {\r
- return false;\r
- }\r
- }\r
- else\r
- {\r
- // Already on next page.\r
- break;\r
- }\r
- ++nextSuballocItem;\r
- }\r
- }\r
- }\r
-\r
- // All tests passed: Success. pOffset is already filled.\r
- return true;\r
-}\r
-\r
-void VmaBlockMetadata_Generic::MergeFreeWithNext(VmaSuballocationList::iterator item)\r
-{\r
- VMA_ASSERT(item != m_Suballocations.end());\r
- VMA_ASSERT(item->type == VMA_SUBALLOCATION_TYPE_FREE);\r
- \r
- VmaSuballocationList::iterator nextItem = item;\r
- ++nextItem;\r
- VMA_ASSERT(nextItem != m_Suballocations.end());\r
- VMA_ASSERT(nextItem->type == VMA_SUBALLOCATION_TYPE_FREE);\r
-\r
- item->size += nextItem->size;\r
- --m_FreeCount;\r
- m_Suballocations.erase(nextItem);\r
-}\r
-\r
-VmaSuballocationList::iterator VmaBlockMetadata_Generic::FreeSuballocation(VmaSuballocationList::iterator suballocItem)\r
-{\r
- // Change this suballocation to be marked as free.\r
- VmaSuballocation& suballoc = *suballocItem;\r
- suballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- suballoc.hAllocation = VK_NULL_HANDLE;\r
- \r
- // Update totals.\r
- ++m_FreeCount;\r
- m_SumFreeSize += suballoc.size;\r
-\r
- // Merge with previous and/or next suballocation if it's also free.\r
- bool mergeWithNext = false;\r
- bool mergeWithPrev = false;\r
- \r
- VmaSuballocationList::iterator nextItem = suballocItem;\r
- ++nextItem;\r
- if((nextItem != m_Suballocations.end()) && (nextItem->type == VMA_SUBALLOCATION_TYPE_FREE))\r
- {\r
- mergeWithNext = true;\r
- }\r
-\r
- VmaSuballocationList::iterator prevItem = suballocItem;\r
- if(suballocItem != m_Suballocations.begin())\r
- {\r
- --prevItem;\r
- if(prevItem->type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- mergeWithPrev = true;\r
- }\r
- }\r
-\r
- if(mergeWithNext)\r
- {\r
- UnregisterFreeSuballocation(nextItem);\r
- MergeFreeWithNext(suballocItem);\r
- }\r
-\r
- if(mergeWithPrev)\r
- {\r
- UnregisterFreeSuballocation(prevItem);\r
- MergeFreeWithNext(prevItem);\r
- RegisterFreeSuballocation(prevItem);\r
- return prevItem;\r
- }\r
- else\r
- {\r
- RegisterFreeSuballocation(suballocItem);\r
- return suballocItem;\r
- }\r
-}\r
-\r
-void VmaBlockMetadata_Generic::RegisterFreeSuballocation(VmaSuballocationList::iterator item)\r
-{\r
- VMA_ASSERT(item->type == VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_ASSERT(item->size > 0);\r
-\r
- // You may want to enable this validation at the beginning or at the end of\r
- // this function, depending on what do you want to check.\r
- VMA_HEAVY_ASSERT(ValidateFreeSuballocationList());\r
-\r
- if(item->size >= VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- if(m_FreeSuballocationsBySize.empty())\r
- {\r
- m_FreeSuballocationsBySize.push_back(item);\r
- }\r
- else\r
- {\r
- VmaVectorInsertSorted<VmaSuballocationItemSizeLess>(m_FreeSuballocationsBySize, item);\r
- }\r
- }\r
-\r
- //VMA_HEAVY_ASSERT(ValidateFreeSuballocationList());\r
-}\r
-\r
-\r
-void VmaBlockMetadata_Generic::UnregisterFreeSuballocation(VmaSuballocationList::iterator item)\r
-{\r
- VMA_ASSERT(item->type == VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_ASSERT(item->size > 0);\r
-\r
- // You may want to enable this validation at the beginning or at the end of\r
- // this function, depending on what do you want to check.\r
- VMA_HEAVY_ASSERT(ValidateFreeSuballocationList());\r
-\r
- if(item->size >= VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- VmaSuballocationList::iterator* const it = VmaBinaryFindFirstNotLess(\r
- m_FreeSuballocationsBySize.data(),\r
- m_FreeSuballocationsBySize.data() + m_FreeSuballocationsBySize.size(),\r
- item,\r
- VmaSuballocationItemSizeLess());\r
- for(size_t index = it - m_FreeSuballocationsBySize.data();\r
- index < m_FreeSuballocationsBySize.size();\r
- ++index)\r
- {\r
- if(m_FreeSuballocationsBySize[index] == item)\r
- {\r
- VmaVectorRemove(m_FreeSuballocationsBySize, index);\r
- return;\r
- }\r
- VMA_ASSERT((m_FreeSuballocationsBySize[index]->size == item->size) && "Not found.");\r
- }\r
- VMA_ASSERT(0 && "Not found.");\r
- }\r
-\r
- //VMA_HEAVY_ASSERT(ValidateFreeSuballocationList());\r
-}\r
-\r
-bool VmaBlockMetadata_Generic::IsBufferImageGranularityConflictPossible(\r
- VkDeviceSize bufferImageGranularity,\r
- VmaSuballocationType& inOutPrevSuballocType) const\r
-{\r
- if(bufferImageGranularity == 1 || IsEmpty())\r
- {\r
- return false;\r
- }\r
-\r
- VkDeviceSize minAlignment = VK_WHOLE_SIZE;\r
- bool typeConflictFound = false;\r
- for(VmaSuballocationList::const_iterator it = m_Suballocations.cbegin();\r
- it != m_Suballocations.cend();\r
- ++it)\r
- {\r
- const VmaSuballocationType suballocType = it->type;\r
- if(suballocType != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- minAlignment = VMA_MIN(minAlignment, it->hAllocation->GetAlignment());\r
- if(VmaIsBufferImageGranularityConflict(inOutPrevSuballocType, suballocType))\r
- {\r
- typeConflictFound = true;\r
- }\r
- inOutPrevSuballocType = suballocType;\r
- }\r
- }\r
-\r
- return typeConflictFound || minAlignment >= bufferImageGranularity;\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaBlockMetadata_Linear\r
-\r
-VmaBlockMetadata_Linear::VmaBlockMetadata_Linear(VmaAllocator hAllocator) :\r
- VmaBlockMetadata(hAllocator),\r
- m_SumFreeSize(0),\r
- m_Suballocations0(VmaStlAllocator<VmaSuballocation>(hAllocator->GetAllocationCallbacks())),\r
- m_Suballocations1(VmaStlAllocator<VmaSuballocation>(hAllocator->GetAllocationCallbacks())),\r
- m_1stVectorIndex(0),\r
- m_2ndVectorMode(SECOND_VECTOR_EMPTY),\r
- m_1stNullItemsBeginCount(0),\r
- m_1stNullItemsMiddleCount(0),\r
- m_2ndNullItemsCount(0)\r
-{\r
-}\r
-\r
-VmaBlockMetadata_Linear::~VmaBlockMetadata_Linear()\r
-{\r
-}\r
-\r
-void VmaBlockMetadata_Linear::Init(VkDeviceSize size)\r
-{\r
- VmaBlockMetadata::Init(size);\r
- m_SumFreeSize = size;\r
-}\r
-\r
-bool VmaBlockMetadata_Linear::Validate() const\r
-{\r
- const SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- const SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
-\r
- VMA_VALIDATE(suballocations2nd.empty() == (m_2ndVectorMode == SECOND_VECTOR_EMPTY));\r
- VMA_VALIDATE(!suballocations1st.empty() ||\r
- suballocations2nd.empty() ||\r
- m_2ndVectorMode != SECOND_VECTOR_RING_BUFFER);\r
-\r
- if(!suballocations1st.empty())\r
- {\r
- // Null item at the beginning should be accounted into m_1stNullItemsBeginCount.\r
- VMA_VALIDATE(suballocations1st[m_1stNullItemsBeginCount].hAllocation != VK_NULL_HANDLE);\r
- // Null item at the end should be just pop_back().\r
- VMA_VALIDATE(suballocations1st.back().hAllocation != VK_NULL_HANDLE);\r
- }\r
- if(!suballocations2nd.empty())\r
- {\r
- // Null item at the end should be just pop_back().\r
- VMA_VALIDATE(suballocations2nd.back().hAllocation != VK_NULL_HANDLE);\r
- }\r
-\r
- VMA_VALIDATE(m_1stNullItemsBeginCount + m_1stNullItemsMiddleCount <= suballocations1st.size());\r
- VMA_VALIDATE(m_2ndNullItemsCount <= suballocations2nd.size());\r
-\r
- VkDeviceSize sumUsedSize = 0;\r
- const size_t suballoc1stCount = suballocations1st.size();\r
- VkDeviceSize offset = VMA_DEBUG_MARGIN;\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- const size_t suballoc2ndCount = suballocations2nd.size();\r
- size_t nullItem2ndCount = 0;\r
- for(size_t i = 0; i < suballoc2ndCount; ++i)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[i];\r
- const bool currFree = (suballoc.type == VMA_SUBALLOCATION_TYPE_FREE);\r
-\r
- VMA_VALIDATE(currFree == (suballoc.hAllocation == VK_NULL_HANDLE));\r
- VMA_VALIDATE(suballoc.offset >= offset);\r
-\r
- if(!currFree)\r
- {\r
- VMA_VALIDATE(suballoc.hAllocation->GetOffset() == suballoc.offset);\r
- VMA_VALIDATE(suballoc.hAllocation->GetSize() == suballoc.size);\r
- sumUsedSize += suballoc.size;\r
- }\r
- else\r
- {\r
- ++nullItem2ndCount;\r
- }\r
-\r
- offset = suballoc.offset + suballoc.size + VMA_DEBUG_MARGIN;\r
- }\r
-\r
- VMA_VALIDATE(nullItem2ndCount == m_2ndNullItemsCount);\r
- }\r
-\r
- for(size_t i = 0; i < m_1stNullItemsBeginCount; ++i)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[i];\r
- VMA_VALIDATE(suballoc.type == VMA_SUBALLOCATION_TYPE_FREE &&\r
- suballoc.hAllocation == VK_NULL_HANDLE);\r
- }\r
-\r
- size_t nullItem1stCount = m_1stNullItemsBeginCount;\r
-\r
- for(size_t i = m_1stNullItemsBeginCount; i < suballoc1stCount; ++i)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[i];\r
- const bool currFree = (suballoc.type == VMA_SUBALLOCATION_TYPE_FREE);\r
-\r
- VMA_VALIDATE(currFree == (suballoc.hAllocation == VK_NULL_HANDLE));\r
- VMA_VALIDATE(suballoc.offset >= offset);\r
- VMA_VALIDATE(i >= m_1stNullItemsBeginCount || currFree);\r
-\r
- if(!currFree)\r
- {\r
- VMA_VALIDATE(suballoc.hAllocation->GetOffset() == suballoc.offset);\r
- VMA_VALIDATE(suballoc.hAllocation->GetSize() == suballoc.size);\r
- sumUsedSize += suballoc.size;\r
- }\r
- else\r
- {\r
- ++nullItem1stCount;\r
- }\r
-\r
- offset = suballoc.offset + suballoc.size + VMA_DEBUG_MARGIN;\r
- }\r
- VMA_VALIDATE(nullItem1stCount == m_1stNullItemsBeginCount + m_1stNullItemsMiddleCount);\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- const size_t suballoc2ndCount = suballocations2nd.size();\r
- size_t nullItem2ndCount = 0;\r
- for(size_t i = suballoc2ndCount; i--; )\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[i];\r
- const bool currFree = (suballoc.type == VMA_SUBALLOCATION_TYPE_FREE);\r
-\r
- VMA_VALIDATE(currFree == (suballoc.hAllocation == VK_NULL_HANDLE));\r
- VMA_VALIDATE(suballoc.offset >= offset);\r
-\r
- if(!currFree)\r
- {\r
- VMA_VALIDATE(suballoc.hAllocation->GetOffset() == suballoc.offset);\r
- VMA_VALIDATE(suballoc.hAllocation->GetSize() == suballoc.size);\r
- sumUsedSize += suballoc.size;\r
- }\r
- else\r
- {\r
- ++nullItem2ndCount;\r
- }\r
-\r
- offset = suballoc.offset + suballoc.size + VMA_DEBUG_MARGIN;\r
- }\r
-\r
- VMA_VALIDATE(nullItem2ndCount == m_2ndNullItemsCount);\r
- }\r
-\r
- VMA_VALIDATE(offset <= GetSize());\r
- VMA_VALIDATE(m_SumFreeSize == GetSize() - sumUsedSize);\r
-\r
- return true;\r
-}\r
-\r
-size_t VmaBlockMetadata_Linear::GetAllocationCount() const\r
-{\r
- return AccessSuballocations1st().size() - (m_1stNullItemsBeginCount + m_1stNullItemsMiddleCount) +\r
- AccessSuballocations2nd().size() - m_2ndNullItemsCount;\r
-}\r
-\r
-VkDeviceSize VmaBlockMetadata_Linear::GetUnusedRangeSizeMax() const\r
-{\r
- const VkDeviceSize size = GetSize();\r
-\r
- /*\r
- We don't consider gaps inside allocation vectors with freed allocations because\r
- they are not suitable for reuse in linear allocator. We consider only space that\r
- is available for new allocations.\r
- */\r
- if(IsEmpty())\r
- {\r
- return size;\r
- }\r
- \r
- const SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
-\r
- switch(m_2ndVectorMode)\r
- {\r
- case SECOND_VECTOR_EMPTY:\r
- /*\r
- Available space is after end of 1st, as well as before beginning of 1st (which\r
- whould make it a ring buffer).\r
- */\r
- {\r
- const size_t suballocations1stCount = suballocations1st.size();\r
- VMA_ASSERT(suballocations1stCount > m_1stNullItemsBeginCount);\r
- const VmaSuballocation& firstSuballoc = suballocations1st[m_1stNullItemsBeginCount];\r
- const VmaSuballocation& lastSuballoc = suballocations1st[suballocations1stCount - 1];\r
- return VMA_MAX(\r
- firstSuballoc.offset,\r
- size - (lastSuballoc.offset + lastSuballoc.size));\r
- }\r
- break;\r
-\r
- case SECOND_VECTOR_RING_BUFFER:\r
- /*\r
- Available space is only between end of 2nd and beginning of 1st.\r
- */\r
- {\r
- const SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- const VmaSuballocation& lastSuballoc2nd = suballocations2nd.back();\r
- const VmaSuballocation& firstSuballoc1st = suballocations1st[m_1stNullItemsBeginCount];\r
- return firstSuballoc1st.offset - (lastSuballoc2nd.offset + lastSuballoc2nd.size);\r
- }\r
- break;\r
-\r
- case SECOND_VECTOR_DOUBLE_STACK:\r
- /*\r
- Available space is only between end of 1st and top of 2nd.\r
- */\r
- {\r
- const SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- const VmaSuballocation& topSuballoc2nd = suballocations2nd.back();\r
- const VmaSuballocation& lastSuballoc1st = suballocations1st.back();\r
- return topSuballoc2nd.offset - (lastSuballoc1st.offset + lastSuballoc1st.size);\r
- }\r
- break;\r
-\r
- default:\r
- VMA_ASSERT(0);\r
- return 0;\r
- }\r
-}\r
-\r
-void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const\r
-{\r
- const VkDeviceSize size = GetSize();\r
- const SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- const SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- const size_t suballoc1stCount = suballocations1st.size();\r
- const size_t suballoc2ndCount = suballocations2nd.size();\r
-\r
- outInfo.blockCount = 1;\r
- outInfo.allocationCount = (uint32_t)GetAllocationCount();\r
- outInfo.unusedRangeCount = 0;\r
- outInfo.usedBytes = 0;\r
- outInfo.allocationSizeMin = UINT64_MAX;\r
- outInfo.allocationSizeMax = 0;\r
- outInfo.unusedRangeSizeMin = UINT64_MAX;\r
- outInfo.unusedRangeSizeMax = 0;\r
-\r
- VkDeviceSize lastOffset = 0;\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- const VkDeviceSize freeSpace2ndTo1stEnd = suballocations1st[m_1stNullItemsBeginCount].offset;\r
- size_t nextAlloc2ndIndex = 0;\r
- while(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // Find next non-null allocation or move nextAllocIndex to the end.\r
- while(nextAlloc2ndIndex < suballoc2ndCount &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex < suballoc2ndCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- outInfo.usedBytes += suballoc.size;\r
- outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size);\r
- outInfo.allocationSizeMax = VMA_MIN(outInfo.allocationSizeMax, suballoc.size);\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- // There is free space from lastOffset to freeSpace2ndTo1stEnd.\r
- if(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- const VkDeviceSize unusedRangeSize = freeSpace2ndTo1stEnd - lastOffset;\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace2ndTo1stEnd;\r
- }\r
- }\r
- }\r
-\r
- size_t nextAlloc1stIndex = m_1stNullItemsBeginCount;\r
- const VkDeviceSize freeSpace1stTo2ndEnd =\r
- m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK ? suballocations2nd.back().offset : size;\r
- while(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- // Find next non-null allocation or move nextAllocIndex to the end.\r
- while(nextAlloc1stIndex < suballoc1stCount &&\r
- suballocations1st[nextAlloc1stIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc1stIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc1stIndex < suballoc1stCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- outInfo.usedBytes += suballoc.size;\r
- outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size);\r
- outInfo.allocationSizeMax = VMA_MIN(outInfo.allocationSizeMax, suballoc.size);\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc1stIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- // There is free space from lastOffset to freeSpace1stTo2ndEnd.\r
- if(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- const VkDeviceSize unusedRangeSize = freeSpace1stTo2ndEnd - lastOffset;\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace1stTo2ndEnd;\r
- }\r
- }\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- size_t nextAlloc2ndIndex = suballocations2nd.size() - 1;\r
- while(lastOffset < size)\r
- {\r
- // Find next non-null allocation or move nextAllocIndex to the end.\r
- while(nextAlloc2ndIndex != SIZE_MAX &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- --nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex != SIZE_MAX)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- outInfo.usedBytes += suballoc.size;\r
- outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size);\r
- outInfo.allocationSizeMax = VMA_MIN(outInfo.allocationSizeMax, suballoc.size);\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- --nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- // There is free space from lastOffset to size.\r
- if(lastOffset < size)\r
- {\r
- const VkDeviceSize unusedRangeSize = size - lastOffset;\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = size;\r
- }\r
- }\r
- }\r
-\r
- outInfo.unusedBytes = size - outInfo.usedBytes;\r
-}\r
-\r
-void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const\r
-{\r
- const SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- const SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- const VkDeviceSize size = GetSize();\r
- const size_t suballoc1stCount = suballocations1st.size();\r
- const size_t suballoc2ndCount = suballocations2nd.size();\r
-\r
- inoutStats.size += size;\r
-\r
- VkDeviceSize lastOffset = 0;\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- const VkDeviceSize freeSpace2ndTo1stEnd = suballocations1st[m_1stNullItemsBeginCount].offset;\r
- size_t nextAlloc2ndIndex = m_1stNullItemsBeginCount;\r
- while(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // Find next non-null allocation or move nextAlloc2ndIndex to the end.\r
- while(nextAlloc2ndIndex < suballoc2ndCount &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex < suballoc2ndCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- inoutStats.unusedSize += unusedRangeSize;\r
- ++inoutStats.unusedRangeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- ++inoutStats.allocationCount;\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // There is free space from lastOffset to freeSpace2ndTo1stEnd.\r
- const VkDeviceSize unusedRangeSize = freeSpace2ndTo1stEnd - lastOffset;\r
- inoutStats.unusedSize += unusedRangeSize;\r
- ++inoutStats.unusedRangeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace2ndTo1stEnd;\r
- }\r
- }\r
- }\r
-\r
- size_t nextAlloc1stIndex = m_1stNullItemsBeginCount;\r
- const VkDeviceSize freeSpace1stTo2ndEnd =\r
- m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK ? suballocations2nd.back().offset : size;\r
- while(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- // Find next non-null allocation or move nextAllocIndex to the end.\r
- while(nextAlloc1stIndex < suballoc1stCount &&\r
- suballocations1st[nextAlloc1stIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc1stIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc1stIndex < suballoc1stCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- inoutStats.unusedSize += unusedRangeSize;\r
- ++inoutStats.unusedRangeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- ++inoutStats.allocationCount;\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc1stIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- // There is free space from lastOffset to freeSpace1stTo2ndEnd.\r
- const VkDeviceSize unusedRangeSize = freeSpace1stTo2ndEnd - lastOffset;\r
- inoutStats.unusedSize += unusedRangeSize;\r
- ++inoutStats.unusedRangeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace1stTo2ndEnd;\r
- }\r
- }\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- size_t nextAlloc2ndIndex = suballocations2nd.size() - 1;\r
- while(lastOffset < size)\r
- {\r
- // Find next non-null allocation or move nextAlloc2ndIndex to the end.\r
- while(nextAlloc2ndIndex != SIZE_MAX &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- --nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex != SIZE_MAX)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- inoutStats.unusedSize += unusedRangeSize;\r
- ++inoutStats.unusedRangeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- ++inoutStats.allocationCount;\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- --nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < size)\r
- {\r
- // There is free space from lastOffset to size.\r
- const VkDeviceSize unusedRangeSize = size - lastOffset;\r
- inoutStats.unusedSize += unusedRangeSize;\r
- ++inoutStats.unusedRangeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = size;\r
- }\r
- }\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const\r
-{\r
- const VkDeviceSize size = GetSize();\r
- const SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- const SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- const size_t suballoc1stCount = suballocations1st.size();\r
- const size_t suballoc2ndCount = suballocations2nd.size();\r
-\r
- // FIRST PASS\r
-\r
- size_t unusedRangeCount = 0;\r
- VkDeviceSize usedBytes = 0;\r
-\r
- VkDeviceSize lastOffset = 0;\r
-\r
- size_t alloc2ndCount = 0;\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- const VkDeviceSize freeSpace2ndTo1stEnd = suballocations1st[m_1stNullItemsBeginCount].offset;\r
- size_t nextAlloc2ndIndex = 0;\r
- while(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // Find next non-null allocation or move nextAlloc2ndIndex to the end.\r
- while(nextAlloc2ndIndex < suballoc2ndCount &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex < suballoc2ndCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- ++unusedRangeCount;\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- ++alloc2ndCount;\r
- usedBytes += suballoc.size;\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // There is free space from lastOffset to freeSpace2ndTo1stEnd.\r
- ++unusedRangeCount;\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace2ndTo1stEnd;\r
- }\r
- }\r
- }\r
-\r
- size_t nextAlloc1stIndex = m_1stNullItemsBeginCount;\r
- size_t alloc1stCount = 0;\r
- const VkDeviceSize freeSpace1stTo2ndEnd =\r
- m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK ? suballocations2nd.back().offset : size;\r
- while(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- // Find next non-null allocation or move nextAllocIndex to the end.\r
- while(nextAlloc1stIndex < suballoc1stCount &&\r
- suballocations1st[nextAlloc1stIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc1stIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc1stIndex < suballoc1stCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- ++unusedRangeCount;\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- ++alloc1stCount;\r
- usedBytes += suballoc.size;\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc1stIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < size)\r
- {\r
- // There is free space from lastOffset to freeSpace1stTo2ndEnd.\r
- ++unusedRangeCount;\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace1stTo2ndEnd;\r
- }\r
- }\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- size_t nextAlloc2ndIndex = suballocations2nd.size() - 1;\r
- while(lastOffset < size)\r
- {\r
- // Find next non-null allocation or move nextAlloc2ndIndex to the end.\r
- while(nextAlloc2ndIndex != SIZE_MAX &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- --nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex != SIZE_MAX)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- ++unusedRangeCount;\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- ++alloc2ndCount;\r
- usedBytes += suballoc.size;\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- --nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < size)\r
- {\r
- // There is free space from lastOffset to size.\r
- ++unusedRangeCount;\r
- }\r
-\r
- // End of loop.\r
- lastOffset = size;\r
- }\r
- }\r
- }\r
-\r
- const VkDeviceSize unusedBytes = size - usedBytes;\r
- PrintDetailedMap_Begin(json, unusedBytes, alloc1stCount + alloc2ndCount, unusedRangeCount);\r
-\r
- // SECOND PASS\r
- lastOffset = 0;\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- const VkDeviceSize freeSpace2ndTo1stEnd = suballocations1st[m_1stNullItemsBeginCount].offset;\r
- size_t nextAlloc2ndIndex = 0;\r
- while(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // Find next non-null allocation or move nextAlloc2ndIndex to the end.\r
- while(nextAlloc2ndIndex < suballoc2ndCount &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex < suballoc2ndCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- PrintDetailedMap_Allocation(json, suballoc.offset, suballoc.hAllocation);\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < freeSpace2ndTo1stEnd)\r
- {\r
- // There is free space from lastOffset to freeSpace2ndTo1stEnd.\r
- const VkDeviceSize unusedRangeSize = freeSpace2ndTo1stEnd - lastOffset;\r
- PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace2ndTo1stEnd;\r
- }\r
- }\r
- }\r
-\r
- nextAlloc1stIndex = m_1stNullItemsBeginCount;\r
- while(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- // Find next non-null allocation or move nextAllocIndex to the end.\r
- while(nextAlloc1stIndex < suballoc1stCount &&\r
- suballocations1st[nextAlloc1stIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++nextAlloc1stIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc1stIndex < suballoc1stCount)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- PrintDetailedMap_Allocation(json, suballoc.offset, suballoc.hAllocation);\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- ++nextAlloc1stIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < freeSpace1stTo2ndEnd)\r
- {\r
- // There is free space from lastOffset to freeSpace1stTo2ndEnd.\r
- const VkDeviceSize unusedRangeSize = freeSpace1stTo2ndEnd - lastOffset;\r
- PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = freeSpace1stTo2ndEnd;\r
- }\r
- }\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- size_t nextAlloc2ndIndex = suballocations2nd.size() - 1;\r
- while(lastOffset < size)\r
- {\r
- // Find next non-null allocation or move nextAlloc2ndIndex to the end.\r
- while(nextAlloc2ndIndex != SIZE_MAX &&\r
- suballocations2nd[nextAlloc2ndIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- --nextAlloc2ndIndex;\r
- }\r
-\r
- // Found non-null allocation.\r
- if(nextAlloc2ndIndex != SIZE_MAX)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex];\r
- \r
- // 1. Process free space before this allocation.\r
- if(lastOffset < suballoc.offset)\r
- {\r
- // There is free space from lastOffset to suballoc.offset.\r
- const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset;\r
- PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize);\r
- }\r
- \r
- // 2. Process this allocation.\r
- // There is allocation with suballoc.offset, suballoc.size.\r
- PrintDetailedMap_Allocation(json, suballoc.offset, suballoc.hAllocation);\r
- \r
- // 3. Prepare for next iteration.\r
- lastOffset = suballoc.offset + suballoc.size;\r
- --nextAlloc2ndIndex;\r
- }\r
- // We are at the end.\r
- else\r
- {\r
- if(lastOffset < size)\r
- {\r
- // There is free space from lastOffset to size.\r
- const VkDeviceSize unusedRangeSize = size - lastOffset;\r
- PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize);\r
- }\r
-\r
- // End of loop.\r
- lastOffset = size;\r
- }\r
- }\r
- }\r
-\r
- PrintDetailedMap_End(json);\r
-}\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-bool VmaBlockMetadata_Linear::CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- VMA_ASSERT(allocSize > 0);\r
- VMA_ASSERT(allocType != VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_ASSERT(pAllocationRequest != VMA_NULL);\r
- VMA_HEAVY_ASSERT(Validate());\r
- return upperAddress ?\r
- CreateAllocationRequest_UpperAddress(\r
- currentFrameIndex, frameInUseCount, bufferImageGranularity,\r
- allocSize, allocAlignment, allocType, canMakeOtherLost, strategy, pAllocationRequest) :\r
- CreateAllocationRequest_LowerAddress(\r
- currentFrameIndex, frameInUseCount, bufferImageGranularity,\r
- allocSize, allocAlignment, allocType, canMakeOtherLost, strategy, pAllocationRequest);\r
-}\r
-\r
-bool VmaBlockMetadata_Linear::CreateAllocationRequest_UpperAddress(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- const VkDeviceSize size = GetSize();\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- VMA_ASSERT(0 && "Trying to use pool with linear algorithm as double stack, while it is already being used as ring buffer.");\r
- return false;\r
- }\r
-\r
- // Try to allocate before 2nd.back(), or end of block if 2nd.empty().\r
- if(allocSize > size)\r
- {\r
- return false;\r
- }\r
- VkDeviceSize resultBaseOffset = size - allocSize;\r
- if(!suballocations2nd.empty())\r
- {\r
- const VmaSuballocation& lastSuballoc = suballocations2nd.back();\r
- resultBaseOffset = lastSuballoc.offset - allocSize;\r
- if(allocSize > lastSuballoc.offset)\r
- {\r
- return false;\r
- }\r
- }\r
-\r
- // Start from offset equal to end of free space.\r
- VkDeviceSize resultOffset = resultBaseOffset;\r
-\r
- // Apply VMA_DEBUG_MARGIN at the end.\r
- if(VMA_DEBUG_MARGIN > 0)\r
- {\r
- if(resultOffset < VMA_DEBUG_MARGIN)\r
- {\r
- return false;\r
- }\r
- resultOffset -= VMA_DEBUG_MARGIN;\r
- }\r
-\r
- // Apply alignment.\r
- resultOffset = VmaAlignDown(resultOffset, allocAlignment);\r
-\r
- // Check next suballocations from 2nd for BufferImageGranularity conflicts.\r
- // Make bigger alignment if necessary.\r
- if(bufferImageGranularity > 1 && !suballocations2nd.empty())\r
- {\r
- bool bufferImageGranularityConflict = false;\r
- for(size_t nextSuballocIndex = suballocations2nd.size(); nextSuballocIndex--; )\r
- {\r
- const VmaSuballocation& nextSuballoc = suballocations2nd[nextSuballocIndex];\r
- if(VmaBlocksOnSamePage(resultOffset, allocSize, nextSuballoc.offset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(nextSuballoc.type, allocType))\r
- {\r
- bufferImageGranularityConflict = true;\r
- break;\r
- }\r
- }\r
- else\r
- // Already on previous page.\r
- break;\r
- }\r
- if(bufferImageGranularityConflict)\r
- {\r
- resultOffset = VmaAlignDown(resultOffset, bufferImageGranularity);\r
- }\r
- }\r
-\r
- // There is enough free space.\r
- const VkDeviceSize endOf1st = !suballocations1st.empty() ?\r
- suballocations1st.back().offset + suballocations1st.back().size :\r
- 0;\r
- if(endOf1st + VMA_DEBUG_MARGIN <= resultOffset)\r
- {\r
- // Check previous suballocations for BufferImageGranularity conflicts.\r
- // If conflict exists, allocation cannot be made here.\r
- if(bufferImageGranularity > 1)\r
- {\r
- for(size_t prevSuballocIndex = suballocations1st.size(); prevSuballocIndex--; )\r
- {\r
- const VmaSuballocation& prevSuballoc = suballocations1st[prevSuballocIndex];\r
- if(VmaBlocksOnSamePage(prevSuballoc.offset, prevSuballoc.size, resultOffset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(allocType, prevSuballoc.type))\r
- {\r
- return false;\r
- }\r
- }\r
- else\r
- {\r
- // Already on next page.\r
- break;\r
- }\r
- }\r
- }\r
-\r
- // All tests passed: Success.\r
- pAllocationRequest->offset = resultOffset;\r
- pAllocationRequest->sumFreeSize = resultBaseOffset + allocSize - endOf1st;\r
- pAllocationRequest->sumItemSize = 0;\r
- // pAllocationRequest->item unused.\r
- pAllocationRequest->itemsToMakeLostCount = 0;\r
- pAllocationRequest->type = VmaAllocationRequestType::UpperAddress;\r
- return true;\r
- }\r
-\r
- return false;\r
-}\r
-\r
-bool VmaBlockMetadata_Linear::CreateAllocationRequest_LowerAddress(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- const VkDeviceSize size = GetSize();\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
-\r
- if(m_2ndVectorMode == SECOND_VECTOR_EMPTY || m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- // Try to allocate at the end of 1st vector.\r
-\r
- VkDeviceSize resultBaseOffset = 0;\r
- if(!suballocations1st.empty())\r
- {\r
- const VmaSuballocation& lastSuballoc = suballocations1st.back();\r
- resultBaseOffset = lastSuballoc.offset + lastSuballoc.size;\r
- }\r
-\r
- // Start from offset equal to beginning of free space.\r
- VkDeviceSize resultOffset = resultBaseOffset;\r
-\r
- // Apply VMA_DEBUG_MARGIN at the beginning.\r
- if(VMA_DEBUG_MARGIN > 0)\r
- {\r
- resultOffset += VMA_DEBUG_MARGIN;\r
- }\r
-\r
- // Apply alignment.\r
- resultOffset = VmaAlignUp(resultOffset, allocAlignment);\r
-\r
- // Check previous suballocations for BufferImageGranularity conflicts.\r
- // Make bigger alignment if necessary.\r
- if(bufferImageGranularity > 1 && !suballocations1st.empty())\r
- {\r
- bool bufferImageGranularityConflict = false;\r
- for(size_t prevSuballocIndex = suballocations1st.size(); prevSuballocIndex--; )\r
- {\r
- const VmaSuballocation& prevSuballoc = suballocations1st[prevSuballocIndex];\r
- if(VmaBlocksOnSamePage(prevSuballoc.offset, prevSuballoc.size, resultOffset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(prevSuballoc.type, allocType))\r
- {\r
- bufferImageGranularityConflict = true;\r
- break;\r
- }\r
- }\r
- else\r
- // Already on previous page.\r
- break;\r
- }\r
- if(bufferImageGranularityConflict)\r
- {\r
- resultOffset = VmaAlignUp(resultOffset, bufferImageGranularity);\r
- }\r
- }\r
-\r
- const VkDeviceSize freeSpaceEnd = m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK ?\r
- suballocations2nd.back().offset : size;\r
-\r
- // There is enough free space at the end after alignment.\r
- if(resultOffset + allocSize + VMA_DEBUG_MARGIN <= freeSpaceEnd)\r
- {\r
- // Check next suballocations for BufferImageGranularity conflicts.\r
- // If conflict exists, allocation cannot be made here.\r
- if(bufferImageGranularity > 1 && m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- for(size_t nextSuballocIndex = suballocations2nd.size(); nextSuballocIndex--; )\r
- {\r
- const VmaSuballocation& nextSuballoc = suballocations2nd[nextSuballocIndex];\r
- if(VmaBlocksOnSamePage(resultOffset, allocSize, nextSuballoc.offset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(allocType, nextSuballoc.type))\r
- {\r
- return false;\r
- }\r
- }\r
- else\r
- {\r
- // Already on previous page.\r
- break;\r
- }\r
- }\r
- }\r
-\r
- // All tests passed: Success.\r
- pAllocationRequest->offset = resultOffset;\r
- pAllocationRequest->sumFreeSize = freeSpaceEnd - resultBaseOffset;\r
- pAllocationRequest->sumItemSize = 0;\r
- // pAllocationRequest->item, customData unused.\r
- pAllocationRequest->type = VmaAllocationRequestType::EndOf1st;\r
- pAllocationRequest->itemsToMakeLostCount = 0;\r
- return true;\r
- }\r
- }\r
-\r
- // Wrap-around to end of 2nd vector. Try to allocate there, watching for the\r
- // beginning of 1st vector as the end of free space.\r
- if(m_2ndVectorMode == SECOND_VECTOR_EMPTY || m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- VMA_ASSERT(!suballocations1st.empty());\r
-\r
- VkDeviceSize resultBaseOffset = 0;\r
- if(!suballocations2nd.empty())\r
- {\r
- const VmaSuballocation& lastSuballoc = suballocations2nd.back();\r
- resultBaseOffset = lastSuballoc.offset + lastSuballoc.size;\r
- }\r
-\r
- // Start from offset equal to beginning of free space.\r
- VkDeviceSize resultOffset = resultBaseOffset;\r
-\r
- // Apply VMA_DEBUG_MARGIN at the beginning.\r
- if(VMA_DEBUG_MARGIN > 0)\r
- {\r
- resultOffset += VMA_DEBUG_MARGIN;\r
- }\r
-\r
- // Apply alignment.\r
- resultOffset = VmaAlignUp(resultOffset, allocAlignment);\r
-\r
- // Check previous suballocations for BufferImageGranularity conflicts.\r
- // Make bigger alignment if necessary.\r
- if(bufferImageGranularity > 1 && !suballocations2nd.empty())\r
- {\r
- bool bufferImageGranularityConflict = false;\r
- for(size_t prevSuballocIndex = suballocations2nd.size(); prevSuballocIndex--; )\r
- {\r
- const VmaSuballocation& prevSuballoc = suballocations2nd[prevSuballocIndex];\r
- if(VmaBlocksOnSamePage(prevSuballoc.offset, prevSuballoc.size, resultOffset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(prevSuballoc.type, allocType))\r
- {\r
- bufferImageGranularityConflict = true;\r
- break;\r
- }\r
- }\r
- else\r
- // Already on previous page.\r
- break;\r
- }\r
- if(bufferImageGranularityConflict)\r
- {\r
- resultOffset = VmaAlignUp(resultOffset, bufferImageGranularity);\r
- }\r
- }\r
-\r
- pAllocationRequest->itemsToMakeLostCount = 0;\r
- pAllocationRequest->sumItemSize = 0;\r
- size_t index1st = m_1stNullItemsBeginCount;\r
-\r
- if(canMakeOtherLost)\r
- {\r
- while(index1st < suballocations1st.size() &&\r
- resultOffset + allocSize + VMA_DEBUG_MARGIN > suballocations1st[index1st].offset)\r
- {\r
- // Next colliding allocation at the beginning of 1st vector found. Try to make it lost.\r
- const VmaSuballocation& suballoc = suballocations1st[index1st];\r
- if(suballoc.type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- // No problem.\r
- }\r
- else\r
- {\r
- VMA_ASSERT(suballoc.hAllocation != VK_NULL_HANDLE);\r
- if(suballoc.hAllocation->CanBecomeLost() &&\r
- suballoc.hAllocation->GetLastUseFrameIndex() + frameInUseCount < currentFrameIndex)\r
- {\r
- ++pAllocationRequest->itemsToMakeLostCount;\r
- pAllocationRequest->sumItemSize += suballoc.size;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
- ++index1st;\r
- }\r
-\r
- // Check next suballocations for BufferImageGranularity conflicts.\r
- // If conflict exists, we must mark more allocations lost or fail.\r
- if(bufferImageGranularity > 1)\r
- {\r
- while(index1st < suballocations1st.size())\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[index1st];\r
- if(VmaBlocksOnSamePage(resultOffset, allocSize, suballoc.offset, bufferImageGranularity))\r
- {\r
- if(suballoc.hAllocation != VK_NULL_HANDLE)\r
- {\r
- // Not checking actual VmaIsBufferImageGranularityConflict(allocType, suballoc.type).\r
- if(suballoc.hAllocation->CanBecomeLost() &&\r
- suballoc.hAllocation->GetLastUseFrameIndex() + frameInUseCount < currentFrameIndex)\r
- {\r
- ++pAllocationRequest->itemsToMakeLostCount;\r
- pAllocationRequest->sumItemSize += suballoc.size;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
- }\r
- else\r
- {\r
- // Already on next page.\r
- break;\r
- }\r
- ++index1st;\r
- }\r
- }\r
-\r
- // Special case: There is not enough room at the end for this allocation, even after making all from the 1st lost.\r
- if(index1st == suballocations1st.size() &&\r
- resultOffset + allocSize + VMA_DEBUG_MARGIN > size)\r
- {\r
- // TODO: This is a known bug that it's not yet implemented and the allocation is failing.\r
- VMA_DEBUG_LOG("Unsupported special case in custom pool with linear allocation algorithm used as ring buffer with allocations that can be lost.");\r
- }\r
- }\r
-\r
- // There is enough free space at the end after alignment.\r
- if((index1st == suballocations1st.size() && resultOffset + allocSize + VMA_DEBUG_MARGIN <= size) ||\r
- (index1st < suballocations1st.size() && resultOffset + allocSize + VMA_DEBUG_MARGIN <= suballocations1st[index1st].offset))\r
- {\r
- // Check next suballocations for BufferImageGranularity conflicts.\r
- // If conflict exists, allocation cannot be made here.\r
- if(bufferImageGranularity > 1)\r
- {\r
- for(size_t nextSuballocIndex = index1st;\r
- nextSuballocIndex < suballocations1st.size();\r
- nextSuballocIndex++)\r
- {\r
- const VmaSuballocation& nextSuballoc = suballocations1st[nextSuballocIndex];\r
- if(VmaBlocksOnSamePage(resultOffset, allocSize, nextSuballoc.offset, bufferImageGranularity))\r
- {\r
- if(VmaIsBufferImageGranularityConflict(allocType, nextSuballoc.type))\r
- {\r
- return false;\r
- }\r
- }\r
- else\r
- {\r
- // Already on next page.\r
- break;\r
- }\r
- }\r
- }\r
-\r
- // All tests passed: Success.\r
- pAllocationRequest->offset = resultOffset;\r
- pAllocationRequest->sumFreeSize =\r
- (index1st < suballocations1st.size() ? suballocations1st[index1st].offset : size)\r
- - resultBaseOffset\r
- - pAllocationRequest->sumItemSize;\r
- pAllocationRequest->type = VmaAllocationRequestType::EndOf2nd;\r
- // pAllocationRequest->item, customData unused.\r
- return true;\r
- }\r
- }\r
-\r
- return false;\r
-}\r
-\r
-bool VmaBlockMetadata_Linear::MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- if(pAllocationRequest->itemsToMakeLostCount == 0)\r
- {\r
- return true;\r
- }\r
-\r
- VMA_ASSERT(m_2ndVectorMode == SECOND_VECTOR_EMPTY || m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER);\r
- \r
- // We always start from 1st.\r
- SuballocationVectorType* suballocations = &AccessSuballocations1st();\r
- size_t index = m_1stNullItemsBeginCount;\r
- size_t madeLostCount = 0;\r
- while(madeLostCount < pAllocationRequest->itemsToMakeLostCount)\r
- {\r
- if(index == suballocations->size())\r
- {\r
- index = 0;\r
- // If we get to the end of 1st, we wrap around to beginning of 2nd of 1st.\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- suballocations = &AccessSuballocations2nd();\r
- }\r
- // else: m_2ndVectorMode == SECOND_VECTOR_EMPTY:\r
- // suballocations continues pointing at AccessSuballocations1st().\r
- VMA_ASSERT(!suballocations->empty());\r
- }\r
- VmaSuballocation& suballoc = (*suballocations)[index];\r
- if(suballoc.type != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- VMA_ASSERT(suballoc.hAllocation != VK_NULL_HANDLE);\r
- VMA_ASSERT(suballoc.hAllocation->CanBecomeLost());\r
- if(suballoc.hAllocation->MakeLost(currentFrameIndex, frameInUseCount))\r
- {\r
- suballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- suballoc.hAllocation = VK_NULL_HANDLE;\r
- m_SumFreeSize += suballoc.size;\r
- if(suballocations == &AccessSuballocations1st())\r
- {\r
- ++m_1stNullItemsMiddleCount;\r
- }\r
- else\r
- {\r
- ++m_2ndNullItemsCount;\r
- }\r
- ++madeLostCount;\r
- }\r
- else\r
- {\r
- return false;\r
- }\r
- }\r
- ++index;\r
- }\r
-\r
- CleanupAfterFree();\r
- //VMA_HEAVY_ASSERT(Validate()); // Already called by ClanupAfterFree().\r
- \r
- return true;\r
-}\r
-\r
-uint32_t VmaBlockMetadata_Linear::MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount)\r
-{\r
- uint32_t lostAllocationCount = 0;\r
- \r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- for(size_t i = m_1stNullItemsBeginCount, count = suballocations1st.size(); i < count; ++i)\r
- {\r
- VmaSuballocation& suballoc = suballocations1st[i];\r
- if(suballoc.type != VMA_SUBALLOCATION_TYPE_FREE &&\r
- suballoc.hAllocation->CanBecomeLost() &&\r
- suballoc.hAllocation->MakeLost(currentFrameIndex, frameInUseCount))\r
- {\r
- suballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- suballoc.hAllocation = VK_NULL_HANDLE;\r
- ++m_1stNullItemsMiddleCount;\r
- m_SumFreeSize += suballoc.size;\r
- ++lostAllocationCount;\r
- }\r
- }\r
-\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- for(size_t i = 0, count = suballocations2nd.size(); i < count; ++i)\r
- {\r
- VmaSuballocation& suballoc = suballocations2nd[i];\r
- if(suballoc.type != VMA_SUBALLOCATION_TYPE_FREE &&\r
- suballoc.hAllocation->CanBecomeLost() &&\r
- suballoc.hAllocation->MakeLost(currentFrameIndex, frameInUseCount))\r
- {\r
- suballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- suballoc.hAllocation = VK_NULL_HANDLE;\r
- ++m_2ndNullItemsCount;\r
- m_SumFreeSize += suballoc.size;\r
- ++lostAllocationCount;\r
- }\r
- }\r
-\r
- if(lostAllocationCount)\r
- {\r
- CleanupAfterFree();\r
- }\r
-\r
- return lostAllocationCount;\r
-}\r
-\r
-VkResult VmaBlockMetadata_Linear::CheckCorruption(const void* pBlockData)\r
-{\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- for(size_t i = m_1stNullItemsBeginCount, count = suballocations1st.size(); i < count; ++i)\r
- {\r
- const VmaSuballocation& suballoc = suballocations1st[i];\r
- if(suballoc.type != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- if(!VmaValidateMagicValue(pBlockData, suballoc.offset - VMA_DEBUG_MARGIN))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED BEFORE VALIDATED ALLOCATION!");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- if(!VmaValidateMagicValue(pBlockData, suballoc.offset + suballoc.size))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED AFTER VALIDATED ALLOCATION!");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- }\r
- }\r
-\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- for(size_t i = 0, count = suballocations2nd.size(); i < count; ++i)\r
- {\r
- const VmaSuballocation& suballoc = suballocations2nd[i];\r
- if(suballoc.type != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- if(!VmaValidateMagicValue(pBlockData, suballoc.offset - VMA_DEBUG_MARGIN))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED BEFORE VALIDATED ALLOCATION!");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- if(!VmaValidateMagicValue(pBlockData, suballoc.offset + suballoc.size))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED AFTER VALIDATED ALLOCATION!");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- }\r
- }\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaBlockMetadata_Linear::Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation)\r
-{\r
- const VmaSuballocation newSuballoc = { request.offset, allocSize, hAllocation, type };\r
-\r
- switch(request.type)\r
- {\r
- case VmaAllocationRequestType::UpperAddress:\r
- {\r
- VMA_ASSERT(m_2ndVectorMode != SECOND_VECTOR_RING_BUFFER &&\r
- "CRITICAL ERROR: Trying to use linear allocator as double stack while it was already used as ring buffer.");\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
- suballocations2nd.push_back(newSuballoc);\r
- m_2ndVectorMode = SECOND_VECTOR_DOUBLE_STACK;\r
- }\r
- break;\r
- case VmaAllocationRequestType::EndOf1st:\r
- {\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
-\r
- VMA_ASSERT(suballocations1st.empty() ||\r
- request.offset >= suballocations1st.back().offset + suballocations1st.back().size);\r
- // Check if it fits before the end of the block.\r
- VMA_ASSERT(request.offset + allocSize <= GetSize());\r
-\r
- suballocations1st.push_back(newSuballoc);\r
- }\r
- break;\r
- case VmaAllocationRequestType::EndOf2nd:\r
- {\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- // New allocation at the end of 2-part ring buffer, so before first allocation from 1st vector.\r
- VMA_ASSERT(!suballocations1st.empty() &&\r
- request.offset + allocSize <= suballocations1st[m_1stNullItemsBeginCount].offset);\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
-\r
- switch(m_2ndVectorMode)\r
- {\r
- case SECOND_VECTOR_EMPTY:\r
- // First allocation from second part ring buffer.\r
- VMA_ASSERT(suballocations2nd.empty());\r
- m_2ndVectorMode = SECOND_VECTOR_RING_BUFFER;\r
- break;\r
- case SECOND_VECTOR_RING_BUFFER:\r
- // 2-part ring buffer is already started.\r
- VMA_ASSERT(!suballocations2nd.empty());\r
- break;\r
- case SECOND_VECTOR_DOUBLE_STACK:\r
- VMA_ASSERT(0 && "CRITICAL ERROR: Trying to use linear allocator as ring buffer while it was already used as double stack.");\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
-\r
- suballocations2nd.push_back(newSuballoc);\r
- }\r
- break;\r
- default:\r
- VMA_ASSERT(0 && "CRITICAL INTERNAL ERROR.");\r
- }\r
-\r
- m_SumFreeSize -= newSuballoc.size;\r
-}\r
-\r
-void VmaBlockMetadata_Linear::Free(const VmaAllocation allocation)\r
-{\r
- FreeAtOffset(allocation->GetOffset());\r
-}\r
-\r
-void VmaBlockMetadata_Linear::FreeAtOffset(VkDeviceSize offset)\r
-{\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
-\r
- if(!suballocations1st.empty())\r
- {\r
- // First allocation: Mark it as next empty at the beginning.\r
- VmaSuballocation& firstSuballoc = suballocations1st[m_1stNullItemsBeginCount];\r
- if(firstSuballoc.offset == offset)\r
- {\r
- firstSuballoc.type = VMA_SUBALLOCATION_TYPE_FREE;\r
- firstSuballoc.hAllocation = VK_NULL_HANDLE;\r
- m_SumFreeSize += firstSuballoc.size;\r
- ++m_1stNullItemsBeginCount;\r
- CleanupAfterFree();\r
- return;\r
- }\r
- }\r
-\r
- // Last allocation in 2-part ring buffer or top of upper stack (same logic).\r
- if(m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER ||\r
- m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK)\r
- {\r
- VmaSuballocation& lastSuballoc = suballocations2nd.back();\r
- if(lastSuballoc.offset == offset)\r
- {\r
- m_SumFreeSize += lastSuballoc.size;\r
- suballocations2nd.pop_back();\r
- CleanupAfterFree();\r
- return;\r
- }\r
- }\r
- // Last allocation in 1st vector.\r
- else if(m_2ndVectorMode == SECOND_VECTOR_EMPTY)\r
- {\r
- VmaSuballocation& lastSuballoc = suballocations1st.back();\r
- if(lastSuballoc.offset == offset)\r
- {\r
- m_SumFreeSize += lastSuballoc.size;\r
- suballocations1st.pop_back();\r
- CleanupAfterFree();\r
- return;\r
- }\r
- }\r
-\r
- // Item from the middle of 1st vector.\r
- {\r
- VmaSuballocation refSuballoc;\r
- refSuballoc.offset = offset;\r
- // Rest of members stays uninitialized intentionally for better performance.\r
- SuballocationVectorType::iterator it = VmaBinaryFindSorted(\r
- suballocations1st.begin() + m_1stNullItemsBeginCount,\r
- suballocations1st.end(),\r
- refSuballoc,\r
- VmaSuballocationOffsetLess());\r
- if(it != suballocations1st.end())\r
- {\r
- it->type = VMA_SUBALLOCATION_TYPE_FREE;\r
- it->hAllocation = VK_NULL_HANDLE;\r
- ++m_1stNullItemsMiddleCount;\r
- m_SumFreeSize += it->size;\r
- CleanupAfterFree();\r
- return;\r
- }\r
- }\r
-\r
- if(m_2ndVectorMode != SECOND_VECTOR_EMPTY)\r
- {\r
- // Item from the middle of 2nd vector.\r
- VmaSuballocation refSuballoc;\r
- refSuballoc.offset = offset;\r
- // Rest of members stays uninitialized intentionally for better performance.\r
- SuballocationVectorType::iterator it = m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER ?\r
- VmaBinaryFindSorted(suballocations2nd.begin(), suballocations2nd.end(), refSuballoc, VmaSuballocationOffsetLess()) :\r
- VmaBinaryFindSorted(suballocations2nd.begin(), suballocations2nd.end(), refSuballoc, VmaSuballocationOffsetGreater());\r
- if(it != suballocations2nd.end())\r
- {\r
- it->type = VMA_SUBALLOCATION_TYPE_FREE;\r
- it->hAllocation = VK_NULL_HANDLE;\r
- ++m_2ndNullItemsCount;\r
- m_SumFreeSize += it->size;\r
- CleanupAfterFree();\r
- return;\r
- }\r
- }\r
-\r
- VMA_ASSERT(0 && "Allocation to free not found in linear allocator!");\r
-}\r
-\r
-bool VmaBlockMetadata_Linear::ShouldCompact1st() const\r
-{\r
- const size_t nullItemCount = m_1stNullItemsBeginCount + m_1stNullItemsMiddleCount;\r
- const size_t suballocCount = AccessSuballocations1st().size();\r
- return suballocCount > 32 && nullItemCount * 2 >= (suballocCount - nullItemCount) * 3;\r
-}\r
-\r
-void VmaBlockMetadata_Linear::CleanupAfterFree()\r
-{\r
- SuballocationVectorType& suballocations1st = AccessSuballocations1st();\r
- SuballocationVectorType& suballocations2nd = AccessSuballocations2nd();\r
-\r
- if(IsEmpty())\r
- {\r
- suballocations1st.clear();\r
- suballocations2nd.clear();\r
- m_1stNullItemsBeginCount = 0;\r
- m_1stNullItemsMiddleCount = 0;\r
- m_2ndNullItemsCount = 0;\r
- m_2ndVectorMode = SECOND_VECTOR_EMPTY;\r
- }\r
- else\r
- {\r
- const size_t suballoc1stCount = suballocations1st.size();\r
- const size_t nullItem1stCount = m_1stNullItemsBeginCount + m_1stNullItemsMiddleCount;\r
- VMA_ASSERT(nullItem1stCount <= suballoc1stCount);\r
-\r
- // Find more null items at the beginning of 1st vector.\r
- while(m_1stNullItemsBeginCount < suballoc1stCount &&\r
- suballocations1st[m_1stNullItemsBeginCount].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++m_1stNullItemsBeginCount;\r
- --m_1stNullItemsMiddleCount;\r
- }\r
-\r
- // Find more null items at the end of 1st vector.\r
- while(m_1stNullItemsMiddleCount > 0 &&\r
- suballocations1st.back().hAllocation == VK_NULL_HANDLE)\r
- {\r
- --m_1stNullItemsMiddleCount;\r
- suballocations1st.pop_back();\r
- }\r
-\r
- // Find more null items at the end of 2nd vector.\r
- while(m_2ndNullItemsCount > 0 &&\r
- suballocations2nd.back().hAllocation == VK_NULL_HANDLE)\r
- {\r
- --m_2ndNullItemsCount;\r
- suballocations2nd.pop_back();\r
- }\r
-\r
- // Find more null items at the beginning of 2nd vector.\r
- while(m_2ndNullItemsCount > 0 &&\r
- suballocations2nd[0].hAllocation == VK_NULL_HANDLE)\r
- {\r
- --m_2ndNullItemsCount;\r
- VmaVectorRemove(suballocations2nd, 0);\r
- }\r
-\r
- if(ShouldCompact1st())\r
- {\r
- const size_t nonNullItemCount = suballoc1stCount - nullItem1stCount;\r
- size_t srcIndex = m_1stNullItemsBeginCount;\r
- for(size_t dstIndex = 0; dstIndex < nonNullItemCount; ++dstIndex)\r
- {\r
- while(suballocations1st[srcIndex].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++srcIndex;\r
- }\r
- if(dstIndex != srcIndex)\r
- {\r
- suballocations1st[dstIndex] = suballocations1st[srcIndex];\r
- }\r
- ++srcIndex;\r
- }\r
- suballocations1st.resize(nonNullItemCount);\r
- m_1stNullItemsBeginCount = 0;\r
- m_1stNullItemsMiddleCount = 0;\r
- }\r
-\r
- // 2nd vector became empty.\r
- if(suballocations2nd.empty())\r
- {\r
- m_2ndVectorMode = SECOND_VECTOR_EMPTY;\r
- }\r
-\r
- // 1st vector became empty.\r
- if(suballocations1st.size() - m_1stNullItemsBeginCount == 0)\r
- {\r
- suballocations1st.clear();\r
- m_1stNullItemsBeginCount = 0;\r
-\r
- if(!suballocations2nd.empty() && m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER)\r
- {\r
- // Swap 1st with 2nd. Now 2nd is empty.\r
- m_2ndVectorMode = SECOND_VECTOR_EMPTY;\r
- m_1stNullItemsMiddleCount = m_2ndNullItemsCount;\r
- while(m_1stNullItemsBeginCount < suballocations2nd.size() &&\r
- suballocations2nd[m_1stNullItemsBeginCount].hAllocation == VK_NULL_HANDLE)\r
- {\r
- ++m_1stNullItemsBeginCount;\r
- --m_1stNullItemsMiddleCount;\r
- }\r
- m_2ndNullItemsCount = 0;\r
- m_1stVectorIndex ^= 1;\r
- }\r
- }\r
- }\r
-\r
- VMA_HEAVY_ASSERT(Validate());\r
-}\r
-\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaBlockMetadata_Buddy\r
-\r
-VmaBlockMetadata_Buddy::VmaBlockMetadata_Buddy(VmaAllocator hAllocator) :\r
- VmaBlockMetadata(hAllocator),\r
- m_Root(VMA_NULL),\r
- m_AllocationCount(0),\r
- m_FreeCount(1),\r
- m_SumFreeSize(0)\r
-{\r
- memset(m_FreeList, 0, sizeof(m_FreeList));\r
-}\r
-\r
-VmaBlockMetadata_Buddy::~VmaBlockMetadata_Buddy()\r
-{\r
- DeleteNode(m_Root);\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::Init(VkDeviceSize size)\r
-{\r
- VmaBlockMetadata::Init(size);\r
-\r
- m_UsableSize = VmaPrevPow2(size);\r
- m_SumFreeSize = m_UsableSize;\r
-\r
- // Calculate m_LevelCount.\r
- m_LevelCount = 1;\r
- while(m_LevelCount < MAX_LEVELS &&\r
- LevelToNodeSize(m_LevelCount) >= MIN_NODE_SIZE)\r
- {\r
- ++m_LevelCount;\r
- }\r
-\r
- Node* rootNode = vma_new(GetAllocationCallbacks(), Node)();\r
- rootNode->offset = 0;\r
- rootNode->type = Node::TYPE_FREE;\r
- rootNode->parent = VMA_NULL;\r
- rootNode->buddy = VMA_NULL;\r
-\r
- m_Root = rootNode;\r
- AddToFreeListFront(0, rootNode);\r
-}\r
-\r
-bool VmaBlockMetadata_Buddy::Validate() const\r
-{\r
- // Validate tree.\r
- ValidationContext ctx;\r
- if(!ValidateNode(ctx, VMA_NULL, m_Root, 0, LevelToNodeSize(0)))\r
- {\r
- VMA_VALIDATE(false && "ValidateNode failed.");\r
- }\r
- VMA_VALIDATE(m_AllocationCount == ctx.calculatedAllocationCount);\r
- VMA_VALIDATE(m_SumFreeSize == ctx.calculatedSumFreeSize);\r
-\r
- // Validate free node lists.\r
- for(uint32_t level = 0; level < m_LevelCount; ++level)\r
- {\r
- VMA_VALIDATE(m_FreeList[level].front == VMA_NULL ||\r
- m_FreeList[level].front->free.prev == VMA_NULL);\r
-\r
- for(Node* node = m_FreeList[level].front;\r
- node != VMA_NULL;\r
- node = node->free.next)\r
- {\r
- VMA_VALIDATE(node->type == Node::TYPE_FREE);\r
- \r
- if(node->free.next == VMA_NULL)\r
- {\r
- VMA_VALIDATE(m_FreeList[level].back == node);\r
- }\r
- else\r
- {\r
- VMA_VALIDATE(node->free.next->free.prev == node);\r
- }\r
- }\r
- }\r
-\r
- // Validate that free lists ar higher levels are empty.\r
- for(uint32_t level = m_LevelCount; level < MAX_LEVELS; ++level)\r
- {\r
- VMA_VALIDATE(m_FreeList[level].front == VMA_NULL && m_FreeList[level].back == VMA_NULL);\r
- }\r
-\r
- return true;\r
-}\r
-\r
-VkDeviceSize VmaBlockMetadata_Buddy::GetUnusedRangeSizeMax() const\r
-{\r
- for(uint32_t level = 0; level < m_LevelCount; ++level)\r
- {\r
- if(m_FreeList[level].front != VMA_NULL)\r
- {\r
- return LevelToNodeSize(level);\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::CalcAllocationStatInfo(VmaStatInfo& outInfo) const\r
-{\r
- const VkDeviceSize unusableSize = GetUnusableSize();\r
-\r
- outInfo.blockCount = 1;\r
-\r
- outInfo.allocationCount = outInfo.unusedRangeCount = 0;\r
- outInfo.usedBytes = outInfo.unusedBytes = 0;\r
-\r
- outInfo.allocationSizeMax = outInfo.unusedRangeSizeMax = 0;\r
- outInfo.allocationSizeMin = outInfo.unusedRangeSizeMin = UINT64_MAX;\r
- outInfo.allocationSizeAvg = outInfo.unusedRangeSizeAvg = 0; // Unused.\r
-\r
- CalcAllocationStatInfoNode(outInfo, m_Root, LevelToNodeSize(0));\r
-\r
- if(unusableSize > 0)\r
- {\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusableSize;\r
- outInfo.unusedRangeSizeMax = VMA_MAX(outInfo.unusedRangeSizeMax, unusableSize);\r
- outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusableSize);\r
- }\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::AddPoolStats(VmaPoolStats& inoutStats) const\r
-{\r
- const VkDeviceSize unusableSize = GetUnusableSize();\r
-\r
- inoutStats.size += GetSize();\r
- inoutStats.unusedSize += m_SumFreeSize + unusableSize;\r
- inoutStats.allocationCount += m_AllocationCount;\r
- inoutStats.unusedRangeCount += m_FreeCount;\r
- inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, GetUnusedRangeSizeMax());\r
-\r
- if(unusableSize > 0)\r
- {\r
- ++inoutStats.unusedRangeCount;\r
- // Not updating inoutStats.unusedRangeSizeMax with unusableSize because this space is not available for allocations.\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-void VmaBlockMetadata_Buddy::PrintDetailedMap(class VmaJsonWriter& json) const\r
-{\r
- // TODO optimize\r
- VmaStatInfo stat;\r
- CalcAllocationStatInfo(stat);\r
-\r
- PrintDetailedMap_Begin(\r
- json,\r
- stat.unusedBytes,\r
- stat.allocationCount,\r
- stat.unusedRangeCount);\r
-\r
- PrintDetailedMapNode(json, m_Root, LevelToNodeSize(0));\r
-\r
- const VkDeviceSize unusableSize = GetUnusableSize();\r
- if(unusableSize > 0)\r
- {\r
- PrintDetailedMap_UnusedRange(json,\r
- m_UsableSize, // offset\r
- unusableSize); // size\r
- }\r
-\r
- PrintDetailedMap_End(json);\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-bool VmaBlockMetadata_Buddy::CreateAllocationRequest(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VkDeviceSize bufferImageGranularity,\r
- VkDeviceSize allocSize,\r
- VkDeviceSize allocAlignment,\r
- bool upperAddress,\r
- VmaSuballocationType allocType,\r
- bool canMakeOtherLost,\r
- uint32_t strategy,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- VMA_ASSERT(!upperAddress && "VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT can be used only with linear algorithm.");\r
-\r
- // Simple way to respect bufferImageGranularity. May be optimized some day.\r
- // Whenever it might be an OPTIMAL image...\r
- if(allocType == VMA_SUBALLOCATION_TYPE_UNKNOWN ||\r
- allocType == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||\r
- allocType == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL)\r
- {\r
- allocAlignment = VMA_MAX(allocAlignment, bufferImageGranularity);\r
- allocSize = VMA_MAX(allocSize, bufferImageGranularity);\r
- }\r
-\r
- if(allocSize > m_UsableSize)\r
- {\r
- return false;\r
- }\r
-\r
- const uint32_t targetLevel = AllocSizeToLevel(allocSize);\r
- for(uint32_t level = targetLevel + 1; level--; )\r
- {\r
- for(Node* freeNode = m_FreeList[level].front;\r
- freeNode != VMA_NULL;\r
- freeNode = freeNode->free.next)\r
- {\r
- if(freeNode->offset % allocAlignment == 0)\r
- {\r
- pAllocationRequest->type = VmaAllocationRequestType::Normal;\r
- pAllocationRequest->offset = freeNode->offset;\r
- pAllocationRequest->sumFreeSize = LevelToNodeSize(level);\r
- pAllocationRequest->sumItemSize = 0;\r
- pAllocationRequest->itemsToMakeLostCount = 0;\r
- pAllocationRequest->customData = (void*)(uintptr_t)level;\r
- return true;\r
- }\r
- }\r
- }\r
-\r
- return false;\r
-}\r
-\r
-bool VmaBlockMetadata_Buddy::MakeRequestedAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- uint32_t frameInUseCount,\r
- VmaAllocationRequest* pAllocationRequest)\r
-{\r
- /*\r
- Lost allocations are not supported in buddy allocator at the moment.\r
- Support might be added in the future.\r
- */\r
- return pAllocationRequest->itemsToMakeLostCount == 0;\r
-}\r
-\r
-uint32_t VmaBlockMetadata_Buddy::MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount)\r
-{\r
- /*\r
- Lost allocations are not supported in buddy allocator at the moment.\r
- Support might be added in the future.\r
- */\r
- return 0;\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::Alloc(\r
- const VmaAllocationRequest& request,\r
- VmaSuballocationType type,\r
- VkDeviceSize allocSize,\r
- VmaAllocation hAllocation)\r
-{\r
- VMA_ASSERT(request.type == VmaAllocationRequestType::Normal);\r
-\r
- const uint32_t targetLevel = AllocSizeToLevel(allocSize);\r
- uint32_t currLevel = (uint32_t)(uintptr_t)request.customData;\r
- \r
- Node* currNode = m_FreeList[currLevel].front;\r
- VMA_ASSERT(currNode != VMA_NULL && currNode->type == Node::TYPE_FREE);\r
- while(currNode->offset != request.offset)\r
- {\r
- currNode = currNode->free.next;\r
- VMA_ASSERT(currNode != VMA_NULL && currNode->type == Node::TYPE_FREE);\r
- }\r
- \r
- // Go down, splitting free nodes.\r
- while(currLevel < targetLevel)\r
- {\r
- // currNode is already first free node at currLevel.\r
- // Remove it from list of free nodes at this currLevel.\r
- RemoveFromFreeList(currLevel, currNode);\r
- \r
- const uint32_t childrenLevel = currLevel + 1;\r
-\r
- // Create two free sub-nodes.\r
- Node* leftChild = vma_new(GetAllocationCallbacks(), Node)();\r
- Node* rightChild = vma_new(GetAllocationCallbacks(), Node)();\r
-\r
- leftChild->offset = currNode->offset;\r
- leftChild->type = Node::TYPE_FREE;\r
- leftChild->parent = currNode;\r
- leftChild->buddy = rightChild;\r
-\r
- rightChild->offset = currNode->offset + LevelToNodeSize(childrenLevel);\r
- rightChild->type = Node::TYPE_FREE;\r
- rightChild->parent = currNode;\r
- rightChild->buddy = leftChild;\r
-\r
- // Convert current currNode to split type.\r
- currNode->type = Node::TYPE_SPLIT;\r
- currNode->split.leftChild = leftChild;\r
-\r
- // Add child nodes to free list. Order is important!\r
- AddToFreeListFront(childrenLevel, rightChild);\r
- AddToFreeListFront(childrenLevel, leftChild);\r
-\r
- ++m_FreeCount;\r
- //m_SumFreeSize -= LevelToNodeSize(currLevel) % 2; // Useful only when level node sizes can be non power of 2.\r
- ++currLevel;\r
- currNode = m_FreeList[currLevel].front;\r
-\r
- /*\r
- We can be sure that currNode, as left child of node previously split,\r
- also fullfills the alignment requirement.\r
- */\r
- }\r
-\r
- // Remove from free list.\r
- VMA_ASSERT(currLevel == targetLevel &&\r
- currNode != VMA_NULL &&\r
- currNode->type == Node::TYPE_FREE);\r
- RemoveFromFreeList(currLevel, currNode);\r
-\r
- // Convert to allocation node.\r
- currNode->type = Node::TYPE_ALLOCATION;\r
- currNode->allocation.alloc = hAllocation;\r
-\r
- ++m_AllocationCount;\r
- --m_FreeCount;\r
- m_SumFreeSize -= allocSize;\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::DeleteNode(Node* node)\r
-{\r
- if(node->type == Node::TYPE_SPLIT)\r
- {\r
- DeleteNode(node->split.leftChild->buddy);\r
- DeleteNode(node->split.leftChild);\r
- }\r
-\r
- vma_delete(GetAllocationCallbacks(), node);\r
-}\r
-\r
-bool VmaBlockMetadata_Buddy::ValidateNode(ValidationContext& ctx, const Node* parent, const Node* curr, uint32_t level, VkDeviceSize levelNodeSize) const\r
-{\r
- VMA_VALIDATE(level < m_LevelCount);\r
- VMA_VALIDATE(curr->parent == parent);\r
- VMA_VALIDATE((curr->buddy == VMA_NULL) == (parent == VMA_NULL));\r
- VMA_VALIDATE(curr->buddy == VMA_NULL || curr->buddy->buddy == curr);\r
- switch(curr->type)\r
- {\r
- case Node::TYPE_FREE:\r
- // curr->free.prev, next are validated separately.\r
- ctx.calculatedSumFreeSize += levelNodeSize;\r
- ++ctx.calculatedFreeCount;\r
- break;\r
- case Node::TYPE_ALLOCATION:\r
- ++ctx.calculatedAllocationCount;\r
- ctx.calculatedSumFreeSize += levelNodeSize - curr->allocation.alloc->GetSize();\r
- VMA_VALIDATE(curr->allocation.alloc != VK_NULL_HANDLE);\r
- break;\r
- case Node::TYPE_SPLIT:\r
- {\r
- const uint32_t childrenLevel = level + 1;\r
- const VkDeviceSize childrenLevelNodeSize = levelNodeSize / 2;\r
- const Node* const leftChild = curr->split.leftChild;\r
- VMA_VALIDATE(leftChild != VMA_NULL);\r
- VMA_VALIDATE(leftChild->offset == curr->offset);\r
- if(!ValidateNode(ctx, curr, leftChild, childrenLevel, childrenLevelNodeSize))\r
- {\r
- VMA_VALIDATE(false && "ValidateNode for left child failed.");\r
- }\r
- const Node* const rightChild = leftChild->buddy;\r
- VMA_VALIDATE(rightChild->offset == curr->offset + childrenLevelNodeSize);\r
- if(!ValidateNode(ctx, curr, rightChild, childrenLevel, childrenLevelNodeSize))\r
- {\r
- VMA_VALIDATE(false && "ValidateNode for right child failed.");\r
- }\r
- }\r
- break;\r
- default:\r
- return false;\r
- }\r
-\r
- return true;\r
-}\r
-\r
-uint32_t VmaBlockMetadata_Buddy::AllocSizeToLevel(VkDeviceSize allocSize) const\r
-{\r
- // I know this could be optimized somehow e.g. by using std::log2p1 from C++20.\r
- uint32_t level = 0;\r
- VkDeviceSize currLevelNodeSize = m_UsableSize;\r
- VkDeviceSize nextLevelNodeSize = currLevelNodeSize >> 1;\r
- while(allocSize <= nextLevelNodeSize && level + 1 < m_LevelCount)\r
- {\r
- ++level;\r
- currLevelNodeSize = nextLevelNodeSize;\r
- nextLevelNodeSize = currLevelNodeSize >> 1;\r
- }\r
- return level;\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::FreeAtOffset(VmaAllocation alloc, VkDeviceSize offset)\r
-{\r
- // Find node and level.\r
- Node* node = m_Root;\r
- VkDeviceSize nodeOffset = 0;\r
- uint32_t level = 0;\r
- VkDeviceSize levelNodeSize = LevelToNodeSize(0);\r
- while(node->type == Node::TYPE_SPLIT)\r
- {\r
- const VkDeviceSize nextLevelSize = levelNodeSize >> 1;\r
- if(offset < nodeOffset + nextLevelSize)\r
- {\r
- node = node->split.leftChild;\r
- }\r
- else\r
- {\r
- node = node->split.leftChild->buddy;\r
- nodeOffset += nextLevelSize;\r
- }\r
- ++level;\r
- levelNodeSize = nextLevelSize;\r
- }\r
-\r
- VMA_ASSERT(node != VMA_NULL && node->type == Node::TYPE_ALLOCATION);\r
- VMA_ASSERT(alloc == VK_NULL_HANDLE || node->allocation.alloc == alloc);\r
-\r
- ++m_FreeCount;\r
- --m_AllocationCount;\r
- m_SumFreeSize += alloc->GetSize();\r
-\r
- node->type = Node::TYPE_FREE;\r
-\r
- // Join free nodes if possible.\r
- while(level > 0 && node->buddy->type == Node::TYPE_FREE)\r
- {\r
- RemoveFromFreeList(level, node->buddy);\r
- Node* const parent = node->parent;\r
-\r
- vma_delete(GetAllocationCallbacks(), node->buddy);\r
- vma_delete(GetAllocationCallbacks(), node);\r
- parent->type = Node::TYPE_FREE;\r
- \r
- node = parent;\r
- --level;\r
- //m_SumFreeSize += LevelToNodeSize(level) % 2; // Useful only when level node sizes can be non power of 2.\r
- --m_FreeCount;\r
- }\r
-\r
- AddToFreeListFront(level, node);\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::CalcAllocationStatInfoNode(VmaStatInfo& outInfo, const Node* node, VkDeviceSize levelNodeSize) const\r
-{\r
- switch(node->type)\r
- {\r
- case Node::TYPE_FREE:\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += levelNodeSize;\r
- outInfo.unusedRangeSizeMax = VMA_MAX(outInfo.unusedRangeSizeMax, levelNodeSize);\r
- outInfo.unusedRangeSizeMin = VMA_MAX(outInfo.unusedRangeSizeMin, levelNodeSize);\r
- break;\r
- case Node::TYPE_ALLOCATION:\r
- {\r
- const VkDeviceSize allocSize = node->allocation.alloc->GetSize();\r
- ++outInfo.allocationCount;\r
- outInfo.usedBytes += allocSize;\r
- outInfo.allocationSizeMax = VMA_MAX(outInfo.allocationSizeMax, allocSize);\r
- outInfo.allocationSizeMin = VMA_MAX(outInfo.allocationSizeMin, allocSize);\r
-\r
- const VkDeviceSize unusedRangeSize = levelNodeSize - allocSize;\r
- if(unusedRangeSize > 0)\r
- {\r
- ++outInfo.unusedRangeCount;\r
- outInfo.unusedBytes += unusedRangeSize;\r
- outInfo.unusedRangeSizeMax = VMA_MAX(outInfo.unusedRangeSizeMax, unusedRangeSize);\r
- outInfo.unusedRangeSizeMin = VMA_MAX(outInfo.unusedRangeSizeMin, unusedRangeSize);\r
- }\r
- }\r
- break;\r
- case Node::TYPE_SPLIT:\r
- {\r
- const VkDeviceSize childrenNodeSize = levelNodeSize / 2;\r
- const Node* const leftChild = node->split.leftChild;\r
- CalcAllocationStatInfoNode(outInfo, leftChild, childrenNodeSize);\r
- const Node* const rightChild = leftChild->buddy;\r
- CalcAllocationStatInfoNode(outInfo, rightChild, childrenNodeSize);\r
- }\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::AddToFreeListFront(uint32_t level, Node* node)\r
-{\r
- VMA_ASSERT(node->type == Node::TYPE_FREE);\r
-\r
- // List is empty.\r
- Node* const frontNode = m_FreeList[level].front;\r
- if(frontNode == VMA_NULL)\r
- {\r
- VMA_ASSERT(m_FreeList[level].back == VMA_NULL);\r
- node->free.prev = node->free.next = VMA_NULL;\r
- m_FreeList[level].front = m_FreeList[level].back = node;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(frontNode->free.prev == VMA_NULL);\r
- node->free.prev = VMA_NULL;\r
- node->free.next = frontNode;\r
- frontNode->free.prev = node;\r
- m_FreeList[level].front = node;\r
- }\r
-}\r
-\r
-void VmaBlockMetadata_Buddy::RemoveFromFreeList(uint32_t level, Node* node)\r
-{\r
- VMA_ASSERT(m_FreeList[level].front != VMA_NULL);\r
-\r
- // It is at the front.\r
- if(node->free.prev == VMA_NULL)\r
- {\r
- VMA_ASSERT(m_FreeList[level].front == node);\r
- m_FreeList[level].front = node->free.next;\r
- }\r
- else\r
- {\r
- Node* const prevFreeNode = node->free.prev;\r
- VMA_ASSERT(prevFreeNode->free.next == node);\r
- prevFreeNode->free.next = node->free.next;\r
- }\r
-\r
- // It is at the back.\r
- if(node->free.next == VMA_NULL)\r
- {\r
- VMA_ASSERT(m_FreeList[level].back == node);\r
- m_FreeList[level].back = node->free.prev;\r
- }\r
- else\r
- {\r
- Node* const nextFreeNode = node->free.next;\r
- VMA_ASSERT(nextFreeNode->free.prev == node);\r
- nextFreeNode->free.prev = node->free.prev;\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-void VmaBlockMetadata_Buddy::PrintDetailedMapNode(class VmaJsonWriter& json, const Node* node, VkDeviceSize levelNodeSize) const\r
-{\r
- switch(node->type)\r
- {\r
- case Node::TYPE_FREE:\r
- PrintDetailedMap_UnusedRange(json, node->offset, levelNodeSize);\r
- break;\r
- case Node::TYPE_ALLOCATION:\r
- { \r
- PrintDetailedMap_Allocation(json, node->offset, node->allocation.alloc);\r
- const VkDeviceSize allocSize = node->allocation.alloc->GetSize();\r
- if(allocSize < levelNodeSize)\r
- {\r
- PrintDetailedMap_UnusedRange(json, node->offset + allocSize, levelNodeSize - allocSize);\r
- }\r
- }\r
- break;\r
- case Node::TYPE_SPLIT:\r
- {\r
- const VkDeviceSize childrenNodeSize = levelNodeSize / 2;\r
- const Node* const leftChild = node->split.leftChild;\r
- PrintDetailedMapNode(json, leftChild, childrenNodeSize);\r
- const Node* const rightChild = leftChild->buddy;\r
- PrintDetailedMapNode(json, rightChild, childrenNodeSize);\r
- }\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
-}\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// class VmaDeviceMemoryBlock\r
-\r
-VmaDeviceMemoryBlock::VmaDeviceMemoryBlock(VmaAllocator hAllocator) :\r
- m_pMetadata(VMA_NULL),\r
- m_MemoryTypeIndex(UINT32_MAX),\r
- m_Id(0),\r
- m_hMemory(VK_NULL_HANDLE),\r
- m_MapCount(0),\r
- m_pMappedData(VMA_NULL)\r
-{\r
-}\r
-\r
-void VmaDeviceMemoryBlock::Init(\r
- VmaAllocator hAllocator,\r
- VmaPool hParentPool,\r
- uint32_t newMemoryTypeIndex,\r
- VkDeviceMemory newMemory,\r
- VkDeviceSize newSize,\r
- uint32_t id,\r
- uint32_t algorithm)\r
-{\r
- VMA_ASSERT(m_hMemory == VK_NULL_HANDLE);\r
-\r
- m_hParentPool = hParentPool;\r
- m_MemoryTypeIndex = newMemoryTypeIndex;\r
- m_Id = id;\r
- m_hMemory = newMemory;\r
-\r
- switch(algorithm)\r
- {\r
- case VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT:\r
- m_pMetadata = vma_new(hAllocator, VmaBlockMetadata_Linear)(hAllocator);\r
- break;\r
- case VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT:\r
- m_pMetadata = vma_new(hAllocator, VmaBlockMetadata_Buddy)(hAllocator);\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- // Fall-through.\r
- case 0:\r
- m_pMetadata = vma_new(hAllocator, VmaBlockMetadata_Generic)(hAllocator);\r
- }\r
- m_pMetadata->Init(newSize);\r
-}\r
-\r
-void VmaDeviceMemoryBlock::Destroy(VmaAllocator allocator)\r
-{\r
- // This is the most important assert in the entire library.\r
- // Hitting it means you have some memory leak - unreleased VmaAllocation objects.\r
- VMA_ASSERT(m_pMetadata->IsEmpty() && "Some allocations were not freed before destruction of this memory block!");\r
-\r
- VMA_ASSERT(m_hMemory != VK_NULL_HANDLE);\r
- allocator->FreeVulkanMemory(m_MemoryTypeIndex, m_pMetadata->GetSize(), m_hMemory);\r
- m_hMemory = VK_NULL_HANDLE;\r
-\r
- vma_delete(allocator, m_pMetadata);\r
- m_pMetadata = VMA_NULL;\r
-}\r
-\r
-bool VmaDeviceMemoryBlock::Validate() const\r
-{\r
- VMA_VALIDATE((m_hMemory != VK_NULL_HANDLE) &&\r
- (m_pMetadata->GetSize() != 0));\r
- \r
- return m_pMetadata->Validate();\r
-}\r
-\r
-VkResult VmaDeviceMemoryBlock::CheckCorruption(VmaAllocator hAllocator)\r
-{\r
- void* pData = nullptr;\r
- VkResult res = Map(hAllocator, 1, &pData);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
-\r
- res = m_pMetadata->CheckCorruption(pData);\r
-\r
- Unmap(hAllocator, 1);\r
-\r
- return res;\r
-}\r
-\r
-VkResult VmaDeviceMemoryBlock::Map(VmaAllocator hAllocator, uint32_t count, void** ppData)\r
-{\r
- if(count == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- VmaMutexLock lock(m_Mutex, hAllocator->m_UseMutex);\r
- if(m_MapCount != 0)\r
- {\r
- m_MapCount += count;\r
- VMA_ASSERT(m_pMappedData != VMA_NULL);\r
- if(ppData != VMA_NULL)\r
- {\r
- *ppData = m_pMappedData;\r
- }\r
- return VK_SUCCESS;\r
- }\r
- else\r
- {\r
- VkResult result = (*hAllocator->GetVulkanFunctions().vkMapMemory)(\r
- hAllocator->m_hDevice,\r
- m_hMemory,\r
- 0, // offset\r
- VK_WHOLE_SIZE,\r
- 0, // flags\r
- &m_pMappedData);\r
- if(result == VK_SUCCESS)\r
- {\r
- if(ppData != VMA_NULL)\r
- {\r
- *ppData = m_pMappedData;\r
- }\r
- m_MapCount = count;\r
- }\r
- return result;\r
- }\r
-}\r
-\r
-void VmaDeviceMemoryBlock::Unmap(VmaAllocator hAllocator, uint32_t count)\r
-{\r
- if(count == 0)\r
- {\r
- return;\r
- }\r
-\r
- VmaMutexLock lock(m_Mutex, hAllocator->m_UseMutex);\r
- if(m_MapCount >= count)\r
- {\r
- m_MapCount -= count;\r
- if(m_MapCount == 0)\r
- {\r
- m_pMappedData = VMA_NULL;\r
- (*hAllocator->GetVulkanFunctions().vkUnmapMemory)(hAllocator->m_hDevice, m_hMemory);\r
- }\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0 && "VkDeviceMemory block is being unmapped while it was not previously mapped.");\r
- }\r
-}\r
-\r
-VkResult VmaDeviceMemoryBlock::WriteMagicValueAroundAllocation(VmaAllocator hAllocator, VkDeviceSize allocOffset, VkDeviceSize allocSize)\r
-{\r
- VMA_ASSERT(VMA_DEBUG_MARGIN > 0 && VMA_DEBUG_MARGIN % 4 == 0 && VMA_DEBUG_DETECT_CORRUPTION);\r
- VMA_ASSERT(allocOffset >= VMA_DEBUG_MARGIN);\r
-\r
- void* pData;\r
- VkResult res = Map(hAllocator, 1, &pData);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
-\r
- VmaWriteMagicValue(pData, allocOffset - VMA_DEBUG_MARGIN);\r
- VmaWriteMagicValue(pData, allocOffset + allocSize);\r
-\r
- Unmap(hAllocator, 1);\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-VkResult VmaDeviceMemoryBlock::ValidateMagicValueAroundAllocation(VmaAllocator hAllocator, VkDeviceSize allocOffset, VkDeviceSize allocSize)\r
-{\r
- VMA_ASSERT(VMA_DEBUG_MARGIN > 0 && VMA_DEBUG_MARGIN % 4 == 0 && VMA_DEBUG_DETECT_CORRUPTION);\r
- VMA_ASSERT(allocOffset >= VMA_DEBUG_MARGIN);\r
-\r
- void* pData;\r
- VkResult res = Map(hAllocator, 1, &pData);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
-\r
- if(!VmaValidateMagicValue(pData, allocOffset - VMA_DEBUG_MARGIN))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED BEFORE FREED ALLOCATION!");\r
- }\r
- else if(!VmaValidateMagicValue(pData, allocOffset + allocSize))\r
- {\r
- VMA_ASSERT(0 && "MEMORY CORRUPTION DETECTED AFTER FREED ALLOCATION!");\r
- }\r
-\r
- Unmap(hAllocator, 1);\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-VkResult VmaDeviceMemoryBlock::BindBufferMemory(\r
- const VmaAllocator hAllocator,\r
- const VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkBuffer hBuffer,\r
- const void* pNext)\r
-{\r
- VMA_ASSERT(hAllocation->GetType() == VmaAllocation_T::ALLOCATION_TYPE_BLOCK &&\r
- hAllocation->GetBlock() == this);\r
- VMA_ASSERT(allocationLocalOffset < hAllocation->GetSize() &&\r
- "Invalid allocationLocalOffset. Did you forget that this offset is relative to the beginning of the allocation, not the whole memory block?");\r
- const VkDeviceSize memoryOffset = hAllocation->GetOffset() + allocationLocalOffset;\r
- // This lock is important so that we don't call vkBind... and/or vkMap... simultaneously on the same VkDeviceMemory from multiple threads.\r
- VmaMutexLock lock(m_Mutex, hAllocator->m_UseMutex);\r
- return hAllocator->BindVulkanBuffer(m_hMemory, memoryOffset, hBuffer, pNext);\r
-}\r
-\r
-VkResult VmaDeviceMemoryBlock::BindImageMemory(\r
- const VmaAllocator hAllocator,\r
- const VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkImage hImage,\r
- const void* pNext)\r
-{\r
- VMA_ASSERT(hAllocation->GetType() == VmaAllocation_T::ALLOCATION_TYPE_BLOCK &&\r
- hAllocation->GetBlock() == this);\r
- VMA_ASSERT(allocationLocalOffset < hAllocation->GetSize() &&\r
- "Invalid allocationLocalOffset. Did you forget that this offset is relative to the beginning of the allocation, not the whole memory block?");\r
- const VkDeviceSize memoryOffset = hAllocation->GetOffset() + allocationLocalOffset;\r
- // This lock is important so that we don't call vkBind... and/or vkMap... simultaneously on the same VkDeviceMemory from multiple threads.\r
- VmaMutexLock lock(m_Mutex, hAllocator->m_UseMutex);\r
- return hAllocator->BindVulkanImage(m_hMemory, memoryOffset, hImage, pNext);\r
-}\r
-\r
-static void InitStatInfo(VmaStatInfo& outInfo)\r
-{\r
- memset(&outInfo, 0, sizeof(outInfo));\r
- outInfo.allocationSizeMin = UINT64_MAX;\r
- outInfo.unusedRangeSizeMin = UINT64_MAX;\r
-}\r
-\r
-// Adds statistics srcInfo into inoutInfo, like: inoutInfo += srcInfo.\r
-static void VmaAddStatInfo(VmaStatInfo& inoutInfo, const VmaStatInfo& srcInfo)\r
-{\r
- inoutInfo.blockCount += srcInfo.blockCount;\r
- inoutInfo.allocationCount += srcInfo.allocationCount;\r
- inoutInfo.unusedRangeCount += srcInfo.unusedRangeCount;\r
- inoutInfo.usedBytes += srcInfo.usedBytes;\r
- inoutInfo.unusedBytes += srcInfo.unusedBytes;\r
- inoutInfo.allocationSizeMin = VMA_MIN(inoutInfo.allocationSizeMin, srcInfo.allocationSizeMin);\r
- inoutInfo.allocationSizeMax = VMA_MAX(inoutInfo.allocationSizeMax, srcInfo.allocationSizeMax);\r
- inoutInfo.unusedRangeSizeMin = VMA_MIN(inoutInfo.unusedRangeSizeMin, srcInfo.unusedRangeSizeMin);\r
- inoutInfo.unusedRangeSizeMax = VMA_MAX(inoutInfo.unusedRangeSizeMax, srcInfo.unusedRangeSizeMax);\r
-}\r
-\r
-static void VmaPostprocessCalcStatInfo(VmaStatInfo& inoutInfo)\r
-{\r
- inoutInfo.allocationSizeAvg = (inoutInfo.allocationCount > 0) ?\r
- VmaRoundDiv<VkDeviceSize>(inoutInfo.usedBytes, inoutInfo.allocationCount) : 0;\r
- inoutInfo.unusedRangeSizeAvg = (inoutInfo.unusedRangeCount > 0) ?\r
- VmaRoundDiv<VkDeviceSize>(inoutInfo.unusedBytes, inoutInfo.unusedRangeCount) : 0;\r
-}\r
-\r
-VmaPool_T::VmaPool_T(\r
- VmaAllocator hAllocator,\r
- const VmaPoolCreateInfo& createInfo,\r
- VkDeviceSize preferredBlockSize) :\r
- m_BlockVector(\r
- hAllocator,\r
- this, // hParentPool\r
- createInfo.memoryTypeIndex,\r
- createInfo.blockSize != 0 ? createInfo.blockSize : preferredBlockSize,\r
- createInfo.minBlockCount,\r
- createInfo.maxBlockCount,\r
- (createInfo.flags & VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT) != 0 ? 1 : hAllocator->GetBufferImageGranularity(),\r
- createInfo.frameInUseCount,\r
- createInfo.blockSize != 0, // explicitBlockSize\r
- createInfo.flags & VMA_POOL_CREATE_ALGORITHM_MASK), // algorithm\r
- m_Id(0),\r
- m_Name(VMA_NULL)\r
-{\r
-}\r
-\r
-VmaPool_T::~VmaPool_T()\r
-{\r
-}\r
-\r
-void VmaPool_T::SetName(const char* pName)\r
-{\r
- const VkAllocationCallbacks* allocs = m_BlockVector.GetAllocator()->GetAllocationCallbacks();\r
- VmaFreeString(allocs, m_Name);\r
- \r
- if(pName != VMA_NULL)\r
- {\r
- m_Name = VmaCreateStringCopy(allocs, pName);\r
- }\r
- else\r
- {\r
- m_Name = VMA_NULL;\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-VmaBlockVector::VmaBlockVector(\r
- VmaAllocator hAllocator,\r
- VmaPool hParentPool,\r
- uint32_t memoryTypeIndex,\r
- VkDeviceSize preferredBlockSize,\r
- size_t minBlockCount,\r
- size_t maxBlockCount,\r
- VkDeviceSize bufferImageGranularity,\r
- uint32_t frameInUseCount,\r
- bool explicitBlockSize,\r
- uint32_t algorithm) :\r
- m_hAllocator(hAllocator),\r
- m_hParentPool(hParentPool),\r
- m_MemoryTypeIndex(memoryTypeIndex),\r
- m_PreferredBlockSize(preferredBlockSize),\r
- m_MinBlockCount(minBlockCount),\r
- m_MaxBlockCount(maxBlockCount),\r
- m_BufferImageGranularity(bufferImageGranularity),\r
- m_FrameInUseCount(frameInUseCount),\r
- m_ExplicitBlockSize(explicitBlockSize),\r
- m_Algorithm(algorithm),\r
- m_HasEmptyBlock(false),\r
- m_Blocks(VmaStlAllocator<VmaDeviceMemoryBlock*>(hAllocator->GetAllocationCallbacks())),\r
- m_NextBlockId(0)\r
-{\r
-}\r
-\r
-VmaBlockVector::~VmaBlockVector()\r
-{\r
- for(size_t i = m_Blocks.size(); i--; )\r
- {\r
- m_Blocks[i]->Destroy(m_hAllocator);\r
- vma_delete(m_hAllocator, m_Blocks[i]);\r
- }\r
-}\r
-\r
-VkResult VmaBlockVector::CreateMinBlocks()\r
-{\r
- for(size_t i = 0; i < m_MinBlockCount; ++i)\r
- {\r
- VkResult res = CreateBlock(m_PreferredBlockSize, VMA_NULL);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- }\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaBlockVector::GetPoolStats(VmaPoolStats* pStats)\r
-{\r
- VmaMutexLockRead lock(m_Mutex, m_hAllocator->m_UseMutex);\r
-\r
- const size_t blockCount = m_Blocks.size();\r
-\r
- pStats->size = 0;\r
- pStats->unusedSize = 0;\r
- pStats->allocationCount = 0;\r
- pStats->unusedRangeCount = 0;\r
- pStats->unusedRangeSizeMax = 0;\r
- pStats->blockCount = blockCount;\r
-\r
- for(uint32_t blockIndex = 0; blockIndex < blockCount; ++blockIndex)\r
- {\r
- const VmaDeviceMemoryBlock* const pBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pBlock);\r
- VMA_HEAVY_ASSERT(pBlock->Validate());\r
- pBlock->m_pMetadata->AddPoolStats(*pStats);\r
- }\r
-}\r
-\r
-bool VmaBlockVector::IsEmpty()\r
-{\r
- VmaMutexLockRead lock(m_Mutex, m_hAllocator->m_UseMutex);\r
- return m_Blocks.empty();\r
-}\r
-\r
-bool VmaBlockVector::IsCorruptionDetectionEnabled() const\r
-{\r
- const uint32_t requiredMemFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;\r
- return (VMA_DEBUG_DETECT_CORRUPTION != 0) &&\r
- (VMA_DEBUG_MARGIN > 0) &&\r
- (m_Algorithm == 0 || m_Algorithm == VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT) &&\r
- (m_hAllocator->m_MemProps.memoryTypes[m_MemoryTypeIndex].propertyFlags & requiredMemFlags) == requiredMemFlags;\r
-}\r
-\r
-static const uint32_t VMA_ALLOCATION_TRY_COUNT = 32;\r
-\r
-VkResult VmaBlockVector::Allocate(\r
- uint32_t currentFrameIndex,\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaSuballocationType suballocType,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations)\r
-{\r
- size_t allocIndex;\r
- VkResult res = VK_SUCCESS;\r
-\r
- if(IsCorruptionDetectionEnabled())\r
- {\r
- size = VmaAlignUp<VkDeviceSize>(size, sizeof(VMA_CORRUPTION_DETECTION_MAGIC_VALUE));\r
- alignment = VmaAlignUp<VkDeviceSize>(alignment, sizeof(VMA_CORRUPTION_DETECTION_MAGIC_VALUE));\r
- }\r
-\r
- {\r
- VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex);\r
- for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex)\r
- {\r
- res = AllocatePage(\r
- currentFrameIndex,\r
- size,\r
- alignment,\r
- createInfo,\r
- suballocType,\r
- pAllocations + allocIndex);\r
- if(res != VK_SUCCESS)\r
- {\r
- break;\r
- }\r
- }\r
- }\r
-\r
- if(res != VK_SUCCESS)\r
- {\r
- // Free all already created allocations.\r
- while(allocIndex--)\r
- {\r
- Free(pAllocations[allocIndex]);\r
- }\r
- memset(pAllocations, 0, sizeof(VmaAllocation) * allocationCount);\r
- }\r
-\r
- return res;\r
-}\r
-\r
-VkResult VmaBlockVector::AllocatePage(\r
- uint32_t currentFrameIndex,\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaSuballocationType suballocType,\r
- VmaAllocation* pAllocation)\r
-{\r
- const bool isUpperAddress = (createInfo.flags & VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT) != 0;\r
- bool canMakeOtherLost = (createInfo.flags & VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT) != 0;\r
- const bool mapped = (createInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0;\r
- const bool isUserDataString = (createInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0;\r
- \r
- VkDeviceSize freeMemory;\r
- {\r
- const uint32_t heapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex);\r
- VmaBudget heapBudget = {};\r
- m_hAllocator->GetBudget(&heapBudget, heapIndex, 1);\r
- freeMemory = (heapBudget.usage < heapBudget.budget) ? (heapBudget.budget - heapBudget.usage) : 0;\r
- }\r
- \r
- const bool canFallbackToDedicated = !IsCustomPool();\r
- const bool canCreateNewBlock =\r
- ((createInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) == 0) &&\r
- (m_Blocks.size() < m_MaxBlockCount) &&\r
- (freeMemory >= size || !canFallbackToDedicated);\r
- uint32_t strategy = createInfo.flags & VMA_ALLOCATION_CREATE_STRATEGY_MASK;\r
-\r
- // If linearAlgorithm is used, canMakeOtherLost is available only when used as ring buffer.\r
- // Which in turn is available only when maxBlockCount = 1.\r
- if(m_Algorithm == VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT && m_MaxBlockCount > 1)\r
- {\r
- canMakeOtherLost = false;\r
- }\r
-\r
- // Upper address can only be used with linear allocator and within single memory block.\r
- if(isUpperAddress &&\r
- (m_Algorithm != VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT || m_MaxBlockCount > 1))\r
- {\r
- return VK_ERROR_FEATURE_NOT_PRESENT;\r
- }\r
-\r
- // Validate strategy.\r
- switch(strategy)\r
- {\r
- case 0:\r
- strategy = VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT;\r
- break;\r
- case VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT:\r
- case VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT:\r
- case VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT:\r
- break;\r
- default:\r
- return VK_ERROR_FEATURE_NOT_PRESENT;\r
- }\r
-\r
- // Early reject: requested allocation size is larger that maximum block size for this block vector.\r
- if(size + 2 * VMA_DEBUG_MARGIN > m_PreferredBlockSize)\r
- {\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
-\r
- /*\r
- Under certain condition, this whole section can be skipped for optimization, so\r
- we move on directly to trying to allocate with canMakeOtherLost. That's the case\r
- e.g. for custom pools with linear algorithm.\r
- */\r
- if(!canMakeOtherLost || canCreateNewBlock)\r
- {\r
- // 1. Search existing allocations. Try to allocate without making other allocations lost.\r
- VmaAllocationCreateFlags allocFlagsCopy = createInfo.flags;\r
- allocFlagsCopy &= ~VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;\r
-\r
- if(m_Algorithm == VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT)\r
- {\r
- // Use only last block.\r
- if(!m_Blocks.empty())\r
- {\r
- VmaDeviceMemoryBlock* const pCurrBlock = m_Blocks.back();\r
- VMA_ASSERT(pCurrBlock);\r
- VkResult res = AllocateFromBlock(\r
- pCurrBlock,\r
- currentFrameIndex,\r
- size,\r
- alignment,\r
- allocFlagsCopy,\r
- createInfo.pUserData,\r
- suballocType,\r
- strategy,\r
- pAllocation);\r
- if(res == VK_SUCCESS)\r
- {\r
- VMA_DEBUG_LOG(" Returned from last block #%u", pCurrBlock->GetId());\r
- return VK_SUCCESS;\r
- }\r
- }\r
- }\r
- else\r
- {\r
- if(strategy == VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT)\r
- {\r
- // Forward order in m_Blocks - prefer blocks with smallest amount of free space.\r
- for(size_t blockIndex = 0; blockIndex < m_Blocks.size(); ++blockIndex )\r
- {\r
- VmaDeviceMemoryBlock* const pCurrBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pCurrBlock);\r
- VkResult res = AllocateFromBlock(\r
- pCurrBlock,\r
- currentFrameIndex,\r
- size,\r
- alignment,\r
- allocFlagsCopy,\r
- createInfo.pUserData,\r
- suballocType,\r
- strategy,\r
- pAllocation);\r
- if(res == VK_SUCCESS)\r
- {\r
- VMA_DEBUG_LOG(" Returned from existing block #%u", pCurrBlock->GetId());\r
- return VK_SUCCESS;\r
- }\r
- }\r
- }\r
- else // WORST_FIT, FIRST_FIT\r
- {\r
- // Backward order in m_Blocks - prefer blocks with largest amount of free space.\r
- for(size_t blockIndex = m_Blocks.size(); blockIndex--; )\r
- {\r
- VmaDeviceMemoryBlock* const pCurrBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pCurrBlock);\r
- VkResult res = AllocateFromBlock(\r
- pCurrBlock,\r
- currentFrameIndex,\r
- size,\r
- alignment,\r
- allocFlagsCopy,\r
- createInfo.pUserData,\r
- suballocType,\r
- strategy,\r
- pAllocation);\r
- if(res == VK_SUCCESS)\r
- {\r
- VMA_DEBUG_LOG(" Returned from existing block #%u", pCurrBlock->GetId());\r
- return VK_SUCCESS;\r
- }\r
- }\r
- }\r
- }\r
-\r
- // 2. Try to create new block.\r
- if(canCreateNewBlock)\r
- {\r
- // Calculate optimal size for new block.\r
- VkDeviceSize newBlockSize = m_PreferredBlockSize;\r
- uint32_t newBlockSizeShift = 0;\r
- const uint32_t NEW_BLOCK_SIZE_SHIFT_MAX = 3;\r
-\r
- if(!m_ExplicitBlockSize)\r
- {\r
- // Allocate 1/8, 1/4, 1/2 as first blocks.\r
- const VkDeviceSize maxExistingBlockSize = CalcMaxBlockSize();\r
- for(uint32_t i = 0; i < NEW_BLOCK_SIZE_SHIFT_MAX; ++i)\r
- {\r
- const VkDeviceSize smallerNewBlockSize = newBlockSize / 2;\r
- if(smallerNewBlockSize > maxExistingBlockSize && smallerNewBlockSize >= size * 2)\r
- {\r
- newBlockSize = smallerNewBlockSize;\r
- ++newBlockSizeShift;\r
- }\r
- else\r
- {\r
- break;\r
- }\r
- }\r
- }\r
-\r
- size_t newBlockIndex = 0;\r
- VkResult res = (newBlockSize <= freeMemory || !canFallbackToDedicated) ?\r
- CreateBlock(newBlockSize, &newBlockIndex) : VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- // Allocation of this size failed? Try 1/2, 1/4, 1/8 of m_PreferredBlockSize.\r
- if(!m_ExplicitBlockSize)\r
- {\r
- while(res < 0 && newBlockSizeShift < NEW_BLOCK_SIZE_SHIFT_MAX)\r
- {\r
- const VkDeviceSize smallerNewBlockSize = newBlockSize / 2;\r
- if(smallerNewBlockSize >= size)\r
- {\r
- newBlockSize = smallerNewBlockSize;\r
- ++newBlockSizeShift;\r
- res = (newBlockSize <= freeMemory || !canFallbackToDedicated) ?\r
- CreateBlock(newBlockSize, &newBlockIndex) : VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- else\r
- {\r
- break;\r
- }\r
- }\r
- }\r
-\r
- if(res == VK_SUCCESS)\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = m_Blocks[newBlockIndex];\r
- VMA_ASSERT(pBlock->m_pMetadata->GetSize() >= size);\r
-\r
- res = AllocateFromBlock(\r
- pBlock,\r
- currentFrameIndex,\r
- size,\r
- alignment,\r
- allocFlagsCopy,\r
- createInfo.pUserData,\r
- suballocType,\r
- strategy,\r
- pAllocation);\r
- if(res == VK_SUCCESS)\r
- {\r
- VMA_DEBUG_LOG(" Created new block #%u Size=%llu", pBlock->GetId(), newBlockSize);\r
- return VK_SUCCESS;\r
- }\r
- else\r
- {\r
- // Allocation from new block failed, possibly due to VMA_DEBUG_MARGIN or alignment.\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- }\r
- }\r
- }\r
-\r
- // 3. Try to allocate from existing blocks with making other allocations lost.\r
- if(canMakeOtherLost)\r
- {\r
- uint32_t tryIndex = 0;\r
- for(; tryIndex < VMA_ALLOCATION_TRY_COUNT; ++tryIndex)\r
- {\r
- VmaDeviceMemoryBlock* pBestRequestBlock = VMA_NULL;\r
- VmaAllocationRequest bestRequest = {};\r
- VkDeviceSize bestRequestCost = VK_WHOLE_SIZE;\r
-\r
- // 1. Search existing allocations.\r
- if(strategy == VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT)\r
- {\r
- // Forward order in m_Blocks - prefer blocks with smallest amount of free space.\r
- for(size_t blockIndex = 0; blockIndex < m_Blocks.size(); ++blockIndex )\r
- {\r
- VmaDeviceMemoryBlock* const pCurrBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pCurrBlock);\r
- VmaAllocationRequest currRequest = {};\r
- if(pCurrBlock->m_pMetadata->CreateAllocationRequest(\r
- currentFrameIndex,\r
- m_FrameInUseCount,\r
- m_BufferImageGranularity,\r
- size,\r
- alignment,\r
- (createInfo.flags & VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT) != 0,\r
- suballocType,\r
- canMakeOtherLost,\r
- strategy,\r
- &currRequest))\r
- {\r
- const VkDeviceSize currRequestCost = currRequest.CalcCost();\r
- if(pBestRequestBlock == VMA_NULL ||\r
- currRequestCost < bestRequestCost)\r
- {\r
- pBestRequestBlock = pCurrBlock;\r
- bestRequest = currRequest;\r
- bestRequestCost = currRequestCost;\r
-\r
- if(bestRequestCost == 0)\r
- {\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- else // WORST_FIT, FIRST_FIT\r
- {\r
- // Backward order in m_Blocks - prefer blocks with largest amount of free space.\r
- for(size_t blockIndex = m_Blocks.size(); blockIndex--; )\r
- {\r
- VmaDeviceMemoryBlock* const pCurrBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pCurrBlock);\r
- VmaAllocationRequest currRequest = {};\r
- if(pCurrBlock->m_pMetadata->CreateAllocationRequest(\r
- currentFrameIndex,\r
- m_FrameInUseCount,\r
- m_BufferImageGranularity,\r
- size,\r
- alignment,\r
- (createInfo.flags & VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT) != 0,\r
- suballocType,\r
- canMakeOtherLost,\r
- strategy,\r
- &currRequest))\r
- {\r
- const VkDeviceSize currRequestCost = currRequest.CalcCost();\r
- if(pBestRequestBlock == VMA_NULL ||\r
- currRequestCost < bestRequestCost ||\r
- strategy == VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT)\r
- {\r
- pBestRequestBlock = pCurrBlock;\r
- bestRequest = currRequest;\r
- bestRequestCost = currRequestCost;\r
-\r
- if(bestRequestCost == 0 ||\r
- strategy == VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT)\r
- {\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- if(pBestRequestBlock != VMA_NULL)\r
- {\r
- if(mapped)\r
- {\r
- VkResult res = pBestRequestBlock->Map(m_hAllocator, 1, VMA_NULL);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- }\r
-\r
- if(pBestRequestBlock->m_pMetadata->MakeRequestedAllocationsLost(\r
- currentFrameIndex,\r
- m_FrameInUseCount,\r
- &bestRequest))\r
- {\r
- // Allocate from this pBlock.\r
- *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(currentFrameIndex, isUserDataString);\r
- pBestRequestBlock->m_pMetadata->Alloc(bestRequest, suballocType, size, *pAllocation);\r
- UpdateHasEmptyBlock();\r
- (*pAllocation)->InitBlockAllocation(\r
- pBestRequestBlock,\r
- bestRequest.offset,\r
- alignment,\r
- size,\r
- m_MemoryTypeIndex,\r
- suballocType,\r
- mapped,\r
- (createInfo.flags & VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT) != 0);\r
- VMA_HEAVY_ASSERT(pBestRequestBlock->Validate());\r
- VMA_DEBUG_LOG(" Returned from existing block");\r
- (*pAllocation)->SetUserData(m_hAllocator, createInfo.pUserData);\r
- m_hAllocator->m_Budget.AddAllocation(m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex), size);\r
- if(VMA_DEBUG_INITIALIZE_ALLOCATIONS)\r
- {\r
- m_hAllocator->FillAllocation(*pAllocation, VMA_ALLOCATION_FILL_PATTERN_CREATED);\r
- }\r
- if(IsCorruptionDetectionEnabled())\r
- {\r
- VkResult res = pBestRequestBlock->WriteMagicValueAroundAllocation(m_hAllocator, bestRequest.offset, size);\r
- VMA_ASSERT(res == VK_SUCCESS && "Couldn't map block memory to write magic value.");\r
- }\r
- return VK_SUCCESS;\r
- }\r
- // else: Some allocations must have been touched while we are here. Next try.\r
- }\r
- else\r
- {\r
- // Could not find place in any of the blocks - break outer loop.\r
- break;\r
- }\r
- }\r
- /* Maximum number of tries exceeded - a very unlike event when many other\r
- threads are simultaneously touching allocations making it impossible to make\r
- lost at the same time as we try to allocate. */\r
- if(tryIndex == VMA_ALLOCATION_TRY_COUNT)\r
- {\r
- return VK_ERROR_TOO_MANY_OBJECTS;\r
- }\r
- }\r
-\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
-}\r
-\r
-void VmaBlockVector::Free(\r
- const VmaAllocation hAllocation)\r
-{\r
- VmaDeviceMemoryBlock* pBlockToDelete = VMA_NULL;\r
-\r
- bool budgetExceeded = false;\r
- {\r
- const uint32_t heapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex);\r
- VmaBudget heapBudget = {};\r
- m_hAllocator->GetBudget(&heapBudget, heapIndex, 1);\r
- budgetExceeded = heapBudget.usage >= heapBudget.budget;\r
- }\r
-\r
- // Scope for lock.\r
- {\r
- VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex);\r
-\r
- VmaDeviceMemoryBlock* pBlock = hAllocation->GetBlock();\r
-\r
- if(IsCorruptionDetectionEnabled())\r
- {\r
- VkResult res = pBlock->ValidateMagicValueAroundAllocation(m_hAllocator, hAllocation->GetOffset(), hAllocation->GetSize());\r
- VMA_ASSERT(res == VK_SUCCESS && "Couldn't map block memory to validate magic value.");\r
- }\r
-\r
- if(hAllocation->IsPersistentMap())\r
- {\r
- pBlock->Unmap(m_hAllocator, 1);\r
- }\r
-\r
- pBlock->m_pMetadata->Free(hAllocation);\r
- VMA_HEAVY_ASSERT(pBlock->Validate());\r
-\r
- VMA_DEBUG_LOG(" Freed from MemoryTypeIndex=%u", m_MemoryTypeIndex);\r
-\r
- const bool canDeleteBlock = m_Blocks.size() > m_MinBlockCount;\r
- // pBlock became empty after this deallocation.\r
- if(pBlock->m_pMetadata->IsEmpty())\r
- {\r
- // Already has empty block. We don't want to have two, so delete this one.\r
- if((m_HasEmptyBlock || budgetExceeded) && canDeleteBlock)\r
- {\r
- pBlockToDelete = pBlock;\r
- Remove(pBlock);\r
- }\r
- // else: We now have an empty block - leave it.\r
- }\r
- // pBlock didn't become empty, but we have another empty block - find and free that one.\r
- // (This is optional, heuristics.)\r
- else if(m_HasEmptyBlock && canDeleteBlock)\r
- {\r
- VmaDeviceMemoryBlock* pLastBlock = m_Blocks.back();\r
- if(pLastBlock->m_pMetadata->IsEmpty())\r
- {\r
- pBlockToDelete = pLastBlock;\r
- m_Blocks.pop_back();\r
- }\r
- }\r
-\r
- UpdateHasEmptyBlock();\r
- IncrementallySortBlocks();\r
- }\r
-\r
- // Destruction of a free block. Deferred until this point, outside of mutex\r
- // lock, for performance reason.\r
- if(pBlockToDelete != VMA_NULL)\r
- {\r
- VMA_DEBUG_LOG(" Deleted empty block");\r
- pBlockToDelete->Destroy(m_hAllocator);\r
- vma_delete(m_hAllocator, pBlockToDelete);\r
- }\r
-}\r
-\r
-VkDeviceSize VmaBlockVector::CalcMaxBlockSize() const\r
-{\r
- VkDeviceSize result = 0;\r
- for(size_t i = m_Blocks.size(); i--; )\r
- {\r
- result = VMA_MAX(result, m_Blocks[i]->m_pMetadata->GetSize());\r
- if(result >= m_PreferredBlockSize)\r
- {\r
- break;\r
- }\r
- }\r
- return result;\r
-}\r
-\r
-void VmaBlockVector::Remove(VmaDeviceMemoryBlock* pBlock)\r
-{\r
- for(uint32_t blockIndex = 0; blockIndex < m_Blocks.size(); ++blockIndex)\r
- {\r
- if(m_Blocks[blockIndex] == pBlock)\r
- {\r
- VmaVectorRemove(m_Blocks, blockIndex);\r
- return;\r
- }\r
- }\r
- VMA_ASSERT(0);\r
-}\r
-\r
-void VmaBlockVector::IncrementallySortBlocks()\r
-{\r
- if(m_Algorithm != VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT)\r
- {\r
- // Bubble sort only until first swap.\r
- for(size_t i = 1; i < m_Blocks.size(); ++i)\r
- {\r
- if(m_Blocks[i - 1]->m_pMetadata->GetSumFreeSize() > m_Blocks[i]->m_pMetadata->GetSumFreeSize())\r
- {\r
- VMA_SWAP(m_Blocks[i - 1], m_Blocks[i]);\r
- return;\r
- }\r
- }\r
- }\r
-}\r
-\r
-VkResult VmaBlockVector::AllocateFromBlock(\r
- VmaDeviceMemoryBlock* pBlock,\r
- uint32_t currentFrameIndex,\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- VmaAllocationCreateFlags allocFlags,\r
- void* pUserData,\r
- VmaSuballocationType suballocType,\r
- uint32_t strategy,\r
- VmaAllocation* pAllocation)\r
-{\r
- VMA_ASSERT((allocFlags & VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT) == 0);\r
- const bool isUpperAddress = (allocFlags & VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT) != 0;\r
- const bool mapped = (allocFlags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0;\r
- const bool isUserDataString = (allocFlags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0;\r
-\r
- VmaAllocationRequest currRequest = {};\r
- if(pBlock->m_pMetadata->CreateAllocationRequest(\r
- currentFrameIndex,\r
- m_FrameInUseCount,\r
- m_BufferImageGranularity,\r
- size,\r
- alignment,\r
- isUpperAddress,\r
- suballocType,\r
- false, // canMakeOtherLost\r
- strategy,\r
- &currRequest))\r
- {\r
- // Allocate from pCurrBlock.\r
- VMA_ASSERT(currRequest.itemsToMakeLostCount == 0);\r
-\r
- if(mapped)\r
- {\r
- VkResult res = pBlock->Map(m_hAllocator, 1, VMA_NULL);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- }\r
- \r
- *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(currentFrameIndex, isUserDataString);\r
- pBlock->m_pMetadata->Alloc(currRequest, suballocType, size, *pAllocation);\r
- UpdateHasEmptyBlock();\r
- (*pAllocation)->InitBlockAllocation(\r
- pBlock,\r
- currRequest.offset,\r
- alignment,\r
- size,\r
- m_MemoryTypeIndex,\r
- suballocType,\r
- mapped,\r
- (allocFlags & VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT) != 0);\r
- VMA_HEAVY_ASSERT(pBlock->Validate());\r
- (*pAllocation)->SetUserData(m_hAllocator, pUserData);\r
- m_hAllocator->m_Budget.AddAllocation(m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex), size);\r
- if(VMA_DEBUG_INITIALIZE_ALLOCATIONS)\r
- {\r
- m_hAllocator->FillAllocation(*pAllocation, VMA_ALLOCATION_FILL_PATTERN_CREATED);\r
- }\r
- if(IsCorruptionDetectionEnabled())\r
- {\r
- VkResult res = pBlock->WriteMagicValueAroundAllocation(m_hAllocator, currRequest.offset, size);\r
- VMA_ASSERT(res == VK_SUCCESS && "Couldn't map block memory to write magic value.");\r
- }\r
- return VK_SUCCESS;\r
- }\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
-}\r
-\r
-VkResult VmaBlockVector::CreateBlock(VkDeviceSize blockSize, size_t* pNewBlockIndex)\r
-{\r
- VkMemoryAllocateInfo allocInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };\r
- allocInfo.memoryTypeIndex = m_MemoryTypeIndex;\r
- allocInfo.allocationSize = blockSize;\r
-\r
-#if VMA_BUFFER_DEVICE_ADDRESS\r
- // Every standalone block can potentially contain a buffer with VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT - always enable the feature.\r
- VkMemoryAllocateFlagsInfoKHR allocFlagsInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR };\r
- if(m_hAllocator->m_UseKhrBufferDeviceAddress)\r
- {\r
- allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;\r
- VmaPnextChainPushFront(&allocInfo, &allocFlagsInfo);\r
- }\r
-#endif // #if VMA_BUFFER_DEVICE_ADDRESS\r
-\r
- VkDeviceMemory mem = VK_NULL_HANDLE;\r
- VkResult res = m_hAllocator->AllocateVulkanMemory(&allocInfo, &mem);\r
- if(res < 0)\r
- {\r
- return res;\r
- }\r
-\r
- // New VkDeviceMemory successfully created.\r
-\r
- // Create new Allocation for it.\r
- VmaDeviceMemoryBlock* const pBlock = vma_new(m_hAllocator, VmaDeviceMemoryBlock)(m_hAllocator);\r
- pBlock->Init(\r
- m_hAllocator,\r
- m_hParentPool,\r
- m_MemoryTypeIndex,\r
- mem,\r
- allocInfo.allocationSize,\r
- m_NextBlockId++,\r
- m_Algorithm);\r
-\r
- m_Blocks.push_back(pBlock);\r
- if(pNewBlockIndex != VMA_NULL)\r
- {\r
- *pNewBlockIndex = m_Blocks.size() - 1;\r
- }\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaBlockVector::ApplyDefragmentationMovesCpu(\r
- class VmaBlockVectorDefragmentationContext* pDefragCtx,\r
- const VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves)\r
-{\r
- const size_t blockCount = m_Blocks.size();\r
- const bool isNonCoherent = m_hAllocator->IsMemoryTypeNonCoherent(m_MemoryTypeIndex);\r
-\r
- enum BLOCK_FLAG\r
- {\r
- BLOCK_FLAG_USED = 0x00000001,\r
- BLOCK_FLAG_MAPPED_FOR_DEFRAGMENTATION = 0x00000002,\r
- };\r
-\r
- struct BlockInfo\r
- {\r
- uint32_t flags;\r
- void* pMappedData;\r
- };\r
- VmaVector< BlockInfo, VmaStlAllocator<BlockInfo> >\r
- blockInfo(blockCount, BlockInfo(), VmaStlAllocator<BlockInfo>(m_hAllocator->GetAllocationCallbacks()));\r
- memset(blockInfo.data(), 0, blockCount * sizeof(BlockInfo));\r
-\r
- // Go over all moves. Mark blocks that are used with BLOCK_FLAG_USED.\r
- const size_t moveCount = moves.size();\r
- for(size_t moveIndex = 0; moveIndex < moveCount; ++moveIndex)\r
- {\r
- const VmaDefragmentationMove& move = moves[moveIndex];\r
- blockInfo[move.srcBlockIndex].flags |= BLOCK_FLAG_USED;\r
- blockInfo[move.dstBlockIndex].flags |= BLOCK_FLAG_USED;\r
- }\r
-\r
- VMA_ASSERT(pDefragCtx->res == VK_SUCCESS);\r
-\r
- // Go over all blocks. Get mapped pointer or map if necessary.\r
- for(size_t blockIndex = 0; pDefragCtx->res == VK_SUCCESS && blockIndex < blockCount; ++blockIndex)\r
- {\r
- BlockInfo& currBlockInfo = blockInfo[blockIndex];\r
- VmaDeviceMemoryBlock* pBlock = m_Blocks[blockIndex];\r
- if((currBlockInfo.flags & BLOCK_FLAG_USED) != 0)\r
- {\r
- currBlockInfo.pMappedData = pBlock->GetMappedData();\r
- // It is not originally mapped - map it.\r
- if(currBlockInfo.pMappedData == VMA_NULL)\r
- {\r
- pDefragCtx->res = pBlock->Map(m_hAllocator, 1, &currBlockInfo.pMappedData);\r
- if(pDefragCtx->res == VK_SUCCESS)\r
- {\r
- currBlockInfo.flags |= BLOCK_FLAG_MAPPED_FOR_DEFRAGMENTATION;\r
- }\r
- }\r
- }\r
- }\r
-\r
- // Go over all moves. Do actual data transfer.\r
- if(pDefragCtx->res == VK_SUCCESS)\r
- {\r
- const VkDeviceSize nonCoherentAtomSize = m_hAllocator->m_PhysicalDeviceProperties.limits.nonCoherentAtomSize;\r
- VkMappedMemoryRange memRange = { VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE };\r
-\r
- for(size_t moveIndex = 0; moveIndex < moveCount; ++moveIndex)\r
- {\r
- const VmaDefragmentationMove& move = moves[moveIndex];\r
-\r
- const BlockInfo& srcBlockInfo = blockInfo[move.srcBlockIndex];\r
- const BlockInfo& dstBlockInfo = blockInfo[move.dstBlockIndex];\r
-\r
- VMA_ASSERT(srcBlockInfo.pMappedData && dstBlockInfo.pMappedData);\r
-\r
- // Invalidate source.\r
- if(isNonCoherent)\r
- {\r
- VmaDeviceMemoryBlock* const pSrcBlock = m_Blocks[move.srcBlockIndex];\r
- memRange.memory = pSrcBlock->GetDeviceMemory();\r
- memRange.offset = VmaAlignDown(move.srcOffset, nonCoherentAtomSize);\r
- memRange.size = VMA_MIN(\r
- VmaAlignUp(move.size + (move.srcOffset - memRange.offset), nonCoherentAtomSize),\r
- pSrcBlock->m_pMetadata->GetSize() - memRange.offset);\r
- (*m_hAllocator->GetVulkanFunctions().vkInvalidateMappedMemoryRanges)(m_hAllocator->m_hDevice, 1, &memRange);\r
- }\r
-\r
- // THE PLACE WHERE ACTUAL DATA COPY HAPPENS.\r
- memmove(\r
- reinterpret_cast<char*>(dstBlockInfo.pMappedData) + move.dstOffset,\r
- reinterpret_cast<char*>(srcBlockInfo.pMappedData) + move.srcOffset,\r
- static_cast<size_t>(move.size));\r
-\r
- if(IsCorruptionDetectionEnabled())\r
- {\r
- VmaWriteMagicValue(dstBlockInfo.pMappedData, move.dstOffset - VMA_DEBUG_MARGIN);\r
- VmaWriteMagicValue(dstBlockInfo.pMappedData, move.dstOffset + move.size);\r
- }\r
-\r
- // Flush destination.\r
- if(isNonCoherent)\r
- {\r
- VmaDeviceMemoryBlock* const pDstBlock = m_Blocks[move.dstBlockIndex];\r
- memRange.memory = pDstBlock->GetDeviceMemory();\r
- memRange.offset = VmaAlignDown(move.dstOffset, nonCoherentAtomSize);\r
- memRange.size = VMA_MIN(\r
- VmaAlignUp(move.size + (move.dstOffset - memRange.offset), nonCoherentAtomSize),\r
- pDstBlock->m_pMetadata->GetSize() - memRange.offset);\r
- (*m_hAllocator->GetVulkanFunctions().vkFlushMappedMemoryRanges)(m_hAllocator->m_hDevice, 1, &memRange);\r
- }\r
- }\r
- }\r
-\r
- // Go over all blocks in reverse order. Unmap those that were mapped just for defragmentation.\r
- // Regardless of pCtx->res == VK_SUCCESS.\r
- for(size_t blockIndex = blockCount; blockIndex--; )\r
- {\r
- const BlockInfo& currBlockInfo = blockInfo[blockIndex];\r
- if((currBlockInfo.flags & BLOCK_FLAG_MAPPED_FOR_DEFRAGMENTATION) != 0)\r
- {\r
- VmaDeviceMemoryBlock* pBlock = m_Blocks[blockIndex];\r
- pBlock->Unmap(m_hAllocator, 1);\r
- }\r
- }\r
-}\r
-\r
-void VmaBlockVector::ApplyDefragmentationMovesGpu(\r
- class VmaBlockVectorDefragmentationContext* pDefragCtx,\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkCommandBuffer commandBuffer)\r
-{\r
- const size_t blockCount = m_Blocks.size();\r
-\r
- pDefragCtx->blockContexts.resize(blockCount);\r
- memset(pDefragCtx->blockContexts.data(), 0, blockCount * sizeof(VmaBlockDefragmentationContext));\r
-\r
- // Go over all moves. Mark blocks that are used with BLOCK_FLAG_USED.\r
- const size_t moveCount = moves.size();\r
- for(size_t moveIndex = 0; moveIndex < moveCount; ++moveIndex)\r
- {\r
- const VmaDefragmentationMove& move = moves[moveIndex];\r
-\r
- //if(move.type == VMA_ALLOCATION_TYPE_UNKNOWN)\r
- {\r
- // Old school move still require us to map the whole block\r
- pDefragCtx->blockContexts[move.srcBlockIndex].flags |= VmaBlockDefragmentationContext::BLOCK_FLAG_USED;\r
- pDefragCtx->blockContexts[move.dstBlockIndex].flags |= VmaBlockDefragmentationContext::BLOCK_FLAG_USED;\r
- }\r
- }\r
-\r
- VMA_ASSERT(pDefragCtx->res == VK_SUCCESS);\r
-\r
- // Go over all blocks. Create and bind buffer for whole block if necessary.\r
- {\r
- VkBufferCreateInfo bufCreateInfo;\r
- VmaFillGpuDefragmentationBufferCreateInfo(bufCreateInfo);\r
-\r
- for(size_t blockIndex = 0; pDefragCtx->res == VK_SUCCESS && blockIndex < blockCount; ++blockIndex)\r
- {\r
- VmaBlockDefragmentationContext& currBlockCtx = pDefragCtx->blockContexts[blockIndex];\r
- VmaDeviceMemoryBlock* pBlock = m_Blocks[blockIndex];\r
- if((currBlockCtx.flags & VmaBlockDefragmentationContext::BLOCK_FLAG_USED) != 0)\r
- {\r
- bufCreateInfo.size = pBlock->m_pMetadata->GetSize();\r
- pDefragCtx->res = (*m_hAllocator->GetVulkanFunctions().vkCreateBuffer)(\r
- m_hAllocator->m_hDevice, &bufCreateInfo, m_hAllocator->GetAllocationCallbacks(), &currBlockCtx.hBuffer);\r
- if(pDefragCtx->res == VK_SUCCESS)\r
- {\r
- pDefragCtx->res = (*m_hAllocator->GetVulkanFunctions().vkBindBufferMemory)(\r
- m_hAllocator->m_hDevice, currBlockCtx.hBuffer, pBlock->GetDeviceMemory(), 0);\r
- }\r
- }\r
- }\r
- }\r
-\r
- // Go over all moves. Post data transfer commands to command buffer.\r
- if(pDefragCtx->res == VK_SUCCESS)\r
- {\r
- for(size_t moveIndex = 0; moveIndex < moveCount; ++moveIndex)\r
- {\r
- const VmaDefragmentationMove& move = moves[moveIndex];\r
-\r
- const VmaBlockDefragmentationContext& srcBlockCtx = pDefragCtx->blockContexts[move.srcBlockIndex];\r
- const VmaBlockDefragmentationContext& dstBlockCtx = pDefragCtx->blockContexts[move.dstBlockIndex];\r
-\r
- VMA_ASSERT(srcBlockCtx.hBuffer && dstBlockCtx.hBuffer);\r
-\r
- VkBufferCopy region = {\r
- move.srcOffset,\r
- move.dstOffset,\r
- move.size };\r
- (*m_hAllocator->GetVulkanFunctions().vkCmdCopyBuffer)(\r
- commandBuffer, srcBlockCtx.hBuffer, dstBlockCtx.hBuffer, 1, ®ion);\r
- }\r
- }\r
-\r
- // Save buffers to defrag context for later destruction.\r
- if(pDefragCtx->res == VK_SUCCESS && moveCount > 0)\r
- {\r
- pDefragCtx->res = VK_NOT_READY;\r
- }\r
-}\r
-\r
-void VmaBlockVector::FreeEmptyBlocks(VmaDefragmentationStats* pDefragmentationStats)\r
-{\r
- for(size_t blockIndex = m_Blocks.size(); blockIndex--; )\r
- {\r
- VmaDeviceMemoryBlock* pBlock = m_Blocks[blockIndex];\r
- if(pBlock->m_pMetadata->IsEmpty())\r
- {\r
- if(m_Blocks.size() > m_MinBlockCount)\r
- {\r
- if(pDefragmentationStats != VMA_NULL)\r
- {\r
- ++pDefragmentationStats->deviceMemoryBlocksFreed;\r
- pDefragmentationStats->bytesFreed += pBlock->m_pMetadata->GetSize();\r
- }\r
-\r
- VmaVectorRemove(m_Blocks, blockIndex);\r
- pBlock->Destroy(m_hAllocator);\r
- vma_delete(m_hAllocator, pBlock);\r
- }\r
- else\r
- {\r
- break;\r
- }\r
- }\r
- }\r
- UpdateHasEmptyBlock();\r
-}\r
-\r
-void VmaBlockVector::UpdateHasEmptyBlock()\r
-{\r
- m_HasEmptyBlock = false;\r
- for(size_t index = 0, count = m_Blocks.size(); index < count; ++index)\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = m_Blocks[index];\r
- if(pBlock->m_pMetadata->IsEmpty())\r
- {\r
- m_HasEmptyBlock = true;\r
- break;\r
- }\r
- }\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-void VmaBlockVector::PrintDetailedMap(class VmaJsonWriter& json)\r
-{\r
- VmaMutexLockRead lock(m_Mutex, m_hAllocator->m_UseMutex);\r
-\r
- json.BeginObject();\r
-\r
- if(IsCustomPool())\r
- {\r
- const char* poolName = m_hParentPool->GetName();\r
- if(poolName != VMA_NULL && poolName[0] != '\0')\r
- {\r
- json.WriteString("Name");\r
- json.WriteString(poolName);\r
- }\r
-\r
- json.WriteString("MemoryTypeIndex");\r
- json.WriteNumber(m_MemoryTypeIndex);\r
-\r
- json.WriteString("BlockSize");\r
- json.WriteNumber(m_PreferredBlockSize);\r
-\r
- json.WriteString("BlockCount");\r
- json.BeginObject(true);\r
- if(m_MinBlockCount > 0)\r
- {\r
- json.WriteString("Min");\r
- json.WriteNumber((uint64_t)m_MinBlockCount);\r
- }\r
- if(m_MaxBlockCount < SIZE_MAX)\r
- {\r
- json.WriteString("Max");\r
- json.WriteNumber((uint64_t)m_MaxBlockCount);\r
- }\r
- json.WriteString("Cur");\r
- json.WriteNumber((uint64_t)m_Blocks.size());\r
- json.EndObject();\r
-\r
- if(m_FrameInUseCount > 0)\r
- {\r
- json.WriteString("FrameInUseCount");\r
- json.WriteNumber(m_FrameInUseCount);\r
- }\r
-\r
- if(m_Algorithm != 0)\r
- {\r
- json.WriteString("Algorithm");\r
- json.WriteString(VmaAlgorithmToStr(m_Algorithm));\r
- }\r
- }\r
- else\r
- {\r
- json.WriteString("PreferredBlockSize");\r
- json.WriteNumber(m_PreferredBlockSize);\r
- }\r
-\r
- json.WriteString("Blocks");\r
- json.BeginObject();\r
- for(size_t i = 0; i < m_Blocks.size(); ++i)\r
- {\r
- json.BeginString();\r
- json.ContinueString(m_Blocks[i]->GetId());\r
- json.EndString();\r
-\r
- m_Blocks[i]->m_pMetadata->PrintDetailedMap(json);\r
- }\r
- json.EndObject();\r
-\r
- json.EndObject();\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-void VmaBlockVector::Defragment(\r
- class VmaBlockVectorDefragmentationContext* pCtx,\r
- VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags,\r
- VkDeviceSize& maxCpuBytesToMove, uint32_t& maxCpuAllocationsToMove,\r
- VkDeviceSize& maxGpuBytesToMove, uint32_t& maxGpuAllocationsToMove,\r
- VkCommandBuffer commandBuffer)\r
-{\r
- pCtx->res = VK_SUCCESS;\r
- \r
- const VkMemoryPropertyFlags memPropFlags =\r
- m_hAllocator->m_MemProps.memoryTypes[m_MemoryTypeIndex].propertyFlags;\r
- const bool isHostVisible = (memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;\r
-\r
- const bool canDefragmentOnCpu = maxCpuBytesToMove > 0 && maxCpuAllocationsToMove > 0 &&\r
- isHostVisible;\r
- const bool canDefragmentOnGpu = maxGpuBytesToMove > 0 && maxGpuAllocationsToMove > 0 &&\r
- !IsCorruptionDetectionEnabled() &&\r
- ((1u << m_MemoryTypeIndex) & m_hAllocator->GetGpuDefragmentationMemoryTypeBits()) != 0;\r
-\r
- // There are options to defragment this memory type.\r
- if(canDefragmentOnCpu || canDefragmentOnGpu)\r
- {\r
- bool defragmentOnGpu;\r
- // There is only one option to defragment this memory type.\r
- if(canDefragmentOnGpu != canDefragmentOnCpu)\r
- {\r
- defragmentOnGpu = canDefragmentOnGpu;\r
- }\r
- // Both options are available: Heuristics to choose the best one.\r
- else\r
- {\r
- defragmentOnGpu = (memPropFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0 ||\r
- m_hAllocator->IsIntegratedGpu();\r
- }\r
-\r
- bool overlappingMoveSupported = !defragmentOnGpu;\r
-\r
- if(m_hAllocator->m_UseMutex)\r
- {\r
- if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL)\r
- {\r
- if(!m_Mutex.TryLockWrite())\r
- {\r
- pCtx->res = VK_ERROR_INITIALIZATION_FAILED;\r
- return;\r
- }\r
- }\r
- else\r
- {\r
- m_Mutex.LockWrite();\r
- pCtx->mutexLocked = true;\r
- }\r
- }\r
-\r
- pCtx->Begin(overlappingMoveSupported, flags);\r
-\r
- // Defragment.\r
-\r
- const VkDeviceSize maxBytesToMove = defragmentOnGpu ? maxGpuBytesToMove : maxCpuBytesToMove;\r
- const uint32_t maxAllocationsToMove = defragmentOnGpu ? maxGpuAllocationsToMove : maxCpuAllocationsToMove;\r
- pCtx->res = pCtx->GetAlgorithm()->Defragment(pCtx->defragmentationMoves, maxBytesToMove, maxAllocationsToMove, flags);\r
-\r
- // Accumulate statistics.\r
- if(pStats != VMA_NULL)\r
- {\r
- const VkDeviceSize bytesMoved = pCtx->GetAlgorithm()->GetBytesMoved();\r
- const uint32_t allocationsMoved = pCtx->GetAlgorithm()->GetAllocationsMoved();\r
- pStats->bytesMoved += bytesMoved;\r
- pStats->allocationsMoved += allocationsMoved;\r
- VMA_ASSERT(bytesMoved <= maxBytesToMove);\r
- VMA_ASSERT(allocationsMoved <= maxAllocationsToMove);\r
- if(defragmentOnGpu)\r
- {\r
- maxGpuBytesToMove -= bytesMoved;\r
- maxGpuAllocationsToMove -= allocationsMoved;\r
- }\r
- else\r
- {\r
- maxCpuBytesToMove -= bytesMoved;\r
- maxCpuAllocationsToMove -= allocationsMoved;\r
- }\r
- }\r
-\r
- if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL)\r
- {\r
- if(m_hAllocator->m_UseMutex)\r
- m_Mutex.UnlockWrite();\r
- \r
- if(pCtx->res >= VK_SUCCESS && !pCtx->defragmentationMoves.empty())\r
- pCtx->res = VK_NOT_READY;\r
-\r
- return;\r
- }\r
- \r
- if(pCtx->res >= VK_SUCCESS)\r
- {\r
- if(defragmentOnGpu)\r
- {\r
- ApplyDefragmentationMovesGpu(pCtx, pCtx->defragmentationMoves, commandBuffer);\r
- }\r
- else\r
- {\r
- ApplyDefragmentationMovesCpu(pCtx, pCtx->defragmentationMoves);\r
- }\r
- }\r
- }\r
-}\r
-\r
-void VmaBlockVector::DefragmentationEnd(\r
- class VmaBlockVectorDefragmentationContext* pCtx,\r
- uint32_t flags,\r
- VmaDefragmentationStats* pStats)\r
-{\r
- if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL && m_hAllocator->m_UseMutex)\r
- {\r
- VMA_ASSERT(pCtx->mutexLocked == false);\r
-\r
- // Incremental defragmentation doesn't hold the lock, so when we enter here we don't actually have any\r
- // lock protecting us. Since we mutate state here, we have to take the lock out now\r
- m_Mutex.LockWrite();\r
- pCtx->mutexLocked = true;\r
- }\r
-\r
- // If the mutex isn't locked we didn't do any work and there is nothing to delete.\r
- if(pCtx->mutexLocked || !m_hAllocator->m_UseMutex)\r
- {\r
- // Destroy buffers.\r
- for(size_t blockIndex = pCtx->blockContexts.size(); blockIndex--;)\r
- {\r
- VmaBlockDefragmentationContext &blockCtx = pCtx->blockContexts[blockIndex];\r
- if(blockCtx.hBuffer)\r
- {\r
- (*m_hAllocator->GetVulkanFunctions().vkDestroyBuffer)(m_hAllocator->m_hDevice, blockCtx.hBuffer, m_hAllocator->GetAllocationCallbacks());\r
- }\r
- }\r
-\r
- if(pCtx->res >= VK_SUCCESS)\r
- {\r
- FreeEmptyBlocks(pStats);\r
- }\r
- }\r
-\r
- if(pCtx->mutexLocked)\r
- {\r
- VMA_ASSERT(m_hAllocator->m_UseMutex);\r
- m_Mutex.UnlockWrite();\r
- }\r
-}\r
-\r
-uint32_t VmaBlockVector::ProcessDefragmentations(\r
- class VmaBlockVectorDefragmentationContext *pCtx,\r
- VmaDefragmentationPassMoveInfo* pMove, uint32_t maxMoves)\r
-{\r
- VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex);\r
- \r
- const uint32_t moveCount = std::min(uint32_t(pCtx->defragmentationMoves.size()) - pCtx->defragmentationMovesProcessed, maxMoves);\r
-\r
- for(uint32_t i = 0; i < moveCount; ++ i)\r
- {\r
- VmaDefragmentationMove& move = pCtx->defragmentationMoves[pCtx->defragmentationMovesProcessed + i];\r
-\r
- pMove->allocation = move.hAllocation;\r
- pMove->memory = move.pDstBlock->GetDeviceMemory();\r
- pMove->offset = move.dstOffset;\r
-\r
- ++ pMove;\r
- }\r
-\r
- pCtx->defragmentationMovesProcessed += moveCount;\r
-\r
- return moveCount;\r
-}\r
-\r
-void VmaBlockVector::CommitDefragmentations(\r
- class VmaBlockVectorDefragmentationContext *pCtx,\r
- VmaDefragmentationStats* pStats)\r
-{\r
- VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex);\r
- \r
- for(uint32_t i = pCtx->defragmentationMovesCommitted; i < pCtx->defragmentationMovesProcessed; ++ i)\r
- {\r
- const VmaDefragmentationMove &move = pCtx->defragmentationMoves[i];\r
-\r
- move.pSrcBlock->m_pMetadata->FreeAtOffset(move.srcOffset);\r
- move.hAllocation->ChangeBlockAllocation(m_hAllocator, move.pDstBlock, move.dstOffset);\r
- }\r
-\r
- pCtx->defragmentationMovesCommitted = pCtx->defragmentationMovesProcessed;\r
- FreeEmptyBlocks(pStats);\r
-}\r
-\r
-size_t VmaBlockVector::CalcAllocationCount() const\r
-{\r
- size_t result = 0;\r
- for(size_t i = 0; i < m_Blocks.size(); ++i)\r
- {\r
- result += m_Blocks[i]->m_pMetadata->GetAllocationCount();\r
- }\r
- return result;\r
-}\r
-\r
-bool VmaBlockVector::IsBufferImageGranularityConflictPossible() const\r
-{\r
- if(m_BufferImageGranularity == 1)\r
- {\r
- return false;\r
- }\r
- VmaSuballocationType lastSuballocType = VMA_SUBALLOCATION_TYPE_FREE;\r
- for(size_t i = 0, count = m_Blocks.size(); i < count; ++i)\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = m_Blocks[i];\r
- VMA_ASSERT(m_Algorithm == 0);\r
- VmaBlockMetadata_Generic* const pMetadata = (VmaBlockMetadata_Generic*)pBlock->m_pMetadata;\r
- if(pMetadata->IsBufferImageGranularityConflictPossible(m_BufferImageGranularity, lastSuballocType))\r
- {\r
- return true;\r
- }\r
- }\r
- return false;\r
-}\r
-\r
-void VmaBlockVector::MakePoolAllocationsLost(\r
- uint32_t currentFrameIndex,\r
- size_t* pLostAllocationCount)\r
-{\r
- VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex);\r
- size_t lostAllocationCount = 0;\r
- for(uint32_t blockIndex = 0; blockIndex < m_Blocks.size(); ++blockIndex)\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pBlock);\r
- lostAllocationCount += pBlock->m_pMetadata->MakeAllocationsLost(currentFrameIndex, m_FrameInUseCount);\r
- }\r
- if(pLostAllocationCount != VMA_NULL)\r
- {\r
- *pLostAllocationCount = lostAllocationCount;\r
- }\r
-}\r
-\r
-VkResult VmaBlockVector::CheckCorruption()\r
-{\r
- if(!IsCorruptionDetectionEnabled())\r
- {\r
- return VK_ERROR_FEATURE_NOT_PRESENT;\r
- }\r
-\r
- VmaMutexLockRead lock(m_Mutex, m_hAllocator->m_UseMutex);\r
- for(uint32_t blockIndex = 0; blockIndex < m_Blocks.size(); ++blockIndex)\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pBlock);\r
- VkResult res = pBlock->CheckCorruption(m_hAllocator);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- }\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaBlockVector::AddStats(VmaStats* pStats)\r
-{\r
- const uint32_t memTypeIndex = m_MemoryTypeIndex;\r
- const uint32_t memHeapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(memTypeIndex);\r
-\r
- VmaMutexLockRead lock(m_Mutex, m_hAllocator->m_UseMutex);\r
-\r
- for(uint32_t blockIndex = 0; blockIndex < m_Blocks.size(); ++blockIndex)\r
- {\r
- const VmaDeviceMemoryBlock* const pBlock = m_Blocks[blockIndex];\r
- VMA_ASSERT(pBlock);\r
- VMA_HEAVY_ASSERT(pBlock->Validate());\r
- VmaStatInfo allocationStatInfo;\r
- pBlock->m_pMetadata->CalcAllocationStatInfo(allocationStatInfo);\r
- VmaAddStatInfo(pStats->total, allocationStatInfo);\r
- VmaAddStatInfo(pStats->memoryType[memTypeIndex], allocationStatInfo);\r
- VmaAddStatInfo(pStats->memoryHeap[memHeapIndex], allocationStatInfo);\r
- }\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaDefragmentationAlgorithm_Generic members definition\r
-\r
-VmaDefragmentationAlgorithm_Generic::VmaDefragmentationAlgorithm_Generic(\r
- VmaAllocator hAllocator,\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currentFrameIndex,\r
- bool overlappingMoveSupported) :\r
- VmaDefragmentationAlgorithm(hAllocator, pBlockVector, currentFrameIndex),\r
- m_AllocationCount(0),\r
- m_AllAllocations(false),\r
- m_BytesMoved(0),\r
- m_AllocationsMoved(0),\r
- m_Blocks(VmaStlAllocator<BlockInfo*>(hAllocator->GetAllocationCallbacks()))\r
-{\r
- // Create block info for each block.\r
- const size_t blockCount = m_pBlockVector->m_Blocks.size();\r
- for(size_t blockIndex = 0; blockIndex < blockCount; ++blockIndex)\r
- {\r
- BlockInfo* pBlockInfo = vma_new(m_hAllocator, BlockInfo)(m_hAllocator->GetAllocationCallbacks());\r
- pBlockInfo->m_OriginalBlockIndex = blockIndex;\r
- pBlockInfo->m_pBlock = m_pBlockVector->m_Blocks[blockIndex];\r
- m_Blocks.push_back(pBlockInfo);\r
- }\r
-\r
- // Sort them by m_pBlock pointer value.\r
- VMA_SORT(m_Blocks.begin(), m_Blocks.end(), BlockPointerLess());\r
-}\r
-\r
-VmaDefragmentationAlgorithm_Generic::~VmaDefragmentationAlgorithm_Generic()\r
-{\r
- for(size_t i = m_Blocks.size(); i--; )\r
- {\r
- vma_delete(m_hAllocator, m_Blocks[i]);\r
- }\r
-}\r
-\r
-void VmaDefragmentationAlgorithm_Generic::AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged)\r
-{\r
- // Now as we are inside VmaBlockVector::m_Mutex, we can make final check if this allocation was not lost.\r
- if(hAlloc->GetLastUseFrameIndex() != VMA_FRAME_INDEX_LOST)\r
- {\r
- VmaDeviceMemoryBlock* pBlock = hAlloc->GetBlock();\r
- BlockInfoVector::iterator it = VmaBinaryFindFirstNotLess(m_Blocks.begin(), m_Blocks.end(), pBlock, BlockPointerLess());\r
- if(it != m_Blocks.end() && (*it)->m_pBlock == pBlock)\r
- {\r
- AllocationInfo allocInfo = AllocationInfo(hAlloc, pChanged);\r
- (*it)->m_Allocations.push_back(allocInfo);\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0);\r
- }\r
-\r
- ++m_AllocationCount;\r
- }\r
-}\r
-\r
-VkResult VmaDefragmentationAlgorithm_Generic::DefragmentRound(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- bool freeOldAllocations)\r
-{\r
- if(m_Blocks.empty())\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- // This is a choice based on research.\r
- // Option 1:\r
- uint32_t strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_TIME_BIT;\r
- // Option 2:\r
- //uint32_t strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_MEMORY_BIT;\r
- // Option 3:\r
- //uint32_t strategy = VMA_ALLOCATION_CREATE_STRATEGY_MIN_FRAGMENTATION_BIT;\r
-\r
- size_t srcBlockMinIndex = 0;\r
- // When FAST_ALGORITHM, move allocations from only last out of blocks that contain non-movable allocations.\r
- /*\r
- if(m_AlgorithmFlags & VMA_DEFRAGMENTATION_FAST_ALGORITHM_BIT)\r
- {\r
- const size_t blocksWithNonMovableCount = CalcBlocksWithNonMovableCount();\r
- if(blocksWithNonMovableCount > 0)\r
- {\r
- srcBlockMinIndex = blocksWithNonMovableCount - 1;\r
- }\r
- }\r
- */\r
-\r
- size_t srcBlockIndex = m_Blocks.size() - 1;\r
- size_t srcAllocIndex = SIZE_MAX;\r
- for(;;)\r
- {\r
- // 1. Find next allocation to move.\r
- // 1.1. Start from last to first m_Blocks - they are sorted from most "destination" to most "source".\r
- // 1.2. Then start from last to first m_Allocations.\r
- while(srcAllocIndex >= m_Blocks[srcBlockIndex]->m_Allocations.size())\r
- {\r
- if(m_Blocks[srcBlockIndex]->m_Allocations.empty())\r
- {\r
- // Finished: no more allocations to process.\r
- if(srcBlockIndex == srcBlockMinIndex)\r
- {\r
- return VK_SUCCESS;\r
- }\r
- else\r
- {\r
- --srcBlockIndex;\r
- srcAllocIndex = SIZE_MAX;\r
- }\r
- }\r
- else\r
- {\r
- srcAllocIndex = m_Blocks[srcBlockIndex]->m_Allocations.size() - 1;\r
- }\r
- }\r
- \r
- BlockInfo* pSrcBlockInfo = m_Blocks[srcBlockIndex];\r
- AllocationInfo& allocInfo = pSrcBlockInfo->m_Allocations[srcAllocIndex];\r
-\r
- const VkDeviceSize size = allocInfo.m_hAllocation->GetSize();\r
- const VkDeviceSize srcOffset = allocInfo.m_hAllocation->GetOffset();\r
- const VkDeviceSize alignment = allocInfo.m_hAllocation->GetAlignment();\r
- const VmaSuballocationType suballocType = allocInfo.m_hAllocation->GetSuballocationType();\r
-\r
- // 2. Try to find new place for this allocation in preceding or current block.\r
- for(size_t dstBlockIndex = 0; dstBlockIndex <= srcBlockIndex; ++dstBlockIndex)\r
- {\r
- BlockInfo* pDstBlockInfo = m_Blocks[dstBlockIndex];\r
- VmaAllocationRequest dstAllocRequest;\r
- if(pDstBlockInfo->m_pBlock->m_pMetadata->CreateAllocationRequest(\r
- m_CurrentFrameIndex,\r
- m_pBlockVector->GetFrameInUseCount(),\r
- m_pBlockVector->GetBufferImageGranularity(),\r
- size,\r
- alignment,\r
- false, // upperAddress\r
- suballocType,\r
- false, // canMakeOtherLost\r
- strategy,\r
- &dstAllocRequest) &&\r
- MoveMakesSense(\r
- dstBlockIndex, dstAllocRequest.offset, srcBlockIndex, srcOffset))\r
- {\r
- VMA_ASSERT(dstAllocRequest.itemsToMakeLostCount == 0);\r
-\r
- // Reached limit on number of allocations or bytes to move.\r
- if((m_AllocationsMoved + 1 > maxAllocationsToMove) ||\r
- (m_BytesMoved + size > maxBytesToMove))\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- VmaDefragmentationMove move = {};\r
- move.srcBlockIndex = pSrcBlockInfo->m_OriginalBlockIndex;\r
- move.dstBlockIndex = pDstBlockInfo->m_OriginalBlockIndex;\r
- move.srcOffset = srcOffset;\r
- move.dstOffset = dstAllocRequest.offset;\r
- move.size = size;\r
- move.hAllocation = allocInfo.m_hAllocation;\r
- move.pSrcBlock = pSrcBlockInfo->m_pBlock;\r
- move.pDstBlock = pDstBlockInfo->m_pBlock;\r
-\r
- moves.push_back(move);\r
-\r
- pDstBlockInfo->m_pBlock->m_pMetadata->Alloc(\r
- dstAllocRequest,\r
- suballocType,\r
- size,\r
- allocInfo.m_hAllocation);\r
-\r
- if(freeOldAllocations)\r
- {\r
- pSrcBlockInfo->m_pBlock->m_pMetadata->FreeAtOffset(srcOffset);\r
- allocInfo.m_hAllocation->ChangeBlockAllocation(m_hAllocator, pDstBlockInfo->m_pBlock, dstAllocRequest.offset);\r
- }\r
- \r
- if(allocInfo.m_pChanged != VMA_NULL)\r
- {\r
- *allocInfo.m_pChanged = VK_TRUE;\r
- }\r
-\r
- ++m_AllocationsMoved;\r
- m_BytesMoved += size;\r
-\r
- VmaVectorRemove(pSrcBlockInfo->m_Allocations, srcAllocIndex);\r
-\r
- break;\r
- }\r
- }\r
-\r
- // If not processed, this allocInfo remains in pBlockInfo->m_Allocations for next round.\r
-\r
- if(srcAllocIndex > 0)\r
- {\r
- --srcAllocIndex;\r
- }\r
- else\r
- {\r
- if(srcBlockIndex > 0)\r
- {\r
- --srcBlockIndex;\r
- srcAllocIndex = SIZE_MAX;\r
- }\r
- else\r
- {\r
- return VK_SUCCESS;\r
- }\r
- }\r
- }\r
-}\r
-\r
-size_t VmaDefragmentationAlgorithm_Generic::CalcBlocksWithNonMovableCount() const\r
-{\r
- size_t result = 0;\r
- for(size_t i = 0; i < m_Blocks.size(); ++i)\r
- {\r
- if(m_Blocks[i]->m_HasNonMovableAllocations)\r
- {\r
- ++result;\r
- }\r
- }\r
- return result;\r
-}\r
-\r
-VkResult VmaDefragmentationAlgorithm_Generic::Defragment(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- VmaDefragmentationFlags flags)\r
-{\r
- if(!m_AllAllocations && m_AllocationCount == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- const size_t blockCount = m_Blocks.size();\r
- for(size_t blockIndex = 0; blockIndex < blockCount; ++blockIndex)\r
- {\r
- BlockInfo* pBlockInfo = m_Blocks[blockIndex];\r
-\r
- if(m_AllAllocations)\r
- {\r
- VmaBlockMetadata_Generic* pMetadata = (VmaBlockMetadata_Generic*)pBlockInfo->m_pBlock->m_pMetadata;\r
- for(VmaSuballocationList::const_iterator it = pMetadata->m_Suballocations.begin();\r
- it != pMetadata->m_Suballocations.end();\r
- ++it)\r
- {\r
- if(it->type != VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- AllocationInfo allocInfo = AllocationInfo(it->hAllocation, VMA_NULL);\r
- pBlockInfo->m_Allocations.push_back(allocInfo);\r
- }\r
- }\r
- }\r
-\r
- pBlockInfo->CalcHasNonMovableAllocations();\r
- \r
- // This is a choice based on research.\r
- // Option 1:\r
- pBlockInfo->SortAllocationsByOffsetDescending();\r
- // Option 2:\r
- //pBlockInfo->SortAllocationsBySizeDescending();\r
- }\r
-\r
- // Sort m_Blocks this time by the main criterium, from most "destination" to most "source" blocks.\r
- VMA_SORT(m_Blocks.begin(), m_Blocks.end(), BlockInfoCompareMoveDestination());\r
-\r
- // This is a choice based on research.\r
- const uint32_t roundCount = 2;\r
-\r
- // Execute defragmentation rounds (the main part).\r
- VkResult result = VK_SUCCESS;\r
- for(uint32_t round = 0; (round < roundCount) && (result == VK_SUCCESS); ++round)\r
- {\r
- result = DefragmentRound(moves, maxBytesToMove, maxAllocationsToMove, !(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL));\r
- }\r
-\r
- return result;\r
-}\r
-\r
-bool VmaDefragmentationAlgorithm_Generic::MoveMakesSense(\r
- size_t dstBlockIndex, VkDeviceSize dstOffset,\r
- size_t srcBlockIndex, VkDeviceSize srcOffset)\r
-{\r
- if(dstBlockIndex < srcBlockIndex)\r
- {\r
- return true;\r
- }\r
- if(dstBlockIndex > srcBlockIndex)\r
- {\r
- return false;\r
- }\r
- if(dstOffset < srcOffset)\r
- {\r
- return true;\r
- }\r
- return false;\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaDefragmentationAlgorithm_Fast\r
-\r
-VmaDefragmentationAlgorithm_Fast::VmaDefragmentationAlgorithm_Fast(\r
- VmaAllocator hAllocator,\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currentFrameIndex,\r
- bool overlappingMoveSupported) :\r
- VmaDefragmentationAlgorithm(hAllocator, pBlockVector, currentFrameIndex),\r
- m_OverlappingMoveSupported(overlappingMoveSupported),\r
- m_AllocationCount(0),\r
- m_AllAllocations(false),\r
- m_BytesMoved(0),\r
- m_AllocationsMoved(0),\r
- m_BlockInfos(VmaStlAllocator<BlockInfo>(hAllocator->GetAllocationCallbacks()))\r
-{\r
- VMA_ASSERT(VMA_DEBUG_MARGIN == 0);\r
-\r
-}\r
-\r
-VmaDefragmentationAlgorithm_Fast::~VmaDefragmentationAlgorithm_Fast()\r
-{\r
-}\r
-\r
-VkResult VmaDefragmentationAlgorithm_Fast::Defragment(\r
- VmaVector< VmaDefragmentationMove, VmaStlAllocator<VmaDefragmentationMove> >& moves,\r
- VkDeviceSize maxBytesToMove,\r
- uint32_t maxAllocationsToMove,\r
- VmaDefragmentationFlags flags)\r
-{\r
- VMA_ASSERT(m_AllAllocations || m_pBlockVector->CalcAllocationCount() == m_AllocationCount);\r
-\r
- const size_t blockCount = m_pBlockVector->GetBlockCount();\r
- if(blockCount == 0 || maxBytesToMove == 0 || maxAllocationsToMove == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- PreprocessMetadata();\r
-\r
- // Sort blocks in order from most destination.\r
-\r
- m_BlockInfos.resize(blockCount);\r
- for(size_t i = 0; i < blockCount; ++i)\r
- {\r
- m_BlockInfos[i].origBlockIndex = i;\r
- }\r
-\r
- VMA_SORT(m_BlockInfos.begin(), m_BlockInfos.end(), [this](const BlockInfo& lhs, const BlockInfo& rhs) -> bool {\r
- return m_pBlockVector->GetBlock(lhs.origBlockIndex)->m_pMetadata->GetSumFreeSize() <\r
- m_pBlockVector->GetBlock(rhs.origBlockIndex)->m_pMetadata->GetSumFreeSize();\r
- });\r
-\r
- // THE MAIN ALGORITHM\r
-\r
- FreeSpaceDatabase freeSpaceDb;\r
-\r
- size_t dstBlockInfoIndex = 0;\r
- size_t dstOrigBlockIndex = m_BlockInfos[dstBlockInfoIndex].origBlockIndex;\r
- VmaDeviceMemoryBlock* pDstBlock = m_pBlockVector->GetBlock(dstOrigBlockIndex);\r
- VmaBlockMetadata_Generic* pDstMetadata = (VmaBlockMetadata_Generic*)pDstBlock->m_pMetadata;\r
- VkDeviceSize dstBlockSize = pDstMetadata->GetSize();\r
- VkDeviceSize dstOffset = 0;\r
-\r
- bool end = false;\r
- for(size_t srcBlockInfoIndex = 0; !end && srcBlockInfoIndex < blockCount; ++srcBlockInfoIndex)\r
- {\r
- const size_t srcOrigBlockIndex = m_BlockInfos[srcBlockInfoIndex].origBlockIndex;\r
- VmaDeviceMemoryBlock* const pSrcBlock = m_pBlockVector->GetBlock(srcOrigBlockIndex);\r
- VmaBlockMetadata_Generic* const pSrcMetadata = (VmaBlockMetadata_Generic*)pSrcBlock->m_pMetadata;\r
- for(VmaSuballocationList::iterator srcSuballocIt = pSrcMetadata->m_Suballocations.begin();\r
- !end && srcSuballocIt != pSrcMetadata->m_Suballocations.end(); )\r
- {\r
- VmaAllocation_T* const pAlloc = srcSuballocIt->hAllocation;\r
- const VkDeviceSize srcAllocAlignment = pAlloc->GetAlignment();\r
- const VkDeviceSize srcAllocSize = srcSuballocIt->size;\r
- if(m_AllocationsMoved == maxAllocationsToMove ||\r
- m_BytesMoved + srcAllocSize > maxBytesToMove)\r
- {\r
- end = true;\r
- break;\r
- }\r
- const VkDeviceSize srcAllocOffset = srcSuballocIt->offset;\r
-\r
- VmaDefragmentationMove move = {};\r
- // Try to place it in one of free spaces from the database.\r
- size_t freeSpaceInfoIndex;\r
- VkDeviceSize dstAllocOffset;\r
- if(freeSpaceDb.Fetch(srcAllocAlignment, srcAllocSize,\r
- freeSpaceInfoIndex, dstAllocOffset))\r
- {\r
- size_t freeSpaceOrigBlockIndex = m_BlockInfos[freeSpaceInfoIndex].origBlockIndex;\r
- VmaDeviceMemoryBlock* pFreeSpaceBlock = m_pBlockVector->GetBlock(freeSpaceOrigBlockIndex);\r
- VmaBlockMetadata_Generic* pFreeSpaceMetadata = (VmaBlockMetadata_Generic*)pFreeSpaceBlock->m_pMetadata;\r
-\r
- // Same block\r
- if(freeSpaceInfoIndex == srcBlockInfoIndex)\r
- {\r
- VMA_ASSERT(dstAllocOffset <= srcAllocOffset);\r
-\r
- // MOVE OPTION 1: Move the allocation inside the same block by decreasing offset.\r
-\r
- VmaSuballocation suballoc = *srcSuballocIt;\r
- suballoc.offset = dstAllocOffset;\r
- suballoc.hAllocation->ChangeOffset(dstAllocOffset);\r
- m_BytesMoved += srcAllocSize;\r
- ++m_AllocationsMoved;\r
- \r
- VmaSuballocationList::iterator nextSuballocIt = srcSuballocIt;\r
- ++nextSuballocIt;\r
- pSrcMetadata->m_Suballocations.erase(srcSuballocIt);\r
- srcSuballocIt = nextSuballocIt;\r
-\r
- InsertSuballoc(pFreeSpaceMetadata, suballoc);\r
-\r
- move.srcBlockIndex = srcOrigBlockIndex;\r
- move.dstBlockIndex = freeSpaceOrigBlockIndex;\r
- move.srcOffset = srcAllocOffset;\r
- move.dstOffset = dstAllocOffset;\r
- move.size = srcAllocSize;\r
- \r
- moves.push_back(move);\r
- }\r
- // Different block\r
- else\r
- {\r
- // MOVE OPTION 2: Move the allocation to a different block.\r
-\r
- VMA_ASSERT(freeSpaceInfoIndex < srcBlockInfoIndex);\r
-\r
- VmaSuballocation suballoc = *srcSuballocIt;\r
- suballoc.offset = dstAllocOffset;\r
- suballoc.hAllocation->ChangeBlockAllocation(m_hAllocator, pFreeSpaceBlock, dstAllocOffset);\r
- m_BytesMoved += srcAllocSize;\r
- ++m_AllocationsMoved;\r
-\r
- VmaSuballocationList::iterator nextSuballocIt = srcSuballocIt;\r
- ++nextSuballocIt;\r
- pSrcMetadata->m_Suballocations.erase(srcSuballocIt);\r
- srcSuballocIt = nextSuballocIt;\r
-\r
- InsertSuballoc(pFreeSpaceMetadata, suballoc);\r
-\r
- move.srcBlockIndex = srcOrigBlockIndex;\r
- move.dstBlockIndex = freeSpaceOrigBlockIndex;\r
- move.srcOffset = srcAllocOffset;\r
- move.dstOffset = dstAllocOffset;\r
- move.size = srcAllocSize;\r
- \r
- moves.push_back(move);\r
- }\r
- }\r
- else\r
- {\r
- dstAllocOffset = VmaAlignUp(dstOffset, srcAllocAlignment);\r
-\r
- // If the allocation doesn't fit before the end of dstBlock, forward to next block.\r
- while(dstBlockInfoIndex < srcBlockInfoIndex &&\r
- dstAllocOffset + srcAllocSize > dstBlockSize)\r
- {\r
- // But before that, register remaining free space at the end of dst block.\r
- freeSpaceDb.Register(dstBlockInfoIndex, dstOffset, dstBlockSize - dstOffset);\r
-\r
- ++dstBlockInfoIndex;\r
- dstOrigBlockIndex = m_BlockInfos[dstBlockInfoIndex].origBlockIndex;\r
- pDstBlock = m_pBlockVector->GetBlock(dstOrigBlockIndex);\r
- pDstMetadata = (VmaBlockMetadata_Generic*)pDstBlock->m_pMetadata;\r
- dstBlockSize = pDstMetadata->GetSize();\r
- dstOffset = 0;\r
- dstAllocOffset = 0;\r
- }\r
-\r
- // Same block\r
- if(dstBlockInfoIndex == srcBlockInfoIndex)\r
- {\r
- VMA_ASSERT(dstAllocOffset <= srcAllocOffset);\r
-\r
- const bool overlap = dstAllocOffset + srcAllocSize > srcAllocOffset;\r
-\r
- bool skipOver = overlap;\r
- if(overlap && m_OverlappingMoveSupported && dstAllocOffset < srcAllocOffset)\r
- {\r
- // If destination and source place overlap, skip if it would move it\r
- // by only < 1/64 of its size.\r
- skipOver = (srcAllocOffset - dstAllocOffset) * 64 < srcAllocSize;\r
- }\r
-\r
- if(skipOver)\r
- {\r
- freeSpaceDb.Register(dstBlockInfoIndex, dstOffset, srcAllocOffset - dstOffset);\r
-\r
- dstOffset = srcAllocOffset + srcAllocSize;\r
- ++srcSuballocIt;\r
- }\r
- // MOVE OPTION 1: Move the allocation inside the same block by decreasing offset.\r
- else\r
- {\r
- srcSuballocIt->offset = dstAllocOffset;\r
- srcSuballocIt->hAllocation->ChangeOffset(dstAllocOffset);\r
- dstOffset = dstAllocOffset + srcAllocSize;\r
- m_BytesMoved += srcAllocSize;\r
- ++m_AllocationsMoved;\r
- ++srcSuballocIt;\r
- \r
- move.srcBlockIndex = srcOrigBlockIndex;\r
- move.dstBlockIndex = dstOrigBlockIndex;\r
- move.srcOffset = srcAllocOffset;\r
- move.dstOffset = dstAllocOffset;\r
- move.size = srcAllocSize;\r
- \r
- moves.push_back(move);\r
- }\r
- }\r
- // Different block\r
- else\r
- {\r
- // MOVE OPTION 2: Move the allocation to a different block.\r
-\r
- VMA_ASSERT(dstBlockInfoIndex < srcBlockInfoIndex);\r
- VMA_ASSERT(dstAllocOffset + srcAllocSize <= dstBlockSize);\r
-\r
- VmaSuballocation suballoc = *srcSuballocIt;\r
- suballoc.offset = dstAllocOffset;\r
- suballoc.hAllocation->ChangeBlockAllocation(m_hAllocator, pDstBlock, dstAllocOffset);\r
- dstOffset = dstAllocOffset + srcAllocSize;\r
- m_BytesMoved += srcAllocSize;\r
- ++m_AllocationsMoved;\r
-\r
- VmaSuballocationList::iterator nextSuballocIt = srcSuballocIt;\r
- ++nextSuballocIt;\r
- pSrcMetadata->m_Suballocations.erase(srcSuballocIt);\r
- srcSuballocIt = nextSuballocIt;\r
-\r
- pDstMetadata->m_Suballocations.push_back(suballoc);\r
-\r
- move.srcBlockIndex = srcOrigBlockIndex;\r
- move.dstBlockIndex = dstOrigBlockIndex;\r
- move.srcOffset = srcAllocOffset;\r
- move.dstOffset = dstAllocOffset;\r
- move.size = srcAllocSize;\r
- \r
- moves.push_back(move);\r
- }\r
- }\r
- }\r
- }\r
-\r
- m_BlockInfos.clear();\r
- \r
- PostprocessMetadata();\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaDefragmentationAlgorithm_Fast::PreprocessMetadata()\r
-{\r
- const size_t blockCount = m_pBlockVector->GetBlockCount();\r
- for(size_t blockIndex = 0; blockIndex < blockCount; ++blockIndex)\r
- {\r
- VmaBlockMetadata_Generic* const pMetadata =\r
- (VmaBlockMetadata_Generic*)m_pBlockVector->GetBlock(blockIndex)->m_pMetadata;\r
- pMetadata->m_FreeCount = 0;\r
- pMetadata->m_SumFreeSize = pMetadata->GetSize();\r
- pMetadata->m_FreeSuballocationsBySize.clear();\r
- for(VmaSuballocationList::iterator it = pMetadata->m_Suballocations.begin();\r
- it != pMetadata->m_Suballocations.end(); )\r
- {\r
- if(it->type == VMA_SUBALLOCATION_TYPE_FREE)\r
- {\r
- VmaSuballocationList::iterator nextIt = it;\r
- ++nextIt;\r
- pMetadata->m_Suballocations.erase(it);\r
- it = nextIt;\r
- }\r
- else\r
- {\r
- ++it;\r
- }\r
- }\r
- }\r
-}\r
-\r
-void VmaDefragmentationAlgorithm_Fast::PostprocessMetadata()\r
-{\r
- const size_t blockCount = m_pBlockVector->GetBlockCount();\r
- for(size_t blockIndex = 0; blockIndex < blockCount; ++blockIndex)\r
- {\r
- VmaBlockMetadata_Generic* const pMetadata =\r
- (VmaBlockMetadata_Generic*)m_pBlockVector->GetBlock(blockIndex)->m_pMetadata;\r
- const VkDeviceSize blockSize = pMetadata->GetSize();\r
- \r
- // No allocations in this block - entire area is free.\r
- if(pMetadata->m_Suballocations.empty())\r
- {\r
- pMetadata->m_FreeCount = 1;\r
- //pMetadata->m_SumFreeSize is already set to blockSize.\r
- VmaSuballocation suballoc = {\r
- 0, // offset\r
- blockSize, // size\r
- VMA_NULL, // hAllocation\r
- VMA_SUBALLOCATION_TYPE_FREE };\r
- pMetadata->m_Suballocations.push_back(suballoc);\r
- pMetadata->RegisterFreeSuballocation(pMetadata->m_Suballocations.begin());\r
- }\r
- // There are some allocations in this block.\r
- else\r
- {\r
- VkDeviceSize offset = 0;\r
- VmaSuballocationList::iterator it;\r
- for(it = pMetadata->m_Suballocations.begin();\r
- it != pMetadata->m_Suballocations.end();\r
- ++it)\r
- {\r
- VMA_ASSERT(it->type != VMA_SUBALLOCATION_TYPE_FREE);\r
- VMA_ASSERT(it->offset >= offset);\r
-\r
- // Need to insert preceding free space.\r
- if(it->offset > offset)\r
- {\r
- ++pMetadata->m_FreeCount;\r
- const VkDeviceSize freeSize = it->offset - offset;\r
- VmaSuballocation suballoc = {\r
- offset, // offset\r
- freeSize, // size\r
- VMA_NULL, // hAllocation\r
- VMA_SUBALLOCATION_TYPE_FREE };\r
- VmaSuballocationList::iterator precedingFreeIt = pMetadata->m_Suballocations.insert(it, suballoc);\r
- if(freeSize >= VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- pMetadata->m_FreeSuballocationsBySize.push_back(precedingFreeIt);\r
- }\r
- }\r
-\r
- pMetadata->m_SumFreeSize -= it->size;\r
- offset = it->offset + it->size;\r
- }\r
-\r
- // Need to insert trailing free space.\r
- if(offset < blockSize)\r
- {\r
- ++pMetadata->m_FreeCount;\r
- const VkDeviceSize freeSize = blockSize - offset;\r
- VmaSuballocation suballoc = {\r
- offset, // offset\r
- freeSize, // size\r
- VMA_NULL, // hAllocation\r
- VMA_SUBALLOCATION_TYPE_FREE };\r
- VMA_ASSERT(it == pMetadata->m_Suballocations.end());\r
- VmaSuballocationList::iterator trailingFreeIt = pMetadata->m_Suballocations.insert(it, suballoc);\r
- if(freeSize > VMA_MIN_FREE_SUBALLOCATION_SIZE_TO_REGISTER)\r
- {\r
- pMetadata->m_FreeSuballocationsBySize.push_back(trailingFreeIt);\r
- }\r
- }\r
-\r
- VMA_SORT(\r
- pMetadata->m_FreeSuballocationsBySize.begin(),\r
- pMetadata->m_FreeSuballocationsBySize.end(),\r
- VmaSuballocationItemSizeLess());\r
- }\r
-\r
- VMA_HEAVY_ASSERT(pMetadata->Validate());\r
- }\r
-}\r
-\r
-void VmaDefragmentationAlgorithm_Fast::InsertSuballoc(VmaBlockMetadata_Generic* pMetadata, const VmaSuballocation& suballoc)\r
-{\r
- // TODO: Optimize somehow. Remember iterator instead of searching for it linearly.\r
- VmaSuballocationList::iterator it = pMetadata->m_Suballocations.begin();\r
- while(it != pMetadata->m_Suballocations.end())\r
- {\r
- if(it->offset < suballoc.offset)\r
- {\r
- ++it;\r
- }\r
- }\r
- pMetadata->m_Suballocations.insert(it, suballoc);\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaBlockVectorDefragmentationContext\r
-\r
-VmaBlockVectorDefragmentationContext::VmaBlockVectorDefragmentationContext(\r
- VmaAllocator hAllocator,\r
- VmaPool hCustomPool,\r
- VmaBlockVector* pBlockVector,\r
- uint32_t currFrameIndex) :\r
- res(VK_SUCCESS),\r
- mutexLocked(false),\r
- blockContexts(VmaStlAllocator<VmaBlockDefragmentationContext>(hAllocator->GetAllocationCallbacks())),\r
- defragmentationMoves(VmaStlAllocator<VmaDefragmentationMove>(hAllocator->GetAllocationCallbacks())),\r
- defragmentationMovesProcessed(0),\r
- defragmentationMovesCommitted(0),\r
- hasDefragmentationPlan(0),\r
- m_hAllocator(hAllocator),\r
- m_hCustomPool(hCustomPool),\r
- m_pBlockVector(pBlockVector),\r
- m_CurrFrameIndex(currFrameIndex),\r
- m_pAlgorithm(VMA_NULL),\r
- m_Allocations(VmaStlAllocator<AllocInfo>(hAllocator->GetAllocationCallbacks())),\r
- m_AllAllocations(false)\r
-{\r
-}\r
-\r
-VmaBlockVectorDefragmentationContext::~VmaBlockVectorDefragmentationContext()\r
-{\r
- vma_delete(m_hAllocator, m_pAlgorithm);\r
-}\r
-\r
-void VmaBlockVectorDefragmentationContext::AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged)\r
-{\r
- AllocInfo info = { hAlloc, pChanged };\r
- m_Allocations.push_back(info);\r
-}\r
-\r
-void VmaBlockVectorDefragmentationContext::Begin(bool overlappingMoveSupported, VmaDefragmentationFlags flags)\r
-{\r
- const bool allAllocations = m_AllAllocations ||\r
- m_Allocations.size() == m_pBlockVector->CalcAllocationCount();\r
-\r
- /********************************\r
- HERE IS THE CHOICE OF DEFRAGMENTATION ALGORITHM.\r
- ********************************/\r
-\r
- /*\r
- Fast algorithm is supported only when certain criteria are met:\r
- - VMA_DEBUG_MARGIN is 0.\r
- - All allocations in this block vector are moveable.\r
- - There is no possibility of image/buffer granularity conflict.\r
- - The defragmentation is not incremental\r
- */\r
- if(VMA_DEBUG_MARGIN == 0 &&\r
- allAllocations &&\r
- !m_pBlockVector->IsBufferImageGranularityConflictPossible() &&\r
- !(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL))\r
- {\r
- m_pAlgorithm = vma_new(m_hAllocator, VmaDefragmentationAlgorithm_Fast)(\r
- m_hAllocator, m_pBlockVector, m_CurrFrameIndex, overlappingMoveSupported);\r
- }\r
- else\r
- {\r
- m_pAlgorithm = vma_new(m_hAllocator, VmaDefragmentationAlgorithm_Generic)(\r
- m_hAllocator, m_pBlockVector, m_CurrFrameIndex, overlappingMoveSupported);\r
- }\r
-\r
- if(allAllocations)\r
- {\r
- m_pAlgorithm->AddAll();\r
- }\r
- else\r
- {\r
- for(size_t i = 0, count = m_Allocations.size(); i < count; ++i)\r
- {\r
- m_pAlgorithm->AddAllocation(m_Allocations[i].hAlloc, m_Allocations[i].pChanged);\r
- }\r
- }\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaDefragmentationContext\r
-\r
-VmaDefragmentationContext_T::VmaDefragmentationContext_T(\r
- VmaAllocator hAllocator,\r
- uint32_t currFrameIndex,\r
- uint32_t flags,\r
- VmaDefragmentationStats* pStats) :\r
- m_hAllocator(hAllocator),\r
- m_CurrFrameIndex(currFrameIndex),\r
- m_Flags(flags),\r
- m_pStats(pStats),\r
- m_CustomPoolContexts(VmaStlAllocator<VmaBlockVectorDefragmentationContext*>(hAllocator->GetAllocationCallbacks()))\r
-{\r
- memset(m_DefaultPoolContexts, 0, sizeof(m_DefaultPoolContexts));\r
-}\r
-\r
-VmaDefragmentationContext_T::~VmaDefragmentationContext_T()\r
-{\r
- for(size_t i = m_CustomPoolContexts.size(); i--; )\r
- {\r
- VmaBlockVectorDefragmentationContext* pBlockVectorCtx = m_CustomPoolContexts[i];\r
- pBlockVectorCtx->GetBlockVector()->DefragmentationEnd(pBlockVectorCtx, m_Flags, m_pStats);\r
- vma_delete(m_hAllocator, pBlockVectorCtx);\r
- }\r
- for(size_t i = m_hAllocator->m_MemProps.memoryTypeCount; i--; )\r
- {\r
- VmaBlockVectorDefragmentationContext* pBlockVectorCtx = m_DefaultPoolContexts[i];\r
- if(pBlockVectorCtx)\r
- {\r
- pBlockVectorCtx->GetBlockVector()->DefragmentationEnd(pBlockVectorCtx, m_Flags, m_pStats);\r
- vma_delete(m_hAllocator, pBlockVectorCtx);\r
- }\r
- }\r
-}\r
-\r
-void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, const VmaPool* pPools)\r
-{\r
- for(uint32_t poolIndex = 0; poolIndex < poolCount; ++poolIndex)\r
- {\r
- VmaPool pool = pPools[poolIndex];\r
- VMA_ASSERT(pool);\r
- // Pools with algorithm other than default are not defragmented.\r
- if(pool->m_BlockVector.GetAlgorithm() == 0)\r
- {\r
- VmaBlockVectorDefragmentationContext* pBlockVectorDefragCtx = VMA_NULL;\r
- \r
- for(size_t i = m_CustomPoolContexts.size(); i--; )\r
- {\r
- if(m_CustomPoolContexts[i]->GetCustomPool() == pool)\r
- {\r
- pBlockVectorDefragCtx = m_CustomPoolContexts[i];\r
- break;\r
- }\r
- }\r
- \r
- if(!pBlockVectorDefragCtx)\r
- {\r
- pBlockVectorDefragCtx = vma_new(m_hAllocator, VmaBlockVectorDefragmentationContext)(\r
- m_hAllocator,\r
- pool,\r
- &pool->m_BlockVector,\r
- m_CurrFrameIndex);\r
- m_CustomPoolContexts.push_back(pBlockVectorDefragCtx);\r
- }\r
-\r
- pBlockVectorDefragCtx->AddAll();\r
- }\r
- }\r
-}\r
-\r
-void VmaDefragmentationContext_T::AddAllocations(\r
- uint32_t allocationCount,\r
- const VmaAllocation* pAllocations,\r
- VkBool32* pAllocationsChanged)\r
-{\r
- // Dispatch pAllocations among defragmentators. Create them when necessary.\r
- for(uint32_t allocIndex = 0; allocIndex < allocationCount; ++allocIndex)\r
- {\r
- const VmaAllocation hAlloc = pAllocations[allocIndex];\r
- VMA_ASSERT(hAlloc);\r
- // DedicatedAlloc cannot be defragmented.\r
- if((hAlloc->GetType() == VmaAllocation_T::ALLOCATION_TYPE_BLOCK) &&\r
- // Lost allocation cannot be defragmented.\r
- (hAlloc->GetLastUseFrameIndex() != VMA_FRAME_INDEX_LOST))\r
- {\r
- VmaBlockVectorDefragmentationContext* pBlockVectorDefragCtx = VMA_NULL;\r
-\r
- const VmaPool hAllocPool = hAlloc->GetBlock()->GetParentPool();\r
- // This allocation belongs to custom pool.\r
- if(hAllocPool != VK_NULL_HANDLE)\r
- {\r
- // Pools with algorithm other than default are not defragmented.\r
- if(hAllocPool->m_BlockVector.GetAlgorithm() == 0)\r
- {\r
- for(size_t i = m_CustomPoolContexts.size(); i--; )\r
- {\r
- if(m_CustomPoolContexts[i]->GetCustomPool() == hAllocPool)\r
- {\r
- pBlockVectorDefragCtx = m_CustomPoolContexts[i];\r
- break;\r
- }\r
- }\r
- if(!pBlockVectorDefragCtx)\r
- {\r
- pBlockVectorDefragCtx = vma_new(m_hAllocator, VmaBlockVectorDefragmentationContext)(\r
- m_hAllocator,\r
- hAllocPool,\r
- &hAllocPool->m_BlockVector,\r
- m_CurrFrameIndex);\r
- m_CustomPoolContexts.push_back(pBlockVectorDefragCtx);\r
- }\r
- }\r
- }\r
- // This allocation belongs to default pool.\r
- else\r
- {\r
- const uint32_t memTypeIndex = hAlloc->GetMemoryTypeIndex();\r
- pBlockVectorDefragCtx = m_DefaultPoolContexts[memTypeIndex];\r
- if(!pBlockVectorDefragCtx)\r
- {\r
- pBlockVectorDefragCtx = vma_new(m_hAllocator, VmaBlockVectorDefragmentationContext)(\r
- m_hAllocator,\r
- VMA_NULL, // hCustomPool\r
- m_hAllocator->m_pBlockVectors[memTypeIndex],\r
- m_CurrFrameIndex);\r
- m_DefaultPoolContexts[memTypeIndex] = pBlockVectorDefragCtx;\r
- }\r
- }\r
-\r
- if(pBlockVectorDefragCtx)\r
- {\r
- VkBool32* const pChanged = (pAllocationsChanged != VMA_NULL) ?\r
- &pAllocationsChanged[allocIndex] : VMA_NULL;\r
- pBlockVectorDefragCtx->AddAllocation(hAlloc, pChanged);\r
- }\r
- }\r
- }\r
-}\r
-\r
-VkResult VmaDefragmentationContext_T::Defragment(\r
- VkDeviceSize maxCpuBytesToMove, uint32_t maxCpuAllocationsToMove,\r
- VkDeviceSize maxGpuBytesToMove, uint32_t maxGpuAllocationsToMove,\r
- VkCommandBuffer commandBuffer, VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags)\r
-{\r
- if(pStats)\r
- {\r
- memset(pStats, 0, sizeof(VmaDefragmentationStats));\r
- }\r
-\r
- if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL)\r
- {\r
- // For incremental defragmetnations, we just earmark how much we can move\r
- // The real meat is in the defragmentation steps\r
- m_MaxCpuBytesToMove = maxCpuBytesToMove;\r
- m_MaxCpuAllocationsToMove = maxCpuAllocationsToMove;\r
-\r
- m_MaxGpuBytesToMove = maxGpuBytesToMove;\r
- m_MaxGpuAllocationsToMove = maxGpuAllocationsToMove;\r
-\r
- if(m_MaxCpuBytesToMove == 0 && m_MaxCpuAllocationsToMove == 0 &&\r
- m_MaxGpuBytesToMove == 0 && m_MaxGpuAllocationsToMove == 0)\r
- return VK_SUCCESS;\r
-\r
- return VK_NOT_READY;\r
- }\r
-\r
- if(commandBuffer == VK_NULL_HANDLE)\r
- {\r
- maxGpuBytesToMove = 0;\r
- maxGpuAllocationsToMove = 0;\r
- }\r
-\r
- VkResult res = VK_SUCCESS;\r
-\r
- // Process default pools.\r
- for(uint32_t memTypeIndex = 0;\r
- memTypeIndex < m_hAllocator->GetMemoryTypeCount() && res >= VK_SUCCESS;\r
- ++memTypeIndex)\r
- {\r
- VmaBlockVectorDefragmentationContext* pBlockVectorCtx = m_DefaultPoolContexts[memTypeIndex];\r
- if(pBlockVectorCtx)\r
- {\r
- VMA_ASSERT(pBlockVectorCtx->GetBlockVector());\r
- pBlockVectorCtx->GetBlockVector()->Defragment(\r
- pBlockVectorCtx,\r
- pStats, flags,\r
- maxCpuBytesToMove, maxCpuAllocationsToMove,\r
- maxGpuBytesToMove, maxGpuAllocationsToMove,\r
- commandBuffer);\r
- if(pBlockVectorCtx->res != VK_SUCCESS)\r
- {\r
- res = pBlockVectorCtx->res;\r
- }\r
- }\r
- }\r
-\r
- // Process custom pools.\r
- for(size_t customCtxIndex = 0, customCtxCount = m_CustomPoolContexts.size();\r
- customCtxIndex < customCtxCount && res >= VK_SUCCESS;\r
- ++customCtxIndex)\r
- {\r
- VmaBlockVectorDefragmentationContext* pBlockVectorCtx = m_CustomPoolContexts[customCtxIndex];\r
- VMA_ASSERT(pBlockVectorCtx && pBlockVectorCtx->GetBlockVector());\r
- pBlockVectorCtx->GetBlockVector()->Defragment(\r
- pBlockVectorCtx,\r
- pStats, flags,\r
- maxCpuBytesToMove, maxCpuAllocationsToMove,\r
- maxGpuBytesToMove, maxGpuAllocationsToMove,\r
- commandBuffer);\r
- if(pBlockVectorCtx->res != VK_SUCCESS)\r
- {\r
- res = pBlockVectorCtx->res;\r
- }\r
- }\r
-\r
- return res;\r
-}\r
-\r
-VkResult VmaDefragmentationContext_T::DefragmentPassBegin(VmaDefragmentationPassInfo* pInfo)\r
-{\r
- VmaDefragmentationPassMoveInfo* pCurrentMove = pInfo->pMoves;\r
- uint32_t movesLeft = pInfo->moveCount;\r
-\r
- // Process default pools.\r
- for(uint32_t memTypeIndex = 0;\r
- memTypeIndex < m_hAllocator->GetMemoryTypeCount();\r
- ++memTypeIndex)\r
- {\r
- VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_DefaultPoolContexts[memTypeIndex];\r
- if(pBlockVectorCtx)\r
- {\r
- VMA_ASSERT(pBlockVectorCtx->GetBlockVector());\r
-\r
- if(!pBlockVectorCtx->hasDefragmentationPlan)\r
- {\r
- pBlockVectorCtx->GetBlockVector()->Defragment(\r
- pBlockVectorCtx,\r
- m_pStats, m_Flags,\r
- m_MaxCpuBytesToMove, m_MaxCpuAllocationsToMove,\r
- m_MaxGpuBytesToMove, m_MaxGpuAllocationsToMove,\r
- VK_NULL_HANDLE);\r
-\r
- if(pBlockVectorCtx->res < VK_SUCCESS)\r
- continue;\r
-\r
- pBlockVectorCtx->hasDefragmentationPlan = true;\r
- }\r
-\r
- const uint32_t processed = pBlockVectorCtx->GetBlockVector()->ProcessDefragmentations(\r
- pBlockVectorCtx,\r
- pCurrentMove, movesLeft);\r
-\r
- movesLeft -= processed;\r
- pCurrentMove += processed;\r
- }\r
- }\r
-\r
- // Process custom pools.\r
- for(size_t customCtxIndex = 0, customCtxCount = m_CustomPoolContexts.size();\r
- customCtxIndex < customCtxCount;\r
- ++customCtxIndex)\r
- {\r
- VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_CustomPoolContexts[customCtxIndex];\r
- VMA_ASSERT(pBlockVectorCtx && pBlockVectorCtx->GetBlockVector());\r
-\r
- if(!pBlockVectorCtx->hasDefragmentationPlan)\r
- {\r
- pBlockVectorCtx->GetBlockVector()->Defragment(\r
- pBlockVectorCtx,\r
- m_pStats, m_Flags,\r
- m_MaxCpuBytesToMove, m_MaxCpuAllocationsToMove,\r
- m_MaxGpuBytesToMove, m_MaxGpuAllocationsToMove,\r
- VK_NULL_HANDLE);\r
-\r
- if(pBlockVectorCtx->res < VK_SUCCESS)\r
- continue;\r
-\r
- pBlockVectorCtx->hasDefragmentationPlan = true;\r
- }\r
-\r
- const uint32_t processed = pBlockVectorCtx->GetBlockVector()->ProcessDefragmentations(\r
- pBlockVectorCtx,\r
- pCurrentMove, movesLeft);\r
-\r
- movesLeft -= processed;\r
- pCurrentMove += processed;\r
- }\r
-\r
- pInfo->moveCount = pInfo->moveCount - movesLeft;\r
-\r
- return VK_SUCCESS;\r
-}\r
-VkResult VmaDefragmentationContext_T::DefragmentPassEnd()\r
-{\r
- VkResult res = VK_SUCCESS;\r
-\r
- // Process default pools.\r
- for(uint32_t memTypeIndex = 0;\r
- memTypeIndex < m_hAllocator->GetMemoryTypeCount();\r
- ++memTypeIndex)\r
- {\r
- VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_DefaultPoolContexts[memTypeIndex];\r
- if(pBlockVectorCtx)\r
- {\r
- VMA_ASSERT(pBlockVectorCtx->GetBlockVector());\r
-\r
- if(!pBlockVectorCtx->hasDefragmentationPlan)\r
- {\r
- res = VK_NOT_READY;\r
- continue;\r
- }\r
-\r
- pBlockVectorCtx->GetBlockVector()->CommitDefragmentations(\r
- pBlockVectorCtx, m_pStats);\r
-\r
- if(pBlockVectorCtx->defragmentationMoves.size() != pBlockVectorCtx->defragmentationMovesCommitted)\r
- res = VK_NOT_READY;\r
- }\r
- }\r
-\r
- // Process custom pools.\r
- for(size_t customCtxIndex = 0, customCtxCount = m_CustomPoolContexts.size();\r
- customCtxIndex < customCtxCount;\r
- ++customCtxIndex)\r
- {\r
- VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_CustomPoolContexts[customCtxIndex];\r
- VMA_ASSERT(pBlockVectorCtx && pBlockVectorCtx->GetBlockVector());\r
-\r
- if(!pBlockVectorCtx->hasDefragmentationPlan)\r
- {\r
- res = VK_NOT_READY;\r
- continue;\r
- }\r
-\r
- pBlockVectorCtx->GetBlockVector()->CommitDefragmentations(\r
- pBlockVectorCtx, m_pStats);\r
-\r
- if(pBlockVectorCtx->defragmentationMoves.size() != pBlockVectorCtx->defragmentationMovesCommitted)\r
- res = VK_NOT_READY;\r
- }\r
-\r
- return res;\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaRecorder\r
-\r
-#if VMA_RECORDING_ENABLED\r
-\r
-VmaRecorder::VmaRecorder() :\r
- m_UseMutex(true),\r
- m_Flags(0),\r
- m_File(VMA_NULL),\r
- m_RecordingStartTime(std::chrono::high_resolution_clock::now())\r
-{\r
-}\r
-\r
-VkResult VmaRecorder::Init(const VmaRecordSettings& settings, bool useMutex)\r
-{\r
- m_UseMutex = useMutex;\r
- m_Flags = settings.flags;\r
-\r
-#if defined(_WIN32)\r
- // Open file for writing.\r
- errno_t err = fopen_s(&m_File, settings.pFilePath, "wb");\r
-\r
- if(err != 0)\r
- {\r
- return VK_ERROR_INITIALIZATION_FAILED;\r
- }\r
-#else\r
- // Open file for writing.\r
- m_File = fopen(settings.pFilePath, "wb");\r
-\r
- if(m_File == 0)\r
- {\r
- return VK_ERROR_INITIALIZATION_FAILED;\r
- }\r
-#endif\r
-\r
- // Write header.\r
- fprintf(m_File, "%s\n", "Vulkan Memory Allocator,Calls recording");\r
- fprintf(m_File, "%s\n", "1,8");\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-VmaRecorder::~VmaRecorder()\r
-{\r
- if(m_File != VMA_NULL)\r
- {\r
- fclose(m_File);\r
- }\r
-}\r
-\r
-void VmaRecorder::RecordCreateAllocator(uint32_t frameIndex)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaCreateAllocator\n", callParams.threadId, callParams.time, frameIndex);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordDestroyAllocator(uint32_t frameIndex)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaDestroyAllocator\n", callParams.threadId, callParams.time, frameIndex);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordCreatePool(uint32_t frameIndex, const VmaPoolCreateInfo& createInfo, VmaPool pool)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaCreatePool,%u,%u,%llu,%llu,%llu,%u,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- createInfo.memoryTypeIndex,\r
- createInfo.flags,\r
- createInfo.blockSize,\r
- (uint64_t)createInfo.minBlockCount,\r
- (uint64_t)createInfo.maxBlockCount,\r
- createInfo.frameInUseCount,\r
- pool);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordDestroyPool(uint32_t frameIndex, VmaPool pool)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaDestroyPool,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- pool);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordAllocateMemory(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(createInfo.flags, createInfo.pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaAllocateMemory,%llu,%llu,%u,%u,%u,%u,%u,%u,%p,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- vkMemReq.size,\r
- vkMemReq.alignment,\r
- vkMemReq.memoryTypeBits,\r
- createInfo.flags,\r
- createInfo.usage,\r
- createInfo.requiredFlags,\r
- createInfo.preferredFlags,\r
- createInfo.memoryTypeBits,\r
- createInfo.pool,\r
- allocation,\r
- userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordAllocateMemoryPages(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- const VmaAllocationCreateInfo& createInfo,\r
- uint64_t allocationCount,\r
- const VmaAllocation* pAllocations)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(createInfo.flags, createInfo.pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaAllocateMemoryPages,%llu,%llu,%u,%u,%u,%u,%u,%u,%p,", callParams.threadId, callParams.time, frameIndex,\r
- vkMemReq.size,\r
- vkMemReq.alignment,\r
- vkMemReq.memoryTypeBits,\r
- createInfo.flags,\r
- createInfo.usage,\r
- createInfo.requiredFlags,\r
- createInfo.preferredFlags,\r
- createInfo.memoryTypeBits,\r
- createInfo.pool);\r
- PrintPointerList(allocationCount, pAllocations);\r
- fprintf(m_File, ",%s\n", userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordAllocateMemoryForBuffer(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- bool requiresDedicatedAllocation,\r
- bool prefersDedicatedAllocation,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(createInfo.flags, createInfo.pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaAllocateMemoryForBuffer,%llu,%llu,%u,%u,%u,%u,%u,%u,%u,%u,%p,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- vkMemReq.size,\r
- vkMemReq.alignment,\r
- vkMemReq.memoryTypeBits,\r
- requiresDedicatedAllocation ? 1 : 0,\r
- prefersDedicatedAllocation ? 1 : 0,\r
- createInfo.flags,\r
- createInfo.usage,\r
- createInfo.requiredFlags,\r
- createInfo.preferredFlags,\r
- createInfo.memoryTypeBits,\r
- createInfo.pool,\r
- allocation,\r
- userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordAllocateMemoryForImage(uint32_t frameIndex,\r
- const VkMemoryRequirements& vkMemReq,\r
- bool requiresDedicatedAllocation,\r
- bool prefersDedicatedAllocation,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(createInfo.flags, createInfo.pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaAllocateMemoryForImage,%llu,%llu,%u,%u,%u,%u,%u,%u,%u,%u,%p,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- vkMemReq.size,\r
- vkMemReq.alignment,\r
- vkMemReq.memoryTypeBits,\r
- requiresDedicatedAllocation ? 1 : 0,\r
- prefersDedicatedAllocation ? 1 : 0,\r
- createInfo.flags,\r
- createInfo.usage,\r
- createInfo.requiredFlags,\r
- createInfo.preferredFlags,\r
- createInfo.memoryTypeBits,\r
- createInfo.pool,\r
- allocation,\r
- userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordFreeMemory(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaFreeMemory,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordFreeMemoryPages(uint32_t frameIndex,\r
- uint64_t allocationCount,\r
- const VmaAllocation* pAllocations)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaFreeMemoryPages,", callParams.threadId, callParams.time, frameIndex);\r
- PrintPointerList(allocationCount, pAllocations);\r
- fprintf(m_File, "\n");\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordSetAllocationUserData(uint32_t frameIndex,\r
- VmaAllocation allocation,\r
- const void* pUserData)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(\r
- allocation->IsUserDataString() ? VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT : 0,\r
- pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaSetAllocationUserData,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation,\r
- userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordCreateLostAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaCreateLostAllocation,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordMapMemory(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaMapMemory,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordUnmapMemory(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaUnmapMemory,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordFlushAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaFlushAllocation,%p,%llu,%llu\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation,\r
- offset,\r
- size);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordInvalidateAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaInvalidateAllocation,%p,%llu,%llu\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation,\r
- offset,\r
- size);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordCreateBuffer(uint32_t frameIndex,\r
- const VkBufferCreateInfo& bufCreateInfo,\r
- const VmaAllocationCreateInfo& allocCreateInfo,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(allocCreateInfo.flags, allocCreateInfo.pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaCreateBuffer,%u,%llu,%u,%u,%u,%u,%u,%u,%u,%p,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- bufCreateInfo.flags,\r
- bufCreateInfo.size,\r
- bufCreateInfo.usage,\r
- bufCreateInfo.sharingMode,\r
- allocCreateInfo.flags,\r
- allocCreateInfo.usage,\r
- allocCreateInfo.requiredFlags,\r
- allocCreateInfo.preferredFlags,\r
- allocCreateInfo.memoryTypeBits,\r
- allocCreateInfo.pool,\r
- allocation,\r
- userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordCreateImage(uint32_t frameIndex,\r
- const VkImageCreateInfo& imageCreateInfo,\r
- const VmaAllocationCreateInfo& allocCreateInfo,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- UserDataString userDataStr(allocCreateInfo.flags, allocCreateInfo.pUserData);\r
- fprintf(m_File, "%u,%.3f,%u,vmaCreateImage,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%p,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- imageCreateInfo.flags,\r
- imageCreateInfo.imageType,\r
- imageCreateInfo.format,\r
- imageCreateInfo.extent.width,\r
- imageCreateInfo.extent.height,\r
- imageCreateInfo.extent.depth,\r
- imageCreateInfo.mipLevels,\r
- imageCreateInfo.arrayLayers,\r
- imageCreateInfo.samples,\r
- imageCreateInfo.tiling,\r
- imageCreateInfo.usage,\r
- imageCreateInfo.sharingMode,\r
- imageCreateInfo.initialLayout,\r
- allocCreateInfo.flags,\r
- allocCreateInfo.usage,\r
- allocCreateInfo.requiredFlags,\r
- allocCreateInfo.preferredFlags,\r
- allocCreateInfo.memoryTypeBits,\r
- allocCreateInfo.pool,\r
- allocation,\r
- userDataStr.GetString());\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordDestroyBuffer(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaDestroyBuffer,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordDestroyImage(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaDestroyImage,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordTouchAllocation(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaTouchAllocation,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordGetAllocationInfo(uint32_t frameIndex,\r
- VmaAllocation allocation)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaGetAllocationInfo,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- allocation);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordMakePoolAllocationsLost(uint32_t frameIndex,\r
- VmaPool pool)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaMakePoolAllocationsLost,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- pool);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordDefragmentationBegin(uint32_t frameIndex,\r
- const VmaDefragmentationInfo2& info,\r
- VmaDefragmentationContext ctx)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaDefragmentationBegin,%u,", callParams.threadId, callParams.time, frameIndex,\r
- info.flags);\r
- PrintPointerList(info.allocationCount, info.pAllocations);\r
- fprintf(m_File, ",");\r
- PrintPointerList(info.poolCount, info.pPools);\r
- fprintf(m_File, ",%llu,%u,%llu,%u,%p,%p\n",\r
- info.maxCpuBytesToMove,\r
- info.maxCpuAllocationsToMove,\r
- info.maxGpuBytesToMove,\r
- info.maxGpuAllocationsToMove,\r
- info.commandBuffer,\r
- ctx);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordDefragmentationEnd(uint32_t frameIndex,\r
- VmaDefragmentationContext ctx)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaDefragmentationEnd,%p\n", callParams.threadId, callParams.time, frameIndex,\r
- ctx);\r
- Flush();\r
-}\r
-\r
-void VmaRecorder::RecordSetPoolName(uint32_t frameIndex,\r
- VmaPool pool,\r
- const char* name)\r
-{\r
- CallParams callParams;\r
- GetBasicParams(callParams);\r
-\r
- VmaMutexLock lock(m_FileMutex, m_UseMutex);\r
- fprintf(m_File, "%u,%.3f,%u,vmaSetPoolName,%p,%s\n", callParams.threadId, callParams.time, frameIndex,\r
- pool, name != VMA_NULL ? name : "");\r
- Flush();\r
-}\r
-\r
-VmaRecorder::UserDataString::UserDataString(VmaAllocationCreateFlags allocFlags, const void* pUserData)\r
-{\r
- if(pUserData != VMA_NULL)\r
- {\r
- if((allocFlags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0)\r
- {\r
- m_Str = (const char*)pUserData;\r
- }\r
- else\r
- {\r
- // If VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is not specified, convert the string's memory address to a string and store it.\r
- snprintf(m_PtrStr, 17, "%p", pUserData);\r
- m_Str = m_PtrStr;\r
- }\r
- }\r
- else\r
- {\r
- m_Str = "";\r
- }\r
-}\r
-\r
-void VmaRecorder::WriteConfiguration(\r
- const VkPhysicalDeviceProperties& devProps,\r
- const VkPhysicalDeviceMemoryProperties& memProps,\r
- uint32_t vulkanApiVersion,\r
- bool dedicatedAllocationExtensionEnabled,\r
- bool bindMemory2ExtensionEnabled,\r
- bool memoryBudgetExtensionEnabled,\r
- bool deviceCoherentMemoryExtensionEnabled)\r
-{\r
- fprintf(m_File, "Config,Begin\n");\r
-\r
- fprintf(m_File, "VulkanApiVersion,%u,%u\n", VK_VERSION_MAJOR(vulkanApiVersion), VK_VERSION_MINOR(vulkanApiVersion));\r
-\r
- fprintf(m_File, "PhysicalDevice,apiVersion,%u\n", devProps.apiVersion);\r
- fprintf(m_File, "PhysicalDevice,driverVersion,%u\n", devProps.driverVersion);\r
- fprintf(m_File, "PhysicalDevice,vendorID,%u\n", devProps.vendorID);\r
- fprintf(m_File, "PhysicalDevice,deviceID,%u\n", devProps.deviceID);\r
- fprintf(m_File, "PhysicalDevice,deviceType,%u\n", devProps.deviceType);\r
- fprintf(m_File, "PhysicalDevice,deviceName,%s\n", devProps.deviceName);\r
-\r
- fprintf(m_File, "PhysicalDeviceLimits,maxMemoryAllocationCount,%u\n", devProps.limits.maxMemoryAllocationCount);\r
- fprintf(m_File, "PhysicalDeviceLimits,bufferImageGranularity,%llu\n", devProps.limits.bufferImageGranularity);\r
- fprintf(m_File, "PhysicalDeviceLimits,nonCoherentAtomSize,%llu\n", devProps.limits.nonCoherentAtomSize);\r
-\r
- fprintf(m_File, "PhysicalDeviceMemory,HeapCount,%u\n", memProps.memoryHeapCount);\r
- for(uint32_t i = 0; i < memProps.memoryHeapCount; ++i)\r
- {\r
- fprintf(m_File, "PhysicalDeviceMemory,Heap,%u,size,%llu\n", i, memProps.memoryHeaps[i].size);\r
- fprintf(m_File, "PhysicalDeviceMemory,Heap,%u,flags,%u\n", i, memProps.memoryHeaps[i].flags);\r
- }\r
- fprintf(m_File, "PhysicalDeviceMemory,TypeCount,%u\n", memProps.memoryTypeCount);\r
- for(uint32_t i = 0; i < memProps.memoryTypeCount; ++i)\r
- {\r
- fprintf(m_File, "PhysicalDeviceMemory,Type,%u,heapIndex,%u\n", i, memProps.memoryTypes[i].heapIndex);\r
- fprintf(m_File, "PhysicalDeviceMemory,Type,%u,propertyFlags,%u\n", i, memProps.memoryTypes[i].propertyFlags);\r
- }\r
-\r
- fprintf(m_File, "Extension,VK_KHR_dedicated_allocation,%u\n", dedicatedAllocationExtensionEnabled ? 1 : 0);\r
- fprintf(m_File, "Extension,VK_KHR_bind_memory2,%u\n", bindMemory2ExtensionEnabled ? 1 : 0);\r
- fprintf(m_File, "Extension,VK_EXT_memory_budget,%u\n", memoryBudgetExtensionEnabled ? 1 : 0);\r
- fprintf(m_File, "Extension,VK_AMD_device_coherent_memory,%u\n", deviceCoherentMemoryExtensionEnabled ? 1 : 0);\r
-\r
- fprintf(m_File, "Macro,VMA_DEBUG_ALWAYS_DEDICATED_MEMORY,%u\n", VMA_DEBUG_ALWAYS_DEDICATED_MEMORY ? 1 : 0);\r
- fprintf(m_File, "Macro,VMA_DEBUG_ALIGNMENT,%llu\n", (VkDeviceSize)VMA_DEBUG_ALIGNMENT);\r
- fprintf(m_File, "Macro,VMA_DEBUG_MARGIN,%llu\n", (VkDeviceSize)VMA_DEBUG_MARGIN);\r
- fprintf(m_File, "Macro,VMA_DEBUG_INITIALIZE_ALLOCATIONS,%u\n", VMA_DEBUG_INITIALIZE_ALLOCATIONS ? 1 : 0);\r
- fprintf(m_File, "Macro,VMA_DEBUG_DETECT_CORRUPTION,%u\n", VMA_DEBUG_DETECT_CORRUPTION ? 1 : 0);\r
- fprintf(m_File, "Macro,VMA_DEBUG_GLOBAL_MUTEX,%u\n", VMA_DEBUG_GLOBAL_MUTEX ? 1 : 0);\r
- fprintf(m_File, "Macro,VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY,%llu\n", (VkDeviceSize)VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY);\r
- fprintf(m_File, "Macro,VMA_SMALL_HEAP_MAX_SIZE,%llu\n", (VkDeviceSize)VMA_SMALL_HEAP_MAX_SIZE);\r
- fprintf(m_File, "Macro,VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE,%llu\n", (VkDeviceSize)VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE);\r
-\r
- fprintf(m_File, "Config,End\n");\r
-}\r
-\r
-void VmaRecorder::GetBasicParams(CallParams& outParams)\r
-{\r
- #if defined(_WIN32)\r
- outParams.threadId = GetCurrentThreadId();\r
- #else\r
- // Use C++11 features to get thread id and convert it to uint32_t.\r
- // There is room for optimization since sstream is quite slow.\r
- // Is there a better way to convert std::this_thread::get_id() to uint32_t?\r
- std::thread::id thread_id = std::this_thread::get_id();\r
- stringstream thread_id_to_string_converter;\r
- thread_id_to_string_converter << thread_id;\r
- string thread_id_as_string = thread_id_to_string_converter.str();\r
- outParams.threadId = static_cast<uint32_t>(std::stoi(thread_id_as_string.c_str()));\r
- #endif\r
- \r
- auto current_time = std::chrono::high_resolution_clock::now();\r
-\r
- outParams.time = std::chrono::duration<double, std::chrono::seconds::period>(current_time - m_RecordingStartTime).count();\r
-}\r
-\r
-void VmaRecorder::PrintPointerList(uint64_t count, const VmaAllocation* pItems)\r
-{\r
- if(count)\r
- {\r
- fprintf(m_File, "%p", pItems[0]);\r
- for(uint64_t i = 1; i < count; ++i)\r
- {\r
- fprintf(m_File, " %p", pItems[i]);\r
- }\r
- }\r
-}\r
-\r
-void VmaRecorder::Flush()\r
-{\r
- if((m_Flags & VMA_RECORD_FLUSH_AFTER_CALL_BIT) != 0)\r
- {\r
- fflush(m_File);\r
- }\r
-}\r
-\r
-#endif // #if VMA_RECORDING_ENABLED\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaAllocationObjectAllocator\r
-\r
-VmaAllocationObjectAllocator::VmaAllocationObjectAllocator(const VkAllocationCallbacks* pAllocationCallbacks) :\r
- m_Allocator(pAllocationCallbacks, 1024)\r
-{\r
-}\r
-\r
-template<typename... Types> VmaAllocation VmaAllocationObjectAllocator::Allocate(Types... args)\r
-{\r
- VmaMutexLock mutexLock(m_Mutex);\r
- return m_Allocator.Alloc<Types...>(std::forward<Types>(args)...);\r
-}\r
-\r
-void VmaAllocationObjectAllocator::Free(VmaAllocation hAlloc)\r
-{\r
- VmaMutexLock mutexLock(m_Mutex);\r
- m_Allocator.Free(hAlloc);\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// VmaAllocator_T\r
-\r
-VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :\r
- m_UseMutex((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT) == 0),\r
- m_VulkanApiVersion(pCreateInfo->vulkanApiVersion != 0 ? pCreateInfo->vulkanApiVersion : VK_API_VERSION_1_0),\r
- m_UseKhrDedicatedAllocation((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT) != 0),\r
- m_UseKhrBindMemory2((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT) != 0),\r
- m_UseExtMemoryBudget((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT) != 0),\r
- m_UseAmdDeviceCoherentMemory((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT) != 0),\r
- m_UseKhrBufferDeviceAddress((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT) != 0),\r
- m_hDevice(pCreateInfo->device),\r
- m_hInstance(pCreateInfo->instance),\r
- m_AllocationCallbacksSpecified(pCreateInfo->pAllocationCallbacks != VMA_NULL),\r
- m_AllocationCallbacks(pCreateInfo->pAllocationCallbacks ?\r
- *pCreateInfo->pAllocationCallbacks : VmaEmptyAllocationCallbacks),\r
- m_AllocationObjectAllocator(&m_AllocationCallbacks),\r
- m_HeapSizeLimitMask(0),\r
- m_PreferredLargeHeapBlockSize(0),\r
- m_PhysicalDevice(pCreateInfo->physicalDevice),\r
- m_CurrentFrameIndex(0),\r
- m_GpuDefragmentationMemoryTypeBits(UINT32_MAX),\r
- m_Pools(VmaStlAllocator<VmaPool>(GetAllocationCallbacks())),\r
- m_NextPoolId(0),\r
- m_GlobalMemoryTypeBits(UINT32_MAX)\r
-#if VMA_RECORDING_ENABLED\r
- ,m_pRecorder(VMA_NULL)\r
-#endif\r
-{\r
- if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- m_UseKhrDedicatedAllocation = false;\r
- m_UseKhrBindMemory2 = false;\r
- }\r
-\r
- if(VMA_DEBUG_DETECT_CORRUPTION)\r
- {\r
- // Needs to be multiply of uint32_t size because we are going to write VMA_CORRUPTION_DETECTION_MAGIC_VALUE to it.\r
- VMA_ASSERT(VMA_DEBUG_MARGIN % sizeof(uint32_t) == 0);\r
- }\r
-\r
- VMA_ASSERT(pCreateInfo->physicalDevice && pCreateInfo->device && pCreateInfo->instance);\r
-\r
- if(m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0))\r
- {\r
-#if !(VMA_DEDICATED_ALLOCATION)\r
- if((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT) != 0)\r
- {\r
- VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT set but required extensions are disabled by preprocessor macros.");\r
- }\r
-#endif\r
-#if !(VMA_BIND_MEMORY2)\r
- if((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT) != 0)\r
- {\r
- VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT set but required extension is disabled by preprocessor macros.");\r
- }\r
-#endif\r
- }\r
-#if !(VMA_MEMORY_BUDGET)\r
- if((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT) != 0)\r
- {\r
- VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT set but required extension is disabled by preprocessor macros.");\r
- }\r
-#endif\r
-#if !(VMA_BUFFER_DEVICE_ADDRESS)\r
- if(m_UseKhrBufferDeviceAddress)\r
- {\r
- VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT is set but required extension or Vulkan 1.2 is not available in your Vulkan header or its support in VMA has been disabled by a preprocessor macro.");\r
- }\r
-#endif\r
-#if VMA_VULKAN_VERSION < 1002000\r
- if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 2, 0))\r
- {\r
- VMA_ASSERT(0 && "vulkanApiVersion >= VK_API_VERSION_1_2 but required Vulkan version is disabled by preprocessor macros.");\r
- }\r
-#endif\r
-#if VMA_VULKAN_VERSION < 1001000\r
- if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- VMA_ASSERT(0 && "vulkanApiVersion >= VK_API_VERSION_1_1 but required Vulkan version is disabled by preprocessor macros.");\r
- }\r
-#endif\r
-\r
- memset(&m_DeviceMemoryCallbacks, 0 ,sizeof(m_DeviceMemoryCallbacks));\r
- memset(&m_PhysicalDeviceProperties, 0, sizeof(m_PhysicalDeviceProperties));\r
- memset(&m_MemProps, 0, sizeof(m_MemProps));\r
- \r
- memset(&m_pBlockVectors, 0, sizeof(m_pBlockVectors));\r
- memset(&m_pDedicatedAllocations, 0, sizeof(m_pDedicatedAllocations));\r
- memset(&m_VulkanFunctions, 0, sizeof(m_VulkanFunctions));\r
-\r
- if(pCreateInfo->pDeviceMemoryCallbacks != VMA_NULL)\r
- {\r
- m_DeviceMemoryCallbacks.pUserData = pCreateInfo->pDeviceMemoryCallbacks->pUserData;\r
- m_DeviceMemoryCallbacks.pfnAllocate = pCreateInfo->pDeviceMemoryCallbacks->pfnAllocate;\r
- m_DeviceMemoryCallbacks.pfnFree = pCreateInfo->pDeviceMemoryCallbacks->pfnFree;\r
- }\r
-\r
- ImportVulkanFunctions(pCreateInfo->pVulkanFunctions);\r
-\r
- (*m_VulkanFunctions.vkGetPhysicalDeviceProperties)(m_PhysicalDevice, &m_PhysicalDeviceProperties);\r
- (*m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties)(m_PhysicalDevice, &m_MemProps);\r
-\r
- VMA_ASSERT(VmaIsPow2(VMA_DEBUG_ALIGNMENT));\r
- VMA_ASSERT(VmaIsPow2(VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY));\r
- VMA_ASSERT(VmaIsPow2(m_PhysicalDeviceProperties.limits.bufferImageGranularity));\r
- VMA_ASSERT(VmaIsPow2(m_PhysicalDeviceProperties.limits.nonCoherentAtomSize));\r
-\r
- m_PreferredLargeHeapBlockSize = (pCreateInfo->preferredLargeHeapBlockSize != 0) ?\r
- pCreateInfo->preferredLargeHeapBlockSize : static_cast<VkDeviceSize>(VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE);\r
-\r
- m_GlobalMemoryTypeBits = CalculateGlobalMemoryTypeBits();\r
-\r
- if(pCreateInfo->pHeapSizeLimit != VMA_NULL)\r
- {\r
- for(uint32_t heapIndex = 0; heapIndex < GetMemoryHeapCount(); ++heapIndex)\r
- {\r
- const VkDeviceSize limit = pCreateInfo->pHeapSizeLimit[heapIndex];\r
- if(limit != VK_WHOLE_SIZE)\r
- {\r
- m_HeapSizeLimitMask |= 1u << heapIndex;\r
- if(limit < m_MemProps.memoryHeaps[heapIndex].size)\r
- {\r
- m_MemProps.memoryHeaps[heapIndex].size = limit;\r
- }\r
- }\r
- }\r
- }\r
-\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- const VkDeviceSize preferredBlockSize = CalcPreferredBlockSize(memTypeIndex);\r
-\r
- m_pBlockVectors[memTypeIndex] = vma_new(this, VmaBlockVector)(\r
- this,\r
- VK_NULL_HANDLE, // hParentPool\r
- memTypeIndex,\r
- preferredBlockSize,\r
- 0,\r
- SIZE_MAX,\r
- GetBufferImageGranularity(),\r
- pCreateInfo->frameInUseCount,\r
- false, // explicitBlockSize\r
- false); // linearAlgorithm\r
- // No need to call m_pBlockVectors[memTypeIndex][blockVectorTypeIndex]->CreateMinBlocks here,\r
- // becase minBlockCount is 0.\r
- m_pDedicatedAllocations[memTypeIndex] = vma_new(this, AllocationVectorType)(VmaStlAllocator<VmaAllocation>(GetAllocationCallbacks()));\r
-\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::Init(const VmaAllocatorCreateInfo* pCreateInfo)\r
-{\r
- VkResult res = VK_SUCCESS;\r
-\r
- if(pCreateInfo->pRecordSettings != VMA_NULL &&\r
- !VmaStrIsEmpty(pCreateInfo->pRecordSettings->pFilePath))\r
- {\r
-#if VMA_RECORDING_ENABLED\r
- m_pRecorder = vma_new(this, VmaRecorder)();\r
- res = m_pRecorder->Init(*pCreateInfo->pRecordSettings, m_UseMutex);\r
- if(res != VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- m_pRecorder->WriteConfiguration(\r
- m_PhysicalDeviceProperties,\r
- m_MemProps,\r
- m_VulkanApiVersion,\r
- m_UseKhrDedicatedAllocation,\r
- m_UseKhrBindMemory2,\r
- m_UseExtMemoryBudget,\r
- m_UseAmdDeviceCoherentMemory);\r
- m_pRecorder->RecordCreateAllocator(GetCurrentFrameIndex());\r
-#else\r
- VMA_ASSERT(0 && "VmaAllocatorCreateInfo::pRecordSettings used, but not supported due to VMA_RECORDING_ENABLED not defined to 1.");\r
- return VK_ERROR_FEATURE_NOT_PRESENT;\r
-#endif\r
- }\r
-\r
-#if VMA_MEMORY_BUDGET\r
- if(m_UseExtMemoryBudget)\r
- {\r
- UpdateVulkanBudget();\r
- }\r
-#endif // #if VMA_MEMORY_BUDGET\r
-\r
- return res;\r
-}\r
-\r
-VmaAllocator_T::~VmaAllocator_T()\r
-{\r
-#if VMA_RECORDING_ENABLED\r
- if(m_pRecorder != VMA_NULL)\r
- {\r
- m_pRecorder->RecordDestroyAllocator(GetCurrentFrameIndex());\r
- vma_delete(this, m_pRecorder);\r
- }\r
-#endif\r
- \r
- VMA_ASSERT(m_Pools.empty());\r
-\r
- for(size_t i = GetMemoryTypeCount(); i--; )\r
- {\r
- if(m_pDedicatedAllocations[i] != VMA_NULL && !m_pDedicatedAllocations[i]->empty())\r
- {\r
- VMA_ASSERT(0 && "Unfreed dedicated allocations found.");\r
- }\r
-\r
- vma_delete(this, m_pDedicatedAllocations[i]);\r
- vma_delete(this, m_pBlockVectors[i]);\r
- }\r
-}\r
-\r
-void VmaAllocator_T::ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunctions)\r
-{\r
-#if VMA_STATIC_VULKAN_FUNCTIONS == 1\r
- ImportVulkanFunctions_Static();\r
-#endif\r
-\r
- if(pVulkanFunctions != VMA_NULL)\r
- {\r
- ImportVulkanFunctions_Custom(pVulkanFunctions);\r
- }\r
-\r
-#if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1\r
- ImportVulkanFunctions_Dynamic();\r
-#endif\r
-\r
- ValidateVulkanFunctions();\r
-}\r
-\r
-#if VMA_STATIC_VULKAN_FUNCTIONS == 1\r
-\r
-void VmaAllocator_T::ImportVulkanFunctions_Static()\r
-{\r
- // Vulkan 1.0\r
- m_VulkanFunctions.vkGetPhysicalDeviceProperties = (PFN_vkGetPhysicalDeviceProperties)vkGetPhysicalDeviceProperties;\r
- m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties = (PFN_vkGetPhysicalDeviceMemoryProperties)vkGetPhysicalDeviceMemoryProperties;\r
- m_VulkanFunctions.vkAllocateMemory = (PFN_vkAllocateMemory)vkAllocateMemory;\r
- m_VulkanFunctions.vkFreeMemory = (PFN_vkFreeMemory)vkFreeMemory;\r
- m_VulkanFunctions.vkMapMemory = (PFN_vkMapMemory)vkMapMemory;\r
- m_VulkanFunctions.vkUnmapMemory = (PFN_vkUnmapMemory)vkUnmapMemory;\r
- m_VulkanFunctions.vkFlushMappedMemoryRanges = (PFN_vkFlushMappedMemoryRanges)vkFlushMappedMemoryRanges;\r
- m_VulkanFunctions.vkInvalidateMappedMemoryRanges = (PFN_vkInvalidateMappedMemoryRanges)vkInvalidateMappedMemoryRanges;\r
- m_VulkanFunctions.vkBindBufferMemory = (PFN_vkBindBufferMemory)vkBindBufferMemory;\r
- m_VulkanFunctions.vkBindImageMemory = (PFN_vkBindImageMemory)vkBindImageMemory;\r
- m_VulkanFunctions.vkGetBufferMemoryRequirements = (PFN_vkGetBufferMemoryRequirements)vkGetBufferMemoryRequirements;\r
- m_VulkanFunctions.vkGetImageMemoryRequirements = (PFN_vkGetImageMemoryRequirements)vkGetImageMemoryRequirements;\r
- m_VulkanFunctions.vkCreateBuffer = (PFN_vkCreateBuffer)vkCreateBuffer;\r
- m_VulkanFunctions.vkDestroyBuffer = (PFN_vkDestroyBuffer)vkDestroyBuffer;\r
- m_VulkanFunctions.vkCreateImage = (PFN_vkCreateImage)vkCreateImage;\r
- m_VulkanFunctions.vkDestroyImage = (PFN_vkDestroyImage)vkDestroyImage;\r
- m_VulkanFunctions.vkCmdCopyBuffer = (PFN_vkCmdCopyBuffer)vkCmdCopyBuffer;\r
-\r
- // Vulkan 1.1\r
-#if VMA_VULKAN_VERSION >= 1001000\r
- if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR = (PFN_vkGetBufferMemoryRequirements2)vkGetBufferMemoryRequirements2;\r
- m_VulkanFunctions.vkGetImageMemoryRequirements2KHR = (PFN_vkGetImageMemoryRequirements2)vkGetImageMemoryRequirements2;\r
- m_VulkanFunctions.vkBindBufferMemory2KHR = (PFN_vkBindBufferMemory2)vkBindBufferMemory2;\r
- m_VulkanFunctions.vkBindImageMemory2KHR = (PFN_vkBindImageMemory2)vkBindImageMemory2;\r
- m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR = (PFN_vkGetPhysicalDeviceMemoryProperties2)vkGetPhysicalDeviceMemoryProperties2;\r
- }\r
-#endif\r
-}\r
-\r
-#endif // #if VMA_STATIC_VULKAN_FUNCTIONS == 1\r
-\r
-void VmaAllocator_T::ImportVulkanFunctions_Custom(const VmaVulkanFunctions* pVulkanFunctions)\r
-{\r
- VMA_ASSERT(pVulkanFunctions != VMA_NULL);\r
-\r
-#define VMA_COPY_IF_NOT_NULL(funcName) \\r
- if(pVulkanFunctions->funcName != VMA_NULL) m_VulkanFunctions.funcName = pVulkanFunctions->funcName;\r
-\r
- VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceProperties);\r
- VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties);\r
- VMA_COPY_IF_NOT_NULL(vkAllocateMemory);\r
- VMA_COPY_IF_NOT_NULL(vkFreeMemory);\r
- VMA_COPY_IF_NOT_NULL(vkMapMemory);\r
- VMA_COPY_IF_NOT_NULL(vkUnmapMemory);\r
- VMA_COPY_IF_NOT_NULL(vkFlushMappedMemoryRanges);\r
- VMA_COPY_IF_NOT_NULL(vkInvalidateMappedMemoryRanges);\r
- VMA_COPY_IF_NOT_NULL(vkBindBufferMemory);\r
- VMA_COPY_IF_NOT_NULL(vkBindImageMemory);\r
- VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements);\r
- VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements);\r
- VMA_COPY_IF_NOT_NULL(vkCreateBuffer);\r
- VMA_COPY_IF_NOT_NULL(vkDestroyBuffer);\r
- VMA_COPY_IF_NOT_NULL(vkCreateImage);\r
- VMA_COPY_IF_NOT_NULL(vkDestroyImage);\r
- VMA_COPY_IF_NOT_NULL(vkCmdCopyBuffer);\r
-\r
-#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements2KHR);\r
- VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements2KHR);\r
-#endif\r
-\r
-#if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000\r
- VMA_COPY_IF_NOT_NULL(vkBindBufferMemory2KHR);\r
- VMA_COPY_IF_NOT_NULL(vkBindImageMemory2KHR);\r
-#endif\r
-\r
-#if VMA_MEMORY_BUDGET\r
- VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties2KHR);\r
-#endif\r
-\r
-#undef VMA_COPY_IF_NOT_NULL\r
-}\r
-\r
-#if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1\r
-\r
-void VmaAllocator_T::ImportVulkanFunctions_Dynamic()\r
-{\r
-#define VMA_FETCH_INSTANCE_FUNC(memberName, functionPointerType, functionNameString) \\r
- if(m_VulkanFunctions.memberName == VMA_NULL) \\r
- m_VulkanFunctions.memberName = \\r
- (functionPointerType)vkGetInstanceProcAddr(m_hInstance, functionNameString);\r
-#define VMA_FETCH_DEVICE_FUNC(memberName, functionPointerType, functionNameString) \\r
- if(m_VulkanFunctions.memberName == VMA_NULL) \\r
- m_VulkanFunctions.memberName = \\r
- (functionPointerType)vkGetDeviceProcAddr(m_hDevice, functionNameString);\r
-\r
- VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceProperties, PFN_vkGetPhysicalDeviceProperties, "vkGetPhysicalDeviceProperties");\r
- VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceMemoryProperties, PFN_vkGetPhysicalDeviceMemoryProperties, "vkGetPhysicalDeviceMemoryProperties");\r
- VMA_FETCH_DEVICE_FUNC(vkAllocateMemory, PFN_vkAllocateMemory, "vkAllocateMemory");\r
- VMA_FETCH_DEVICE_FUNC(vkFreeMemory, PFN_vkFreeMemory, "vkFreeMemory");\r
- VMA_FETCH_DEVICE_FUNC(vkMapMemory, PFN_vkMapMemory, "vkMapMemory");\r
- VMA_FETCH_DEVICE_FUNC(vkUnmapMemory, PFN_vkUnmapMemory, "vkUnmapMemory");\r
- VMA_FETCH_DEVICE_FUNC(vkFlushMappedMemoryRanges, PFN_vkFlushMappedMemoryRanges, "vkFlushMappedMemoryRanges");\r
- VMA_FETCH_DEVICE_FUNC(vkInvalidateMappedMemoryRanges, PFN_vkInvalidateMappedMemoryRanges, "vkInvalidateMappedMemoryRanges");\r
- VMA_FETCH_DEVICE_FUNC(vkBindBufferMemory, PFN_vkBindBufferMemory, "vkBindBufferMemory");\r
- VMA_FETCH_DEVICE_FUNC(vkBindImageMemory, PFN_vkBindImageMemory, "vkBindImageMemory");\r
- VMA_FETCH_DEVICE_FUNC(vkGetBufferMemoryRequirements, PFN_vkGetBufferMemoryRequirements, "vkGetBufferMemoryRequirements");\r
- VMA_FETCH_DEVICE_FUNC(vkGetImageMemoryRequirements, PFN_vkGetImageMemoryRequirements, "vkGetImageMemoryRequirements");\r
- VMA_FETCH_DEVICE_FUNC(vkCreateBuffer, PFN_vkCreateBuffer, "vkCreateBuffer");\r
- VMA_FETCH_DEVICE_FUNC(vkDestroyBuffer, PFN_vkDestroyBuffer, "vkDestroyBuffer");\r
- VMA_FETCH_DEVICE_FUNC(vkCreateImage, PFN_vkCreateImage, "vkCreateImage");\r
- VMA_FETCH_DEVICE_FUNC(vkDestroyImage, PFN_vkDestroyImage, "vkDestroyImage");\r
- VMA_FETCH_DEVICE_FUNC(vkCmdCopyBuffer, PFN_vkCmdCopyBuffer, "vkCmdCopyBuffer");\r
-\r
-#if VMA_DEDICATED_ALLOCATION\r
- if(m_UseKhrDedicatedAllocation)\r
- {\r
- VMA_FETCH_DEVICE_FUNC(vkGetBufferMemoryRequirements2KHR, PFN_vkGetBufferMemoryRequirements2KHR, "vkGetBufferMemoryRequirements2KHR");\r
- VMA_FETCH_DEVICE_FUNC(vkGetImageMemoryRequirements2KHR, PFN_vkGetImageMemoryRequirements2KHR, "vkGetImageMemoryRequirements2KHR");\r
- }\r
-#endif\r
-\r
-#if VMA_BIND_MEMORY2\r
- if(m_UseKhrBindMemory2)\r
- {\r
- VMA_FETCH_DEVICE_FUNC(vkBindBufferMemory2KHR, PFN_vkBindBufferMemory2KHR, "vkBindBufferMemory2KHR");\r
- VMA_FETCH_DEVICE_FUNC(vkBindImageMemory2KHR, PFN_vkBindImageMemory2KHR, "vkBindImageMemory2KHR");\r
- }\r
-#endif // #if VMA_BIND_MEMORY2\r
-\r
-#if VMA_MEMORY_BUDGET\r
- if(m_UseExtMemoryBudget && m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceMemoryProperties2KHR, PFN_vkGetPhysicalDeviceMemoryProperties2KHR, "vkGetPhysicalDeviceMemoryProperties2KHR");\r
- }\r
-#endif // #if VMA_MEMORY_BUDGET\r
-\r
-#undef VMA_FETCH_DEVICE_FUNC\r
-#undef VMA_FETCH_INSTANCE_FUNC\r
-}\r
-\r
-#endif // #if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1\r
-\r
-void VmaAllocator_T::ValidateVulkanFunctions()\r
-{\r
- VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceProperties != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkAllocateMemory != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkFreeMemory != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkMapMemory != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkUnmapMemory != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkFlushMappedMemoryRanges != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkInvalidateMappedMemoryRanges != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkBindBufferMemory != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkBindImageMemory != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkGetBufferMemoryRequirements != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkGetImageMemoryRequirements != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkCreateBuffer != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkDestroyBuffer != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkCreateImage != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkDestroyImage != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkCmdCopyBuffer != VMA_NULL);\r
-\r
-#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0) || m_UseKhrDedicatedAllocation)\r
- {\r
- VMA_ASSERT(m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkGetImageMemoryRequirements2KHR != VMA_NULL);\r
- }\r
-#endif\r
-\r
-#if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000\r
- if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0) || m_UseKhrBindMemory2)\r
- {\r
- VMA_ASSERT(m_VulkanFunctions.vkBindBufferMemory2KHR != VMA_NULL);\r
- VMA_ASSERT(m_VulkanFunctions.vkBindImageMemory2KHR != VMA_NULL);\r
- }\r
-#endif\r
-\r
-#if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000\r
- if(m_UseExtMemoryBudget || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR != VMA_NULL);\r
- }\r
-#endif\r
-}\r
-\r
-VkDeviceSize VmaAllocator_T::CalcPreferredBlockSize(uint32_t memTypeIndex)\r
-{\r
- const uint32_t heapIndex = MemoryTypeIndexToHeapIndex(memTypeIndex);\r
- const VkDeviceSize heapSize = m_MemProps.memoryHeaps[heapIndex].size;\r
- const bool isSmallHeap = heapSize <= VMA_SMALL_HEAP_MAX_SIZE;\r
- return VmaAlignUp(isSmallHeap ? (heapSize / 8) : m_PreferredLargeHeapBlockSize, (VkDeviceSize)32);\r
-}\r
-\r
-VkResult VmaAllocator_T::AllocateMemoryOfType(\r
- VkDeviceSize size,\r
- VkDeviceSize alignment,\r
- bool dedicatedAllocation,\r
- VkBuffer dedicatedBuffer,\r
- VkBufferUsageFlags dedicatedBufferUsage,\r
- VkImage dedicatedImage,\r
- const VmaAllocationCreateInfo& createInfo,\r
- uint32_t memTypeIndex,\r
- VmaSuballocationType suballocType,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations)\r
-{\r
- VMA_ASSERT(pAllocations != VMA_NULL);\r
- VMA_DEBUG_LOG(" AllocateMemory: MemoryTypeIndex=%u, AllocationCount=%zu, Size=%llu", memTypeIndex, allocationCount, size);\r
-\r
- VmaAllocationCreateInfo finalCreateInfo = createInfo;\r
-\r
- // If memory type is not HOST_VISIBLE, disable MAPPED.\r
- if((finalCreateInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0 &&\r
- (m_MemProps.memoryTypes[memTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)\r
- {\r
- finalCreateInfo.flags &= ~VMA_ALLOCATION_CREATE_MAPPED_BIT;\r
- }\r
- // If memory is lazily allocated, it should be always dedicated.\r
- if(finalCreateInfo.usage == VMA_MEMORY_USAGE_GPU_LAZILY_ALLOCATED)\r
- {\r
- finalCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;\r
- }\r
-\r
- VmaBlockVector* const blockVector = m_pBlockVectors[memTypeIndex];\r
- VMA_ASSERT(blockVector);\r
-\r
- const VkDeviceSize preferredBlockSize = blockVector->GetPreferredBlockSize();\r
- bool preferDedicatedMemory =\r
- VMA_DEBUG_ALWAYS_DEDICATED_MEMORY ||\r
- dedicatedAllocation ||\r
- // Heuristics: Allocate dedicated memory if requested size if greater than half of preferred block size.\r
- size > preferredBlockSize / 2;\r
-\r
- if(preferDedicatedMemory &&\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) == 0 &&\r
- finalCreateInfo.pool == VK_NULL_HANDLE)\r
- {\r
- finalCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;\r
- }\r
-\r
- if((finalCreateInfo.flags & VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT) != 0)\r
- {\r
- if((finalCreateInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) != 0)\r
- {\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- else\r
- {\r
- return AllocateDedicatedMemory(\r
- size,\r
- suballocType,\r
- memTypeIndex,\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT) != 0,\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0,\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0,\r
- finalCreateInfo.pUserData,\r
- dedicatedBuffer,\r
- dedicatedBufferUsage,\r
- dedicatedImage,\r
- allocationCount,\r
- pAllocations);\r
- }\r
- }\r
- else\r
- {\r
- VkResult res = blockVector->Allocate(\r
- m_CurrentFrameIndex.load(),\r
- size,\r
- alignment,\r
- finalCreateInfo,\r
- suballocType,\r
- allocationCount,\r
- pAllocations);\r
- if(res == VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
-\r
- // 5. Try dedicated memory.\r
- if((finalCreateInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) != 0)\r
- {\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- else\r
- {\r
- res = AllocateDedicatedMemory(\r
- size,\r
- suballocType,\r
- memTypeIndex,\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT) != 0,\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0,\r
- (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0,\r
- finalCreateInfo.pUserData,\r
- dedicatedBuffer,\r
- dedicatedBufferUsage,\r
- dedicatedImage,\r
- allocationCount,\r
- pAllocations);\r
- if(res == VK_SUCCESS)\r
- {\r
- // Succeeded: AllocateDedicatedMemory function already filld pMemory, nothing more to do here.\r
- VMA_DEBUG_LOG(" Allocated as DedicatedMemory");\r
- return VK_SUCCESS;\r
- }\r
- else\r
- {\r
- // Everything failed: Return error code.\r
- VMA_DEBUG_LOG(" vkAllocateMemory FAILED");\r
- return res;\r
- }\r
- }\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::AllocateDedicatedMemory(\r
- VkDeviceSize size,\r
- VmaSuballocationType suballocType,\r
- uint32_t memTypeIndex,\r
- bool withinBudget,\r
- bool map,\r
- bool isUserDataString,\r
- void* pUserData,\r
- VkBuffer dedicatedBuffer,\r
- VkBufferUsageFlags dedicatedBufferUsage,\r
- VkImage dedicatedImage,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations)\r
-{\r
- VMA_ASSERT(allocationCount > 0 && pAllocations);\r
-\r
- if(withinBudget)\r
- {\r
- const uint32_t heapIndex = MemoryTypeIndexToHeapIndex(memTypeIndex);\r
- VmaBudget heapBudget = {};\r
- GetBudget(&heapBudget, heapIndex, 1);\r
- if(heapBudget.usage + size * allocationCount > heapBudget.budget)\r
- {\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- }\r
-\r
- VkMemoryAllocateInfo allocInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };\r
- allocInfo.memoryTypeIndex = memTypeIndex;\r
- allocInfo.allocationSize = size;\r
-\r
-#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- VkMemoryDedicatedAllocateInfoKHR dedicatedAllocInfo = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR };\r
- if(m_UseKhrDedicatedAllocation || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- if(dedicatedBuffer != VK_NULL_HANDLE)\r
- {\r
- VMA_ASSERT(dedicatedImage == VK_NULL_HANDLE);\r
- dedicatedAllocInfo.buffer = dedicatedBuffer;\r
- VmaPnextChainPushFront(&allocInfo, &dedicatedAllocInfo);\r
- }\r
- else if(dedicatedImage != VK_NULL_HANDLE)\r
- {\r
- dedicatedAllocInfo.image = dedicatedImage;\r
- VmaPnextChainPushFront(&allocInfo, &dedicatedAllocInfo);\r
- }\r
- }\r
-#endif // #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
-\r
-#if VMA_BUFFER_DEVICE_ADDRESS\r
- VkMemoryAllocateFlagsInfoKHR allocFlagsInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR };\r
- if(m_UseKhrBufferDeviceAddress)\r
- {\r
- bool canContainBufferWithDeviceAddress = true;\r
- if(dedicatedBuffer != VK_NULL_HANDLE)\r
- {\r
- canContainBufferWithDeviceAddress = dedicatedBufferUsage == UINT32_MAX || // Usage flags unknown\r
- (dedicatedBufferUsage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_EXT) != 0;\r
- }\r
- else if(dedicatedImage != VK_NULL_HANDLE)\r
- {\r
- canContainBufferWithDeviceAddress = false;\r
- }\r
- if(canContainBufferWithDeviceAddress)\r
- {\r
- allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;\r
- VmaPnextChainPushFront(&allocInfo, &allocFlagsInfo);\r
- }\r
- }\r
-#endif // #if VMA_BUFFER_DEVICE_ADDRESS\r
-\r
- size_t allocIndex;\r
- VkResult res = VK_SUCCESS;\r
- for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex)\r
- {\r
- res = AllocateDedicatedMemoryPage(\r
- size,\r
- suballocType,\r
- memTypeIndex,\r
- allocInfo,\r
- map,\r
- isUserDataString,\r
- pUserData,\r
- pAllocations + allocIndex);\r
- if(res != VK_SUCCESS)\r
- {\r
- break;\r
- }\r
- }\r
-\r
- if(res == VK_SUCCESS)\r
- {\r
- // Register them in m_pDedicatedAllocations.\r
- {\r
- VmaMutexLockWrite lock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex);\r
- AllocationVectorType* pDedicatedAllocations = m_pDedicatedAllocations[memTypeIndex];\r
- VMA_ASSERT(pDedicatedAllocations);\r
- for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex)\r
- {\r
- VmaVectorInsertSorted<VmaPointerLess>(*pDedicatedAllocations, pAllocations[allocIndex]);\r
- }\r
- }\r
-\r
- VMA_DEBUG_LOG(" Allocated DedicatedMemory Count=%zu, MemoryTypeIndex=#%u", allocationCount, memTypeIndex);\r
- }\r
- else\r
- {\r
- // Free all already created allocations.\r
- while(allocIndex--)\r
- {\r
- VmaAllocation currAlloc = pAllocations[allocIndex];\r
- VkDeviceMemory hMemory = currAlloc->GetMemory();\r
- \r
- /*\r
- There is no need to call this, because Vulkan spec allows to skip vkUnmapMemory\r
- before vkFreeMemory.\r
-\r
- if(currAlloc->GetMappedData() != VMA_NULL)\r
- {\r
- (*m_VulkanFunctions.vkUnmapMemory)(m_hDevice, hMemory);\r
- }\r
- */\r
- \r
- FreeVulkanMemory(memTypeIndex, currAlloc->GetSize(), hMemory);\r
- m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), currAlloc->GetSize());\r
- currAlloc->SetUserData(this, VMA_NULL);\r
- m_AllocationObjectAllocator.Free(currAlloc);\r
- }\r
-\r
- memset(pAllocations, 0, sizeof(VmaAllocation) * allocationCount);\r
- }\r
-\r
- return res;\r
-}\r
-\r
-VkResult VmaAllocator_T::AllocateDedicatedMemoryPage(\r
- VkDeviceSize size,\r
- VmaSuballocationType suballocType,\r
- uint32_t memTypeIndex,\r
- const VkMemoryAllocateInfo& allocInfo,\r
- bool map,\r
- bool isUserDataString,\r
- void* pUserData,\r
- VmaAllocation* pAllocation)\r
-{\r
- VkDeviceMemory hMemory = VK_NULL_HANDLE;\r
- VkResult res = AllocateVulkanMemory(&allocInfo, &hMemory);\r
- if(res < 0)\r
- {\r
- VMA_DEBUG_LOG(" vkAllocateMemory FAILED");\r
- return res;\r
- }\r
-\r
- void* pMappedData = VMA_NULL;\r
- if(map)\r
- {\r
- res = (*m_VulkanFunctions.vkMapMemory)(\r
- m_hDevice,\r
- hMemory,\r
- 0,\r
- VK_WHOLE_SIZE,\r
- 0,\r
- &pMappedData);\r
- if(res < 0)\r
- {\r
- VMA_DEBUG_LOG(" vkMapMemory FAILED");\r
- FreeVulkanMemory(memTypeIndex, size, hMemory);\r
- return res;\r
- }\r
- }\r
-\r
- *pAllocation = m_AllocationObjectAllocator.Allocate(m_CurrentFrameIndex.load(), isUserDataString);\r
- (*pAllocation)->InitDedicatedAllocation(memTypeIndex, hMemory, suballocType, pMappedData, size);\r
- (*pAllocation)->SetUserData(this, pUserData);\r
- m_Budget.AddAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), size);\r
- if(VMA_DEBUG_INITIALIZE_ALLOCATIONS)\r
- {\r
- FillAllocation(*pAllocation, VMA_ALLOCATION_FILL_PATTERN_CREATED);\r
- }\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaAllocator_T::GetBufferMemoryRequirements(\r
- VkBuffer hBuffer,\r
- VkMemoryRequirements& memReq,\r
- bool& requiresDedicatedAllocation,\r
- bool& prefersDedicatedAllocation) const\r
-{\r
-#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- if(m_UseKhrDedicatedAllocation || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- VkBufferMemoryRequirementsInfo2KHR memReqInfo = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2_KHR };\r
- memReqInfo.buffer = hBuffer;\r
-\r
- VkMemoryDedicatedRequirementsKHR memDedicatedReq = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR };\r
-\r
- VkMemoryRequirements2KHR memReq2 = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR };\r
- VmaPnextChainPushFront(&memReq2, &memDedicatedReq);\r
-\r
- (*m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR)(m_hDevice, &memReqInfo, &memReq2);\r
-\r
- memReq = memReq2.memoryRequirements;\r
- requiresDedicatedAllocation = (memDedicatedReq.requiresDedicatedAllocation != VK_FALSE);\r
- prefersDedicatedAllocation = (memDedicatedReq.prefersDedicatedAllocation != VK_FALSE);\r
- }\r
- else\r
-#endif // #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- {\r
- (*m_VulkanFunctions.vkGetBufferMemoryRequirements)(m_hDevice, hBuffer, &memReq);\r
- requiresDedicatedAllocation = false;\r
- prefersDedicatedAllocation = false;\r
- }\r
-}\r
-\r
-void VmaAllocator_T::GetImageMemoryRequirements(\r
- VkImage hImage,\r
- VkMemoryRequirements& memReq,\r
- bool& requiresDedicatedAllocation,\r
- bool& prefersDedicatedAllocation) const\r
-{\r
-#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- if(m_UseKhrDedicatedAllocation || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))\r
- {\r
- VkImageMemoryRequirementsInfo2KHR memReqInfo = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2_KHR };\r
- memReqInfo.image = hImage;\r
-\r
- VkMemoryDedicatedRequirementsKHR memDedicatedReq = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR };\r
-\r
- VkMemoryRequirements2KHR memReq2 = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR };\r
- VmaPnextChainPushFront(&memReq2, &memDedicatedReq);\r
-\r
- (*m_VulkanFunctions.vkGetImageMemoryRequirements2KHR)(m_hDevice, &memReqInfo, &memReq2);\r
-\r
- memReq = memReq2.memoryRequirements;\r
- requiresDedicatedAllocation = (memDedicatedReq.requiresDedicatedAllocation != VK_FALSE);\r
- prefersDedicatedAllocation = (memDedicatedReq.prefersDedicatedAllocation != VK_FALSE);\r
- }\r
- else\r
-#endif // #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000\r
- {\r
- (*m_VulkanFunctions.vkGetImageMemoryRequirements)(m_hDevice, hImage, &memReq);\r
- requiresDedicatedAllocation = false;\r
- prefersDedicatedAllocation = false;\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::AllocateMemory(\r
- const VkMemoryRequirements& vkMemReq,\r
- bool requiresDedicatedAllocation,\r
- bool prefersDedicatedAllocation,\r
- VkBuffer dedicatedBuffer,\r
- VkBufferUsageFlags dedicatedBufferUsage,\r
- VkImage dedicatedImage,\r
- const VmaAllocationCreateInfo& createInfo,\r
- VmaSuballocationType suballocType,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations)\r
-{\r
- memset(pAllocations, 0, sizeof(VmaAllocation) * allocationCount);\r
-\r
- VMA_ASSERT(VmaIsPow2(vkMemReq.alignment));\r
-\r
- if(vkMemReq.size == 0)\r
- {\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- if((createInfo.flags & VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT) != 0 &&\r
- (createInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) != 0)\r
- {\r
- VMA_ASSERT(0 && "Specifying VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT together with VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT makes no sense.");\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- if((createInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0 &&\r
- (createInfo.flags & VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT) != 0)\r
- {\r
- VMA_ASSERT(0 && "Specifying VMA_ALLOCATION_CREATE_MAPPED_BIT together with VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT is invalid.");\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- if(requiresDedicatedAllocation)\r
- {\r
- if((createInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) != 0)\r
- {\r
- VMA_ASSERT(0 && "VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT specified while dedicated allocation is required.");\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- if(createInfo.pool != VK_NULL_HANDLE)\r
- {\r
- VMA_ASSERT(0 && "Pool specified while dedicated allocation is required.");\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- }\r
- if((createInfo.pool != VK_NULL_HANDLE) &&\r
- ((createInfo.flags & (VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT)) != 0))\r
- {\r
- VMA_ASSERT(0 && "Specifying VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT when pool != null is invalid.");\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
-\r
- if(createInfo.pool != VK_NULL_HANDLE)\r
- {\r
- const VkDeviceSize alignmentForPool = VMA_MAX(\r
- vkMemReq.alignment,\r
- GetMemoryTypeMinAlignment(createInfo.pool->m_BlockVector.GetMemoryTypeIndex()));\r
-\r
- VmaAllocationCreateInfo createInfoForPool = createInfo;\r
- // If memory type is not HOST_VISIBLE, disable MAPPED.\r
- if((createInfoForPool.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0 &&\r
- (m_MemProps.memoryTypes[createInfo.pool->m_BlockVector.GetMemoryTypeIndex()].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)\r
- {\r
- createInfoForPool.flags &= ~VMA_ALLOCATION_CREATE_MAPPED_BIT;\r
- }\r
-\r
- return createInfo.pool->m_BlockVector.Allocate(\r
- m_CurrentFrameIndex.load(),\r
- vkMemReq.size,\r
- alignmentForPool,\r
- createInfoForPool,\r
- suballocType,\r
- allocationCount,\r
- pAllocations);\r
- }\r
- else\r
- {\r
- // Bit mask of memory Vulkan types acceptable for this allocation.\r
- uint32_t memoryTypeBits = vkMemReq.memoryTypeBits;\r
- uint32_t memTypeIndex = UINT32_MAX;\r
- VkResult res = vmaFindMemoryTypeIndex(this, memoryTypeBits, &createInfo, &memTypeIndex);\r
- if(res == VK_SUCCESS)\r
- {\r
- VkDeviceSize alignmentForMemType = VMA_MAX(\r
- vkMemReq.alignment,\r
- GetMemoryTypeMinAlignment(memTypeIndex));\r
-\r
- res = AllocateMemoryOfType(\r
- vkMemReq.size,\r
- alignmentForMemType,\r
- requiresDedicatedAllocation || prefersDedicatedAllocation,\r
- dedicatedBuffer,\r
- dedicatedBufferUsage,\r
- dedicatedImage,\r
- createInfo,\r
- memTypeIndex,\r
- suballocType,\r
- allocationCount,\r
- pAllocations);\r
- // Succeeded on first try.\r
- if(res == VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- // Allocation from this memory type failed. Try other compatible memory types.\r
- else\r
- {\r
- for(;;)\r
- {\r
- // Remove old memTypeIndex from list of possibilities.\r
- memoryTypeBits &= ~(1u << memTypeIndex);\r
- // Find alternative memTypeIndex.\r
- res = vmaFindMemoryTypeIndex(this, memoryTypeBits, &createInfo, &memTypeIndex);\r
- if(res == VK_SUCCESS)\r
- {\r
- alignmentForMemType = VMA_MAX(\r
- vkMemReq.alignment,\r
- GetMemoryTypeMinAlignment(memTypeIndex));\r
- \r
- res = AllocateMemoryOfType(\r
- vkMemReq.size,\r
- alignmentForMemType,\r
- requiresDedicatedAllocation || prefersDedicatedAllocation,\r
- dedicatedBuffer,\r
- dedicatedBufferUsage,\r
- dedicatedImage,\r
- createInfo,\r
- memTypeIndex,\r
- suballocType,\r
- allocationCount,\r
- pAllocations);\r
- // Allocation from this alternative memory type succeeded.\r
- if(res == VK_SUCCESS)\r
- {\r
- return res;\r
- }\r
- // else: Allocation from this memory type failed. Try next one - next loop iteration.\r
- }\r
- // No other matching memory type index could be found.\r
- else\r
- {\r
- // Not returning res, which is VK_ERROR_FEATURE_NOT_PRESENT, because we already failed to allocate once.\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- }\r
- }\r
- }\r
- // Can't find any single memory type maching requirements. res is VK_ERROR_FEATURE_NOT_PRESENT.\r
- else\r
- return res;\r
- }\r
-}\r
-\r
-void VmaAllocator_T::FreeMemory(\r
- size_t allocationCount,\r
- const VmaAllocation* pAllocations)\r
-{\r
- VMA_ASSERT(pAllocations);\r
-\r
- for(size_t allocIndex = allocationCount; allocIndex--; )\r
- {\r
- VmaAllocation allocation = pAllocations[allocIndex];\r
-\r
- if(allocation != VK_NULL_HANDLE)\r
- {\r
- if(TouchAllocation(allocation))\r
- {\r
- if(VMA_DEBUG_INITIALIZE_ALLOCATIONS)\r
- {\r
- FillAllocation(allocation, VMA_ALLOCATION_FILL_PATTERN_DESTROYED);\r
- }\r
-\r
- switch(allocation->GetType())\r
- {\r
- case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:\r
- {\r
- VmaBlockVector* pBlockVector = VMA_NULL;\r
- VmaPool hPool = allocation->GetBlock()->GetParentPool();\r
- if(hPool != VK_NULL_HANDLE)\r
- {\r
- pBlockVector = &hPool->m_BlockVector;\r
- }\r
- else\r
- {\r
- const uint32_t memTypeIndex = allocation->GetMemoryTypeIndex();\r
- pBlockVector = m_pBlockVectors[memTypeIndex];\r
- }\r
- pBlockVector->Free(allocation);\r
- }\r
- break;\r
- case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED:\r
- FreeDedicatedMemory(allocation);\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
- }\r
-\r
- // Do this regardless of whether the allocation is lost. Lost allocations still account to Budget.AllocationBytes.\r
- m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(allocation->GetMemoryTypeIndex()), allocation->GetSize());\r
- allocation->SetUserData(this, VMA_NULL);\r
- m_AllocationObjectAllocator.Free(allocation);\r
- }\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::ResizeAllocation(\r
- const VmaAllocation alloc,\r
- VkDeviceSize newSize)\r
-{\r
- // This function is deprecated and so it does nothing. It's left for backward compatibility.\r
- if(newSize == 0 || alloc->GetLastUseFrameIndex() == VMA_FRAME_INDEX_LOST)\r
- {\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- if(newSize == alloc->GetSize())\r
- {\r
- return VK_SUCCESS;\r
- }\r
- return VK_ERROR_OUT_OF_POOL_MEMORY;\r
-}\r
-\r
-void VmaAllocator_T::CalculateStats(VmaStats* pStats)\r
-{\r
- // Initialize.\r
- InitStatInfo(pStats->total);\r
- for(size_t i = 0; i < VK_MAX_MEMORY_TYPES; ++i)\r
- InitStatInfo(pStats->memoryType[i]);\r
- for(size_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i)\r
- InitStatInfo(pStats->memoryHeap[i]);\r
- \r
- // Process default pools.\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- VmaBlockVector* const pBlockVector = m_pBlockVectors[memTypeIndex];\r
- VMA_ASSERT(pBlockVector);\r
- pBlockVector->AddStats(pStats);\r
- }\r
-\r
- // Process custom pools.\r
- {\r
- VmaMutexLockRead lock(m_PoolsMutex, m_UseMutex);\r
- for(size_t poolIndex = 0, poolCount = m_Pools.size(); poolIndex < poolCount; ++poolIndex)\r
- {\r
- m_Pools[poolIndex]->m_BlockVector.AddStats(pStats);\r
- }\r
- }\r
-\r
- // Process dedicated allocations.\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- const uint32_t memHeapIndex = MemoryTypeIndexToHeapIndex(memTypeIndex);\r
- VmaMutexLockRead dedicatedAllocationsLock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex);\r
- AllocationVectorType* const pDedicatedAllocVector = m_pDedicatedAllocations[memTypeIndex];\r
- VMA_ASSERT(pDedicatedAllocVector);\r
- for(size_t allocIndex = 0, allocCount = pDedicatedAllocVector->size(); allocIndex < allocCount; ++allocIndex)\r
- {\r
- VmaStatInfo allocationStatInfo;\r
- (*pDedicatedAllocVector)[allocIndex]->DedicatedAllocCalcStatsInfo(allocationStatInfo);\r
- VmaAddStatInfo(pStats->total, allocationStatInfo);\r
- VmaAddStatInfo(pStats->memoryType[memTypeIndex], allocationStatInfo);\r
- VmaAddStatInfo(pStats->memoryHeap[memHeapIndex], allocationStatInfo);\r
- }\r
- }\r
-\r
- // Postprocess.\r
- VmaPostprocessCalcStatInfo(pStats->total);\r
- for(size_t i = 0; i < GetMemoryTypeCount(); ++i)\r
- VmaPostprocessCalcStatInfo(pStats->memoryType[i]);\r
- for(size_t i = 0; i < GetMemoryHeapCount(); ++i)\r
- VmaPostprocessCalcStatInfo(pStats->memoryHeap[i]);\r
-}\r
-\r
-void VmaAllocator_T::GetBudget(VmaBudget* outBudget, uint32_t firstHeap, uint32_t heapCount)\r
-{\r
-#if VMA_MEMORY_BUDGET\r
- if(m_UseExtMemoryBudget)\r
- {\r
- if(m_Budget.m_OperationsSinceBudgetFetch < 30)\r
- {\r
- VmaMutexLockRead lockRead(m_Budget.m_BudgetMutex, m_UseMutex);\r
- for(uint32_t i = 0; i < heapCount; ++i, ++outBudget)\r
- {\r
- const uint32_t heapIndex = firstHeap + i;\r
-\r
- outBudget->blockBytes = m_Budget.m_BlockBytes[heapIndex];\r
- outBudget->allocationBytes = m_Budget.m_AllocationBytes[heapIndex];\r
-\r
- if(m_Budget.m_VulkanUsage[heapIndex] + outBudget->blockBytes > m_Budget.m_BlockBytesAtBudgetFetch[heapIndex])\r
- {\r
- outBudget->usage = m_Budget.m_VulkanUsage[heapIndex] +\r
- outBudget->blockBytes - m_Budget.m_BlockBytesAtBudgetFetch[heapIndex];\r
- }\r
- else\r
- {\r
- outBudget->usage = 0;\r
- }\r
-\r
- // Have to take MIN with heap size because explicit HeapSizeLimit is included in it.\r
- outBudget->budget = VMA_MIN(\r
- m_Budget.m_VulkanBudget[heapIndex], m_MemProps.memoryHeaps[heapIndex].size);\r
- }\r
- }\r
- else\r
- {\r
- UpdateVulkanBudget(); // Outside of mutex lock\r
- GetBudget(outBudget, firstHeap, heapCount); // Recursion\r
- }\r
- }\r
- else\r
-#endif\r
- {\r
- for(uint32_t i = 0; i < heapCount; ++i, ++outBudget)\r
- {\r
- const uint32_t heapIndex = firstHeap + i;\r
-\r
- outBudget->blockBytes = m_Budget.m_BlockBytes[heapIndex];\r
- outBudget->allocationBytes = m_Budget.m_AllocationBytes[heapIndex];\r
-\r
- outBudget->usage = outBudget->blockBytes;\r
- outBudget->budget = m_MemProps.memoryHeaps[heapIndex].size * 8 / 10; // 80% heuristics.\r
- }\r
- }\r
-}\r
-\r
-static const uint32_t VMA_VENDOR_ID_AMD = 4098;\r
-\r
-VkResult VmaAllocator_T::DefragmentationBegin(\r
- const VmaDefragmentationInfo2& info,\r
- VmaDefragmentationStats* pStats,\r
- VmaDefragmentationContext* pContext)\r
-{\r
- if(info.pAllocationsChanged != VMA_NULL)\r
- {\r
- memset(info.pAllocationsChanged, 0, info.allocationCount * sizeof(VkBool32));\r
- }\r
-\r
- *pContext = vma_new(this, VmaDefragmentationContext_T)(\r
- this, m_CurrentFrameIndex.load(), info.flags, pStats);\r
-\r
- (*pContext)->AddPools(info.poolCount, info.pPools);\r
- (*pContext)->AddAllocations(\r
- info.allocationCount, info.pAllocations, info.pAllocationsChanged);\r
-\r
- VkResult res = (*pContext)->Defragment(\r
- info.maxCpuBytesToMove, info.maxCpuAllocationsToMove,\r
- info.maxGpuBytesToMove, info.maxGpuAllocationsToMove,\r
- info.commandBuffer, pStats, info.flags);\r
-\r
- if(res != VK_NOT_READY)\r
- {\r
- vma_delete(this, *pContext);\r
- *pContext = VMA_NULL;\r
- }\r
-\r
- return res;\r
-}\r
-\r
-VkResult VmaAllocator_T::DefragmentationEnd(\r
- VmaDefragmentationContext context)\r
-{\r
- vma_delete(this, context);\r
- return VK_SUCCESS;\r
-}\r
-\r
-VkResult VmaAllocator_T::DefragmentationPassBegin(\r
- VmaDefragmentationPassInfo* pInfo,\r
- VmaDefragmentationContext context)\r
-{\r
- return context->DefragmentPassBegin(pInfo);\r
-}\r
-VkResult VmaAllocator_T::DefragmentationPassEnd(\r
- VmaDefragmentationContext context)\r
-{\r
- return context->DefragmentPassEnd();\r
- \r
-}\r
-\r
-void VmaAllocator_T::GetAllocationInfo(VmaAllocation hAllocation, VmaAllocationInfo* pAllocationInfo)\r
-{\r
- if(hAllocation->CanBecomeLost())\r
- {\r
- /*\r
- Warning: This is a carefully designed algorithm.\r
- Do not modify unless you really know what you're doing :)\r
- */\r
- const uint32_t localCurrFrameIndex = m_CurrentFrameIndex.load();\r
- uint32_t localLastUseFrameIndex = hAllocation->GetLastUseFrameIndex();\r
- for(;;)\r
- {\r
- if(localLastUseFrameIndex == VMA_FRAME_INDEX_LOST)\r
- {\r
- pAllocationInfo->memoryType = UINT32_MAX;\r
- pAllocationInfo->deviceMemory = VK_NULL_HANDLE;\r
- pAllocationInfo->offset = 0;\r
- pAllocationInfo->size = hAllocation->GetSize();\r
- pAllocationInfo->pMappedData = VMA_NULL;\r
- pAllocationInfo->pUserData = hAllocation->GetUserData();\r
- return;\r
- }\r
- else if(localLastUseFrameIndex == localCurrFrameIndex)\r
- {\r
- pAllocationInfo->memoryType = hAllocation->GetMemoryTypeIndex();\r
- pAllocationInfo->deviceMemory = hAllocation->GetMemory();\r
- pAllocationInfo->offset = hAllocation->GetOffset();\r
- pAllocationInfo->size = hAllocation->GetSize();\r
- pAllocationInfo->pMappedData = VMA_NULL;\r
- pAllocationInfo->pUserData = hAllocation->GetUserData();\r
- return;\r
- }\r
- else // Last use time earlier than current time.\r
- {\r
- if(hAllocation->CompareExchangeLastUseFrameIndex(localLastUseFrameIndex, localCurrFrameIndex))\r
- {\r
- localLastUseFrameIndex = localCurrFrameIndex;\r
- }\r
- }\r
- }\r
- }\r
- else\r
- {\r
-#if VMA_STATS_STRING_ENABLED\r
- uint32_t localCurrFrameIndex = m_CurrentFrameIndex.load();\r
- uint32_t localLastUseFrameIndex = hAllocation->GetLastUseFrameIndex();\r
- for(;;)\r
- {\r
- VMA_ASSERT(localLastUseFrameIndex != VMA_FRAME_INDEX_LOST);\r
- if(localLastUseFrameIndex == localCurrFrameIndex)\r
- {\r
- break;\r
- }\r
- else // Last use time earlier than current time.\r
- {\r
- if(hAllocation->CompareExchangeLastUseFrameIndex(localLastUseFrameIndex, localCurrFrameIndex))\r
- {\r
- localLastUseFrameIndex = localCurrFrameIndex;\r
- }\r
- }\r
- }\r
-#endif\r
-\r
- pAllocationInfo->memoryType = hAllocation->GetMemoryTypeIndex();\r
- pAllocationInfo->deviceMemory = hAllocation->GetMemory();\r
- pAllocationInfo->offset = hAllocation->GetOffset();\r
- pAllocationInfo->size = hAllocation->GetSize();\r
- pAllocationInfo->pMappedData = hAllocation->GetMappedData();\r
- pAllocationInfo->pUserData = hAllocation->GetUserData();\r
- }\r
-}\r
-\r
-bool VmaAllocator_T::TouchAllocation(VmaAllocation hAllocation)\r
-{\r
- // This is a stripped-down version of VmaAllocator_T::GetAllocationInfo.\r
- if(hAllocation->CanBecomeLost())\r
- {\r
- uint32_t localCurrFrameIndex = m_CurrentFrameIndex.load();\r
- uint32_t localLastUseFrameIndex = hAllocation->GetLastUseFrameIndex();\r
- for(;;)\r
- {\r
- if(localLastUseFrameIndex == VMA_FRAME_INDEX_LOST)\r
- {\r
- return false;\r
- }\r
- else if(localLastUseFrameIndex == localCurrFrameIndex)\r
- {\r
- return true;\r
- }\r
- else // Last use time earlier than current time.\r
- {\r
- if(hAllocation->CompareExchangeLastUseFrameIndex(localLastUseFrameIndex, localCurrFrameIndex))\r
- {\r
- localLastUseFrameIndex = localCurrFrameIndex;\r
- }\r
- }\r
- }\r
- }\r
- else\r
- {\r
-#if VMA_STATS_STRING_ENABLED\r
- uint32_t localCurrFrameIndex = m_CurrentFrameIndex.load();\r
- uint32_t localLastUseFrameIndex = hAllocation->GetLastUseFrameIndex();\r
- for(;;)\r
- {\r
- VMA_ASSERT(localLastUseFrameIndex != VMA_FRAME_INDEX_LOST);\r
- if(localLastUseFrameIndex == localCurrFrameIndex)\r
- {\r
- break;\r
- }\r
- else // Last use time earlier than current time.\r
- {\r
- if(hAllocation->CompareExchangeLastUseFrameIndex(localLastUseFrameIndex, localCurrFrameIndex))\r
- {\r
- localLastUseFrameIndex = localCurrFrameIndex;\r
- }\r
- }\r
- }\r
-#endif\r
-\r
- return true;\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::CreatePool(const VmaPoolCreateInfo* pCreateInfo, VmaPool* pPool)\r
-{\r
- VMA_DEBUG_LOG(" CreatePool: MemoryTypeIndex=%u, flags=%u", pCreateInfo->memoryTypeIndex, pCreateInfo->flags);\r
-\r
- VmaPoolCreateInfo newCreateInfo = *pCreateInfo;\r
-\r
- if(newCreateInfo.maxBlockCount == 0)\r
- {\r
- newCreateInfo.maxBlockCount = SIZE_MAX;\r
- }\r
- if(newCreateInfo.minBlockCount > newCreateInfo.maxBlockCount)\r
- {\r
- return VK_ERROR_INITIALIZATION_FAILED;\r
- }\r
- // Memory type index out of range or forbidden.\r
- if(pCreateInfo->memoryTypeIndex >= GetMemoryTypeCount() ||\r
- ((1u << pCreateInfo->memoryTypeIndex) & m_GlobalMemoryTypeBits) == 0)\r
- {\r
- return VK_ERROR_FEATURE_NOT_PRESENT;\r
- }\r
-\r
- const VkDeviceSize preferredBlockSize = CalcPreferredBlockSize(newCreateInfo.memoryTypeIndex);\r
-\r
- *pPool = vma_new(this, VmaPool_T)(this, newCreateInfo, preferredBlockSize);\r
-\r
- VkResult res = (*pPool)->m_BlockVector.CreateMinBlocks();\r
- if(res != VK_SUCCESS)\r
- {\r
- vma_delete(this, *pPool);\r
- *pPool = VMA_NULL;\r
- return res;\r
- }\r
-\r
- // Add to m_Pools.\r
- {\r
- VmaMutexLockWrite lock(m_PoolsMutex, m_UseMutex);\r
- (*pPool)->SetId(m_NextPoolId++);\r
- VmaVectorInsertSorted<VmaPointerLess>(m_Pools, *pPool);\r
- }\r
-\r
- return VK_SUCCESS;\r
-}\r
-\r
-void VmaAllocator_T::DestroyPool(VmaPool pool)\r
-{\r
- // Remove from m_Pools.\r
- {\r
- VmaMutexLockWrite lock(m_PoolsMutex, m_UseMutex);\r
- bool success = VmaVectorRemoveSorted<VmaPointerLess>(m_Pools, pool);\r
- VMA_ASSERT(success && "Pool not found in Allocator.");\r
- }\r
-\r
- vma_delete(this, pool);\r
-}\r
-\r
-void VmaAllocator_T::GetPoolStats(VmaPool pool, VmaPoolStats* pPoolStats)\r
-{\r
- pool->m_BlockVector.GetPoolStats(pPoolStats);\r
-}\r
-\r
-void VmaAllocator_T::SetCurrentFrameIndex(uint32_t frameIndex)\r
-{\r
- m_CurrentFrameIndex.store(frameIndex);\r
-\r
-#if VMA_MEMORY_BUDGET\r
- if(m_UseExtMemoryBudget)\r
- {\r
- UpdateVulkanBudget();\r
- }\r
-#endif // #if VMA_MEMORY_BUDGET\r
-}\r
-\r
-void VmaAllocator_T::MakePoolAllocationsLost(\r
- VmaPool hPool,\r
- size_t* pLostAllocationCount)\r
-{\r
- hPool->m_BlockVector.MakePoolAllocationsLost(\r
- m_CurrentFrameIndex.load(),\r
- pLostAllocationCount);\r
-}\r
-\r
-VkResult VmaAllocator_T::CheckPoolCorruption(VmaPool hPool)\r
-{\r
- return hPool->m_BlockVector.CheckCorruption();\r
-}\r
-\r
-VkResult VmaAllocator_T::CheckCorruption(uint32_t memoryTypeBits)\r
-{\r
- VkResult finalRes = VK_ERROR_FEATURE_NOT_PRESENT;\r
-\r
- // Process default pools.\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- if(((1u << memTypeIndex) & memoryTypeBits) != 0)\r
- {\r
- VmaBlockVector* const pBlockVector = m_pBlockVectors[memTypeIndex];\r
- VMA_ASSERT(pBlockVector);\r
- VkResult localRes = pBlockVector->CheckCorruption();\r
- switch(localRes)\r
- {\r
- case VK_ERROR_FEATURE_NOT_PRESENT:\r
- break;\r
- case VK_SUCCESS:\r
- finalRes = VK_SUCCESS;\r
- break;\r
- default:\r
- return localRes;\r
- }\r
- }\r
- }\r
-\r
- // Process custom pools.\r
- {\r
- VmaMutexLockRead lock(m_PoolsMutex, m_UseMutex);\r
- for(size_t poolIndex = 0, poolCount = m_Pools.size(); poolIndex < poolCount; ++poolIndex)\r
- {\r
- if(((1u << m_Pools[poolIndex]->m_BlockVector.GetMemoryTypeIndex()) & memoryTypeBits) != 0)\r
- {\r
- VkResult localRes = m_Pools[poolIndex]->m_BlockVector.CheckCorruption();\r
- switch(localRes)\r
- {\r
- case VK_ERROR_FEATURE_NOT_PRESENT:\r
- break;\r
- case VK_SUCCESS:\r
- finalRes = VK_SUCCESS;\r
- break;\r
- default:\r
- return localRes;\r
- }\r
- }\r
- }\r
- }\r
-\r
- return finalRes;\r
-}\r
-\r
-void VmaAllocator_T::CreateLostAllocation(VmaAllocation* pAllocation)\r
-{\r
- *pAllocation = m_AllocationObjectAllocator.Allocate(VMA_FRAME_INDEX_LOST, false);\r
- (*pAllocation)->InitLost();\r
-}\r
-\r
-VkResult VmaAllocator_T::AllocateVulkanMemory(const VkMemoryAllocateInfo* pAllocateInfo, VkDeviceMemory* pMemory)\r
-{\r
- const uint32_t heapIndex = MemoryTypeIndexToHeapIndex(pAllocateInfo->memoryTypeIndex);\r
-\r
- // HeapSizeLimit is in effect for this heap.\r
- if((m_HeapSizeLimitMask & (1u << heapIndex)) != 0)\r
- {\r
- const VkDeviceSize heapSize = m_MemProps.memoryHeaps[heapIndex].size;\r
- VkDeviceSize blockBytes = m_Budget.m_BlockBytes[heapIndex];\r
- for(;;)\r
- {\r
- const VkDeviceSize blockBytesAfterAllocation = blockBytes + pAllocateInfo->allocationSize;\r
- if(blockBytesAfterAllocation > heapSize)\r
- {\r
- return VK_ERROR_OUT_OF_DEVICE_MEMORY;\r
- }\r
- if(m_Budget.m_BlockBytes[heapIndex].compare_exchange_strong(blockBytes, blockBytesAfterAllocation))\r
- {\r
- break;\r
- }\r
- }\r
- }\r
- else\r
- {\r
- m_Budget.m_BlockBytes[heapIndex] += pAllocateInfo->allocationSize;\r
- }\r
-\r
- // VULKAN CALL vkAllocateMemory.\r
- VkResult res = (*m_VulkanFunctions.vkAllocateMemory)(m_hDevice, pAllocateInfo, GetAllocationCallbacks(), pMemory);\r
-\r
- if(res == VK_SUCCESS)\r
- {\r
-#if VMA_MEMORY_BUDGET\r
- ++m_Budget.m_OperationsSinceBudgetFetch;\r
-#endif\r
-\r
- // Informative callback.\r
- if(m_DeviceMemoryCallbacks.pfnAllocate != VMA_NULL)\r
- {\r
- (*m_DeviceMemoryCallbacks.pfnAllocate)(this, pAllocateInfo->memoryTypeIndex, *pMemory, pAllocateInfo->allocationSize, m_DeviceMemoryCallbacks.pUserData);\r
- }\r
- }\r
- else\r
- {\r
- m_Budget.m_BlockBytes[heapIndex] -= pAllocateInfo->allocationSize;\r
- }\r
-\r
- return res;\r
-}\r
-\r
-void VmaAllocator_T::FreeVulkanMemory(uint32_t memoryType, VkDeviceSize size, VkDeviceMemory hMemory)\r
-{\r
- // Informative callback.\r
- if(m_DeviceMemoryCallbacks.pfnFree != VMA_NULL)\r
- {\r
- (*m_DeviceMemoryCallbacks.pfnFree)(this, memoryType, hMemory, size, m_DeviceMemoryCallbacks.pUserData);\r
- }\r
-\r
- // VULKAN CALL vkFreeMemory.\r
- (*m_VulkanFunctions.vkFreeMemory)(m_hDevice, hMemory, GetAllocationCallbacks());\r
-\r
- m_Budget.m_BlockBytes[MemoryTypeIndexToHeapIndex(memoryType)] -= size;\r
-}\r
-\r
-VkResult VmaAllocator_T::BindVulkanBuffer(\r
- VkDeviceMemory memory,\r
- VkDeviceSize memoryOffset,\r
- VkBuffer buffer,\r
- const void* pNext)\r
-{\r
- if(pNext != VMA_NULL)\r
- {\r
-#if VMA_VULKAN_VERSION >= 1001000 || VMA_BIND_MEMORY2\r
- if((m_UseKhrBindMemory2 || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)) &&\r
- m_VulkanFunctions.vkBindBufferMemory2KHR != VMA_NULL)\r
- {\r
- VkBindBufferMemoryInfoKHR bindBufferMemoryInfo = { VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR };\r
- bindBufferMemoryInfo.pNext = pNext;\r
- bindBufferMemoryInfo.buffer = buffer;\r
- bindBufferMemoryInfo.memory = memory;\r
- bindBufferMemoryInfo.memoryOffset = memoryOffset;\r
- return (*m_VulkanFunctions.vkBindBufferMemory2KHR)(m_hDevice, 1, &bindBufferMemoryInfo);\r
- }\r
- else\r
-#endif // #if VMA_VULKAN_VERSION >= 1001000 || VMA_BIND_MEMORY2\r
- {\r
- return VK_ERROR_EXTENSION_NOT_PRESENT;\r
- }\r
- }\r
- else\r
- {\r
- return (*m_VulkanFunctions.vkBindBufferMemory)(m_hDevice, buffer, memory, memoryOffset);\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::BindVulkanImage(\r
- VkDeviceMemory memory,\r
- VkDeviceSize memoryOffset,\r
- VkImage image,\r
- const void* pNext)\r
-{\r
- if(pNext != VMA_NULL)\r
- {\r
-#if VMA_VULKAN_VERSION >= 1001000 || VMA_BIND_MEMORY2\r
- if((m_UseKhrBindMemory2 || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)) &&\r
- m_VulkanFunctions.vkBindImageMemory2KHR != VMA_NULL)\r
- {\r
- VkBindImageMemoryInfoKHR bindBufferMemoryInfo = { VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO_KHR };\r
- bindBufferMemoryInfo.pNext = pNext;\r
- bindBufferMemoryInfo.image = image;\r
- bindBufferMemoryInfo.memory = memory;\r
- bindBufferMemoryInfo.memoryOffset = memoryOffset;\r
- return (*m_VulkanFunctions.vkBindImageMemory2KHR)(m_hDevice, 1, &bindBufferMemoryInfo);\r
- }\r
- else\r
-#endif // #if VMA_BIND_MEMORY2\r
- {\r
- return VK_ERROR_EXTENSION_NOT_PRESENT;\r
- }\r
- }\r
- else\r
- {\r
- return (*m_VulkanFunctions.vkBindImageMemory)(m_hDevice, image, memory, memoryOffset);\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::Map(VmaAllocation hAllocation, void** ppData)\r
-{\r
- if(hAllocation->CanBecomeLost())\r
- {\r
- return VK_ERROR_MEMORY_MAP_FAILED;\r
- }\r
-\r
- switch(hAllocation->GetType())\r
- {\r
- case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = hAllocation->GetBlock();\r
- char *pBytes = VMA_NULL;\r
- VkResult res = pBlock->Map(this, 1, (void**)&pBytes);\r
- if(res == VK_SUCCESS)\r
- {\r
- *ppData = pBytes + (ptrdiff_t)hAllocation->GetOffset();\r
- hAllocation->BlockAllocMap();\r
- }\r
- return res;\r
- }\r
- case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED:\r
- return hAllocation->DedicatedAllocMap(this, ppData);\r
- default:\r
- VMA_ASSERT(0);\r
- return VK_ERROR_MEMORY_MAP_FAILED;\r
- }\r
-}\r
-\r
-void VmaAllocator_T::Unmap(VmaAllocation hAllocation)\r
-{\r
- switch(hAllocation->GetType())\r
- {\r
- case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = hAllocation->GetBlock();\r
- hAllocation->BlockAllocUnmap();\r
- pBlock->Unmap(this, 1);\r
- }\r
- break;\r
- case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED:\r
- hAllocation->DedicatedAllocUnmap(this);\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
-}\r
-\r
-VkResult VmaAllocator_T::BindBufferMemory(\r
- VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkBuffer hBuffer,\r
- const void* pNext)\r
-{\r
- VkResult res = VK_SUCCESS;\r
- switch(hAllocation->GetType())\r
- {\r
- case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED:\r
- res = BindVulkanBuffer(hAllocation->GetMemory(), allocationLocalOffset, hBuffer, pNext);\r
- break;\r
- case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:\r
- {\r
- VmaDeviceMemoryBlock* const pBlock = hAllocation->GetBlock();\r
- VMA_ASSERT(pBlock && "Binding buffer to allocation that doesn't belong to any block. Is the allocation lost?");\r
- res = pBlock->BindBufferMemory(this, hAllocation, allocationLocalOffset, hBuffer, pNext);\r
- break;\r
- }\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
- return res;\r
-}\r
-\r
-VkResult VmaAllocator_T::BindImageMemory(\r
- VmaAllocation hAllocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkImage hImage,\r
- const void* pNext)\r
-{\r
- VkResult res = VK_SUCCESS;\r
- switch(hAllocation->GetType())\r
- {\r
- case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED:\r
- res = BindVulkanImage(hAllocation->GetMemory(), allocationLocalOffset, hImage, pNext);\r
- break;\r
- case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:\r
- {\r
- VmaDeviceMemoryBlock* pBlock = hAllocation->GetBlock();\r
- VMA_ASSERT(pBlock && "Binding image to allocation that doesn't belong to any block. Is the allocation lost?");\r
- res = pBlock->BindImageMemory(this, hAllocation, allocationLocalOffset, hImage, pNext);\r
- break;\r
- }\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
- return res;\r
-}\r
-\r
-VkResult VmaAllocator_T::FlushOrInvalidateAllocation(\r
- VmaAllocation hAllocation,\r
- VkDeviceSize offset, VkDeviceSize size,\r
- VMA_CACHE_OPERATION op)\r
-{\r
- VkResult res = VK_SUCCESS;\r
-\r
- VkMappedMemoryRange memRange = {};\r
- if(GetFlushOrInvalidateRange(hAllocation, offset, size, memRange))\r
- {\r
- switch(op)\r
- {\r
- case VMA_CACHE_FLUSH:\r
- res = (*GetVulkanFunctions().vkFlushMappedMemoryRanges)(m_hDevice, 1, &memRange);\r
- break;\r
- case VMA_CACHE_INVALIDATE:\r
- res = (*GetVulkanFunctions().vkInvalidateMappedMemoryRanges)(m_hDevice, 1, &memRange);\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
- }\r
- // else: Just ignore this call.\r
- return res;\r
-}\r
-\r
-VkResult VmaAllocator_T::FlushOrInvalidateAllocations(\r
- uint32_t allocationCount,\r
- const VmaAllocation* allocations,\r
- const VkDeviceSize* offsets, const VkDeviceSize* sizes,\r
- VMA_CACHE_OPERATION op)\r
-{\r
- typedef VmaStlAllocator<VkMappedMemoryRange> RangeAllocator;\r
- typedef VmaSmallVector<VkMappedMemoryRange, RangeAllocator, 16> RangeVector;\r
- RangeVector ranges = RangeVector(RangeAllocator(GetAllocationCallbacks()));\r
- \r
- for(uint32_t allocIndex = 0; allocIndex < allocationCount; ++allocIndex)\r
- {\r
- const VmaAllocation alloc = allocations[allocIndex];\r
- const VkDeviceSize offset = offsets != VMA_NULL ? offsets[allocIndex] : 0;\r
- const VkDeviceSize size = sizes != VMA_NULL ? sizes[allocIndex] : VK_WHOLE_SIZE;\r
- VkMappedMemoryRange newRange;\r
- if(GetFlushOrInvalidateRange(alloc, offset, size, newRange))\r
- {\r
- ranges.push_back(newRange);\r
- }\r
- }\r
-\r
- VkResult res = VK_SUCCESS;\r
- if(!ranges.empty())\r
- {\r
- switch(op)\r
- {\r
- case VMA_CACHE_FLUSH:\r
- res = (*GetVulkanFunctions().vkFlushMappedMemoryRanges)(m_hDevice, (uint32_t)ranges.size(), ranges.data());\r
- break;\r
- case VMA_CACHE_INVALIDATE:\r
- res = (*GetVulkanFunctions().vkInvalidateMappedMemoryRanges)(m_hDevice, (uint32_t)ranges.size(), ranges.data());\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
- }\r
- // else: Just ignore this call.\r
- return res;\r
-}\r
-\r
-void VmaAllocator_T::FreeDedicatedMemory(const VmaAllocation allocation)\r
-{\r
- VMA_ASSERT(allocation && allocation->GetType() == VmaAllocation_T::ALLOCATION_TYPE_DEDICATED);\r
-\r
- const uint32_t memTypeIndex = allocation->GetMemoryTypeIndex();\r
- {\r
- VmaMutexLockWrite lock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex);\r
- AllocationVectorType* const pDedicatedAllocations = m_pDedicatedAllocations[memTypeIndex];\r
- VMA_ASSERT(pDedicatedAllocations);\r
- bool success = VmaVectorRemoveSorted<VmaPointerLess>(*pDedicatedAllocations, allocation);\r
- VMA_ASSERT(success);\r
- }\r
-\r
- VkDeviceMemory hMemory = allocation->GetMemory();\r
- \r
- /*\r
- There is no need to call this, because Vulkan spec allows to skip vkUnmapMemory\r
- before vkFreeMemory.\r
-\r
- if(allocation->GetMappedData() != VMA_NULL)\r
- {\r
- (*m_VulkanFunctions.vkUnmapMemory)(m_hDevice, hMemory);\r
- }\r
- */\r
- \r
- FreeVulkanMemory(memTypeIndex, allocation->GetSize(), hMemory);\r
-\r
- VMA_DEBUG_LOG(" Freed DedicatedMemory MemoryTypeIndex=%u", memTypeIndex);\r
-}\r
-\r
-uint32_t VmaAllocator_T::CalculateGpuDefragmentationMemoryTypeBits() const\r
-{\r
- VkBufferCreateInfo dummyBufCreateInfo;\r
- VmaFillGpuDefragmentationBufferCreateInfo(dummyBufCreateInfo);\r
-\r
- uint32_t memoryTypeBits = 0;\r
-\r
- // Create buffer.\r
- VkBuffer buf = VK_NULL_HANDLE;\r
- VkResult res = (*GetVulkanFunctions().vkCreateBuffer)(\r
- m_hDevice, &dummyBufCreateInfo, GetAllocationCallbacks(), &buf);\r
- if(res == VK_SUCCESS)\r
- {\r
- // Query for supported memory types.\r
- VkMemoryRequirements memReq;\r
- (*GetVulkanFunctions().vkGetBufferMemoryRequirements)(m_hDevice, buf, &memReq);\r
- memoryTypeBits = memReq.memoryTypeBits;\r
-\r
- // Destroy buffer.\r
- (*GetVulkanFunctions().vkDestroyBuffer)(m_hDevice, buf, GetAllocationCallbacks());\r
- }\r
-\r
- return memoryTypeBits;\r
-}\r
-\r
-uint32_t VmaAllocator_T::CalculateGlobalMemoryTypeBits() const\r
-{\r
- // Make sure memory information is already fetched.\r
- VMA_ASSERT(GetMemoryTypeCount() > 0);\r
-\r
- uint32_t memoryTypeBits = UINT32_MAX;\r
-\r
- if(!m_UseAmdDeviceCoherentMemory)\r
- {\r
- // Exclude memory types that have VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD.\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- if((m_MemProps.memoryTypes[memTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY) != 0)\r
- {\r
- memoryTypeBits &= ~(1u << memTypeIndex);\r
- }\r
- }\r
- }\r
-\r
- return memoryTypeBits;\r
-}\r
-\r
-bool VmaAllocator_T::GetFlushOrInvalidateRange(\r
- VmaAllocation allocation,\r
- VkDeviceSize offset, VkDeviceSize size,\r
- VkMappedMemoryRange& outRange) const\r
-{\r
- const uint32_t memTypeIndex = allocation->GetMemoryTypeIndex();\r
- if(size > 0 && IsMemoryTypeNonCoherent(memTypeIndex))\r
- {\r
- const VkDeviceSize nonCoherentAtomSize = m_PhysicalDeviceProperties.limits.nonCoherentAtomSize;\r
- const VkDeviceSize allocationSize = allocation->GetSize();\r
- VMA_ASSERT(offset <= allocationSize);\r
-\r
- outRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;\r
- outRange.pNext = VMA_NULL;\r
- outRange.memory = allocation->GetMemory();\r
-\r
- switch(allocation->GetType())\r
- {\r
- case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED:\r
- outRange.offset = VmaAlignDown(offset, nonCoherentAtomSize);\r
- if(size == VK_WHOLE_SIZE)\r
- {\r
- outRange.size = allocationSize - outRange.offset;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(offset + size <= allocationSize);\r
- outRange.size = VMA_MIN(\r
- VmaAlignUp(size + (offset - outRange.offset), nonCoherentAtomSize),\r
- allocationSize - outRange.offset);\r
- }\r
- break;\r
- case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:\r
- {\r
- // 1. Still within this allocation.\r
- outRange.offset = VmaAlignDown(offset, nonCoherentAtomSize);\r
- if(size == VK_WHOLE_SIZE)\r
- {\r
- size = allocationSize - offset;\r
- }\r
- else\r
- {\r
- VMA_ASSERT(offset + size <= allocationSize);\r
- }\r
- outRange.size = VmaAlignUp(size + (offset - outRange.offset), nonCoherentAtomSize);\r
-\r
- // 2. Adjust to whole block.\r
- const VkDeviceSize allocationOffset = allocation->GetOffset();\r
- VMA_ASSERT(allocationOffset % nonCoherentAtomSize == 0);\r
- const VkDeviceSize blockSize = allocation->GetBlock()->m_pMetadata->GetSize();\r
- outRange.offset += allocationOffset;\r
- outRange.size = VMA_MIN(outRange.size, blockSize - outRange.offset);\r
-\r
- break;\r
- }\r
- default:\r
- VMA_ASSERT(0);\r
- }\r
- return true;\r
- }\r
- return false;\r
-}\r
-\r
-#if VMA_MEMORY_BUDGET\r
-\r
-void VmaAllocator_T::UpdateVulkanBudget()\r
-{\r
- VMA_ASSERT(m_UseExtMemoryBudget);\r
-\r
- VkPhysicalDeviceMemoryProperties2KHR memProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2_KHR };\r
-\r
- VkPhysicalDeviceMemoryBudgetPropertiesEXT budgetProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT };\r
- VmaPnextChainPushFront(&memProps, &budgetProps);\r
-\r
- GetVulkanFunctions().vkGetPhysicalDeviceMemoryProperties2KHR(m_PhysicalDevice, &memProps);\r
-\r
- {\r
- VmaMutexLockWrite lockWrite(m_Budget.m_BudgetMutex, m_UseMutex);\r
-\r
- for(uint32_t heapIndex = 0; heapIndex < GetMemoryHeapCount(); ++heapIndex)\r
- {\r
- m_Budget.m_VulkanUsage[heapIndex] = budgetProps.heapUsage[heapIndex];\r
- m_Budget.m_VulkanBudget[heapIndex] = budgetProps.heapBudget[heapIndex];\r
- m_Budget.m_BlockBytesAtBudgetFetch[heapIndex] = m_Budget.m_BlockBytes[heapIndex].load();\r
-\r
- // Some bugged drivers return the budget incorrectly, e.g. 0 or much bigger than heap size.\r
- if(m_Budget.m_VulkanBudget[heapIndex] == 0)\r
- {\r
- m_Budget.m_VulkanBudget[heapIndex] = m_MemProps.memoryHeaps[heapIndex].size * 8 / 10; // 80% heuristics.\r
- }\r
- else if(m_Budget.m_VulkanBudget[heapIndex] > m_MemProps.memoryHeaps[heapIndex].size)\r
- {\r
- m_Budget.m_VulkanBudget[heapIndex] = m_MemProps.memoryHeaps[heapIndex].size;\r
- }\r
- if(m_Budget.m_VulkanUsage[heapIndex] == 0 && m_Budget.m_BlockBytesAtBudgetFetch[heapIndex] > 0)\r
- {\r
- m_Budget.m_VulkanUsage[heapIndex] = m_Budget.m_BlockBytesAtBudgetFetch[heapIndex];\r
- }\r
- }\r
- m_Budget.m_OperationsSinceBudgetFetch = 0;\r
- }\r
-}\r
-\r
-#endif // #if VMA_MEMORY_BUDGET\r
-\r
-void VmaAllocator_T::FillAllocation(const VmaAllocation hAllocation, uint8_t pattern)\r
-{\r
- if(VMA_DEBUG_INITIALIZE_ALLOCATIONS &&\r
- !hAllocation->CanBecomeLost() &&\r
- (m_MemProps.memoryTypes[hAllocation->GetMemoryTypeIndex()].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0)\r
- {\r
- void* pData = VMA_NULL;\r
- VkResult res = Map(hAllocation, &pData);\r
- if(res == VK_SUCCESS)\r
- {\r
- memset(pData, (int)pattern, (size_t)hAllocation->GetSize());\r
- FlushOrInvalidateAllocation(hAllocation, 0, VK_WHOLE_SIZE, VMA_CACHE_FLUSH);\r
- Unmap(hAllocation);\r
- }\r
- else\r
- {\r
- VMA_ASSERT(0 && "VMA_DEBUG_INITIALIZE_ALLOCATIONS is enabled, but couldn't map memory to fill allocation.");\r
- }\r
- }\r
-}\r
-\r
-uint32_t VmaAllocator_T::GetGpuDefragmentationMemoryTypeBits()\r
-{\r
- uint32_t memoryTypeBits = m_GpuDefragmentationMemoryTypeBits.load();\r
- if(memoryTypeBits == UINT32_MAX)\r
- {\r
- memoryTypeBits = CalculateGpuDefragmentationMemoryTypeBits();\r
- m_GpuDefragmentationMemoryTypeBits.store(memoryTypeBits);\r
- }\r
- return memoryTypeBits;\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-void VmaAllocator_T::PrintDetailedMap(VmaJsonWriter& json)\r
-{\r
- bool dedicatedAllocationsStarted = false;\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- VmaMutexLockRead dedicatedAllocationsLock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex);\r
- AllocationVectorType* const pDedicatedAllocVector = m_pDedicatedAllocations[memTypeIndex];\r
- VMA_ASSERT(pDedicatedAllocVector);\r
- if(pDedicatedAllocVector->empty() == false)\r
- {\r
- if(dedicatedAllocationsStarted == false)\r
- {\r
- dedicatedAllocationsStarted = true;\r
- json.WriteString("DedicatedAllocations");\r
- json.BeginObject();\r
- }\r
-\r
- json.BeginString("Type ");\r
- json.ContinueString(memTypeIndex);\r
- json.EndString();\r
- \r
- json.BeginArray();\r
-\r
- for(size_t i = 0; i < pDedicatedAllocVector->size(); ++i)\r
- {\r
- json.BeginObject(true);\r
- const VmaAllocation hAlloc = (*pDedicatedAllocVector)[i];\r
- hAlloc->PrintParameters(json);\r
- json.EndObject();\r
- }\r
-\r
- json.EndArray();\r
- }\r
- }\r
- if(dedicatedAllocationsStarted)\r
- {\r
- json.EndObject();\r
- }\r
-\r
- {\r
- bool allocationsStarted = false;\r
- for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex)\r
- {\r
- if(m_pBlockVectors[memTypeIndex]->IsEmpty() == false)\r
- {\r
- if(allocationsStarted == false)\r
- {\r
- allocationsStarted = true;\r
- json.WriteString("DefaultPools");\r
- json.BeginObject();\r
- }\r
-\r
- json.BeginString("Type ");\r
- json.ContinueString(memTypeIndex);\r
- json.EndString();\r
-\r
- m_pBlockVectors[memTypeIndex]->PrintDetailedMap(json);\r
- }\r
- }\r
- if(allocationsStarted)\r
- {\r
- json.EndObject();\r
- }\r
- }\r
-\r
- // Custom pools\r
- {\r
- VmaMutexLockRead lock(m_PoolsMutex, m_UseMutex);\r
- const size_t poolCount = m_Pools.size();\r
- if(poolCount > 0)\r
- {\r
- json.WriteString("Pools");\r
- json.BeginObject();\r
- for(size_t poolIndex = 0; poolIndex < poolCount; ++poolIndex)\r
- {\r
- json.BeginString();\r
- json.ContinueString(m_Pools[poolIndex]->GetId());\r
- json.EndString();\r
-\r
- m_Pools[poolIndex]->m_BlockVector.PrintDetailedMap(json);\r
- }\r
- json.EndObject();\r
- }\r
- }\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// Public interface\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateAllocator(\r
- const VmaAllocatorCreateInfo* pCreateInfo,\r
- VmaAllocator* pAllocator)\r
-{\r
- VMA_ASSERT(pCreateInfo && pAllocator);\r
- VMA_ASSERT(pCreateInfo->vulkanApiVersion == 0 ||\r
- (VK_VERSION_MAJOR(pCreateInfo->vulkanApiVersion) == 1 && VK_VERSION_MINOR(pCreateInfo->vulkanApiVersion) <= 2));\r
- VMA_DEBUG_LOG("vmaCreateAllocator");\r
- *pAllocator = vma_new(pCreateInfo->pAllocationCallbacks, VmaAllocator_T)(pCreateInfo);\r
- return (*pAllocator)->Init(pCreateInfo);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyAllocator(\r
- VmaAllocator allocator)\r
-{\r
- if(allocator != VK_NULL_HANDLE)\r
- {\r
- VMA_DEBUG_LOG("vmaDestroyAllocator");\r
- VkAllocationCallbacks allocationCallbacks = allocator->m_AllocationCallbacks;\r
- vma_delete(&allocationCallbacks, allocator);\r
- }\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocatorInfo(VmaAllocator allocator, VmaAllocatorInfo* pAllocatorInfo)\r
-{\r
- VMA_ASSERT(allocator && pAllocatorInfo);\r
- pAllocatorInfo->instance = allocator->m_hInstance;\r
- pAllocatorInfo->physicalDevice = allocator->GetPhysicalDevice();\r
- pAllocatorInfo->device = allocator->m_hDevice;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetPhysicalDeviceProperties(\r
- VmaAllocator allocator,\r
- const VkPhysicalDeviceProperties **ppPhysicalDeviceProperties)\r
-{\r
- VMA_ASSERT(allocator && ppPhysicalDeviceProperties);\r
- *ppPhysicalDeviceProperties = &allocator->m_PhysicalDeviceProperties;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryProperties(\r
- VmaAllocator allocator,\r
- const VkPhysicalDeviceMemoryProperties** ppPhysicalDeviceMemoryProperties)\r
-{\r
- VMA_ASSERT(allocator && ppPhysicalDeviceMemoryProperties);\r
- *ppPhysicalDeviceMemoryProperties = &allocator->m_MemProps;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryTypeProperties(\r
- VmaAllocator allocator,\r
- uint32_t memoryTypeIndex,\r
- VkMemoryPropertyFlags* pFlags)\r
-{\r
- VMA_ASSERT(allocator && pFlags);\r
- VMA_ASSERT(memoryTypeIndex < allocator->GetMemoryTypeCount());\r
- *pFlags = allocator->m_MemProps.memoryTypes[memoryTypeIndex].propertyFlags;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaSetCurrentFrameIndex(\r
- VmaAllocator allocator,\r
- uint32_t frameIndex)\r
-{\r
- VMA_ASSERT(allocator);\r
- VMA_ASSERT(frameIndex != VMA_FRAME_INDEX_LOST);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- allocator->SetCurrentFrameIndex(frameIndex);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaCalculateStats(\r
- VmaAllocator allocator,\r
- VmaStats* pStats)\r
-{\r
- VMA_ASSERT(allocator && pStats);\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
- allocator->CalculateStats(pStats);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetBudget(\r
- VmaAllocator allocator,\r
- VmaBudget* pBudget)\r
-{\r
- VMA_ASSERT(allocator && pBudget);\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
- allocator->GetBudget(pBudget, 0, allocator->GetMemoryHeapCount());\r
-}\r
-\r
-#if VMA_STATS_STRING_ENABLED\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaBuildStatsString(\r
- VmaAllocator allocator,\r
- char** ppStatsString,\r
- VkBool32 detailedMap)\r
-{\r
- VMA_ASSERT(allocator && ppStatsString);\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VmaStringBuilder sb(allocator);\r
- {\r
- VmaJsonWriter json(allocator->GetAllocationCallbacks(), sb);\r
- json.BeginObject();\r
-\r
- VmaBudget budget[VK_MAX_MEMORY_HEAPS];\r
- allocator->GetBudget(budget, 0, allocator->GetMemoryHeapCount());\r
-\r
- VmaStats stats;\r
- allocator->CalculateStats(&stats);\r
-\r
- json.WriteString("Total");\r
- VmaPrintStatInfo(json, stats.total);\r
- \r
- for(uint32_t heapIndex = 0; heapIndex < allocator->GetMemoryHeapCount(); ++heapIndex)\r
- {\r
- json.BeginString("Heap ");\r
- json.ContinueString(heapIndex);\r
- json.EndString();\r
- json.BeginObject();\r
-\r
- json.WriteString("Size");\r
- json.WriteNumber(allocator->m_MemProps.memoryHeaps[heapIndex].size);\r
-\r
- json.WriteString("Flags");\r
- json.BeginArray(true);\r
- if((allocator->m_MemProps.memoryHeaps[heapIndex].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0)\r
- {\r
- json.WriteString("DEVICE_LOCAL");\r
- }\r
- json.EndArray();\r
-\r
- json.WriteString("Budget");\r
- json.BeginObject();\r
- {\r
- json.WriteString("BlockBytes");\r
- json.WriteNumber(budget[heapIndex].blockBytes);\r
- json.WriteString("AllocationBytes");\r
- json.WriteNumber(budget[heapIndex].allocationBytes);\r
- json.WriteString("Usage");\r
- json.WriteNumber(budget[heapIndex].usage);\r
- json.WriteString("Budget");\r
- json.WriteNumber(budget[heapIndex].budget);\r
- }\r
- json.EndObject();\r
-\r
- if(stats.memoryHeap[heapIndex].blockCount > 0)\r
- {\r
- json.WriteString("Stats");\r
- VmaPrintStatInfo(json, stats.memoryHeap[heapIndex]);\r
- }\r
-\r
- for(uint32_t typeIndex = 0; typeIndex < allocator->GetMemoryTypeCount(); ++typeIndex)\r
- {\r
- if(allocator->MemoryTypeIndexToHeapIndex(typeIndex) == heapIndex)\r
- {\r
- json.BeginString("Type ");\r
- json.ContinueString(typeIndex);\r
- json.EndString();\r
-\r
- json.BeginObject();\r
-\r
- json.WriteString("Flags");\r
- json.BeginArray(true);\r
- VkMemoryPropertyFlags flags = allocator->m_MemProps.memoryTypes[typeIndex].propertyFlags;\r
- if((flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0)\r
- {\r
- json.WriteString("DEVICE_LOCAL");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0)\r
- {\r
- json.WriteString("HOST_VISIBLE");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0)\r
- {\r
- json.WriteString("HOST_COHERENT");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) != 0)\r
- {\r
- json.WriteString("HOST_CACHED");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) != 0)\r
- {\r
- json.WriteString("LAZILY_ALLOCATED");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_PROTECTED_BIT) != 0)\r
- {\r
- json.WriteString(" PROTECTED");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY) != 0)\r
- {\r
- json.WriteString(" DEVICE_COHERENT");\r
- }\r
- if((flags & VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY) != 0)\r
- {\r
- json.WriteString(" DEVICE_UNCACHED");\r
- }\r
- json.EndArray();\r
-\r
- if(stats.memoryType[typeIndex].blockCount > 0)\r
- {\r
- json.WriteString("Stats");\r
- VmaPrintStatInfo(json, stats.memoryType[typeIndex]);\r
- }\r
-\r
- json.EndObject();\r
- }\r
- }\r
-\r
- json.EndObject();\r
- }\r
- if(detailedMap == VK_TRUE)\r
- {\r
- allocator->PrintDetailedMap(json);\r
- }\r
-\r
- json.EndObject();\r
- }\r
-\r
- const size_t len = sb.GetLength();\r
- char* const pChars = vma_new_array(allocator, char, len + 1);\r
- if(len > 0)\r
- {\r
- memcpy(pChars, sb.GetData(), len);\r
- }\r
- pChars[len] = '\0';\r
- *ppStatsString = pChars;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString(\r
- VmaAllocator allocator,\r
- char* pStatsString)\r
-{\r
- if(pStatsString != VMA_NULL)\r
- {\r
- VMA_ASSERT(allocator);\r
- size_t len = strlen(pStatsString);\r
- vma_delete_array(allocator, pStatsString, len + 1);\r
- }\r
-}\r
-\r
-#endif // #if VMA_STATS_STRING_ENABLED\r
-\r
-/*\r
-This function is not protected by any mutex because it just reads immutable data.\r
-*/\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndex(\r
- VmaAllocator allocator,\r
- uint32_t memoryTypeBits,\r
- const VmaAllocationCreateInfo* pAllocationCreateInfo,\r
- uint32_t* pMemoryTypeIndex)\r
-{\r
- VMA_ASSERT(allocator != VK_NULL_HANDLE);\r
- VMA_ASSERT(pAllocationCreateInfo != VMA_NULL);\r
- VMA_ASSERT(pMemoryTypeIndex != VMA_NULL);\r
-\r
- memoryTypeBits &= allocator->GetGlobalMemoryTypeBits();\r
-\r
- if(pAllocationCreateInfo->memoryTypeBits != 0)\r
- {\r
- memoryTypeBits &= pAllocationCreateInfo->memoryTypeBits;\r
- }\r
- \r
- uint32_t requiredFlags = pAllocationCreateInfo->requiredFlags;\r
- uint32_t preferredFlags = pAllocationCreateInfo->preferredFlags;\r
- uint32_t notPreferredFlags = 0;\r
-\r
- // Convert usage to requiredFlags and preferredFlags.\r
- switch(pAllocationCreateInfo->usage)\r
- {\r
- case VMA_MEMORY_USAGE_UNKNOWN:\r
- break;\r
- case VMA_MEMORY_USAGE_GPU_ONLY:\r
- if(!allocator->IsIntegratedGpu() || (preferredFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)\r
- {\r
- preferredFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;\r
- }\r
- break;\r
- case VMA_MEMORY_USAGE_CPU_ONLY:\r
- requiredFlags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;\r
- break;\r
- case VMA_MEMORY_USAGE_CPU_TO_GPU:\r
- requiredFlags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;\r
- if(!allocator->IsIntegratedGpu() || (preferredFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)\r
- {\r
- preferredFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;\r
- }\r
- break;\r
- case VMA_MEMORY_USAGE_GPU_TO_CPU:\r
- requiredFlags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;\r
- preferredFlags |= VK_MEMORY_PROPERTY_HOST_CACHED_BIT;\r
- break;\r
- case VMA_MEMORY_USAGE_CPU_COPY:\r
- notPreferredFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;\r
- break;\r
- case VMA_MEMORY_USAGE_GPU_LAZILY_ALLOCATED:\r
- requiredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT;\r
- break;\r
- default:\r
- VMA_ASSERT(0);\r
- break;\r
- }\r
-\r
- // Avoid DEVICE_COHERENT unless explicitly requested.\r
- if(((pAllocationCreateInfo->requiredFlags | pAllocationCreateInfo->preferredFlags) &\r
- (VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY | VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY)) == 0)\r
- {\r
- notPreferredFlags |= VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY;\r
- }\r
-\r
- *pMemoryTypeIndex = UINT32_MAX;\r
- uint32_t minCost = UINT32_MAX;\r
- for(uint32_t memTypeIndex = 0, memTypeBit = 1;\r
- memTypeIndex < allocator->GetMemoryTypeCount();\r
- ++memTypeIndex, memTypeBit <<= 1)\r
- {\r
- // This memory type is acceptable according to memoryTypeBits bitmask.\r
- if((memTypeBit & memoryTypeBits) != 0)\r
- {\r
- const VkMemoryPropertyFlags currFlags =\r
- allocator->m_MemProps.memoryTypes[memTypeIndex].propertyFlags;\r
- // This memory type contains requiredFlags.\r
- if((requiredFlags & ~currFlags) == 0)\r
- {\r
- // Calculate cost as number of bits from preferredFlags not present in this memory type.\r
- uint32_t currCost = VmaCountBitsSet(preferredFlags & ~currFlags) +\r
- VmaCountBitsSet(currFlags & notPreferredFlags);\r
- // Remember memory type with lowest cost.\r
- if(currCost < minCost)\r
- {\r
- *pMemoryTypeIndex = memTypeIndex;\r
- if(currCost == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
- minCost = currCost;\r
- }\r
- }\r
- }\r
- }\r
- return (*pMemoryTypeIndex != UINT32_MAX) ? VK_SUCCESS : VK_ERROR_FEATURE_NOT_PRESENT;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo(\r
- VmaAllocator allocator,\r
- const VkBufferCreateInfo* pBufferCreateInfo,\r
- const VmaAllocationCreateInfo* pAllocationCreateInfo,\r
- uint32_t* pMemoryTypeIndex)\r
-{\r
- VMA_ASSERT(allocator != VK_NULL_HANDLE);\r
- VMA_ASSERT(pBufferCreateInfo != VMA_NULL);\r
- VMA_ASSERT(pAllocationCreateInfo != VMA_NULL);\r
- VMA_ASSERT(pMemoryTypeIndex != VMA_NULL);\r
-\r
- const VkDevice hDev = allocator->m_hDevice;\r
- VkBuffer hBuffer = VK_NULL_HANDLE;\r
- VkResult res = allocator->GetVulkanFunctions().vkCreateBuffer(\r
- hDev, pBufferCreateInfo, allocator->GetAllocationCallbacks(), &hBuffer);\r
- if(res == VK_SUCCESS)\r
- {\r
- VkMemoryRequirements memReq = {};\r
- allocator->GetVulkanFunctions().vkGetBufferMemoryRequirements(\r
- hDev, hBuffer, &memReq);\r
-\r
- res = vmaFindMemoryTypeIndex(\r
- allocator,\r
- memReq.memoryTypeBits,\r
- pAllocationCreateInfo,\r
- pMemoryTypeIndex);\r
-\r
- allocator->GetVulkanFunctions().vkDestroyBuffer(\r
- hDev, hBuffer, allocator->GetAllocationCallbacks());\r
- }\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo(\r
- VmaAllocator allocator,\r
- const VkImageCreateInfo* pImageCreateInfo,\r
- const VmaAllocationCreateInfo* pAllocationCreateInfo,\r
- uint32_t* pMemoryTypeIndex)\r
-{\r
- VMA_ASSERT(allocator != VK_NULL_HANDLE);\r
- VMA_ASSERT(pImageCreateInfo != VMA_NULL);\r
- VMA_ASSERT(pAllocationCreateInfo != VMA_NULL);\r
- VMA_ASSERT(pMemoryTypeIndex != VMA_NULL);\r
-\r
- const VkDevice hDev = allocator->m_hDevice;\r
- VkImage hImage = VK_NULL_HANDLE;\r
- VkResult res = allocator->GetVulkanFunctions().vkCreateImage(\r
- hDev, pImageCreateInfo, allocator->GetAllocationCallbacks(), &hImage);\r
- if(res == VK_SUCCESS)\r
- {\r
- VkMemoryRequirements memReq = {};\r
- allocator->GetVulkanFunctions().vkGetImageMemoryRequirements(\r
- hDev, hImage, &memReq);\r
-\r
- res = vmaFindMemoryTypeIndex(\r
- allocator,\r
- memReq.memoryTypeBits,\r
- pAllocationCreateInfo,\r
- pMemoryTypeIndex);\r
-\r
- allocator->GetVulkanFunctions().vkDestroyImage(\r
- hDev, hImage, allocator->GetAllocationCallbacks());\r
- }\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreatePool(\r
- VmaAllocator allocator,\r
- const VmaPoolCreateInfo* pCreateInfo,\r
- VmaPool* pPool)\r
-{\r
- VMA_ASSERT(allocator && pCreateInfo && pPool);\r
- \r
- VMA_DEBUG_LOG("vmaCreatePool");\r
- \r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
- \r
- VkResult res = allocator->CreatePool(pCreateInfo, pPool);\r
- \r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordCreatePool(allocator->GetCurrentFrameIndex(), *pCreateInfo, *pPool);\r
- }\r
-#endif\r
- \r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyPool(\r
- VmaAllocator allocator,\r
- VmaPool pool)\r
-{\r
- VMA_ASSERT(allocator);\r
- \r
- if(pool == VK_NULL_HANDLE)\r
- {\r
- return;\r
- }\r
- \r
- VMA_DEBUG_LOG("vmaDestroyPool");\r
- \r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
- \r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordDestroyPool(allocator->GetCurrentFrameIndex(), pool);\r
- }\r
-#endif\r
-\r
- allocator->DestroyPool(pool);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolStats(\r
- VmaAllocator allocator,\r
- VmaPool pool,\r
- VmaPoolStats* pPoolStats)\r
-{\r
- VMA_ASSERT(allocator && pool && pPoolStats);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- allocator->GetPoolStats(pool, pPoolStats);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaMakePoolAllocationsLost(\r
- VmaAllocator allocator,\r
- VmaPool pool,\r
- size_t* pLostAllocationCount)\r
-{\r
- VMA_ASSERT(allocator && pool);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordMakePoolAllocationsLost(allocator->GetCurrentFrameIndex(), pool);\r
- }\r
-#endif\r
-\r
- allocator->MakePoolAllocationsLost(pool, pLostAllocationCount);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator allocator, VmaPool pool)\r
-{\r
- VMA_ASSERT(allocator && pool);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VMA_DEBUG_LOG("vmaCheckPoolCorruption");\r
-\r
- return allocator->CheckPoolCorruption(pool);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName(\r
- VmaAllocator allocator,\r
- VmaPool pool,\r
- const char** ppName)\r
-{\r
- VMA_ASSERT(allocator && pool && ppName);\r
- \r
- VMA_DEBUG_LOG("vmaGetPoolName");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- *ppName = pool->GetName();\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaSetPoolName(\r
- VmaAllocator allocator,\r
- VmaPool pool,\r
- const char* pName)\r
-{\r
- VMA_ASSERT(allocator && pool);\r
-\r
- VMA_DEBUG_LOG("vmaSetPoolName");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- pool->SetName(pName);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordSetPoolName(allocator->GetCurrentFrameIndex(), pool, pName);\r
- }\r
-#endif\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory(\r
- VmaAllocator allocator,\r
- const VkMemoryRequirements* pVkMemoryRequirements,\r
- const VmaAllocationCreateInfo* pCreateInfo,\r
- VmaAllocation* pAllocation,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- VMA_ASSERT(allocator && pVkMemoryRequirements && pCreateInfo && pAllocation);\r
-\r
- VMA_DEBUG_LOG("vmaAllocateMemory");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VkResult result = allocator->AllocateMemory(\r
- *pVkMemoryRequirements,\r
- false, // requiresDedicatedAllocation\r
- false, // prefersDedicatedAllocation\r
- VK_NULL_HANDLE, // dedicatedBuffer\r
- UINT32_MAX, // dedicatedBufferUsage\r
- VK_NULL_HANDLE, // dedicatedImage\r
- *pCreateInfo,\r
- VMA_SUBALLOCATION_TYPE_UNKNOWN,\r
- 1, // allocationCount\r
- pAllocation);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordAllocateMemory(\r
- allocator->GetCurrentFrameIndex(),\r
- *pVkMemoryRequirements,\r
- *pCreateInfo,\r
- *pAllocation);\r
- }\r
-#endif\r
- \r
- if(pAllocationInfo != VMA_NULL && result == VK_SUCCESS)\r
- {\r
- allocator->GetAllocationInfo(*pAllocation, pAllocationInfo);\r
- }\r
-\r
- return result;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages(\r
- VmaAllocator allocator,\r
- const VkMemoryRequirements* pVkMemoryRequirements,\r
- const VmaAllocationCreateInfo* pCreateInfo,\r
- size_t allocationCount,\r
- VmaAllocation* pAllocations,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- if(allocationCount == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- VMA_ASSERT(allocator && pVkMemoryRequirements && pCreateInfo && pAllocations);\r
-\r
- VMA_DEBUG_LOG("vmaAllocateMemoryPages");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VkResult result = allocator->AllocateMemory(\r
- *pVkMemoryRequirements,\r
- false, // requiresDedicatedAllocation\r
- false, // prefersDedicatedAllocation\r
- VK_NULL_HANDLE, // dedicatedBuffer\r
- UINT32_MAX, // dedicatedBufferUsage\r
- VK_NULL_HANDLE, // dedicatedImage\r
- *pCreateInfo,\r
- VMA_SUBALLOCATION_TYPE_UNKNOWN,\r
- allocationCount,\r
- pAllocations);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordAllocateMemoryPages(\r
- allocator->GetCurrentFrameIndex(),\r
- *pVkMemoryRequirements,\r
- *pCreateInfo,\r
- (uint64_t)allocationCount,\r
- pAllocations);\r
- }\r
-#endif\r
- \r
- if(pAllocationInfo != VMA_NULL && result == VK_SUCCESS)\r
- {\r
- for(size_t i = 0; i < allocationCount; ++i)\r
- {\r
- allocator->GetAllocationInfo(pAllocations[i], pAllocationInfo + i);\r
- }\r
- }\r
-\r
- return result;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer(\r
- VmaAllocator allocator,\r
- VkBuffer buffer,\r
- const VmaAllocationCreateInfo* pCreateInfo,\r
- VmaAllocation* pAllocation,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- VMA_ASSERT(allocator && buffer != VK_NULL_HANDLE && pCreateInfo && pAllocation);\r
-\r
- VMA_DEBUG_LOG("vmaAllocateMemoryForBuffer");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VkMemoryRequirements vkMemReq = {};\r
- bool requiresDedicatedAllocation = false;\r
- bool prefersDedicatedAllocation = false;\r
- allocator->GetBufferMemoryRequirements(buffer, vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation);\r
-\r
- VkResult result = allocator->AllocateMemory(\r
- vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation,\r
- buffer, // dedicatedBuffer\r
- UINT32_MAX, // dedicatedBufferUsage\r
- VK_NULL_HANDLE, // dedicatedImage\r
- *pCreateInfo,\r
- VMA_SUBALLOCATION_TYPE_BUFFER,\r
- 1, // allocationCount\r
- pAllocation);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordAllocateMemoryForBuffer(\r
- allocator->GetCurrentFrameIndex(),\r
- vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation,\r
- *pCreateInfo,\r
- *pAllocation);\r
- }\r
-#endif\r
-\r
- if(pAllocationInfo && result == VK_SUCCESS)\r
- {\r
- allocator->GetAllocationInfo(*pAllocation, pAllocationInfo);\r
- }\r
-\r
- return result;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage(\r
- VmaAllocator allocator,\r
- VkImage image,\r
- const VmaAllocationCreateInfo* pCreateInfo,\r
- VmaAllocation* pAllocation,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- VMA_ASSERT(allocator && image != VK_NULL_HANDLE && pCreateInfo && pAllocation);\r
-\r
- VMA_DEBUG_LOG("vmaAllocateMemoryForImage");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VkMemoryRequirements vkMemReq = {};\r
- bool requiresDedicatedAllocation = false;\r
- bool prefersDedicatedAllocation = false;\r
- allocator->GetImageMemoryRequirements(image, vkMemReq,\r
- requiresDedicatedAllocation, prefersDedicatedAllocation);\r
-\r
- VkResult result = allocator->AllocateMemory(\r
- vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation,\r
- VK_NULL_HANDLE, // dedicatedBuffer\r
- UINT32_MAX, // dedicatedBufferUsage\r
- image, // dedicatedImage\r
- *pCreateInfo,\r
- VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN,\r
- 1, // allocationCount\r
- pAllocation);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordAllocateMemoryForImage(\r
- allocator->GetCurrentFrameIndex(),\r
- vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation,\r
- *pCreateInfo,\r
- *pAllocation);\r
- }\r
-#endif\r
-\r
- if(pAllocationInfo && result == VK_SUCCESS)\r
- {\r
- allocator->GetAllocationInfo(*pAllocation, pAllocationInfo);\r
- }\r
-\r
- return result;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation)\r
-{\r
- VMA_ASSERT(allocator);\r
- \r
- if(allocation == VK_NULL_HANDLE)\r
- {\r
- return;\r
- }\r
- \r
- VMA_DEBUG_LOG("vmaFreeMemory");\r
- \r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordFreeMemory(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
- \r
- allocator->FreeMemory(\r
- 1, // allocationCount\r
- &allocation);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages(\r
- VmaAllocator allocator,\r
- size_t allocationCount,\r
- const VmaAllocation* pAllocations)\r
-{\r
- if(allocationCount == 0)\r
- {\r
- return;\r
- }\r
-\r
- VMA_ASSERT(allocator);\r
- \r
- VMA_DEBUG_LOG("vmaFreeMemoryPages");\r
- \r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordFreeMemoryPages(\r
- allocator->GetCurrentFrameIndex(),\r
- (uint64_t)allocationCount,\r
- pAllocations);\r
- }\r
-#endif\r
- \r
- allocator->FreeMemory(allocationCount, pAllocations);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaResizeAllocation(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- VkDeviceSize newSize)\r
-{\r
- VMA_ASSERT(allocator && allocation);\r
- \r
- VMA_DEBUG_LOG("vmaResizeAllocation");\r
- \r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- return allocator->ResizeAllocation(allocation, newSize);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationInfo(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- VMA_ASSERT(allocator && allocation && pAllocationInfo);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordGetAllocationInfo(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
-\r
- allocator->GetAllocationInfo(allocation, pAllocationInfo);\r
-}\r
-\r
-VMA_CALL_PRE VkBool32 VMA_CALL_POST vmaTouchAllocation(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation)\r
-{\r
- VMA_ASSERT(allocator && allocation);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordTouchAllocation(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
-\r
- return allocator->TouchAllocation(allocation);\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- void* pUserData)\r
-{\r
- VMA_ASSERT(allocator && allocation);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- allocation->SetUserData(allocator, pUserData);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordSetAllocationUserData(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation,\r
- pUserData);\r
- }\r
-#endif\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaCreateLostAllocation(\r
- VmaAllocator allocator,\r
- VmaAllocation* pAllocation)\r
-{\r
- VMA_ASSERT(allocator && pAllocation);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK;\r
-\r
- allocator->CreateLostAllocation(pAllocation);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordCreateLostAllocation(\r
- allocator->GetCurrentFrameIndex(),\r
- *pAllocation);\r
- }\r
-#endif\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaMapMemory(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- void** ppData)\r
-{\r
- VMA_ASSERT(allocator && allocation && ppData);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VkResult res = allocator->Map(allocation, ppData);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordMapMemory(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
-\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaUnmapMemory(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation)\r
-{\r
- VMA_ASSERT(allocator && allocation);\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordUnmapMemory(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
-\r
- allocator->Unmap(allocation);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size)\r
-{\r
- VMA_ASSERT(allocator && allocation);\r
-\r
- VMA_DEBUG_LOG("vmaFlushAllocation");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- const VkResult res = allocator->FlushOrInvalidateAllocation(allocation, offset, size, VMA_CACHE_FLUSH);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordFlushAllocation(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation, offset, size);\r
- }\r
-#endif\r
-\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size)\r
-{\r
- VMA_ASSERT(allocator && allocation);\r
-\r
- VMA_DEBUG_LOG("vmaInvalidateAllocation");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- const VkResult res = allocator->FlushOrInvalidateAllocation(allocation, offset, size, VMA_CACHE_INVALIDATE);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordInvalidateAllocation(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation, offset, size);\r
- }\r
-#endif\r
-\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocations(\r
- VmaAllocator allocator,\r
- uint32_t allocationCount,\r
- const VmaAllocation* allocations,\r
- const VkDeviceSize* offsets,\r
- const VkDeviceSize* sizes)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- if(allocationCount == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- VMA_ASSERT(allocations);\r
-\r
- VMA_DEBUG_LOG("vmaFlushAllocations");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- const VkResult res = allocator->FlushOrInvalidateAllocations(allocationCount, allocations, offsets, sizes, VMA_CACHE_FLUSH);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- //TODO\r
- }\r
-#endif\r
-\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocations(\r
- VmaAllocator allocator,\r
- uint32_t allocationCount,\r
- const VmaAllocation* allocations,\r
- const VkDeviceSize* offsets,\r
- const VkDeviceSize* sizes)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- if(allocationCount == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- VMA_ASSERT(allocations);\r
-\r
- VMA_DEBUG_LOG("vmaInvalidateAllocations");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- const VkResult res = allocator->FlushOrInvalidateAllocations(allocationCount, allocations, offsets, sizes, VMA_CACHE_INVALIDATE);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- //TODO\r
- }\r
-#endif\r
-\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator allocator, uint32_t memoryTypeBits)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- VMA_DEBUG_LOG("vmaCheckCorruption");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- return allocator->CheckCorruption(memoryTypeBits);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragment(\r
- VmaAllocator allocator,\r
- const VmaAllocation* pAllocations,\r
- size_t allocationCount,\r
- VkBool32* pAllocationsChanged,\r
- const VmaDefragmentationInfo *pDefragmentationInfo,\r
- VmaDefragmentationStats* pDefragmentationStats)\r
-{\r
- // Deprecated interface, reimplemented using new one.\r
-\r
- VmaDefragmentationInfo2 info2 = {};\r
- info2.allocationCount = (uint32_t)allocationCount;\r
- info2.pAllocations = pAllocations;\r
- info2.pAllocationsChanged = pAllocationsChanged;\r
- if(pDefragmentationInfo != VMA_NULL)\r
- {\r
- info2.maxCpuAllocationsToMove = pDefragmentationInfo->maxAllocationsToMove;\r
- info2.maxCpuBytesToMove = pDefragmentationInfo->maxBytesToMove;\r
- }\r
- else\r
- {\r
- info2.maxCpuAllocationsToMove = UINT32_MAX;\r
- info2.maxCpuBytesToMove = VK_WHOLE_SIZE;\r
- }\r
- // info2.flags, maxGpuAllocationsToMove, maxGpuBytesToMove, commandBuffer deliberately left zero.\r
-\r
- VmaDefragmentationContext ctx;\r
- VkResult res = vmaDefragmentationBegin(allocator, &info2, pDefragmentationStats, &ctx);\r
- if(res == VK_NOT_READY)\r
- {\r
- res = vmaDefragmentationEnd( allocator, ctx);\r
- }\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationBegin(\r
- VmaAllocator allocator,\r
- const VmaDefragmentationInfo2* pInfo,\r
- VmaDefragmentationStats* pStats,\r
- VmaDefragmentationContext *pContext)\r
-{\r
- VMA_ASSERT(allocator && pInfo && pContext);\r
-\r
- // Degenerate case: Nothing to defragment.\r
- if(pInfo->allocationCount == 0 && pInfo->poolCount == 0)\r
- {\r
- return VK_SUCCESS;\r
- }\r
-\r
- VMA_ASSERT(pInfo->allocationCount == 0 || pInfo->pAllocations != VMA_NULL);\r
- VMA_ASSERT(pInfo->poolCount == 0 || pInfo->pPools != VMA_NULL);\r
- VMA_HEAVY_ASSERT(VmaValidatePointerArray(pInfo->allocationCount, pInfo->pAllocations));\r
- VMA_HEAVY_ASSERT(VmaValidatePointerArray(pInfo->poolCount, pInfo->pPools));\r
-\r
- VMA_DEBUG_LOG("vmaDefragmentationBegin");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- VkResult res = allocator->DefragmentationBegin(*pInfo, pStats, pContext);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordDefragmentationBegin(\r
- allocator->GetCurrentFrameIndex(), *pInfo, *pContext);\r
- }\r
-#endif\r
-\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationEnd(\r
- VmaAllocator allocator,\r
- VmaDefragmentationContext context)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- VMA_DEBUG_LOG("vmaDefragmentationEnd");\r
-\r
- if(context != VK_NULL_HANDLE)\r
- {\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordDefragmentationEnd(\r
- allocator->GetCurrentFrameIndex(), context);\r
- }\r
-#endif\r
-\r
- return allocator->DefragmentationEnd(context);\r
- }\r
- else\r
- {\r
- return VK_SUCCESS;\r
- }\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBeginDefragmentationPass(\r
- VmaAllocator allocator,\r
- VmaDefragmentationContext context,\r
- VmaDefragmentationPassInfo* pInfo\r
- )\r
-{\r
- VMA_ASSERT(allocator);\r
- VMA_ASSERT(pInfo);\r
- VMA_HEAVY_ASSERT(VmaValidatePointerArray(pInfo->moveCount, pInfo->pMoves));\r
-\r
- VMA_DEBUG_LOG("vmaBeginDefragmentationPass");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- if(context == VK_NULL_HANDLE)\r
- {\r
- pInfo->moveCount = 0;\r
- return VK_SUCCESS;\r
- }\r
-\r
- return allocator->DefragmentationPassBegin(pInfo, context);\r
-}\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaEndDefragmentationPass(\r
- VmaAllocator allocator,\r
- VmaDefragmentationContext context)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- VMA_DEBUG_LOG("vmaEndDefragmentationPass");\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- if(context == VK_NULL_HANDLE)\r
- return VK_SUCCESS;\r
-\r
- return allocator->DefragmentationPassEnd(context);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- VkBuffer buffer)\r
-{\r
- VMA_ASSERT(allocator && allocation && buffer);\r
-\r
- VMA_DEBUG_LOG("vmaBindBufferMemory");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- return allocator->BindBufferMemory(allocation, 0, buffer, VMA_NULL);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory2(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkBuffer buffer,\r
- const void* pNext)\r
-{\r
- VMA_ASSERT(allocator && allocation && buffer);\r
-\r
- VMA_DEBUG_LOG("vmaBindBufferMemory2");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- return allocator->BindBufferMemory(allocation, allocationLocalOffset, buffer, pNext);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- VkImage image)\r
-{\r
- VMA_ASSERT(allocator && allocation && image);\r
-\r
- VMA_DEBUG_LOG("vmaBindImageMemory");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- return allocator->BindImageMemory(allocation, 0, image, VMA_NULL);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory2(\r
- VmaAllocator allocator,\r
- VmaAllocation allocation,\r
- VkDeviceSize allocationLocalOffset,\r
- VkImage image,\r
- const void* pNext)\r
-{\r
- VMA_ASSERT(allocator && allocation && image);\r
-\r
- VMA_DEBUG_LOG("vmaBindImageMemory2");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- return allocator->BindImageMemory(allocation, allocationLocalOffset, image, pNext);\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBuffer(\r
- VmaAllocator allocator,\r
- const VkBufferCreateInfo* pBufferCreateInfo,\r
- const VmaAllocationCreateInfo* pAllocationCreateInfo,\r
- VkBuffer* pBuffer,\r
- VmaAllocation* pAllocation,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- VMA_ASSERT(allocator && pBufferCreateInfo && pAllocationCreateInfo && pBuffer && pAllocation);\r
-\r
- if(pBufferCreateInfo->size == 0)\r
- {\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- if((pBufferCreateInfo->usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_COPY) != 0 &&\r
- !allocator->m_UseKhrBufferDeviceAddress)\r
- {\r
- VMA_ASSERT(0 && "Creating a buffer with VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT is not valid if VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT was not used.");\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
- \r
- VMA_DEBUG_LOG("vmaCreateBuffer");\r
- \r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- *pBuffer = VK_NULL_HANDLE;\r
- *pAllocation = VK_NULL_HANDLE;\r
-\r
- // 1. Create VkBuffer.\r
- VkResult res = (*allocator->GetVulkanFunctions().vkCreateBuffer)(\r
- allocator->m_hDevice,\r
- pBufferCreateInfo,\r
- allocator->GetAllocationCallbacks(),\r
- pBuffer);\r
- if(res >= 0)\r
- {\r
- // 2. vkGetBufferMemoryRequirements.\r
- VkMemoryRequirements vkMemReq = {};\r
- bool requiresDedicatedAllocation = false;\r
- bool prefersDedicatedAllocation = false;\r
- allocator->GetBufferMemoryRequirements(*pBuffer, vkMemReq,\r
- requiresDedicatedAllocation, prefersDedicatedAllocation);\r
-\r
- // 3. Allocate memory using allocator.\r
- res = allocator->AllocateMemory(\r
- vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation,\r
- *pBuffer, // dedicatedBuffer\r
- pBufferCreateInfo->usage, // dedicatedBufferUsage\r
- VK_NULL_HANDLE, // dedicatedImage\r
- *pAllocationCreateInfo,\r
- VMA_SUBALLOCATION_TYPE_BUFFER,\r
- 1, // allocationCount\r
- pAllocation);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordCreateBuffer(\r
- allocator->GetCurrentFrameIndex(),\r
- *pBufferCreateInfo,\r
- *pAllocationCreateInfo,\r
- *pAllocation);\r
- }\r
-#endif\r
-\r
- if(res >= 0)\r
- {\r
- // 3. Bind buffer with memory.\r
- if((pAllocationCreateInfo->flags & VMA_ALLOCATION_CREATE_DONT_BIND_BIT) == 0)\r
- {\r
- res = allocator->BindBufferMemory(*pAllocation, 0, *pBuffer, VMA_NULL);\r
- }\r
- if(res >= 0)\r
- {\r
- // All steps succeeded.\r
- #if VMA_STATS_STRING_ENABLED\r
- (*pAllocation)->InitBufferImageUsage(pBufferCreateInfo->usage);\r
- #endif\r
- if(pAllocationInfo != VMA_NULL)\r
- {\r
- allocator->GetAllocationInfo(*pAllocation, pAllocationInfo);\r
- }\r
-\r
- return VK_SUCCESS;\r
- }\r
- allocator->FreeMemory(\r
- 1, // allocationCount\r
- pAllocation);\r
- *pAllocation = VK_NULL_HANDLE;\r
- (*allocator->GetVulkanFunctions().vkDestroyBuffer)(allocator->m_hDevice, *pBuffer, allocator->GetAllocationCallbacks());\r
- *pBuffer = VK_NULL_HANDLE;\r
- return res;\r
- }\r
- (*allocator->GetVulkanFunctions().vkDestroyBuffer)(allocator->m_hDevice, *pBuffer, allocator->GetAllocationCallbacks());\r
- *pBuffer = VK_NULL_HANDLE;\r
- return res;\r
- }\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyBuffer(\r
- VmaAllocator allocator,\r
- VkBuffer buffer,\r
- VmaAllocation allocation)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- if(buffer == VK_NULL_HANDLE && allocation == VK_NULL_HANDLE)\r
- {\r
- return;\r
- }\r
-\r
- VMA_DEBUG_LOG("vmaDestroyBuffer");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordDestroyBuffer(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
-\r
- if(buffer != VK_NULL_HANDLE)\r
- {\r
- (*allocator->GetVulkanFunctions().vkDestroyBuffer)(allocator->m_hDevice, buffer, allocator->GetAllocationCallbacks());\r
- }\r
-\r
- if(allocation != VK_NULL_HANDLE)\r
- {\r
- allocator->FreeMemory(\r
- 1, // allocationCount\r
- &allocation);\r
- }\r
-}\r
-\r
-VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateImage(\r
- VmaAllocator allocator,\r
- const VkImageCreateInfo* pImageCreateInfo,\r
- const VmaAllocationCreateInfo* pAllocationCreateInfo,\r
- VkImage* pImage,\r
- VmaAllocation* pAllocation,\r
- VmaAllocationInfo* pAllocationInfo)\r
-{\r
- VMA_ASSERT(allocator && pImageCreateInfo && pAllocationCreateInfo && pImage && pAllocation);\r
-\r
- if(pImageCreateInfo->extent.width == 0 ||\r
- pImageCreateInfo->extent.height == 0 ||\r
- pImageCreateInfo->extent.depth == 0 ||\r
- pImageCreateInfo->mipLevels == 0 ||\r
- pImageCreateInfo->arrayLayers == 0)\r
- {\r
- return VK_ERROR_VALIDATION_FAILED_EXT;\r
- }\r
-\r
- VMA_DEBUG_LOG("vmaCreateImage");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
- *pImage = VK_NULL_HANDLE;\r
- *pAllocation = VK_NULL_HANDLE;\r
-\r
- // 1. Create VkImage.\r
- VkResult res = (*allocator->GetVulkanFunctions().vkCreateImage)(\r
- allocator->m_hDevice,\r
- pImageCreateInfo,\r
- allocator->GetAllocationCallbacks(),\r
- pImage);\r
- if(res >= 0)\r
- {\r
- VmaSuballocationType suballocType = pImageCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL ?\r
- VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL :\r
- VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR;\r
- \r
- // 2. Allocate memory using allocator.\r
- VkMemoryRequirements vkMemReq = {};\r
- bool requiresDedicatedAllocation = false;\r
- bool prefersDedicatedAllocation = false;\r
- allocator->GetImageMemoryRequirements(*pImage, vkMemReq,\r
- requiresDedicatedAllocation, prefersDedicatedAllocation);\r
-\r
- res = allocator->AllocateMemory(\r
- vkMemReq,\r
- requiresDedicatedAllocation,\r
- prefersDedicatedAllocation,\r
- VK_NULL_HANDLE, // dedicatedBuffer\r
- UINT32_MAX, // dedicatedBufferUsage\r
- *pImage, // dedicatedImage\r
- *pAllocationCreateInfo,\r
- suballocType,\r
- 1, // allocationCount\r
- pAllocation);\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordCreateImage(\r
- allocator->GetCurrentFrameIndex(),\r
- *pImageCreateInfo,\r
- *pAllocationCreateInfo,\r
- *pAllocation);\r
- }\r
-#endif\r
-\r
- if(res >= 0)\r
- {\r
- // 3. Bind image with memory.\r
- if((pAllocationCreateInfo->flags & VMA_ALLOCATION_CREATE_DONT_BIND_BIT) == 0)\r
- {\r
- res = allocator->BindImageMemory(*pAllocation, 0, *pImage, VMA_NULL);\r
- }\r
- if(res >= 0)\r
- {\r
- // All steps succeeded.\r
- #if VMA_STATS_STRING_ENABLED\r
- (*pAllocation)->InitBufferImageUsage(pImageCreateInfo->usage);\r
- #endif\r
- if(pAllocationInfo != VMA_NULL)\r
- {\r
- allocator->GetAllocationInfo(*pAllocation, pAllocationInfo);\r
- }\r
-\r
- return VK_SUCCESS;\r
- }\r
- allocator->FreeMemory(\r
- 1, // allocationCount\r
- pAllocation);\r
- *pAllocation = VK_NULL_HANDLE;\r
- (*allocator->GetVulkanFunctions().vkDestroyImage)(allocator->m_hDevice, *pImage, allocator->GetAllocationCallbacks());\r
- *pImage = VK_NULL_HANDLE;\r
- return res;\r
- }\r
- (*allocator->GetVulkanFunctions().vkDestroyImage)(allocator->m_hDevice, *pImage, allocator->GetAllocationCallbacks());\r
- *pImage = VK_NULL_HANDLE;\r
- return res;\r
- }\r
- return res;\r
-}\r
-\r
-VMA_CALL_PRE void VMA_CALL_POST vmaDestroyImage(\r
- VmaAllocator allocator,\r
- VkImage image,\r
- VmaAllocation allocation)\r
-{\r
- VMA_ASSERT(allocator);\r
-\r
- if(image == VK_NULL_HANDLE && allocation == VK_NULL_HANDLE)\r
- {\r
- return;\r
- }\r
-\r
- VMA_DEBUG_LOG("vmaDestroyImage");\r
-\r
- VMA_DEBUG_GLOBAL_MUTEX_LOCK\r
-\r
-#if VMA_RECORDING_ENABLED\r
- if(allocator->GetRecorder() != VMA_NULL)\r
- {\r
- allocator->GetRecorder()->RecordDestroyImage(\r
- allocator->GetCurrentFrameIndex(),\r
- allocation);\r
- }\r
-#endif\r
-\r
- if(image != VK_NULL_HANDLE)\r
- {\r
- (*allocator->GetVulkanFunctions().vkDestroyImage)(allocator->m_hDevice, image, allocator->GetAllocationCallbacks());\r
- }\r
- if(allocation != VK_NULL_HANDLE)\r
- {\r
- allocator->FreeMemory(\r
- 1, // allocationCount\r
- &allocation);\r
- }\r
-}\r
-\r
-#endif // #ifdef VMA_IMPLEMENTATION\r
+//
+// Copyright 2023 Pixar
+//
+// Licensed under the Apache License, Version 2.0 (the "Apache License")
+// with the following modification; you may not use this file except in
+// compliance with the Apache License and the following modification to it:
+// Section 6. Trademarks. is deleted and replaced with:
+//
+// 6. Trademarks. This License does not grant permission to use the trade
+// names, trademarks, service marks, or product names of the Licensor
+// and its affiliates, except as required to comply with Section 4(c) of
+// the License and to reproduce the content of the NOTICE file.
+//
+// You may obtain a copy of the Apache License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the Apache License with the above modification is
+// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the Apache License for the specific
+// language governing permissions and limitations under the Apache License.
+//
+#ifndef PXR_IMAGING_HGIVULKAN_VK_MEM_ALLOC_H
+#define PXR_IMAGING_HGIVULKAN_VK_MEM_ALLOC_H
+
+// This is an implementation of VMA (Vulkan Memory Allocator) included
+// from the Vulkan SDK.
+
+#include <vma/vk_mem_alloc.h>
+
+#endif