Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
CommandBuffer.inl
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#ifndef DVD_COMMAND_BUFFER_INL_
33#define DVD_COMMAND_BUFFER_INL_
34
35#include "CommandTypes.h"
36
37namespace Divide {
38namespace GFX {
39
40struct DrawCommand;
41struct BindShaderResourcesCommand;
42struct SendPushConstantsCommand;
43
44namespace
45{
46 template<typename T> requires std::is_base_of_v<CommandBase, T>
47 constexpr size_t MemoryPoolSize()
48 {
49 constexpr size_t g_commandSize = prevPOW2( sizeof( T ) );
50
51 if constexpr ( std::is_same<T, GFX::BindShaderResourcesCommand>::value )
52 {
53 return g_commandSize * (1u << 5);
54 }
55 else if constexpr ( std::is_same<T, GFX::DrawCommand>::value )
56 {
57 return g_commandSize * (1u << 6);
58 }
59
60 return g_commandSize * (1u << 4);
61 }
62
63 template<typename T> requires std::is_base_of_v<CommandBase, T>
65 {
66 using Pool = MemoryPool<T, MemoryPoolSize<T>()>;
67
68 static Pool& GetPool()
69 {
70 NO_DESTROY thread_local Pool pool;
71 return pool;
72 }
73 };
74}
75
76template<typename T> requires std::is_base_of_v<CommandBase, T>
78{
79 T* mem = CmdAllocator<T>::GetPool().newElement();
80 _commands.emplace_back(mem);
81 return mem;
82}
83
84template<typename T> requires std::is_base_of_v<CommandBase, T>
85T* CommandBuffer::add(const T& command)
86{
87 T* mem = CmdAllocator<T>::GetPool().newElement(command);
88 _commands.emplace_back( mem );
89 return mem;
90}
91
92template<typename T> requires std::is_base_of_v<CommandBase, T>
93T* CommandBuffer::add(T&& command)
94{
95 T* mem = CmdAllocator<T>::GetPool().newElement( MOV(command) );
96 _commands.emplace_back( mem );
97 return mem;
98}
99
100template<typename T> requires std::is_base_of_v<CommandBase, T>
101bool TryMergeCommands(const CommandType type, T* prevCommand, T* crtCommand)
102{
104
105 bool ret = false;
106 assert(prevCommand != nullptr && crtCommand != nullptr);
107 switch (type)
108 {
110 {
111 ret = Merge(static_cast<DrawCommand*>(prevCommand), static_cast<DrawCommand*>(crtCommand));
112 } break;
114 {
115 ret = Merge( static_cast<MemoryBarrierCommand*>(prevCommand), static_cast<MemoryBarrierCommand*>(crtCommand));
116 } break;
118 {
119 ret = Merge( static_cast<SendPushConstantsCommand*>(prevCommand), static_cast<SendPushConstantsCommand*>(crtCommand));
120 } break;
121 default:
122 {
123 ret = false;
124 } break;
125 }
126
127 return ret;
128}
129
130}; //namespace GFX
131}; //namespace Divide
132
133#endif //DVD_COMMAND_BUFFER_INL_
#define MOV(...)
#define NO_DESTROY
#define PROFILE_SCOPE_AUTO(CATEGORY)
Definition: Profiler.h:87
bool Merge(DrawCommand *prevCommand, DrawCommand *crtCommand)
bool TryMergeCommands(CommandType type, T *prevCommand, T *crtCommand)
constexpr Optick::Category::Type Graphics
Definition: Profiler.h:60
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 prevPOW2(U32 n) noexcept
Definition: MathHelper.inl:219