forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl_manager.cpp
More file actions
2235 lines (1881 loc) · 83.8 KB
/
gl_manager.cpp
File metadata and controls
2235 lines (1881 loc) · 83.8 KB
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2015-2017 Baldur Karlsson
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "driver/gl/gl_manager.h"
#include "driver/gl/gl_driver.h"
struct VertexAttribInitialData
{
uint32_t enabled;
uint32_t vbslot;
uint32_t offset;
GLenum type;
int32_t normalized;
uint32_t integer;
uint32_t size;
};
struct VertexBufferInitialData
{
ResourceId Buffer;
uint64_t Stride;
uint64_t Offset;
uint32_t Divisor;
};
// note these data structures below contain a 'valid' bool, since due to complexities of
// fetching the state on the right context, we might never be able to fetch the data at
// all. So the valid is set to false to indicate that we shouldn't try to restore it on
// replay.
struct VAOInitialData
{
bool valid;
VertexAttribInitialData VertexAttribs[16];
VertexBufferInitialData VertexBuffers[16];
ResourceId ElementArrayBuffer;
};
struct FeedbackInitialData
{
bool valid;
ResourceId Buffer[4];
uint64_t Offset[4];
uint64_t Size[4];
};
struct FramebufferAttachmentData
{
bool renderbuffer;
bool layered;
int32_t layer;
int32_t level;
ResourceId obj;
};
struct FramebufferInitialData
{
bool valid;
FramebufferAttachmentData Attachments[10];
GLenum DrawBuffers[8];
GLenum ReadBuffer;
static const GLenum attachmentNames[10];
};
const GLenum FramebufferInitialData::attachmentNames[10] = {
eGL_COLOR_ATTACHMENT0, eGL_COLOR_ATTACHMENT1, eGL_COLOR_ATTACHMENT2, eGL_COLOR_ATTACHMENT3,
eGL_COLOR_ATTACHMENT4, eGL_COLOR_ATTACHMENT5, eGL_COLOR_ATTACHMENT6, eGL_COLOR_ATTACHMENT7,
eGL_DEPTH_ATTACHMENT, eGL_STENCIL_ATTACHMENT,
};
template <>
void Serialiser::Serialise(const char *name, VertexAttribInitialData &el)
{
ScopedContext scope(this, name, "VertexArrayInitialData", 0, true);
Serialise("enabled", el.enabled);
Serialise("vbslot", el.vbslot);
Serialise("offset", el.offset);
Serialise("type", el.type);
Serialise("normalized", el.normalized);
Serialise("integer", el.integer);
Serialise("size", el.size);
}
template <>
void Serialiser::Serialise(const char *name, VertexBufferInitialData &el)
{
ScopedContext scope(this, name, "VertexBufferInitialData", 0, true);
Serialise("Buffer", el.Buffer);
Serialise("Stride", el.Stride);
Serialise("Offset", el.Offset);
Serialise("Divisor", el.Divisor);
}
template <>
void Serialiser::Serialise(const char *name, FeedbackInitialData &el)
{
ScopedContext scope(this, name, "FeedbackInitialData", 0, true);
Serialise("valid", el.valid);
SerialisePODArray<4>("Buffer", el.Buffer);
SerialisePODArray<4>("Offset", el.Offset);
SerialisePODArray<4>("Size", el.Size);
}
template <>
void Serialiser::Serialise(const char *name, FramebufferAttachmentData &el)
{
ScopedContext scope(this, name, "FramebufferAttachmentData", 0, true);
Serialise("renderbuffer", el.renderbuffer);
Serialise("layered", el.layered);
Serialise("layer", el.layer);
Serialise("level", el.level);
Serialise("obj", el.obj);
}
template <>
void Serialiser::Serialise(const char *name, FramebufferInitialData &el)
{
ScopedContext scope(this, name, "FramebufferInitialData", 0, true);
Serialise("valid", el.valid);
SerialisePODArray<8>("DrawBuffers", el.DrawBuffers);
for(size_t i = 0; i < ARRAY_COUNT(el.Attachments); i++)
Serialise("Attachments", el.Attachments[i]);
Serialise("ReadBuffer", el.ReadBuffer);
}
struct TextureStateInitialData
{
int32_t baseLevel, maxLevel;
float minLod, maxLod;
GLenum srgbDecode;
GLenum depthMode;
GLenum compareFunc, compareMode;
GLenum minFilter, magFilter;
int32_t seamless;
GLenum swizzle[4];
GLenum wrap[3];
float border[4];
float lodBias;
ResourceId texBuffer;
uint32_t texBufOffs;
uint32_t texBufSize;
};
template <>
void Serialiser::Serialise(const char *name, TextureStateInitialData &el)
{
ScopedContext scope(this, name, "TextureStateInitialData", 0, true);
Serialise("baseLevel", el.baseLevel);
Serialise("maxLevel", el.maxLevel);
Serialise("minLod", el.minLod);
Serialise("maxLod", el.maxLod);
Serialise("srgbDecode", el.srgbDecode);
Serialise("depthMode", el.depthMode);
Serialise("compareFunc", el.compareFunc);
Serialise("compareMode", el.compareMode);
Serialise("seamless", el.seamless);
Serialise("minFilter", el.minFilter);
Serialise("magFilter", el.magFilter);
SerialisePODArray<4>("swizzle", el.swizzle);
SerialisePODArray<3>("wrap", el.wrap);
SerialisePODArray<4>("border", el.border);
Serialise("lodBias", el.lodBias);
Serialise("texBuffer", el.texBuffer);
Serialise("texBufOffs", el.texBufOffs);
Serialise("texBufSize", el.texBufSize);
}
void GLResourceManager::MarkVAOReferenced(GLResource res, FrameRefType ref, bool allowFake0)
{
const GLHookSet &gl = m_GL->GetInternalHookset();
if(res.name || allowFake0)
{
MarkResourceFrameReferenced(res, ref == eFrameRef_Unknown ? eFrameRef_Unknown : eFrameRef_Read);
GLint numVBufferBindings = 16;
gl.glGetIntegerv(eGL_MAX_VERTEX_ATTRIB_BINDINGS, &numVBufferBindings);
for(GLuint i = 0; i < (GLuint)numVBufferBindings; i++)
{
GLuint buffer = GetBoundVertexBuffer(gl, i);
MarkResourceFrameReferenced(BufferRes(res.Context, buffer), ref);
}
GLuint ibuffer = 0;
gl.glGetIntegerv(eGL_ELEMENT_ARRAY_BUFFER_BINDING, (GLint *)&ibuffer);
MarkResourceFrameReferenced(BufferRes(res.Context, ibuffer), ref);
}
}
void GLResourceManager::MarkFBOReferenced(GLResource res, FrameRefType ref)
{
if(res.name == 0)
return;
MarkResourceFrameReferenced(res, ref == eFrameRef_Unknown ? eFrameRef_Unknown : eFrameRef_Read);
const GLHookSet &gl = m_GL->GetInternalHookset();
GLint numCols = 8;
gl.glGetIntegerv(eGL_MAX_COLOR_ATTACHMENTS, &numCols);
GLenum type = eGL_TEXTURE;
GLuint name = 0;
for(int c = 0; c < numCols; c++)
{
gl.glGetNamedFramebufferAttachmentParameterivEXT(res.name, GLenum(eGL_COLOR_ATTACHMENT0 + c),
eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
(GLint *)&name);
gl.glGetNamedFramebufferAttachmentParameterivEXT(res.name, GLenum(eGL_COLOR_ATTACHMENT0 + c),
eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
(GLint *)&type);
if(type == eGL_RENDERBUFFER)
MarkResourceFrameReferenced(RenderbufferRes(res.Context, name), ref);
else
MarkResourceFrameReferenced(TextureRes(res.Context, name), ref);
}
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, eGL_DEPTH_ATTACHMENT, eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, (GLint *)&name);
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, eGL_DEPTH_ATTACHMENT, eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, (GLint *)&type);
if(name)
{
if(type == eGL_RENDERBUFFER)
MarkResourceFrameReferenced(RenderbufferRes(res.Context, name), ref);
else
MarkResourceFrameReferenced(TextureRes(res.Context, name), ref);
}
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, eGL_STENCIL_ATTACHMENT, eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, (GLint *)&name);
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, eGL_STENCIL_ATTACHMENT, eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, (GLint *)&type);
if(name)
{
if(type == eGL_RENDERBUFFER)
MarkResourceFrameReferenced(RenderbufferRes(res.Context, name), ref);
else
MarkResourceFrameReferenced(TextureRes(res.Context, name), ref);
}
}
bool GLResourceManager::SerialisableResource(ResourceId id, GLResourceRecord *record)
{
if(id == m_GL->GetContextResourceID())
return false;
return true;
}
bool GLResourceManager::Need_InitialStateChunk(GLResource res)
{
return true;
}
bool GLResourceManager::Prepare_InitialState(GLResource res, byte *blob)
{
const GLHookSet &gl = m_State < WRITING ? m_GL->GetHookset() : m_GL->GetInternalHookset();
if(res.Namespace == eResFramebuffer)
{
FramebufferInitialData *data = (FramebufferInitialData *)blob;
data->valid = true;
GLuint prevread = 0, prevdraw = 0;
gl.glGetIntegerv(eGL_DRAW_FRAMEBUFFER_BINDING, (GLint *)&prevdraw);
gl.glGetIntegerv(eGL_READ_FRAMEBUFFER_BINDING, (GLint *)&prevread);
gl.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, res.name);
gl.glBindFramebuffer(eGL_READ_FRAMEBUFFER, res.name);
// need to serialise out which objects are bound
GLenum type = eGL_TEXTURE;
GLuint object = 0;
GLint layered = 0;
for(int i = 0; i < (int)ARRAY_COUNT(data->Attachments); i++)
{
FramebufferAttachmentData &a = data->Attachments[i];
gl.glGetNamedFramebufferAttachmentParameterivEXT(res.name, data->attachmentNames[i],
eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
(GLint *)&object);
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, data->attachmentNames[i], eGL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, (GLint *)&type);
a.renderbuffer = (type == eGL_RENDERBUFFER);
layered = 0;
a.level = 0;
a.layer = 0;
if(object && !a.renderbuffer)
{
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, data->attachmentNames[i], eGL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &a.level);
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, data->attachmentNames[i], eGL_FRAMEBUFFER_ATTACHMENT_LAYERED, &layered);
if(layered == 0)
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, data->attachmentNames[i], eGL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER, &a.layer);
}
a.layered = (layered != 0);
a.obj = GetID(a.renderbuffer ? RenderbufferRes(res.Context, object)
: TextureRes(res.Context, object));
if(!a.renderbuffer)
{
WrappedOpenGL::TextureData &details = m_GL->m_Textures[a.obj];
if(details.curType == eGL_TEXTURE_CUBE_MAP)
{
GLenum face;
gl.glGetNamedFramebufferAttachmentParameterivEXT(
res.name, data->attachmentNames[i], eGL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
(GLint *)&face);
a.layer = CubeTargetIndex(face);
}
}
}
for(int i = 0; i < (int)ARRAY_COUNT(data->DrawBuffers); i++)
gl.glGetIntegerv(GLenum(eGL_DRAW_BUFFER0 + i), (GLint *)&data->DrawBuffers[i]);
gl.glGetIntegerv(eGL_READ_BUFFER, (GLint *)&data->ReadBuffer);
gl.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, prevdraw);
gl.glBindFramebuffer(eGL_READ_FRAMEBUFFER, prevread);
}
else if(res.Namespace == eResFeedback)
{
FeedbackInitialData *data = (FeedbackInitialData *)blob;
data->valid = true;
GLuint prevfeedback = 0;
gl.glGetIntegerv(eGL_TRANSFORM_FEEDBACK, (GLint *)&prevfeedback);
gl.glBindTransformFeedback(eGL_TRANSFORM_FEEDBACK, res.name);
GLint maxCount = 0;
gl.glGetIntegerv(eGL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &maxCount);
for(int i = 0; i < (int)ARRAY_COUNT(data->Buffer) && i < maxCount; i++)
{
GLuint buffer = 0;
gl.glGetIntegeri_v(eGL_TRANSFORM_FEEDBACK_BUFFER_BINDING, i, (GLint *)&buffer);
data->Buffer[i] = GetID(BufferRes(res.Context, buffer));
gl.glGetInteger64i_v(eGL_TRANSFORM_FEEDBACK_BUFFER_START, i, (GLint64 *)&data->Offset[i]);
gl.glGetInteger64i_v(eGL_TRANSFORM_FEEDBACK_BUFFER_SIZE, i, (GLint64 *)&data->Size[i]);
}
gl.glBindTransformFeedback(eGL_TRANSFORM_FEEDBACK, prevfeedback);
}
else if(res.Namespace == eResVertexArray)
{
VAOInitialData *data = (VAOInitialData *)blob;
data->valid = true;
GLuint prevVAO = 0;
gl.glGetIntegerv(eGL_VERTEX_ARRAY_BINDING, (GLint *)&prevVAO);
if(res.name == 0)
gl.glBindVertexArray(m_GL->GetFakeVAO());
else
gl.glBindVertexArray(res.name);
for(GLuint i = 0; i < 16; i++)
{
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_ARRAY_ENABLED,
(GLint *)&data->VertexAttribs[i].enabled);
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_BINDING, (GLint *)&data->VertexAttribs[i].vbslot);
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_RELATIVE_OFFSET,
(GLint *)&data->VertexAttribs[i].offset);
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_ARRAY_TYPE, (GLint *)&data->VertexAttribs[i].type);
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_ARRAY_NORMALIZED,
(GLint *)&data->VertexAttribs[i].normalized);
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_ARRAY_INTEGER,
(GLint *)&data->VertexAttribs[i].integer);
gl.glGetVertexAttribiv(i, eGL_VERTEX_ATTRIB_ARRAY_SIZE, (GLint *)&data->VertexAttribs[i].size);
GLuint buffer = GetBoundVertexBuffer(gl, i);
data->VertexBuffers[i].Buffer = GetID(BufferRes(res.Context, buffer));
gl.glGetIntegeri_v(eGL_VERTEX_BINDING_STRIDE, i, (GLint *)&data->VertexBuffers[i].Stride);
gl.glGetIntegeri_v(eGL_VERTEX_BINDING_OFFSET, i, (GLint *)&data->VertexBuffers[i].Offset);
gl.glGetIntegeri_v(eGL_VERTEX_BINDING_DIVISOR, i, (GLint *)&data->VertexBuffers[i].Divisor);
}
GLuint buffer = 0;
gl.glGetIntegerv(eGL_ELEMENT_ARRAY_BUFFER_BINDING, (GLint *)&buffer);
data->ElementArrayBuffer = GetID(BufferRes(res.Context, buffer));
gl.glBindVertexArray(prevVAO);
}
return true;
}
bool GLResourceManager::Prepare_InitialState(GLResource res)
{
// this function needs to be refactored to better deal with multiple
// contexts and resources that are specific to a particular context
ResourceId Id = GetID(res);
const GLHookSet &gl = m_State < WRITING ? m_GL->GetHookset() : m_GL->GetInternalHookset();
if(res.Namespace == eResBuffer)
{
// get the length of the buffer
uint32_t length = 1;
gl.glGetNamedBufferParameterivEXT(res.name, eGL_BUFFER_SIZE, (GLint *)&length);
// save old bindings
GLuint oldbuf1 = 0, oldbuf2 = 0;
gl.glGetIntegerv(eGL_COPY_READ_BUFFER_BINDING, (GLint *)&oldbuf1);
gl.glGetIntegerv(eGL_COPY_WRITE_BUFFER_BINDING, (GLint *)&oldbuf2);
// create a new buffer big enough to hold the contents
GLuint buf = 0;
gl.glGenBuffers(1, &buf);
gl.glBindBuffer(eGL_COPY_WRITE_BUFFER, buf);
gl.glNamedBufferDataEXT(buf, (GLsizeiptr)length, NULL, eGL_STATIC_READ);
// bind the live buffer for copying
gl.glBindBuffer(eGL_COPY_READ_BUFFER, res.name);
// do the actual copy
gl.glCopyBufferSubData(eGL_COPY_READ_BUFFER, eGL_COPY_WRITE_BUFFER, 0, 0, (GLsizeiptr)length);
// restore old bindings
gl.glBindBuffer(eGL_COPY_READ_BUFFER, oldbuf1);
gl.glBindBuffer(eGL_COPY_WRITE_BUFFER, oldbuf2);
SetInitialContents(Id, InitialContentData(BufferRes(res.Context, buf), length, NULL));
}
else if(res.Namespace == eResProgram)
{
ScopedContext scope(m_pSerialiser, "Initial Contents", "Initial Contents", INITIAL_CONTENTS,
false);
m_pSerialiser->Serialise("Id", Id);
SerialiseProgramUniforms(gl, m_pSerialiser, res.name, NULL, true);
SetInitialChunk(Id, scope.Get());
}
else if(res.Namespace == eResTexture)
{
PrepareTextureInitialContents(Id, Id, res);
}
else if(res.Namespace == eResFramebuffer)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(FramebufferInitialData));
RDCEraseMem(data, sizeof(FramebufferInitialData));
SetInitialContents(Id, InitialContentData(GLResource(MakeNullResource), 0, data));
// if FBOs aren't shared we need to fetch the data for this FBO on the right context. It's
// not safe for us to go changing contexts ourselves (the context could be active on another
// thread), so instead we'll queue this up to fetch when we are on the correct context.
//
// Because we've already allocated and set the blob above, it can be filled in any time
// before serialising (end of the frame, and if the context is never used before the end of
// the frame the resource can't be used, so not fetching the initial state doesn't matter).
//
// Note we also need to detect the case where the context is already current on another thread
// and we just start getting commands there, but that case already isn't supported as we don't
// detect it and insert state-change chunks, we assume all commands will come from a single
// thread.
if(!VendorCheck[VendorCheck_EXT_fbo_shared] && res.Context && m_GL->GetCtx() != res.Context)
{
m_GL->QueuePrepareInitialState(res, data);
}
else
{
// call immediately, we are on the right context or for one reason or another the context
// doesn't matter for fetching this resource (res.Context is NULL or vendorcheck means they're
// shared).
Prepare_InitialState(res, (byte *)data);
}
}
else if(res.Namespace == eResFeedback)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(FeedbackInitialData));
RDCEraseMem(data, sizeof(FeedbackInitialData));
SetInitialContents(Id, InitialContentData(GLResource(MakeNullResource), 0, data));
// queue initial state fetching if we're not on the right context, see above in FBOs for more
// explanation of this.
if(res.Context && m_GL->GetCtx() != res.Context)
{
m_GL->QueuePrepareInitialState(res, data);
}
else
{
Prepare_InitialState(res, (byte *)data);
}
}
else if(res.Namespace == eResVertexArray)
{
byte *data = Serialiser::AllocAlignedBuffer(sizeof(VAOInitialData));
RDCEraseMem(data, sizeof(VAOInitialData));
SetInitialContents(Id, InitialContentData(GLResource(MakeNullResource), 0, data));
// queue initial state fetching if we're not on the right context, see above in FBOs for more
// explanation of this.
if(res.Context && m_GL->GetCtx() != res.Context)
{
m_GL->QueuePrepareInitialState(res, data);
}
else
{
Prepare_InitialState(res, (byte *)data);
}
}
else if(res.Namespace == eResRenderbuffer)
{
//
}
else
{
RDCERR("Unexpected type of resource requiring initial state");
}
return true;
}
void GLResourceManager::PrepareTextureInitialContents(ResourceId liveid, ResourceId origid,
GLResource res)
{
const GLHookSet &gl = m_State < WRITING ? m_GL->GetHookset() : m_GL->GetInternalHookset();
WrappedOpenGL::TextureData &details = m_GL->m_Textures[liveid];
TextureStateInitialData *state =
(TextureStateInitialData *)Serialiser::AllocAlignedBuffer(sizeof(TextureStateInitialData));
RDCEraseMem(state, sizeof(TextureStateInitialData));
if(details.internalFormat == eGL_NONE)
{
// textures can get here as GL_NONE if they were created and dirtied (by setting lots of
// texture parameters) without ever having storage allocated (via glTexStorage or glTexImage).
// in that case, just ignore as we won't bother with the initial states.
SetInitialContents(origid, InitialContentData(GLResource(MakeNullResource), 0, (byte *)state));
}
else if(details.curType != eGL_TEXTURE_BUFFER)
{
GLenum binding = TextureBinding(details.curType);
bool ms = (details.curType == eGL_TEXTURE_2D_MULTISAMPLE ||
details.curType == eGL_TEXTURE_2D_MULTISAMPLE_ARRAY);
state->depthMode = eGL_NONE;
if(IsDepthStencilFormat(details.internalFormat))
{
if(HasExt[ARB_stencil_texturing])
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_DEPTH_STENCIL_TEXTURE_MODE,
(GLint *)&state->depthMode);
else
state->depthMode = eGL_DEPTH_COMPONENT;
}
state->seamless = GL_FALSE;
if(details.curType == eGL_TEXTURE_CUBE_MAP || details.curType == eGL_TEXTURE_CUBE_MAP_ARRAY)
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_CUBE_MAP_SEAMLESS,
(GLint *)&state->seamless);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_BASE_LEVEL,
(GLint *)&state->baseLevel);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_MAX_LEVEL,
(GLint *)&state->maxLevel);
if(HasExt[ARB_texture_swizzle] || HasExt[EXT_texture_swizzle])
{
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_SWIZZLE_RGBA,
(GLint *)&state->swizzle[0]);
}
else
{
state->swizzle[0] = eGL_RED;
state->swizzle[1] = eGL_GREEN;
state->swizzle[2] = eGL_BLUE;
state->swizzle[3] = eGL_ALPHA;
}
// only non-ms textures have sampler state
if(!ms)
{
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_SRGB_DECODE_EXT,
(GLint *)&state->srgbDecode);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_COMPARE_FUNC,
(GLint *)&state->compareFunc);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_COMPARE_MODE,
(GLint *)&state->compareMode);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_MIN_FILTER,
(GLint *)&state->minFilter);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_MAG_FILTER,
(GLint *)&state->magFilter);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_WRAP_R,
(GLint *)&state->wrap[0]);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_WRAP_S,
(GLint *)&state->wrap[1]);
gl.glGetTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_WRAP_T,
(GLint *)&state->wrap[2]);
gl.glGetTextureParameterfvEXT(res.name, details.curType, eGL_TEXTURE_MIN_LOD, &state->minLod);
gl.glGetTextureParameterfvEXT(res.name, details.curType, eGL_TEXTURE_MAX_LOD, &state->maxLod);
gl.glGetTextureParameterfvEXT(res.name, details.curType, eGL_TEXTURE_BORDER_COLOR,
&state->border[0]);
gl.glGetTextureParameterfvEXT(res.name, details.curType, eGL_TEXTURE_LOD_BIAS, &state->lodBias);
// CLAMP isn't supported (border texels gone), assume they meant CLAMP_TO_EDGE
if(state->wrap[0] == eGL_CLAMP)
state->wrap[0] = eGL_CLAMP_TO_EDGE;
if(state->wrap[1] == eGL_CLAMP)
state->wrap[1] = eGL_CLAMP_TO_EDGE;
if(state->wrap[2] == eGL_CLAMP)
state->wrap[2] = eGL_CLAMP_TO_EDGE;
}
// we only copy contents for non-views
GLuint tex = 0;
if(!details.view)
{
{
GLuint oldtex = 0;
gl.glGetIntegerv(binding, (GLint *)&oldtex);
gl.glGenTextures(1, &tex);
gl.glBindTexture(details.curType, tex);
gl.glBindTexture(details.curType, oldtex);
}
int depth = details.depth;
if(details.curType != eGL_TEXTURE_3D)
depth = 1;
int mips =
GetNumMips(gl, details.curType, res.name, details.width, details.height, details.depth);
GLenum baseFormat = eGL_RGBA;
GLenum dataType = eGL_UNSIGNED_BYTE;
if(!IsCompressedFormat(details.internalFormat))
{
baseFormat = GetBaseFormat(details.internalFormat);
dataType = GetDataType(details.internalFormat);
}
// create texture of identical format/size to store initial contents
if(details.curType == eGL_TEXTURE_2D_MULTISAMPLE)
{
gl.glTextureStorage2DMultisampleEXT(tex, details.curType, details.samples,
details.internalFormat, details.width, details.height,
GL_TRUE);
mips = 1;
}
else if(details.curType == eGL_TEXTURE_2D_MULTISAMPLE_ARRAY)
{
gl.glTextureStorage3DMultisampleEXT(tex, details.curType, details.samples,
details.internalFormat, details.width, details.height,
details.depth, GL_TRUE);
mips = 1;
}
else if(details.dimension == 1)
{
gl.glTextureParameteriEXT(tex, details.curType, eGL_TEXTURE_MAX_LEVEL, mips - 1);
int w = details.width;
for(int i = 0; i < mips; i++)
{
gl.glTextureImage1DEXT(tex, details.curType, i, details.internalFormat, w, 0, baseFormat,
dataType, NULL);
w = RDCMAX(1, w >> 1);
}
}
else if(details.dimension == 2)
{
GLenum targets[] = {
eGL_TEXTURE_CUBE_MAP_POSITIVE_X, eGL_TEXTURE_CUBE_MAP_NEGATIVE_X,
eGL_TEXTURE_CUBE_MAP_POSITIVE_Y, eGL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
eGL_TEXTURE_CUBE_MAP_POSITIVE_Z, eGL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
};
gl.glTextureParameteriEXT(tex, details.curType, eGL_TEXTURE_MAX_LEVEL, mips - 1);
int w = details.width;
int h = details.height;
for(int i = 0; i < mips; i++)
{
if(details.curType != eGL_TEXTURE_CUBE_MAP)
{
gl.glTextureImage2DEXT(tex, details.curType, i, details.internalFormat, w, h, 0,
baseFormat, dataType, NULL);
}
else
{
for(size_t t = 0; t < ARRAY_COUNT(targets); t++)
gl.glTextureImage2DEXT(tex, targets[t], i, details.internalFormat, w, h, 0,
baseFormat, dataType, NULL);
}
w = RDCMAX(1, w >> 1);
if(details.curType != eGL_TEXTURE_1D_ARRAY)
h = RDCMAX(1, h >> 1);
}
}
else if(details.dimension == 3)
{
gl.glTextureParameteriEXT(tex, details.curType, eGL_TEXTURE_MAX_LEVEL, mips - 1);
int w = details.width;
int h = details.height;
int d = details.depth;
for(int i = 0; i < mips; i++)
{
gl.glTextureImage3DEXT(tex, details.curType, i, details.internalFormat, w, h, d, 0,
baseFormat, dataType, NULL);
w = RDCMAX(1, w >> 1);
h = RDCMAX(1, h >> 1);
if(details.curType == eGL_TEXTURE_3D)
d = RDCMAX(1, d >> 1);
}
}
// we need to set maxlevel appropriately for number of mips to force the texture to be
// complete.
// This can happen if e.g. a texture is initialised just by default with glTexImage for level
// 0 and used as a framebuffer attachment, then the implementation is fine with it.
// Unfortunately glCopyImageSubData requires completeness across all mips, a stricter
// requirement :(.
// We set max_level to mips - 1 (so mips=1 means MAX_LEVEL=0). Then restore it to the 'real'
// value we fetched above
int maxlevel = mips - 1;
gl.glTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_MAX_LEVEL,
(GLint *)&maxlevel);
bool iscomp = IsCompressedFormat(details.internalFormat);
bool avoidCopySubImage = false;
if(iscomp && VendorCheck[VendorCheck_AMD_copy_compressed_tinymips])
avoidCopySubImage = true;
if(iscomp && details.curType == eGL_TEXTURE_CUBE_MAP &&
VendorCheck[VendorCheck_AMD_copy_compressed_cubemaps])
avoidCopySubImage = true;
GLint packParams[8] = {0};
GLint unpackParams[8] = {0};
GLuint pixelPackBuffer = 0;
GLuint pixelUnpackBuffer = 0;
if(avoidCopySubImage)
{
gl.glGetIntegerv(eGL_PACK_SWAP_BYTES, &packParams[0]);
gl.glGetIntegerv(eGL_PACK_LSB_FIRST, &packParams[1]);
gl.glGetIntegerv(eGL_PACK_ROW_LENGTH, &packParams[2]);
gl.glGetIntegerv(eGL_PACK_IMAGE_HEIGHT, &packParams[3]);
gl.glGetIntegerv(eGL_PACK_SKIP_PIXELS, &packParams[4]);
gl.glGetIntegerv(eGL_PACK_SKIP_ROWS, &packParams[5]);
gl.glGetIntegerv(eGL_PACK_SKIP_IMAGES, &packParams[6]);
gl.glGetIntegerv(eGL_PACK_ALIGNMENT, &packParams[7]);
gl.glPixelStorei(eGL_PACK_SWAP_BYTES, 0);
gl.glPixelStorei(eGL_PACK_LSB_FIRST, 0);
gl.glPixelStorei(eGL_PACK_ROW_LENGTH, 0);
gl.glPixelStorei(eGL_PACK_IMAGE_HEIGHT, 0);
gl.glPixelStorei(eGL_PACK_SKIP_PIXELS, 0);
gl.glPixelStorei(eGL_PACK_SKIP_ROWS, 0);
gl.glPixelStorei(eGL_PACK_SKIP_IMAGES, 0);
gl.glPixelStorei(eGL_PACK_ALIGNMENT, 1);
gl.glGetIntegerv(eGL_UNPACK_SWAP_BYTES, &unpackParams[0]);
gl.glGetIntegerv(eGL_UNPACK_LSB_FIRST, &unpackParams[1]);
gl.glGetIntegerv(eGL_UNPACK_ROW_LENGTH, &unpackParams[2]);
gl.glGetIntegerv(eGL_UNPACK_IMAGE_HEIGHT, &unpackParams[3]);
gl.glGetIntegerv(eGL_UNPACK_SKIP_PIXELS, &unpackParams[4]);
gl.glGetIntegerv(eGL_UNPACK_SKIP_ROWS, &unpackParams[5]);
gl.glGetIntegerv(eGL_UNPACK_SKIP_IMAGES, &unpackParams[6]);
gl.glGetIntegerv(eGL_UNPACK_ALIGNMENT, &unpackParams[7]);
gl.glPixelStorei(eGL_UNPACK_SWAP_BYTES, 0);
gl.glPixelStorei(eGL_UNPACK_LSB_FIRST, 0);
gl.glPixelStorei(eGL_UNPACK_ROW_LENGTH, 0);
gl.glPixelStorei(eGL_UNPACK_IMAGE_HEIGHT, 0);
gl.glPixelStorei(eGL_UNPACK_SKIP_PIXELS, 0);
gl.glPixelStorei(eGL_UNPACK_SKIP_ROWS, 0);
gl.glPixelStorei(eGL_UNPACK_SKIP_IMAGES, 0);
gl.glPixelStorei(eGL_UNPACK_ALIGNMENT, 1);
gl.glGetIntegerv(eGL_PIXEL_PACK_BUFFER_BINDING, (GLint *)&pixelPackBuffer);
gl.glGetIntegerv(eGL_PIXEL_UNPACK_BUFFER_BINDING, (GLint *)&pixelUnpackBuffer);
gl.glBindBuffer(eGL_PIXEL_PACK_BUFFER, 0);
gl.glBindBuffer(eGL_PIXEL_UNPACK_BUFFER, 0);
}
// copy over mips
for(int i = 0; i < mips; i++)
{
int w = RDCMAX(details.width >> i, 1);
int h = RDCMAX(details.height >> i, 1);
int d = RDCMAX(details.depth >> i, 1);
if(details.curType == eGL_TEXTURE_CUBE_MAP)
d *= 6;
else if(details.curType == eGL_TEXTURE_CUBE_MAP_ARRAY ||
details.curType == eGL_TEXTURE_1D_ARRAY || details.curType == eGL_TEXTURE_2D_ARRAY)
d = details.depth;
// AMD throws an error copying mips that are smaller than the block size in one dimension,
// so do copy via CPU instead (will be slow, potentially we could optimise this if there's a
// different GPU-side image copy routine that works on these dimensions. Hopefully there'll
// only be a couple of such mips).
// AMD also has issues copying cubemaps
if((iscomp && VendorCheck[VendorCheck_AMD_copy_compressed_tinymips] && (w < 4 || h < 4)) ||
(iscomp && VendorCheck[VendorCheck_AMD_copy_compressed_cubemaps] &&
details.curType == eGL_TEXTURE_CUBE_MAP))
{
GLenum targets[] = {
eGL_TEXTURE_CUBE_MAP_POSITIVE_X, eGL_TEXTURE_CUBE_MAP_NEGATIVE_X,
eGL_TEXTURE_CUBE_MAP_POSITIVE_Y, eGL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
eGL_TEXTURE_CUBE_MAP_POSITIVE_Z, eGL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
};
int count = ARRAY_COUNT(targets);
if(details.curType != eGL_TEXTURE_CUBE_MAP)
{
targets[0] = details.curType;
count = 1;
}
for(int trg = 0; trg < count; trg++)
{
size_t size = GetCompressedByteSize(w, h, d, details.internalFormat, i);
byte *buf = new byte[size];
// read to CPU
gl.glGetCompressedTextureImageEXT(res.name, targets[trg], i, buf);
// write to GPU
if(details.dimension == 1)
gl.glCompressedTextureSubImage1DEXT(tex, targets[trg], i, 0, w,
details.internalFormat, (GLsizei)size, buf);
else if(details.dimension == 2)
gl.glCompressedTextureSubImage2DEXT(tex, targets[trg], i, 0, 0, w, h,
details.internalFormat, (GLsizei)size, buf);
else if(details.dimension == 3)
gl.glCompressedTextureSubImage3DEXT(tex, targets[trg], i, 0, 0, 0, w, h, d,
details.internalFormat, (GLsizei)size, buf);
delete[] buf;
}
}
else
{
// it seems like everything explodes if I do glCopyImageSubData on a D32F_S8 texture -
// in-program the overlay gets corrupted as one UBO seems to not provide data anymore
// until it's "refreshed". It seems like a driver bug, nvidia specific. In most cases a
// program isn't going to rely on the contents of a depth-stencil buffer (shadow maps that
// it might require would be depth-only formatted).
if(details.internalFormat == eGL_DEPTH32F_STENCIL8 &&
VendorCheck[VendorCheck_NV_avoid_D32S8_copy])
RDCDEBUG("Not fetching initial contents of D32F_S8 texture");
else
gl.glCopyImageSubData(res.name, details.curType, i, 0, 0, 0, tex, details.curType, i, 0,
0, 0, w, h, d);
}
}
if(avoidCopySubImage)
{
gl.glPixelStorei(eGL_PACK_SWAP_BYTES, packParams[0]);
gl.glPixelStorei(eGL_PACK_LSB_FIRST, packParams[1]);
gl.glPixelStorei(eGL_PACK_ROW_LENGTH, packParams[2]);
gl.glPixelStorei(eGL_PACK_IMAGE_HEIGHT, packParams[3]);
gl.glPixelStorei(eGL_PACK_SKIP_PIXELS, packParams[4]);
gl.glPixelStorei(eGL_PACK_SKIP_ROWS, packParams[5]);
gl.glPixelStorei(eGL_PACK_SKIP_IMAGES, packParams[6]);
gl.glPixelStorei(eGL_PACK_ALIGNMENT, packParams[7]);
gl.glPixelStorei(eGL_UNPACK_SWAP_BYTES, unpackParams[0]);
gl.glPixelStorei(eGL_UNPACK_LSB_FIRST, unpackParams[1]);
gl.glPixelStorei(eGL_UNPACK_ROW_LENGTH, unpackParams[2]);
gl.glPixelStorei(eGL_UNPACK_IMAGE_HEIGHT, unpackParams[3]);
gl.glPixelStorei(eGL_UNPACK_SKIP_PIXELS, unpackParams[4]);
gl.glPixelStorei(eGL_UNPACK_SKIP_ROWS, unpackParams[5]);
gl.glPixelStorei(eGL_UNPACK_SKIP_IMAGES, unpackParams[6]);
gl.glPixelStorei(eGL_UNPACK_ALIGNMENT, unpackParams[7]);
gl.glBindBuffer(eGL_PIXEL_PACK_BUFFER, pixelPackBuffer);
gl.glBindBuffer(eGL_PIXEL_UNPACK_BUFFER, pixelUnpackBuffer);
}
gl.glTextureParameterivEXT(res.name, details.curType, eGL_TEXTURE_MAX_LEVEL,
(GLint *)&state->maxLevel);
}
SetInitialContents(origid, InitialContentData(TextureRes(res.Context, tex), 0, (byte *)state));
}
else
{
// record texbuffer only state
GLuint bufName = 0;
gl.glGetTextureLevelParameterivEXT(res.name, details.curType, 0,
eGL_TEXTURE_BUFFER_DATA_STORE_BINDING, (GLint *)&bufName);
state->texBuffer = GetID(BufferRes(res.Context, bufName));
gl.glGetTextureLevelParameterivEXT(res.name, details.curType, 0, eGL_TEXTURE_BUFFER_OFFSET,
(GLint *)&state->texBufOffs);
gl.glGetTextureLevelParameterivEXT(res.name, details.curType, 0, eGL_TEXTURE_BUFFER_SIZE,
(GLint *)&state->texBufSize);
SetInitialContents(origid, InitialContentData(GLResource(MakeNullResource), 0, (byte *)state));
}
}
bool GLResourceManager::Force_InitialState(GLResource res, bool prepare)
{
if(res.Namespace != eResBuffer && res.Namespace != eResTexture)
return false;
// don't need to force anything if we're already including all resources
if(RenderDoc::Inst().GetCaptureOptions().RefAllResources)
return false;
GLResourceRecord *record = GetResourceRecord(res);
// if we have some viewers, check to see if they were referenced but we weren't, and force our own
// initial state inclusion.
if(record && !record->viewTextures.empty())
{
// need to prepare all such resources, just in case for the worst case.
if(prepare)
return true;
// if this data resource was referenced already, just skip
if(m_FrameReferencedResources.find(record->GetResourceID()) != m_FrameReferencedResources.end())
return false;
// see if any of our viewers were referenced
for(auto it = record->viewTextures.begin(); it != record->viewTextures.end(); ++it)
{
// if so, return true to force our inclusion, for the benefit of the view
if(m_FrameReferencedResources.find(*it) != m_FrameReferencedResources.end())
{
RDCDEBUG("Forcing inclusion of %llu for %llu", record->GetResourceID(), *it);
return true;
}
}
}
return false;
}
bool GLResourceManager::Serialise_InitialState(ResourceId resid, GLResource res)
{
SERIALISE_ELEMENT(ResourceId, Id, GetID(res));
if(m_State < WRITING)
{
if(HasLiveResource(Id))
res = GetLiveResource(Id);
else
res = GLResource(MakeNullResource);