static copyDepthData(
      sourceDepthArrayBuffer, width, height, sourceFormatEnum,
      sourceClipFromSourceView, mojoFromSourceView,
      targetFormatEnum,
      targetClipFromTargetView, mojoFromTargetView) {

      const targetViewFromTargetClip = XRMathHelper.inverse(targetClipFromTargetView);
      const sourceViewFromMojo = XRMathHelper.inverse(mojoFromSourceView);

      // Check if any matrices were not supplied or matrix inversions failed.
      if (!targetViewFromTargetClip || !sourceViewFromMojo || !mojoFromTargetView) {
          return null;
      }

      // Build the full transformation from Target Clip space to Source Clip space.
      const mojoFromTargetClip = XRMathHelper.mul4x4(mojoFromTargetView, targetViewFromTargetClip);
      if (!mojoFromTargetClip) return null;

      const sourceViewFromTargetClip = XRMathHelper.mul4x4(sourceViewFromMojo, mojoFromTargetClip);
      if (!sourceViewFromTargetClip) return null;

      const sourceClipFromTargetClip = XRMathHelper.mul4x4(sourceClipFromSourceView, sourceViewFromTargetClip);
      if (!sourceClipFromTargetClip) return null;

      const sourceTypedArray = MockRuntime._getTypedArrayForFormat(sourceDepthArrayBuffer, sourceFormatEnum);
      let internalTargetDepthTypedArray = MockRuntime._getEmptyTypedArrayForFormat(targetFormatEnum, width * height);

      // Iterate over target pixels (Backward Mapping)
      for (let ty = 0; ty < height; ++ty) {
          for (let tx = 0; tx < width; ++tx) {
              // Convert target pixel (tx, ty) to target NDC coordinates
              const u_tgt_pixel = (tx + 0.5) / width;  // u in [0, 1], Y-down from top-left
              const v_tgt_pixel = (ty + 0.5) / height; // v in [0, 1], Y-down from top-left

              const ndc_x_tgt = u_tgt_pixel * 2.0 - 1.0;   // NDC X in [-1, 1]
              const ndc_y_tgt = 1.0 - v_tgt_pixel * 2.0;   // NDC Y in [-1, 1], Y-up

              // Define a point on the near plane in target clip space
              const P_clip_tgt = { x: ndc_x_tgt, y: ndc_y_tgt, z: -1.0, w: 1.0 };

              // Transform this point to source clip space
              const P_clip_src = XRMathHelper.transform_by_matrix(sourceClipFromTargetClip, P_clip_tgt);

              // Homogenize to get source NDC coordinates
              if (Math.abs(P_clip_src.w) < XRMathHelper.EPSILON) {
                  internalTargetDepthTypedArray[ty * width + tx] = 0; // Cannot project
                  continue;
              }
              const ndc_x_src = P_clip_src.x / P_clip_src.w;
              const ndc_y_src = P_clip_src.y / P_clip_src.w;

              // Convert source NDC to source pixel coordinates
              const u_src_pixel = (ndc_x_src + 1.0) / 2.0;
              const v_src_pixel = (1.0 - ndc_y_src) / 2.0; // Convert source NDC Y-up to pixel Y-down

              const sx = Math.floor(u_src_pixel * width);
              const sy = Math.floor(v_src_pixel * height);

              let target_raw_depth = 0; // Default to 0 (no data)

              // Check if the calculated source pixel is within bounds
              if (sx >= 0 && sx < width && sy >= 0 && sy < height) {
                  const source_raw_value = sourceTypedArray[sy * width + sx];

                  let isValidSourceDepth = false;
                  if (sourceFormatEnum === xrSessionMojom.XRDepthDataFormat.kFloat32) {
                      if (source_raw_value > 0 && isFinite(source_raw_value)) {
                          isValidSourceDepth = true;
                      }
                  } else { // Uint16 source
                      if (source_raw_value > 0) {
                          isValidSourceDepth = true;
                      }
                  }

                  if (isValidSourceDepth) {
                      if (targetFormatEnum === xrSessionMojom.XRDepthDataFormat.kFloat32) {
                          target_raw_depth = source_raw_value;
                      } else {
                          // Clamp to the valid range for Uint16
                          target_raw_depth = Math.max(0, Math.min(0xFFFF, Math.round(source_raw_value)));
                      }
                  }
              }

              // If not in bounds or source depth invalid, target_raw_depth remains 0.
              internalTargetDepthTypedArray[ty * width + tx] = target_raw_depth;
          }
      }

      return new Uint8Array(internalTargetDepthTypedArray.buffer);
  }
Reprojects depth data from a source view to a target view.
The returned array will be the same width/height, but will be returned as a
Uint8Array of the targetFormat (essentially a byte array that can be sent
across mojo), so the overall returned size may be different.

@param {ArrayBuffer} sourceDepthArrayBuffer - Raw depth data for the source.
@param {number} width - Width of the depth data.
@param {number} height - Height of the depth data.
@param {xrSessionMojom.XRDepthDataFormat} sourceFormatEnum - Format of the source depth data.
@param {Float32Array} sourceClipFromSourceView - Projection matrix for the source view.
@param {Float32Array} mojoFromSourceView- Matrix of the transform for the source view.
@param {xrSessionMojom.XRDepthDataFormat} targetFormatEnum - Format of the target depth data.
@param {Float32Array} targetClipFromTargetView - Projection matrix for the target view.
@param {Float32Array} mojoFromTargetView - Matrix of the transform for the target view.
@return {Uint8Array | null} The reprojected depth data as an Uint8Array, or null on matrix error.