-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathHardwareOpenGL.cpp
More file actions
1836 lines (1512 loc) · 53.5 KB
/
HardwareOpenGL.cpp
File metadata and controls
1836 lines (1512 loc) · 53.5 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
/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <array>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <optional>
// TODO: Use SDL_FunctionPointer properly instead
#define SDL_FUNCTION_POINTER_IS_VOID_POINTER
#include <SDL3/SDL.h>
#if defined(WIN32)
#include <windows.h>
#endif
#define DECLARE_OPENGL
#include "dyna_gl.h"
#include "byteswap.h"
#include "pserror.h"
#include "mono.h"
#include "3d.h"
#include "renderer.h"
#include "application.h"
#include "bitmap.h"
#include "lightmap.h"
#include "log.h"
#include "rend_opengl.h"
#include "grdefs.h"
#include "mem.h"
#include "config.h"
#include "rtperformance.h"
#include "HardwareInternal.h"
#include "args.h"
#include "NewBitmap.h"
#include "shaders.h"
#include "ShaderProgram.h"
#if defined(WIN32)
#include "win/arb_extensions.h"
#endif
// General renderer states
extern int gpu_Overlay_map;
int Bump_map = 0;
int Bumpmap_ready = 0;
extern uint8_t gpu_Overlay_type;
float Z_bias = 0.0f;
uint8_t Renderer_close_flag = 0;
extern uint8_t Renderer_initted;
renderer_type Renderer_type = RENDERER_OPENGL;
struct Renderer {
Renderer()
: shader_{shaders::vertex,
shaders::fragment,
{vertexAttrib(3, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::pos, "in_pos"),
vertexAttrib(4, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::color, "in_color"),
vertexAttrib(2, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::uv0, "in_uv0"),
vertexAttrib(2, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::uv1, "in_uv1")}} {
shader_.Use();
// these are effectively just constants, for now
shader_.setUniform1i("u_texture0", 0);
shader_.setUniform1i("u_texture1", 1);
}
/**
* Sets the vertex transformation matrices. Takes all three of the MVP matrices at once, in order to avoid
* multiple SetUniform operations. Pass std::nullopt for any matrices that should not be altered.
*/
void setTransform(std::optional<glm::mat4x4> const &model, std::optional<glm::mat4x4> const &view,
std::optional<glm::mat4x4> const &projection) {
if (model) {
model_ = *model;
}
if (view) {
view_ = *view;
}
if (projection) {
projection_ = *projection;
}
shader_.setUniformMat4f("u_modelview", view_ * model_);
shader_.setUniformMat4f("u_projection", projection_);
}
void setTextureEnabled(GLuint index, bool enabled) {
GLint bit = 1 << index;
if (enabled) {
texture_enable_ |= bit;
} else {
texture_enable_ &= ~bit;
}
shader_.setUniform1i("u_texture_enable", texture_enable_);
}
template <typename VertexIter, typename = std::enable_if_t<std::is_same_v<
typename std::iterator_traits<VertexIter>::value_type, PosColorUV2Vertex>>>
size_t addVertexData(VertexIter begin, VertexIter end) {
return shader_.addVertexData(begin, end);
}
struct PosColorUVVertex_tag {};
template <typename VertexIter, typename = std::enable_if_t<std::is_same_v<
typename std::iterator_traits<VertexIter>::value_type, PosColorUVVertex>>>
size_t addVertexData(VertexIter begin, VertexIter end, PosColorUVVertex_tag = {}) {
std::array<PosColorUV2Vertex, MAX_POINTS_IN_POLY> converted;
std::transform(begin, end, converted.begin(),
[](auto const &vtx) { return PosColorUV2Vertex{vtx.pos, vtx.color, vtx.uv, {}}; });
return shader_.addVertexData(converted.cbegin(), converted.cend());
}
void setFogEnabled(bool enabled) { shader_.setUniform1i("u_fog_enable", enabled); }
void setFogBorders(float nearz, float farz) {
shader_.setUniform1f("u_fog_start", nearz);
shader_.setUniform1f("u_fog_end", farz);
}
void setGammaCorrection(float gamma) { shader_.setUniform1f("u_gamma", gamma); }
void setFogColor(ddgr_color color) {
shader_.setUniform4fv("u_fog_color", GR_COLOR_RED(color) / 255.0f, GR_COLOR_GREEN(color) / 255.0f,
GR_COLOR_BLUE(color) / 255.0f, 1);
}
private:
glm::mat4x4 model_;
glm::mat4x4 view_;
glm::mat4x4 projection_;
GLint texture_enable_{};
ShaderProgram<PosColorUV2Vertex> shader_;
};
std::optional<Renderer> gRenderer;
#ifndef GL_UNSIGNED_SHORT_5_5_5_1
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
#endif
#ifndef GL_UNSIGNED_SHORT_4_4_4_4
#define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
#endif
#define CHECK_ERROR(x)
SDL_Window *GSDLWindow = nullptr;
SDL_GLContext GSDLGLContext = nullptr;
char loadedLibrary[_MAX_PATH];
#define GET_WRAP_STATE(x) ((x) >> 4)
#define GET_FILTER_STATE(x) ((x) & 0x0f)
#define SET_WRAP_STATE(x, s) \
{ \
(x) &= 0x0F; \
(x) |= ((s) << 4); \
}
#define SET_FILTER_STATE(x, s) \
{ \
(x) &= 0xF0; \
(x) |= (s); \
}
// OpenGL Stuff
static int OpenGL_polys_drawn = 0;
static int OpenGL_verts_processed = 0;
static int OpenGL_uploads = 0;
static int OpenGL_sets_this_frame[10];
static int OpenGL_packed_pixels = 0;
static std::vector<GLuint> textures_;
static int OpenGL_cache_initted = 0;
static int OpenGL_last_bound[2];
extern int gpu_last_frame_polys_drawn;
extern int gpu_last_frame_verts_processed;
extern int gpu_last_uploaded;
extern float gpu_Alpha_factor;
extern float gpu_Alpha_multiplier;
uint16_t *OpenGL_bitmap_remap = nullptr;
uint16_t *OpenGL_lightmap_remap = nullptr;
uint8_t *OpenGL_bitmap_states = nullptr;
uint8_t *OpenGL_lightmap_states = nullptr;
uint32_t *opengl_Upload_data = nullptr;
uint32_t *opengl_Translate_table = nullptr;
uint32_t *opengl_4444_translate_table = nullptr;
uint16_t *opengl_packed_Upload_data = nullptr;
uint16_t *opengl_packed_Translate_table = nullptr;
uint16_t *opengl_packed_4444_translate_table = nullptr;
extern rendering_state gpu_state;
extern renderer_preferred_state gpu_preferred_state;
bool OpenGL_multitexture_state = false;
module_t *OpenGLDLLHandle = nullptr;
int Already_loaded = 0;
bool opengl_Blending_on = false;
static oeApplication *ParentApplication = nullptr;
static GLuint GOpenGLFBO = 0;
static GLuint GOpenGLRBOColor = 0;
static GLuint GOpenGLRBODepth = 0;
static GLuint GOpenGLFBOWidth = 0;
static GLuint GOpenGLFBOHeight = 0;
// returns true if the passed in extension name is supported
bool opengl_CheckExtension(std::string_view extName) {
GLint numExtensions;
dglGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
for (GLint i = 0; i < numExtensions; i++) {
if (extName == reinterpret_cast<char const *>(dglGetStringi(GL_EXTENSIONS, i))) {
return true;
}
}
return false;
}
// Gets some specific information about this particular flavor of opengl
void opengl_GetInformation() {
LOG_INFO.printf("OpenGL Vendor: %s", dglGetString(GL_VENDOR));
LOG_INFO.printf("OpenGL Renderer: %s", dglGetString(GL_RENDERER));
LOG_INFO.printf("OpenGL Version: %s", dglGetString(GL_VERSION));
}
int opengl_MakeTextureObject(int tn) {
GLuint num;
dglGenTextures(1, &num);
textures_.push_back(num);
dglActiveTexture(GL_TEXTURE0_ARB + tn);
dglBindTexture(GL_TEXTURE_2D, num);
dglPixelStorei(GL_UNPACK_ALIGNMENT, 2);
dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexEnvf (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
CHECK_ERROR(2)
return num;
}
int opengl_InitCache() {
OpenGL_bitmap_remap = mem_rmalloc<uint16_t>(MAX_BITMAPS);
ASSERT(OpenGL_bitmap_remap);
OpenGL_lightmap_remap = mem_rmalloc<uint16_t>(MAX_LIGHTMAPS);
ASSERT(OpenGL_lightmap_remap);
OpenGL_bitmap_states = mem_rmalloc<uint8_t>(MAX_BITMAPS);
ASSERT(OpenGL_bitmap_states);
OpenGL_lightmap_states = mem_rmalloc<uint8_t>(MAX_LIGHTMAPS);
ASSERT(OpenGL_lightmap_states);
// Setup textures and cacheing
int i;
for (i = 0; i < MAX_BITMAPS; i++) {
OpenGL_bitmap_remap[i] = 65535;
OpenGL_bitmap_states[i] = 255;
GameBitmaps[i].flags |= BF_CHANGED | BF_BRAND_NEW;
}
for (i = 0; i < MAX_LIGHTMAPS; i++) {
OpenGL_lightmap_remap[i] = 65535;
OpenGL_lightmap_states[i] = 255;
GameLightmaps[i].flags |= LF_CHANGED | LF_BRAND_NEW;
}
CHECK_ERROR(3)
OpenGL_cache_initted = 1;
return 1;
}
// Sets default states for our renderer
void opengl_SetDefaults() {
LOG_INFO << "Setting states";
gpu_state.cur_color = 0x00FFFFFF;
gpu_state.cur_bilinear_state = -1;
gpu_state.cur_zbuffer_state = -1;
gpu_state.cur_texture_quality = -1;
gpu_state.cur_light_state = LS_GOURAUD;
gpu_state.cur_color_model = CM_MONO;
gpu_state.cur_bilinear_state = -1;
gpu_state.cur_alpha_type = AT_TEXTURE;
// Enable some states
dglEnable(GL_BLEND);
dglEnable(GL_DITHER);
opengl_Blending_on = true;
rend_SetAlphaType(AT_ALWAYS);
rend_SetAlphaValue(255);
rend_SetFiltering(1);
rend_SetLighting(LS_NONE);
rend_SetTextureType(TT_FLAT);
rend_SetColorModel(CM_RGB);
rend_SetZBufferState(1);
rend_SetGammaValue(gpu_preferred_state.gamma);
OpenGL_last_bound[0] = 9999999;
OpenGL_last_bound[1] = 9999999;
OpenGL_multitexture_state = false;
dglEnable(GL_SCISSOR_TEST);
dglScissor(0, 0, gpu_state.screen_width, gpu_state.screen_height);
dglDisable(GL_SCISSOR_TEST);
gpu_BindTexture(BAD_BITMAP_HANDLE, MAP_TYPE_BITMAP, 0);
gpu_BindTexture(BAD_BITMAP_HANDLE, MAP_TYPE_BITMAP, 1);
dglActiveTexture(GL_TEXTURE0_ARB + 1);
gRenderer->setTextureEnabled(1, false);
dglEnable(GL_BLEND);
dglEnable(GL_DITHER);
dglBlendFunc(GL_DST_COLOR, GL_ZERO);
dglActiveTexture(GL_TEXTURE0_ARB + 0);
}
extern renderer_preferred_state Render_preferred_state;
int opengl_Setup(oeApplication *app, const int *width, const int *height) {
int winw = Video_res_list[Current_video_resolution_id].width;
int winh = Video_res_list[Current_video_resolution_id].height;
SDL_ClearError();
if (!SDL_WasInit(SDL_INIT_VIDEO)) {
const int rc = SDL_Init(SDL_INIT_VIDEO);
if (rc != 0) {
char buffer[512];
snprintf(buffer, sizeof(buffer), "SDL_GetError() reports \"%s\".\n", SDL_GetError());
fprintf(stderr, "SDL: SDL_Init() failed! rc == (%d).\n", rc);
fprintf(stderr, "%s", buffer);
rend_SetErrorMessage(buffer);
return (0);
}
}
if (!Already_loaded) {
char gl_library[256];
int arg;
arg = FindArgChar("-gllibrary", 'g');
if (arg != 0) {
strcpy(gl_library, GameArgs[arg + 1]);
} else {
gl_library[0] = 0;
}
LOG_INFO.printf("OpenGL: Attempting to use \"%s\" for OpenGL",
gl_library[0] ? gl_library : "[system default library]");
// ryan's adds. 04/18/2000...SDL stuff on 04/25/2000
bool success = true;
OpenGLDLLHandle = LoadOpenGLDLL(gl_library);
if (!(OpenGLDLLHandle)) {
// rcg07072000 last ditch effort...
#if defined(POSIX)
strcpy(gl_library, "libGL.so.1");
#else
strcpy(gl_library, "opengl32.dll");
#endif
OpenGLDLLHandle = LoadOpenGLDLL(gl_library);
if (!(OpenGLDLLHandle)) {
success = false;
}
} // if
if (!success) {
char buffer[512];
snprintf(buffer, sizeof(buffer), "Failed to load library [%s].\n", gl_library);
fprintf(stderr, "%s", buffer);
rend_SetErrorMessage(buffer);
return 0;
} // if
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
if (!GSDLWindow) {
int display_num = 0;
int display_arg = FindArg("-display");
int display_count = 0;
// High-DPI support
{
float scale = SDL_GetDisplayContentScale(Display_id);
LOG_WARNING.printf("Using content scale %f", scale);
winw = std::floor(static_cast<float>(winw) * scale);
winh = std::floor(static_cast<float>(winh) * scale);
}
SDL_PropertiesID props = SDL_CreateProperties();
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Descent 3");
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_UNDEFINED_DISPLAY(Display_id));
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_UNDEFINED_DISPLAY(Display_id));
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, winw);
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, winh);
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, SDL_WINDOW_OPENGL);
GSDLWindow = SDL_CreateWindowWithProperties(props);
SDL_DestroyProperties(props);
if (!GSDLWindow) {
LOG_ERROR.printf("OpenGL: SDL window creation failed: %s", SDL_GetError());
return 0;
}
bool grabMouse = FindArgChar("-nomousegrab", 'm') == 0;
SDL_SetWindowRelativeMouseMode(GSDLWindow, grabMouse);
SDL_SetWindowKeyboardGrab(GSDLWindow, grabMouse);
rend_SetFullScreen(Game_fullscreen);
} else if (!Game_fullscreen) {
SDL_SetWindowSize(GSDLWindow, winw, winh);
}
if (!GSDLGLContext) {
GSDLGLContext = SDL_GL_CreateContext(GSDLWindow);
if (!GSDLGLContext) {
LOG_ERROR.printf("OpenGL: OpenGL context creation failed: %s", SDL_GetError());
SDL_DestroyWindow(GSDLWindow);
GSDLWindow = nullptr;
return 0;
}
}
try {
LoadGLFnPtrs();
} catch (std::exception const &ex) {
// TODO: more raii-esque construction and cleanup here
SDL_GL_DestroyContext(GSDLGLContext);
GSDLGLContext = nullptr;
SDL_DestroyWindow(GSDLWindow);
GSDLWindow = nullptr;
LOG_ERROR.printf("Error loading opengl dll: %s", ex.what());
mod_FreeModule(&OpenGLDLLInst);
OpenGLDLLHandle = nullptr;
return 0;
}
// clear the window framebuffer to start.
dglClearColor(0.0f, 0.0f, 0.0f, 1.0f);
dglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(GSDLWindow);
/* Tear down the backbuffer and rebuild at new dimensions... */
if (GOpenGLFBO) {
dglBindFramebuffer(GL_FRAMEBUFFER, GOpenGLFBO);
dglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
dglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
dglBindRenderbuffer(GL_RENDERBUFFER, 0);
dglBindFramebuffer(GL_FRAMEBUFFER, 0);
dglDeleteFramebuffers(1, &GOpenGLFBO);
dglDeleteRenderbuffers(1, &GOpenGLRBOColor);
dglDeleteRenderbuffers(1, &GOpenGLRBODepth);
GOpenGLFBOWidth = GOpenGLFBOHeight = GOpenGLFBO = GOpenGLRBOColor = GOpenGLRBODepth = 0;
}
const GLsizei w = (GLsizei)*width;
const GLsizei h = (GLsizei)*height;
GOpenGLFBOWidth = w;
GOpenGLFBOHeight = h;
dglGenFramebuffers(1, &GOpenGLFBO);
dglBindFramebuffer(GL_FRAMEBUFFER, GOpenGLFBO);
dglGenRenderbuffers(1, &GOpenGLRBOColor);
dglBindRenderbuffer(GL_RENDERBUFFER, GOpenGLRBOColor);
dglRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h);
dglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, GOpenGLRBOColor);
dglGenRenderbuffers(1, &GOpenGLRBODepth);
dglBindRenderbuffer(GL_RENDERBUFFER, GOpenGLRBODepth);
dglRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, w, h);
dglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, GOpenGLRBODepth);
if (dglCheckFramebufferStatus(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) {
LOG_WARNING << "OpenGL: our framebuffer object is incomplete, giving up";
dglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
dglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
dglBindRenderbuffer(GL_RENDERBUFFER, 0);
dglBindFramebuffer(GL_FRAMEBUFFER, 0);
dglDeleteFramebuffers(1, &GOpenGLFBO);
dglDeleteRenderbuffers(1, &GOpenGLRBOColor);
dglDeleteRenderbuffers(1, &GOpenGLRBODepth);
GOpenGLFBO = GOpenGLRBOColor = GOpenGLRBODepth = 0;
SDL_GL_DestroyContext(GSDLGLContext);
SDL_DestroyWindow(GSDLWindow);
GSDLGLContext = nullptr;
GSDLWindow = nullptr;
return 0;
}
if (ParentApplication) {
reinterpret_cast<oeLnxApplication *>(ParentApplication)->set_sizepos(0, 0, *width, *height);
}
gRenderer.emplace();
Already_loaded = 1;
return 1;
}
// Sets up our OpenGL rendering context
// Returns 1 if ok, 0 if something bad
int opengl_Init(oeApplication *app, renderer_preferred_state *pref_state) {
int width, height;
int retval = 1;
int i;
LOG_INFO << "Setting up opengl mode!";
if (pref_state) {
gpu_preferred_state = *pref_state;
}
if (app != nullptr) {
ParentApplication = app;
}
/***********************************************************
* LINUX OPENGL
***********************************************************
*/
// Setup gpu_state.screen_width & gpu_state.screen_height & width & height
width = gpu_preferred_state.width;
height = gpu_preferred_state.height;
if (!opengl_Setup(app, &width, &height)) {
opengl_Close();
return 0;
}
memset(&gpu_state, 0, sizeof(rendering_state));
gpu_state.screen_width = width;
gpu_state.screen_height = height;
// Get some info
opengl_GetInformation();
// Do we have packed pixel formats?
OpenGL_packed_pixels = opengl_CheckExtension("GL_EXT_packed_pixels");
opengl_InitCache();
if (OpenGL_packed_pixels) {
opengl_packed_Upload_data = mem_rmalloc<uint16_t>(2048 * 2048);
opengl_packed_Translate_table = mem_rmalloc<uint16_t>(65536);
opengl_packed_4444_translate_table = mem_rmalloc<uint16_t>(65536);
ASSERT(opengl_packed_Upload_data);
ASSERT(opengl_packed_Translate_table);
ASSERT(opengl_packed_4444_translate_table);
LOG_INFO << "Building packed OpenGL translate table...";
for (i = 0; i < 65536; i++) {
int r = (i >> 10) & 0x1f;
int g = (i >> 5) & 0x1f;
int b = i & 0x1f;
#ifdef BRIGHTNESS_HACK
r *= BRIGHTNESS_HACK;
g *= BRIGHTNESS_HACK;
b *= BRIGHTNESS_HACK;
if (r > 0x1F)
r = 0x1F;
if (g > 0x1F)
g = 0x1F;
if (b > 0x1F)
b = 0x1F;
#endif
uint16_t pix;
if (!(i & OPAQUE_FLAG)) {
pix = 0;
} else {
pix = (r << 11) | (g << 6) | (b << 1) | 1;
}
opengl_packed_Translate_table[i] = INTEL_INT(pix);
// 4444 table
int a = (i >> 12) & 0xf;
r = (i >> 8) & 0xf;
g = (i >> 4) & 0xf;
b = i & 0xf;
pix = (r << 12) | (g << 8) | (b << 4) | a;
opengl_packed_4444_translate_table[i] = INTEL_INT(pix);
}
} else {
opengl_Upload_data = mem_rmalloc<uint32_t>(2048 * 2048);
opengl_Translate_table = mem_rmalloc<uint32_t>(65536);
opengl_4444_translate_table = mem_rmalloc<uint32_t>(65536);
ASSERT(opengl_Upload_data);
ASSERT(opengl_Translate_table);
ASSERT(opengl_4444_translate_table);
LOG_INFO << "Building OpenGL translate table...";
for (i = 0; i < 65536; i++) {
uint32_t pix;
int r = (i >> 10) & 0x1f;
int g = (i >> 5) & 0x1f;
int b = i & 0x1f;
#ifdef BRIGHTNESS_HACK
r *= BRIGHTNESS_HACK;
g *= BRIGHTNESS_HACK;
b *= BRIGHTNESS_HACK;
if (r > 0x1F)
r = 0x1F;
if (g > 0x1F)
g = 0x1F;
if (b > 0x1F)
b = 0x1F;
#endif
float fr = (float)r / 31.0f;
float fg = (float)g / 31.0f;
float fb = (float)b / 31.0f;
r = 255 * fr;
g = 255 * fg;
b = 255 * fb;
if (!(i & OPAQUE_FLAG)) {
pix = 0;
} else {
pix = (255 << 24) | (b << 16) | (g << 8) | (r);
}
opengl_Translate_table[i] = INTEL_INT(pix);
// Do 4444
int a = (i >> 12) & 0xf;
r = (i >> 8) & 0xf;
g = (i >> 4) & 0xf;
b = i & 0xf;
float fa = (float)a / 15.0f;
fr = (float)r / 15.0f;
fg = (float)g / 15.0f;
fb = (float)b / 15.0f;
a = 255 * fa;
r = 255 * fr;
g = 255 * fg;
b = 255 * fb;
pix = (a << 24) | (b << 16) | (g << 8) | (r);
opengl_4444_translate_table[i] = INTEL_INT(pix);
}
}
opengl_SetDefaults();
g3_ForceTransformRefresh();
CHECK_ERROR(4)
gpu_state.initted = 1;
LOG_INFO.printf("OpenGL initialization at %d x %d was successful.", width, height);
return retval;
}
// Releases the rendering context
void opengl_Close(const bool just_resizing) {
CHECK_ERROR(5)
dglDeleteTextures(textures_.size(), textures_.data());
textures_.clear();
gRenderer.reset();
if (GSDLGLContext) {
SDL_GL_MakeCurrent(nullptr, nullptr);
SDL_GL_DestroyContext(GSDLGLContext);
GSDLGLContext = nullptr;
GOpenGLFBOWidth = GOpenGLFBOHeight = GOpenGLFBO = GOpenGLRBOColor = GOpenGLRBODepth = 0;
}
if (!just_resizing && GSDLWindow) {
SDL_DestroyWindow(GSDLWindow);
GSDLWindow = nullptr;
}
if (OpenGL_packed_pixels) {
if (opengl_packed_Upload_data) {
mem_free(opengl_packed_Upload_data);
}
if (opengl_packed_Translate_table) {
mem_free(opengl_packed_Translate_table);
}
if (opengl_packed_4444_translate_table) {
mem_free(opengl_packed_4444_translate_table);
}
opengl_packed_Upload_data = nullptr;
opengl_packed_Translate_table = nullptr;
opengl_packed_4444_translate_table = nullptr;
} else {
if (opengl_Upload_data)
mem_free(opengl_Upload_data);
if (opengl_Translate_table)
mem_free(opengl_Translate_table);
if (opengl_4444_translate_table)
mem_free(opengl_4444_translate_table);
opengl_Upload_data = nullptr;
opengl_Translate_table = nullptr;
opengl_4444_translate_table = nullptr;
}
if (OpenGL_cache_initted) {
mem_free(OpenGL_lightmap_remap);
mem_free(OpenGL_bitmap_remap);
mem_free(OpenGL_lightmap_states);
mem_free(OpenGL_bitmap_states);
OpenGL_cache_initted = 0;
}
// mod_FreeModule (OpenGLDLLHandle);
gpu_state.initted = 0;
}
// Takes our 16bit format and converts it into the memory scheme that OpenGL wants
void opengl_TranslateBitmapToOpenGL(int texnum, int bm_handle, int map_type, int replace, int tn) {
uint16_t *bm_ptr;
int w, h;
int size;
dglActiveTexture(GL_TEXTURE0_ARB + tn);
if (map_type == MAP_TYPE_LIGHTMAP) {
if (GameLightmaps[bm_handle].flags & LF_BRAND_NEW)
replace = 0;
bm_ptr = lm_data(bm_handle);
GameLightmaps[bm_handle].flags &= ~(LF_CHANGED | LF_BRAND_NEW);
w = lm_w(bm_handle);
h = lm_h(bm_handle);
size = GameLightmaps[bm_handle].square_res;
} else {
if (GameBitmaps[bm_handle].flags & BF_BRAND_NEW)
replace = 0;
bm_ptr = bm_data(bm_handle, 0);
GameBitmaps[bm_handle].flags &= ~(BF_CHANGED | BF_BRAND_NEW);
w = bm_w(bm_handle, 0);
h = bm_h(bm_handle, 0);
size = w;
}
if (OpenGL_last_bound[tn] != texnum) {
dglBindTexture(GL_TEXTURE_2D, texnum);
OpenGL_sets_this_frame[0]++;
OpenGL_last_bound[tn] = texnum;
}
if (OpenGL_packed_pixels) {
if (map_type == MAP_TYPE_LIGHTMAP) {
uint16_t *left_data = (uint16_t *)opengl_packed_Upload_data;
int bm_left = 0;
for (int i = 0; i < h; i++, left_data += size, bm_left += w) {
uint16_t *dest_data = left_data;
for (int t = 0; t < w; t++) {
*dest_data++ = opengl_packed_Translate_table[bm_ptr[bm_left + t]];
}
}
if (replace) {
dglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
opengl_packed_Upload_data);
} else {
dglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, size, size, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
opengl_packed_Upload_data);
}
} else {
int limit = 0;
if (bm_mipped(bm_handle)) {
limit = NUM_MIP_LEVELS + 3;
} else {
limit = 1;
}
for (int m = 0; m < limit; m++) {
if (m < NUM_MIP_LEVELS) {
bm_ptr = bm_data(bm_handle, m);
w = bm_w(bm_handle, m);
h = bm_h(bm_handle, m);
} else {
bm_ptr = bm_data(bm_handle, NUM_MIP_LEVELS - 1);
w = bm_w(bm_handle, NUM_MIP_LEVELS - 1);
h = bm_h(bm_handle, NUM_MIP_LEVELS - 1);
w >>= m - (NUM_MIP_LEVELS - 1);
h >>= m - (NUM_MIP_LEVELS - 1);
if ((w < 1) || (h < 1))
continue;
}
if (bm_format(bm_handle) == BITMAP_FORMAT_4444) {
// Do 4444
if (bm_mipped(bm_handle)) {
for (int i = 0; i < w * h; i++)
opengl_packed_Upload_data[i] = 0xf | (opengl_packed_4444_translate_table[bm_ptr[i]]);
} else {
for (int i = 0; i < w * h; i++)
opengl_packed_Upload_data[i] = opengl_packed_4444_translate_table[bm_ptr[i]];
}
if (replace) {
dglTexSubImage2D(GL_TEXTURE_2D, m, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
opengl_packed_Upload_data);
} else {
dglTexImage2D(GL_TEXTURE_2D, m, GL_RGBA4, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
opengl_packed_Upload_data);
}
} else {
// Do 1555
for (int i = 0; i < w * h; i++) {
opengl_packed_Upload_data[i] = opengl_packed_Translate_table[bm_ptr[i]];
}
if (replace) {
dglTexSubImage2D(GL_TEXTURE_2D, m, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
opengl_packed_Upload_data);
} else {
dglTexImage2D(GL_TEXTURE_2D, m, GL_RGB5_A1, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
opengl_packed_Upload_data);
}
}
}
}
} else {
if (map_type == MAP_TYPE_LIGHTMAP) {
uint32_t *left_data = (uint32_t *)opengl_Upload_data;
int bm_left = 0;
for (int i = 0; i < h; i++, left_data += size, bm_left += w) {
uint32_t *dest_data = left_data;
for (int t = 0; t < w; t++) {
*dest_data++ = opengl_Translate_table[bm_ptr[bm_left + t]];
}
}
if (size > 0) {
if (replace) {
dglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
} else {
dglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
}
}
} else {
int limit = 0;
if (bm_mipped(bm_handle)) {
limit = NUM_MIP_LEVELS + 3; // ryan added +3.
} else {
limit = 1;
}
for (int m = 0; m < limit; m++) {
bm_ptr = bm_data(bm_handle, m);
w = bm_w(bm_handle, m);
h = bm_h(bm_handle, m);
if (bm_format(bm_handle) == BITMAP_FORMAT_4444) {
// Do 4444
if (bm_mipped(bm_handle)) {
for (int i = 0; i < w * h; i++)
opengl_Upload_data[i] = INTEL_INT((255 << 24)) | opengl_4444_translate_table[bm_ptr[i]];
} else {
for (int i = 0; i < w * h; i++)
opengl_Upload_data[i] = opengl_4444_translate_table[bm_ptr[i]];
}
} else {
// Do 1555
for (int i = 0; i < w * h; i++)
opengl_Upload_data[i] = opengl_Translate_table[bm_ptr[i]];
}
// rcg06262000 my if wrapper.
if ((w > 0) && (h > 0)) {
if (replace) {
dglTexSubImage2D(GL_TEXTURE_2D, m, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
} else {
dglTexImage2D(GL_TEXTURE_2D, m, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
}
}
}
}
}
// mprintf(1,"Doing slow upload to opengl!\n");
if (map_type == MAP_TYPE_LIGHTMAP) {
GameLightmaps[bm_handle].flags &= ~LF_LIMITS;
}
CHECK_ERROR(6)
OpenGL_uploads++;
}
extern bool Force_one_texture;
// Utilizes a LRU cacheing scheme to select/upload textures the opengl driver
int opengl_MakeBitmapCurrent(int handle, int map_type, int tn) {
int w, h;
int texnum;
if (map_type == MAP_TYPE_LIGHTMAP) {
w = GameLightmaps[handle].square_res;
h = GameLightmaps[handle].square_res;
} else {
if (Force_one_texture) {
handle = 0;
}
w = bm_w(handle, 0);
h = bm_h(handle, 0);
}
if (w != h) {
LOG_WARNING << "Can't use non-square textures with OpenGL!";
return 0;
}
// See if the bitmaps is already in the cache
if (map_type == MAP_TYPE_LIGHTMAP) {
if (OpenGL_lightmap_remap[handle] == 65535) {
texnum = opengl_MakeTextureObject(tn);
SET_WRAP_STATE(OpenGL_lightmap_states[handle], 1);
SET_FILTER_STATE(OpenGL_lightmap_states[handle], 0);
OpenGL_lightmap_remap[handle] = texnum;
opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 0, tn);
} else {
texnum = OpenGL_lightmap_remap[handle];
if (GameLightmaps[handle].flags & LF_CHANGED)
opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 1, tn);
}
} else {
if (OpenGL_bitmap_remap[handle] == 65535) {
texnum = opengl_MakeTextureObject(tn);
SET_WRAP_STATE(OpenGL_bitmap_states[handle], 1);
SET_FILTER_STATE(OpenGL_bitmap_states[handle], 0);
OpenGL_bitmap_remap[handle] = texnum;
opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 0, tn);
} else {
texnum = OpenGL_bitmap_remap[handle];
if (GameBitmaps[handle].flags & BF_CHANGED) {
opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 1, tn);
}
}