-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
GLGSRender.cpp
1247 lines (1033 loc) · 38.2 KB
/
GLGSRender.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
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
#include "stdafx.h"
#include "../Overlays/overlay_compile_notification.h"
#include "../Overlays/Shaders/shader_loading_dialog_native.h"
#include "GLGSRender.h"
#include "GLCompute.h"
#include "Emu/Memory/vm_locking.h"
#include "Emu/RSX/rsx_methods.h"
#include "Emu/RSX/NV47/HW/context_accessors.define.h"
[[noreturn]] extern void report_fatal_error(std::string_view _text, bool is_html = false, bool include_help_text = true);
namespace
{
void throw_fatal(const std::vector<std::string>& reasons)
{
const std::string delimiter = "\n- ";
const std::string reasons_list = fmt::merge(reasons, delimiter);
const std::string message = fmt::format(
"OpenGL could not be initialized on this system for the following reason(s):\n"
"\n"
"- %s",
reasons_list);
Emu.BlockingCallFromMainThread([message]()
{
report_fatal_error(message, false, false);
});
}
}
u64 GLGSRender::get_cycles()
{
return thread_ctrl::get_cycles(static_cast<named_thread<GLGSRender>&>(*this));
}
GLGSRender::GLGSRender(utils::serial* ar) noexcept : GSRender(ar)
{
m_shaders_cache = std::make_unique<gl::shader_cache>(m_prog_buffer, "opengl", "v1.94");
if (g_cfg.video.disable_vertex_cache)
m_vertex_cache = std::make_unique<gl::null_vertex_cache>();
else
m_vertex_cache = std::make_unique<gl::weak_vertex_cache>();
backend_config.supports_hw_a2c = false;
backend_config.supports_hw_a2one = false;
backend_config.supports_multidraw = true;
backend_config.supports_normalized_barycentrics = true;
}
extern CellGcmContextData current_context;
void GLGSRender::set_viewport()
{
// NOTE: scale offset matrix already contains the viewport transformation
const auto [clip_width, clip_height] = rsx::apply_resolution_scale<true>(
rsx::method_registers.surface_clip_width(), rsx::method_registers.surface_clip_height());
glViewport(0, 0, clip_width, clip_height);
}
void GLGSRender::set_scissor(bool clip_viewport)
{
areau scissor;
if (get_scissor(scissor, clip_viewport))
{
// NOTE: window origin does not affect scissor region (probably only affects viewport matrix; already applied)
// See LIMBO [NPUB-30373] which uses shader window origin = top
glScissor(scissor.x1, scissor.y1, scissor.width(), scissor.height());
gl_state.enable(GL_TRUE, GL_SCISSOR_TEST);
}
}
void GLGSRender::on_init_thread()
{
ensure(m_frame);
// NOTES: All contexts have to be created before any is bound to a thread
// This allows context sharing to work (both GLRCs passed to wglShareLists have to be idle or you get ERROR_BUSY)
m_context = m_frame->make_context();
const auto shadermode = g_cfg.video.shadermode.get();
if (shadermode != shader_mode::recompiler)
{
auto context_create_func = [m_frame = m_frame]()
{
return m_frame->make_context();
};
auto context_bind_func = [m_frame = m_frame](draw_context_t ctx)
{
m_frame->set_current(ctx);
};
auto context_destroy_func = [m_frame = m_frame](draw_context_t ctx)
{
m_frame->delete_context(ctx);
};
gl::initialize_pipe_compiler(context_create_func, context_bind_func, context_destroy_func, g_cfg.video.shader_compiler_threads_count);
}
else
{
auto null_context_create_func = []() -> draw_context_t
{
return nullptr;
};
gl::initialize_pipe_compiler(null_context_create_func, {}, {}, 1);
}
// Bind primary context to main RSX thread
m_frame->set_current(m_context);
gl::set_primary_context_thread();
zcull_ctrl.reset(static_cast<::rsx::reports::ZCULL_control*>(this));
m_occlusion_type = g_cfg.video.precise_zpass_count ? GL_SAMPLES_PASSED : GL_ANY_SAMPLES_PASSED;
gl::init();
gl::set_command_context(gl_state);
// Enable adaptive vsync if vsync is requested
gl::set_swapinterval(g_cfg.video.vsync ? -1 : 0);
if (g_cfg.video.debug_output)
gl::enable_debugging();
rsx_log.success("GL RENDERER: %s (%s)", reinterpret_cast<const char*>(glGetString(GL_RENDERER)), reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
rsx_log.success("GL VERSION: %s", reinterpret_cast<const char*>(glGetString(GL_VERSION)));
rsx_log.success("GLSL VERSION: %s", reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION)));
auto& gl_caps = gl::get_driver_caps();
std::vector<std::string> exception_reasons;
if (!gl_caps.ARB_texture_buffer_supported)
{
exception_reasons.push_back("GL_ARB_texture_buffer_object is required but not supported by your GPU");
}
if (!gl_caps.ARB_dsa_supported && !gl_caps.EXT_dsa_supported)
{
exception_reasons.push_back("GL_ARB_direct_state_access or GL_EXT_direct_state_access is required but not supported by your GPU");
}
if (!exception_reasons.empty())
{
throw_fatal(exception_reasons);
}
if (!gl_caps.ARB_depth_buffer_float_supported && g_cfg.video.force_high_precision_z_buffer)
{
rsx_log.warning("High precision Z buffer requested but your GPU does not support GL_ARB_depth_buffer_float. Option ignored.");
}
if (!gl_caps.ARB_texture_barrier_supported && !gl_caps.NV_texture_barrier_supported && !g_cfg.video.strict_rendering_mode)
{
rsx_log.warning("Texture barriers are not supported by your GPU. Feedback loops will have undefined results.");
}
if (!gl_caps.ARB_bindless_texture_supported)
{
switch (shadermode)
{
case shader_mode::async_with_interpreter:
case shader_mode::interpreter_only:
rsx_log.error("Bindless texture extension required for shader interpreter is not supported on your GPU. Will use async recompiler as a fallback.");
g_cfg.video.shadermode.set(shader_mode::async_recompiler);
break;
default:
break;
}
}
if (gl_caps.NV_fragment_shader_barycentric_supported &&
gl_caps.vendor_NVIDIA &&
g_cfg.video.shader_precision != gpu_preset_level::low)
{
// NVIDIA's attribute interpolation requires some workarounds
backend_config.supports_normalized_barycentrics = false;
}
// Use industry standard resource alignment values as defaults
m_uniform_buffer_offset_align = 256;
m_min_texbuffer_alignment = 256;
m_max_texbuffer_size = 0;
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &m_uniform_buffer_offset_align);
glGetIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT, &m_min_texbuffer_alignment);
glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &m_max_texbuffer_size);
m_vao.create();
// Set min alignment to 16-bytes for SSE optimizations with aligned addresses to work
m_min_texbuffer_alignment = std::max(m_min_texbuffer_alignment, 16);
m_uniform_buffer_offset_align = std::max(m_uniform_buffer_offset_align, 16);
rsx_log.notice("Supported texel buffer size reported: %d bytes", m_max_texbuffer_size);
if (m_max_texbuffer_size < (16 * 0x100000))
{
rsx_log.error("Max texture buffer size supported is less than 16M which is useless. Expect undefined behaviour.");
m_max_texbuffer_size = (16 * 0x100000);
}
// Array stream buffer
{
m_gl_persistent_stream_buffer = std::make_unique<gl::texture>(GL_TEXTURE_BUFFER, 0, 0, 0, 0, GL_R8UI);
gl_state.bind_texture(GL_STREAM_BUFFER_START + 0, GL_TEXTURE_BUFFER, m_gl_persistent_stream_buffer->id());
}
// Register stream buffer
{
m_gl_volatile_stream_buffer = std::make_unique<gl::texture>(GL_TEXTURE_BUFFER, 0, 0, 0, 0, GL_R8UI);
gl_state.bind_texture(GL_STREAM_BUFFER_START + 1, GL_TEXTURE_BUFFER, m_gl_volatile_stream_buffer->id());
}
// Fallback null texture instead of relying on texture0
{
std::array<u32, 8> pixeldata = { 0, 0, 0, 0, 0, 0, 0, 0 };
// 1D
auto tex1D = std::make_unique<gl::texture>(GL_TEXTURE_1D, 1, 1, 1, 1, GL_RGBA8);
tex1D->copy_from(pixeldata.data(), gl::texture::format::rgba, gl::texture::type::uint_8_8_8_8, {});
// 2D
auto tex2D = std::make_unique<gl::texture>(GL_TEXTURE_2D, 1, 1, 1, 1, GL_RGBA8);
tex2D->copy_from(pixeldata.data(), gl::texture::format::rgba, gl::texture::type::uint_8_8_8_8, {});
// 3D
auto tex3D = std::make_unique<gl::texture>(GL_TEXTURE_3D, 1, 1, 1, 1, GL_RGBA8);
tex3D->copy_from(pixeldata.data(), gl::texture::format::rgba, gl::texture::type::uint_8_8_8_8, {});
// CUBE
auto texCUBE = std::make_unique<gl::texture>(GL_TEXTURE_CUBE_MAP, 1, 1, 1, 1, GL_RGBA8);
texCUBE->copy_from(pixeldata.data(), gl::texture::format::rgba, gl::texture::type::uint_8_8_8_8, {});
m_null_textures[GL_TEXTURE_1D] = std::move(tex1D);
m_null_textures[GL_TEXTURE_2D] = std::move(tex2D);
m_null_textures[GL_TEXTURE_3D] = std::move(tex3D);
m_null_textures[GL_TEXTURE_CUBE_MAP] = std::move(texCUBE);
}
if (!gl_caps.ARB_buffer_storage_supported)
{
rsx_log.warning("Forcing use of legacy OpenGL buffers because ARB_buffer_storage is not supported");
// TODO: do not modify config options
g_cfg.video.renderdoc_compatiblity.from_string("true");
}
if (g_cfg.video.renderdoc_compatiblity)
{
rsx_log.warning("Using legacy openGL buffers.");
manually_flush_ring_buffers = true;
m_attrib_ring_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_transform_constants_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_fragment_constants_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_fragment_env_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_vertex_env_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_texture_parameters_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_vertex_layout_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_index_ring_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_vertex_instructions_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_fragment_instructions_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_raster_env_ring_buffer = std::make_unique<gl::legacy_ring_buffer>();
m_scratch_ring_buffer = std::make_unique<gl::legacy_ring_buffer>();
}
else
{
m_attrib_ring_buffer = std::make_unique<gl::ring_buffer>();
m_transform_constants_buffer = std::make_unique<gl::ring_buffer>();
m_fragment_constants_buffer = std::make_unique<gl::ring_buffer>();
m_fragment_env_buffer = std::make_unique<gl::ring_buffer>();
m_vertex_env_buffer = std::make_unique<gl::ring_buffer>();
m_texture_parameters_buffer = std::make_unique<gl::ring_buffer>();
m_vertex_layout_buffer = std::make_unique<gl::ring_buffer>();
m_index_ring_buffer = gl_caps.vendor_AMD ? std::make_unique<gl::transient_ring_buffer>() : std::make_unique<gl::ring_buffer>();
m_vertex_instructions_buffer = std::make_unique<gl::ring_buffer>();
m_fragment_instructions_buffer = std::make_unique<gl::ring_buffer>();
m_raster_env_ring_buffer = std::make_unique<gl::ring_buffer>();
m_scratch_ring_buffer = std::make_unique<gl::ring_buffer>();
}
m_attrib_ring_buffer->create(gl::buffer::target::texture, 256 * 0x100000);
m_index_ring_buffer->create(gl::buffer::target::element_array, 16 * 0x100000);
m_transform_constants_buffer->create(gl::buffer::target::uniform, 64 * 0x100000);
m_fragment_constants_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
m_fragment_env_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
m_vertex_env_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
m_texture_parameters_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
m_vertex_layout_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
m_raster_env_ring_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
m_scratch_ring_buffer->create(gl::buffer::target::uniform, 16 * 0x100000);
if (shadermode == shader_mode::async_with_interpreter || shadermode == shader_mode::interpreter_only)
{
m_vertex_instructions_buffer->create(gl::buffer::target::ssbo, 16 * 0x100000);
m_fragment_instructions_buffer->create(gl::buffer::target::ssbo, 16 * 0x100000);
m_shader_interpreter.create();
}
if (gl_caps.vendor_AMD)
{
// Initialize with 256k identity entries
std::vector<u32> dst(256 * 1024);
for (u32 n = 0; n < (0x100000 >> 2); ++n)
{
dst[n] = n;
}
m_identity_index_buffer = std::make_unique<gl::buffer>();
m_identity_index_buffer->create(gl::buffer::target::element_array,dst.size() * sizeof(u32), dst.data(), gl::buffer::memory_type::local);
}
else if (gl_caps.vendor_NVIDIA)
{
// NOTE: On NVIDIA cards going back decades (including the PS3) there is a slight normalization inaccuracy in compressed formats.
// Confirmed in BLES01916 (The Evil Within) which uses RGB565 for some virtual texturing data.
backend_config.supports_hw_renormalization = true;
}
m_persistent_stream_view.update(m_attrib_ring_buffer.get(), 0, std::min<u32>(static_cast<u32>(m_attrib_ring_buffer->size()), m_max_texbuffer_size));
m_volatile_stream_view.update(m_attrib_ring_buffer.get(), 0, std::min<u32>(static_cast<u32>(m_attrib_ring_buffer->size()), m_max_texbuffer_size));
m_gl_persistent_stream_buffer->copy_from(m_persistent_stream_view);
m_gl_volatile_stream_buffer->copy_from(m_volatile_stream_view);
m_vao.element_array_buffer = *m_index_ring_buffer;
int image_unit = 0;
for (auto &sampler : m_fs_sampler_states)
{
sampler.create();
sampler.bind(image_unit++);
}
for (auto &sampler : m_vs_sampler_states)
{
sampler.create();
sampler.bind(image_unit++);
}
for (auto& sampler : m_fs_sampler_mirror_states)
{
sampler.create();
sampler.apply_defaults();
sampler.bind(image_unit++);
}
//Occlusion query
for (u32 i = 0; i < rsx::reports::occlusion_query_count; ++i)
{
GLuint handle = 0;
auto &query = m_occlusion_query_data[i];
glGenQueries(1, &handle);
query.driver_handle = handle;
query.pending = false;
query.active = false;
query.result = 0;
}
m_ui_renderer.create();
m_video_output_pass.create();
m_gl_texture_cache.initialize();
m_prog_buffer.initialize
(
[this](void* const& props, const RSXVertexProgram& vp, const RSXFragmentProgram& fp)
{
// Program was linked or queued for linking
m_shaders_cache->store(props, vp, fp);
}
);
if (!m_overlay_manager)
{
m_frame->hide();
m_shaders_cache->load(nullptr);
m_frame->show();
}
else
{
rsx::shader_loading_dialog_native dlg(this);
m_shaders_cache->load(&dlg);
}
}
void GLGSRender::on_exit()
{
// Destroy internal RSX state, may call upon this->do_local_task
GSRender::on_exit();
// Globals
// TODO: Move these
gl::destroy_compute_tasks();
gl::destroy_overlay_passes();
gl::destroy_global_texture_resources();
gl::debug::g_vis_texture.reset(); // TODO
gl::destroy_pipe_compiler();
m_prog_buffer.clear();
m_rtts.destroy();
for (auto &fbo : m_framebuffer_cache)
{
fbo.remove();
}
m_framebuffer_cache.clear();
m_upscaler.reset();
for (auto& flip_tex_image : m_flip_tex_color)
{
flip_tex_image.reset();
}
if (m_vao)
{
m_vao.remove();
}
m_gl_persistent_stream_buffer.reset();
m_gl_volatile_stream_buffer.reset();
for (auto &sampler : m_fs_sampler_states)
{
sampler.remove();
}
for (auto &sampler : m_fs_sampler_mirror_states)
{
sampler.remove();
}
for (auto &sampler : m_vs_sampler_states)
{
sampler.remove();
}
if (m_attrib_ring_buffer)
{
m_attrib_ring_buffer->remove();
}
if (m_transform_constants_buffer)
{
m_transform_constants_buffer->remove();
}
if (m_fragment_constants_buffer)
{
m_fragment_constants_buffer->remove();
}
if (m_fragment_env_buffer)
{
m_fragment_env_buffer->remove();
}
if (m_vertex_env_buffer)
{
m_vertex_env_buffer->remove();
}
if (m_texture_parameters_buffer)
{
m_texture_parameters_buffer->remove();
}
if (m_vertex_layout_buffer)
{
m_vertex_layout_buffer->remove();
}
if (m_index_ring_buffer)
{
m_index_ring_buffer->remove();
}
if (m_identity_index_buffer)
{
m_identity_index_buffer->remove();
}
if (m_vertex_instructions_buffer)
{
m_vertex_instructions_buffer->remove();
}
if (m_fragment_instructions_buffer)
{
m_fragment_instructions_buffer->remove();
}
if (m_raster_env_ring_buffer)
{
m_raster_env_ring_buffer->remove();
}
if (m_scratch_ring_buffer)
{
m_scratch_ring_buffer->remove();
}
m_null_textures.clear();
m_gl_texture_cache.destroy();
m_ui_renderer.destroy();
m_video_output_pass.destroy();
m_shader_interpreter.destroy();
for (u32 i = 0; i < rsx::reports::occlusion_query_count; ++i)
{
auto &query = m_occlusion_query_data[i];
query.active = false;
query.pending = false;
GLuint handle = query.driver_handle;
glDeleteQueries(1, &handle);
query.driver_handle = 0;
}
zcull_ctrl.release();
gl::set_primary_context_thread(false);
}
void GLGSRender::clear_surface(u32 arg)
{
if (skip_current_frame) return;
// If stencil write mask is disabled, remove clear_stencil bit
if (!rsx::method_registers.stencil_mask()) arg &= ~RSX_GCM_CLEAR_STENCIL_BIT;
// Ignore invalid clear flags
if ((arg & RSX_GCM_CLEAR_ANY_MASK) == 0) return;
u8 ctx = rsx::framebuffer_creation_context::context_draw;
if (arg & RSX_GCM_CLEAR_COLOR_RGBA_MASK) ctx |= rsx::framebuffer_creation_context::context_clear_color;
if (arg & RSX_GCM_CLEAR_DEPTH_STENCIL_MASK) ctx |= rsx::framebuffer_creation_context::context_clear_depth;
init_buffers(static_cast<rsx::framebuffer_creation_context>(ctx), true);
if (!m_graphics_state.test(rsx::rtt_config_valid)) return;
gl::clear_cmd_info clear_cmd{};
gl::command_context cmd{ gl_state };
const bool full_frame =
rsx::method_registers.scissor_origin_x() == 0 &&
rsx::method_registers.scissor_origin_y() == 0 &&
rsx::method_registers.scissor_width() >= rsx::method_registers.surface_clip_width() &&
rsx::method_registers.scissor_height() >= rsx::method_registers.surface_clip_height();
bool update_color = false, update_z = false;
rsx::surface_depth_format2 surface_depth_format = rsx::method_registers.surface_depth_fmt();
if (auto ds = std::get<1>(m_rtts.m_bound_depth_stencil); arg & RSX_GCM_CLEAR_DEPTH_STENCIL_MASK)
{
if (arg & RSX_GCM_CLEAR_DEPTH_BIT)
{
u32 max_depth_value = get_max_depth_value(surface_depth_format);
u32 clear_depth = rsx::method_registers.z_clear_value(is_depth_stencil_format(surface_depth_format));
clear_cmd.clear_depth.value = f32(clear_depth) / max_depth_value;
clear_cmd.aspect_mask |= gl::image_aspect::depth;
}
if (is_depth_stencil_format(surface_depth_format))
{
if (arg & RSX_GCM_CLEAR_STENCIL_BIT)
{
clear_cmd.clear_stencil.mask = rsx::method_registers.stencil_mask();
clear_cmd.clear_stencil.value = rsx::method_registers.stencil_clear_value();
clear_cmd.aspect_mask |= gl::image_aspect::stencil;
}
if (const auto ds_mask = (arg & RSX_GCM_CLEAR_DEPTH_STENCIL_MASK);
ds_mask != RSX_GCM_CLEAR_DEPTH_STENCIL_MASK || !full_frame)
{
ensure(clear_cmd.aspect_mask);
if (ds->state_flags & rsx::surface_state_flags::erase_bkgnd && // Needs initialization
ds->old_contents.empty() && !g_cfg.video.read_depth_buffer) // No way to load data from memory, so no initialization given
{
// Only one aspect was cleared. Make sure to memory initialize the other before removing dirty flag
if (ds_mask == RSX_GCM_CLEAR_DEPTH_BIT)
{
// Depth was cleared, initialize stencil
clear_cmd.clear_stencil.mask = 0xff;
clear_cmd.clear_stencil.value = 0xff;
clear_cmd.aspect_mask |= gl::image_aspect::stencil;
}
else if (ds_mask == RSX_GCM_CLEAR_STENCIL_BIT)
{
// Stencil was cleared, initialize depth
clear_cmd.clear_depth.value = 1.f;
clear_cmd.aspect_mask |= gl::image_aspect::depth;
}
}
else
{
ds->write_barrier(cmd);
}
}
}
if (clear_cmd.aspect_mask)
{
// Memory has been initialized
update_z = true;
}
}
if (auto colormask = (arg & 0xf0))
{
u8 clear_a = rsx::method_registers.clear_color_a();
u8 clear_r = rsx::method_registers.clear_color_r();
u8 clear_g = rsx::method_registers.clear_color_g();
u8 clear_b = rsx::method_registers.clear_color_b();
switch (rsx::method_registers.surface_color())
{
case rsx::surface_color_format::x32:
case rsx::surface_color_format::w16z16y16x16:
case rsx::surface_color_format::w32z32y32x32:
{
// Nop
colormask = 0;
break;
}
case rsx::surface_color_format::b8:
{
rsx::get_b8_clear_color(clear_r, clear_g, clear_b, clear_a);
colormask = rsx::get_b8_clearmask(colormask);
break;
}
case rsx::surface_color_format::g8b8:
{
rsx::get_g8b8_clear_color(clear_r, clear_g, clear_b, clear_a);
colormask = rsx::get_g8b8_r8g8_clearmask(colormask);
break;
}
case rsx::surface_color_format::r5g6b5:
{
rsx::get_rgb565_clear_color(clear_r, clear_g, clear_b, clear_a);
break;
}
case rsx::surface_color_format::x1r5g5b5_o1r5g5b5:
{
rsx::get_a1rgb555_clear_color(clear_r, clear_g, clear_b, clear_a, 255);
break;
}
case rsx::surface_color_format::x1r5g5b5_z1r5g5b5:
{
rsx::get_a1rgb555_clear_color(clear_r, clear_g, clear_b, clear_a, 0);
break;
}
case rsx::surface_color_format::a8b8g8r8:
case rsx::surface_color_format::x8b8g8r8_o8b8g8r8:
case rsx::surface_color_format::x8b8g8r8_z8b8g8r8:
{
rsx::get_abgr8_clear_color(clear_r, clear_g, clear_b, clear_a);
colormask = rsx::get_abgr8_clearmask(colormask);
break;
}
default:
{
break;
}
}
if (colormask)
{
clear_cmd.clear_color.mask = colormask;
clear_cmd.clear_color.attachment_count = static_cast<u8>(m_rtts.m_bound_render_target_ids.size());
clear_cmd.clear_color.r = clear_r;
clear_cmd.clear_color.g = clear_g;
clear_cmd.clear_color.b = clear_b;
clear_cmd.clear_color.a = clear_a;
clear_cmd.aspect_mask |= gl::image_aspect::color;
if (!full_frame)
{
for (const auto& index : m_rtts.m_bound_render_target_ids)
{
m_rtts.m_bound_render_targets[index].second->write_barrier(cmd);
}
}
update_color = true;
}
}
if (update_color || update_z)
{
m_rtts.on_write({ update_color, update_color, update_color, update_color }, update_z);
}
if (!full_frame)
{
gl_state.enable(GL_SCISSOR_TEST);
}
gl::clear_attachments(cmd, clear_cmd);
}
bool GLGSRender::load_program()
{
const auto shadermode = g_cfg.video.shadermode.get();
if (m_graphics_state & rsx::pipeline_state::invalidate_pipeline_bits)
{
get_current_fragment_program(fs_sampler_state);
ensure(current_fragment_program.valid);
get_current_vertex_program(vs_sampler_state);
}
else if (m_program)
{
if (!m_shader_interpreter.is_interpreter(m_program)) [[likely]]
{
return true;
}
if (shadermode == shader_mode::interpreter_only)
{
m_program = m_shader_interpreter.get(current_fp_metadata);
return true;
}
}
const bool was_interpreter = m_shader_interpreter.is_interpreter(m_program);
m_vertex_prog = nullptr;
m_fragment_prog = nullptr;
if (shadermode != shader_mode::interpreter_only) [[likely]]
{
void* pipeline_properties = nullptr;
std::tie(m_program, m_vertex_prog, m_fragment_prog) = m_prog_buffer.get_graphics_pipeline(current_vertex_program, current_fragment_program, pipeline_properties,
shadermode != shader_mode::recompiler, true);
if (m_prog_buffer.check_cache_missed())
{
// Notify the user with HUD notification
if (g_cfg.misc.show_shader_compilation_hint)
{
rsx::overlays::show_shader_compile_notification();
}
}
else
{
ensure(m_program);
m_program->sync();
}
}
else
{
m_program = nullptr;
}
if (!m_program && (shadermode == shader_mode::async_with_interpreter || shadermode == shader_mode::interpreter_only))
{
// Fall back to interpreter
m_program = m_shader_interpreter.get(current_fp_metadata);
if (was_interpreter != m_shader_interpreter.is_interpreter(m_program))
{
// Program has changed, reupload
m_interpreter_state = rsx::invalidate_pipeline_bits;
}
}
return m_program != nullptr;
}
void GLGSRender::load_program_env()
{
if (!m_program)
{
fmt::throw_exception("Unreachable right now");
}
const u32 fragment_constants_size = current_fp_metadata.program_constants_buffer_length;
const bool update_transform_constants = m_graphics_state & rsx::pipeline_state::transform_constants_dirty;
const bool update_fragment_constants = (m_graphics_state & rsx::pipeline_state::fragment_constants_dirty) && fragment_constants_size;
const bool update_vertex_env = m_graphics_state & rsx::pipeline_state::vertex_state_dirty;
const bool update_fragment_env = m_graphics_state & rsx::pipeline_state::fragment_state_dirty;
const bool update_fragment_texture_env = m_graphics_state & rsx::pipeline_state::fragment_texture_state_dirty;
const bool update_instruction_buffers = !!m_interpreter_state && m_shader_interpreter.is_interpreter(m_program);
const bool update_raster_env = rsx::method_registers.polygon_stipple_enabled() && (m_graphics_state & rsx::pipeline_state::polygon_stipple_pattern_dirty);
if (manually_flush_ring_buffers)
{
if (update_fragment_env) m_fragment_env_buffer->reserve_storage_on_heap(128);
if (update_vertex_env) m_vertex_env_buffer->reserve_storage_on_heap(256);
if (update_fragment_texture_env) m_texture_parameters_buffer->reserve_storage_on_heap(256);
if (update_fragment_constants) m_fragment_constants_buffer->reserve_storage_on_heap(utils::align(fragment_constants_size, 256));
if (update_transform_constants) m_transform_constants_buffer->reserve_storage_on_heap(8192);
if (update_raster_env) m_raster_env_ring_buffer->reserve_storage_on_heap(128);
if (update_instruction_buffers)
{
m_vertex_instructions_buffer->reserve_storage_on_heap(513 * 16);
m_fragment_instructions_buffer->reserve_storage_on_heap(current_fp_metadata.program_ucode_length);
}
}
if (update_vertex_env)
{
// Vertex state
auto mapping = m_vertex_env_buffer->alloc_from_heap(144, m_uniform_buffer_offset_align);
auto buf = static_cast<u8*>(mapping.first);
fill_scale_offset_data(buf, false);
fill_user_clip_data(buf + 64);
*(reinterpret_cast<u32*>(buf + 128)) = rsx::method_registers.transform_branch_bits();
*(reinterpret_cast<f32*>(buf + 132)) = rsx::method_registers.point_size() * rsx::get_resolution_scale();
*(reinterpret_cast<f32*>(buf + 136)) = rsx::method_registers.clip_min();
*(reinterpret_cast<f32*>(buf + 140)) = rsx::method_registers.clip_max();
m_vertex_env_buffer->bind_range(GL_VERTEX_PARAMS_BIND_SLOT, mapping.second, 144);
}
if (update_transform_constants)
{
// Vertex constants
u32 mem_offset = 0;
auto mem_alloc = [&](usz size) -> std::pair<void*, usz>
{
const auto mapping = m_transform_constants_buffer->alloc_from_heap(static_cast<u32>(size), m_uniform_buffer_offset_align);
mem_offset = mapping.second;
return { mapping.first, size };
};
rsx::io_buffer io_buf(mem_alloc);
upload_transform_constants(io_buf);
if (!io_buf.empty())
{
m_transform_constants_buffer->bind_range(GL_VERTEX_CONSTANT_BUFFERS_BIND_SLOT, mem_offset, ::size32(io_buf));
}
}
if (update_fragment_constants && !update_instruction_buffers)
{
// Fragment constants
auto mapping = m_fragment_constants_buffer->alloc_from_heap(fragment_constants_size, m_uniform_buffer_offset_align);
auto buf = static_cast<u8*>(mapping.first);
m_prog_buffer.fill_fragment_constants_buffer({ reinterpret_cast<float*>(buf), fragment_constants_size },
*ensure(m_fragment_prog), current_fragment_program, true);
m_fragment_constants_buffer->bind_range(GL_FRAGMENT_CONSTANT_BUFFERS_BIND_SLOT, mapping.second, fragment_constants_size);
}
if (update_fragment_env)
{
// Fragment state
auto mapping = m_fragment_env_buffer->alloc_from_heap(32, m_uniform_buffer_offset_align);
auto buf = static_cast<u8*>(mapping.first);
fill_fragment_state_buffer(buf, current_fragment_program);
m_fragment_env_buffer->bind_range(GL_FRAGMENT_STATE_BIND_SLOT, mapping.second, 32);
}
if (update_fragment_texture_env)
{
// Fragment texture parameters
auto mapping = m_texture_parameters_buffer->alloc_from_heap(768, m_uniform_buffer_offset_align);
current_fragment_program.texture_params.write_to(mapping.first, current_fp_metadata.referenced_textures_mask);
m_texture_parameters_buffer->bind_range(GL_FRAGMENT_TEXTURE_PARAMS_BIND_SLOT, mapping.second, 768);
}
if (update_raster_env)
{
auto mapping = m_raster_env_ring_buffer->alloc_from_heap(128, m_uniform_buffer_offset_align);
std::memcpy(mapping.first, rsx::method_registers.polygon_stipple_pattern(), 128);
m_raster_env_ring_buffer->bind_range(GL_RASTERIZER_STATE_BIND_SLOT, mapping.second, 128);
m_graphics_state.clear(rsx::pipeline_state::polygon_stipple_pattern_dirty);
}
if (update_instruction_buffers)
{
if (m_interpreter_state & rsx::vertex_program_dirty)
{
// Attach vertex buffer data
const auto vp_block_length = current_vp_metadata.ucode_length + 16;
auto vp_mapping = m_vertex_instructions_buffer->alloc_from_heap(vp_block_length, 16);
auto vp_buf = static_cast<u8*>(vp_mapping.first);
auto vp_config = reinterpret_cast<u32*>(vp_buf);
vp_config[0] = current_vertex_program.base_address;
vp_config[1] = current_vertex_program.entry;
vp_config[2] = current_vertex_program.output_mask;
vp_config[3] = rsx::method_registers.two_side_light_en() ? 1u : 0u;
std::memcpy(vp_buf + 16, current_vertex_program.data.data(), current_vp_metadata.ucode_length);
m_vertex_instructions_buffer->bind_range(GL_INTERPRETER_VERTEX_BLOCK, vp_mapping.second, vp_block_length);
m_vertex_instructions_buffer->notify();
}
if (m_interpreter_state & rsx::fragment_program_dirty)
{
// Attach fragment buffer data
const auto fp_block_length = current_fp_metadata.program_ucode_length + 80;
auto fp_mapping = m_fragment_instructions_buffer->alloc_from_heap(fp_block_length, 16);
auto fp_buf = static_cast<u8*>(fp_mapping.first);
// Control mask
const auto control_masks = reinterpret_cast<u32*>(fp_buf);
control_masks[0] = rsx::method_registers.shader_control();
control_masks[1] = current_fragment_program.texture_state.texture_dimensions;
// Bind textures
m_shader_interpreter.update_fragment_textures(fs_sampler_state, current_fp_metadata.referenced_textures_mask, reinterpret_cast<u32*>(fp_buf + 16));
std::memcpy(fp_buf + 80, current_fragment_program.get_data(), current_fragment_program.ucode_length);
m_fragment_instructions_buffer->bind_range(GL_INTERPRETER_FRAGMENT_BLOCK, fp_mapping.second, fp_block_length);
m_fragment_instructions_buffer->notify();
}
}
if (manually_flush_ring_buffers)
{
if (update_fragment_env) m_fragment_env_buffer->unmap();
if (update_vertex_env) m_vertex_env_buffer->unmap();
if (update_fragment_texture_env) m_texture_parameters_buffer->unmap();
if (update_fragment_constants) m_fragment_constants_buffer->unmap();
if (update_transform_constants) m_transform_constants_buffer->unmap();
if (update_raster_env) m_raster_env_ring_buffer->unmap();
if (update_instruction_buffers)
{
m_vertex_instructions_buffer->unmap();
m_fragment_instructions_buffer->unmap();
}
}
m_graphics_state.clear(
rsx::pipeline_state::fragment_state_dirty |
rsx::pipeline_state::vertex_state_dirty |
rsx::pipeline_state::transform_constants_dirty |
rsx::pipeline_state::fragment_constants_dirty |
rsx::pipeline_state::fragment_texture_state_dirty);
}
void GLGSRender::upload_transform_constants(const rsx::io_buffer& buffer)
{
const usz transform_constants_size = (!m_vertex_prog || m_vertex_prog->has_indexed_constants) ? 8192 : m_vertex_prog->constant_ids.size() * 16;
if (transform_constants_size)
{
const auto constant_ids = (transform_constants_size == 8192)
? std::span<const u16>{}
: std::span<const u16>(m_vertex_prog->constant_ids);
buffer.reserve(transform_constants_size);
fill_vertex_program_constants_data(buffer.data(), constant_ids);
}
}
void GLGSRender::update_vertex_env(const gl::vertex_upload_info& upload_info)
{
if (manually_flush_ring_buffers)
{
m_vertex_layout_buffer->reserve_storage_on_heap(128 + 16);
}
// Vertex layout state
auto mapping = m_vertex_layout_buffer->alloc_from_heap(128 + 16, m_uniform_buffer_offset_align);
auto buf = static_cast<u32*>(mapping.first);
buf[0] = upload_info.vertex_index_base;
buf[1] = upload_info.vertex_index_offset;
buf += 4;
fill_vertex_layout_state(m_vertex_layout, upload_info.first_vertex, upload_info.allocated_vertex_count, reinterpret_cast<s32*>(buf), upload_info.persistent_mapping_offset, upload_info.volatile_mapping_offset);
m_vertex_layout_buffer->bind_range(GL_VERTEX_LAYOUT_BIND_SLOT, mapping.second, 128 + 16);
if (manually_flush_ring_buffers)
{
m_vertex_layout_buffer->unmap();
}
}
void GLGSRender::patch_transform_constants(rsx::context* ctx, u32 index, u32 count)
{
if (!m_vertex_prog)