-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine.cpp
4076 lines (3027 loc) · 132 KB
/
engine.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 <gl/glew.h>
#include <gl/gl.h>
#include "engine.hpp"
#include <math.h>
#include <gl/glext.h>
#include "clstate.h"
#include <iostream>
#include <gl/gl.h>
//#include "obj_mem_manager.hpp"
#include <stdio.h>
#include <limits.h>
//#include "texture_manager.hpp"
#include "interact_manager.hpp"
#include "text_handler.hpp"
#include "point_cloud.hpp"
//#include "hologram.hpp"
#include "vec.hpp"
//#include "ui_manager.hpp"
#include <chrono>
#include "ocl.h"
#include "controls.hpp"
#include "logging.hpp"
#ifdef RIFT
#include "Rift/Include/OVR.h"
#include "Rift/Include/OVR_Kernel.h"
#include "Rift/Src/OVR_CAPI.h"
#include "Rift/Src/OVR_Stereo.h"
#endif
#define FOV_CONST 500.0f
#include "cl_gl_interop_texture.hpp"
#include "texture.hpp"
bool rift::enabled = false;
///rift
ovrHmd rift::HMD = 0;
ovrEyeRenderDesc rift::EyeRenderDesc[2];
ovrFovPort rift::eyeFov[2];
ovrPosef rift::eyeRenderPose[2];
ovrTrackingState rift::HmdState;
cl_float4 rift::eye_position[2] = {0};
cl_float4 rift::eye_rotation[2] = {0};
///need to fetch these from util, will change per lense
cl_float4 rift::distortion_constants = {1.0f, 0.22f, 0.24f, 0.0f};
/*distortions[numDistortions].Config.Eqn = Distortion_CatmullRom10;
distortions[numDistortions].Config.K[0] = 1.003f;
distortions[numDistortions].Config.K[1] = 1.02f;
distortions[numDistortions].Config.K[2] = 1.042f;
distortions[numDistortions].Config.K[3] = 1.066f;*/
//cl_float4 rift::distortion_constants = {1.003f, 1.02f, 1.0422f, 1.066f};
cl_float rift::distortion_scale = {0};
cl_float4 rift::abberation_constants = {1};
//compute::opengl_renderbuffer engine::g_screen;
///this needs changing
///opengl ids
unsigned int engine::gl_framebuffer_id=-1;
unsigned int engine::gl_reprojected_framebuffer_id=0;
unsigned int engine::gl_screen_id=0;
unsigned int engine::gl_rift_screen_id[2]={0};
unsigned int engine::gl_smoothed_framebuffer_id=0;
///the empty light buffer, and number of shadow lights
cl_uint* engine::blank_light_buf;
///gpuside light buffer, and lightmap cubemap resolution
compute::buffer engine::g_shadow_light_buffer;
compute::buffer engine::g_static_shadow_light_buffer;
cl_uint engine::l_size;
cl_uint engine::height;
cl_uint engine::width;
cl_uint engine::depth;
cl_float4 engine::c_pos;
cl_float4 engine::c_rot;
cl_float4 engine::c_rot_keyboard_only;
int engine::nbuf=0; ///which depth buffer are we using?
compute::buffer engine::g_ui_id_screen;
float depth_far = 350000;
std::unordered_map<std::string, std::map<int, const void*>> kernel_map;
float idcalc(float value)
{
return value * depth_far;
}
Timer::Timer(const std::string& n) : name(n), stopped(false){clk.restart();}
void Timer::stop()
{
float time = clk.getElapsedTime().asMicroseconds() / 1000.f;
std::cout << name << " " << time << std::endl;
stopped = true;
}
float calculate_fov_constant_from_hfov(float horizontal_fov_degrees, float screenwidth)
{
float fov_radians = (horizontal_fov_degrees / 360.f) * 2 * M_PI;
///fov_const, screenwidth/2, and whatever that third length is called form a right angled triangle triangle
float triangle_angle = fov_radians / 2;
///tan o = screenwidth/2 / fov_const
///1/tan = fov_const / screenwidth/2
///fov_const = screenwidth/2 / tan
float fov_constant = (screenwidth/2) / tan(triangle_angle);
return fov_constant;
}
compute::opengl_renderbuffer engine::gen_cl_gl_framebuffer_renderbuffer(GLuint* renderbuffer_id, int w, int h)
{
///OpenGL is literally the worst API
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
PFNGLGENRENDERBUFFERSEXTPROC glGenRenderbuffersEXT = (PFNGLGENRENDERBUFFERSEXTPROC)wglGetProcAddress("glGenRenderbuffersEXT");
PFNGLBINDRENDERBUFFEREXTPROC glBindRenderbufferEXT = (PFNGLBINDRENDERBUFFEREXTPROC)wglGetProcAddress("glBindRenderbufferEXT");
PFNGLRENDERBUFFERSTORAGEEXTPROC glRenderbufferStorageEXT = (PFNGLRENDERBUFFERSTORAGEEXTPROC)wglGetProcAddress("glRenderbufferStorageEXT");
PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC glFramebufferRenderbufferEXT = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)wglGetProcAddress("glFramebufferRenderbufferEXT");
GLuint screen_id;
glGenRenderbuffersEXT(1, &screen_id);
glBindRenderbufferEXT(GL_RENDERBUFFER, screen_id);
///generate storage for renderbuffer
glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_RGBA16, w, h);
GLuint framebuf;
///get a framebuffer and bind it
glGenFramebuffersEXT(1, &framebuf);
glBindFramebufferEXT(GL_FRAMEBUFFER, framebuf);
///attach one to the other
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, screen_id);
//glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
//glBindRenderbufferEXT(GL_RENDERBUFFER, 0);
///have opencl nab this and store in g_screen
compute::opengl_renderbuffer buf = compute::opengl_renderbuffer(cl::context, screen_id, compute::memory_object::read_write);
if(renderbuffer_id != NULL)
*renderbuffer_id = framebuf;
return buf;
}
compute::buffer engine::make_screen_buffer(int element_size)
{
return compute::buffer(cl::context, element_size*width*height, CL_MEM_READ_WRITE, nullptr);
}
///this method basically exists because my ide does not autocomplete the compute::buffer calls
///making it extremely tedious to remember how to create them
compute::buffer engine::make_read_write(int size, void* data)
{
auto buf = compute::buffer(cl::context, size, CL_MEM_READ_WRITE, nullptr);
if(data)
{
cl::cqueue.enqueue_write_buffer(buf, 0, size, data);
}
return buf;
}
void engine::request_close()
{
requested_close = true;
}
bool engine::is_requested_close()
{
return requested_close;
}
///fuck this shit I'm out
///does borderless window toggling and also returns the height of the bar at the top
int window_fuckery(sf::RenderWindow& window, int videowidth, int height, bool fullscreen)
{
#ifdef WIN32
window.setSize({videowidth, height});
window.setView(sf::View(sf::FloatRect(0, 0, videowidth, height)));
HWND handle = window.getSystemHandle();
RECT rcClient, rcWind;
POINT ptDiff;
GetClientRect(handle, &rcClient);
GetWindowRect(handle, &rcWind);
ptDiff.x = (rcWind.right - rcWind.left) - rcClient.right;
ptDiff.y = (rcWind.bottom - rcWind.top) - rcClient.bottom;
int yheight = ptDiff.y;
LONG_PTR lStyle = GetWindowLongPtr(handle, GWL_STYLE);
LONG_PTR lExStyle = GetWindowLongPtr(handle, GWL_EXSTYLE);
LONG_PTR style_mod = (WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
LONG_PTR exstyle_mod = (WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
if(fullscreen)
{
lStyle &= ~style_mod;
SetWindowLongPtr(handle, GWL_STYLE, lStyle);
lExStyle &= ~exstyle_mod;
//SetWindowLongPtr(handle, GWL_EXSTYLE, lExStyle);
}
else
{
SetWindowLongPtr(handle, GWL_STYLE, WS_CAPTION | WS_VISIBLE | WS_THICKFRAME | WS_SIZEBOX | WS_SYSMENU | WS_MINIMIZEBOX);
}
return yheight;
#else // WIN32
return 0;
#endif
}
void engine::set_fov(float phorizontal_fov_degrees)
{
horizontal_fov_degrees = phorizontal_fov_degrees;
}
void resize_window(int width, int height, bool fullscreen, engine& window)
{
int yheight;
window_fuckery(window.window, width, height, fullscreen);
///I don't know. I dont know why we have to do this twice, it just fixes absolutely everything ever
///Why? Who knows. SFML seems to have a heart attack if you just call setview/setsize again for a second time
///without doing all the appropriate window fiddling afterwards, even though nothing has changed
///and we don't even call setwindow pos. Who knows. This probably wont work on nvidia (kill me)
yheight = window_fuckery(window.window, width, height, fullscreen);
///problem is,we setwindowpos here which applies the above updates, which means that our titlebar is no longer valid
if(window.loaded && fullscreen)
{
window.window.setPosition({0, -yheight});
}
if(window.loaded && !fullscreen)
{
window.window.setPosition({10, 10});
}
sf::Image img;
img.create(32, 32);
sf::Texture tex;
tex.loadFromImage(img);
sf::Sprite spr(tex);
window.window.draw(spr);
}
void engine::load(cl_uint pwidth, cl_uint pheight, cl_uint pdepth, const std::string& name, const std::string& loc, bool only_3d, bool fullscreen)
{
#ifdef RIFT
ovr_Initialize();
#endif
cl_float2 old_win_pos = {100, 100};
///preserve window position
if(loaded)
{
old_win_pos = {window.getPosition().x, window.getPosition().y};
}
if(fullscreen)
{
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
pwidth = desktop.width;
pheight = desktop.height;
}
width = pwidth;
height = pheight;
depth = pdepth;
old_time = 0;
current_time = 0;
#ifdef RIFT
{
using namespace rift;
if(!HMD)
{
bool state = true;
HMD = ovrHmd_Create(0);
if(!HMD)
{
printf("No oculus rift detected, check oculus configurator\n");
state = false;
}
else if(HMD->ProductName[0] == '\0')
{
printf("Oculus display not enabled ( ?? )\n");
state = false;
}
rift::enabled = state;
}
if(rift::enabled)
{
printf("Oculus rift support enabled\n");
ovrHmd_SetEnabledCaps(HMD, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
// Start the sensor which informs of the Rift's pose and motion
ovrHmd_ConfigureTracking(HMD, ovrTrackingCap_Orientation |
ovrTrackingCap_MagYawCorrection |
ovrTrackingCap_Position, 0);
eyeFov[0] = HMD->DefaultEyeFov[0];
eyeFov[1] = HMD->DefaultEyeFov[1];
EyeRenderDesc[0] = ovrHmd_GetRenderDesc(HMD, (ovrEyeType) 0, eyeFov[0]);
EyeRenderDesc[1] = ovrHmd_GetRenderDesc(HMD, (ovrEyeType) 1, eyeFov[1]);
Sizei recommendedTex0Size = ovrHmd_GetFovTextureSize(HMD, ovrEye_Left, HMD->DefaultEyeFov[0], 1.0f);
Sizei recommendedTex1Size = ovrHmd_GetFovTextureSize(HMD, ovrEye_Right, HMD->DefaultEyeFov[1], 1.0f);
///do this properly?
width = recommendedTex0Size.w;
height = recommendedTex0Size.h;
width *= 2;//?
HMDInfo info = CreateDebugHMDInfo(HmdType_DK2);
HmdRenderInfo rinfo = GenerateHmdRenderInfoFromHmdInfo(info, NULL, Distortion_RecipPoly4, EyeCup_LAST);
float eye_relief_in_meters = rinfo.EyeLeft.ReliefInMeters;
LensConfig lens = GenerateLensConfigFromEyeRelief(eye_relief_in_meters, rinfo, Distortion_RecipPoly4);
float max_radius = lens.MaxR;
printf("MR: %f\n", max_radius);
for(int i=0; i<4; i++)
{
rift::distortion_constants.s[i] = lens.K[i];
abberation_constants.s[i] = lens.ChromaticAberration[i];
printf("%f\n", lens.K[i]);
}
}
}
#endif
///in case I need to do scaling for oculus
int videowidth = width;
//printf("Initialised with width %i and height %i\n", videowidth, height);
lg::log("Trying to initialise with width ", videowidth, " and height ", height);
int yheight = 0;
///now AKA borderless window mode, but its a massive hack to get this to work
bool do_resize_hack = true;
bool do_title_resize_operations = false;
#ifdef WIN32
if(!loaded || !do_resize_hack)
#endif // WIN32
{
///window.create might invalidate the context
#ifdef OCULUS
window.create(sf::VideoMode(videowidth, height), name, sf::Style::Fullscreen);
#else
if(!fullscreen)
window.create(sf::VideoMode(videowidth, height), name);
if(fullscreen && !do_resize_hack)
window.create(sf::VideoMode::getDesktopMode(), name, sf::Style::Fullscreen);
if(fullscreen && do_resize_hack)
window.create(sf::VideoMode(videowidth, height), name);
#endif
}
///for this to work, sfml would need to support going fullscreen without recreating context
#ifdef WIN32
else
{
window_fuckery(window, videowidth, height, fullscreen);
///I don't know. I dont know why we have to do this twice, it just fixes absolutely everything ever
///Why? Who knows. SFML seems to have a heart attack if you just call setview/setsize again for a second time
///without doing all the appropriate window fiddling afterwards, even though nothing has changed
///and we don't even call setwindow pos. Who knows. This probably wont work on nvidia (kill me)
yheight = window_fuckery(window, videowidth, height, fullscreen);
}
#endif // WIN32
///so that the default window initialised will get the maximisation thing disabled properly, also if we're booting in fullscreen
#ifdef WIN32
window_fuckery(window, videowidth, height, fullscreen);
#endif // WIN32
lg::log("Successful init");
is_fullscreen = fullscreen;
///imgui mouse position is off for some reason
if(loaded && !fullscreen)
{
window.setPosition({old_win_pos.x, old_win_pos.y});
}
///problem is, we setwindowpos here which applies the above updates, which means that our titlebar is no longer valid
///fix alt tabbing
///Why does this fix alt tabbing you ask?
///I'm not sure. I think its because we're tinkering around with the internals of SFML
if(do_resize_hack)
{
resize_window(width-1, height-1, false, *this);
resize_window(width, height, fullscreen, *this);
}
///passed in as compilation parameter to opencl
l_size = 1024;
float fov = calculate_fov_constant_from_hfov(horizontal_fov_degrees, width);
std::string fov_str = std::to_string(fov) + "f";
append_opencl_extra_command_line("-D FOV_CONST=" + fov_str);
lg::log("fov angle degrees ", horizontal_fov_degrees, " fov_const ", fov, " fov_str " + fov_str);
sf::Clock ocltime;
lg::log("pre opencl");
reset_program_built();
///including opencl compilation parameters
if(!loaded)
oclstuff(loc, width, height, l_size, only_3d, opencl_extra_command_line);
else
{
//build(loc, width, height, l_size, only_3d);
cl::cqueue.finish();
cl::cqueue2.finish();
cl::cqueue_ooo.finish();
//oclstuff(loc, width, height, l_size, only_3d, opencl_extra_command_line);
//build(loc, width, height, l_size, only_3d, opencl_extra_command_line);
#ifdef WIN32
if(do_resize_hack)
build_thread = std::thread(build, loc, width, height, l_size, only_3d, opencl_extra_command_line);
#endif
#ifdef WIN32
if(!do_resize_hack)
#endif
oclstuff(loc, width, height, l_size, only_3d, opencl_extra_command_line);
}
lg::log("post opencl");
lg::log("Does this device support opengl interop: ", supports_extension("cl_khr_gl_sharing"));
//printf("kernel compilation time: %f\n", ocltime.getElapsedTime().asMicroseconds() / 1000.f);
lg::log("kernel compilation time: ", ocltime.getElapsedTime().asMicroseconds() / 1000.f);
mdx = 0;
mdy = 0;
cmx = 0;
cmy = 0;
blank_light_buf=NULL;
c_pos.x=0;
c_pos.y=0;
c_pos.z=0;
c_rot.x=0;
c_rot.y=0;
c_rot.z=0;
c_rot_keyboard_only = {0,0,0};
///frame lookahead
max_render_events = 2;
render_events_num = 0;
render_me = false;
last_frametype = frametype::REPROJECT; ///so that the first frame is a triangle render
running_frametime_smoothed = 0;
cl_float4 *blank = new cl_float4[width*height];
memset(blank, 0, width*height*sizeof(cl_float4));
cl_uint *arr = new cl_uint[width*height];
memset(arr, UINT_MAX, width*height*sizeof(cl_uint));
#ifdef DEBUGGING
d_depth_buf = new cl_uint[width*height];
#endif
lg::log("Pre opengl ptrs");
///opengl is the best, getting function ptrs
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
PFNGLGENRENDERBUFFERSEXTPROC glGenRenderbuffersEXT = (PFNGLGENRENDERBUFFERSEXTPROC)wglGetProcAddress("glGenRenderbuffersEXT");
PFNGLBINDRENDERBUFFEREXTPROC glBindRenderbufferEXT = (PFNGLBINDRENDERBUFFEREXTPROC)wglGetProcAddress("glBindRenderbufferEXT");
PFNGLRENDERBUFFERSTORAGEEXTPROC glRenderbufferStorageEXT = (PFNGLRENDERBUFFERSTORAGEEXTPROC)wglGetProcAddress("glRenderbufferStorageEXT");
PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC glFramebufferRenderbufferEXT = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)wglGetProcAddress("glFramebufferRenderbufferEXT");
lg::log("Post opengl ptrs");
///generate and bind renderbuffers
/*if(!rift::enabled)
{
if(gl_framebuffer_id != -1)
{
//compute::opengl_enqueue_release_gl_objects(1, &g_screen.get(), cl::cqueue);
}
g_screen = gen_cl_gl_framebuffer_renderbuffer(&gl_framebuffer_id, width, height);
compute::opengl_enqueue_acquire_gl_objects(1, &g_screen.get(), cl::cqueue);
}
else
{
for(int i=0; i<2; i++)
{
g_rift_screen[i] = gen_cl_gl_framebuffer_renderbuffer(&gl_rift_screen_id[i], width, height);
compute::opengl_enqueue_acquire_gl_objects(1, &g_rift_screen[i].get(), cl::cqueue);
}
}*/
//lg::log("Acquired shared main opengl screen renderwindow");
//g_screen_reprojected = gen_cl_gl_framebuffer_renderbuffer(&gl_reprojected_framebuffer_id, width, height);
//g_screen_edge_smoothed = gen_cl_gl_framebuffer_renderbuffer(&gl_smoothed_framebuffer_id, width, height);
//? compute::opengl_enqueue_acquire_gl_objects(1, &g_screen.get(), cl::cqueue);
//compute::opengl_enqueue_acquire_gl_objects(1, &g_screen_reprojected.get(), cl::cqueue);
//compute::opengl_enqueue_acquire_gl_objects(1, &g_screen_edge_smoothed.get(), cl::cqueue);
///this is a completely arbitrary size to store triangle uids in
cl_uint size_of_uid_buffer = 10*1024*1024;
cl_uint zero=0;
cl_float2* distortion_clear = new cl_float2[width*height];
memset(distortion_clear, 0, sizeof(cl_float2)*width*height);
///creates the two depth buffers and 2d triangle id buffer with size width*height
//depth_buffer[0] = compute::buffer(cl::context, sizeof(cl_uint)*width*height, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);
//depth_buffer[1] = compute::buffer(cl::context, sizeof(cl_uint)*width*height, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);
//reprojected_depth_buffer[0] = compute::buffer(cl::context, sizeof(cl_uint)*width*height, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);
//reprojected_depth_buffer[1] = compute::buffer(cl::context, sizeof(cl_uint)*width*height, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);
lg::log("Pre cl alloc");
//g_tid_buf = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE, nullptr);
g_tid_buf = compute::buffer(cl::context, size_of_uid_buffer*sizeof(cl_uint), CL_MEM_READ_WRITE, NULL);
lg::log("Allocated g_tid_buf");
g_tid_buf_max_len = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &size_of_uid_buffer);
g_tid_buf_atomic_count = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &zero);
lg::log("Allocated g_tid/max_len/atomic_count");
//g_valid_fragment_num = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &zero);
//g_valid_fragment_mem = compute::buffer(cl::context, size_of_uid_buffer*sizeof(cl_uint), CL_MEM_READ_WRITE, NULL);
//g_ui_id_screen = compute::buffer(cl::context, width*height*sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);
//g_reprojected_ids = compute::buffer(cl::context, width*height*sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);
/*int tile_size = 32;
int tile_depth = 1000;
int tilew = ceil((float)width/tile_size);
int tileh = ceil((float)height/tile_size);
g_tile_information = compute::buffer(cl::context, (tilew+1)*(tileh+1)*sizeof(cl_float4)*tile_depth, CL_MEM_READ_WRITE, nullptr);
g_tile_count = compute::buffer(cl::context, (tilew+1)*(tileh+1)*sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, arr);*/
lg::log("Real uid buffer size ", g_tid_buf.size() / 1024 / 1024, " mb");
///length of the fragment buffer id thing, stored cpu side
c_tid_buf_len = size_of_uid_buffer;
///number of lights
//obj_mem_manager::g_light_num = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, &zero);
g_shadow_light_buffer = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &zero);
g_static_shadow_light_buffer = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &zero);
lg::log("post pseudo shadow light buffer");
compute::image_format format(CL_R, CL_UNSIGNED_INT32);
compute::image_format format_ids(CL_R, CL_UNSIGNED_INT32);
compute::image_format format_occ(CL_R, CL_FLOAT);
compute::image_format format_diffuse(CL_RGBA, CL_FLOAT);
dummy_buffer = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &zero);
dummy_image2d_t = compute::image2d(cl::context, CL_MEM_READ_WRITE, format, 64, 64, 0, NULL);
///screen ids as a uint32 texture
//g_id_screen_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_ids, width, height, 0, NULL);
//g_reprojected_id_screen_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_ids, width, height, 0, NULL);
//g_object_id_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format, width, height, 0, NULL);
//g_occlusion_intermediate_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_occ, width, height, 0, NULL);
//g_occlusion_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_occ, width, height, 0, NULL);
//g_diffuse_intermediate_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_diffuse, width, height, 0, NULL);
//g_diffuse_tex = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_diffuse, width, height, 0, NULL);
//g_distortion_buffer = compute::buffer(cl::context, sizeof(cl_float2)*width*height, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, distortion_clear);
//lg::log("post g_id_screen_tex");
tile_size = 64;
tile_tri_chunk_size = 256;
tile_num_w = ceil((float)width / tile_size);
tile_num_h = ceil((float)height / tile_size);
///max possible number of fragments is tri_num * tilew*tileh, but that number is huge so we have to do what we normally do
int display_list_num = tile_num_w*tile_num_h*tile_tri_chunk_size * 5;
cl_uint* default_slots = new cl_uint[tile_num_w*tile_num_h]();
for(int i=0; i<tile_num_w*tile_num_h; i++)
{
default_slots[i] = i;
}
cl_uint next_free_slot = tile_num_w*tile_num_h;
///triangle id
int display_list_element_size = sizeof(cl_uint);
/*g_tiled_counters = compute::buffer(cl::context, sizeof(cl_uint)*tile_num_w*tile_num_h, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, blank);
g_tiled_display_list = compute::buffer(cl::context, display_list_num * display_list_element_size, CL_MEM_READ_WRITE, nullptr);
g_tiled_tile_tracker = compute::buffer(cl::context, ceil((float)display_list_num / tile_tri_chunk_size) * sizeof(cl_uint));
g_tiled_currently_free_memory_slot = compute::buffer(cl::context, sizeof(cl_uint)*tile_num_w*tile_num_h, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, default_slots);
g_tiled_global_memory_slot_counter = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &next_free_slot);
g_tiled_global_count = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &zero);*/
delete [] default_slots;
///fixme
delete [] blank;
delete [] distortion_clear;
ready_to_flip = false;
///rift
//glEnable(GL_TEXTURE2D); ///?
if(!do_resize_hack)
raw_input_inited = false;
///ie if we're experimenting with context stuff, dont recreate the raw input as the context isn't invalid
if(!loaded && do_resize_hack)
raw_input_inited = false;
loaded = true;
suppress_resize = true;
lg::log("end engine::load()");
}
void engine::set_light_data(light_gpu& ldat)
{
light_data = &ldat;
}
///this is not a copy?
void engine::set_object_data(object_context_data& odat)
{
obj_data = &odat;
}
/*light* engine::add_light(light* l)
{
light* new_light = light::add_light(l);
realloc_light_gmem();
return new_light;
}
void engine::remove_light(light* l)
{
light::remove_light(l); ///realloc light gmem or?
}
void engine::set_light_pos(light* l, cl_float4 position)
{
l->pos.x = position.x;
l->pos.y = position.y;
l->pos.z = position.z;
}
void engine::g_flush_light(light* l) ///just position?
{
///flush light information to gpu
int lid = light::get_light_id(l);
cl::cqueue.enqueue_write_buffer(obj_mem_manager::g_light_mem, sizeof(light)*lid, sizeof(light), light::lightlist[lid]);
}*/
cl_float4 engine::rot_about(cl_float4 point, cl_float4 c_pos, cl_float4 c_rot)
{
/*cl_float4 cos_rot;
cos_rot.x = cos(c_rot.x);
cos_rot.y = cos(c_rot.y);
cos_rot.z = cos(c_rot.z);
cl_float4 sin_rot;
sin_rot.x = sin(c_rot.x);
sin_rot.y = sin(c_rot.y);
sin_rot.z = sin(c_rot.z);
cl_float4 ret;
ret.x= cos_rot.y*(sin_rot.z+cos_rot.z*(point.x-c_pos.x))-sin_rot.y*(point.z-c_pos.z);
ret.y= sin_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))+cos_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
ret.z= cos_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))-sin_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
ret.w = 0;*/
cl_float3 c;
c.x = cos(c_rot.x);
c.y = cos(c_rot.y);
c.z = cos(c_rot.z);
cl_float3 s;
s.x = sin(c_rot.x);
s.y = sin(c_rot.y);
s.z = sin(c_rot.z);
//float3 ret;
//ret.x = cos_rot.y*(sin_rot.z+cos_rot.z*(point.x-c_pos.x))-sin_rot.y*(point.z-c_pos.z);
//ret.y = sin_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))+cos_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
//ret.z = cos_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))-sin_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
//float3 r1 = {cos_rot.y*cos_rot.z, -cos_rot.y*sin_rot.z, sin_rot.y};
//float3 r2 = {cos_rot.x*sin_rot.z + cos_rot.z*sin_rot.x*sin_rot.y, cos_rot.x*cos_rot.z - sin_rot.x*sin_rot.y*sin_rot.z, -cos_rot.y*sin_rot.x};
//float3 r3 = {sin_rot.x*sin_rot.z - cos_rot.x*cos_rot.z*sin_rot.y, cos_rot.z*sin_rot.x + cos_rot.x*sin_rot.y*sin_rot.z, cos_rot.y*cos_rot.x};
//float3 r1 = {cr.x*cr.y, cr.x*sr.y*sr.z - cr.z*sr.x, sr.x*sr.z + cr.x*cr.z*sr.y};
//float3 r2 = {cr.y*sr.x, cr.x*cr.z + sr.x*sr.y*sr.z, cr.z*sr.x*sr.y - cr.x*sr.z};
//float3 r3 = {-sr.y, cr.y*sr.z, cr.y*cr.z};
//float3 r1 = {cr.x*cr.z - sr.x*sr.y*sr.z, -cr.y*sr.x, cr.x*sr.z + cr.z*sr.x*sr.y};
//float3 r2 = {cr.z*sr.x + cr.x*sr.y*sr.z, cr.x*cr.y, sr.x*sr.z - cr.x*cr.z*sr.y};
//float3 r3 = {-cr.y*sr.z, sr.y, cr.y*cr.z};
/*float3 r1 = {cr.x*cr.z - cr.y*sr.x*sr.z, sr.x*sr.y, cr.x*sr.z + cr.y*cr.z*sr.x};
float3 r2 = {sr.y*sr.z, cr.y, -cr.z*sr.y};
float3 r3 = {-cr.z*sr.x - cr.x*cr.y*sr.z, cr.x*sr.y, cr.x*cr.y*cr.z - sr.x*sr.z};*/
cl_float3 rel = sub(point, c_pos);
//cl_float3 r1, r2, r3;
cl_float3 ret;
ret.x = c.y * (s.z * rel.y + c.z*rel.x) - s.y*rel.z;
ret.y = s.x * (c.y * rel.z + s.y*(s.z*rel.y + c.z*rel.x)) + c.x*(c.z*rel.y - s.z*rel.x);
ret.z = c.x * (c.y * rel.z + s.y*(s.z*rel.y + c.z*rel.x)) - s.x*(c.z*rel.y - s.z*rel.x);
//float3 ret;
//ret.x = r1.x * rel.x + r1.y * rel.y + r1.z * rel.z;
//ret.y = r2.x * rel.x + r2.y * rel.y + r2.z * rel.z;
//ret.z = r3.x * rel.x + r3.y * rel.y + r3.z * rel.z;
return (cl_float4){ret.x, ret.y, ret.z, 0.0f};
}
cl_float4 engine::rot_about_camera(cl_float4 val)
{
return rot_about(val, c_pos, c_rot);
}
cl_float4 engine::back_project_about_camera(cl_float4 pos) ///from screenspace to worldspace
{
cl_float4 local_position= {((pos.x - width/2.0f)*pos.z/FOV_CONST), ((pos.y - height/2.0f)*pos.z/FOV_CONST), pos.z, 0};
cl_float4 zero = {0,0,0,0};
///backrotate pixel coordinate into globalspace
cl_float4 global_position = local_position;
global_position = rot_about(global_position, zero, (cl_float4)
{
-c_rot.x, 0.0f, 0.0f, 0.0f
});
global_position = rot_about(global_position, zero, (cl_float4)
{
0.0f, -c_rot.y, 0.0f, 0.0f
});
global_position = rot_about(global_position, zero, (cl_float4)
{
0.0f, 0.0f, -c_rot.z, 0.0f
});
global_position.x += c_pos.x;
global_position.y += c_pos.y;
global_position.z += c_pos.z;
global_position.w = 0;
return global_position;
}
cl_float4 engine::rotate(cl_float4 pos, cl_float4 rot)
{
cl_float4 zero = {0,0,0,0};
return rot_about(pos, zero, rot);
}
cl_float4 engine::back_rotate(cl_float4 pos, cl_float4 rot)
{
pos = rotate(pos, (cl_float4)
{
-rot.x, 0.0f, 0.0f, 0.0f
});
pos = rotate(pos, (cl_float4)
{
0.0f, -rot.y, 0.0f, 0.0f
});
pos = rotate(pos, (cl_float4)
{
0.0f, 0.0f, -rot.z, 0.0f
});
return pos;
}
cl_float4 depth_project_singular(cl_float4 rotated, int width, int height, float fovc)
{
float rx;
rx=(rotated.x) * (fovc/(rotated.z));
float ry;
ry=(rotated.y) * (fovc/(rotated.z));
rx+=width/2.0f;
ry+=height/2.0f;
cl_float4 ret;
ret.x = rx;
ret.y = ry;
ret.z = rotated.z;
ret.w = 0;
return ret;
}
cl_float4 engine::project(cl_float4 val)
{
///?
cl_float4 rotated = rot_about(val, c_pos, c_rot);//sub({0,0,0,0}, c_rot));
cl_float4 projected = depth_project_singular(rotated, width, height, FOV_CONST);
return projected;
}
int engine::get_mouse_delta_x()
{
return mdx;
}
int engine::get_mouse_delta_y()
{
return mdy;
}
float engine::get_mouse_sens_adjusted_x()
{
return get_mouse_delta_x() * mouse_sens;
}
float engine::get_mouse_sens_adjusted_y()
{
return get_mouse_delta_y() * mouse_sens;
}
void engine::set_mouse_sens(float sens)
{
mouse_sens = sens;
}
void engine::set_relative_mouse_mode(bool is_relative)
{
if(is_relative == mouse_is_relative)
return;
mouse_is_relative = is_relative;
#ifdef RAW_INPUT_ENABLED
if(raw_input_active)
{
///we have to double this as when we refresh the window
///and reset the same setting, it does not seem to stick
///so we have to toggle
if(is_relative)
{
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_SetRelativeMouseMode(SDL_TRUE);
}
else
{
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_SetRelativeMouseMode(SDL_FALSE);
}
}
#endif // RAW_INPUT_ENABLED
if(!raw_input_active && is_relative)
{
cmx = width/2;
cmy = height/2;
mouse.setPosition({cmx, cmy}, window);