Nebula
Loading...
Searching...
No Matches
drawthread.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
11//------------------------------------------------------------------------------
12#include "threading/thread.h"
13#include "threading/event.h"
14namespace CoreGraphics
15{
16
18{
19public:
20
22 DrawThread();
23
59
65
67 {
68 static const CommandType Type = Sync;
70 };
71
73 {
74 byte* buffer;
77
79 : buffer(nullptr)
80 , size(0)
81 , capacity(1024)
82 {};
83
85 void Append(const byte* buf, SizeT numBytes)
86 {
87 // if not enough space, resize
88 if (this->buffer == nullptr)
89 {
90 this->capacity = Math::max(numBytes, this->capacity);
91 this->buffer = new byte[this->capacity];
92 }
93 else if (this->size + numBytes > this->capacity)
94 {
95 // grow, but stop growing exponentially at 2^16
96 const SizeT grow = Math::max(numBytes, this->capacity >> 1);
97 this->capacity += grow;
98 byte* newBuf = new byte[this->capacity];
99
100 // copy over old contents
101 memcpy(newBuf, this->buffer, this->size);
102 delete[] this->buffer;
103 this->buffer = newBuf;
104 }
105
106 // write command to buffer
107 memcpy(this->buffer + this->size, buf, numBytes);
108 this->size += numBytes;
109 }
110
112 const SizeT Size() const
113 {
114 return this->size;
115 }
116
118 void Clear()
119 {
120 delete[] this->buffer;
121 this->buffer = nullptr;
122 this->capacity = 0;
123 this->size = 0;
124 }
125
127 void Reset()
128 {
129 this->size = 0;
130 }
131 };
132
134 void EmitWakeupSignal() override;
135
137 template<typename T> void Push(const T& command);
139 void Flush();
140
143protected:
146
150};
151
154
155
156//------------------------------------------------------------------------------
159template<typename T>
160inline void
161DrawThread::Push(const T& command)
162{
163 // lock resources
164 this->lock.Enter();
165
166 // create entry
167 Command entry;
168 entry.size = sizeof(T);
169 entry.type = T::Type;
170 this->commands.Append(entry);
171
172 // record memory of command
173 this->commandBuffer.Append(reinterpret_cast<const byte*>(&command), sizeof(T));
174
175 // release lock
176 this->lock.Leave();
177}
178
179} // namespace CoreGraphics
A CoreGraphics thread meant to only record draw commands to command buffers.
Definition drawthread.h:18
void Push(const T &command)
push command to thread
Definition drawthread.h:161
Threading::Event signalEvent
Definition drawthread.h:145
void EmitWakeupSignal() override
called if thread needs a wakeup call before stopping
Definition drawthread.cc:22
DrawThread()
constructor
Definition drawthread.cc:13
CommandType
Definition drawthread.h:25
@ StencilWriteMask
Definition drawthread.h:45
@ ViewportArray
Definition drawthread.h:40
@ ScissorRect
Definition drawthread.h:41
@ Draw
Definition drawthread.h:33
@ GraphicsPipeline
Definition drawthread.h:29
@ Viewport
Definition drawthread.h:39
@ Dispatch
Definition drawthread.h:36
@ BindDescriptors
Definition drawthread.h:37
@ EndCommand
Definition drawthread.h:28
@ WaitForEvent
Definition drawthread.h:49
@ InputAssemblyIndex
Definition drawthread.h:32
@ SetEvent
Definition drawthread.h:47
@ Barrier
Definition drawthread.h:50
@ InsertMarker
Definition drawthread.h:57
@ ComputePipeline
Definition drawthread.h:30
@ StencilRefs
Definition drawthread.h:43
@ BeginMarker
Definition drawthread.h:55
@ PushRange
Definition drawthread.h:38
@ ResetCommand
Definition drawthread.h:27
@ ScissorRectArray
Definition drawthread.h:42
@ IndirectIndexedDraw
Definition drawthread.h:35
@ BeginQuery
Definition drawthread.h:53
@ StencilReadMask
Definition drawthread.h:44
@ BeginCommand
Definition drawthread.h:26
@ Sync
Definition drawthread.h:51
@ UpdateBuffer
Definition drawthread.h:46
@ EndMarker
Definition drawthread.h:56
@ ResetEvent
Definition drawthread.h:48
@ EndQuery
Definition drawthread.h:54
@ InputAssemblyVertex
Definition drawthread.h:31
@ Timestamp
Definition drawthread.h:52
@ IndirectDraw
Definition drawthread.h:34
void Signal(Threading::Event *event)
submit sync call
Definition drawthread.cc:40
Threading::CriticalSection lock
Definition drawthread.h:144
CommandBuffer commandBuffer
Definition drawthread.h:148
void Flush()
flush commands
Definition drawthread.cc:31
Util::Array< Command > commands
Definition drawthread.h:147
Threading::Event * event
Definition drawthread.h:149
Critical section objects are used to protect a portion of code from parallel execution.
Nebula's dynamic array class.
Definition array.h:60
Acceleration structures are used to enable ray tracing on the GPU by dividing the scene into a BVH.
Definition accelerationstructure.h:24
DrawThread * CreateDrawThread()
create new draw thread, define in implementation thread
Definition vkcommandbufferthread.cc:14
__forceinline TYPE max(TYPE a, TYPE b)
Definition scalar.h:359
Definition drawthread.h:73
byte * buffer
Definition drawthread.h:74
void Reset()
reset buffer
Definition drawthread.h:127
void Append(const byte *buf, SizeT numBytes)
append to buffer
Definition drawthread.h:85
void Clear()
clear buffer
Definition drawthread.h:118
CommandBuffer()
Definition drawthread.h:78
SizeT size
Definition drawthread.h:75
const SizeT Size() const
get size
Definition drawthread.h:112
SizeT capacity
Definition drawthread.h:76
Definition drawthread.h:61
CommandType type
Definition drawthread.h:63
IndexT size
Definition drawthread.h:62
Definition drawthread.h:67
static const CommandType Type
Definition drawthread.h:68
Threading::Event * event
Definition drawthread.h:69
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48