Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
CommandBuffer.h
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#pragma once
33#ifndef DVD_COMMAND_BUFFER_H_
34#define DVD_COMMAND_BUFFER_H_
35
36
37namespace Divide {
38struct GenericDrawCommand;
39
40namespace GFX {
41
42enum class ErrorType : U8
43{
44 NONE = 0,
59 COUNT
60};
61
62namespace Names {
63 static const char* errorType[] = {
64 "NONE",
65 "MISSING_BEGIN_RENDER_PASS",
66 "INVALID_BEGIN_RENDER_PASS",
67 "MISSING_END_RENDER_PASS",
68 "MISSING_BEGIN_GPU_QUERY",
69 "MISSING_END_GPU_QUERY",
70 "MISSING_PUSH_DEBUG_SCOPE",
71 "MISSING_POP_DEBUG_SCOPE",
72 "MISSING_POP_CAMERA",
73 "MISSING_POP_VIEWPORT",
74 "MISSING_VALID_PIPELINE",
75 "MISSING_BLIT_DESCRIPTOR_SET",
76 "INVALID_DISPATCH_COUNT",
77 "INVALID_DESCRIPTOR_SET",
78 "INVALID_RENDER_PASS_FOR_PIPELINE",
79 "UNKNOW"
80 };
81};
82
83static_assert(std::size( Names::errorType ) == to_base( ErrorType::COUNT ) + 1u, "ErrorType name array out of sync!");
84
85enum class CommandType : U8;
86class CommandBuffer;
87struct CommandBase;
88struct DrawCommand;
89struct MemoryBarrierCommand;
90struct SendPushConstantsCommand;
91
93{
94 static constexpr U32 COMMAND_BUFFER_INIT_SIZE = 4u;
95
96 struct Entry
97 {
98 Handle<GFX::CommandBuffer> _buffer{INVALID_HANDLE<GFX::CommandBuffer>};
99 bool _owning{false};
100 };
101
102 eastl::fixed_vector<Entry, COMMAND_BUFFER_INIT_SIZE, true> _commandBuffers;
103};
104
108
110{
111 public:
112 static constexpr U32 COMMAND_BUFFER_INIT_SIZE = 32u;
113 using CommandList = eastl::fixed_vector<CommandBase*, COMMAND_BUFFER_INIT_SIZE, true>;
114
115 public:
117
118 template<typename T> requires std::is_base_of_v<CommandBase, T>
119 T* add();
120 template<typename T> requires std::is_base_of_v<CommandBase, T>
121 T* add(const T& command);
122 template<typename T> requires std::is_base_of_v<CommandBase, T>
123 T* add(T&& command);
124
125
126 void add(const CommandBuffer& other);
127 void add(Handle<CommandBuffer> other);
128
129 void batch();
130 void clear();
131 void clear( const char* name, size_t reservedCmdCount);
132
134 [[nodiscard]] string toString() const;
135
137 [[nodiscard]] std::pair<ErrorType, size_t> validate() const;
138
140
141 protected:
142
143 void clean();
144 bool cleanInternal();
145
146 protected:
147 bool _batched{ false };
149};
150
151static void ToString(const CommandBase& cmd, CommandType type, I32& crtIndent, string& out);
152
153// Return true if merge is successful
154template<typename T = CommandBase> requires std::is_base_of_v<CommandBase, T>
155[[nodiscard]] bool TryMergeCommands( CommandType type, T* prevCommand, T* crtCommand );
156[[nodiscard]] bool Merge(DrawCommand* prevCommand, DrawCommand* crtCommand);
157[[nodiscard]] bool Merge(MemoryBarrierCommand* lhs, MemoryBarrierCommand* rhs);
158[[nodiscard]] bool Merge(SendPushConstantsCommand* lhs, SendPushConstantsCommand* rhs);
159[[nodiscard]] bool BatchDrawCommands(GenericDrawCommand& previousGDC, GenericDrawCommand& currentGDC) noexcept;
160
161template<typename T> requires std::is_base_of_v<CommandBase, T>
162FORCE_INLINE T* EnqueueCommand(CommandBuffer& buffer) { return buffer.add<T>(); }
163
164template<typename T> requires std::is_base_of_v<CommandBase, T>
165FORCE_INLINE T* EnqueueCommand(CommandBuffer& buffer, T& cmd) { return buffer.add(cmd); }
166
167template<typename T> requires std::is_base_of_v<CommandBase, T>
168FORCE_INLINE T* EnqueueCommand(CommandBuffer& buffer, T&& cmd) { return buffer.add( MOV(cmd) ); }
169
170}; //namespace GFX
171}; //namespace Divide
172
173#endif //DVD_COMMAND_BUFFER_H_
174
175#include "CommandBuffer.inl"
#define MOV(...)
#define FORCE_INLINE
eastl::fixed_vector< CommandBase *, COMMAND_BUFFER_INIT_SIZE, true > CommandList
PROPERTY_R(CommandList, commands)
static constexpr U32 COMMAND_BUFFER_INIT_SIZE
string toString() const
Multi-line. indented list of all commands (and params for some of them)
std::pair< ErrorType, size_t > validate() const
Verify that the commands in the buffer are valid and in the right order.
static const char * errorType[]
Definition: CommandBuffer.h:63
void ToString(const CommandBase &cmd, const CommandType type, I32 &crtIndent, string &out)
void AddCommandBufferToQueue(CommandBufferQueue &queue, const Handle< GFX::CommandBuffer > &commandBuffer)
void ResetCommandBufferQueue(CommandBufferQueue &queue)
bool BatchDrawCommands(GenericDrawCommand &previousGDC, GenericDrawCommand &currentGDC) noexcept
FORCE_INLINE T * EnqueueCommand(CommandBuffer &buffer)
bool Merge(DrawCommand *prevCommand, DrawCommand *crtCommand)
bool TryMergeCommands(CommandType type, T *prevCommand, T *crtCommand)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
uint8_t U8
uint32_t U32
constexpr auto to_base(const Type value) -> Type
Definition: CommandBuffer.h:97
Handle< GFX::CommandBuffer > _buffer
Definition: CommandBuffer.h:98
bool _owning
Definition: CommandBuffer.h:99
static constexpr U32 COMMAND_BUFFER_INIT_SIZE
Definition: CommandBuffer.h:94
eastl::fixed_vector< Entry, COMMAND_BUFFER_INIT_SIZE, true > _commandBuffers