Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/win_clang_dbg_x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
set "PATH=%CD%\..\depot_tools;%PATH%"
set "DEPOT_TOOLS_WIN_TOOLCHAIN=0"
cd base
gn gen out\Debug --args="is_debug=true gpgmm_enable_assert_on_warning=true gpgmm_enable_device_checks=true"
gn gen out\Debug --args="is_debug=true gpgmm_enable_assert_on_warning=true gpgmm_enable_device_checks=true gpgmm_disable_size_cache=true"

- name: Build for main branch
shell: cmd
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
set "PATH=%CD%\..\depot_tools;%PATH%"
set "DEPOT_TOOLS_WIN_TOOLCHAIN=0"
cd test
gn gen out\Debug --args="is_debug=true gpgmm_enable_assert_on_warning=true gpgmm_enable_device_checks=true"
gn gen out\Debug --args="is_debug=true gpgmm_enable_assert_on_warning=true gpgmm_enable_device_checks=true gpgmm_disable_size_cache=true"

- name: Build for main branch (with patch)
shell: cmd
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/win_msvc_dbg_x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
set "PATH=%CD%\..\depot_tools;%PATH%"
set "DEPOT_TOOLS_WIN_TOOLCHAIN=0"
cd test
gn gen out\Debug --args="is_debug=true is_clang=false"
gn gen out\Debug --args="is_debug=true is_clang=false gpgmm_disable_size_cache=true"

- name: Build for main branch (with patch)
shell: cmd
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/win_msvc_dbg_x64_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
shell: cmd
run: |
cd test
cmake . -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DCMAKE_TOOLCHAIN_FILE=..\vcpkg\scripts\buildsystems\vcpkg.cmake
cmake . -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DGPGMM_DISABLE_SIZE_CACHE=ON -DCMAKE_TOOLCHAIN_FILE=..\vcpkg\scripts\buildsystems\vcpkg.cmake

- name: Build for main branch (with patch)
shell: cmd
Expand Down
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ option_if_not_defined(GPGMM_FORCE_TRACING "Enables event tracing even in release
option_if_not_defined(GPGMM_ENABLE_DEVICE_CHECKS "Enables checking of device leaks" OFF)
option_if_not_defined(GPGMM_ENABLE_ALLOCATOR_CHECKS "Enables checking of allocator leaks" ${ENABLE_ALLOCATOR_CHECKS})
option_if_not_defined(GPGMM_ENABLE_ASSERT_ON_WARNING "Enables ASSERT on severity functionality" ${ENABLE_ASSERT_ON_WARNING})
option_if_not_defined(GPGMM_ENABLE_SIZE_CACHE "Enables warming of caches with common sizes" ON)
option_if_not_defined(GPGMM_DISABLE_SIZE_CACHE "Enables warming of caches with common resource sizes" OFF)

# The Vulkan loader is an optional dependency.
if (GPGMM_ENABLE_VK)
Expand Down Expand Up @@ -180,8 +180,8 @@ if(GPGMM_ENABLE_ASSERT_ON_WARNING)
target_compile_definitions(gpgmm_common_config INTERFACE "GPGMM_ENABLE_ASSERT_ON_WARNING")
endif()

if(GPGMM_ENABLE_SIZE_CACHE)
target_compile_definitions(gpgmm_common_config INTERFACE "GPGMM_ENABLE_SIZE_CACHE")
if(GPGMM_DISABLE_SIZE_CACHE)
target_compile_definitions(gpgmm_common_config INTERFACE "GPGMM_DISABLE_SIZE_CACHE")
endif()

if(WIN32)
Expand Down
6 changes: 3 additions & 3 deletions build_overrides/gpgmm_features.gni
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ declare_args() {
# Sets -dGPGMM_ENABLE_ASSERT_ON_WARNING
gpgmm_enable_assert_on_warning = is_debug

# Enables warming of caches with common sizes.
# Sets -dGPGMM_ENABLE_SIZE_CACHE
gpgmm_enable_size_cache = true
# Enables warming of caches with common resource sizes.
# Sets -dGPGMM_DISABLE_SIZE_CACHE
gpgmm_disable_size_cache = false

# Enables checking of device leaks.
# Sets -dGPGMM_ENABLE_DEVICE_CHECKS
Expand Down
2 changes: 1 addition & 1 deletion docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ GPGMM detects and reports leaks at the allocation-level (vs Memory Object). Shou

### Caching requirements

GPGMM conditionally caches for *known* requests sizes. The size-cache is predefined at compile-time and supports multiple *size classes* (ex. multiples of page or power-of-two). The size-cache can be disabled by not providing `GPGMM_ENABLE_SIZE_CACHE`.
GPGMM caches for common requests sizes. The size-cache is predefined at compile-time and supports multiple *size classes* (ex. page-multiple or power-of-two). The size-cache can be disabled by providing `GPGMM_DISABLE_SIZE_CACHE`.

### Capacity planning

Expand Down
1 change: 1 addition & 0 deletions src/gpgmm/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ source_set("gpgmm_sources") {
"d3d12/ResourceAllocatorD3D12.h",
"d3d12/ResourceHeapAllocatorD3D12.cpp",
"d3d12/ResourceHeapAllocatorD3D12.h",
"d3d12/ResourceSizeD3D12.h",
"d3d12/UtilsD3D12.cpp",
"d3d12/UtilsD3D12.h",
"d3d12/d3d12_platform.h",
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ config("gpgmm_common_config") {
defines += [ "GPGMM_ENABLE_ASSERT_ON_WARNING" ]
}

if (gpgmm_enable_size_cache) {
defines += [ "GPGMM_ENABLE_SIZE_CACHE" ]
if (gpgmm_disable_size_cache) {
defines += [ "GPGMM_DISABLE_SIZE_CACHE" ]
}

if (gpgmm_enable_device_checks) {
Expand Down
65 changes: 0 additions & 65 deletions src/gpgmm/common/MemorySize.h

This file was deleted.

59 changes: 59 additions & 0 deletions src/gpgmm/common/SizeClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021 The GPGMM Authors
//
// 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.

#ifndef GPGMM_COMMON_SIZECLASS_H_
#define GPGMM_COMMON_SIZECLASS_H_

#include <array>

namespace gpgmm {

struct SizeClassInfo {
uint64_t SizeInBytes;
uint64_t Alignment;
};

// Generates array containing values in range [2^N1...2^N2], where N2 > N1.
template <size_t N1, size_t N2>
static constexpr std::array<SizeClassInfo, N2 - N1 + 1> GeneratePowerOfTwoSizes() {
static_assert(N2 > N1, "Invalid range specified.");
std::array<SizeClassInfo, N2 - N1 + 1> sizeArray{};
for (size_t i = N1; i < N2; ++i) {
sizeArray[i] = {static_cast<uint64_t>(1ull << i), i};
}
return sizeArray;
}

template <size_t N, size_t alignment>
static constexpr std::array<SizeClassInfo, N> GenerateAlignedSizes() {
std::array<SizeClassInfo, N> sizeArray{};
for (size_t i = 1; i < N; ++i) {
sizeArray[i] = {i * alignment, alignment};
}
return sizeArray;
}

class MemorySizeClass {
protected:
static constexpr auto GenerateAllClassSizes() {
return kPowerOfTwoCacheSizes;
}

private:
static constexpr auto kPowerOfTwoCacheSizes = GeneratePowerOfTwoSizes<1, 27>();
};

} // namespace gpgmm

#endif // GPGMM_COMMON_SIZECLASS_H_
23 changes: 9 additions & 14 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "gpgmm/common/BuddyMemoryAllocator.h"
#include "gpgmm/common/Defaults.h"
#include "gpgmm/common/EventMessage.h"
#include "gpgmm/common/MemorySize.h"
#include "gpgmm/common/PooledMemoryAllocator.h"
#include "gpgmm/common/SegmentedMemoryAllocator.h"
#include "gpgmm/common/SlabMemoryAllocator.h"
Expand All @@ -34,6 +33,7 @@
#include "gpgmm/d3d12/ResidencyManagerD3D12.h"
#include "gpgmm/d3d12/ResourceAllocationD3D12.h"
#include "gpgmm/d3d12/ResourceHeapAllocatorD3D12.h"
#include "gpgmm/d3d12/ResourceSizeD3D12.h"
#include "gpgmm/d3d12/UtilsD3D12.h"

namespace gpgmm::d3d12 {
Expand Down Expand Up @@ -454,7 +454,7 @@ namespace gpgmm::d3d12 {
// increasing memory footprint. Since resources are always sized-aligned, the
// cached size must be requested per alignment {4KB, 64KB, or 4MB}. To avoid unbounded
// cache growth, a known set of pre-defined sizes initializes the allocators.
#if defined(GPGMM_ENABLE_SIZE_CACHE)
#if !defined(GPGMM_DISABLE_SIZE_CACHE)
{
// Temporary suppress log messages emitted from internal cache-miss requests.
ScopedLogLevel scopedLogLevel(LogSeverity::Info);
Expand All @@ -465,36 +465,31 @@ namespace gpgmm::d3d12 {
cacheRequest.AlwaysPrefetch = false;
cacheRequest.AvailableForAllocation = kInvalidSize;

for (uint64_t i = 0; i < MemorySize::kPowerOfTwoClassSize; i++) {
for (const SizeClassInfo& sizeInfo : ResourceSize::GenerateAllClassSizes()) {
MemoryAllocator* allocator = nullptr;
cacheRequest.SizeInBytes = MemorySize::kPowerOfTwoCacheSizes[i].SizeInBytes;
cacheRequest.SizeInBytes = sizeInfo.SizeInBytes;
cacheRequest.Alignment = sizeInfo.Alignment;

allocator = mSmallBufferAllocatorOfType[resourceHeapTypeIndex].get();
if (cacheRequest.SizeInBytes <= allocator->GetMemorySize() &&
IsAligned(cacheRequest.SizeInBytes,
D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT)) {
cacheRequest.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
sizeInfo.Alignment == D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT) {
allocator->TryAllocateMemory(cacheRequest);
}

allocator = mResourceAllocatorOfType[resourceHeapTypeIndex].get();
if (cacheRequest.SizeInBytes <= allocator->GetMemorySize() &&
IsAligned(cacheRequest.SizeInBytes,
D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT)) {
cacheRequest.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
sizeInfo.Alignment == D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT) {
allocator->TryAllocateMemory(cacheRequest);
}

allocator = mMSAAResourceAllocatorOfType[resourceHeapTypeIndex].get();
if (cacheRequest.SizeInBytes <= allocator->GetMemorySize() &&
IsAligned(cacheRequest.SizeInBytes,
D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT)) {
cacheRequest.Alignment = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT;
sizeInfo.Alignment == D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT) {
allocator->TryAllocateMemory(cacheRequest);
}
}
}
#endif // defined(GPGMM_ENABLE_SIZE_CACHE)
#endif // !defined(GPGMM_DISABLE_SIZE_CACHE)
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/gpgmm/d3d12/ResourceSizeD3D12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 The GPGMM Authors
//
// 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.

#ifndef GPGMM_D3D12_RESOURCESIZE_H_
#define GPGMM_D3D12_RESOURCESIZE_H_

#include "gpgmm/common/SizeClass.h"
#include "gpgmm/d3d12/d3d12_platform.h"
#include "gpgmm/utils/Utils.h"

namespace gpgmm::d3d12 {

// Generates array of resource sizes aligned per resource type.
class ResourceSize : public gpgmm::MemorySizeClass {
public:
static constexpr auto GenerateAllClassSizes() {
auto tmp0 = gpgmm::MemorySizeClass::GenerateAllClassSizes();
auto tmp1 = ConstexprConcat<SizeClassInfo>(tmp0, kSmallResourceSizes);
auto tmp2 = ConstexprConcat<SizeClassInfo>(tmp1, kDefaultResourceCacheSizes);
auto tmp3 = ConstexprConcat<SizeClassInfo>(tmp2, kMSAAResourceCacheSizes);
return tmp3;
}

private:
// Small textures but non-MSAA (from 4KB to 64KB).
static constexpr auto kSmallResourceSizes =
GenerateAlignedSizes<16, D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT>();

// Buffer or textures and also MSAA (from 64KB to 1MB).
static constexpr auto kDefaultResourceCacheSizes =
GenerateAlignedSizes<16, D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT>();

// MSAA only textures (from 4MB to 64MB).
static constexpr auto kMSAAResourceCacheSizes =
GenerateAlignedSizes<16, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT>();
};

} // namespace gpgmm::d3d12

#endif // GPGMM_D3D12_RESOURCESIZE_H_
18 changes: 18 additions & 0 deletions src/gpgmm/utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ namespace gpgmm {
return ToString(object) + ToString(args...);
}

template <typename T, typename A1, typename A2>
constexpr auto ConstexprConcat(A1 lhs, A2 rhs) {
std::array<T, lhs.size() + rhs.size()> sizeArray{};
size_t i = 0;

for (auto& item : lhs) {
sizeArray[i] = std::move(item);
++i;
}

for (auto& item : rhs) {
sizeArray[i] = std::move(item);
++i;
}

return sizeArray;
}

} // namespace gpgmm

#endif // GPGMM_UTILS_UTILS_H_
6 changes: 3 additions & 3 deletions src/tests/D3D12Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ namespace gpgmm::d3d12 {
}

bool D3D12TestBase::IsSizeCacheEnabled() const {
#if defined(GPGMM_ENABLE_SIZE_CACHE)
return true;
#else
#if defined(GPGMM_DISABLE_SIZE_CACHE)
return false;
#else
return true;
#endif
}

Expand Down
Loading