-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
GLHelpers.h
2936 lines (2457 loc) · 66.6 KB
/
GLHelpers.h
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
#pragma once
#include <exception>
#include <string>
#include <functional>
#include <vector>
#include <memory>
#include <unordered_map>
#include <algorithm>
#include "GLExecutionState.h"
#include "../GCM.h"
#include "../Common/TextureUtils.h"
#include "Emu/system_config.h"
#include "Utilities/geometry.h"
#include "util/logs.hpp"
#define GL_FRAGMENT_TEXTURES_START 0
#define GL_VERTEX_TEXTURES_START (GL_FRAGMENT_TEXTURES_START + 16)
#define GL_STENCIL_MIRRORS_START (GL_VERTEX_TEXTURES_START + 4)
#define GL_STREAM_BUFFER_START (GL_STENCIL_MIRRORS_START + 16)
#define GL_VERTEX_PARAMS_BIND_SLOT 0
#define GL_VERTEX_LAYOUT_BIND_SLOT 1
#define GL_VERTEX_CONSTANT_BUFFERS_BIND_SLOT 2
#define GL_FRAGMENT_CONSTANT_BUFFERS_BIND_SLOT 3
#define GL_FRAGMENT_STATE_BIND_SLOT 4
#define GL_FRAGMENT_TEXTURE_PARAMS_BIND_SLOT 5
#define GL_COMPUTE_BUFFER_SLOT(index) (index + 6)
inline static void _SelectTexture(int unit) { glActiveTexture(GL_TEXTURE0 + unit); }
namespace gl
{
#ifdef _DEBUG
struct __glcheck_impl_t
{
const char* file;
const char* function;
int line;
constexpr __glcheck_impl_t(const char* file, const char* function, int line)
: file(file)
, function(function)
, line(line)
{
}
~__glcheck_impl_t() noexcept(false)
{
if (GLenum err = glGetError())
{
std::string error;
switch (err)
{
case GL_INVALID_OPERATION: error = "invalid operation"; break;
case GL_INVALID_ENUM: error = "invalid enum"; break;
case GL_INVALID_VALUE: error = "invalid value"; break;
case GL_OUT_OF_MEMORY: error = "out of memory"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error = "invalid framebuffer operation"; break;
default: error = "unknown error"; break;
}
fmt::throw_exception("OpenGL error: %s\n(in file %s:%d, function %s)", error, file, line, function);
}
}
};
#define __glcheck ::gl::__glcheck_impl_t{ __FILE__, __FUNCTION__, __LINE__ },
#else
#define __glcheck
#endif
//Function call wrapped in ARB_DSA vs EXT_DSA compat check
#define DSA_CALL(func, texture_name, target, ...)\
if (::gl::get_driver_caps().ARB_dsa_supported)\
gl##func(texture_name, __VA_ARGS__);\
else\
gl##func##EXT(texture_name, target, __VA_ARGS__);
class capabilities;
class blitter;
void enable_debugging();
capabilities& get_driver_caps();
bool is_primitive_native(rsx::primitive_type in);
GLenum draw_mode(rsx::primitive_type in);
void set_primary_context_thread();
bool is_primary_context_thread();
// Texture helpers
std::array<GLenum, 4> apply_swizzle_remap(const std::array<GLenum, 4>& swizzle_remap, const std::pair<std::array<u8, 4>, std::array<u8, 4>>& decoded_remap);
class exception : public std::exception
{
protected:
std::string m_what;
public:
const char* what() const noexcept override
{
return m_what.c_str();
}
};
class capabilities
{
public:
bool EXT_dsa_supported = false;
bool ARB_dsa_supported = false;
bool ARB_buffer_storage_supported = false;
bool ARB_texture_buffer_supported = false;
bool ARB_shader_draw_parameters_supported = false;
bool ARB_depth_buffer_float_supported = false;
bool ARB_texture_barrier_supported = false;
bool NV_texture_barrier_supported = false;
bool NV_gpu_shader5_supported = false;
bool AMD_gpu_shader_half_float_supported = false;
bool ARB_compute_shader_supported = false;
bool initialized = false;
bool vendor_INTEL = false; // has broken GLSL compiler
bool vendor_AMD = false; // has broken ARB_multidraw
bool vendor_NVIDIA = false; // has NaN poisoning issues
bool vendor_MESA = false; // requires CLIENT_STORAGE bit set for streaming buffers
bool check(const std::string& ext_name, const char* test)
{
if (ext_name == test)
{
rsx_log.notice("Extension %s is supported", ext_name);
return true;
}
return false;
}
void initialize()
{
int find_count = 11;
int ext_count = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &ext_count);
for (int i = 0; i < ext_count; i++)
{
if (!find_count) break;
const std::string ext_name = reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i));
if (check(ext_name, "GL_ARB_shader_draw_parameters"))
{
ARB_shader_draw_parameters_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_EXT_direct_state_access"))
{
EXT_dsa_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_ARB_direct_state_access"))
{
ARB_dsa_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_ARB_buffer_storage"))
{
ARB_buffer_storage_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_ARB_texture_buffer_object"))
{
ARB_texture_buffer_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_ARB_depth_buffer_float"))
{
ARB_depth_buffer_float_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_ARB_texture_barrier"))
{
ARB_texture_barrier_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_NV_texture_barrier"))
{
NV_texture_barrier_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_NV_gpu_shader5"))
{
NV_gpu_shader5_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_AMD_gpu_shader_half_float"))
{
AMD_gpu_shader_half_float_supported = true;
find_count--;
continue;
}
if (check(ext_name, "GL_ARB_compute_shader"))
{
ARB_compute_shader_supported = true;
find_count--;
continue;
}
}
// Workaround for intel drivers which have terrible capability reporting
std::string vendor_string = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
if (!vendor_string.empty())
{
std::transform(vendor_string.begin(), vendor_string.end(), vendor_string.begin(), ::tolower);
}
else
{
rsx_log.error("Failed to get vendor string from driver. Are we missing a context?");
vendor_string = "intel"; //lowest acceptable value
}
if (vendor_string.find("intel") != umax)
{
int version_major = 0;
int version_minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &version_major);
glGetIntegerv(GL_MINOR_VERSION, &version_minor);
vendor_INTEL = true;
//Texture buffers moved into core at GL 3.3
if (version_major > 3 || (version_major == 3 && version_minor >= 3))
ARB_texture_buffer_supported = true;
//Check for expected library entry-points for some required functions
if (!ARB_buffer_storage_supported && glBufferStorage && glMapBufferRange)
ARB_buffer_storage_supported = true;
if (!ARB_dsa_supported && glGetTextureImage && glTextureBufferRange)
ARB_dsa_supported = true;
if (!EXT_dsa_supported && glGetTextureImageEXT && glTextureBufferRangeEXT)
EXT_dsa_supported = true;
}
else if (vendor_string.find("nvidia") != umax)
{
vendor_NVIDIA = true;
}
else if (vendor_string.find("x.org") != umax)
{
vendor_MESA = true;
}
#ifdef _WIN32
else if (vendor_string.find("amd") != umax || vendor_string.find("ati") != umax)
{
vendor_AMD = true;
}
#endif
initialized = true;
}
};
class fence
{
GLsync m_value = nullptr;
GLenum flags = GL_SYNC_FLUSH_COMMANDS_BIT;
bool signaled = false;
public:
fence() = default;
~fence() = default;
void create()
{
m_value = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
flags = GL_SYNC_FLUSH_COMMANDS_BIT;
}
void destroy()
{
glDeleteSync(m_value);
m_value = nullptr;
}
void reset()
{
if (m_value != nullptr)
destroy();
create();
}
bool is_empty()
{
return (m_value == nullptr);
}
bool check_signaled()
{
verify(HERE), m_value != nullptr;
if (signaled)
return true;
if (flags)
{
GLenum err = glClientWaitSync(m_value, flags, 0);
flags = 0;
if (!(err == GL_ALREADY_SIGNALED || err == GL_CONDITION_SATISFIED))
return false;
}
else
{
GLint status = GL_UNSIGNALED;
GLint tmp;
glGetSynciv(m_value, GL_SYNC_STATUS, 4, &tmp, &status);
if (status != GL_SIGNALED)
return false;
}
signaled = true;
return true;
}
bool wait_for_signal()
{
verify(HERE), m_value != nullptr;
if (signaled == GL_FALSE)
{
GLenum err = GL_WAIT_FAILED;
bool done = false;
while (!done)
{
if (flags)
{
err = glClientWaitSync(m_value, flags, 0);
flags = 0;
switch (err)
{
default:
rsx_log.error("gl::fence sync returned unknown error 0x%X", err);
[[fallthrough]];
case GL_ALREADY_SIGNALED:
case GL_CONDITION_SATISFIED:
done = true;
break;
case GL_TIMEOUT_EXPIRED:
continue;
}
}
else
{
GLint status = GL_UNSIGNALED;
GLint tmp;
glGetSynciv(m_value, GL_SYNC_STATUS, 4, &tmp, &status);
if (status == GL_SIGNALED)
break;
}
}
signaled = (err == GL_ALREADY_SIGNALED || err == GL_CONDITION_SATISFIED);
}
glDeleteSync(m_value);
m_value = nullptr;
return signaled;
}
void server_wait_sync() const
{
verify(HERE), m_value != nullptr;
glWaitSync(m_value, 0, GL_TIMEOUT_IGNORED);
}
};
template<typename Type, uint BindId, uint GetStateId>
class save_binding_state_base
{
GLint m_last_binding;
public:
save_binding_state_base(const Type& new_state) : save_binding_state_base()
{
new_state.bind();
}
save_binding_state_base()
{
glGetIntegerv(GetStateId, &m_last_binding);
}
~save_binding_state_base()
{
glBindBuffer(BindId, m_last_binding);
}
};
enum class filter
{
nearest = GL_NEAREST,
linear = GL_LINEAR
};
enum class min_filter
{
nearest = GL_NEAREST,
linear = GL_LINEAR,
nearest_mipmap_nearest = GL_NEAREST_MIPMAP_NEAREST,
nearest_mipmap_linear = GL_NEAREST_MIPMAP_LINEAR,
linear_mipmap_nearest = GL_LINEAR_MIPMAP_NEAREST,
linear_mipmap_linear = GL_LINEAR_MIPMAP_LINEAR
};
enum class buffers
{
none = 0,
color = GL_COLOR_BUFFER_BIT,
depth = GL_DEPTH_BUFFER_BIT,
stencil = GL_STENCIL_BUFFER_BIT,
color_depth = color | depth,
color_depth_stencil = color | depth | stencil,
color_stencil = color | stencil,
depth_stencil = depth | stencil
};
class pixel_pack_settings
{
bool m_swap_bytes = false;
bool m_lsb_first = false;
int m_row_length = 0;
int m_image_height = 0;
int m_skip_rows = 0;
int m_skip_pixels = 0;
int m_skip_images = 0;
int m_alignment = 4;
public:
void apply() const
{
glPixelStorei(GL_PACK_SWAP_BYTES, m_swap_bytes ? GL_TRUE : GL_FALSE);
glPixelStorei(GL_PACK_LSB_FIRST, m_lsb_first ? GL_TRUE : GL_FALSE);
glPixelStorei(GL_PACK_ROW_LENGTH, m_row_length);
glPixelStorei(GL_PACK_IMAGE_HEIGHT, m_image_height);
glPixelStorei(GL_PACK_SKIP_ROWS, m_skip_rows);
glPixelStorei(GL_PACK_SKIP_PIXELS, m_skip_pixels);
glPixelStorei(GL_PACK_SKIP_IMAGES, m_skip_images);
glPixelStorei(GL_PACK_ALIGNMENT, m_alignment);
}
pixel_pack_settings& swap_bytes(bool value = true)
{
m_swap_bytes = value;
return *this;
}
pixel_pack_settings& lsb_first(bool value = true)
{
m_lsb_first = value;
return *this;
}
pixel_pack_settings& row_length(int value)
{
m_row_length = value;
return *this;
}
pixel_pack_settings& image_height(int value)
{
m_image_height = value;
return *this;
}
pixel_pack_settings& skip_rows(int value)
{
m_skip_rows = value;
return *this;
}
pixel_pack_settings& skip_pixels(int value)
{
m_skip_pixels = value;
return *this;
}
pixel_pack_settings& skip_images(int value)
{
m_skip_images = value;
return *this;
}
pixel_pack_settings& alignment(int value)
{
m_alignment = value;
return *this;
}
};
class pixel_unpack_settings
{
bool m_swap_bytes = false;
bool m_lsb_first = false;
int m_row_length = 0;
int m_image_height = 0;
int m_skip_rows = 0;
int m_skip_pixels = 0;
int m_skip_images = 0;
int m_alignment = 4;
public:
void apply() const
{
glPixelStorei(GL_UNPACK_SWAP_BYTES, m_swap_bytes ? GL_TRUE : GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST, m_lsb_first ? GL_TRUE : GL_FALSE);
glPixelStorei(GL_UNPACK_ROW_LENGTH, m_row_length);
glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, m_image_height);
glPixelStorei(GL_UNPACK_SKIP_ROWS, m_skip_rows);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, m_skip_pixels);
glPixelStorei(GL_UNPACK_SKIP_IMAGES, m_skip_images);
glPixelStorei(GL_UNPACK_ALIGNMENT, m_alignment);
}
pixel_unpack_settings& swap_bytes(bool value = true)
{
m_swap_bytes = value;
return *this;
}
pixel_unpack_settings& lsb_first(bool value = true)
{
m_lsb_first = value;
return *this;
}
pixel_unpack_settings& row_length(int value)
{
m_row_length = value;
return *this;
}
pixel_unpack_settings& image_height(int value)
{
m_image_height = value;
return *this;
}
pixel_unpack_settings& skip_rows(int value)
{
m_skip_rows = value;
return *this;
}
pixel_unpack_settings& skip_pixels(int value)
{
m_skip_pixels = value;
return *this;
}
pixel_unpack_settings& skip_images(int value)
{
m_skip_images = value;
return *this;
}
pixel_unpack_settings& alignment(int value)
{
m_alignment = value;
return *this;
}
};
class vao;
class attrib_t;
class buffer_pointer
{
public:
enum class type
{
s8 = GL_BYTE,
u8 = GL_UNSIGNED_BYTE,
s16 = GL_SHORT,
u16 = GL_UNSIGNED_SHORT,
s32 = GL_INT,
u32 = GL_UNSIGNED_INT,
f16 = GL_HALF_FLOAT,
f32 = GL_FLOAT,
f64 = GL_DOUBLE,
fixed = GL_FIXED,
s32_2_10_10_10_rev = GL_INT_2_10_10_10_REV,
u32_2_10_10_10_rev = GL_UNSIGNED_INT_2_10_10_10_REV,
u32_10f_11f_11f_rev = GL_UNSIGNED_INT_10F_11F_11F_REV
};
private:
vao* m_vao;
u32 m_offset;
u32 m_stride;
u32 m_size = 4;
type m_type = type::f32;
bool m_normalize = false;
public:
buffer_pointer(vao* vao, u32 offset = 0, u32 stride = 0)
: m_vao(vao)
, m_offset(offset)
, m_stride(stride)
{
}
const class ::gl::vao& get_vao() const
{
return *m_vao;
}
class ::gl::vao& get_vao()
{
return *m_vao;
}
buffer_pointer& offset(u32 value)
{
m_offset = value;
return *this;
}
u32 offset() const
{
return m_offset;
}
buffer_pointer& stride(u32 value)
{
m_stride = value;
return *this;
}
u32 stride() const
{
return m_stride;
}
buffer_pointer& size(u32 value)
{
m_size = value;
return *this;
}
u32 size() const
{
return m_size;
}
buffer_pointer& set_type(type value)
{
m_type = value;
return *this;
}
type get_type() const
{
return m_type;
}
buffer_pointer& normalize(bool value)
{
m_normalize = value;
return *this;
}
bool normalize() const
{
return m_normalize;
}
buffer_pointer& operator >> (u32 value)
{
return stride(value);
}
buffer_pointer& config(type type_ = type::f32, u32 size_ = 4, bool normalize_ = false)
{
return set_type(type_).size(size_).normalize(normalize_);
}
};
class buffer
{
public:
enum class target
{
pixel_pack = GL_PIXEL_PACK_BUFFER,
pixel_unpack = GL_PIXEL_UNPACK_BUFFER,
array = GL_ARRAY_BUFFER,
element_array = GL_ELEMENT_ARRAY_BUFFER,
uniform = GL_UNIFORM_BUFFER,
texture = GL_TEXTURE_BUFFER,
ssbo = GL_SHADER_STORAGE_BUFFER
};
enum class access
{
read = GL_READ_ONLY,
write = GL_WRITE_ONLY,
read_write = GL_READ_WRITE
};
enum class memory_type
{
undefined = 0,
local = 1,
host_visible = 2
};
class save_binding_state
{
GLint m_last_binding;
GLenum m_target;
public:
save_binding_state(target target_, const buffer& new_state) : save_binding_state(target_)
{
new_state.bind(target_);
}
save_binding_state(target target_)
{
GLenum pname;
switch (target_)
{
case target::pixel_pack: pname = GL_PIXEL_PACK_BUFFER_BINDING; break;
case target::pixel_unpack: pname = GL_PIXEL_UNPACK_BUFFER_BINDING; break;
case target::array: pname = GL_ARRAY_BUFFER_BINDING; break;
case target::element_array: pname = GL_ELEMENT_ARRAY_BUFFER_BINDING; break;
case target::uniform: pname = GL_UNIFORM_BUFFER_BINDING; break;
case target::texture: pname = GL_TEXTURE_BUFFER_BINDING; break;
case target::ssbo: pname = GL_SHADER_STORAGE_BUFFER_BINDING; break;
}
glGetIntegerv(pname, &m_last_binding);
m_target = static_cast<GLenum>(target_);
}
~save_binding_state()
{
glBindBuffer(m_target, m_last_binding);
}
};
protected:
GLuint m_id = GL_NONE;
GLsizeiptr m_size = 0;
target m_target = target::array;
memory_type m_memory_type = memory_type::undefined;
void allocate(GLsizeiptr size, const void* data_, memory_type type, GLenum usage)
{
if (const auto& caps = get_driver_caps();
caps.ARB_buffer_storage_supported)
{
target target_ = current_target();
save_binding_state save(target_, *this);
GLenum flags = 0;
if (type == memory_type::host_visible)
{
switch (usage)
{
case GL_STREAM_DRAW:
case GL_STATIC_DRAW:
case GL_DYNAMIC_DRAW:
flags |= GL_MAP_WRITE_BIT;
break;
case GL_STREAM_READ:
case GL_STATIC_READ:
case GL_DYNAMIC_READ:
flags |= GL_MAP_READ_BIT;
break;
default:
fmt::throw_exception("Unsupported buffer usage 0x%x", usage);
}
}
if ((flags & GL_MAP_READ_BIT) && !caps.vendor_AMD)
{
// This flag stops NVIDIA from allocating read-only memory in VRAM.
// NOTE: On AMD, allocating client-side memory via CLIENT_STORAGE_BIT or
// making use of GL_AMD_pinned_memory brings everything down to a crawl.
// Afaict there is no reason for this; disabling pixel pack/unpack operations does not alleviate the problem.
// The driver seems to eventually figure out the optimal storage location by itself.
flags |= GL_CLIENT_STORAGE_BIT;
}
glBufferStorage(static_cast<GLenum>(target_), size, data_, flags);
m_size = size;
}
else
{
data(size, data_, usage);
}
m_memory_type = type;
}
public:
buffer() = default;
buffer(const buffer&) = delete;
buffer(GLuint id)
{
set_id(id);
}
~buffer()
{
if (created())
remove();
}
void recreate()
{
if (created())
{
remove();
}
create();
}
void recreate(GLsizeiptr size, const void* data = nullptr)
{
if (created())
{
remove();
}
create(size, data);
}
void create()
{
glGenBuffers(1, &m_id);
}
void create(GLsizeiptr size, const void* data_ = nullptr, memory_type type = memory_type::local, GLenum usage = GL_STREAM_DRAW)
{
create();
allocate(size, data_, type, usage);
}
void create(target target_, GLsizeiptr size, const void* data_ = nullptr, memory_type type = memory_type::local, GLenum usage = GL_STREAM_DRAW)
{
create();
m_target = target_;
allocate(size, data_, type, usage);
}
void bind(target target_) const
{
glBindBuffer(static_cast<GLenum>(target_), m_id);
}
void bind() const
{
bind(current_target());
}
target current_target() const
{
return m_target;
}
void remove()
{
glDeleteBuffers(1, &m_id);
m_id = 0;
}
GLsizeiptr size() const
{
return m_size;
}
uint id() const
{
return m_id;
}
void set_id(uint id)
{
m_id = id;
}
bool created() const
{
return m_id != 0;
}
explicit operator bool() const
{
return created();
}
void data(GLsizeiptr size, const void* data_ = nullptr, GLenum usage = GL_STREAM_DRAW)
{
verify(HERE), m_memory_type != memory_type::local;
target target_ = current_target();
save_binding_state save(target_, *this);
glBufferData(static_cast<GLenum>(target_), size, data_, usage);
m_size = size;
}
GLubyte* map(access access_)
{
verify(HERE), m_memory_type == memory_type::host_visible;
bind(current_target());
return reinterpret_cast<GLubyte*>(glMapBuffer(static_cast<GLenum>(current_target()), static_cast<GLenum>(access_)));
}
void unmap()
{
verify(HERE), m_memory_type == memory_type::host_visible;
glUnmapBuffer(static_cast<GLenum>(current_target()));
}
void bind_range(u32 index, u32 offset, u32 size) const
{
glBindBufferRange(static_cast<GLenum>(current_target()), index, id(), offset, size);
}
void bind_range(target target_, u32 index, u32 offset, u32 size) const
{
glBindBufferRange(static_cast<GLenum>(target_), index, id(), offset, size);
}
};
class ring_buffer : public buffer
{
protected:
u32 m_data_loc = 0;
void *m_memory_mapping = nullptr;
fence m_fence;
public:
virtual void recreate(GLsizeiptr size, const void* data = nullptr)
{
if (m_id)
{
m_fence.wait_for_signal();
remove();
}
buffer::create();
GLbitfield buffer_storage_flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
if (gl::get_driver_caps().vendor_MESA) buffer_storage_flags |= GL_CLIENT_STORAGE_BIT;
glBindBuffer(static_cast<GLenum>(m_target), m_id);
glBufferStorage(static_cast<GLenum>(m_target), size, data, buffer_storage_flags);
m_memory_mapping = glMapBufferRange(static_cast<GLenum>(m_target), 0, size, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
verify(HERE), m_memory_mapping != nullptr;
m_data_loc = 0;
m_size = ::narrow<u32>(size);
}
void create(target target_, GLsizeiptr size, const void* data_ = nullptr)
{
m_target = target_;
recreate(size, data_);
}
virtual std::pair<void*, u32> alloc_from_heap(u32 alloc_size, u16 alignment)
{
u32 offset = m_data_loc;
if (m_data_loc) offset = align(offset, alignment);