MonoGame/MonoGame · error · ArgumentOutOfRangeException

cubeMapFace

Error message

cubeMapFace

What it means

Thrown by the RenderTargetBinding(RenderTargetCube, CubeMapFace) constructor when cubeMapFace is outside the valid CubeMapFace enum range (PositiveX..NegativeZ). The face index maps directly to an array slice, so an out-of-range value would index an invalid cube face.

Source

Thrown at MonoGame.Framework/Graphics/RenderTargetBinding.cs:75

            _depthFormat = renderTarget.DepthStencilFormat;
		}

        /// <summary>
        /// Creates a new <b>RenderTargetBinding</b> with the specified parameters.
        /// </summary>
        /// <param name="renderTarget">A cube map render target.</param>
        /// <param name="cubeMapFace">The cube map of the render target.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="renderTarget"/> parameter is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The <paramref name="cubeMapFace"/> parameter is less than <see cref="CubeMapFace.PositiveX"/> or greater
        /// than <see cref="CubeMapFace.NegativeZ"/>.
        /// </exception>
        public RenderTargetBinding(RenderTargetCube renderTarget, CubeMapFace cubeMapFace)
        {
            if (renderTarget == null)
                throw new ArgumentNullException("renderTarget");
            if (cubeMapFace < CubeMapFace.PositiveX || cubeMapFace > CubeMapFace.NegativeZ)
                throw new ArgumentOutOfRangeException("cubeMapFace");

            _renderTarget = renderTarget;
            _arraySlice = (int)cubeMapFace;
            _depthFormat = renderTarget.DepthStencilFormat;
        }

#if DIRECTX

        /// <summary>
        /// Creates a new <b>RenderTargetBinding</b> with the specified parameters.
        /// </summary>
        /// <param name="renderTarget">The render target to create the binding for.</param>
        /// <param name="arraySlice">The index of the texture array slice to bind.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="renderTarget" /> parameter is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The <paramref name="arraySlice" /> is less than zero or greater than the array size of the render target
        /// </exception>
        /// <exception cref="InvalidOperationException">Texture arrays are not supported by the current graphics device.</exception>

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Clamp or validate the face value to the CubeMapFace enum range before constructing the binding.
  2. Use a switch/mapping from your computed index to an explicit CubeMapFace value.
  3. Ensure the variable is assigned from CubeMapFace members, not arbitrary integers.

Example fix

// before
var face = (CubeMapFace)computedIndex; // could be out of range
var binding = new RenderTargetBinding(_cube, face);

// after
if (computedIndex < 0 || computedIndex > 5)
    throw new ArgumentOutOfRangeException(nameof(computedIndex));
var face = (CubeMapFace)computedIndex;
var binding = new RenderTargetBinding(_cube, face);
Defensive patterns

Strategy: validation

Validate before calling

if (cubeMapFace < CubeMapFace.PositiveX || cubeMapFace > CubeMapFace.NegativeZ)
    throw new ArgumentOutOfRangeException(nameof(cubeMapFace));
var binding = new RenderTargetBinding(renderTarget, cubeMapFace);

Type guard

static bool IsValidCubeFace(CubeMapFace f) => f >= CubeMapFace.PositiveX && f <= CubeMapFace.NegativeZ;

Prevention

When it happens

Trigger: Casting an arbitrary integer to CubeMapFace without range-checking; passing an uninitialized CubeMapFace field (defaults to 0 = PositiveX, so this is rare); computing a face from math that overflows the enum bounds.

Common situations: Dynamic face selection in cube-map rendering where the index is computed from a direction vector and not clamped; passing a raw int cast from an external data source.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/6e34490d6da93173. Report an issue: GitHub.