SDL: Don't try to create a vbo if we aren't using any vertices

From b08e57e9263f034f3ea1262902769bb1f3e273f1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 14 Dec 2021 09:32:05 -0800
Subject: [PATCH] Don't try to create a vbo if we aren't using any vertices

---
 src/render/direct3d/SDL_render_d3d.c | 66 ++++++++++++++--------------
 1 file changed, 34 insertions(+), 32 deletions(-)

diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index fb5fc628913..3a876e1b8f1 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -1090,46 +1090,48 @@ D3D_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
         return -1;
     }
 
-    /* upload the new VBO data for this set of commands. */
-    vbo = data->vertexBuffers[vboidx];
-    if (data->vertexBufferSize[vboidx] < vertsize) {
-        const DWORD usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
-        const DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
-        if (vbo) {
-            IDirect3DVertexBuffer9_Release(vbo);
-        }
+    if (vertices) {
+        /* upload the new VBO data for this set of commands. */
+        vbo = data->vertexBuffers[vboidx];
+        if (data->vertexBufferSize[vboidx] < vertsize) {
+            const DWORD usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
+            const DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
+            if (vbo) {
+                IDirect3DVertexBuffer9_Release(vbo);
+            }
 
-        if (FAILED(IDirect3DDevice9_CreateVertexBuffer(data->device, (UINT) vertsize, usage, fvf, D3DPOOL_DEFAULT, &vbo, NULL))) {
-            vbo = NULL;
+            if (FAILED(IDirect3DDevice9_CreateVertexBuffer(data->device, (UINT) vertsize, usage, fvf, D3DPOOL_DEFAULT, &vbo, NULL))) {
+                vbo = NULL;
+            }
+            data->vertexBuffers[vboidx] = vbo;
+            data->vertexBufferSize[vboidx] = vbo ? vertsize : 0;
         }
-        data->vertexBuffers[vboidx] = vbo;
-        data->vertexBufferSize[vboidx] = vbo ? vertsize : 0;
-    }
 
-    if (vbo) {
-        void *ptr;
-        if (FAILED(IDirect3DVertexBuffer9_Lock(vbo, 0, (UINT) vertsize, &ptr, D3DLOCK_DISCARD))) {
-            vbo = NULL;  /* oh well, we'll do immediate mode drawing.  :(  */
-        } else {
-            SDL_memcpy(ptr, vertices, vertsize);
-            if (FAILED(IDirect3DVertexBuffer9_Unlock(vbo))) {
+        if (vbo) {
+            void *ptr;
+            if (FAILED(IDirect3DVertexBuffer9_Lock(vbo, 0, (UINT) vertsize, &ptr, D3DLOCK_DISCARD))) {
                 vbo = NULL;  /* oh well, we'll do immediate mode drawing.  :(  */
+            } else {
+                SDL_memcpy(ptr, vertices, vertsize);
+                if (FAILED(IDirect3DVertexBuffer9_Unlock(vbo))) {
+                    vbo = NULL;  /* oh well, we'll do immediate mode drawing.  :(  */
+                }
             }
         }
-    }
 
-    /* cycle through a few VBOs so D3D has some time with the data before we replace it. */
-    if (vbo) {
-        data->currentVertexBuffer++;
-        if (data->currentVertexBuffer >= SDL_arraysize(data->vertexBuffers)) {
-            data->currentVertexBuffer = 0;
+        /* cycle through a few VBOs so D3D has some time with the data before we replace it. */
+        if (vbo) {
+            data->currentVertexBuffer++;
+            if (data->currentVertexBuffer >= SDL_arraysize(data->vertexBuffers)) {
+                data->currentVertexBuffer = 0;
+            }
+        } else if (!data->reportedVboProblem) {
+            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL failed to get a vertex buffer for this Direct3D 9 rendering batch!");
+            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Dropping back to a slower method.");
+            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "This might be a brief hiccup, but if performance is bad, this is probably why.");
+            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "This error will not be logged again for this renderer.");
+            data->reportedVboProblem = SDL_TRUE;
         }
-    } else if (!data->reportedVboProblem) {
-        SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL failed to get a vertex buffer for this Direct3D 9 rendering batch!");
-        SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Dropping back to a slower method.");
-        SDL_LogError(SDL_LOG_CATEGORY_RENDER, "This might be a brief hiccup, but if performance is bad, this is probably why.");
-        SDL_LogError(SDL_LOG_CATEGORY_RENDER, "This error will not be logged again for this renderer.");
-        data->reportedVboProblem = SDL_TRUE;
     }
 
     IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, 0, sizeof (Vertex));