1 module re.gfx.vr;
2 
3 import re;
4 import re.math;
5 import re.gfx;
6 import std.format;
7 
8 static import raylib;
9 
10 version (vr) class VRSupport {
11     /// whether VR is enabled
12     public bool enabled = false;
13     public raylib.VrStereoConfig config;
14     public raylib.Shader distortion_shader;
15 
16     /// set up VR rendering
17     public void setup_vr(raylib.VrDeviceInfo device_info) {
18         enabled = true;
19 
20         config = raylib.LoadVrStereoConfig(device_info);
21 
22         Core.log.info(format("initializing vr stereo config for device: %s", device_info));
23 
24         // // update render resolution
25         // resolution = Vector2(device_info.hResolution, device_info.vResolution);
26 
27         import re.util.vr_distortion;
28         import re.util.interop : c_str;
29 
30         // set up distortion shader
31         version (lite) {
32             distortion_shader = raylib.LoadShaderFromMemory(
33                 null, VR_DISTORTION_SHADER_GL100.c_str);
34         } else {
35             distortion_shader = raylib.LoadShaderFromMemory(
36                 null, VR_DISTORTION_SHADER_GL330.c_str);
37         }
38 
39         // set shader vars
40         alias vartype = raylib.ShaderUniformDataType;
41         // Update distortion shader with lens and distortion-scale parameters
42         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "leftLensCenter"),
43             cast(float*) config.leftLensCenter, vartype.SHADER_UNIFORM_VEC2);
44         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "rightLensCenter"),
45             cast(float*) config.rightLensCenter, vartype.SHADER_UNIFORM_VEC2);
46         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "leftScreenCenter"),
47             cast(float*) config.leftScreenCenter, vartype.SHADER_UNIFORM_VEC2);
48         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "rightScreenCenter"),
49             cast(float*) config.rightScreenCenter, vartype.SHADER_UNIFORM_VEC2);
50 
51         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "scale"),
52             cast(float*) config.scale, vartype.SHADER_UNIFORM_VEC2);
53         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "scaleIn"),
54             cast(float*) config.scaleIn, vartype.SHADER_UNIFORM_VEC2);
55         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "deviceWarpParam"),
56             cast(float*) device_info.lensDistortionValues, vartype.SHADER_UNIFORM_VEC4);
57         raylib.SetShaderValue(distortion_shader, raylib.GetShaderLocation(distortion_shader, "chromaAbParam"),
58             cast(float*) device_info.chromaAbCorrection, vartype.SHADER_UNIFORM_VEC4);
59     }
60 }