# SDL: End the scene before resetting the D3D device, since we'll start with BeginScene after that.

**URL:** <https://discourse.libsdl.org/t/sdl-end-the-scene-before-resetting-the-d3d-device-since-well-start-with-beginscene-after-that/33399>\
**Category:** SDL Commits\
**Created:** [November 10, 2021, 1:28pm UTC](https://discourse.libsdl.org/t/sdl-end-the-scene-before-resetting-the-d3d-device-since-well-start-with-beginscene-after-that/33399 "2021-11-10T13:28:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [November 10, 2021, 1:28pm UTC](https://discourse.libsdl.org/t/sdl-end-the-scene-before-resetting-the-d3d-device-since-well-start-with-beginscene-after-that/33399/1 "2021-11-10T13:28:02Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/3a31a450287212307e4796494e9c6642b3824310>
>
> If we don't do this, we'll end up with unbalanced Begin/End scene pairs which ca…uses D3DERR\_INVALIDCALL in the present.
> 
> Fixes https://github.com/libsdl-org/SDL/issues/4933

```diff
From 3a31a450287212307e4796494e9c6642b3824310 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 10 Nov 2021 05:24:31 -0800
Subject: [PATCH] End the scene before resetting the D3D device, since we'll
 start with BeginScene after that.

If we don't do this, we'll end up with unbalanced Begin/End scene pairs which causes D3DERR_INVALIDCALL in the present.

Fixes https://github.com/libsdl-org/SDL/issues/4933
---
 src/render/direct3d/SDL_render_d3d.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index 06c1740949..fb5fc62891 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -1425,6 +1425,12 @@ D3D_Reset(SDL_Renderer * renderer)
     SDL_Texture *texture;
     int i;
 
+ /* Cancel any scene that we've started */
+ if (!data->beginScene) {
+ IDirect3DDevice9_EndScene(data->device);
+ data->beginScene = SDL_TRUE;
+ }
+
     /* Release the default render target before reset */
     if (data->defaultRenderTarget) {
         IDirect3DSurface9_Release(data->defaultRenderTarget);

```
