Skip to content
This repository was archived by the owner on Jun 3, 2023. It is now read-only.

Commit 01b2355

Browse files
committed
Better usage of AMD's vma allocator. Start using the VmaAllocationInfo structs instead.
1 parent 718d064 commit 01b2355

File tree

11 files changed

+154
-77
lines changed

11 files changed

+154
-77
lines changed

neo/Engine.vcxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
<ClCompile Include="renderer\Vulkan\BufferObject_VK.cpp">
208208
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug GL|Win32'">true</ExcludedFromBuild>
209209
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release GL|Win32'">true</ExcludedFromBuild>
210+
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug VK|Win32'">ID_VULKAN;_INLINEDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
210211
</ClCompile>
211212
<ClCompile Include="renderer\Vulkan\Allocator_VK.cpp">
212213
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug GL|Win32'">true</ExcludedFromBuild>
@@ -234,6 +235,12 @@
234235
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug GL|Win32'">true</ExcludedFromBuild>
235236
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release GL|Win32'">true</ExcludedFromBuild>
236237
</ClCompile>
238+
<ClCompile Include="renderer\Vulkan\vma.cpp">
239+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug VK|Win32'">NotUsing</PrecompiledHeader>
240+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release VK|Win32'">NotUsing</PrecompiledHeader>
241+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug GL|Win32'">true</ExcludedFromBuild>
242+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release GL|Win32'">true</ExcludedFromBuild>
243+
</ClCompile>
237244
<ClCompile Include="sound\snd_emitter.cpp" />
238245
<ClCompile Include="sound\snd_shader.cpp" />
239246
<ClCompile Include="sound\snd_system.cpp" />

neo/Engine.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@
651651
<ClCompile Include="renderer\Vulkan\Staging_VK.cpp">
652652
<Filter>Renderer\Vulkan</Filter>
653653
</ClCompile>
654+
<ClCompile Include="renderer\Vulkan\vma.cpp">
655+
<Filter>Renderer\Vulkan</Filter>
656+
</ClCompile>
654657
</ItemGroup>
655658
<ItemGroup>
656659
<ClInclude Include="ui\BindWindow.h">

neo/renderer/BufferObject.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ idBufferObject::idBufferObject() {
127127

128128
#if defined( ID_VULKAN )
129129
m_apiObject = VK_NULL_HANDLE;
130+
#if defined( ID_USE_AMD_ALLOCATOR )
131+
m_vmaAllocation = NULL;
132+
#endif
130133
#else
131134
m_apiObject = 0xFFFF;
132135
m_buffer = NULL;

neo/renderer/BufferObject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class idBufferObject {
8686
#if defined( ID_VULKAN )
8787
VkBuffer m_apiObject;
8888
#if defined( ID_USE_AMD_ALLOCATOR )
89-
VmaAllocation m_allocation;
89+
VmaAllocation m_vmaAllocation;
90+
VmaAllocationInfo m_allocation;
9091
#else
9192
vulkanAllocation_t m_allocation;
9293
#endif

neo/renderer/RenderBackend.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ struct vulkanContext_t {
169169
uint32 currentSwapIndex;
170170
VkImage msaaImage;
171171
VkImageView msaaImageView;
172+
#if defined( ID_USE_AMD_ALLOCATOR )
173+
VmaAllocation msaaVmaAllocation;
174+
VmaAllocationInfo msaaAllocation;
175+
#else
172176
vulkanAllocation_t msaaAllocation;
177+
#endif
173178
idArray< idImage * , NUM_FRAME_DATA > swapchainImages;
174179
idArray< VkFramebuffer, NUM_FRAME_DATA > frameBuffers;
175180
idArray< VkSemaphore, NUM_FRAME_DATA > acquireSemaphores;

neo/renderer/Vulkan/BufferObject_VK.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ If you have questions concerning this license or the applicable additional terms
3232
#include "../BufferObject.h"
3333
#include "Staging_VK.h"
3434

35-
#if defined( ID_USE_AMD_ALLOCATOR )
36-
#define VMA_IMPLEMENTATION
37-
38-
#define VMA_MAX( v1, v2 ) Max( (v1), (v2) )
39-
#define VMA_MIN( v1, v2 ) Min( (v1), (v2) )
40-
41-
#include "vma.h"
42-
#endif
43-
4435
extern idCVar r_showBuffers;
4536

4637
/*
@@ -108,7 +99,7 @@ bool idVertexBuffer::AllocBufferObject( const void * data, int allocSize, buffer
10899
vmaReq.flags = VMA_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT;
109100
}
110101

111-
ID_VK_CHECK( vmaCreateBuffer( vmaAllocator, &bufferCreateInfo, &vmaReq, &m_apiObject, &m_allocation, NULL ) );
102+
ID_VK_CHECK( vmaCreateBuffer( vmaAllocator, &bufferCreateInfo, &vmaReq, &m_apiObject, &m_vmaAllocation, &m_allocation ) );
112103

113104
#else
114105
VkResult ret = vkCreateBuffer( vkcontext.device, &bufferCreateInfo, NULL, &m_apiObject );
@@ -167,9 +158,10 @@ void idVertexBuffer::FreeBufferObject() {
167158

168159
if ( m_apiObject != VK_NULL_HANDLE ) {
169160
#if defined( ID_USE_AMD_ALLOCATOR )
170-
vmaDestroyBuffer( vmaAllocator, m_apiObject, m_allocation );
161+
vmaDestroyBuffer( vmaAllocator, m_apiObject, m_vmaAllocation );
171162
m_apiObject = VK_NULL_HANDLE;
172-
m_allocation = VmaAllocation();
163+
m_allocation = VmaAllocationInfo();
164+
m_vmaAllocation = NULL;
173165
#else
174166
vulkanAllocator.Free( m_allocation );
175167
vkDestroyBuffer( vkcontext.device, m_apiObject, NULL );
@@ -198,7 +190,7 @@ void idVertexBuffer::Update( const void * data, int size, int offset ) const {
198190
if ( m_usage == BU_DYNAMIC ) {
199191
CopyBuffer(
200192
#if defined( ID_USE_AMD_ALLOCATOR )
201-
(byte *)m_allocation->GetMappedData() + GetOffset() + offset,
193+
(byte *)m_allocation.pMappedData + GetOffset() + offset,
202194
#else
203195
m_allocation.data + GetOffset() + offset,
204196
#endif
@@ -233,7 +225,7 @@ void * idVertexBuffer::MapBuffer( bufferMapType_t mapType ) {
233225
}
234226

235227
#if defined( ID_USE_AMD_ALLOCATOR )
236-
void * buffer = (byte *)m_allocation->GetMappedData() + GetOffset();
228+
void * buffer = (byte *)m_allocation.pMappedData + GetOffset();
237229
#else
238230
void * buffer = m_allocation.data + GetOffset();
239231
#endif
@@ -271,7 +263,8 @@ void idVertexBuffer::ClearWithoutFreeing() {
271263
m_offsetInOtherBuffer = OWNS_BUFFER_FLAG;
272264
m_apiObject = VK_NULL_HANDLE;
273265
#if defined( ID_USE_AMD_ALLOCATOR )
274-
m_allocation = VmaAllocation();
266+
m_allocation = VmaAllocationInfo();
267+
m_vmaAllocation = NULL;
275268
#else
276269
m_allocation.deviceMemory = VK_NULL_HANDLE;
277270
#endif
@@ -332,7 +325,7 @@ bool idIndexBuffer::AllocBufferObject( const void * data, int allocSize, bufferU
332325
vmaReq.flags = VMA_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT;
333326
}
334327

335-
ID_VK_CHECK( vmaCreateBuffer( vmaAllocator, &bufferCreateInfo, &vmaReq, &m_apiObject, &m_allocation, NULL ) );
328+
ID_VK_CHECK( vmaCreateBuffer( vmaAllocator, &bufferCreateInfo, &vmaReq, &m_apiObject, &m_vmaAllocation, &m_allocation ) );
336329

337330
#else
338331
VkResult ret = vkCreateBuffer( vkcontext.device, &bufferCreateInfo, NULL, &m_apiObject );
@@ -391,9 +384,10 @@ void idIndexBuffer::FreeBufferObject() {
391384

392385
if ( m_apiObject != VK_NULL_HANDLE ) {
393386
#if defined( ID_USE_AMD_ALLOCATOR )
394-
vmaDestroyBuffer( vmaAllocator, m_apiObject, m_allocation );
387+
vmaDestroyBuffer( vmaAllocator, m_apiObject, m_vmaAllocation );
395388
m_apiObject = VK_NULL_HANDLE;
396-
m_allocation = VmaAllocation();
389+
m_allocation = VmaAllocationInfo();
390+
m_vmaAllocation = NULL;
397391
#else
398392
vulkanAllocator.Free( m_allocation );
399393
vkDestroyBuffer( vkcontext.device, m_apiObject, NULL );
@@ -422,7 +416,7 @@ void idIndexBuffer::Update( const void * data, int size, int offset ) const {
422416
if ( m_usage == BU_DYNAMIC ) {
423417
CopyBuffer(
424418
#if defined( ID_USE_AMD_ALLOCATOR )
425-
(byte *)m_allocation->GetMappedData() + GetOffset() + offset,
419+
(byte *)m_allocation.pMappedData + GetOffset() + offset,
426420
#else
427421
m_allocation.data + GetOffset() + offset,
428422
#endif
@@ -457,7 +451,7 @@ void * idIndexBuffer::MapBuffer( bufferMapType_t mapType ) {
457451
}
458452

459453
#if defined( ID_USE_AMD_ALLOCATOR )
460-
void * buffer = (byte *)m_allocation->GetMappedData() + GetOffset();
454+
void * buffer = (byte *)m_allocation.pMappedData + GetOffset();
461455
#else
462456
void * buffer = m_allocation.data + GetOffset();
463457
#endif
@@ -495,7 +489,8 @@ void idIndexBuffer::ClearWithoutFreeing() {
495489
m_offsetInOtherBuffer = OWNS_BUFFER_FLAG;
496490
m_apiObject = VK_NULL_HANDLE;
497491
#if defined( ID_USE_AMD_ALLOCATOR )
498-
m_allocation = VmaAllocation();
492+
m_allocation = VmaAllocationInfo();
493+
m_vmaAllocation = NULL;
499494
#else
500495
m_allocation.deviceMemory = VK_NULL_HANDLE;
501496
#endif
@@ -557,13 +552,13 @@ bool idUniformBuffer::AllocBufferObject( const void * data, int allocSize, buffe
557552
vmaReq.flags = VMA_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT;
558553
}
559554

560-
ID_VK_CHECK( vmaCreateBuffer( vmaAllocator, &bufferCreateInfo, &vmaReq, &m_apiObject, &m_allocation, NULL ) );
555+
ID_VK_CHECK( vmaCreateBuffer( vmaAllocator, &bufferCreateInfo, &vmaReq, &m_apiObject, &m_vmaAllocation, &m_allocation ) );
561556

562557
#else
563558
VkResult ret = vkCreateBuffer( vkcontext.device, &bufferCreateInfo, NULL, &m_apiObject );
564559
assert( ret == VK_SUCCESS );
565560

566-
VkMemoryRequirements memoryRequirements;
561+
VkMemoryRequirements memoryRequirements = {};
567562
vkGetBufferMemoryRequirements( vkcontext.device, m_apiObject, &memoryRequirements );
568563

569564
vulkanMemoryUsage_t memUsage = ( m_usage == BU_STATIC ) ? VULKAN_MEMORY_USAGE_GPU_ONLY : VULKAN_MEMORY_USAGE_CPU_TO_GPU;
@@ -616,9 +611,10 @@ void idUniformBuffer::FreeBufferObject() {
616611

617612
if ( m_apiObject != VK_NULL_HANDLE ) {
618613
#if defined( ID_USE_AMD_ALLOCATOR )
619-
vmaDestroyBuffer( vmaAllocator, m_apiObject, m_allocation );
614+
vmaDestroyBuffer( vmaAllocator, m_apiObject, m_vmaAllocation );
620615
m_apiObject = VK_NULL_HANDLE;
621-
m_allocation = VmaAllocation();
616+
m_allocation = VmaAllocationInfo();
617+
m_vmaAllocation = NULL;
622618
#else
623619
vulkanAllocator.Free( m_allocation );
624620
vkDestroyBuffer( vkcontext.device, m_apiObject, NULL );
@@ -647,7 +643,7 @@ void idUniformBuffer::Update( const void * data, int size, int offset ) const {
647643
if ( m_usage == BU_DYNAMIC ) {
648644
CopyBuffer(
649645
#if defined( ID_USE_AMD_ALLOCATOR )
650-
(byte *)m_allocation->GetMappedData() + GetOffset() + offset,
646+
(byte *)m_allocation.pMappedData + GetOffset() + offset,
651647
#else
652648
m_allocation.data + GetOffset() + offset,
653649
#endif
@@ -683,7 +679,7 @@ void * idUniformBuffer::MapBuffer( bufferMapType_t mapType ) {
683679
}
684680

685681
#if defined( ID_USE_AMD_ALLOCATOR )
686-
void * buffer = (byte *)m_allocation->GetMappedData() + GetOffset();
682+
void * buffer = (byte *)m_allocation.pMappedData + GetOffset();
687683
#else
688684
void * buffer = m_allocation.data + GetOffset();
689685
#endif
@@ -721,7 +717,8 @@ void idUniformBuffer::ClearWithoutFreeing() {
721717
m_offsetInOtherBuffer = OWNS_BUFFER_FLAG;
722718
m_apiObject = VK_NULL_HANDLE;
723719
#if defined( ID_USE_AMD_ALLOCATOR )
724-
m_allocation = VmaAllocation();
720+
m_allocation = VmaAllocationInfo();
721+
m_vmaAllocation = NULL;
725722
#else
726723
m_allocation.deviceMemory = VK_NULL_HANDLE;
727724
#endif

neo/renderer/Vulkan/Image_VK.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void idImage::PurgeImage() {
388388
m_imageGarbage[ m_garbageIndex ].Append( m_image );
389389

390390
#if defined( ID_USE_AMD_ALLOCATOR )
391-
m_allocation = VmaAllocation();
391+
m_allocation = NULL;
392392
#else
393393
m_allocation = vulkanAllocation_t();
394394
#endif

neo/renderer/Vulkan/RenderBackend_VK.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ static void CreateRenderTargets() {
938938
VmaMemoryRequirements vmaReq = {};
939939
vmaReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
940940

941-
ID_VK_CHECK( vmaCreateImage( vmaAllocator, &createInfo, &vmaReq, &vkcontext.msaaImage, &vkcontext.msaaAllocation, NULL ) );
941+
ID_VK_CHECK( vmaCreateImage( vmaAllocator, &createInfo, &vmaReq, &vkcontext.msaaImage, &vkcontext.msaaVmaAllocation, &vkcontext.msaaAllocation ) );
942942
#else
943943
VkMemoryRequirements memoryRequirements = {};
944944
vkGetImageMemoryRequirements( vkcontext.device, vkcontext.msaaImage, &memoryRequirements );
@@ -974,9 +974,19 @@ DestroyRenderTargets
974974
=============
975975
*/
976976
static void DestroyRenderTargets() {
977-
vulkanAllocator.Free( vkcontext.msaaAllocation );
978977
vkDestroyImageView( vkcontext.device, vkcontext.msaaImageView, NULL );
978+
#if defined( ID_USE_AMD_ALLOCATOR )
979+
vmaDestroyImage( vmaAllocator, vkcontext.msaaImage, vkcontext.msaaVmaAllocation );
980+
vkcontext.msaaAllocation = VmaAllocationInfo();
981+
vkcontext.msaaVmaAllocation = NULL;
982+
#else
979983
vkDestroyImage( vkcontext.device, vkcontext.msaaImage, NULL );
984+
vulkanAllocator.Free( vkcontext.msaaAllocation );
985+
vkcontext.msaaAllocation = vulkanAllocation_t();
986+
#endif
987+
988+
vkcontext.msaaImage = VK_NULL_HANDLE;
989+
vkcontext.msaaImageView = VK_NULL_HANDLE;
980990
}
981991

982992
/*

neo/renderer/Vulkan/qvk.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ If you have questions concerning this license or the applicable additional terms
3838
#include <vulkan/vulkan.h>
3939

4040
#if defined( ID_USE_AMD_ALLOCATOR )
41-
#define VMA_MAX( v1, v2 ) Max( (v1), (v2) )
42-
#define VMA_MIN( v1, v2 ) Min( (v1), (v2) )
43-
4441
#include "vma.h"
4542
#endif
4643

neo/renderer/Vulkan/vma.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (c) 2017 Advanced Micro Devices, Inc. All rights reserved.
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
//
22+
23+
#pragma warning( disable: 4244 ) // warning C4244: conversion from 'double' to 'float', possible loss of data
24+
25+
template<class T> T Max( T x, T y ) { return ( x > y ) ? x : y; }
26+
template<class T> T Min( T x, T y ) { return ( x < y ) ? x : y; }
27+
28+
#define VMA_IMPLEMENTATION
29+
#define VMA_MAX( v1, v2 ) Max( (v1), (v2) )
30+
#define VMA_MIN( v1, v2 ) Min( (v1), (v2) )
31+
#include "vma.h"

0 commit comments

Comments
 (0)