Photon Editor Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
GraphicsArena.ipp
Go to the documentation of this file.
1#pragma once
2
6
7#include <Common/assertion.h>
8
9#include <utility>
10
11namespace ph::editor::ghi
12{
13
14template<typename T, typename... Args>
15inline T* GraphicsArena::make(Args&&... args)
16{
17 if(!m_memoryBlock)
18 {
19 m_memoryBlock = allocNextBlock();
20 }
21
22 // Get next block if failed
23 T* ptr = m_memoryBlock->make<T>(std::forward<Args>(args)...);
24 if(!ptr)
25 {
26 m_memoryBlock = allocNextBlock();
27 ptr = m_memoryBlock->make<T>(std::forward<Args>(args)...);
28 }
29
30 // Generally should not be null. If so, consider to increase the size of default memory block.
31 PH_ASSERT(ptr);
32 return ptr;
33}
34
35template<typename T>
36inline TSpan<T> GraphicsArena::makeArray(std::size_t arraySize)
37{
38 PH_ASSERT_GT(arraySize, 0);
39
40 if(!m_memoryBlock)
41 {
42 m_memoryBlock = allocNextBlock();
43 }
44
45 // Alignment not checked--the allocation generally will align to `std::max_align_t` at least.
46 // If the user specifies custom alignment, there may be extra cost to satisfy the request
47 // (e.g., wasting a new default block then allocating a custom block).
48 bool mayFitDefaultBlock = sizeof(T) * arraySize <= m_memoryBlock->numAllocatedBytes();
49
50 TSpan<T> arr;
51 if(mayFitDefaultBlock)
52 {
53 // Only retry if current block is not a new one
54 arr = m_memoryBlock->makeArray<T>(arraySize);
55 if(!arr.data() && m_memoryBlock->numUsedBytes() > 0)
56 {
57 m_memoryBlock = allocNextBlock();
58 arr = m_memoryBlock->makeArray<T>(arraySize);
59 }
60 }
61
62 // If we cannot make the array with default block, then use a custom block with performance hit
63 if(!arr.data())
64 {
65 GraphicsMemoryBlock* customBlock = allocCustomBlock(sizeof(T) * arraySize, alignof(T));
66 arr = customBlock->makeArray<T>(arraySize);
67 }
68
69 // Cannot be null as we can fallback to custom block.
70 PH_ASSERT(arr.data());
71 return arr;
72}
73
74}// end namespace ph::editor::ghi
T * make(Args &&... args)
Make an object of type T as if by calling its constructor with Args.
Definition GraphicsArena.ipp:15
TSpan< T > makeArray(std::size_t arraySize)
Make an array of default constructed objects of type T.
Definition GraphicsArena.ipp:36
Definition GraphicsMemoryBlock.h:13
T * make(Args &&... args)
Make an object of type T as if by calling its constructor with Args.
Definition GraphicsMemoryBlock.ipp:38
TSpan< T > makeArray(std::size_t arraySize)
Make an array of default constructed objects of type T.
Definition GraphicsMemoryBlock.ipp:53
std::size_t numUsedBytes() const
Definition GraphicsMemoryBlock.ipp:20
Definition PlatformDisplay.h:13