-
Notifications
You must be signed in to change notification settings - Fork 26
/
rendererthread_glcmd.cpp
724 lines (570 loc) · 17.5 KB
/
rendererthread_glcmd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
* Copyright (c) 2014-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2014-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
/* Contact [email protected] (Christoph Kubisch) for feedback */
#if HAS_OPENGL
#include <algorithm>
#include <assert.h>
#include <mutex>
#include <queue>
#include <nvgl/contextwindow_gl.hpp>
#include <nvpwindow.hpp>
#include "renderer.hpp"
#include "resources_gl.hpp"
#include "common.h"
namespace csfthreaded {
//////////////////////////////////////////////////////////////////////////
class RendererThreadedGLCMD : public Renderer
{
public:
enum Mode
{
MODE_BUFFER_PERS,
};
class Type : public Renderer::Type
{
bool isAvailable() const { return !!load_GL_NV_command_list(nvgl::ContextWindow::sysGetProcAddress); }
const char* name() const { return "GL MT nvcmd pers main process"; }
Renderer* create() const
{
RendererThreadedGLCMD* renderer = new RendererThreadedGLCMD();
renderer->m_mode = MODE_BUFFER_PERS;
return renderer;
}
Resources* resources() { return ResourcesGL::get(); }
unsigned int priority() const { return 5; }
};
public:
void init(const CadScene* NV_RESTRICT scene, Resources* resources, const Renderer::Config& config);
void deinit();
void draw(ShadeType shadetype, Resources* NV_RESTRICT resources, const Resources::Global& global);
Mode m_mode;
RendererThreadedGLCMD()
: m_mode(MODE_BUFFER_PERS)
{
}
private:
static const int NUM_FRAMES = 4;
struct ShadeCommand
{
std::vector<GLintptr> offsets;
std::vector<GLsizei> sizes;
std::vector<GLuint> states;
std::vector<GLuint> fbos;
int subframe;
GLuint buffer;
size_t bufferOffset;
size_t bufferSize;
unsigned char* NV_RESTRICT bufferData;
};
struct ThreadJob
{
RendererThreadedGLCMD* renderer;
int index;
GLuint m_buffers[NUM_FRAMES];
PointerStream m_streams[NUM_FRAMES];
std::string m_tokens[NUM_FRAMES];
int m_frame;
std::condition_variable m_hasWorkCond;
std::mutex m_hasWorkMutex;
volatile int m_hasWork;
size_t m_scIdx;
std::vector<ShadeCommand*> m_scs;
void resetFrame() { m_scIdx = 0; }
ShadeCommand* getFrameCommand()
{
if(m_scIdx + 1 > m_scs.size())
{
ShadeCommand* sc = new ShadeCommand;
m_scIdx++;
m_scs.push_back(sc);
return sc;
}
else
{
return m_scs[m_scIdx++];
}
}
};
std::vector<DrawItem> m_drawItems;
const ResourcesGL* NV_RESTRICT m_resources;
int m_numThreads;
ResourcesGL::StateChangeID m_state;
int m_workingSet;
ShadeType m_shade;
int m_frame;
GLsync m_syncs[NUM_FRAMES];
ThreadJob* m_jobs;
volatile int m_hadPrint;
volatile int m_ready;
volatile int m_stopThreads;
volatile size_t m_numCurItems;
std::condition_variable m_readyCond;
std::mutex m_readyMutex;
size_t m_numEnqueues;
std::queue<ShadeCommand*> m_drawQueue;
std::mutex m_workMutex;
std::mutex m_drawMutex;
static void threadMaster(void* arg)
{
ThreadJob* job = (ThreadJob*)arg;
job->renderer->RunThread(job->index);
}
bool getWork_ts(size_t& start, size_t& num)
{
std::lock_guard<std::mutex> lock(m_workMutex);
bool hasWork = false;
const size_t chunkSize = m_workingSet;
size_t total = m_drawItems.size();
if(m_numCurItems < total)
{
size_t batch = std::min(total - m_numCurItems, chunkSize);
start = m_numCurItems;
num = batch;
m_numCurItems += batch;
hasWork = true;
}
else
{
hasWork = false;
start = 0;
num = 0;
}
return hasWork;
}
void RunThread(int index);
unsigned int RunThreadFrame(ShadeType shadetype, ThreadJob& job);
void enqueueShadeCommand_ts(ShadeCommand* sc);
template <class T, ShadeType shade, bool sorted>
void GenerateTokens(T& stream, ShadeCommand& sc, const DrawItem* NV_RESTRICT drawItems, size_t numItems, const ResourcesGL* NV_RESTRICT res)
{
const CadScene* NV_RESTRICT scene = m_scene;
const CadSceneGL& sceneGL = res->m_scene;
int lastMaterial = -1;
int lastGeometry = -1;
int lastMatrix = -1;
bool lastSolid = true;
sc.fbos.clear();
sc.offsets.clear();
sc.sizes.clear();
sc.states.clear();
size_t begin = stream.size();
{
ResourcesGL::tokenUbo ubo;
ubo.cmd.index = DRAW_UBO_SCENE;
ubo.cmd.stage = UBOSTAGE_VERTEX;
ResourcesGL::encodeAddress(&ubo.cmd.addressLo, res->m_common.view.bufferADDR);
ubo.enqueue(stream);
ubo.cmd.stage = UBOSTAGE_FRAGMENT;
ubo.enqueue(stream);
ResourcesGL::tokenPolyOffset offset;
offset.cmd.bias = 1;
offset.cmd.scale = 1;
offset.enqueue(stream);
}
for(int i = 0; i < numItems; i++)
{
const DrawItem& di = drawItems[i];
if(shade == SHADE_SOLID && !di.solid)
{
if(sorted)
break;
continue;
}
if(shade == SHADE_SOLIDWIRE && di.solid != lastSolid)
{
sc.offsets.push_back(begin);
sc.sizes.push_back(GLsizei((stream.size() - begin)));
sc.states.push_back(lastSolid ? res->m_stateobjects.draw_line_tris : res->m_stateobjects.draw_line);
sc.fbos.push_back(res->m_framebuffer.fboScene);
begin = stream.size();
lastSolid = di.solid;
}
if(lastGeometry != di.geometryIndex)
{
const CadScene::Geometry& geo = scene->m_geometry[di.geometryIndex];
const CadSceneGL::Geometry& geogl = sceneGL.m_geometry[di.geometryIndex];
ResourcesGL::tokenVbo vbo;
vbo.cmd.index = 0;
ResourcesGL::encodeAddress(&vbo.cmd.addressLo, geogl.vbo.bufferADDR);
vbo.enqueue(stream);
ResourcesGL::tokenIbo ibo;
ResourcesGL::encodeAddress(&ibo.cmd.addressLo, geogl.ibo.bufferADDR);
ibo.cmd.typeSizeInByte = 4;
ibo.enqueue(stream);
lastGeometry = di.geometryIndex;
}
if(lastMatrix != di.matrixIndex)
{
ResourcesGL::tokenUbo ubo;
ubo.cmd.index = DRAW_UBO_MATRIX;
ubo.cmd.stage = UBOSTAGE_VERTEX;
ResourcesGL::encodeAddress(&ubo.cmd.addressLo,
sceneGL.m_buffers.matrices.bufferADDR + res->m_alignedMatrixSize * di.matrixIndex);
ubo.enqueue(stream);
lastMatrix = di.matrixIndex;
}
if(lastMaterial != di.materialIndex)
{
ResourcesGL::tokenUbo ubo;
ubo.cmd.index = DRAW_UBO_MATERIAL;
ubo.cmd.stage = UBOSTAGE_FRAGMENT;
ResourcesGL::encodeAddress(&ubo.cmd.addressLo,
sceneGL.m_buffers.materials.bufferADDR + res->m_alignedMaterialSize * di.materialIndex);
ubo.enqueue(stream);
lastMaterial = di.materialIndex;
}
ResourcesGL::tokenDrawElems drawelems;
drawelems.cmd.baseVertex = 0;
drawelems.cmd.count = di.range.count;
drawelems.cmd.firstIndex = GLuint((di.range.offset) / sizeof(GLuint));
drawelems.enqueue(stream);
}
sc.offsets.push_back(begin);
sc.sizes.push_back(GLsizei((stream.size() - begin)));
if(shade == SHADE_SOLID)
{
sc.states.push_back(res->m_stateobjects.draw_tris);
}
else
{
sc.states.push_back(lastSolid ? res->m_stateobjects.draw_line_tris : res->m_stateobjects.draw_line);
}
sc.fbos.push_back(res->m_framebuffer.fboScene);
}
template <class T>
void GenerateTokens(T& stream,
ShadeCommand& sc,
ShadeType shade,
const DrawItem* NV_RESTRICT drawItems,
size_t numItems,
const ResourcesGL* NV_RESTRICT res,
bool sorted)
{
if(sorted)
{
switch(shade)
{
case SHADE_SOLID:
GenerateTokens<T, SHADE_SOLID, true>(stream, sc, drawItems, numItems, res);
break;
case SHADE_SOLIDWIRE:
GenerateTokens<T, SHADE_SOLIDWIRE, true>(stream, sc, drawItems, numItems, res);
break;
}
}
else
{
switch(shade)
{
case SHADE_SOLID:
GenerateTokens<T, SHADE_SOLID, false>(stream, sc, drawItems, numItems, res);
break;
case SHADE_SOLIDWIRE:
GenerateTokens<T, SHADE_SOLIDWIRE, false>(stream, sc, drawItems, numItems, res);
break;
}
}
}
};
static RendererThreadedGLCMD::Type s_uborange;
void RendererThreadedGLCMD::init(const CadScene* NV_RESTRICT scene, Resources* resources, const Renderer::Config& config)
{
m_scene = scene;
const ResourcesGL* NV_RESTRICT res = (const ResourcesGL*)resources;
fillDrawItems(m_drawItems, config, true, true);
if(config.sorted)
{
std::sort(m_drawItems.begin(), m_drawItems.end(), DrawItem_compare_groups);
}
size_t worstCaseSize;
{
std::string dummy;
ShadeCommand sc;
GenerateTokens<std::string>(dummy, sc, SHADE_SOLIDWIRE, &m_drawItems[0], m_drawItems.size(), res, config.sorted);
worstCaseSize = (dummy.size() * 4) / 3;
LOGI("buffer size: %d\n", uint32_t(worstCaseSize));
}
res->rebuildStateObjects();
m_state = res->m_state;
m_resources = (const ResourcesGL*)resources;
m_numThreads = config.threads;
// make jobs
m_ready = 0;
m_jobs = new ThreadJob[m_numThreads];
m_stopThreads = 0;
for(int f = 0; f < NUM_FRAMES; f++)
{
m_syncs[f] = 0;
}
for(int i = 0; i < m_numThreads; i++)
{
ThreadJob& job = m_jobs[i];
job.index = i;
job.renderer = this;
job.m_hasWork = -1;
job.m_frame = 0;
if(m_mode == MODE_BUFFER_PERS)
{
glCreateBuffers(NUM_FRAMES, m_jobs[i].m_buffers);
for(int f = 0; f < NUM_FRAMES; f++)
{
glNamedBufferStorage(job.m_buffers[f], worstCaseSize, 0, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_DYNAMIC_STORAGE_BIT);
job.m_streams[f].init(glMapNamedBufferRange(job.m_buffers[f], 0, worstCaseSize, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT),
worstCaseSize);
}
}
s_threadpool.activateJob(i, threadMaster, &m_jobs[i]);
}
m_frame = 0;
}
void RendererThreadedGLCMD::deinit()
{
m_stopThreads = 1;
m_ready = 0;
NV_BARRIER();
for(int i = 0; i < m_numThreads; i++)
{
std::unique_lock<std::mutex> lock(m_jobs[i].m_hasWorkMutex);
m_jobs[i].m_hasWork = m_frame;
m_jobs[i].m_hasWorkCond.notify_one();
}
std::this_thread::yield();
{
std::unique_lock<std::mutex> lock(m_readyMutex);
while(m_ready < m_numThreads)
{
m_readyCond.wait(lock);
}
}
NV_BARRIER();
for(int f = 0; f < NUM_FRAMES; f++)
{
if(m_syncs[f])
{
glDeleteSync(m_syncs[f]);
}
}
for(int i = 0; i < m_numThreads; i++)
{
if(m_mode == MODE_BUFFER_PERS)
{
for(int f = 0; f < NUM_FRAMES; f++)
{
glUnmapNamedBuffer(m_jobs[i].m_buffers[f]);
}
glDeleteBuffers(NUM_FRAMES, m_jobs[i].m_buffers);
}
for(size_t s = 0; s < m_jobs[i].m_scs.size(); s++)
{
delete m_jobs[i].m_scs[s];
}
}
delete[] m_jobs;
m_drawItems.clear();
}
void RendererThreadedGLCMD::enqueueShadeCommand_ts(ShadeCommand* sc)
{
std::lock_guard<std::mutex> lock(m_drawMutex);
m_drawQueue.push(sc);
}
unsigned int RendererThreadedGLCMD::RunThreadFrame(ShadeType shadetype, ThreadJob& job)
{
unsigned int dispatches = 0;
bool first = true;
size_t tnum = 0;
size_t begin = 0;
size_t num = 0;
size_t offset = 0;
job.resetFrame();
int subframe = job.m_frame % NUM_FRAMES;
job.m_streams[subframe].clear();
while(getWork_ts(begin, num))
{
ShadeCommand* sc = job.getFrameCommand();
sc->bufferData = job.m_streams[subframe].dataptr;
if(m_mode == MODE_BUFFER_PERS)
{
sc->bufferOffset = job.m_streams[subframe].size();
}
GenerateTokens<PointerStream>(job.m_streams[subframe], *sc, shadetype, &m_drawItems[begin], num, m_resources,
m_config.sorted);
sc->bufferSize = job.m_streams[subframe].size() - sc->bufferOffset;
if(m_mode == MODE_BUFFER_PERS)
{
sc->buffer = job.m_buffers[subframe];
}
enqueueShadeCommand_ts(sc);
dispatches += 1;
tnum += num;
}
// NULL signals we are done
enqueueShadeCommand_ts(NULL);
return dispatches;
}
void RendererThreadedGLCMD::RunThread(int tid)
{
ThreadJob& job = m_jobs[tid];
ShadeType shadetype;
double timeWork = 0;
double timeFrame = 0;
int timerFrames = 0;
size_t dispatches = 0;
double timePrint = NVPSystem::getTime();
while(!m_stopThreads)
{
//NV_BARRIER();
double beginFrame = NVPSystem::getTime();
timeFrame -= NVPSystem::getTime();
{
std::unique_lock<std::mutex> lock(job.m_hasWorkMutex);
while(job.m_hasWork != job.m_frame)
{
job.m_hasWorkCond.wait(lock);
}
shadetype = m_shade;
}
if(m_stopThreads)
{
break;
}
double beginWork = NVPSystem::getTime();
timeWork -= NVPSystem::getTime();
dispatches += RunThreadFrame(shadetype, job);
job.m_frame++;
timeWork += NVPSystem::getTime();
double currentTime = NVPSystem::getTime();
timeFrame += currentTime;
timerFrames++;
if(timerFrames && (currentTime - timePrint) > 2.0)
{
timeFrame /= double(timerFrames);
timeWork /= double(timerFrames);
timeFrame *= 1000000.0;
timeWork *= 1000000.0;
timePrint = currentTime;
GLuint avgdispatch = GLuint(double(dispatches) / double(timerFrames));
#if PRINT_TIMER_STATS
LOGI("thread %d: work %6d [us] dispatches %5d\n", tid, GLuint(timeWork), GLuint(avgdispatch));
#endif
timeFrame = 0;
timeWork = 0;
timerFrames = 0;
dispatches = 0;
}
}
{
std::unique_lock<std::mutex> lock(m_readyMutex);
m_ready++;
m_readyCond.notify_all();
}
}
void RendererThreadedGLCMD::draw(ShadeType shadetype, Resources* NV_RESTRICT resources, const Resources::Global& global)
{
const CadScene* NV_RESTRICT scene = m_scene;
ResourcesGL* NV_RESTRICT res = (ResourcesGL*)resources;
const nvgl::ProfilerGL::Section profile(res->m_profilerGL, "Render");
// generic state setup
glViewport(0, 0, global.winWidth, global.winHeight);
// workaround
glEnableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
glEnableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
if(m_state.programs != res->m_state.programs || m_state.fbos != res->m_state.fbos)
{
res->rebuildStateObjects();
}
glBindFramebuffer(GL_FRAMEBUFFER, res->m_framebuffer.fboScene);
glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glNamedBufferSubData(res->m_common.view, 0, sizeof(SceneData), &global.sceneUbo);
m_workingSet = global.workingSet;
m_shade = shadetype;
m_numCurItems = 0;
m_numEnqueues = 0;
// generate & tokens/cmdbuffers in parallel
NV_BARRIER();
// start to dispatch threads
for(int i = 0; i < m_numThreads; i++)
{
{
std::unique_lock<std::mutex> lock(m_jobs[i].m_hasWorkMutex);
m_jobs[i].m_hasWork = m_frame;
}
m_jobs[i].m_hasWorkCond.notify_one();
}
int subframe = m_frame % NUM_FRAMES;
if(m_mode == MODE_BUFFER_PERS)
{
if(m_syncs[subframe])
{
GLenum ret = glClientWaitSync(m_syncs[subframe], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
glDeleteSync(m_syncs[subframe]);
m_syncs[subframe] = 0;
}
}
// dispatch drawing here
{
int numTerminated = 0;
while(true)
{
bool hadEntry = false;
ShadeCommand* sc = NULL;
{
std::lock_guard<std::mutex> lock(m_drawMutex);
if(!m_drawQueue.empty())
{
sc = m_drawQueue.front();
m_drawQueue.pop();
hadEntry = true;
}
}
if(hadEntry)
{
if(sc)
{
NV_BARRIER();
m_numEnqueues++;
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
glDrawCommandsStatesNV(sc->buffer, &sc->offsets[0], &sc->sizes[0], &sc->states[0], &sc->fbos[0],
(uint32_t)sc->sizes.size());
}
else
{
numTerminated++;
}
}
if(numTerminated == m_numThreads)
{
break;
}
std::this_thread::yield();
}
}
if(m_mode == MODE_BUFFER_PERS)
{
m_syncs[subframe] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}
m_frame++;
glDisableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
glDisableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
m_state = res->m_state;
}
} // namespace csfthreaded
#endif