Oculus Rift SDK

Please note: Oculus, Oculus VR, Rift and the Oculus-Logo ("Oculus Marks") are trademarks or registered trademarks of Oculus VR, LLC. Please have a look at the homepage to read about and download the SDK.

As of 14th July 2017 this code's status is work in progress!

 

  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
unit OculusVR_header;

interface

uses windows;

const
{$IFDEF Win64}
  DLL_FILENAME = 'LibOVRRT64_1.dll';
{$ENDIF}
{$IFDEF Win32}
  DLL_FILENAME = 'LibOVRRT32_1.dll';
{$ENDIF}

  OVR_MINOR_VERSION = 15;
  OVRINIT_REQUESTVERSION: UInt32 = 4;

  OVREYE_LEFT_Left = 0;
  OVREYE_RIGHT = 1;
  OVREYE_COUNT = 2;

  OVRPROJECTION_NONE = 0;
  OVRPROJECTION_LEFTHANDED = $1;
  OVRPROJECTION_FARLESSTHANNEAR = $2;
  OVRPROJECTION_FARCLIPATINFINITY = $4;
  OVRPROJECTION_CLIPRANGEIOENGL = $8;

type
  TSmallStruct = Int64;


  PUInt16 = ^UInt16;
  PUInt32 = ^UInt32;


  TOvrLogCallback = procedure(userData: PUInt32; level: int32; message: PAnsiChar);


  TOvrFovPort = record
    UpTan: single; ///< Tangent of the angle between the viewing vector and top edge of the FOV.
    DownTan: single; ///< Tangent of the angle between the viewing vector and bottom edge of the FOV.
    LeftTan: single; ///< Tangent of the angle between the viewing vector and left edge of the FOV.
    RightTan: single; ///<
  end;

  TOvrBool = (ovrFalse = 0, ovrTrue = 1);

  TOvrVector2i = record
    x, y: Int32;
  end;

  TOvrSizei = record
    w, h: Int32;
  end;

  TOvrQuatF = record
    x,y,z,w: single;
  end;

  TOvrVector2f = record
    x,y: single;
  end;

  TOvrVector3f = record
    x,y,z: single;
  end;

  TOvrVector3fPair = array[0..1] of TOvrVector3f;

  TOvrRecti = record
    Pos: TOvrVector2i;
    Size: TOvrSizei;
  end;

  POvrPosef = ^TOvrPosef;
  TOvrPosef = record
    Orientation: TOvrQuatF;
    Position: TOvrVector3f;
  end;

  TOvrPoseStateF = record
    ThePose: TOvrPoseF;
    AngularVelocity: TOvrVector3F;
    LinearVelocity: TOvrVector3F;
    AngularAcceleration: TOvrVector3F;
    LinearAccelaration: TOvrVector3F;
    {$IFDEF Win64}
    pad0: array[0..3] of AnsiChar;
    {$ENDIF}
    TimeInSeconds: double;
  end;

  POvrMatrix = ^TOvrMatrix;
  TOvrMatrix = array[0..3,0..3] of single;

  TOvrEyeType = Int32;

  POvrInitParams = ^TOvrInitParams;
  TOvrInitParams = record
    Flags: UInt32;
    RequestedMinorVersion: UInt32;
    LogCallback: TOvrLogCallback;
    UserData: PUInt32;
    ConnectionTimeoutMS: UInt32;
    {$IFDEF Win64}
    pad0: array[0..3] of AnsiChar;
    {$ENDIF}
  end;

  POvrSession = ^TOvrSession;
  TOvrSession = pointer;

  POvrGraphicsLuid = ^TOvrGraphicsLuid;
  TOvrGraphicsLuid = TLUID;

  TOvrResult = Int32;

  POvrMirrorTexture = pointer;

  TOvrTextureFormat =
  (
    OVR_FORMAT_UNKNOWN = 0,
    OVR_FORMAT_B5G6R5_UNORM = 1, ///< Not currently supported on PC. Requires a DirectX 11.1 device.
    OVR_FORMAT_B5G5R5A1_UNORM = 2, ///< Not currently supported on PC. Requires a DirectX 11.1 device.
    OVR_FORMAT_B4G4R4A4_UNORM = 3, ///< Not currently supported on PC. Requires a DirectX 11.1 device.
    OVR_FORMAT_R8G8B8A8_UNORM = 4,
    OVR_FORMAT_R8G8B8A8_UNORM_SRGB = 5,
    OVR_FORMAT_B8G8R8A8_UNORM = 6,
    OVR_FORMAT_B8G8R8A8_UNORM_SRGB = 7, ///< Not supported for OpenGL applications
    OVR_FORMAT_B8G8R8X8_UNORM = 8, ///< Not supported for OpenGL applications
    OVR_FORMAT_B8G8R8X8_UNORM_SRGB = 9, ///< Not supported for OpenGL applications
    OVR_FORMAT_R16G16B16A16_FLOAT = 10,
    OVR_FORMAT_R11G11B10_FLOAT = 25, ///< Introduced in v1.10

    // Depth formats
    OVR_FORMAT_D16_UNORM = 11,
    OVR_FORMAT_D24_UNORM_S8_UINT = 12,
    OVR_FORMAT_D32_FLOAT = 13,
    OVR_FORMAT_D32_FLOAT_S8X24_UINT = 14,

    // Added in 1.5 compressed formats can be used for static layers
    OVR_FORMAT_BC1_UNORM = 15,
    OVR_FORMAT_BC1_UNORM_SRGB = 16,
    OVR_FORMAT_BC2_UNORM = 17,
    OVR_FORMAT_BC2_UNORM_SRGB = 18,
    OVR_FORMAT_BC3_UNORM = 19,
    OVR_FORMAT_BC3_UNORM_SRGB = 20,
    OVR_FORMAT_BC6H_UF16 = 21,
    OVR_FORMAT_BC6H_SF16 = 22,
    OVR_FORMAT_BC7_UNORM = 23,
    OVR_FORMAT_BC7_UNORM_SRGB = 24,

    OVR_FORMAT_ENUMSIZE = $7fffffff ///< \internal Force type int32_t.
  );

  TOvrTrackingState = record
    HeadPose: TOvrPoseStateF;
    StatusFlags: UInt;
    HandPoses: array[0..1] of TOvrPoseStateF;
    HandStatusFlags: array[0..1] of UInt;
    CalibrateOrigin: TOvrPoseF;
  end;

  TOvrTextureType = (
    ovrTexture_2D,
    ovrTexture_2D_External,
    ovrTexture_Cube,
    ovrTexture_Count,
    ovrTexture_EnumSize = $7fffffff
  );

  TOvrHmdType =
  (
    ovrHmd_None = 0,
    ovrHmd_DK1 = 3,
    ovrHmd_DKHD = 4,
    ovrHmd_DK2 = 6,
    ovrHmd_CB = 8,
    ovrHmd_Other = 9,
    ovrHmd_E3_2015 = 10,
    ovrHmd_ES06 = 11,
    ovrHmd_ES09 = 12,
    ovrHmd_ES11 = 13,
    ovrHmd_CV1 = 14,
    ovrHmd_EnumSize = $7fffffff ///< \internal Force type int32_t.
  );

  TOvrHmdDesc = record
    typ: TOvrHmdType;
    {$IFDEF Win64}
    pad1: array[0..3] of AnsiChar;
    {$ENDIF}
    ProductName: array[0..63] of AnsiChar;
    Manufacturer: array[0..63] of AnsiChar;
    VendorId: Short;
    ProductID: Short;
    SerialNumber: array[0..23] of AnsiChar;
    FirmwareMajor: short;
    FirmwareMinor: short;
    AvailableHmdCaps: UInt32;
    DefaultHmdCaps: UInt32; ///< Default ovrHmdCaps bits.
    AvailableTrackingCaps: UInt32; ///< Available ovrTrackingCaps bits.
    DefaultTrackingCaps: UInt32; ///< Default ovrTrackingCaps bits.
    DefaultEyeFov: array[0..1] of TOvrFovPort; ///< Defines the recommended FOVs for the HMD.
    MaxEyeFov: array[0..1] of TOvrFovPort; ///< Defines the maximum FOVs for the HMD.
    Resolution: TOvrSizei; ///< Resolution of the full HMD screen (both eyes) in pixels.
    DisplayRefreshRate: single; ///< Refresh rate of the display in cycles per second.
    {$IFDEF Win64}
    pad1: array[0..3] of AnsiChar;
    {$ENDIF}
  end;

  POvrViewScaleDesc = ^TOvrViewScaleDesc;
  TOvrViewScaleDesc = record
    HmdToEyeOffset: TOvrVector3fPair;
    HmdSpaceToWorldScaleInMeters: single;
  end;

  POvrEyeRenderDesc = ^TOvrEyeRenderDesc;
  TOvrEyeRenderDesc = record
    Eye: TOvrEyeType;
    Fov: TOvrFovPort;
    DistortedViewport: TOvrRecti;
    PixelsPerTanAngleAtCenter: TOvrVector2F;
    HmdToEyeOffset: TOvrVector3F;
  end;

  POvrSessionStatus = ^TOvrSessionStatus;
  TOvrSessionStatus = record
    IsVisible: TOvrBool;
    HmdPresent: TOvrBool;
    HmdMounted: TOvrBool;
    DisplayLost: TOvrBool;
    ShouldQuit: TOvrBool;
    ShouldRecenter: TOvrBool;
  end;

  TOvrMirrorTextureDesc = record
    Format: TOvrTextureFormat;
    Width,
    Heigth: Int32;
    MiscFlags: UInt32;
    MirrorOptions: UInt32;
  end;

  TOvrLayerType = (
    ovrLayerType_Disabled = 0,
    ovrLayerType_EyeFov = 1,
    ovrLayerType_Quad = 3,
    ovrLayerType_EyeMatrix = 5,
    ovrLayerType_EnumSize = $7fffffff
  );

  PPOvrLayerHeader = ^POvrLayerHeader;
  POvrLayerHeader = ^TOvrLayerHeader;
  TOvrLayerHeader = record
    typ: TOvrLayerType; ///< Described by ovrLayerType.
    Flags: UInt32; ///< Described by ovrLayerFlags.
  end;

  POvrTextureSwapChain = ^TOvrTextureSwapChain;
  TOvrTextureSwapChain = pointer;

  POvrTextureSwapChainDesc = ^TOvrTextureSwapChainDesc;
  TOvrTextureSwapChainDesc = record
    Typ: TOvrTextureType;
    Format: TOvrTextureFormat;
    ArraySize: integer;
    Width: integer;
    Height: integer;
    MipLevels: integer;
    SampleCount: integer;
    StaticImage: TOvrBool;
    MiscFlags: UInt;
    BindFlags: UInt;
  end;

  TOvrLayerEyeFov = record
    Header: TOvrLayerHeader;
    ColorTexture: array[0..1] of TOvrTextureSwapChain;
    Viewport: array[0..1] of TOvrRecti;
    Fov: array[0..1] of TOvrFovPort;
    RenderPose: array[0..1] of TOvrPosef;
    SensorSampleTime: double;
  end;

  POvrErrorInfo = ^TOvrErrorInfo;
  TOvrErrorInfo = record
    _result: TOvrResult;
    ErrorString: array[0..511] of AnsiChar;
  end;

  // Entrypoints:

const
  OVR_INITIALIZE_FUNCNAME = 'ovr_Initialize';
  OVR_CREATE_FUNCNAME = 'ovr_Create';
  OVR_GETHMDDESC_FUNCNAME = 'ovr_GetHmdDesc';
  OVR_GETFOVTEXTURESIZE_FUNCNAME = 'ovr_GetFovTextureSize';
  OVR_GETMIRRORTEXTUREBUFFERGL_FUNCNAME = 'ovr_GetMirrorTextureBufferGL';
  ovr_CreateTextureSwapChainGL_FUNCNAME = 'ovr_CreateTextureSwapChainGL';
  ovr_GetTextureSwapChainLength_FUNCNAME = 'ovr_GetTextureSwapChainLength';
  ovr_GetTextureSwapChainBufferGL_FUNCNAME = 'ovr_GetTextureSwapChainBufferGL';
  ovr_DestroyTextureSwapChain_FUNCNAME = 'ovr_DestroyTextureSwapChain';
  ovr_GetTextureSwapChainCurrentIndex_FUNCNAME = 'ovr_GetTextureSwapChainCurrentIndex';
  ovr_CommitTextureSwapChain_FUNCNAME = 'ovr_CommitTextureSwapChain';
  ovr_GetLastErrorInfo_FUNCNAME = 'ovr_GetLastErrorInfo';
  ovrMatrix4f_Projection_FUNCNAME = 'ovrMatrix4f_Projection';
  ovr_GetSessionStatus_FUNCNAME = 'ovr_GetSessionStatus';
  ovr_RecenterTrackingOrigin_FUNCNAME = 'ovr_RecenterTrackingOrigin';
  ovr_GetRenderDesc_FUNCNAME = 'ovr_GetRenderDesc';
  ovr_GetEyePoses_FUNCNAME = 'ovr_GetEyePoses';
  ovr_SubmitFrame_FUNCNAME = 'ovr_SubmitFrame';
  ovr_GetPredictedDisplayTime_FUNCNAME = 'ovr_GetPredictedDisplayTime';
  ovr_GetTrackingState_FUNCNAME = 'ovr_GetTrackingState';
  ovr_CalcEyePoses_FUNCNAME = 'ovr_CalcEyePoses';
  ovr_Destroy_FUNCNAME = 'ovr_Destroy';

  OVRLAYERFLAG_HIGHQUALITY = $01;
  OVRLAYERFLAG_TEXTUREORIGINATBOTTOMLEFT = $02;
  OVRLAYERFLAG_HEADLOCKED = $04;

type
  TFunc_ovr_Initialize = function(params: POvrInitParams): TOvrResult; cdecl;
  TFunc_ovr_Create = function(pSession: POvrSession; pLuid: POvrGraphicsLuid): TOvrResult; cdecl;
  TFunc_ovr_GetHmdDesc = function(pSession: POvrSession): TOvrHmdDesc; cdecl;
  TFunc_ovr_GetFovTextureSize = function(session: TOvrSession; eye: TOvrEyeType; fov: TOvrFovPort; pixelsPerDisplayPixel: single): TSmallStruct; cdecl;
  TFunc_ovr_GetMirrorTextureBufferGL = function(session: TOvrSession; mirrorTexture: POvrMirrorTexture; out_TexID: PUInt32): TOvrResult; cdecl;
  TFunc_ovr_CreateTextureSwapChainGL = function(session: TOvrSession; desc: POvrTextureSwapChainDesc; out_TextureSwapChain: POvrTextureSwapChain): TOvrResult; cdecl;
  TFunc_ovr_GetTextureSwapChainLength = function(session: TOvrSession; chain: TOvrTextureSwapChain; out_Length: PInteger): TOvrResult; cdecl;
  TFunc_ovr_GetTextureSwapChainBufferGL = function(session: TOvrSession; chain: TOvrTextureSwapChain; index: integer; out_TexID: PUInt): TOvrResult; cdecl;
  TFunc_ovr_DestroyTextureSwapChain = procedure(session: TOvrSession; chan: TOvrTextureSwapChain); cdecl;
  TFunc_ovr_GetTextureSwapChainCurrentIndex = function(session: TOvrSession; chain: TOvrTextureSwapChain; out_Index: PInteger): TOvrResult; cdecl;
  TFunc_ovr_CommitTextureSwapChain = function(session: TOvrSession; chain: TOvrTextureSwapChain): TOvrResult; cdecl;
  TFunc_ovr_GetLastErrorInfo = procedure(errorInfo: POvrErrorInfo); cdecl;
  TFunc_ovrMatrix4f_Projection = function(fov: TOvrFovPort; znear: single; zfar: single; projectionModFlags: UInt): TOvrMatrix; cdecl;
  TFunc_ovr_GetSessionStatus = function(session: TOvrSession; sessionStatus: POvrSessionStatus): TOvrResult; cdecl;
  TFunc_ovr_RecenterTrackingOrigin = function(session: TOvrSession): TOvrResult; cdecl;
  TFunc_ovr_GetRenderDesc = function(session: TOvrSession; eyeType: TOvrEyeType; fov: TOvrFovPort): TOvrEyeRenderDesc; cdecl;
  TFunc_ovr_GetEyePoses = procedure(session: TOvrSession; frameIndex: int64; latencyMarker: TOvrBool; hmdToEyeOffset: TOvrVector3fPair; outEyePoses: POvrPosef; outSensorSampleTime: PDouble); cdecl;
  TFunc_ovr_SubmitFrame = function(session: TOvrSession; frameIndex: int64; viewScaleDesc: POvrViewScaleDesc; layerPtrList: PPOvrLayerHeader; layercount: cardinal): TOvrResult; cdecl;
  TFunc_ovr_GetPredictedDisplayTime = function(session:TOvrSession; frameIndex: int64): double; cdecl;
  TFunc_ovr_GetTrackingState = function(session: TOvrSession; absTime: double; latencyMarker: TOvrBool): TOvrTrackingState; cdecl;
  TFunc_ovr_CalcEyePoses = procedure(headPose: TOvrPoseF; hmdToEyeOffset: TOvrVector3fPair; outEyePoses: POvrPosef); cdecl;
  TFunc_ovr_Destroy = procedure(session: TOvrSession); cdecl;

var
  ovr_Initialize: TFunc_Ovr_Initialize;
  ovr_Create: TFunc_Ovr_Create;
  ovr_GetHmdDesc: TFunc_ovr_GetHmdDesc;
  ovr_GetFovTextureSize_internal: TFunc_ovr_GetFovTextureSize;
  ovr_GetMirrorTextureBufferGL: TFunc_ovr_GetMirrorTextureBufferGL;
  ovr_CreateTextureSwapChainGL: TFunc_ovr_CreateTextureSwapChainGL;
  ovr_GetTextureSwapChainLength: TFunc_ovr_GetTextureSwapChainLength;
  ovr_GetTextureSwapChainBufferGL: TFunc_ovr_GetTextureSwapChainBufferGL;
  ovr_DestroyTextureSwapChain: TFunc_ovr_DestroyTextureSwapChain;
  ovr_GetTextureSwapChainCurrentIndex: TFunc_ovr_GetTextureSwapChainCurrentIndex;
  ovr_CommitTextureSwapChain: TFunc_ovr_CommitTextureSwapChain;
  ovr_GetLastErrorInfo: TFunc_ovr_GetLastErrorInfo;
  ovrMatrix4f_Projection: TFunc_ovrMatrix4f_Projection;
  ovr_GetSessionStatus: TFunc_ovr_GetSessionStatus;
  ovr_RecenterTrackingOrigin: TFunc_ovr_RecenterTrackingOrigin;
  ovr_GetRenderDesc: TFunc_ovr_GetRenderDesc;
  ovr_GetEyePoses: TFunc_ovr_GetEyePoses;
  ovr_SubmitFrame: TFunc_ovr_SubmitFrame;
  ovr_GetPredictedDisplayTime: TFunc_ovr_GetPredictedDisplayTime;
  ovr_GetTrackingState: TFunc_ovr_GetTrackingState;
  ovr_CalcEyePoses: TFunc_ovr_CalcEyePoses;
  ovr_Destroy: TFunc_ovr_Destroy;

  // Eigene Funktionen:

  procedure OvrInitDLL;
  procedure OvrFinDLL;
  function OvrGetErrorString(ocResult: TOvrResult): string;
  function OVR_SUCCESS(_result: TOvrResult; _dlg: boolean = false): boolean;

  // Konvertierte Funktionen:

  function ovr_GetFovTextureSize(session: TOvrSession; eye: TOvrEyeType; fov: TOvrFovPort; pixelsPerDisplayPixel: single): TOvrSizei;

var
  OvrDLLLoaded: boolean;

implementation

uses
  System.SysUtils, Logging;

var
  DLLHandle: THandle;

  procedure OvrInitDLL;
  begin
    OvrFinDLL;
    DLLHandle := LoadLibrary(PChar(DLL_FILENAME));
    if DLLHandle = 0 then
    begin
      Logger.AddMessage('OculusVR not found', lmtInfo);
      exit;
    end;

    @ovr_Initialize :=                      GetProcAddress(DLLHandle, OVR_INITIALIZE_FUNCNAME);
    @ovr_Create :=                          GetProcAddress(DLLHandle, OVR_CREATE_FUNCNAME);
    @ovr_GetHmdDesc :=                      GetProcAddress(DLLHandle, OVR_GETHMDDESC_FUNCNAME);
    @ovr_GetFovTextureSize_internal :=      GetProcAddress(DLLHandle, OVR_GETFOVTEXTURESIZE_FUNCNAME);
    @ovr_GetMirrorTextureBufferGL :=        GetProcAddress(DLLHandle, OVR_GETMIRRORTEXTUREBUFFERGL_FUNCNAME);
    @ovr_CreateTextureSwapChainGL :=        GetProcAddress(DLLHandle, ovr_CreateTextureSwapChainGL_FUNCNAME);
    @ovr_GetTextureSwapChainLength :=       GetProcAddress(DLLHandle, ovr_GetTextureSwapChainLength_FUNCNAME);
    @ovr_GetTextureSwapChainBufferGL :=     GetProcAddress(DLLHandle, ovr_GetTextureSwapChainBufferGL_FUNCNAME);
    @ovr_DestroyTextureSwapChain :=         GetProcAddress(DLLHandle, ovr_DestroyTextureSwapChain_FUNCNAME);
    @ovr_GetTextureSwapChainCurrentIndex := GetProcAddress(DLLHandle, ovr_GetTextureSwapChainCurrentIndex_FUNCNAME);
    @ovr_CommitTextureSwapChain :=          GetProcAddress(DLLHandle, ovr_CommitTextureSwapChain_FUNCNAME);
    @ovr_GetLastErrorInfo :=                GetProcAddress(DLLHandle, ovr_GetLastErrorInfo_FUNCNAME);
    @ovrMatrix4f_Projection :=              GetProcAddress(DLLHandle, ovrMatrix4f_Projection_FUNCNAME);
    @ovr_GetSessionStatus :=                GetProcAddress(DLLHandle, ovr_GetSessionStatus_FUNCNAME);
    @ovr_RecenterTrackingOrigin :=          GetProcAddress(DLLHandle, ovr_RecenterTrackingOrigin_FUNCNAME);
    @ovr_GetRenderDesc :=                   GetProcAddress(DLLHandle, ovr_GetRenderDesc_FUNCNAME);
    @ovr_GetEyePoses :=                     GetProcAddress(DLLHandle, ovr_GetEyePoses_FUNCNAME);
    @ovr_SubmitFrame :=                     GetProcAddress(DLLHandle, ovr_SubmitFrame_FUNCNAME);
    @ovr_GetPredictedDisplayTime :=         GetProcAddress(DLLHandle, ovr_GetPredictedDisplayTime_FUNCNAME);
    @ovr_GetTrackingState :=                GetProcAddress(DLLHandle, ovr_GetTrackingState_FUNCNAME);
    @ovr_CalcEyePoses :=                    GetProcAddress(DLLHandle, ovr_CalcEyePoses_FUNCNAME);
    @ovr_Destroy :=                         GetProcAddress(DLLHandle, ovr_Destroy_FUNCNAME);

    OvrDLLLoaded := true;
  end;

  procedure OvrFinDLL;
  begin
    OvrDLLLoaded := false;
    if DLLHandle <> 0 then
      FreeLibrary(DLLHandle);
    DLLHandle := 0;
  end;

  function OvrGetErrorString(ocResult: TOvrResult): string;
  begin
    case ocResult of
      0: result := 'No error';

      -1000: result := 'Failure to allocate memory.';
      -1002: result := 'Invalid ovrSession parameter provided.';
      -1003: result := 'The operation timed out.';
      -1004: result := 'The system or component has not been initialized';
      -1005: result := 'Invalid parameter provided.';
      -1006: result := 'Generic service error.';
      -1007: result := 'The given HMD doesn''t exist.';
      -1009: result := 'Function call is not supported on this hardware/software';
      -1010: result := 'Specified device type isn''t available';
      -1011: result := 'The headset was in an invalid orientation for the requested operation (e.g. vertically oriented during ovr_RecenterPose';
      -1012: result := 'The client failed to call ovr_Destroy on an active session before calling ovr_Shutdown.';
      -1013: result := 'The client failed to call ovr_Shutdown or the client crashed.';
      -1014: result := 'The service watchdog discovered a deadlock.';
      -1015: result := 'Function call is invalid fo object''s current state';
      -1016: result := 'Increase size of output array';
      -1017: result := 'There is not any external camera information stored by ovrServer.';

      -2001: result := 'Failure to find the specified audio device.';
      -2002: result := 'Generic COM error.';

      -3000: result := 'Generic initialization error.';
      -3001: result := 'Couldn''t load LibOVRRT';
      -3002: result := 'LibOVRRT version incompatibility.';
      -3003: result := 'Couldn''t cnnect to the OVR Service.';
      -3004: result := 'OVR Service version incompatibility.';
      -3005: result := 'The operatingsystem version is incompatible.';
      -3006: result := 'Unable to initialize the HMD display.';
      -3007: result := 'Unable to start the server.';
      -3008: result := 'Attempting to re-initialize with a different version.';
      -3009: result := 'Chosen rendering adapters between client and service do not match.';
      -3010: result := 'Calling application has leaked resources.';
      -3011: result := 'Client version too old to connect to service.';
      -3012: result := 'The operating system is out of date.';
      -3013: result := 'The graphics driver is out of date.';
      -3014: result := 'The graphics hardware is not supported.';
      -3015: result := 'No valid VR display system found.';
      -3016: result := 'Feature or API is obsolete and no longer supported.';
      -3017: result := 'No supported VR display system found, but disabled or driverless adapter found.';
      -3018: result := 'THe system is using hybrid graphics (Optimus, etc...), which is not support.';
      -3019: result := 'Initialization of the DisplayManager failed.';
      -3020: result := 'Failed to get the interface for an attached tracker.';
      -3021: result := 'LobOVRRT signature check failure.';
      -3022: result := 'LibOVRRT spath failure.';
      -3023: result := 'LibOVRRT symbol resolution failure.';
      -3024: result := 'Failed to connect to the service because remote connections to the service are not allowed.';
      -3025: result := 'Vulkan initialization error.';

      -6000: result := 'Event of a system-wide graphics reset or cable unplug.';
      -6001: result := 'ovr_CommitTextureSwapChain was called too many times on a texture swapchain without calling submit to use the chain.';
      -6002: result := 'The ovrTextureSwapChain is in an incomplete or inconsistent state.';
      -6003: result := 'Graphics device has been reset (TDR, etc...)';
      -6004: result := 'HMD removed from the display adapter';
      -6005: result := 'Content protection is not available for the display.';
      -6006: result := 'Application declared itself as an inivisible type and is not allowed to submit frames.';
      -6007: result := 'The given request is disallowed under the current conditions.';
      -6008: result := 'Display portion of HMD is plugged into an incompatible port (ex: IGP)';

      -7000: result := 'A runtime exception occured. The application is required to shutdown LibOVR and re-initialize it before this error state will be cleared.';

      -9000: result := 'Missing calibration block';
      -9001: result := 'Old calibration block';
      -9002: result := 'Bad calibration block due to lengths';

      else result := 'Unknown error';
    end;
    result := result + ' ('+ inttostr(ocResult) + ')';
  end;

  function OVR_SUCCESS(_result: TOvrResult; _dlg: boolean = false): boolean;
  var
    errorstring: string;
    errorinfo: TOvrErrorInfo;
  begin
    result := (_result >= 0);
    if not result then
    begin
      errorstring := 'Oculus VR error: ' + OvrGetErrorString(_result);
      ovr_GetLastErrorInfo(@errorInfo);
      errorstring := errorstring + #13#10#13#10 + errorInfo.ErrorString;
      if _dlg then
        Logger.AddMessage(errorstring, lmtWarningDlg)
      else
        Logger.AddMessage(errorstring, lmtWarning);
    end;
  end;


  function ovr_GetFovTextureSize(session: TOvrSession; eye: TOvrEyeType; fov: TOvrFovPort; pixelsPerDisplayPixel: single): TOvrSizei;
  begin
    result := TOvrSizei(ovr_GetFovTextureSize_internal(session, eye, fov, pixelsPerDisplayPixel));
  end;

initialization
  OvrDLLLoaded := false;

end.