stride3d/stride · error · InvalidOperationException

Cannot use a different context between Load and Draw

Error message

Cannot use a different context between Load and Draw

What it means

RendererCoreBase caches the RenderContext it was initialized/loaded with and requires the same context for every subsequent Draw. EnsureContext compares the incoming context with the stored one; a mismatch means the renderer's state (services, graphics device, resources) belongs to a different pipeline instance, so it throws InvalidOperationException rather than silently drawing with the wrong context.

Solutions

  1. Ensure each RenderFeature/renderer is used with exactly one RenderContext; create a separate instance per context
  2. If sharing is intended, reset the renderer (Destroy/Dispose and recreate) when switching contexts
  3. Check for accidentally cached/shared RenderFeature instances (static fields, singleton services) between scenes or games
  4. Route all draws through the compositor that owns the renderer instead of calling Draw directly with a foreign context

Example fix

// before
sharedFeature.Draw(drawContextOfOtherScene); // InvalidOperationException
// after
var feature = new MyRenderFeature();
feature.Initialize(sceneContext);
feature.Draw(sceneDrawContext);
Defensive patterns

Strategy: validation

Validate before calling

if (renderer.Context != null && renderer.Context != context.RenderContext)
    throw new InvalidOperationException("Renderer bound to a different RenderContext.");

Try / catch

try
{
    feature.Draw(drawContext);
}
catch (InvalidOperationException) when (ex.Message.Contains("different context"))
{
    // recreate feature bound to this context
    feature.Dispose();
    feature = CreateFeatureFor(context);
}

Prevention

When it happens

Trigger: Calling PreDrawCoreInternal/Draw with a RenderDrawContext whose RenderContext differs from the one used during Initialize/Load — e.g. rendering the same feature through two GraphicsCompositors/render systems, sharing a RenderFeature or Material across two scenes/games, or drawing on a different RenderContext than the one the renderer loaded with.

Common situations: Reusing render stages, features or renderers across multiple SceneRenderers; multi-window or offscreen rendering where each target has its own RenderContext; running rendering logic on another Game instance's context.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/6bfeeed848f82f0c. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Rendering/Rendering/RendererCoreBase.cs:251

            {
                context.QueryManager.BeginProfile(Color.Green, GPUProfilingKey);
            }

            PreDrawCore(context);

            // Allow scoped allocation RenderTargets
            isInDrawCore = true;
        }

        protected void EnsureContext(RenderContext context)
        {
            if (Context == null)
            {
                Initialize(context);
            }
            else if (Context != context)
            {
                throw new InvalidOperationException("Cannot use a different context between Load and Draw");
            }
        }

        protected void PostDrawCoreInternal(RenderDrawContext context)
        {
            isInDrawCore = false;

            // Release scoped RenderTargets
            ReleaseAllScopedResources();

            PostDrawCore(context);

            if (GPUProfilingKey.Name != null && Profiling)
            {
                context.QueryManager.EndProfile(GPUProfilingKey);
            }
        }
    }

View on GitHub (pinned to 96fad776d2)