Skip to content

Commit

Permalink
d3d12: Move platform-specific includes/defines to SDL_d3d12.h
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo authored and slouken committed Aug 1, 2024
1 parent 2e058ba commit efefc4a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 93 deletions.
111 changes: 35 additions & 76 deletions src/render/direct3d12/SDL_render_d3d12.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,10 @@
#include "../../video/windows/SDL_windowswindow.h"
#include "../SDL_sysrender.h"
#include "../SDL_d3dmath.h"
#include "../../video/directx/SDL_d3d12.h"

#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
#include "SDL_render_d3d12_xbox.h"
#ifndef D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
#define D3D12_TEXTURE_DATA_PITCH_ALIGNMENT 256
#endif
#else

/* From the DirectX-Headers build system:
* "MinGW has RPC headers which define old versions, and complain if D3D
* headers are included before the RPC headers, since D3D headers were
* generated with new MIDL and "require" new RPC headers."
*/
#define __REQUIRED_RPCNDR_H_VERSION__ 475
#ifndef WINAPI_PARTITION_GAMES
#define WINAPI_PARTITION_GAMES 0
#endif /* WINAPI_PARTITION_GAMES */
#include "../../video/directx/d3d12.h"

#include <dxgi1_6.h>
#include <dxgidebug.h>
#include <d3d12sdklayers.h>
#include <sdkddkver.h>
#endif

#include "SDL_shaders_d3d12.h"
Expand All @@ -64,28 +45,6 @@
#define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str
#endif

#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
#define SAFE_RELEASE(X) \
if (X) { \
(X)->Release(); \
X = NULL; \
}
#define D3D_CALL(THIS, FUNC, ...) (THIS)->FUNC(__VA_ARGS__)
#define D3D_CALL_RET(THIS, FUNC, RETVAL, ...) *(RETVAL) = (THIS)->FUNC(__VA_ARGS__)
#define D3D_GUID(X) (X)
/* DXGI_PRESENT flags are removed on Xbox */
#define DXGI_PRESENT_ALLOW_TEARING 0
#else
#define SAFE_RELEASE(X) \
if (X) { \
(X)->lpVtbl->Release(X); \
X = NULL; \
}
#define D3D_CALL(THIS, FUNC, ...) (THIS)->lpVtbl->FUNC((THIS), ##__VA_ARGS__)
#define D3D_CALL_RET(THIS, FUNC, ...) (THIS)->lpVtbl->FUNC((THIS), ##__VA_ARGS__)
#define D3D_GUID(X) &(X)
#endif

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -431,39 +390,39 @@ static void D3D12_ReleaseAll(SDL_Renderer *renderer)
int i;

#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
SAFE_RELEASE(data->dxgiFactory);
SAFE_RELEASE(data->dxgiAdapter);
SAFE_RELEASE(data->swapChain);
D3D_SAFE_RELEASE(data->dxgiFactory);
D3D_SAFE_RELEASE(data->dxgiAdapter);
D3D_SAFE_RELEASE(data->swapChain);
#endif
SAFE_RELEASE(data->d3dDevice);
SAFE_RELEASE(data->debugInterface);
SAFE_RELEASE(data->commandQueue);
SAFE_RELEASE(data->commandList);
SAFE_RELEASE(data->rtvDescriptorHeap);
SAFE_RELEASE(data->textureRTVDescriptorHeap);
SAFE_RELEASE(data->srvDescriptorHeap);
SAFE_RELEASE(data->samplerDescriptorHeap);
SAFE_RELEASE(data->fence);
D3D_SAFE_RELEASE(data->d3dDevice);
D3D_SAFE_RELEASE(data->debugInterface);
D3D_SAFE_RELEASE(data->commandQueue);
D3D_SAFE_RELEASE(data->commandList);
D3D_SAFE_RELEASE(data->rtvDescriptorHeap);
D3D_SAFE_RELEASE(data->textureRTVDescriptorHeap);
D3D_SAFE_RELEASE(data->srvDescriptorHeap);
D3D_SAFE_RELEASE(data->samplerDescriptorHeap);
D3D_SAFE_RELEASE(data->fence);

for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
SAFE_RELEASE(data->commandAllocators[i]);
SAFE_RELEASE(data->renderTargets[i]);
D3D_SAFE_RELEASE(data->commandAllocators[i]);
D3D_SAFE_RELEASE(data->renderTargets[i]);
}

if (data->pipelineStateCount > 0) {
for (i = 0; i < data->pipelineStateCount; ++i) {
SAFE_RELEASE(data->pipelineStates[i].pipelineState);
D3D_SAFE_RELEASE(data->pipelineStates[i].pipelineState);
}
SDL_free(data->pipelineStates);
data->pipelineStateCount = 0;
}

for (i = 0; i < NUM_ROOTSIGS; ++i) {
SAFE_RELEASE(data->rootSignatures[i]);
D3D_SAFE_RELEASE(data->rootSignatures[i]);
}

for (i = 0; i < SDL_D3D12_NUM_VERTEX_BUFFERS; ++i) {
SAFE_RELEASE(data->vertexBuffers[i].resource);
D3D_SAFE_RELEASE(data->vertexBuffers[i].resource);
data->vertexBuffers[i].size = 0;
}

Expand All @@ -477,7 +436,7 @@ static void D3D12_ReleaseAll(SDL_Renderer *renderer)
if (data->dxgiDebug) {
DXGI_DEBUG_RLO_FLAGS rloFlags = (DXGI_DEBUG_RLO_FLAGS)(DXGI_DEBUG_RLO_DETAIL | DXGI_DEBUG_RLO_IGNORE_INTERNAL);
D3D_CALL(data->dxgiDebug, ReportLiveObjects, SDL_DXGI_DEBUG_ALL, rloFlags);
SAFE_RELEASE(data->dxgiDebug);
D3D_SAFE_RELEASE(data->dxgiDebug);
}
#endif

Expand Down Expand Up @@ -578,7 +537,7 @@ static void D3D12_ResetCommandList(D3D12_RenderData *data)

/* Release any upload buffers that were inflight */
for (i = 0; i < data->currentUploadBuffer; ++i) {
SAFE_RELEASE(data->uploadBuffers[i]);
D3D_SAFE_RELEASE(data->uploadBuffers[i]);
}
data->currentUploadBuffer = 0;

Expand Down Expand Up @@ -737,7 +696,7 @@ static D3D12_PipelineState *D3D12_CreatePipelineState(SDL_Renderer *renderer,

pipelineStates = (D3D12_PipelineState *)SDL_realloc(data->pipelineStates, (data->pipelineStateCount + 1) * sizeof(*pipelineStates));
if (!pipelineStates) {
SAFE_RELEASE(pipelineState);
D3D_SAFE_RELEASE(pipelineState);
return NULL;
}

Expand All @@ -758,7 +717,7 @@ static HRESULT D3D12_CreateVertexBuffer(D3D12_RenderData *data, size_t vbidx, si
D3D12_RESOURCE_DESC vbufferDesc;
HRESULT result;

SAFE_RELEASE(data->vertexBuffers[vbidx].resource);
D3D_SAFE_RELEASE(data->vertexBuffers[vbidx].resource);

SDL_zero(vbufferHeapProps);
vbufferHeapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
Expand Down Expand Up @@ -914,7 +873,7 @@ static HRESULT D3D12_CreateDeviceResources(SDL_Renderer *renderer)

D3D_CALL(dxgiInfoQueue, SetBreakOnSeverity, SDL_DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, TRUE);
D3D_CALL(dxgiInfoQueue, SetBreakOnSeverity, SDL_DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, TRUE);
SAFE_RELEASE(dxgiInfoQueue);
D3D_SAFE_RELEASE(dxgiInfoQueue);
#endif /* __IDXGIInfoQueue_INTERFACE_DEFINED__ */
creationFlags = DXGI_CREATE_FACTORY_DEBUG;
}
Expand Down Expand Up @@ -965,7 +924,7 @@ static HRESULT D3D12_CreateDeviceResources(SDL_Renderer *renderer)
D3D_CALL(infoQueue, SetBreakOnSeverity, D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
D3D_CALL(infoQueue, SetBreakOnSeverity, D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);

SAFE_RELEASE(infoQueue);
D3D_SAFE_RELEASE(infoQueue);
}
#endif /*!defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)*/

Expand Down Expand Up @@ -1178,7 +1137,7 @@ static HRESULT D3D12_CreateDeviceResources(SDL_Renderer *renderer)
SDL_SetPointerProperty(props, SDL_PROP_RENDERER_D3D12_COMMAND_QUEUE_POINTER, data->commandQueue);

done:
SAFE_RELEASE(d3dDevice);
D3D_SAFE_RELEASE(d3dDevice);
return result;
}

Expand Down Expand Up @@ -1361,7 +1320,7 @@ static HRESULT D3D12_CreateSwapChain(SDL_Renderer *renderer, int w, int h)
SDL_SetPointerProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_D3D12_SWAPCHAIN_POINTER, data->swapChain);

done:
SAFE_RELEASE(swapChain);
D3D_SAFE_RELEASE(swapChain);
return result;
}
#endif
Expand Down Expand Up @@ -1414,7 +1373,7 @@ static HRESULT D3D12_CreateWindowSizeDependentResources(SDL_Renderer *renderer)

/* Release render targets */
for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
SAFE_RELEASE(data->renderTargets[i]);
D3D_SAFE_RELEASE(data->renderTargets[i]);
}

/* The width and height of the swap chain must be based on the display's
Expand Down Expand Up @@ -1827,12 +1786,12 @@ static void D3D12_DestroyTexture(SDL_Renderer *renderer,
Unfortunately, this means that deleting a lot of textures mid-frame will have poor performance. */
D3D12_IssueBatch(rendererData);

SAFE_RELEASE(textureData->mainTexture);
SAFE_RELEASE(textureData->stagingBuffer);
D3D_SAFE_RELEASE(textureData->mainTexture);
D3D_SAFE_RELEASE(textureData->stagingBuffer);
D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndex);
#if SDL_HAVE_YUV
SAFE_RELEASE(textureData->mainTextureU);
SAFE_RELEASE(textureData->mainTextureV);
D3D_SAFE_RELEASE(textureData->mainTextureU);
D3D_SAFE_RELEASE(textureData->mainTextureV);
if (textureData->yuv) {
D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexU);
D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexV);
Expand Down Expand Up @@ -1923,7 +1882,7 @@ static int D3D12_UpdateTextureInternal(D3D12_RenderData *rendererData, ID3D12Res
NULL,
(void **)&textureMemory);
if (FAILED(result)) {
SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
D3D_SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
}

Expand Down Expand Up @@ -2172,7 +2131,7 @@ static int D3D12_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
NULL,
(void **)&textureMemory);
if (FAILED(result)) {
SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
D3D_SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
}

Expand Down Expand Up @@ -2277,7 +2236,7 @@ static void D3D12_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)

/* Execute the command list before releasing the staging buffer */
D3D12_IssueBatch(rendererData);
SAFE_RELEASE(textureData->stagingBuffer);
D3D_SAFE_RELEASE(textureData->stagingBuffer);
}

static void D3D12_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
Expand Down Expand Up @@ -3136,7 +3095,7 @@ static SDL_Surface *D3D12_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rec
D3D_CALL(readbackBuffer, Unmap, 0, NULL);

done:
SAFE_RELEASE(readbackBuffer);
D3D_SAFE_RELEASE(readbackBuffer);
return output;
}

Expand Down
7 changes: 1 addition & 6 deletions src/render/direct3d12/SDL_render_d3d12_xbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
#define SDL_render_d3d12_xbox_h_

#include "../../SDL_internal.h"

#if defined(SDL_PLATFORM_XBOXONE)
#include <d3d12_x.h>
#else /* SDL_PLATFORM_XBOXSERIES */
#include <d3d12_xs.h>
#endif
#include "../../video/directx/SDL_d3d12.h"

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
Expand Down
12 changes: 1 addition & 11 deletions src/render/direct3d12/SDL_shaders_d3d12.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@
#if SDL_VIDEO_RENDER_D3D12 && !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)

#include "../../core/windows/SDL_windows.h"

/* From the DirectX-Headers build system:
* "MinGW has RPC headers which define old versions, and complain if D3D
* headers are included before the RPC headers, since D3D headers were
* generated with new MIDL and "require" new RPC headers."
*/
#define __REQUIRED_RPCNDR_H_VERSION__ 475
#ifndef WINAPI_PARTITION_GAMES
#define WINAPI_PARTITION_GAMES 0
#endif /* WINAPI_PARTITION_GAMES */
#include "../../video/directx/d3d12.h"
#include "../../video/directx/SDL_d3d12.h"

#include "SDL_shaders_d3d12.h"

Expand Down
87 changes: 87 additions & 0 deletions src/video/directx/SDL_d3d12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

#ifndef SDL_D3D12_H
#define SDL_D3D12_H

#if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))

/* From the DirectX-Headers build system:
* "MinGW has RPC headers which define old versions, and complain if D3D
* headers are included before the RPC headers, since D3D headers were
* generated with new MIDL and "require" new RPC headers."
*/
#define __REQUIRED_RPCNDR_H_VERSION__ 475

/* May not be defined in winapifamily.h, can safely be ignored */
#ifndef WINAPI_PARTITION_GAMES
#define WINAPI_PARTITION_GAMES 0
#endif /* WINAPI_PARTITION_GAMES */

#define COBJMACROS
#include "d3d12.h"
#include <dxgi1_6.h>
#include <dxgidebug.h>

#define D3D_GUID(X) &(X)

#define D3D_SAFE_RELEASE(X) \
if (X) { \
(X)->lpVtbl->Release(X); \
X = NULL; \
}

/* FIXME: Remove this in favor of the COBJMACROS defines */
#define D3D_CALL(THIS, FUNC, ...) (THIS)->lpVtbl->FUNC((THIS), ##__VA_ARGS__)
#define D3D_CALL_RET(THIS, FUNC, ...) (THIS)->lpVtbl->FUNC((THIS), ##__VA_ARGS__)

#else /* !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) */

#if defined(SDL_PLATFORM_XBOXONE)
#include <d3d12_x.h>
#else /* SDL_PLATFORM_XBOXSERIES */
#include <d3d12_xs.h>
#endif

#define D3D_GUID(X) (X)

#define D3D_SAFE_RELEASE(X) \
if (X) { \
(X)->Release(); \
X = NULL; \
}

/* Older versions of the Xbox GDK may not have this defined */
#ifndef D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
#define D3D12_TEXTURE_DATA_PITCH_ALIGNMENT 256
#endif

/* DXGI_PRESENT flags are removed on Xbox */
#define DXGI_PRESENT_ALLOW_TEARING 0

/* FIXME: Xbox D3D12 does not define the COBJMACROS, so we need to define them ourselves */
#define D3D_CALL(THIS, FUNC, ...) (THIS)->FUNC(__VA_ARGS__)
#define D3D_CALL_RET(THIS, FUNC, RETVAL, ...) *(RETVAL) = (THIS)->FUNC(__VA_ARGS__)

#endif /* !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) */

#endif /* SDL_D3D12_H */

0 comments on commit efefc4a

Please sign in to comment.