| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using UnityEngine; |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
| 20 | using System.Runtime.InteropServices; |
| 21 | |
| 22 | #if USING_XR_SDK |
| 23 | using UnityEngine.Experimental.XR; |
| 24 | #endif |
| 25 | |
| 26 | #if UNITY_2017_2_OR_NEWER |
| 27 | using InputTracking = UnityEngine.XR.InputTracking; |
| 28 | using Node = UnityEngine.XR.XRNode; |
| 29 | using NodeState = UnityEngine.XR.XRNodeState; |
| 30 | using Device = UnityEngine.XR.XRDevice; |
| 31 | #elif UNITY_2017_1_OR_NEWER |
| 32 | using InputTracking = UnityEngine.VR.InputTracking; |
| 33 | using Node = UnityEngine.VR.VRNode; |
| 34 | using NodeState = UnityEngine.VR.VRNodeState; |
| 35 | using Device = UnityEngine.VR.VRDevice; |
| 36 | #else |
| 37 | using InputTracking = UnityEngine.VR.InputTracking; |
| 38 | using Node = UnityEngine.VR.VRNode; |
| 39 | using Device = UnityEngine.VR.VRDevice; |
| 40 | #endif |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Miscellaneous extension methods that any script can use. |
| 44 | /// </summary> |
| 45 | public static class OVRExtensions |
| 46 | { |
| 47 | /// <summary> |
| 48 | /// Converts the given world-space transform to an OVRPose in tracking space. |
| 49 | /// </summary> |
| 50 | public static OVRPose ToTrackingSpacePose(this Transform transform, Camera camera) |
| 51 | { |
| 52 | //Initializing to identity, but for all Oculus headsets, down below the pose will be initialized to the runtime's pose value, so identity will never be returned. |
| 53 | OVRPose headPose = OVRPose.identity; |
| 54 | |
| 55 | Vector3 pos; |
| 56 | Quaternion rot; |
| 57 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos)) |
| 58 | headPose.position = pos; |
| 59 | if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot)) |
| 60 | headPose.orientation = rot; |
| 61 | |
| 62 | var ret = headPose * transform.ToHeadSpacePose(camera); |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /// <summary> |
| 69 | /// Converts the given pose from tracking-space to world-space. |
| 70 | /// </summary> |
| 71 | public static OVRPose ToWorldSpacePose(OVRPose trackingSpacePose) |
| 72 | { |
| 73 | OVRPose headPose = OVRPose.identity; |
| 74 | |
| 75 | Vector3 pos; |
| 76 | Quaternion rot; |
| 77 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos)) |
| 78 | headPose.position = pos; |
| 79 | if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot)) |
| 80 | headPose.orientation = rot; |
| 81 | |
| 82 | // Transform from tracking-Space to head-Space |
| 83 | OVRPose poseInHeadSpace = headPose.Inverse() * trackingSpacePose; |
| 84 | |
| 85 | // Transform from head space to world space |
| 86 | OVRPose ret = Camera.main.transform.ToOVRPose() * poseInHeadSpace; |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Converts the given world-space transform to an OVRPose in head space. |
| 93 | /// </summary> |
| 94 | public static OVRPose ToHeadSpacePose(this Transform transform, Camera camera) |
| 95 | { |
| 96 | return camera.transform.ToOVRPose().Inverse() * transform.ToOVRPose(); |
| 97 | } |
| 98 | |
| 99 | public static OVRPose ToOVRPose(this Transform t, bool isLocal = false) |
| 100 | { |
| 101 | OVRPose pose; |
| 102 | pose.orientation = (isLocal) ? t.localRotation : t.rotation; |
| 103 | pose.position = (isLocal) ? t.localPosition : t.position; |
| 104 | return pose; |
| 105 | } |
| 106 | |
| 107 | public static void FromOVRPose(this Transform t, OVRPose pose, bool isLocal = false) |
| 108 | { |
| 109 | if (isLocal) |
| 110 | { |
| 111 | t.localRotation = pose.orientation; |
| 112 | t.localPosition = pose.position; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | t.rotation = pose.orientation; |
| 117 | t.position = pose.position; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public static OVRPose ToOVRPose(this OVRPlugin.Posef p) |
| 122 | { |
| 123 | return new OVRPose() |
| 124 | { |
| 125 | position = new Vector3(p.Position.x, p.Position.y, -p.Position.z), |
| 126 | orientation = new Quaternion(-p.Orientation.x, -p.Orientation.y, p.Orientation.z, p.Orientation.w) |
| 127 | }; |
| 128 | } |
| 129 | |
| 130 | public static OVRTracker.Frustum ToFrustum(this OVRPlugin.Frustumf f) |
| 131 | { |
| 132 | return new OVRTracker.Frustum() |
| 133 | { |
| 134 | nearZ = f.zNear, |
| 135 | farZ = f.zFar, |
| 136 | |
| 137 | fov = new Vector2() |
| 138 | { |
| 139 | x = Mathf.Rad2Deg * f.fovX, |
| 140 | y = Mathf.Rad2Deg * f.fovY |
| 141 | } |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | public static Color FromColorf(this OVRPlugin.Colorf c) |
| 146 | { |
| 147 | return new Color() { r = c.r, g = c.g, b = c.b, a = c.a }; |
| 148 | } |
| 149 | |
| 150 | public static OVRPlugin.Colorf ToColorf(this Color c) |
| 151 | { |
| 152 | return new OVRPlugin.Colorf() { r = c.r, g = c.g, b = c.b, a = c.a }; |
| 153 | } |
| 154 | |
| 155 | public static Vector3 FromVector3f(this OVRPlugin.Vector3f v) |
| 156 | { |
| 157 | return new Vector3() { x = v.x, y = v.y, z = v.z }; |
| 158 | } |
| 159 | |
| 160 | public static Vector3 FromFlippedZVector3f(this OVRPlugin.Vector3f v) |
| 161 | { |
| 162 | return new Vector3() { x = v.x, y = v.y, z = -v.z }; |
| 163 | } |
| 164 | |
| 165 | public static OVRPlugin.Vector3f ToVector3f(this Vector3 v) |
| 166 | { |
| 167 | return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = v.z }; |
| 168 | } |
| 169 | |
| 170 | public static OVRPlugin.Vector3f ToFlippedZVector3f(this Vector3 v) |
| 171 | { |
| 172 | return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = -v.z }; |
| 173 | } |
| 174 | |
| 175 | public static Quaternion FromQuatf(this OVRPlugin.Quatf q) |
| 176 | { |
| 177 | return new Quaternion() { x = q.x, y = q.y, z = q.z, w = q.w }; |
| 178 | } |
| 179 | |
| 180 | public static Quaternion FromFlippedZQuatf(this OVRPlugin.Quatf q) |
| 181 | { |
| 182 | return new Quaternion() { x = -q.x, y = -q.y, z = q.z, w = q.w }; |
| 183 | } |
| 184 | |
| 185 | public static OVRPlugin.Quatf ToQuatf(this Quaternion q) |
| 186 | { |
| 187 | return new OVRPlugin.Quatf() { x = q.x, y = q.y, z = q.z, w = q.w }; |
| 188 | } |
| 189 | |
| 190 | public static OVRPlugin.Quatf ToFlippedZQuatf(this Quaternion q) |
| 191 | { |
| 192 | return new OVRPlugin.Quatf() { x = -q.x, y = -q.y, z = q.z, w = q.w }; |
| 193 | } |
| 194 | |
| 195 | public static OVR.OpenVR.HmdMatrix34_t ConvertToHMDMatrix34(this Matrix4x4 m) |
| 196 | { |
| 197 | OVR.OpenVR.HmdMatrix34_t pose = new OVR.OpenVR.HmdMatrix34_t(); |
| 198 | |
| 199 | pose.m0 = m[0, 0]; |
| 200 | pose.m1 = m[0, 1]; |
| 201 | pose.m2 = -m[0, 2]; |
| 202 | pose.m3 = m[0, 3]; |
| 203 | |
| 204 | pose.m4 = m[1, 0]; |
| 205 | pose.m5 = m[1, 1]; |
| 206 | pose.m6 = -m[1, 2]; |
| 207 | pose.m7 = m[1, 3]; |
| 208 | |
| 209 | pose.m8 = -m[2, 0]; |
| 210 | pose.m9 = -m[2, 1]; |
| 211 | pose.m10 = m[2, 2]; |
| 212 | pose.m11 = -m[2, 3]; |
| 213 | |
| 214 | return pose; |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
| 219 | //4 types of node state properties that can be queried with UnityEngine.XR |
| 220 | public enum NodeStatePropertyType |
| 221 | { |
| 222 | Acceleration, |
| 223 | AngularAcceleration, |
| 224 | Velocity, |
| 225 | AngularVelocity, |
| 226 | Position, |
| 227 | Orientation |
| 228 | } |
| 229 | |
| 230 | public static class OVRNodeStateProperties |
| 231 | { |
| 232 | #if UNITY_2017_1_OR_NEWER |
| 233 | private static List<NodeState> nodeStateList = new List<NodeState>(); |
| 234 | #endif |
| 235 | |
| 236 | public static bool IsHmdPresent() |
| 237 | { |
| 238 | if (OVRManager.OVRManagerinitialized && OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 239 | return OVRPlugin.hmdPresent; |
| 240 | #if USING_XR_SDK |
| 241 | XRDisplaySubsystem currentDisplaySubsystem = OVRManager.GetCurrentDisplaySubsystem(); |
| 242 | if (currentDisplaySubsystem != null) |
| 243 | return currentDisplaySubsystem.running; //In 2019.3, this should be changed to currentDisplaySubsystem.isConnected, but this is a fine placeholder for now. |
| 244 | return false; |
| 245 | #else |
| 246 | return Device.isPresent; |
| 247 | #endif |
| 248 | } |
| 249 | |
| 250 | public static bool GetNodeStatePropertyVector3(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Vector3 retVec) |
| 251 | { |
| 252 | retVec = Vector3.zero; |
| 253 | switch (propertyType) |
| 254 | { |
| 255 | case NodeStatePropertyType.Acceleration: |
| 256 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 257 | { |
| 258 | retVec = OVRPlugin.GetNodeAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f(); |
| 259 | return true; |
| 260 | } |
| 261 | #if UNITY_2017_1_OR_NEWER |
| 262 | if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Acceleration, out retVec)) |
| 263 | return true; |
| 264 | #endif |
| 265 | break; |
| 266 | |
| 267 | case NodeStatePropertyType.AngularAcceleration: |
| 268 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 269 | { |
| 270 | retVec = OVRPlugin.GetNodeAngularAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f(); |
| 271 | return true; |
| 272 | } |
| 273 | #if UNITY_2017_2_OR_NEWER |
| 274 | if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularAcceleration, out retVec)) |
| 275 | return true; |
| 276 | #endif |
| 277 | break; |
| 278 | |
| 279 | case NodeStatePropertyType.Velocity: |
| 280 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 281 | { |
| 282 | retVec = OVRPlugin.GetNodeVelocity(ovrpNodeType, stepType).FromFlippedZVector3f(); |
| 283 | return true; |
| 284 | } |
| 285 | #if UNITY_2017_1_OR_NEWER |
| 286 | if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Velocity, out retVec)) |
| 287 | return true; |
| 288 | #endif |
| 289 | break; |
| 290 | |
| 291 | case NodeStatePropertyType.AngularVelocity: |
| 292 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 293 | { |
| 294 | retVec = OVRPlugin.GetNodeAngularVelocity(ovrpNodeType, stepType).FromFlippedZVector3f(); |
| 295 | return true; |
| 296 | } |
| 297 | #if UNITY_2017_2_OR_NEWER |
| 298 | if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularVelocity, out retVec)) |
| 299 | return true; |
| 300 | #endif |
| 301 | break; |
| 302 | |
| 303 | case NodeStatePropertyType.Position: |
| 304 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 305 | { |
| 306 | retVec = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().position; |
| 307 | return true; |
| 308 | } |
| 309 | #if UNITY_2017_1_OR_NEWER |
| 310 | if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Position, out retVec)) |
| 311 | return true; |
| 312 | #endif |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | public static bool GetNodeStatePropertyQuaternion(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Quaternion retQuat) |
| 320 | { |
| 321 | retQuat = Quaternion.identity; |
| 322 | switch (propertyType) |
| 323 | { |
| 324 | case NodeStatePropertyType.Orientation: |
| 325 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 326 | { |
| 327 | retQuat = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().orientation; |
| 328 | return true; |
| 329 | } |
| 330 | #if UNITY_2017_1_OR_NEWER |
| 331 | if (GetUnityXRNodeStateQuaternion(nodeType, NodeStatePropertyType.Orientation, out retQuat)) |
| 332 | return true; |
| 333 | #endif |
| 334 | break; |
| 335 | } |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | #if UNITY_2017_1_OR_NEWER |
| 340 | private static bool ValidateProperty(Node nodeType, ref NodeState requestedNodeState) |
| 341 | { |
| 342 | InputTracking.GetNodeStates(nodeStateList); |
| 343 | |
| 344 | if (nodeStateList.Count == 0) |
| 345 | return false; |
| 346 | |
| 347 | bool nodeStateFound = false; |
| 348 | requestedNodeState = nodeStateList[0]; |
| 349 | |
| 350 | for (int i = 0; i < nodeStateList.Count; i++) |
| 351 | { |
| 352 | if (nodeStateList[i].nodeType == nodeType) |
| 353 | { |
| 354 | requestedNodeState = nodeStateList[i]; |
| 355 | nodeStateFound = true; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return nodeStateFound; |
| 361 | } |
| 362 | #endif |
| 363 | |
| 364 | #if UNITY_2017_1_OR_NEWER |
| 365 | private static bool GetUnityXRNodeStateVector3(Node nodeType, NodeStatePropertyType propertyType, out Vector3 retVec) |
| 366 | { |
| 367 | retVec = Vector3.zero; |
| 368 | |
| 369 | NodeState requestedNodeState = default(NodeState); |
| 370 | |
| 371 | if (!ValidateProperty(nodeType, ref requestedNodeState)) |
| 372 | return false; |
| 373 | |
| 374 | if (propertyType == NodeStatePropertyType.Acceleration) |
| 375 | { |
| 376 | if (requestedNodeState.TryGetAcceleration(out retVec)) |
| 377 | { |
| 378 | return true; |
| 379 | } |
| 380 | } |
| 381 | else if (propertyType == NodeStatePropertyType.AngularAcceleration) |
| 382 | { |
| 383 | #if UNITY_2017_2_OR_NEWER |
| 384 | if (requestedNodeState.TryGetAngularAcceleration(out retVec)) |
| 385 | { |
| 386 | return true; |
| 387 | } |
| 388 | #endif |
| 389 | } |
| 390 | else if (propertyType == NodeStatePropertyType.Velocity) |
| 391 | { |
| 392 | if (requestedNodeState.TryGetVelocity(out retVec)) |
| 393 | { |
| 394 | return true; |
| 395 | } |
| 396 | } |
| 397 | else if (propertyType == NodeStatePropertyType.AngularVelocity) |
| 398 | { |
| 399 | #if UNITY_2017_2_OR_NEWER |
| 400 | if (requestedNodeState.TryGetAngularVelocity(out retVec)) |
| 401 | { |
| 402 | return true; |
| 403 | } |
| 404 | #endif |
| 405 | } |
| 406 | else if (propertyType == NodeStatePropertyType.Position) |
| 407 | { |
| 408 | if (requestedNodeState.TryGetPosition(out retVec)) |
| 409 | { |
| 410 | return true; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return false; |
| 415 | } |
| 416 | #endif |
| 417 | |
| 418 | #if UNITY_2017_1_OR_NEWER |
| 419 | private static bool GetUnityXRNodeStateQuaternion(Node nodeType, NodeStatePropertyType propertyType, out Quaternion retQuat) |
| 420 | { |
| 421 | retQuat = Quaternion.identity; |
| 422 | |
| 423 | NodeState requestedNodeState = default(NodeState); |
| 424 | |
| 425 | if (!ValidateProperty(nodeType, ref requestedNodeState)) |
| 426 | return false; |
| 427 | |
| 428 | if (propertyType == NodeStatePropertyType.Orientation) |
| 429 | { |
| 430 | if (requestedNodeState.TryGetRotation(out retQuat)) |
| 431 | { |
| 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return false; |
| 437 | } |
| 438 | #endif |
| 439 | |
| 440 | } |
| 441 | |
| 442 | /// <summary> |
| 443 | /// An affine transformation built from a Unity position and orientation. |
| 444 | /// </summary> |
| 445 | [System.Serializable] |
| 446 | public struct OVRPose |
| 447 | { |
| 448 | /// <summary> |
| 449 | /// A pose with no translation or rotation. |
| 450 | /// </summary> |
| 451 | public static OVRPose identity |
| 452 | { |
| 453 | get { |
| 454 | return new OVRPose() |
| 455 | { |
| 456 | position = Vector3.zero, |
| 457 | orientation = Quaternion.identity |
| 458 | }; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | public override bool Equals(System.Object obj) |
| 463 | { |
| 464 | return obj is OVRPose && this == (OVRPose)obj; |
| 465 | } |
| 466 | |
| 467 | public override int GetHashCode() |
| 468 | { |
| 469 | return position.GetHashCode() ^ orientation.GetHashCode(); |
| 470 | } |
| 471 | |
| 472 | public static bool operator ==(OVRPose x, OVRPose y) |
| 473 | { |
| 474 | return x.position == y.position && x.orientation == y.orientation; |
| 475 | } |
| 476 | |
| 477 | public static bool operator !=(OVRPose x, OVRPose y) |
| 478 | { |
| 479 | return !(x == y); |
| 480 | } |
| 481 | |
| 482 | /// <summary> |
| 483 | /// The position. |
| 484 | /// </summary> |
| 485 | public Vector3 position; |
| 486 | |
| 487 | /// <summary> |
| 488 | /// The orientation. |
| 489 | /// </summary> |
| 490 | public Quaternion orientation; |
| 491 | |
| 492 | /// <summary> |
| 493 | /// Multiplies two poses. |
| 494 | /// </summary> |
| 495 | public static OVRPose operator*(OVRPose lhs, OVRPose rhs) |
| 496 | { |
| 497 | var ret = new OVRPose(); |
| 498 | ret.position = lhs.position + lhs.orientation * rhs.position; |
| 499 | ret.orientation = lhs.orientation * rhs.orientation; |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | /// <summary> |
| 504 | /// Computes the inverse of the given pose. |
| 505 | /// </summary> |
| 506 | public OVRPose Inverse() |
| 507 | { |
| 508 | OVRPose ret; |
| 509 | ret.orientation = Quaternion.Inverse(orientation); |
| 510 | ret.position = ret.orientation * -position; |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | /// <summary> |
| 515 | /// Converts the pose from left- to right-handed or vice-versa. |
| 516 | /// </summary> |
| 517 | public OVRPose flipZ() |
| 518 | { |
| 519 | var ret = this; |
| 520 | ret.position.z = -ret.position.z; |
| 521 | ret.orientation.z = -ret.orientation.z; |
| 522 | ret.orientation.w = -ret.orientation.w; |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | public OVRPlugin.Posef ToPosef() |
| 527 | { |
| 528 | return new OVRPlugin.Posef() |
| 529 | { |
| 530 | Position = position.ToVector3f(), |
| 531 | Orientation = orientation.ToQuatf() |
| 532 | }; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /// <summary> |
| 537 | /// Encapsulates an 8-byte-aligned of unmanaged memory. |
| 538 | /// </summary> |
| 539 | public class OVRNativeBuffer : IDisposable |
| 540 | { |
| 541 | private bool disposed = false; |
| 542 | private int m_numBytes = 0; |
| 543 | private IntPtr m_ptr = IntPtr.Zero; |
| 544 | |
| 545 | /// <summary> |
| 546 | /// Creates a buffer of the specified size. |
| 547 | /// </summary> |
| 548 | public OVRNativeBuffer(int numBytes) |
| 549 | { |
| 550 | Reallocate(numBytes); |
| 551 | } |
| 552 | |
| 553 | /// <summary> |
| 554 | /// Releases unmanaged resources and performs other cleanup operations before the <see cref="OVRNativeBuffer"/> is |
| 555 | /// reclaimed by garbage collection. |
| 556 | /// </summary> |
| 557 | ~OVRNativeBuffer() |
| 558 | { |
| 559 | Dispose(false); |
| 560 | } |
| 561 | |
| 562 | /// <summary> |
| 563 | /// Reallocates the buffer with the specified new size. |
| 564 | /// </summary> |
| 565 | public void Reset(int numBytes) |
| 566 | { |
| 567 | Reallocate(numBytes); |
| 568 | } |
| 569 | |
| 570 | /// <summary> |
| 571 | /// The current number of bytes in the buffer. |
| 572 | /// </summary> |
| 573 | public int GetCapacity() |
| 574 | { |
| 575 | return m_numBytes; |
| 576 | } |
| 577 | |
| 578 | /// <summary> |
| 579 | /// A pointer to the unmanaged memory in the buffer, starting at the given offset in bytes. |
| 580 | /// </summary> |
| 581 | public IntPtr GetPointer(int byteOffset = 0) |
| 582 | { |
| 583 | if (byteOffset < 0 || byteOffset >= m_numBytes) |
| 584 | return IntPtr.Zero; |
| 585 | return (byteOffset == 0) ? m_ptr : new IntPtr(m_ptr.ToInt64() + byteOffset); |
| 586 | } |
| 587 | |
| 588 | /// <summary> |
| 589 | /// Releases all resource used by the <see cref="OVRNativeBuffer"/> object. |
| 590 | /// </summary> |
| 591 | /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="OVRNativeBuffer"/>. The <see cref="Dispose"/> |
| 592 | /// method leaves the <see cref="OVRNativeBuffer"/> in an unusable state. After calling <see cref="Dispose"/>, you must |
| 593 | /// release all references to the <see cref="OVRNativeBuffer"/> so the garbage collector can reclaim the memory that |
| 594 | /// the <see cref="OVRNativeBuffer"/> was occupying.</remarks> |
| 595 | public void Dispose() |
| 596 | { |
| 597 | Dispose(true); |
| 598 | GC.SuppressFinalize(this); |
| 599 | } |
| 600 | |
| 601 | private void Dispose(bool disposing) |
| 602 | { |
| 603 | if (disposed) |
| 604 | return; |
| 605 | |
| 606 | if (disposing) |
| 607 | { |
| 608 | // dispose managed resources |
| 609 | } |
| 610 | |
| 611 | // dispose unmanaged resources |
| 612 | Release(); |
| 613 | |
| 614 | disposed = true; |
| 615 | } |
| 616 | |
| 617 | private void Reallocate(int numBytes) |
| 618 | { |
| 619 | Release(); |
| 620 | |
| 621 | if (numBytes > 0) |
| 622 | { |
| 623 | m_ptr = Marshal.AllocHGlobal(numBytes); |
| 624 | m_numBytes = numBytes; |
| 625 | } |
| 626 | else |
| 627 | { |
| 628 | m_ptr = IntPtr.Zero; |
| 629 | m_numBytes = 0; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | private void Release() |
| 634 | { |
| 635 | if (m_ptr != IntPtr.Zero) |
| 636 | { |
| 637 | Marshal.FreeHGlobal(m_ptr); |
| 638 | m_ptr = IntPtr.Zero; |
| 639 | m_numBytes = 0; |
| 640 | } |
| 641 | } |
| 642 | } |