-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathOgreGL3PlusRenderSystem.cpp
More file actions
1957 lines (1641 loc) · 71.4 KB
/
OgreGL3PlusRenderSystem.cpp
File metadata and controls
1957 lines (1641 loc) · 71.4 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
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org
Copyright (c) 2000-2014 Torus Knot Software Ltd
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 "OgreGL3PlusRenderSystem.h"
#include "OgreGLUtil.h"
#include "OgreRenderSystem.h"
#include "OgreLogManager.h"
#include "OgreStringConverter.h"
#include "OgreLight.h"
#include "OgreCamera.h"
#include "OgreGL3PlusTextureManager.h"
#include "OgreGL3PlusHardwareBuffer.h"
#include "OgreGLSLShader.h"
#include "OgreGpuProgramManager.h"
#include "OgreException.h"
#include "OgreGLSLExtSupport.h"
#include "OgreGL3PlusHardwareOcclusionQuery.h"
#include "OgreGLDepthBufferCommon.h"
#include "OgreGL3PlusHardwarePixelBuffer.h"
#include "OgreGLContext.h"
#include "OgreGL3PlusFBORenderTexture.h"
#include "OgreGL3PlusHardwareBufferManager.h"
#include "OgreGLSLProgramManager.h"
#include "OgreGLSLSeparableProgram.h"
#include "OgreGLVertexArrayObject.h"
#include "OgreRoot.h"
#include "OgreConfig.h"
#include "OgreViewport.h"
#include "OgreGL3PlusPixelFormat.h"
#include "OgreGL3PlusStateCacheManager.h"
#include "OgreGLSLProgramCommon.h"
#include "OgreGL3PlusFBOMultiRenderTarget.h"
#include "OgreSPIRVShaderFactory.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
extern "C" void glFlushRenderAPPLE();
#endif
#ifndef GL_EXT_texture_filter_anisotropic
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
#endif
static void APIENTRY GLDebugCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const GLvoid* userParam)
{
const char *debSource = "", *debType = "", *debSev = "";
auto lml = Ogre::LML_NORMAL;
if (source == GL_DEBUG_SOURCE_API)
debSource = "OpenGL";
else if (source == GL_DEBUG_SOURCE_WINDOW_SYSTEM)
debSource = "Windows";
else if (source == GL_DEBUG_SOURCE_SHADER_COMPILER)
debSource = "Shader Compiler";
else if (source == GL_DEBUG_SOURCE_THIRD_PARTY)
debSource = "Third Party";
else if (source == GL_DEBUG_SOURCE_APPLICATION)
debSource = "Application";
else if (source == GL_DEBUG_SOURCE_OTHER)
debSource = "Other";
if (type == GL_DEBUG_TYPE_ERROR)
debType = "error";
else if (type == GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR)
debType = "deprecated behavior";
else if (type == GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR)
debType = "undefined behavior";
else if (type == GL_DEBUG_TYPE_PORTABILITY)
debType = "portability";
else if (type == GL_DEBUG_TYPE_PERFORMANCE)
debType = "performance";
else if (type == GL_DEBUG_TYPE_OTHER)
debType = "message";
if (severity == GL_DEBUG_SEVERITY_HIGH)
{
debSev = "high";
lml = Ogre::LML_CRITICAL;
}
else if (severity == GL_DEBUG_SEVERITY_MEDIUM)
{
debSev = "medium";
lml = Ogre::LML_WARNING;
}
else if (severity == GL_DEBUG_SEVERITY_LOW)
debSev = "low";
else if (severity == GL_DEBUG_SEVERITY_NOTIFICATION)
debSev = "note";
Ogre::LogManager::getSingleton().stream(lml) << debSource << ":" << debType << "(" << debSev << ") - " << message;
}
namespace Ogre {
static GLNativeSupport* glsupport;
static GL3WglProc get_proc(const char* proc) {
return (GL3WglProc)glsupport->getProcAddress(proc);
}
GL3PlusRenderSystem::GL3PlusRenderSystem()
: mDepthWrite(true),
mStencilWriteMask(0xFFFFFFFF),
mStateCacheManager(0),
mProgramManager(0),
mGLSLShaderFactory(0),
mSPIRVShaderFactory(0),
mHardwareBufferManager(0),
mActiveTextureUnit(0)
{
size_t i;
LogManager::getSingleton().logMessage(getName() + " created.");
// Get our GLSupport
mGLSupport = getGLSupport();
glsupport = mGLSupport;
initConfigOptions();
for (i = 0; i < OGRE_MAX_TEXTURE_LAYERS; i++)
{
// Dummy value
mTextureTypes[i] = 0;
}
mActiveRenderTarget = 0;
mCurrentContext = 0;
mMainContext = 0;
mGLInitialised = false;
mMinFilter = FO_LINEAR;
mMipFilter = FO_POINT;
mCurrentShader.fill(NULL);
mLargestSupportedAnisotropy = 1;
mRTTManager = NULL;
mSeparateShaderObjectsEnabled = false;
}
GL3PlusRenderSystem::~GL3PlusRenderSystem()
{
shutdown();
if (mGLSupport)
OGRE_DELETE mGLSupport;
}
const String& GL3PlusRenderSystem::getName(void) const
{
static String strName("OpenGL 3+ Rendering Subsystem");
return strName;
}
void GL3PlusRenderSystem::_initialise()
{
RenderSystem::_initialise();
mGLSupport->start();
}
void GL3PlusRenderSystem::initConfigOptions()
{
GLRenderSystemCommon::initConfigOptions();
ConfigOption opt;
opt.name = "Reversed Z-Buffer";
opt.possibleValues = {"No", "Yes"};
opt.currentValue = opt.possibleValues[0];
opt.immutable = false;
mOptions[opt.name] = opt;
opt.name = "Separate Shader Objects";
opt.possibleValues = {"No", "Yes"};
opt.currentValue = opt.possibleValues[1];
opt.immutable = false;
mOptions[opt.name] = opt;
opt.name = "Debug Layer";
opt.possibleValues = {"Off", "On"};
opt.currentValue = opt.possibleValues[0];
opt.immutable = false;
mOptions[opt.name] = opt;
}
RenderSystemCapabilities* GL3PlusRenderSystem::createRenderSystemCapabilities() const
{
RenderSystemCapabilities* rsc = OGRE_NEW RenderSystemCapabilities();
rsc->setCategoryRelevant(CAPS_CATEGORY_GL, true);
rsc->setDriverVersion(mDriverVersion);
const char* deviceName = (const char*)glGetString(GL_RENDERER);
if (deviceName)
{
rsc->setDeviceName(deviceName);
}
rsc->setRenderSystemName(getName());
rsc->setVendor(mVendor);
// Check for hardware mipmapping support.
rsc->setCapability(RSC_AUTOMIPMAP_COMPRESSED);
// Multitexturing support and set number of texture units
GLint units;
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &units));
rsc->setNumTextureUnits(std::min(OGRE_MAX_TEXTURE_LAYERS, units));
glGetIntegerv( GL_MAX_VERTEX_ATTRIBS , &units);
rsc->setNumVertexAttributes(units);
// Check for Anisotropy support
if (checkExtension("GL_EXT_texture_filter_anisotropic"))
{
GLfloat maxAnisotropy = 0;
OGRE_CHECK_GL_ERROR(glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy));
rsc->setMaxSupportedAnisotropy(maxAnisotropy);
rsc->setCapability(RSC_ANISOTROPY);
}
// Point sprites
rsc->setCapability(RSC_POINT_SPRITES);
rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
// Check for hardware stencil support and set bit depth
rsc->setCapability(RSC_HWSTENCIL);
rsc->setCapability(RSC_TWO_SIDED_STENCIL);
rsc->setCapability(RSC_HW_GAMMA);
// Vertex Buffer Objects are always supported
rsc->setCapability(RSC_MAPBUFFER);
rsc->setCapability(RSC_32BIT_INDEX);
// Vertex Array Objects are supported in 3.0
rsc->setCapability(RSC_VAO);
// Check for texture compression
rsc->setCapability(RSC_TEXTURE_COMPRESSION);
// Check for dxt compression
if (checkExtension("GL_EXT_texture_compression_s3tc"))
{
rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
}
// Check for etc compression
if (hasMinGLVersion(4, 3) || checkExtension("GL_ARB_ES3_compatibility"))
{
rsc->setCapability(RSC_TEXTURE_COMPRESSION_ETC2);
}
// Check for vtc compression
if (checkExtension("GL_NV_texture_compression_vtc"))
{
rsc->setCapability(RSC_TEXTURE_COMPRESSION_VTC);
}
// RGTC(BC4/BC5) is supported by the 3.0 spec
rsc->setCapability(RSC_TEXTURE_COMPRESSION_BC4_BC5);
// BPTC(BC6H/BC7) is supported by the extension or OpenGL 4.2 or higher
if (hasMinGLVersion(4, 2) || checkExtension("GL_ARB_texture_compression_bptc"))
{
rsc->setCapability(RSC_TEXTURE_COMPRESSION_BC6H_BC7);
}
if (checkExtension("WEBGL_compressed_texture_astc") ||
checkExtension("GL_KHR_texture_compression_astc_ldr"))
rsc->setCapability(RSC_TEXTURE_COMPRESSION_ASTC);
rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
// Probe number of draw buffers
// Only makes sense with FBO support, so probe here
GLint buffers;
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_DRAW_BUFFERS, &buffers));
rsc->setNumMultiRenderTargets(std::min<int>(buffers, (GLint)OGRE_MAX_MULTIPLE_RENDER_TARGETS));
rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
// Stencil wrapping
rsc->setCapability(RSC_STENCIL_WRAP);
// Check for non-power-of-2 texture support
rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
// Check for SSBO support
if (hasMinGLVersion(4, 3) || checkExtension("GL_ARB_shader_storage_buffer_object"))
rsc->setCapability(RSC_READ_WRITE_BUFFERS);
// As are user clipping planes
rsc->setCapability(RSC_USER_CLIP_PLANES);
// So are 1D & 3D textures
rsc->setCapability(RSC_TEXTURE_1D);
rsc->setCapability(RSC_TEXTURE_3D);
rsc->setCapability(RSC_TEXTURE_2D_ARRAY);
rsc->setCapability(RSC_DEPTH_CLAMP);
// Check for hardware occlusion support
rsc->setCapability(RSC_HWOCCLUSION);
// Point size
GLfloat psRange[2] = {0.0, 0.0};
OGRE_CHECK_GL_ERROR(glGetFloatv(GL_POINT_SIZE_RANGE, psRange));
rsc->setMaxPointSize(psRange[1]);
// GLSL is always supported in GL
// TODO: Deprecate this profile name in favor of versioned names
rsc->addShaderProfile("glsl");
// Support for specific shader profiles
bool limitedOSXCoreProfile = OGRE_PLATFORM == OGRE_PLATFORM_APPLE && hasMinGLVersion(3, 2);
for (uint16 ver = getNativeShadingLanguageVersion(); ver >= 400; ver -= 10)
rsc->addShaderProfile("glsl" + StringConverter::toString(ver));
if (getNativeShadingLanguageVersion() >= 330)
rsc->addShaderProfile("glsl330");
if (getNativeShadingLanguageVersion() >= 150)
rsc->addShaderProfile("glsl150");
if (getNativeShadingLanguageVersion() >= 140 && !limitedOSXCoreProfile)
rsc->addShaderProfile("glsl140");
if (getNativeShadingLanguageVersion() >= 130 && !limitedOSXCoreProfile)
rsc->addShaderProfile("glsl130");
if (mSeparateShaderObjectsEnabled &&
(hasMinGLVersion(4, 3) ||
(checkExtension("GL_ARB_separate_shader_objects") && checkExtension("GL_ARB_program_interface_query"))))
{
rsc->setCapability(RSC_SEPARATE_SHADER_OBJECTS);
rsc->setCapability(RSC_GLSL_SSO_REDECLARE);
}
if (checkExtension("GL_ARB_gl_spirv") && rsc->hasCapability(RSC_SEPARATE_SHADER_OBJECTS))
rsc->addShaderProfile("gl_spirv");
// Mesa 11.2 does not behave according to spec and throws a "gl_Position redefined"
if(rsc->getDeviceName().find("Mesa") != String::npos) {
rsc->unsetCapability(RSC_GLSL_SSO_REDECLARE);
}
// Vertex/Fragment Programs
rsc->setCapability(RSC_VERTEX_PROGRAM);
GLint constantCount = 0;
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &constantCount));
rsc->setVertexProgramConstantFloatCount((Ogre::ushort)constantCount/4);
// Fragment Program Properties
constantCount = 0;
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &constantCount));
rsc->setFragmentProgramConstantFloatCount((Ogre::ushort)constantCount/4);
// Geometry Program Properties
rsc->setCapability(RSC_GEOMETRY_PROGRAM);
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_GEOMETRY_UNIFORM_COMPONENTS, &constantCount));
rsc->setGeometryProgramConstantFloatCount(constantCount/4);
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES, &constantCount));
rsc->setGeometryProgramNumOutputVertices(constantCount/4);
// Tessellation Program Properties
if (hasMinGLVersion(4, 0) || checkExtension("GL_ARB_tessellation_shader"))
{
rsc->setCapability(RSC_TESSELLATION_HULL_PROGRAM);
rsc->setCapability(RSC_TESSELLATION_DOMAIN_PROGRAM);
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_TESS_CONTROL_UNIFORM_COMPONENTS, &constantCount));
// float params
rsc->setTessellationHullProgramConstantFloatCount(constantCount/4);
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_TESS_EVALUATION_UNIFORM_COMPONENTS, &constantCount));
// float params
rsc->setTessellationDomainProgramConstantFloatCount(constantCount/4);
}
// Compute Program Properties
if (hasMinGLVersion(4, 3) || checkExtension("GL_ARB_compute_shader"))
{
rsc->setCapability(RSC_COMPUTE_PROGRAM);
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_COMPUTE_UNIFORM_COMPONENTS, &constantCount));
rsc->setComputeProgramConstantFloatCount(constantCount);
//TODO we should also check max workgroup count & size
// OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_SIZE, &workgroupCount));
// OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &workgroupInvocations));
}
if (hasMinGLVersion(4, 1) || checkExtension("GL_ARB_get_program_binary"))
{
GLint formats = 0;
OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats));
if (formats > 0)
rsc->setCapability(RSC_CAN_GET_COMPILED_SHADER_BUFFER);
}
rsc->setCapability(RSC_VERTEX_FORMAT_INT_10_10_10_2);
rsc->setCapability(RSC_VERTEX_BUFFER_INSTANCE_DATA);
// Check for Float textures
rsc->setCapability(RSC_TEXTURE_FLOAT);
// OpenGL 3.0 requires a minimum of 16 texture image units
units = std::max<GLint>(16, units);
rsc->setNumVertexTextureUnits(static_cast<ushort>(units));
rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
// Mipmap LOD biasing?
rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
// Alpha to coverage always 'supported' when MSAA is available
// although card may ignore it if it doesn't specifically support A2C
rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
// Check if render to vertex buffer (transform feedback in OpenGL)
rsc->setCapability(RSC_HWRENDER_TO_VERTEX_BUFFER);
if (hasMinGLVersion(4, 3) || checkExtension("GL_KHR_debug"))
rsc->setCapability(RSC_DEBUG);
if( hasMinGLVersion(4, 3) || checkExtension("GL_ARB_ES3_compatibility"))
rsc->setCapability(RSC_PRIMITIVE_RESTART);
GLfloat lineWidth[2] = {1, 1};
glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, lineWidth);
if(lineWidth[1] != 1 && lineWidth[1] != lineWidth[0])
rsc->setCapability(RSC_WIDE_LINES);
return rsc;
}
void GL3PlusRenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary)
{
if (caps->getRenderSystemName() != getName())
{
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Trying to initialize GL3PlusRenderSystem from RenderSystemCapabilities that do not support OpenGL 3+",
"GL3PlusRenderSystem::initialiseFromRenderSystemCapabilities");
}
mProgramManager = new GLSLProgramManager(this);
// Create GLSL shader factory
mGLSLShaderFactory = new GLSLShaderFactory();
HighLevelGpuProgramManager::getSingleton().addFactory(mGLSLShaderFactory);
mSPIRVShaderFactory = new SPIRVShaderFactory();
HighLevelGpuProgramManager::getSingleton().addFactory(mSPIRVShaderFactory);
// Use VBO's by default
mHardwareBufferManager = new GL3PlusHardwareBufferManager();
// Use FBO's for RTT, PBuffers and Copy are no longer supported
// Create FBO manager
mRTTManager = new GL3PlusFBOManager(this);
caps->setCapability(RSC_RTT_DEPTHBUFFER_RESOLUTION_LESSEQUAL);
// Create the texture manager
mTextureManager = new GL3PlusTextureManager(this);
mGLInitialised = true;
}
void GL3PlusRenderSystem::shutdown(void)
{
RenderSystem::shutdown();
// Remove from manager safely
if (auto progMgr = HighLevelGpuProgramManager::getSingletonPtr())
{
if(mGLSLShaderFactory)
progMgr->removeFactory(mGLSLShaderFactory);
if(mSPIRVShaderFactory)
progMgr->removeFactory(mSPIRVShaderFactory);
}
OGRE_DELETE mGLSLShaderFactory;
mGLSLShaderFactory = 0;
OGRE_DELETE mSPIRVShaderFactory;
mSPIRVShaderFactory = 0;
// Delete extra threads contexts
for (auto pCurContext : mBackgroundContextList)
{
pCurContext->releaseContext();
OGRE_DELETE pCurContext;
}
mBackgroundContextList.clear();
// Deleting the GPU program manager and hardware buffer manager. Has to be done before the mGLSupport->stop().
delete mProgramManager;
mProgramManager = NULL;
OGRE_DELETE mHardwareBufferManager;
mHardwareBufferManager = 0;
OGRE_DELETE mRTTManager;
mRTTManager = 0;
OGRE_DELETE mTextureManager;
mTextureManager = 0;
mGLSupport->stop();
// delete mTextureManager;
// mTextureManager = 0;
mGLInitialised = 0;
// RenderSystem::shutdown();
}
RenderWindow* GL3PlusRenderSystem::_createRenderWindow(const String &name, unsigned int width, unsigned int height,
bool fullScreen, const NameValuePairList *miscParams)
{
RenderSystem::_createRenderWindow(name, width, height, fullScreen, miscParams);
// Create the window
RenderWindow* win = mGLSupport->newWindow(name, width, height, fullScreen, miscParams);
attachRenderTarget((Ogre::RenderTarget&) *win);
if (!mGLInitialised)
{
initialiseContext(win);
const char* shadingLangVersion = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
StringVector tokens = StringUtil::split(shadingLangVersion, ". ");
mNativeShadingLanguageVersion = (StringConverter::parseUnsignedInt(tokens[0]) * 100) + StringConverter::parseUnsignedInt(tokens[1]);
auto it = mOptions.find("Reversed Z-Buffer");
if (it != mOptions.end())
{
mIsReverseDepthBufferEnabled = StringConverter::parseBool(it->second.currentValue);
if(mIsReverseDepthBufferEnabled && !hasMinGLVersion(4, 5) && !checkExtension("GL_ARB_clip_control"))
{
mIsReverseDepthBufferEnabled = false;
LogManager::getSingleton().logWarning("Reversed Z-Buffer was requested, but it is not supported. Disabling.");
}
}
it = mOptions.find("Separate Shader Objects");
if (it != mOptions.end())
{
mSeparateShaderObjectsEnabled = StringConverter::parseBool(it->second.currentValue);
}
// Initialise GL after the first window has been created
// TODO: fire this from emulation options, and don't duplicate Real and Current capabilities
mRealCapabilities = createRenderSystemCapabilities();
// use real capabilities if custom capabilities are not available
if (!mUseCustomCapabilities)
mCurrentCapabilities = mRealCapabilities;
fireEvent("RenderSystemCapabilitiesCreated");
initialiseFromRenderSystemCapabilities(mCurrentCapabilities, (RenderTarget *) win);
// Initialise the main context
_oneTimeContextInitialization();
if (mCurrentContext)
mCurrentContext->setInitialized();
}
if ( win->getDepthBufferPool() != DepthBuffer::POOL_NO_DEPTH )
{
// Unlike D3D9, OGL doesn't allow sharing the main depth buffer, so keep them separate.
GL3PlusContext *windowContext = dynamic_cast<GLRenderTarget*>(win)->getContext();
auto depthBuffer =
new GLDepthBufferCommon(DepthBuffer::POOL_DEFAULT, this, windowContext, 0, 0, win, true);
mDepthBufferPool[depthBuffer->getPoolId()].push_back( depthBuffer );
win->attachDepthBuffer( depthBuffer );
}
return win;
}
DepthBuffer* GL3PlusRenderSystem::_createDepthBufferFor( RenderTarget *renderTarget )
{
if ( auto fbo = dynamic_cast<GLRenderTarget*>(renderTarget)->getFBO() )
{
// Find best depth & stencil format suited for the RT's format.
GLuint depthFormat, stencilFormat;
_getDepthStencilFormatFor(fbo->getFormat(), &depthFormat, &stencilFormat);
GL3PlusRenderBuffer *depthBuffer = new GL3PlusRenderBuffer( depthFormat, fbo->getWidth(),
fbo->getHeight(), fbo->getFSAA() );
GL3PlusRenderBuffer *stencilBuffer = NULL;
if ( depthFormat == GL_DEPTH24_STENCIL8 || depthFormat == GL_DEPTH32F_STENCIL8)
{
// If we have a packed format, the stencilBuffer is the same as the depthBuffer
stencilBuffer = depthBuffer;
}
else if(stencilFormat)
{
stencilBuffer = new GL3PlusRenderBuffer( stencilFormat, fbo->getWidth(),
fbo->getHeight(), fbo->getFSAA() );
}
return new GLDepthBufferCommon(0, this, mCurrentContext, depthBuffer, stencilBuffer,
renderTarget, false);
}
return NULL;
}
MultiRenderTarget* GL3PlusRenderSystem::createMultiRenderTarget(const String & name)
{
MultiRenderTarget* retval =
new GL3PlusFBOMultiRenderTarget(static_cast<GL3PlusFBOManager*>(mRTTManager), name);
attachRenderTarget(*retval);
return retval;
}
void GL3PlusRenderSystem::destroyRenderWindow(const String& name)
{
// Find it to remove from list.
RenderTarget* pWin = detachRenderTarget(name);
OgreAssert(pWin, "unknown RenderWindow name");
GL3PlusContext *windowContext = dynamic_cast<GLRenderTarget*>(pWin)->getContext();
// 1 Window <-> 1 Context, should be always true.
assert( windowContext );
bool bFound = false;
// Find the depth buffer from this window and remove it.
DepthBufferMap::iterator itMap = mDepthBufferPool.begin();
DepthBufferMap::iterator enMap = mDepthBufferPool.end();
while( itMap != enMap && !bFound )
{
DepthBufferVec::iterator itor = itMap->second.begin();
DepthBufferVec::iterator end = itMap->second.end();
while( itor != end )
{
// A DepthBuffer with no depth & stencil pointers is a dummy one,
// look for the one that matches the same GL context.
auto depthBuffer = static_cast<GLDepthBufferCommon*>(*itor);
GL3PlusContext *glContext = depthBuffer->getGLContext();
if ( glContext == windowContext &&
(depthBuffer->getDepthBuffer() || depthBuffer->getStencilBuffer()) )
{
bFound = true;
delete *itor;
itMap->second.erase( itor );
break;
}
++itor;
}
++itMap;
}
delete pWin;
}
void GL3PlusRenderSystem::_setTexture(size_t stage, bool enabled, const TexturePtr &texPtr)
{
if (!mStateCacheManager->activateGLTextureUnit(stage))
return;
if (enabled)
{
GL3PlusTexturePtr tex = static_pointer_cast<GL3PlusTexture>(texPtr);
// Note used
tex->touch();
mTextureTypes[stage] = tex->getGL3PlusTextureTarget();
mStateCacheManager->bindGLTexture( mTextureTypes[stage], tex->getGLID() );
}
else
{
// Bind zero texture.
mStateCacheManager->bindGLTexture(GL_TEXTURE_2D, 0);
}
}
void GL3PlusRenderSystem::_setSampler(size_t unit, Sampler& sampler)
{
static_cast<GL3PlusSampler&>(sampler).bind(unit);
}
void GL3PlusRenderSystem::_setTextureAddressingMode(size_t stage, const Sampler::UVWAddressingMode& uvw)
{
if (!mStateCacheManager->activateGLTextureUnit(stage))
return;
mStateCacheManager->setTexParameteri(mTextureTypes[stage], GL_TEXTURE_WRAP_S,
GL3PlusSampler::getTextureAddressingMode(uvw.u));
mStateCacheManager->setTexParameteri(mTextureTypes[stage], GL_TEXTURE_WRAP_T,
GL3PlusSampler::getTextureAddressingMode(uvw.v));
mStateCacheManager->setTexParameteri(mTextureTypes[stage], GL_TEXTURE_WRAP_R,
GL3PlusSampler::getTextureAddressingMode(uvw.w));
}
void GL3PlusRenderSystem::_setLineWidth(float width)
{
OGRE_CHECK_GL_ERROR(glLineWidth(width));
}
GLenum GL3PlusRenderSystem::getBlendMode(SceneBlendFactor ogreBlend) const
{
switch (ogreBlend)
{
case SBF_ONE:
return GL_ONE;
case SBF_ZERO:
return GL_ZERO;
case SBF_DEST_COLOUR:
return GL_DST_COLOR;
case SBF_SOURCE_COLOUR:
return GL_SRC_COLOR;
case SBF_ONE_MINUS_DEST_COLOUR:
return GL_ONE_MINUS_DST_COLOR;
case SBF_ONE_MINUS_SOURCE_COLOUR:
return GL_ONE_MINUS_SRC_COLOR;
case SBF_DEST_ALPHA:
return GL_DST_ALPHA;
case SBF_SOURCE_ALPHA:
return GL_SRC_ALPHA;
case SBF_ONE_MINUS_DEST_ALPHA:
return GL_ONE_MINUS_DST_ALPHA;
case SBF_ONE_MINUS_SOURCE_ALPHA:
return GL_ONE_MINUS_SRC_ALPHA;
};
// To keep compiler happy.
return GL_ONE;
}
void GL3PlusRenderSystem::_setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage)
{
mStateCacheManager->setEnabled(GL_SAMPLE_ALPHA_TO_COVERAGE, (func != CMPF_ALWAYS_PASS) && alphaToCoverage);
}
void GL3PlusRenderSystem::_setViewport(Viewport *vp)
{
// Check if viewport is different
if (!vp)
{
mActiveViewport = NULL;
_setRenderTarget(NULL);
}
else if (vp != mActiveViewport || vp->_isUpdated())
{
RenderTarget* target;
target = vp->getTarget();
_setRenderTarget(target);
mActiveViewport = vp;
// Calculate the "lower-left" corner of the viewport
Rect vpRect = vp->getActualDimensions();
if (!target->requiresTextureFlipping())
{
// Convert "upper-left" corner to "lower-left"
std::swap(vpRect.top, vpRect.bottom);
vpRect.top = target->getHeight() - vpRect.top;
vpRect.bottom = target->getHeight() - vpRect.bottom;
}
mStateCacheManager->setViewport(vpRect);
vp->_clearUpdatedFlag();
}
}
void GL3PlusRenderSystem::_endFrame(void)
{
// unbind GPU programs at end of frame
// this is mostly to avoid holding bound programs that might get deleted
// outside via the resource manager
unbindGpuProgram(GPT_VERTEX_PROGRAM);
unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
unbindGpuProgram(GPT_GEOMETRY_PROGRAM);
if (mDriverVersion.major >= 4)
{
unbindGpuProgram(GPT_HULL_PROGRAM);
unbindGpuProgram(GPT_DOMAIN_PROGRAM);
if (mDriverVersion.minor >= 3)
unbindGpuProgram(GPT_COMPUTE_PROGRAM);
}
}
void GL3PlusRenderSystem::_setCullingMode(CullingMode mode)
{
mCullingMode = mode;
// NB: Because two-sided stencil API dependence of the front face, we must
// use the same 'winding' for the front face everywhere. As the OGRE default
// culling mode is clockwise, we also treat anticlockwise winding as front
// face for consistently. On the assumption that, we can't change the front
// face by glFrontFace anywhere.
GLenum cullMode;
bool flip = flipFrontFace();
switch( mode )
{
case CULL_NONE:
mStateCacheManager->setEnabled( GL_CULL_FACE, false );
return;
case CULL_CLOCKWISE:
cullMode = flip ? GL_FRONT : GL_BACK;
break;
case CULL_ANTICLOCKWISE:
cullMode = flip ? GL_BACK : GL_FRONT;
break;
}
mStateCacheManager->setEnabled( GL_CULL_FACE, true );
mStateCacheManager->setCullFace( cullMode );
}
void GL3PlusRenderSystem::_setDepthClamp(bool enable)
{
mStateCacheManager->setEnabled(GL_DEPTH_CLAMP, enable);
}
void GL3PlusRenderSystem::_setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
{
_setDepthBufferCheckEnabled(depthTest);
_setDepthBufferWriteEnabled(depthWrite);
_setDepthBufferFunction(depthFunction);
}
void GL3PlusRenderSystem::_setDepthBufferCheckEnabled(bool enabled)
{
if (enabled)
{
mStateCacheManager->setClearDepth(isReverseDepthBufferEnabled() ? 0.0f : 1.0f);
}
mStateCacheManager->setEnabled(GL_DEPTH_TEST, enabled);
}
void GL3PlusRenderSystem::_setDepthBufferWriteEnabled(bool enabled)
{
GLboolean flag = enabled ? GL_TRUE : GL_FALSE;
mStateCacheManager->setDepthMask( flag );
// Store for reference in _beginFrame
mDepthWrite = enabled;
}
void GL3PlusRenderSystem::_setDepthBufferFunction(CompareFunction func)
{
if(isReverseDepthBufferEnabled())
func = reverseCompareFunction(func);
mStateCacheManager->setDepthFunc(convertCompareFunction(func));
}
void GL3PlusRenderSystem::_setDepthBias(float constantBias, float slopeScaleBias)
{
bool enable = constantBias != 0 || slopeScaleBias != 0;
mStateCacheManager->setEnabled(GL_POLYGON_OFFSET_FILL, enable);
mStateCacheManager->setEnabled(GL_POLYGON_OFFSET_POINT, enable);
mStateCacheManager->setEnabled(GL_POLYGON_OFFSET_LINE, enable);
if (enable)
{
if(isReverseDepthBufferEnabled())
{
slopeScaleBias *= -1;
constantBias *= -1;
}
glPolygonOffset(-slopeScaleBias, -constantBias);
}
}
static GLenum getBlendOp(SceneBlendOperation op)
{
switch (op)
{
case SBO_ADD:
return GL_FUNC_ADD;
case SBO_SUBTRACT:
return GL_FUNC_SUBTRACT;
case SBO_REVERSE_SUBTRACT:
return GL_FUNC_REVERSE_SUBTRACT;
case SBO_MIN:
return GL_MIN;
case SBO_MAX:
return GL_MAX;
}
return GL_FUNC_ADD;
}
void GL3PlusRenderSystem::setColourBlendState(const ColourBlendState& state)
{
// record this
mCurrentBlend = state;
if (state.blendingEnabled())
{
mStateCacheManager->setEnabled(GL_BLEND, true);
mStateCacheManager->setBlendFunc(
getBlendMode(state.sourceFactor), getBlendMode(state.destFactor),
getBlendMode(state.sourceFactorAlpha), getBlendMode(state.destFactorAlpha));
}
else
{
mStateCacheManager->setEnabled(GL_BLEND, false);
}
mStateCacheManager->setBlendEquation(getBlendOp(state.operation), getBlendOp(state.alphaOperation));
mStateCacheManager->setColourMask(state.writeR, state.writeG, state.writeB, state.writeA);
}
HardwareOcclusionQuery* GL3PlusRenderSystem::createHardwareOcclusionQuery(void)
{
GL3PlusHardwareOcclusionQuery* ret = new GL3PlusHardwareOcclusionQuery();
mHwOcclusionQueries.push_back(ret);
return ret;
}
void GL3PlusRenderSystem::_setPolygonMode(PolygonMode level)
{
switch(level)
{
case PM_POINTS:
mStateCacheManager->setPolygonMode(GL_POINT);
break;
case PM_WIREFRAME:
mStateCacheManager->setPolygonMode(GL_LINE);
break;
case PM_SOLID:
mStateCacheManager->setPolygonMode(GL_FILL);
break;
}
}
void GL3PlusRenderSystem::setStencilState(const StencilState& state)
{
mStateCacheManager->setEnabled(GL_STENCIL_TEST, state.enabled);
if(!state.enabled)
return;
bool flip;
mStencilWriteMask = state.writeMask;
auto compareOp = convertCompareFunction(state.compareOp);
if (state.twoSidedOperation)
{
// NB: We should always treat CCW as front face for consistent with default
// culling mode. Therefore, we must take care with two-sided stencil settings.
flip = flipFrontFace();
// Back
OGRE_CHECK_GL_ERROR(glStencilMaskSeparate(GL_BACK, state.writeMask));
OGRE_CHECK_GL_ERROR(glStencilFuncSeparate(GL_BACK, compareOp, state.referenceValue, state.compareMask));
OGRE_CHECK_GL_ERROR(glStencilOpSeparate(GL_BACK,
convertStencilOp(state.stencilFailOp, !flip),