forked from nvpro-samples/gl_commandlist_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatesystem.hpp
705 lines (583 loc) · 15.7 KB
/
statesystem.hpp
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
/*
* 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 ckubisch@nvidia.com (Christoph Kubisch) for feedback */
#ifndef STATESYSTEM_H__
#define STATESYSTEM_H__
#include <nvgl/extensions_gl.hpp>
#include <vector>
class StateSystem {
public:
static inline bool isBitSet(GLbitfield bits, GLuint key)
{
return (bits & (1 << key)) ? true : false;
}
static inline void setBit(GLbitfield& bits, GLuint key)
{
bits |= (1 << key);
}
static GLbitfield getBit(GLuint key)
{
return (1 << key);
}
static inline GLboolean setBitState(GLbitfield& bits, GLuint key, GLboolean state)
{
if (state) bits |= (1 << key);
else bits &= ~(1 << key);
return state;
}
static const GLuint MAX_DRAWBUFFERS = 8;
static const GLuint MAX_CLIPPLANES = 8;
static const GLuint MAX_VIEWPORTS = 16;
static const GLuint MAX_VERTEXATTRIBS = 16;
static const GLuint MAX_VERTEXBINDINGS = 16;
static const GLuint MAX_COLORS = 4;
enum StateBits {
BLEND,
COLOR_LOGIC_OP,
CULL_FACE,
DEPTH_CLAMP,
DEPTH_TEST,
DITHER,
FRAMEBUFFER_SRGB,
LINE_SMOOTH,
MULTISAMPLE,
POLYGON_OFFSET_FILL,
POLYGON_OFFSET_LINE,
POLYGON_OFFSET_POINT,
POLYGON_SMOOTH,
PRIMITIVE_RESTART,
PRIMITIVE_RESTART_FIXED_INDEX,
RASTERIZER_DISCARD,
SAMPLE_ALPHA_TO_COVERAGE,
SAMPLE_ALPHA_TO_ONE,
SAMPLE_COVERAGE,
SAMPLE_SHADING,
SAMPLE_MASK,
STENCIL_TEST,
SCISSOR_TEST,
TEXTURE_CUBE_MAP_SEAMLESS,
PROGRAM_POINT_SIZE,
NUM_STATEBITS,
};
#if STATESYSTEM_USE_DEPRECATED
enum StateBitsDepr {
DEPR_ALPHA_TEST,
DEPR_LINE_STIPPLE,
DEPR_POINT_SMOOTH,
DEPR_POINT_SPRITE,
DEPR_POLYGON_STIPPLE,
NUM_STATEBITSDEPR,
};
#endif
enum Faces {
FACE_FRONT,
FACE_BACK,
MAX_FACES,
};
//////////////////////////////////////////////////////////////////////////
struct ClipDistanceState {
GLbitfield enabled;
ClipDistanceState()
{
enabled = 0;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
#if STATESYSTEM_USE_DEPRECATED
struct AlphaStateDepr {
GLenum mode;
GLfloat refvalue;
AlphaStateDepr()
{
mode = GL_ALWAYS;
refvalue = 1.0;
}
void applyGL() const;
void getGL();
};
#endif
//////////////////////////////////////////////////////////////////////////
struct StencilOp
{
GLenum fail;
GLenum zfail;
GLenum zpass;
};
struct StencilFunc
{
GLenum func;
GLuint refvalue;
GLuint mask;
};
struct StencilState {
StencilFunc funcs[MAX_FACES];
StencilOp ops[MAX_FACES];
StencilState()
{
for (GLuint i = 0; i < MAX_FACES; i++) {
funcs[i].func = GL_ALWAYS;
funcs[i].refvalue = 0;
funcs[i].mask = ~0;
}
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct BlendMode {
GLenum srcw;
GLenum dstw;
GLenum equ;
};
struct BlendStage {
BlendMode rgb;
BlendMode alpha;
};
struct BlendState {
GLbitfield separateEnable; // only set this if you want per draw enable
//GLfloat color[4];
GLuint useSeparate; // if set uses per draw, otherwise first
BlendStage blends[MAX_DRAWBUFFERS];
BlendState() {
separateEnable = 0;
useSeparate = GL_FALSE;
for (GLuint i = 0; i < MAX_DRAWBUFFERS; i++) {
blends[i].alpha.srcw = GL_ONE;
blends[i].alpha.dstw = GL_ZERO;
blends[i].alpha.equ = GL_FUNC_ADD;
blends[i].rgb = blends[i].alpha;
}
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct DepthState {
GLenum func;
// depth bounds for NV?
DepthState() {
func = GL_LESS;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct LogicState {
GLenum op;
LogicState() {
op = GL_COPY;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct RasterState {
//GLenum frontFace;
GLenum cullFace;
//GLfloat polyOffsetFactor;
//GLfloat polyOffsetUnits;
GLenum polyMode; // front and back, no separate support
//GLfloat lineWidth;
GLfloat pointSize;
GLfloat pointFade;
GLenum pointSpriteOrigin;
RasterState() {
//frontFace = GL_CCW;
cullFace = GL_BACK;
//polyOffsetFactor = 0;
//polyOffsetUnits = 0;
polyMode = GL_FILL;
//lineWidth = 1.0f;
pointSize = 1.0f;
pointFade = 1.0f;
pointSpriteOrigin = GL_UPPER_LEFT;
}
void applyGL() const;
void getGL();
};
#if STATESYSTEM_USE_DEPRECATED
struct RasterStateDepr {
GLint lineStippleFactor;
GLushort lineStipplePattern;
GLenum shadeModel;
// ignore polygonStipple
RasterStateDepr() {
lineStippleFactor = 1;
lineStipplePattern = ~0;
shadeModel = GL_SMOOTH;
}
void applyGL() const;
void getGL();
};
#endif
//////////////////////////////////////////////////////////////////////////
struct PrimitiveState {
GLuint restartIndex;
GLint patchVertices;
GLenum provokingVertex;
PrimitiveState() {
restartIndex = ~0;
patchVertices = 3;
provokingVertex = GL_LAST_VERTEX_CONVENTION;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct SampleState {
GLfloat coverage;
GLboolean invert;
GLuint mask;
SampleState() {
coverage = 1.0;
invert = GL_FALSE;
mask = ~0;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct Viewport {
float x;
float y;
float width;
float height;
};
struct DepthRange {
double nearPlane;
double farPlane;
};
struct Scissor {
GLint x;
GLint y;
GLsizei width;
GLsizei height;
};
/*
struct ViewportState {
GLuint useSeparate; // if set uses per view, otherwise first
Viewport viewports[MAX_VIEWPORTS];
ViewportState() {
useSeparate = GL_FALSE;
for (GLuint i = 0; i < MAX_VIEWPORTS; i++){
viewports[i].x = 0;
viewports[i].y = 0;
viewports[i].width = 0;
viewports[i].height = 0;
}
}
void applyGL() const;
void getGL();
};
*/
struct DepthRangeState {
GLuint useSeparate; // if set uses per view, otherwise first
DepthRange depths[MAX_VIEWPORTS];
DepthRangeState() {
useSeparate = GL_FALSE;
for (GLuint i = 0; i < MAX_VIEWPORTS; i++) {
depths[i].nearPlane = 0;
depths[i].farPlane = 1;
}
}
void applyGL() const;
void getGL();
};
/*
struct ScissorState {
GLuint useSeparate; // if set uses per draw, otherwise first
Scissor scissor[MAX_VIEWPORTS];
ScissorState() {
useSeparate = GL_FALSE;
for (GLuint i = 0; i < MAX_VIEWPORTS; i++){
scissor[i].x = 0;
scissor[i].y = 0;
scissor[i].width = 0;
scissor[i].height = 0;
}
}
void applyGL() const;
void getGL();
};
*/
struct ScissorEnableState {
GLbitfield separateEnable; // only set this if you want per view enable
ScissorEnableState() {
separateEnable = 0;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct MaskState {
GLuint colormaskUseSeparate;
GLboolean colormask[MAX_DRAWBUFFERS][MAX_COLORS];
GLboolean depth;
GLuint stencil[MAX_FACES];
MaskState() {
colormaskUseSeparate = GL_FALSE;
depth = GL_TRUE;
stencil[FACE_FRONT] = ~0;
stencil[FACE_BACK] = ~0;
for (GLuint i = 0; i < MAX_DRAWBUFFERS; i++) {
for (GLuint c = 0; c < MAX_COLORS; c++) {
colormask[i][c] = GL_TRUE;
}
}
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct FBOState {
GLuint fboDraw;
GLuint fboRead;
GLenum readBuffer;
GLenum drawBuffers[MAX_DRAWBUFFERS];
GLuint numBuffers;
FBOState() {
fboDraw = 0;
fboRead = 0;
readBuffer = GL_BACK;
for (GLuint i = 0; i < MAX_DRAWBUFFERS; i++) {
drawBuffers[i] = GL_NONE;
}
drawBuffers[0] = GL_BACK;
numBuffers = 1;
}
void setFbo(GLuint fbo) {
fboDraw = fbo;
fboRead = fbo;
readBuffer = GL_COLOR_ATTACHMENT0;
drawBuffers[0] = GL_COLOR_ATTACHMENT0;
numBuffers = 1;
}
void applyGL(bool noBind = false) const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct VertexEnableState {
GLbitfield enabled;
VertexEnableState() {
enabled = 0;
}
void applyGL(GLbitfield changed = ~0) const;
void getGL();
};
enum VertexModeType {
VERTEXMODE_FLOAT,
VERTEXMODE_INT,
VERTEXMODE_UINT,
// ignore double and int64 for now
};
struct VertexFormat {
VertexModeType mode;
GLboolean normalized;
GLuint size;
GLenum type;
GLsizei relativeoffset;
GLuint binding;
};
struct VertexBinding {
GLsizei divisor;
GLsizei stride;
};
struct VertexFormatState {
VertexFormat formats[MAX_VERTEXATTRIBS];
VertexBinding bindings[MAX_VERTEXBINDINGS];
VertexFormatState() {
for (GLuint i = 0; i < MAX_VERTEXATTRIBS; i++) {
formats[i].mode = VERTEXMODE_FLOAT;
formats[i].size = 4;
formats[i].type = GL_FLOAT;
formats[i].normalized = GL_FALSE;
formats[i].relativeoffset = 0;
formats[i].binding = i;
}
for (GLuint i = 0; i < MAX_VERTEXATTRIBS; i++) {
bindings[i].divisor = 0;
bindings[i].stride = 0;
}
}
void applyGL(GLbitfield changedFormat = ~0, GLbitfield changedBinding = ~0) const;
void getGL();
};
struct VertexData {
VertexModeType mode;
union {
float floats[4];
int ints[4];
unsigned int uints[4];
};
};
struct VertexImmediateState {
VertexData data[MAX_VERTEXATTRIBS];
VertexImmediateState() {
for (GLuint i = 0; i < MAX_VERTEXATTRIBS; i++) {
data[i].mode = VERTEXMODE_FLOAT;
data[i].floats[0] = 0;
data[i].floats[1] = 0;
data[i].floats[2] = 0;
data[i].floats[3] = 1;
}
}
void applyGL(GLbitfield changed = ~0) const;
void getGL(); // ensure proper mode, otherwise will get garbage
};
//////////////////////////////////////////////////////////////////////////
struct ProgramState {
// for sake of simplicity this mechanism only support programs
// and not program pipelines, nor use of subroutines
GLuint program;
ProgramState() {
program = 0;
}
void applyGL() const;
void getGL();
};
//////////////////////////////////////////////////////////////////////////
struct EnableState {
GLbitfield stateBits;
EnableState() {
stateBits = 0;
}
void applyGL(GLbitfield changed = ~0) const;
void getGL();
};
#if STATESYSTEM_USE_DEPRECATED
struct EnableStateDepr {
GLbitfield stateBitsDepr;
EnableStateDepr() {
stateBitsDepr = 0;
}
void applyGL(GLbitfield changed = ~0) const;
void getGL();
};
#endif
//////////////////////////////////////////////////////////////////////////
struct State {
EnableState enable;
#if STATESYSTEM_USE_DEPRECATED
EnableStateDepr enableDepr;
#endif
ProgramState program;
ClipDistanceState clip;
#if STATESYSTEM_USE_DEPRECATED
AlphaStateDepr alpha;
#endif
BlendState blend;
DepthState depth;
StencilState stencil;
LogicState logic;
PrimitiveState primitive;
SampleState sample;
RasterState raster;
#if STATESYSTEM_USE_DEPRECATED
RasterStateDepr rasterDepr;
#endif
//ViewportState viewport;
DepthRangeState depthrange;
//ScissorState scissor;
ScissorEnableState scissorenable;
MaskState mask;
FBOState fbo;
VertexEnableState vertexenable;
VertexFormatState vertexformat;
VertexImmediateState verteximm;
// This value only exists to ease compatibility with NV_command_list
// and is unaffected by apply or get operations, its value
// is set during StateSystem::set
GLenum basePrimitiveMode;
State()
: basePrimitiveMode(GL_TRIANGLES)
{
}
void applyGL(bool coreonly = false, bool skipFboBinding = false) const;
void getGL(bool coreonly = false);
};
typedef unsigned int StateID;
static const StateID INVALID_ID = ~0;
void init(bool coreonly = false);
void deinit();
void generate(GLuint num, StateID* objects);
void destroy(GLuint num, const StateID* objects);
void set(StateID id, const State& state, GLenum basePrimitiveMode);
const State& get(StateID id) const;
void applyGL(StateID id, bool skipFboBinding) const; // brute force sets everything
void applyGL(StateID id, StateID prev, bool skipFboBinding); // tries to avoid redundant, can pass INVALID_ID as previous
void prepareTransition(StateID id, StateID prev); // can speed up state apply
private:
static const int MAX_DIFFS = 16;
struct StateDiffKey {
StateID state;
GLuint changeID;
};
struct StateDiff {
enum ContentBits {
ENABLE,
ENABLE_DEPR,
PROGRAM,
CLIP,
ALPHA_DEPR,
BLEND,
DEPTH,
STENCIL,
LOGIC,
PRIMITIVE,
RASTER,
RASTER_DEPR,
//VIEWPORT,
DEPTHRANGE,
//SCISSOR,
SCISSORENABLE,
MASK,
FBO,
VERTEXENABLE,
VERTEXFORMAT,
VERTEXIMMEDIATE,
};
GLbitfield changedContentBits;
GLbitfield changedStateBits;
GLbitfield changedStateDeprBits;
GLbitfield changedVertexEnable;
GLbitfield changedVertexImm;
GLbitfield changedVertexFormat;
GLbitfield changedVertexBinding;
GLuint pad;
};
struct StateInternal {
State state;
GLuint changeID;
int usedDiff;
StateDiffKey others[MAX_DIFFS];
StateDiff diffs[MAX_DIFFS];
StateInternal() {
changeID = 0;
}
};
bool m_coreonly;
std::vector<StateInternal> m_states;
std::vector<StateID> m_freeIDs;
void makeDiff(StateDiff& diff, const StateInternal &fromInternal, const StateInternal &toInternal);
void applyDiffGL(const StateDiff& diff, const State &to, bool skipFboBinding);
int prepareTransitionCache(StateID prev, StateInternal& to);
};
#endif