Divide Framework  Gettin' there (and back again)
A free and open-source 3D Framework under heavy development
Commands.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4 
5 This file is part of DIVIDE Framework.
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 
30 */
31 
32 #pragma once
33 #ifndef _GFX_COMMAND_H_
34 #define _GFX_COMMAND_H_
35 
36 #include "GenericDrawCommand.h"
40 
41 struct ImDrawData;
42 
43 namespace Divide {
44 namespace GFX {
45 
46 constexpr size_t g_commandPoolSizeFactor = 256;
47 
48 template<typename T>
49 struct CmdAllocator {
51  static MemoryPool<T, prevPOW2(sizeof(T) * g_commandPoolSizeFactor)> s_Pool;
52 
53  template <class... Args>
54  static T* allocate(Args&&... args) {
55  return s_Pool.newElement(s_PoolMutex, std::forward<Args>(args)...);
56  }
57 
58  static void deallocate(T*& ptr) {
59  s_Pool.deleteElement(s_PoolMutex, ptr);
60  }
61 };
62 
63 #define TO_STR(arg) #arg
64 
65 #define DEFINE_POOL(Command) \
66 decltype(CmdAllocator<Command>::s_PoolMutex) CmdAllocator<Command>::s_PoolMutex; \
67 decltype(CmdAllocator<Command>::s_Pool) CmdAllocator<Command>::s_Pool; \
68 decltype(Command::s_deleter) Command::s_deleter;
69 
70 #define BEGIN_COMMAND(Name, Enum) struct Name final : Command<Name, Enum> { \
71 using Base = Command<Name, Enum>; \
72 inline const char* commandName() const noexcept final { return TO_STR(Enum); }
73 
74 #define END_COMMAND(Name) \
75 }
76 
77 enum class CommandType : U8 {
88  SET_BLEND,
89  CLEAR_RT,
90  RESET_RT,
92  BLIT_RT,
96  SET_CAMERA,
98  POP_CAMERA,
104  DRAW_TEXT,
105  DRAW_IMGUI,
114  EXTERNAL,
115  COUNT
116 };
117 
118 class CommandBuffer;
119 struct CommandBase;
120 
121 struct Deleter {
122  virtual ~Deleter() = default;
123 
124  virtual void operate(CommandBase*& cmd) const noexcept {
125  ACKNOWLEDGE_UNUSED(cmd);
126  }
127 };
128 
129 template<typename T>
130 struct DeleterImpl final : Deleter {
131  void operate(CommandBase*& cmd) const noexcept final {
132  CmdAllocator<T>::deallocate((T*&)(cmd));
133  cmd = nullptr;
134  }
135 };
136 
138 {
139  virtual ~CommandBase() = default;
140 
141  virtual void addToBuffer(CommandBuffer* buffer) const = 0;
142  virtual stringImpl toString(U16 indent) const = 0;
143  virtual const char* commandName() const noexcept = 0;
144 
145  virtual Deleter& getDeleter() const noexcept = 0;
146 };
147 
148 template<typename T, CommandType EnumVal>
149 struct Command : public CommandBase {
151 
152  virtual ~Command() = default;
153 
154  inline void addToBuffer(CommandBuffer* buffer) const final {
155  buffer->add(reinterpret_cast<const T&>(*this));
156  }
157 
158  stringImpl toString(U16 indent) const override {
159  ACKNOWLEDGE_UNUSED(indent);
160  return stringImpl(commandName());
161  }
162 
163  Deleter& getDeleter() const noexcept final {
164  return s_deleter;
165  }
166 
167  static constexpr CommandType EType = EnumVal;
168 };
169 
171  BindPipelineCommand(const Pipeline* pipeline) noexcept
172  : _pipeline(pipeline)
173  {
174  }
175 
176  BindPipelineCommand() = default;
177 
178  const Pipeline* _pipeline = nullptr;
179 
180  stringImpl toString(U16 indent) const final;
182 
183 
186  : _constants(constants)
187  {
188  }
189 
190  SendPushConstantsCommand() = default;
191 
193 
194  stringImpl toString(U16 indent) const final;
196 
197 
199  using CommandContainer = eastl::fixed_vector<GenericDrawCommand, 4, true, eastl::dvd_eastl_allocator>;
200 
201  static_assert(sizeof(GenericDrawCommand) == 32, "Wrong command size! May cause performance issues. Disable assert to continue anyway.");
202 
203  DrawCommand() = default;
204  DrawCommand(size_t reserveSize) { _drawCommands.reserve(reserveSize); }
205  DrawCommand(const GenericDrawCommand& cmd) : _drawCommands({ cmd }) {}
206  DrawCommand(const CommandContainer& cmds) : _drawCommands(cmds) {}
207 
209 
210  stringImpl toString(U16 indent) const final;
212 
213 
215  SetViewportCommand() = default;
216  SetViewportCommand(const Rect<I32>& viewport) noexcept : _viewport(viewport) {}
217 
219 
220  stringImpl toString(U16 indent) const final;
222 
224  PushViewportCommand() = default;
225  PushViewportCommand(const Rect<I32> & viewport) noexcept : _viewport(viewport) {}
226 
228 
229  stringImpl toString(U16 indent) const final;
231 
234 
236  RenderTargetID _target;
238  Str64 _name = "";
239 
240  stringImpl toString(U16 indent) const final;
242 
244  bool _setDefaultRTState = true;
246 
248  PixelBuffer* _buffer = nullptr;
251 
254 
256  bool _validateWriteLevel = false;
257  U16 _mipWriteLevel = std::numeric_limits<U16>::max();
260 
263 
265  // Depth layer to blit
266  DepthBlitEntry _blitDepth;
267  // List of colours + colour layer to blit
272 
274  RenderTargetID _target;
277 
279  RenderTargetID _source;
282 
284  RenderTargetID _source;
288 
290  TextureData _source;
294 
296  Texture* _texture = nullptr;
297  vec2<U32> _layerRange = { 0, 1 };
299 
301  Rect<I32> _rect;
302 
303  stringImpl toString(U16 indent) const final;
305 
307  BlendingProperties _blendProperties;
309 
311  SetCameraCommand() = default;
312  SetCameraCommand(const CameraSnapshot& snapshot) noexcept : _cameraSnapshot(snapshot) {}
313 
315 
316  stringImpl toString(U16 indent) const final;
318 
320  PushCameraCommand() = default;
321  PushCameraCommand(const CameraSnapshot & snapshot) : _cameraSnapshot(snapshot) {}
322 
325 
328 
330  SetClipPlanesCommand() = default;
331  SetClipPlanesCommand(const FrustumClipPlanes& clippingPlanes) : _clippingPlanes(clippingPlanes) {}
332 
334 
335  stringImpl toString(U16 indent) const final;
337 
339  DescriptorSet _set;
340 
341  stringImpl toString(U16 indent) const final;
342 
344 
346  Texture* _texture = nullptr;
347  U16 _baseLevel = 0;
348  U16 _maxLevel = 1000;
349 
350  stringImpl toString(U16 indent) const final;
351 
353 
355  Str64 _scopeName;
356  I32 _scopeID = -1;
357 
358  stringImpl toString(U16 indent) const final;
360 
363 
365  DrawTextCommand() = default;
366  DrawTextCommand(const TextElementBatch& batch) : _batch(batch) {}
367 
369 
370  stringImpl toString(U16 indent) const final;
372 
374  ImDrawData* _data = nullptr;
375  I64 _windowGUID = 0;
377 
379  vec3<U32> _computeGroupSize;
380 
381  stringImpl toString(U16 indent) const final;
383 
385  U32 _barrierMask = 0;
386 
387  stringImpl toString(U16 indent) const final;
389 
391  ShaderBuffer* _buffer = nullptr;
392  bufferPtr _target = nullptr;
393  U32 _offsetElementCount = 0;
394  U32 _elementCount = 0;
396 
397 
399  ShaderBuffer* _buffer = nullptr;
400  U32 _offsetElementCount = 0;
401  U32 _elementCount = 0;
403 
405  bool _lowerLeftOrigin = true;
406  bool _negativeOneToOneDepth = true;
407 
408  stringImpl toString(U16 indent) const final;
410 
412  std::function<void()> _cbk;
414 
415 }; //namespace GFX
416 }; //namespace Divide
417 
418 #endif //_GFX_COMMAND_H_
SetViewportCommand(const Rect< I32 > &viewport) noexcept
Definition: Commands.h:216
vectorEASTL< RenderTarget::DrawLayerParams > _writeLayers
Definition: Commands.h:258
thread::mutex Mutex
Definition: SharedMutex.h:51
virtual Deleter & getDeleter() const noexcept=0
vectorEASTL< ColourBlitEntry > _blitColours
Definition: Commands.h:268
virtual ~CommandBase()=default
CameraSnapshot _cameraSnapshot
Definition: Commands.h:314
BindPipelineCommand(const Pipeline *pipeline) noexcept
Definition: Commands.h:171
static void deallocate(T *&ptr)
Definition: Commands.h:58
void operate(CommandBase *&cmd) const noexcept final
Definition: Commands.h:131
CameraSnapshot _cameraSnapshot
Definition: Commands.h:323
CommandContainer _drawCommands
Definition: Commands.h:208
uint8_t U8
< For NavMesh creation
Definition: AIProcessor.cpp:8
virtual void addToBuffer(CommandBuffer *buffer) const =0
Definition: RenderTarget.h:84
FrustumClipPlanes _clippingPlanes
Definition: Commands.h:333
int64_t I64
std::function< Ret(Args...) > DELEGATE
virtual ~Command()=default
int32_t I32
virtual void operate(CommandBase *&cmd) const noexcept
Definition: Commands.h:124
SetCameraCommand(const CameraSnapshot &snapshot) noexcept
Definition: Commands.h:312
uint32_t U32
DrawCommand(const GenericDrawCommand &cmd)
Definition: Commands.h:205
#define ACKNOWLEDGE_UNUSED(p)
constexpr size_t g_commandPoolSizeFactor
Definition: Commands.h:46
uint16_t U16
PushCameraCommand(const CameraSnapshot &snapshot)
Definition: Commands.h:321
An API-independent representation of a texture.
Definition: Texture.h:57
stringAlg::basic_string< char, stringAlg::char_traits< char >, stringAlg::allocator< char > > stringImpl
Definition: STLString.h:51
DrawCommand(const CommandContainer &cmds)
Definition: Commands.h:206
void addToBuffer(CommandBuffer *buffer) const final
Definition: Commands.h:154
#define BEGIN_COMMAND(Name, Enum)
Definition: Commands.h:70
stringImpl toString(U16 indent) const override
Definition: Commands.h:158
DELEGATE< void, bufferPtr > _command
Definition: Commands.h:249
DrawCommand(size_t reserveSize)
Definition: Commands.h:204
static T * allocate(Args &&... args)
Definition: Commands.h:54
#define END_COMMAND(Name)
Definition: Commands.h:74
eastl::fixed_vector< GenericDrawCommand, 4, true, eastl::dvd_eastl_allocator > CommandContainer
Definition: Commands.h:199
TextElementBatch _batch
Definition: Commands.h:368
static Mutex s_PoolMutex
Definition: Commands.h:50
SendPushConstantsCommand(const PushConstants &constants)
Definition: Commands.h:185
virtual stringImpl toString(U16 indent) const =0
Deleter & getDeleter() const noexcept final
Definition: Commands.h:163
static MemoryPool< T, prevPOW2(sizeof(T) *g_commandPoolSizeFactor)> s_Pool
Definition: Commands.h:51
static DeleterImpl< T > s_deleter
Definition: Commands.h:150
DrawTextCommand(const TextElementBatch &batch)
Definition: Commands.h:366
SetClipPlanesCommand(const FrustumClipPlanes &clippingPlanes)
Definition: Commands.h:331
void * bufferPtr
constexpr U32 prevPOW2(U32 n) noexcept
Definition: MathHelper.inl:481
static constexpr CommandType EType
Definition: Commands.h:167
PushViewportCommand(const Rect< I32 > &viewport) noexcept
Definition: Commands.h:225
virtual ~Deleter()=default
virtual const char * commandName() const noexcept=0
eastl::vector< Type, Allocator > vectorEASTL
Definition: Vector.h:43