diff options
| author | Andrew Reidmeyer <[email protected]> | 2017-06-13 20:38:29 -0600 |
|---|---|---|
| committer | Andrew Reidmeyer <[email protected]> | 2017-06-13 20:38:29 -0600 |
| commit | 62a1427154131f2387675957c1d2027061f354f0 (patch) | |
| tree | 4efdcb7d3fe9722d46523a51bc5c90b96f25441e /demo/DemoAppD3D12 | |
| parent | Initial 1.0.0 binary release (diff) | |
| download | flow-1.0.1.tar.xz flow-1.0.1.zip | |
NvFlow 1.0.1v1.0.1
Diffstat (limited to 'demo/DemoAppD3D12')
| -rw-r--r-- | demo/DemoAppD3D12/NvFlowInteropD3D12.cpp | 54 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/appD3D12Ctx.cpp | 131 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/appD3D12Ctx.h | 44 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/computeContextD3D12.cpp | 272 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/computeContextD3D12.h | 86 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/imguiGraphD3D12.cpp | 40 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/imguiGraphD3D12.h | 46 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/imguiInteropD3D12.cpp | 22 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/meshD3D12.cpp | 105 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/meshD3D12.h | 31 | ||||
| -rw-r--r-- | demo/DemoAppD3D12/meshInteropD3D12.cpp | 20 |
11 files changed, 610 insertions, 241 deletions
diff --git a/demo/DemoAppD3D12/NvFlowInteropD3D12.cpp b/demo/DemoAppD3D12/NvFlowInteropD3D12.cpp index ce79bcc..ecb13d8 100644 --- a/demo/DemoAppD3D12/NvFlowInteropD3D12.cpp +++ b/demo/DemoAppD3D12/NvFlowInteropD3D12.cpp @@ -15,9 +15,21 @@ #include "NvFlowContextD3D12.h" #include "appD3D12Ctx.h" +NV_FLOW_API NvFlowContext* NvFlowInteropCreateContextD3D12(AppGraphCtx* appctx); + +NV_FLOW_API NvFlowDepthStencilView* NvFlowInteropCreateDepthStencilViewD3D12(AppGraphCtx* appctx, NvFlowContext* flowctx); + +NV_FLOW_API NvFlowRenderTargetView* NvFlowInteropCreateRenderTargetViewD3D12(AppGraphCtx* appctx, NvFlowContext* flowctx); + +NV_FLOW_API void NvFlowInteropUpdateContextD3D12(NvFlowContext* context, AppGraphCtx* appctx); + +NV_FLOW_API void NvFlowInteropUpdateDepthStencilViewD3D12(NvFlowDepthStencilView* view, AppGraphCtx* appctx, NvFlowContext* flowctx); + +NV_FLOW_API void NvFlowInteropUpdateRenderTargetViewD3D12(NvFlowRenderTargetView* view, AppGraphCtx* appctx, NvFlowContext* flowctx); + NvFlowDescriptorReserveHandleD3D12 NvFlowInteropReserveDescriptors(void* userdata, NvFlowUint numDescriptors, NvFlowUint64 lastFenceCompleted, NvFlowUint64 nextFenceValue) { - auto appctx = static_cast<AppGraphCtx*>(userdata); + auto appctx = cast_to_AppGraphCtxD3D12((AppGraphCtx*)userdata); auto srcHandle = appctx->m_dynamicHeapCbvSrvUav.reserveDescriptors(numDescriptors, lastFenceCompleted, nextFenceValue); NvFlowDescriptorReserveHandleD3D12 handle = {}; handle.heap = srcHandle.heap; @@ -27,8 +39,10 @@ NvFlowDescriptorReserveHandleD3D12 NvFlowInteropReserveDescriptors(void* userdat return handle; } -void NvFlowInteropUpdateContextDesc(NvFlowContextDescD3D12* desc, AppGraphCtx* appctx) +void NvFlowInteropUpdateContextDesc(NvFlowContextDescD3D12* desc, AppGraphCtx* appctxIn) { + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + desc->device = appctx->m_device; desc->commandQueue = appctx->m_commandQueue; desc->commandQueueFence = appctx->m_fence; @@ -40,28 +54,36 @@ void NvFlowInteropUpdateContextDesc(NvFlowContextDescD3D12* desc, AppGraphCtx* a desc->dynamicHeapCbvSrvUav.reserveDescriptors = NvFlowInteropReserveDescriptors; } -NvFlowContext* NvFlowInteropCreateContext(AppGraphCtx* appctx) +NvFlowContext* NvFlowInteropCreateContextD3D12(AppGraphCtx* appctx) { NvFlowContextDescD3D12 desc = {}; NvFlowInteropUpdateContextDesc(&desc, appctx); return NvFlowCreateContextD3D12(NV_FLOW_VERSION, &desc); } -NvFlowDepthStencilView* NvFlowInteropCreateDepthStencilView(AppGraphCtx* appctx, NvFlowContext* flowctx) +NvFlowDepthStencilView* NvFlowInteropCreateDepthStencilViewD3D12(AppGraphCtx* appctxIn, NvFlowContext* flowctx) { + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + NvFlowDepthStencilViewDescD3D12 desc = {}; desc.dsvHandle = appctx->m_current_dsvHandle; desc.dsvDesc = appctx->m_current_dsvDesc; + desc.dsvResource = appctx->m_depthStencil; + desc.dsvCurrentState = D3D12_RESOURCE_STATE_DEPTH_WRITE; + desc.srvHandle = appctx->m_current_depth_srvHandle; desc.srvDesc = appctx->m_current_depth_srvDesc; - desc.resource = appctx->m_depthStencil; - desc.currentState = D3D12_RESOURCE_STATE_DEPTH_WRITE; + desc.srvResource = appctx->m_depthStencil; + desc.srvCurrentState = D3D12_RESOURCE_STATE_DEPTH_WRITE; + desc.viewport = appctx->m_viewport; return NvFlowCreateDepthStencilViewD3D12(flowctx, &desc); } -NvFlowRenderTargetView* NvFlowInteropCreateRenderTargetView(AppGraphCtx* appctx, NvFlowContext* flowctx) +NvFlowRenderTargetView* NvFlowInteropCreateRenderTargetViewD3D12(AppGraphCtx* appctxIn, NvFlowContext* flowctx) { + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + NvFlowRenderTargetViewDescD3D12 desc = {}; desc.rtvHandle = appctx->m_current_rtvHandle; desc.rtvDesc = appctx->m_current_rtvDesc; @@ -72,28 +94,36 @@ NvFlowRenderTargetView* NvFlowInteropCreateRenderTargetView(AppGraphCtx* appctx, return NvFlowCreateRenderTargetViewD3D12(flowctx, &desc); } -void NvFlowInteropUpdateContext(NvFlowContext* context, AppGraphCtx* appctx) +void NvFlowInteropUpdateContextD3D12(NvFlowContext* context, AppGraphCtx* appctx) { NvFlowContextDescD3D12 desc = {}; NvFlowInteropUpdateContextDesc(&desc, appctx); NvFlowUpdateContextD3D12(context, &desc); } -void NvFlowInteropUpdateDepthStencilView(NvFlowDepthStencilView* view, AppGraphCtx* appctx, NvFlowContext* flowctx) +void NvFlowInteropUpdateDepthStencilViewD3D12(NvFlowDepthStencilView* view, AppGraphCtx* appctxIn, NvFlowContext* flowctx) { + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + NvFlowDepthStencilViewDescD3D12 desc = {}; desc.dsvHandle = appctx->m_current_dsvHandle; desc.dsvDesc = appctx->m_current_dsvDesc; + desc.dsvResource = appctx->m_depthStencil; + desc.dsvCurrentState = D3D12_RESOURCE_STATE_DEPTH_WRITE; + desc.srvHandle = appctx->m_current_depth_srvHandle; desc.srvDesc = appctx->m_current_depth_srvDesc; - desc.resource = appctx->m_depthStencil; - desc.currentState = D3D12_RESOURCE_STATE_DEPTH_WRITE; + desc.srvResource = appctx->m_depthStencil; + desc.srvCurrentState = D3D12_RESOURCE_STATE_DEPTH_WRITE; + desc.viewport = appctx->m_viewport; NvFlowUpdateDepthStencilViewD3D12(flowctx, view, &desc); } -void NvFlowInteropUpdateRenderTargetView(NvFlowRenderTargetView* view, AppGraphCtx* appctx, NvFlowContext* flowctx) +void NvFlowInteropUpdateRenderTargetViewD3D12(NvFlowRenderTargetView* view, AppGraphCtx* appctxIn, NvFlowContext* flowctx) { + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + NvFlowRenderTargetViewDescD3D12 desc = {}; desc.rtvHandle = appctx->m_current_rtvHandle; desc.rtvDesc = appctx->m_current_rtvDesc; diff --git a/demo/DemoAppD3D12/appD3D12Ctx.cpp b/demo/DemoAppD3D12/appD3D12Ctx.cpp index 8d1d7ff..1779ba1 100644 --- a/demo/DemoAppD3D12/appD3D12Ctx.cpp +++ b/demo/DemoAppD3D12/appD3D12Ctx.cpp @@ -45,25 +45,23 @@ namespace } } -AppGraphProfiler* appGraphCreateProfiler(AppGraphCtx* ctx); -void appGraphProfilerFrameBegin(AppGraphProfiler* profiler); -void appGraphProfilerFrameEnd(AppGraphProfiler* profiler); -void appGraphProfilerEnable(AppGraphProfiler* profiler, bool enabled); -void appGraphProfilerBegin(AppGraphProfiler* profiler, const char* label); -void appGraphProfilerEnd(AppGraphProfiler* profiler, const char* label); -bool appGraphProfilerGet(AppGraphProfiler* profiler, const char** plabel, float* cpuTime, float* gpuTime, int index); -void appGraphReleaseProfiler(AppGraphProfiler* profiler); - -void AppGraphCtxInitRenderTarget(AppGraphCtx* context, SDL_Window* window, bool fullscreen); - -AppGraphCtx::AppGraphCtx() +AppGraphProfilerD3D12* appGraphCreateProfilerD3D12(AppGraphCtx* ctx); +void appGraphProfilerD3D12FrameBegin(AppGraphProfilerD3D12* profiler); +void appGraphProfilerD3D12FrameEnd(AppGraphProfilerD3D12* profiler); +void appGraphProfilerD3D12Enable(AppGraphProfilerD3D12* profiler, bool enabled); +void appGraphProfilerD3D12Begin(AppGraphProfilerD3D12* profiler, const char* label); +void appGraphProfilerD3D12End(AppGraphProfilerD3D12* profiler, const char* label); +bool appGraphProfilerD3D12Get(AppGraphProfilerD3D12* profiler, const char** plabel, float* cpuTime, float* gpuTime, int index); +void appGraphReleaseProfiler(AppGraphProfilerD3D12* profiler); + +AppGraphCtxD3D12::AppGraphCtxD3D12() { - m_profiler = appGraphCreateProfiler(this); + m_profiler = appGraphCreateProfilerD3D12(cast_from_AppGraphCtxD3D12(this)); } -AppGraphCtx::~AppGraphCtx() +AppGraphCtxD3D12::~AppGraphCtxD3D12() { - AppGraphCtxReleaseRenderTarget(this); + AppGraphCtxReleaseRenderTargetD3D12(cast_from_AppGraphCtxD3D12(this)); COMRelease(m_device); COMRelease(m_commandQueue); @@ -83,9 +81,9 @@ AppGraphCtx::~AppGraphCtx() m_profiler = nullptr; } -AppGraphCtx* AppGraphCtxCreate(int deviceID) +AppGraphCtx* AppGraphCtxCreateD3D12(int deviceID) { - AppGraphCtx* context = new AppGraphCtx; + AppGraphCtxD3D12* context = new AppGraphCtxD3D12; HRESULT hr = S_OK; @@ -247,11 +245,15 @@ AppGraphCtx* AppGraphCtxCreate(int deviceID) } } - return context; + return cast_from_AppGraphCtxD3D12(context); } -bool AppGraphCtxUpdateSize(AppGraphCtx* context, SDL_Window* window, bool fullscreen) +void AppGraphCtxInitRenderTargetD3D12(AppGraphCtx* context, SDL_Window* window, bool fullscreen); + +bool AppGraphCtxUpdateSizeD3D12(AppGraphCtx* contextIn, SDL_Window* window, bool fullscreen) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); + // TODO: fix iflip fullscreen support fullscreen = false; @@ -291,18 +293,20 @@ bool AppGraphCtxUpdateSize(AppGraphCtx* context, SDL_Window* window, bool fullsc if (sizeChanged) { - AppGraphCtxReleaseRenderTarget(context); + AppGraphCtxReleaseRenderTargetD3D12(cast_from_AppGraphCtxD3D12(context)); } if (sizeChanged && context->m_valid) { - AppGraphCtxInitRenderTarget(context, window, fullscreen); + AppGraphCtxInitRenderTargetD3D12(cast_from_AppGraphCtxD3D12(context), window, fullscreen); } return context->m_valid; } -void AppGraphCtxInitRenderTarget(AppGraphCtx* context, SDL_Window* window, bool fullscreen) +void AppGraphCtxInitRenderTargetD3D12(AppGraphCtx* contextIn, SDL_Window* window, bool fullscreen) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); + HWND hWnd = nullptr; // get Windows handle to this SDL window SDL_SysWMinfo winInfo; @@ -473,8 +477,10 @@ void AppGraphCtxInitRenderTarget(AppGraphCtx* context, SDL_Window* window, bool } } -void AppGraphCtxReleaseRenderTarget(AppGraphCtx* context) +void AppGraphCtxReleaseRenderTargetD3D12(AppGraphCtx* contextIn) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); + if (context->m_swapChain == nullptr) { return; @@ -505,15 +511,17 @@ void AppGraphCtxReleaseRenderTarget(AppGraphCtx* context) context->m_winH = 0u; } -void AppGraphCtxRelease(AppGraphCtx* context) +void AppGraphCtxReleaseD3D12(AppGraphCtx* context) { if (context == nullptr) return; - delete context; + delete cast_to_AppGraphCtxD3D12(context); } -void AppGraphCtxFrameStart(AppGraphCtx* context, float clearColor[4]) +void AppGraphCtxFrameStartD3D12(AppGraphCtx* contextIn, AppGraphColor clearColor) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); + // Get back render target index context->m_renderTargetIndex = context->m_swapChain->GetCurrentBackBufferIndex(); @@ -535,7 +543,7 @@ void AppGraphCtxFrameStart(AppGraphCtx* context, float clearColor[4]) // reset command list with this frame's allocator context->m_commandList->Reset(context->m_commandAllocators[context->m_frameIndex], nullptr); - appGraphProfilerFrameBegin(context->m_profiler); + appGraphProfilerD3D12FrameBegin(context->m_profiler); context->m_commandList->RSSetViewports(1, &context->m_viewport); context->m_commandList->RSSetScissorRects(1, &context->m_scissorRect); @@ -555,7 +563,7 @@ void AppGraphCtxFrameStart(AppGraphCtx* context, float clearColor[4]) D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = context->m_dsvHeap->GetCPUDescriptorHandleForHeapStart(); context->m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, &dsvHandle); - context->m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr); + context->m_commandList->ClearRenderTargetView(rtvHandle, &clearColor.r, 0, nullptr); context->m_commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH, 1.f, 0, 0, nullptr); /// to simplify interop implementation @@ -565,8 +573,10 @@ void AppGraphCtxFrameStart(AppGraphCtx* context, float clearColor[4]) context->m_current_depth_srvHandle = context->m_depthSrvHeap->GetCPUDescriptorHandleForHeapStart(); } -void AppGraphCtxFramePresent(AppGraphCtx* context, bool fullsync) +void AppGraphCtxFramePresentD3D12(AppGraphCtx* contextIn, bool fullsync) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); + D3D12_RESOURCE_BARRIER barrier = {}; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; @@ -590,7 +600,7 @@ void AppGraphCtxFramePresent(AppGraphCtx* context, bool fullsync) context->m_renderTargetID++; } - appGraphProfilerFrameEnd(context->m_profiler); + appGraphProfilerD3D12FrameEnd(context->m_profiler); // signal for this frame id context->m_frameID++; @@ -615,8 +625,10 @@ void AppGraphCtxFramePresent(AppGraphCtx* context, bool fullsync) } } -void AppGraphCtxWaitForFrames(AppGraphCtx* context, unsigned int maxFramesInFlight) +void AppGraphCtxWaitForFramesD3D12(AppGraphCtx* contextIn, unsigned int maxFramesInFlight) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); + unsigned int framesActive = maxFramesInFlight; while (framesActive >= maxFramesInFlight) { @@ -668,24 +680,28 @@ void AppGraphCtxWaitForFrames(AppGraphCtx* context, unsigned int maxFramesInFlig } } -void AppGraphCtxProfileEnable(AppGraphCtx* context, bool enabled) +void AppGraphCtxProfileEnableD3D12(AppGraphCtx* contextIn, bool enabled) { - appGraphProfilerEnable(context->m_profiler, enabled); + auto context = cast_to_AppGraphCtxD3D12(contextIn); + appGraphProfilerD3D12Enable(context->m_profiler, enabled); } -void AppGraphCtxProfileBegin(AppGraphCtx* context, const char* label) +void AppGraphCtxProfileBeginD3D12(AppGraphCtx* contextIn, const char* label) { - appGraphProfilerBegin(context->m_profiler, label); + auto context = cast_to_AppGraphCtxD3D12(contextIn); + appGraphProfilerD3D12Begin(context->m_profiler, label); } -void AppGraphCtxProfileEnd(AppGraphCtx* context, const char* label) +void AppGraphCtxProfileEndD3D12(AppGraphCtx* contextIn, const char* label) { - appGraphProfilerEnd(context->m_profiler, label); + auto context = cast_to_AppGraphCtxD3D12(contextIn); + appGraphProfilerD3D12End(context->m_profiler, label); } -bool AppGraphCtxProfileGet(AppGraphCtx* context, const char** plabel, float* cpuTime, float* gpuTime, int index) +bool AppGraphCtxProfileGetD3D12(AppGraphCtx* contextIn, const char** plabel, float* cpuTime, float* gpuTime, int index) { - return appGraphProfilerGet(context->m_profiler, plabel, cpuTime, gpuTime, index); + auto context = cast_to_AppGraphCtxD3D12(contextIn); + return appGraphProfilerD3D12Get(context->m_profiler, plabel, cpuTime, gpuTime, index); } // ******************************* Dynamic descriptor heap ****************************** @@ -889,9 +905,9 @@ namespace }; } -struct AppGraphProfiler +struct AppGraphProfilerD3D12 { - AppGraphCtx* m_context; + AppGraphCtxD3D12* m_context; int m_state = 0; bool m_enabled = false; @@ -906,29 +922,29 @@ struct AppGraphProfiler TimerValue m_timerValues[m_timersCap]; int m_timerValuesSize = 0; - AppGraphProfiler(AppGraphCtx* context); - ~AppGraphProfiler(); + AppGraphProfilerD3D12(AppGraphCtx* context); + ~AppGraphProfilerD3D12(); }; -AppGraphProfiler::AppGraphProfiler(AppGraphCtx* context) : m_context(context) +AppGraphProfilerD3D12::AppGraphProfilerD3D12(AppGraphCtx* context) : m_context(cast_to_AppGraphCtxD3D12(context)) { } -AppGraphProfiler::~AppGraphProfiler() +AppGraphProfilerD3D12::~AppGraphProfilerD3D12() { } -AppGraphProfiler* appGraphCreateProfiler(AppGraphCtx* ctx) +AppGraphProfilerD3D12* appGraphCreateProfilerD3D12(AppGraphCtx* ctx) { - return new AppGraphProfiler(ctx); + return new AppGraphProfilerD3D12(ctx); } -void appGraphReleaseProfiler(AppGraphProfiler* profiler) +void appGraphReleaseProfiler(AppGraphProfilerD3D12* profiler) { delete profiler; } -void appGraphProfilerFrameBegin(AppGraphProfiler* p) +void appGraphProfilerD3D12FrameBegin(AppGraphProfilerD3D12* p) { p->m_frameTime = (float)p->m_frameTimer.getDeltaTime(); @@ -940,7 +956,7 @@ void appGraphProfilerFrameBegin(AppGraphProfiler* p) } } -void appGraphProfilerFrameEnd(AppGraphProfiler* p) +void appGraphProfilerD3D12FrameEnd(AppGraphProfilerD3D12* p) { if (p->m_state == 1) { @@ -948,12 +964,12 @@ void appGraphProfilerFrameEnd(AppGraphProfiler* p) } } -void appGraphProfilerEnable(AppGraphProfiler* p, bool enabled) +void appGraphProfilerD3D12Enable(AppGraphProfilerD3D12* p, bool enabled) { p->m_enabled = enabled; } -void appGraphProfilerBegin(AppGraphProfiler* p, const char* label) +void appGraphProfilerD3D12Begin(AppGraphProfilerD3D12* p, const char* label) { if (p->m_state == 1 && p->m_timersSize < p->m_timersCap) { @@ -987,7 +1003,7 @@ void appGraphProfilerBegin(AppGraphProfiler* p, const char* label) } } -void appGraphProfilerEnd(AppGraphProfiler* p, const char* label) +void appGraphProfilerD3D12End(AppGraphProfilerD3D12* p, const char* label) { if (p->m_state == 1) { @@ -1013,7 +1029,7 @@ void appGraphProfilerEnd(AppGraphProfiler* p, const char* label) } } -bool appGraphProfilerFlush(AppGraphProfiler* p) +bool appGraphProfilerD3D12Flush(AppGraphProfilerD3D12* p) { if (p->m_state == 2) { @@ -1071,9 +1087,9 @@ bool appGraphProfilerFlush(AppGraphProfiler* p) return false; } -bool appGraphProfilerGet(AppGraphProfiler* p, const char** plabel, float* cpuTime, float* gpuTime, int index) +bool appGraphProfilerD3D12Get(AppGraphProfilerD3D12* p, const char** plabel, float* cpuTime, float* gpuTime, int index) { - appGraphProfilerFlush(p); + appGraphProfilerD3D12Flush(p); { if (index < p->m_timerValuesSize) { @@ -1091,7 +1107,8 @@ bool appGraphProfilerGet(AppGraphProfiler* p, const char** plabel, float* cpuTim return false; } -size_t AppGraphCtxDedicatedVideoMemory(AppGraphCtx* context) +size_t AppGraphCtxDedicatedVideoMemoryD3D12(AppGraphCtx* contextIn) { + auto context = cast_to_AppGraphCtxD3D12(contextIn); return context->m_dedicatedVideoMemory; }
\ No newline at end of file diff --git a/demo/DemoAppD3D12/appD3D12Ctx.h b/demo/DemoAppD3D12/appD3D12Ctx.h index f6d2ba3..a20e44a 100644 --- a/demo/DemoAppD3D12/appD3D12Ctx.h +++ b/demo/DemoAppD3D12/appD3D12Ctx.h @@ -15,7 +15,7 @@ struct IDXGISwapChain3; -struct AppGraphProfiler; +struct AppGraphProfilerD3D12; struct AppDescriptorReserveHandleD3D12 { @@ -41,7 +41,7 @@ struct AppDynamicDescriptorHeapD3D12 ~AppDynamicDescriptorHeapD3D12() { release(); } }; -struct AppGraphCtx +struct AppGraphCtxD3D12 { HWND m_hWnd = nullptr; @@ -104,10 +104,44 @@ struct AppGraphCtx AppDynamicDescriptorHeapD3D12 m_dynamicHeapCbvSrvUav; - AppGraphProfiler* m_profiler = nullptr; + AppGraphProfilerD3D12* m_profiler = nullptr; - AppGraphCtx(); - ~AppGraphCtx(); + AppGraphCtxD3D12(); + ~AppGraphCtxD3D12(); }; +inline AppGraphCtxD3D12* cast_to_AppGraphCtxD3D12(AppGraphCtx* appctx) +{ + return (AppGraphCtxD3D12*)(appctx); +} + +inline AppGraphCtx* cast_from_AppGraphCtxD3D12(AppGraphCtxD3D12* appctx) +{ + return (AppGraphCtx*)(appctx); +} + +APP_GRAPH_CTX_API AppGraphCtx* AppGraphCtxCreateD3D12(int deviceID); + +APP_GRAPH_CTX_API bool AppGraphCtxUpdateSizeD3D12(AppGraphCtx* context, SDL_Window* window, bool fullscreen); + +APP_GRAPH_CTX_API void AppGraphCtxReleaseRenderTargetD3D12(AppGraphCtx* context); + +APP_GRAPH_CTX_API void AppGraphCtxReleaseD3D12(AppGraphCtx* context); + +APP_GRAPH_CTX_API void AppGraphCtxFrameStartD3D12(AppGraphCtx* context, AppGraphColor clearColor); + +APP_GRAPH_CTX_API void AppGraphCtxFramePresentD3D12(AppGraphCtx* context, bool fullsync); + +APP_GRAPH_CTX_API void AppGraphCtxWaitForFramesD3D12(AppGraphCtx* context, unsigned int maxFramesInFlight); + +APP_GRAPH_CTX_API void AppGraphCtxProfileEnableD3D12(AppGraphCtx* context, bool enabled); + +APP_GRAPH_CTX_API void AppGraphCtxProfileBeginD3D12(AppGraphCtx* context, const char* label); + +APP_GRAPH_CTX_API void AppGraphCtxProfileEndD3D12(AppGraphCtx* context, const char* label); + +APP_GRAPH_CTX_API bool AppGraphCtxProfileGetD3D12(AppGraphCtx* context, const char** plabel, float* cpuTime, float* gpuTime, int index); + +APP_GRAPH_CTX_API size_t AppGraphCtxDedicatedVideoMemoryD3D12(AppGraphCtx* context); + #endif
\ No newline at end of file diff --git a/demo/DemoAppD3D12/computeContextD3D12.cpp b/demo/DemoAppD3D12/computeContextD3D12.cpp index f6cf26e..9e54325 100644 --- a/demo/DemoAppD3D12/computeContextD3D12.cpp +++ b/demo/DemoAppD3D12/computeContextD3D12.cpp @@ -250,9 +250,9 @@ struct ComputeContextUserData virtual void Release() = 0; }; -struct ComputeContext +struct ComputeContextD3D12 { - ComputeContextDesc m_desc = {}; + ComputeContextDescD3D12 m_desc = {}; ID3D12RootSignature* m_rootSignatureCompute = nullptr; ID3D12DescriptorHeap* m_nullHeap = nullptr; @@ -261,8 +261,10 @@ struct ComputeContext ComputeContextUserData* m_computeUserdata = nullptr; - ComputeContext(const ComputeContextDesc* desc) + ComputeContextD3D12(const ComputeContextDesc* descIn) { + auto desc = cast_to_ComputeContextDescD3D12(descIn); + m_desc = *desc; auto createRootSignature = [&](D3D12_ROOT_SIGNATURE_DESC* desc, ID3D12RootSignature** root) @@ -323,7 +325,7 @@ struct ComputeContext m_desc.device->CreateShaderResourceView(nullptr, &srvDesc, m_nullSRV); } } - ~ComputeContext() + ~ComputeContextD3D12() { COMRelease(m_rootSignatureCompute); COMRelease(m_nullHeap); @@ -331,20 +333,40 @@ struct ComputeContext } }; -struct ComputeShader +inline ComputeContextD3D12* cast_to_ComputeContextD3D12(ComputeContext* ctx) +{ + return (ComputeContextD3D12*)(ctx); +} + +inline ComputeContext* cast_from_ComputeContextD3D12(ComputeContextD3D12* ctx) +{ + return (ComputeContext*)(ctx); +} + +struct ComputeShaderD3D12 { ID3D12PipelineState* m_shader = nullptr; - ComputeShader(ID3D12PipelineState* shader) + ComputeShaderD3D12(ID3D12PipelineState* shader) { m_shader = shader; } - ~ComputeShader() + ~ComputeShaderD3D12() { COMRelease(m_shader); } }; -struct ComputeConstantBuffer +inline ComputeShaderD3D12* cast_to_ComputeShaderD3D12(ComputeShader* ctx) +{ + return (ComputeShaderD3D12*)(ctx); +} + +inline ComputeShader* cast_from_ComputeShaderD3D12(ComputeShaderD3D12* ctx) +{ + return (ComputeShader*)(ctx); +} + +struct ComputeConstantBufferD3D12 { ComputeConstantBufferDesc m_desc = {}; @@ -357,38 +379,54 @@ struct ComputeConstantBuffer }; VersionedBuffer<BufferData> m_buffers; - ComputeConstantBuffer(ComputeContext* context, const ComputeConstantBufferDesc* desc) - { - m_desc = *desc; + ComputeConstantBufferD3D12(ComputeContext* context, const ComputeConstantBufferDesc* desc); + ~ComputeConstantBufferD3D12(); +}; - // map and unmap to trigger initial allocation - ComputeConstantBufferMap(context, this); - ComputeConstantBufferUnmap(context, this); - } - ~ComputeConstantBuffer() +inline ComputeConstantBufferD3D12* cast_to_ComputeConstantBufferD3D12(ComputeConstantBuffer* ctx) +{ + return (ComputeConstantBufferD3D12*)(ctx); +} + +inline ComputeConstantBuffer* cast_from_ComputeConstantBufferD3D12(ComputeConstantBufferD3D12* ctx) +{ + return (ComputeConstantBuffer*)(ctx); +} + +ComputeConstantBufferD3D12::ComputeConstantBufferD3D12(ComputeContext* context, const ComputeConstantBufferDesc* desc) +{ + m_desc = *desc; + + // map and unmap to trigger initial allocation + ComputeConstantBufferMapD3D12(context, cast_from_ComputeConstantBufferD3D12(this)); + ComputeConstantBufferUnmapD3D12(context, cast_from_ComputeConstantBufferD3D12(this)); +} + +ComputeConstantBufferD3D12::~ComputeConstantBufferD3D12() +{ + for (ComputeUint i = 0; i < m_buffers.m_buffers.m_size; i++) { - for (ComputeUint i = 0; i < m_buffers.m_buffers.m_size; i++) - { - COMRelease(m_buffers.m_buffers[i].bufferData.m_buffer); - } + COMRelease(m_buffers.m_buffers[i].bufferData.m_buffer); } -}; +} -struct ComputeResource +struct ComputeResourceD3D12 { protected: D3D12_CPU_DESCRIPTOR_HANDLE m_srv; ID3D12Resource* m_resource; D3D12_RESOURCE_STATES* m_currentState; public: - void update(const ComputeResourceDesc* desc) + void update(const ComputeResourceDesc* descIn) { + auto desc = cast_to_ComputeResourceDescD3D12(descIn); + m_srv = desc->srv; m_resource = desc->resource; m_currentState = desc->currentState; } - ComputeResource(const ComputeResourceDesc* desc) + ComputeResourceD3D12(const ComputeResourceDesc* desc) { update(desc); } @@ -409,21 +447,36 @@ public: } }; -struct ComputeResourceRW : public ComputeResource +inline ComputeResourceD3D12* cast_to_ComputeResourceD3D12(ComputeResource* ctx) +{ + return (ComputeResourceD3D12*)(ctx); +} + +inline ComputeResource* cast_from_ComputeResourceD3D12(ComputeResourceD3D12* ctx) +{ + return (ComputeResource*)(ctx); +} + +struct ComputeResourceRWD3D12 : public ComputeResourceD3D12 { protected: D3D12_CPU_DESCRIPTOR_HANDLE m_uav; public: + static const ComputeResourceRWDescD3D12* cast(const ComputeResourceRWDesc* descRW) + { + return cast_to_ComputeResourceRWDescD3D12(descRW); + } + void update(const ComputeResourceRWDesc* descRW) { - m_uav = descRW->uav; - ComputeResource::update(&descRW->resource); + m_uav = cast(descRW)->uav; + ComputeResourceD3D12::update(cast_from_ComputeResourceDescD3D12(&cast(descRW)->resource)); } - ComputeResourceRW(const ComputeResourceRWDesc* descRW) : - ComputeResource(&descRW->resource) + ComputeResourceRWD3D12(const ComputeResourceRWDesc* descRW) : + ComputeResourceD3D12(cast_from_ComputeResourceDescD3D12(&cast(descRW)->resource)) { - m_uav = descRW->uav; + m_uav = cast(descRW)->uav; } D3D12_CPU_DESCRIPTOR_HANDLE UAV() @@ -432,25 +485,41 @@ public: } }; +inline ComputeResourceRWD3D12* cast_to_ComputeResourceRWD3D12(ComputeResourceRW* ctx) +{ + return (ComputeResourceRWD3D12*)(ctx); +} + +inline ComputeResourceRW* cast_from_ComputeResourceRWD3D12(ComputeResourceRWD3D12* ctx) +{ + return (ComputeResourceRW*)(ctx); +} + + // ************* API functions **************** -ComputeContext* ComputeContextCreate(ComputeContextDesc* desc) +ComputeContext* ComputeContextCreateD3D12(ComputeContextDesc* desc) { - return new ComputeContext(desc); + return cast_from_ComputeContextD3D12(new ComputeContextD3D12(desc)); } -void ComputeContextUpdate(ComputeContext* context, ComputeContextDesc* desc) +void ComputeContextUpdateD3D12(ComputeContext* contextIn, ComputeContextDesc* descIn) { + auto context = cast_to_ComputeContextD3D12(contextIn); + auto desc = cast_to_ComputeContextDescD3D12(descIn); + context->m_desc = *desc; } -void ComputeContextRelease(ComputeContext* context) +void ComputeContextReleaseD3D12(ComputeContext* context) { - delete context; + delete cast_to_ComputeContextD3D12(context); } -ComputeShader* ComputeShaderCreate(ComputeContext* context, const ComputeShaderDesc* desc) +ComputeShader* ComputeShaderCreateD3D12(ComputeContext* contextIn, const ComputeShaderDesc* desc) { + auto context = cast_to_ComputeContextD3D12(contextIn); + ID3D12PipelineState* computeShader = nullptr; D3D12_COMPUTE_PIPELINE_STATE_DESC psoDesc = {}; @@ -460,26 +529,29 @@ ComputeShader* ComputeShaderCreate(ComputeContext* context, const ComputeShaderD context->m_desc.device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&computeShader)); - return new ComputeShader(computeShader); + return cast_from_ComputeShaderD3D12(new ComputeShaderD3D12(computeShader)); } -void ComputeShaderRelease(ComputeShader* shader) +void ComputeShaderReleaseD3D12(ComputeShader* shader) { - delete shader; + delete cast_to_ComputeShaderD3D12(shader); } -ComputeConstantBuffer* ComputeConstantBufferCreate(ComputeContext* context, const ComputeConstantBufferDesc* desc) +ComputeConstantBuffer* ComputeConstantBufferCreateD3D12(ComputeContext* context, const ComputeConstantBufferDesc* desc) { - return new ComputeConstantBuffer(context, desc); + return cast_from_ComputeConstantBufferD3D12(new ComputeConstantBufferD3D12(context, desc)); } -void ComputeConstantBufferRelease(ComputeConstantBuffer* constantBuffer) +void ComputeConstantBufferReleaseD3D12(ComputeConstantBuffer* constantBuffer) { - delete constantBuffer; + delete cast_to_ComputeConstantBufferD3D12(constantBuffer); } -void* ComputeConstantBufferMap(ComputeContext* context, ComputeConstantBuffer* constantBuffer) +void* ComputeConstantBufferMapD3D12(ComputeContext* contextIn, ComputeConstantBuffer* constantBufferIn) { + auto context = cast_to_ComputeContextD3D12(contextIn); + auto constantBuffer = cast_to_ComputeConstantBufferD3D12(constantBufferIn); + auto bufferData = constantBuffer->m_buffers.map(context->m_desc.lastFenceCompleted, context->m_desc.nextFenceValue); if (bufferData->m_buffer == nullptr) @@ -500,52 +572,60 @@ void* ComputeConstantBufferMap(ComputeContext* context, ComputeConstantBuffer* c return bufferData->m_mappedData; } -void ComputeConstantBufferUnmap(ComputeContext* context, ComputeConstantBuffer* constantBuffer) +void ComputeConstantBufferUnmapD3D12(ComputeContext* contextIn, ComputeConstantBuffer* constantBufferIn) { + auto context = cast_to_ComputeContextD3D12(contextIn); + auto constantBuffer = cast_to_ComputeConstantBufferD3D12(constantBufferIn); + constantBuffer->m_buffers.unmap(context->m_desc.lastFenceCompleted, context->m_desc.nextFenceValue); } -ComputeResource* ComputeResourceCreate(ComputeContext* context, const ComputeResourceDesc* desc) +ComputeResource* ComputeResourceCreateD3D12(ComputeContext* context, const ComputeResourceDesc* desc) { - return new ComputeResource(desc); + return cast_from_ComputeResourceD3D12(new ComputeResourceD3D12(desc)); } -void ComputeResourceUpdate(ComputeContext* context, ComputeResource* resource, const ComputeResourceDesc* desc) +void ComputeResourceUpdateD3D12(ComputeContext* context, ComputeResource* resourceIn, const ComputeResourceDesc* desc) { + auto resource = cast_to_ComputeResourceD3D12(resourceIn); resource->update(desc); } -void ComputeResourceRelease(ComputeResource* resource) +void ComputeResourceReleaseD3D12(ComputeResource* resource) { - delete resource; + delete cast_to_ComputeResourceD3D12(resource); } -ComputeResourceRW* ComputeResourceRWCreate(ComputeContext* context, const ComputeResourceRWDesc* desc) +ComputeResourceRW* ComputeResourceRWCreateD3D12(ComputeContext* context, const ComputeResourceRWDesc* desc) { - return new ComputeResourceRW(desc); + return cast_from_ComputeResourceRWD3D12(new ComputeResourceRWD3D12(desc)); } -void ComputeResourceRWUpdate(ComputeContext* context, ComputeResourceRW* resourceRW, const ComputeResourceRWDesc* desc) +void ComputeResourceRWUpdateD3D12(ComputeContext* context, ComputeResourceRW* resourceRWIn, const ComputeResourceRWDesc* desc) { + auto resourceRW = cast_to_ComputeResourceRWD3D12(resourceRWIn); resourceRW->update(desc); } -void ComputeResourceRWRelease(ComputeResourceRW* resourceRW) +void ComputeResourceRWReleaseD3D12(ComputeResourceRW* resourceRW) { - delete resourceRW; + delete cast_to_ComputeResourceRWD3D12(resourceRW); } -ComputeResource* ComputeResourceRWGetResource(ComputeResourceRW* resourceRW) +ComputeResource* ComputeResourceRWGetResourceD3D12(ComputeResourceRW* resourceRWIn) { - return static_cast<ComputeResource*>(resourceRW); + auto resourceRW = cast_to_ComputeResourceRWD3D12(resourceRWIn); + return cast_from_ComputeResourceD3D12(static_cast<ComputeResourceD3D12*>(resourceRW)); } -void ComputeContextDispatch(ComputeContext* context, const ComputeDispatchParams* params) +void ComputeContextDispatchD3D12(ComputeContext* contextIn, const ComputeDispatchParams* params) { + auto context = cast_to_ComputeContextD3D12(contextIn); + auto& commandList = context->m_desc.commandList; commandList->SetComputeRootSignature(context->m_rootSignatureCompute); - if (params->shader) commandList->SetPipelineState(params->shader->m_shader); + if (params->shader) commandList->SetPipelineState(cast_to_ComputeShaderD3D12(params->shader)->m_shader); auto handles = context->m_desc.dynamicHeapCbvSrvUav.reserveDescriptors(context->m_desc.dynamicHeapCbvSrvUav.userdata, ComputeDispatchMaxResources + ComputeDispatchMaxResourcesRW, @@ -557,14 +637,14 @@ void ComputeContextDispatch(ComputeContext* context, const ComputeDispatchParams for (ComputeUint i = 0u; i < ComputeDispatchMaxResources; i++) { auto r = params->resources[i]; - D3D12_CPU_DESCRIPTOR_HANDLE srcHandle = r ? r->SRV() : context->m_nullSRV; + D3D12_CPU_DESCRIPTOR_HANDLE srcHandle = r ? cast_to_ComputeResourceD3D12(r)->SRV() : context->m_nullSRV; context->m_desc.device->CopyDescriptorsSimple(1u, handles.cpuHandle, srcHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); handles.cpuHandle.ptr += handles.descriptorSize; } for (ComputeUint i = 0u; i < ComputeDispatchMaxResourcesRW; i++) { auto rw = params->resourcesRW[i]; - D3D12_CPU_DESCRIPTOR_HANDLE srcHandle = rw ? rw->UAV() : context->m_nullUAV; + D3D12_CPU_DESCRIPTOR_HANDLE srcHandle = rw ? cast_to_ComputeResourceRWD3D12(rw)->UAV() : context->m_nullUAV; context->m_desc.device->CopyDescriptorsSimple(1u, handles.cpuHandle, srcHandle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); handles.cpuHandle.ptr += handles.descriptorSize; } @@ -575,7 +655,7 @@ void ComputeContextDispatch(ComputeContext* context, const ComputeDispatchParams if (params->constantBuffer) { - auto cbv = params->constantBuffer->m_buffers.front()->m_buffer; + auto cbv = cast_to_ComputeConstantBufferD3D12(params->constantBuffer)->m_buffers.front()->m_buffer; commandList->SetComputeRootConstantBufferView(0u, cbv->GetGPUVirtualAddress()); } @@ -586,17 +666,17 @@ void ComputeContextDispatch(ComputeContext* context, const ComputeDispatchParams auto r = params->resources[i]; if (r) { - if (!((*r->currentState()) & D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE)) + if (!((*cast_to_ComputeResourceD3D12(r)->currentState()) & D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE)) { D3D12_RESOURCE_BARRIER& bar = barrier[barrierIdx++]; bar.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; bar.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - bar.Transition.pResource = r->resource(); + bar.Transition.pResource = cast_to_ComputeResourceD3D12(r)->resource(); bar.Transition.Subresource = 0u; - bar.Transition.StateBefore = *r->currentState(); + bar.Transition.StateBefore = *cast_to_ComputeResourceD3D12(r)->currentState(); bar.Transition.StateAfter = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; - *r->currentState() = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + *cast_to_ComputeResourceD3D12(r)->currentState() = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; } } } @@ -605,24 +685,24 @@ void ComputeContextDispatch(ComputeContext* context, const ComputeDispatchParams auto rw = params->resourcesRW[i]; if (rw) { - if ((*rw->currentState()) == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) + if ((*cast_to_ComputeResourceRWD3D12(rw)->currentState()) == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) { D3D12_RESOURCE_BARRIER& bar = barrier[barrierIdx++]; bar.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; bar.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - bar.UAV.pResource = rw->resource(); + bar.UAV.pResource = cast_to_ComputeResourceRWD3D12(rw)->resource(); } else { D3D12_RESOURCE_BARRIER& bar = barrier[barrierIdx++]; bar.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; bar.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - bar.Transition.pResource = rw->resource(); + bar.Transition.pResource = cast_to_ComputeResourceRWD3D12(rw)->resource(); bar.Transition.Subresource = 0u; - bar.Transition.StateBefore = *rw->currentState(); + bar.Transition.StateBefore = *cast_to_ComputeResourceRWD3D12(rw)->currentState(); bar.Transition.StateAfter = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; - *rw->currentState() = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + *cast_to_ComputeResourceRWD3D12(rw)->currentState() = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; } } } @@ -647,7 +727,7 @@ struct ComputeContextUserDataNvFlow : public ComputeContextUserData } }; -ComputeDescriptorReserveHandleD3D12 ComputeReserveDescriptors(void* userdata, UINT numDescriptors, UINT64 lastFenceCompleted, UINT64 nextFenceValue) +ComputeDescriptorReserveHandleD3D12 ComputeReserveDescriptorsD3D12(void* userdata, UINT numDescriptors, UINT64 lastFenceCompleted, UINT64 nextFenceValue) { auto data = static_cast<ComputeContextUserDataNvFlow*>(userdata); auto srcHandle = data->flowHeap.reserveDescriptors(data->flowHeap.userdata, numDescriptors, lastFenceCompleted, nextFenceValue); @@ -659,9 +739,9 @@ ComputeDescriptorReserveHandleD3D12 ComputeReserveDescriptors(void* userdata, UI return handle; } -ComputeContext* ComputeContextNvFlowContextCreate(NvFlowContext* flowContext) +ComputeContext* ComputeContextNvFlowContextCreateD3D12(NvFlowContext* flowContext) { - ComputeContextDesc desc = {}; + ComputeContextDescD3D12 desc = {}; NvFlowContextDescD3D12 srcDesc = {}; NvFlowUpdateContextDescD3D12(flowContext, &srcDesc); @@ -677,18 +757,20 @@ ComputeContext* ComputeContextNvFlowContextCreate(NvFlowContext* flowContext) computeUserdata->flowHeap.reserveDescriptors = srcDesc.dynamicHeapCbvSrvUav.reserveDescriptors; desc.dynamicHeapCbvSrvUav.userdata = computeUserdata; - desc.dynamicHeapCbvSrvUav.reserveDescriptors = ComputeReserveDescriptors; + desc.dynamicHeapCbvSrvUav.reserveDescriptors = ComputeReserveDescriptorsD3D12; - auto computeContext = ComputeContextCreate(&desc); + auto computeContext = cast_to_ComputeContextD3D12(ComputeContextCreateD3D12(cast_from_ComputeContextDescD3D12(&desc))); computeContext->m_computeUserdata = computeUserdata; - return computeContext; + return cast_from_ComputeContextD3D12(computeContext); } -void ComputeContextNvFlowContextUpdate(ComputeContext* computeContext, NvFlowContext* flowContext) +void ComputeContextNvFlowContextUpdateD3D12(ComputeContext* computeContextIn, NvFlowContext* flowContext) { - ComputeContextDesc desc = {}; + auto computeContext = cast_to_ComputeContextD3D12(computeContextIn); + + ComputeContextDescD3D12 desc = {}; NvFlowContextDescD3D12 srcDesc = {}; NvFlowUpdateContextDescD3D12(flowContext, &srcDesc); @@ -704,56 +786,56 @@ void ComputeContextNvFlowContextUpdate(ComputeContext* computeContext, NvFlowCon computeUserdata->flowHeap.reserveDescriptors = srcDesc.dynamicHeapCbvSrvUav.reserveDescriptors; desc.dynamicHeapCbvSrvUav.userdata = computeUserdata; - desc.dynamicHeapCbvSrvUav.reserveDescriptors = ComputeReserveDescriptors; + desc.dynamicHeapCbvSrvUav.reserveDescriptors = ComputeReserveDescriptorsD3D12; - ComputeContextUpdate(computeContext, &desc); + ComputeContextUpdateD3D12(cast_from_ComputeContextD3D12(computeContext), cast_from_ComputeContextDescD3D12(&desc)); } -inline void updateComputeResourceDesc(NvFlowResourceViewDescD3D12* flowViewDesc, ComputeResourceDesc* desc) +inline void updateComputeResourceDesc(NvFlowResourceViewDescD3D12* flowViewDesc, ComputeResourceDescD3D12* desc) { desc->srv = flowViewDesc->srvHandle; desc->resource = flowViewDesc->resource; desc->currentState = flowViewDesc->currentState; } -ComputeResource* ComputeResourceNvFlowCreate(ComputeContext* context, NvFlowContext* flowContext, NvFlowResource* flowResource) +ComputeResource* ComputeResourceNvFlowCreateD3D12(ComputeContext* context, NvFlowContext* flowContext, NvFlowResource* flowResource) { NvFlowResourceViewDescD3D12 flowViewDesc = {}; NvFlowUpdateResourceViewDescD3D12(flowContext, flowResource, &flowViewDesc); - ComputeResourceDesc desc = {}; + ComputeResourceDescD3D12 desc = {}; updateComputeResourceDesc(&flowViewDesc, &desc); - return ComputeResourceCreate(context, &desc); + return ComputeResourceCreateD3D12(context, cast_from_ComputeResourceDescD3D12(&desc)); } -void ComputeResourceNvFlowUpdate(ComputeContext* context, ComputeResource* resource, NvFlowContext* flowContext, NvFlowResource* flowResource) +void ComputeResourceNvFlowUpdateD3D12(ComputeContext* context, ComputeResource* resource, NvFlowContext* flowContext, NvFlowResource* flowResource) { NvFlowResourceViewDescD3D12 flowViewDesc = {}; NvFlowUpdateResourceViewDescD3D12(flowContext, flowResource, &flowViewDesc); - ComputeResourceDesc desc = {}; + ComputeResourceDescD3D12 desc = {}; updateComputeResourceDesc(&flowViewDesc, &desc); - ComputeResourceUpdate(context, resource, &desc); + ComputeResourceUpdateD3D12(context, resource, cast_from_ComputeResourceDescD3D12(&desc)); } -inline void updateComputeResourceRWDesc(NvFlowResourceRWViewDescD3D12* flowViewDesc, ComputeResourceRWDesc* desc) +inline void updateComputeResourceRWDesc(NvFlowResourceRWViewDescD3D12* flowViewDesc, ComputeResourceRWDescD3D12* desc) { updateComputeResourceDesc(&flowViewDesc->resourceView, &desc->resource); desc->uav = flowViewDesc->uavHandle; } -ComputeResourceRW* ComputeResourceRWNvFlowCreate(ComputeContext* context, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW) +ComputeResourceRW* ComputeResourceRWNvFlowCreateD3D12(ComputeContext* context, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW) { NvFlowResourceRWViewDescD3D12 flowViewDesc = {}; NvFlowUpdateResourceRWViewDescD3D12(flowContext, flowResourceRW, &flowViewDesc); - ComputeResourceRWDesc desc = {}; + ComputeResourceRWDescD3D12 desc = {}; updateComputeResourceRWDesc(&flowViewDesc, &desc); - return ComputeResourceRWCreate(context, &desc); + return ComputeResourceRWCreateD3D12(context, cast_from_ComputeResourceRWDescD3D12(&desc)); } -void ComputeResourceRWNvFlowUpdate(ComputeContext* context, ComputeResourceRW* resourceRW, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW) +void ComputeResourceRWNvFlowUpdateD3D12(ComputeContext* context, ComputeResourceRW* resourceRW, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW) { NvFlowResourceRWViewDescD3D12 flowViewDesc = {}; NvFlowUpdateResourceRWViewDescD3D12(flowContext, flowResourceRW, &flowViewDesc); - ComputeResourceRWDesc desc = {}; + ComputeResourceRWDescD3D12 desc = {}; updateComputeResourceRWDesc(&flowViewDesc, &desc); - ComputeResourceRWUpdate(context, resourceRW, &desc); + ComputeResourceRWUpdateD3D12(context, resourceRW, cast_from_ComputeResourceRWDescD3D12(&desc)); }
\ No newline at end of file diff --git a/demo/DemoAppD3D12/computeContextD3D12.h b/demo/DemoAppD3D12/computeContextD3D12.h index 0acb489..835e7d7 100644 --- a/demo/DemoAppD3D12/computeContextD3D12.h +++ b/demo/DemoAppD3D12/computeContextD3D12.h @@ -26,7 +26,7 @@ struct ComputeDynamicDescriptorHeapD3D12 ComputeDescriptorReserveHandleD3D12(*reserveDescriptors)(void* userdata, UINT numDescriptors, UINT64 lastFenceCompleted, UINT64 nextFenceValue); }; -struct ComputeContextDesc +struct ComputeContextDescD3D12 { ID3D12Device* device; ID3D12CommandQueue* commandQueue; @@ -38,15 +38,91 @@ struct ComputeContextDesc ComputeDynamicDescriptorHeapD3D12 dynamicHeapCbvSrvUav; }; -struct ComputeResourceDesc +struct ComputeResourceDescD3D12 { D3D12_CPU_DESCRIPTOR_HANDLE srv; ID3D12Resource* resource; D3D12_RESOURCE_STATES* currentState; }; -struct ComputeResourceRWDesc +struct ComputeResourceRWDescD3D12 { - ComputeResourceDesc resource; + ComputeResourceDescD3D12 resource; D3D12_CPU_DESCRIPTOR_HANDLE uav; -};
\ No newline at end of file +}; + +inline const ComputeContextDescD3D12* cast_to_ComputeContextDescD3D12(const ComputeContextDesc* desc) +{ + return (const ComputeContextDescD3D12*)(desc); +} + +inline ComputeContextDesc* cast_from_ComputeContextDescD3D12(ComputeContextDescD3D12* desc) +{ + return (ComputeContextDesc*)(desc); +} + +inline const ComputeResourceDescD3D12* cast_to_ComputeResourceDescD3D12(const ComputeResourceDesc* desc) +{ + return (const ComputeResourceDescD3D12*)(desc); +} + +inline const ComputeResourceDesc* cast_from_ComputeResourceDescD3D12(const ComputeResourceDescD3D12* desc) +{ + return (const ComputeResourceDesc*)(desc); +} + +inline const ComputeResourceRWDescD3D12* cast_to_ComputeResourceRWDescD3D12(const ComputeResourceRWDesc* desc) +{ + return (const ComputeResourceRWDescD3D12*)(desc); +} + +inline const ComputeResourceRWDesc* cast_from_ComputeResourceRWDescD3D12(const ComputeResourceRWDescD3D12* desc) +{ + return (const ComputeResourceRWDesc*)(desc); +} + +COMPUTE_API ComputeContext* ComputeContextCreateD3D12(ComputeContextDesc* desc); + +COMPUTE_API void ComputeContextUpdateD3D12(ComputeContext* context, ComputeContextDesc* desc); + +COMPUTE_API void ComputeContextReleaseD3D12(ComputeContext* context); + +COMPUTE_API ComputeShader* ComputeShaderCreateD3D12(ComputeContext* context, const ComputeShaderDesc* desc); + +COMPUTE_API void ComputeShaderReleaseD3D12(ComputeShader* shader); + +COMPUTE_API ComputeConstantBuffer* ComputeConstantBufferCreateD3D12(ComputeContext* context, const ComputeConstantBufferDesc* desc); + +COMPUTE_API void ComputeConstantBufferReleaseD3D12(ComputeConstantBuffer* constantBuffer); + +COMPUTE_API void* ComputeConstantBufferMapD3D12(ComputeContext* context, ComputeConstantBuffer* constantBuffer); + +COMPUTE_API void ComputeConstantBufferUnmapD3D12(ComputeContext* context, ComputeConstantBuffer* constantBuffer); + +COMPUTE_API ComputeResource* ComputeResourceCreateD3D12(ComputeContext* context, const ComputeResourceDesc* desc); + +COMPUTE_API void ComputeResourceUpdateD3D12(ComputeContext* context, ComputeResource* resource, const ComputeResourceDesc* desc); + +COMPUTE_API void ComputeResourceReleaseD3D12(ComputeResource* resource); + +COMPUTE_API ComputeResourceRW* ComputeResourceRWCreateD3D12(ComputeContext* context, const ComputeResourceRWDesc* desc); + +COMPUTE_API void ComputeResourceRWUpdateD3D12(ComputeContext* context, ComputeResourceRW* resourceRW, const ComputeResourceRWDesc* desc); + +COMPUTE_API void ComputeResourceRWReleaseD3D12(ComputeResourceRW* resourceRW); + +COMPUTE_API ComputeResource* ComputeResourceRWGetResourceD3D12(ComputeResourceRW* resourceRW); + +COMPUTE_API void ComputeContextDispatchD3D12(ComputeContext* context, const ComputeDispatchParams* params); + +COMPUTE_API ComputeContext* ComputeContextNvFlowContextCreateD3D12(NvFlowContext* flowContext); + +COMPUTE_API void ComputeContextNvFlowContextUpdateD3D12(ComputeContext* computeContext, NvFlowContext* flowContext); + +COMPUTE_API ComputeResource* ComputeResourceNvFlowCreateD3D12(ComputeContext* context, NvFlowContext* flowContext, NvFlowResource* flowResource); + +COMPUTE_API void ComputeResourceNvFlowUpdateD3D12(ComputeContext* context, ComputeResource* resource, NvFlowContext* flowContext, NvFlowResource* flowResource); + +COMPUTE_API ComputeResourceRW* ComputeResourceRWNvFlowCreateD3D12(ComputeContext* context, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW); + +COMPUTE_API void ComputeResourceRWNvFlowUpdateD3D12(ComputeContext* context, ComputeResourceRW* resourceRW, NvFlowContext* flowContext, NvFlowResourceRW* flowResourceRW);
\ No newline at end of file diff --git a/demo/DemoAppD3D12/imguiGraphD3D12.cpp b/demo/DemoAppD3D12/imguiGraphD3D12.cpp index 9b5f74a..92cbab1 100644 --- a/demo/DemoAppD3D12/imguiGraphD3D12.cpp +++ b/demo/DemoAppD3D12/imguiGraphD3D12.cpp @@ -28,7 +28,7 @@ namespace namespace { - ImguiGraphDesc m_desc = {}; + ImguiGraphDescD3D12 m_desc = {}; struct Vertex { @@ -75,8 +75,10 @@ namespace int frameIndex = 0; } -void imguiGraphContextInit(const ImguiGraphDesc* desc) +void imguiGraphContextInitD3D12(const ImguiGraphDesc* descIn) { + const auto desc = cast_to_imguiGraphDescD3D12(descIn); + m_desc = *desc; // create the root signature @@ -308,12 +310,14 @@ void imguiGraphContextInit(const ImguiGraphDesc* desc) } } -void imguiGraphContextUpdate(const ImguiGraphDesc* desc) +void imguiGraphContextUpdateD3D12(const ImguiGraphDesc* descIn) { + const auto desc = cast_to_imguiGraphDescD3D12(descIn); + m_desc = *desc; } -void imguiGraphContextDestroy() +void imguiGraphContextDestroyD3D12() { COMRelease(m_rootSignature); COMRelease(m_pipelineState); @@ -321,7 +325,7 @@ void imguiGraphContextDestroy() COMRelease(m_vertexBuffer); } -void imguiGraphRecordBegin() +void imguiGraphRecordBeginD3D12() { frameIndex = (frameIndex + 1) % frameCount; @@ -406,7 +410,7 @@ static void imguiGraphFlush() } } -void imguiGraphRecordEnd() +void imguiGraphRecordEndD3D12() { ID3D12GraphicsCommandList* commandList = m_desc.commandList; @@ -424,7 +428,7 @@ void imguiGraphRecordEnd() commandList->RSSetScissorRects(1, &rect); } -void imguiGraphEnableScissor(int x, int y, int width, int height) +void imguiGraphEnableScissorD3D12(int x, int y, int width, int height) { // mark end of last region m_stateScissor.stopIdx = m_stateVertIdx; @@ -439,7 +443,7 @@ void imguiGraphEnableScissor(int x, int y, int width, int height) m_stateScissor.height = height; } -void imguiGraphDisableScissor() +void imguiGraphDisableScissorD3D12() { if (m_stateVertIdx == 0) return; @@ -456,13 +460,13 @@ void imguiGraphDisableScissor() m_stateScissor.height = m_desc.winH; } -void imguiGraphVertex2f(float x, float y) +void imguiGraphVertex2fD3D12(float x, float y) { float v[2] = { x,y }; - imguiGraphVertex2fv(v); + imguiGraphVertex2fvD3D12(v); } -void imguiGraphVertex2fv(const float* v) +void imguiGraphVertex2fvD3D12(const float* v) { // update state m_stateVert.x = v[0]; @@ -477,13 +481,13 @@ void imguiGraphVertex2fv(const float* v) } } -void imguiGraphTexCoord2f(float u, float v) +void imguiGraphTexCoord2fD3D12(float u, float v) { m_stateVert.u = u; m_stateVert.v = v; } -void imguiGraphColor4ub(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) +void imguiGraphColor4ubD3D12(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) { m_stateVert.rgba[0] = red; m_stateVert.rgba[1] = green; @@ -491,7 +495,7 @@ void imguiGraphColor4ub(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) m_stateVert.rgba[3] = alpha; } -void imguiGraphColor4ubv(const uint8_t* v) +void imguiGraphColor4ubvD3D12(const uint8_t* v) { m_stateVert.rgba[0] = v[0]; m_stateVert.rgba[1] = v[1]; @@ -499,18 +503,18 @@ void imguiGraphColor4ubv(const uint8_t* v) m_stateVert.rgba[3] = v[3]; } -void imguiGraphFontTextureEnable() +void imguiGraphFontTextureEnableD3D12() { } -void imguiGraphFontTextureDisable() +void imguiGraphFontTextureDisableD3D12() { m_stateVert.u = -1.f; m_stateVert.v = -1.f; } -void imguiGraphFontTextureInit(unsigned char* data) +void imguiGraphFontTextureInitD3D12(unsigned char* data) { ID3D12GraphicsCommandList* commandList = m_desc.commandList; @@ -653,7 +657,7 @@ void imguiGraphFontTextureInit(unsigned char* data) } -void imguiGraphFontTextureRelease() +void imguiGraphFontTextureReleaseD3D12() { COMRelease(m_texture); COMRelease(m_textureUploadHeap); diff --git a/demo/DemoAppD3D12/imguiGraphD3D12.h b/demo/DemoAppD3D12/imguiGraphD3D12.h index 7eeb538..d011188 100644 --- a/demo/DemoAppD3D12/imguiGraphD3D12.h +++ b/demo/DemoAppD3D12/imguiGraphD3D12.h @@ -29,7 +29,7 @@ struct ImguiDynamicDescriptorHeapD3D12 ImguiDescriptorReserveHandleD3D12(*reserveDescriptors)(void* userdata, UINT numDescriptors, UINT64 lastFenceCompleted, UINT64 nextFenceValue); }; -struct ImguiGraphDesc +struct ImguiGraphDescD3D12 { ID3D12Device* device = nullptr; ID3D12GraphicsCommandList* commandList = nullptr; @@ -42,7 +42,49 @@ struct ImguiGraphDesc ImguiDynamicDescriptorHeapD3D12 dynamicHeapCbvSrvUav = {}; - ImguiGraphDesc() {} + ImguiGraphDescD3D12() {} }; +inline const ImguiGraphDescD3D12* cast_to_imguiGraphDescD3D12(const ImguiGraphDesc* desc) +{ + return (const ImguiGraphDescD3D12*)(desc); +} + +inline ImguiGraphDesc* cast_from_imguiGraphDescD3D12(ImguiGraphDescD3D12* desc) +{ + return (ImguiGraphDesc*)(desc); +} + +IMGUI_GRAPH_API void imguiGraphContextInitD3D12(const ImguiGraphDesc* desc); + +IMGUI_GRAPH_API void imguiGraphContextUpdateD3D12(const ImguiGraphDesc* desc); + +IMGUI_GRAPH_API void imguiGraphContextDestroyD3D12(); + +IMGUI_GRAPH_API void imguiGraphRecordBeginD3D12(); + +IMGUI_GRAPH_API void imguiGraphRecordEndD3D12(); + +IMGUI_GRAPH_API void imguiGraphVertex2fD3D12(float x, float y); + +IMGUI_GRAPH_API void imguiGraphVertex2fvD3D12(const float* v); + +IMGUI_GRAPH_API void imguiGraphTexCoord2fD3D12(float u, float v); + +IMGUI_GRAPH_API void imguiGraphColor4ubD3D12(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); + +IMGUI_GRAPH_API void imguiGraphColor4ubvD3D12(const uint8_t* v); + +IMGUI_GRAPH_API void imguiGraphFontTextureEnableD3D12(); + +IMGUI_GRAPH_API void imguiGraphFontTextureDisableD3D12(); + +IMGUI_GRAPH_API void imguiGraphEnableScissorD3D12(int x, int y, int width, int height); + +IMGUI_GRAPH_API void imguiGraphDisableScissorD3D12(); + +IMGUI_GRAPH_API void imguiGraphFontTextureInitD3D12(unsigned char* data); + +IMGUI_GRAPH_API void imguiGraphFontTextureReleaseD3D12(); + #endif
\ No newline at end of file diff --git a/demo/DemoAppD3D12/imguiInteropD3D12.cpp b/demo/DemoAppD3D12/imguiInteropD3D12.cpp index 322e1b5..30a8760 100644 --- a/demo/DemoAppD3D12/imguiInteropD3D12.cpp +++ b/demo/DemoAppD3D12/imguiInteropD3D12.cpp @@ -21,7 +21,7 @@ ImguiDescriptorReserveHandleD3D12 imguiInteropReserveDescriptors(void* userdata, UINT numDescriptors, UINT64 lastFenceCompleted, UINT64 nextFenceValue) { - auto appctx = static_cast<AppGraphCtx*>(userdata); + auto appctx = cast_to_AppGraphCtxD3D12((AppGraphCtx*)userdata); auto srcHandle = appctx->m_dynamicHeapCbvSrvUav.reserveDescriptors(numDescriptors, lastFenceCompleted, nextFenceValue); ImguiDescriptorReserveHandleD3D12 handle = {}; handle.heap = srcHandle.heap; @@ -31,8 +31,10 @@ ImguiDescriptorReserveHandleD3D12 imguiInteropReserveDescriptors(void* userdata, return handle; } -void imguiInteropUpdateDesc(ImguiGraphDesc& desc, AppGraphCtx* appctx) +inline void imguiInteropUpdateDesc(ImguiGraphDescD3D12& desc, AppGraphCtx* appctxIn) { + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + desc.device = appctx->m_device; desc.commandList = appctx->m_commandList; desc.lastFenceCompleted = appctx->m_lastFenceComplete; @@ -43,18 +45,22 @@ void imguiInteropUpdateDesc(ImguiGraphDesc& desc, AppGraphCtx* appctx) desc.dynamicHeapCbvSrvUav.reserveDescriptors = imguiInteropReserveDescriptors; } -bool imguiInteropGraphInit(imguiGraphInit_t func, const char* fontpath, AppGraphCtx* appctx) +IMGUI_GRAPH_API bool imguiInteropGraphInitD3D12(imguiGraphInit_t func, const char* fontpath, AppGraphCtx* appctx); + +IMGUI_GRAPH_API void imguiInteropGraphUpdateD3D12(imguiGraphUpdate_t func, AppGraphCtx* appctx); + +bool imguiInteropGraphInitD3D12(imguiGraphInit_t func, const char* fontpath, AppGraphCtx* appctx) { - ImguiGraphDesc desc; + ImguiGraphDescD3D12 desc; imguiInteropUpdateDesc(desc, appctx); - return func(fontpath, &desc); + return func(fontpath, cast_from_imguiGraphDescD3D12(&desc)); } -void imguiInteropGraphUpdate(imguiGraphUpdate_t func, AppGraphCtx* appctx) +void imguiInteropGraphUpdateD3D12(imguiGraphUpdate_t func, AppGraphCtx* appctx) { - ImguiGraphDesc desc; + ImguiGraphDescD3D12 desc; imguiInteropUpdateDesc(desc, appctx); - return func(&desc); + return func(cast_from_imguiGraphDescD3D12(&desc)); }
\ No newline at end of file diff --git a/demo/DemoAppD3D12/meshD3D12.cpp b/demo/DemoAppD3D12/meshD3D12.cpp index 3b04436..5821c89 100644 --- a/demo/DemoAppD3D12/meshD3D12.cpp +++ b/demo/DemoAppD3D12/meshD3D12.cpp @@ -105,7 +105,7 @@ struct MeshConstantHeap } }; -struct MeshContext +struct MeshContextD3D12 { ID3D12Device* m_device = nullptr; ID3D12GraphicsCommandList* m_commandList = nullptr; @@ -116,8 +116,8 @@ struct MeshContext MeshConstantHeap* m_constantHeap = nullptr; - MeshContext() {} - ~MeshContext() + MeshContextD3D12() {} + ~MeshContextD3D12() { COMRelease(m_rootSignature); COMRelease(m_pipelineStateLH); @@ -130,7 +130,17 @@ struct MeshContext } }; -struct MeshIndexBuffer +inline MeshContextD3D12* cast_to_MeshContextD3D12(MeshContext* ctx) +{ + return (MeshContextD3D12*)(ctx); +} + +inline MeshContext* cast_from_MeshContextD3D12(MeshContextD3D12* ctx) +{ + return (MeshContext*)(ctx); +} + +struct MeshIndexBufferD3D12 { ID3D12Resource* m_buffer = nullptr; MeshUint m_numElements = 0u; @@ -138,15 +148,25 @@ struct MeshIndexBuffer ID3D12Resource* m_upload = nullptr; - MeshIndexBuffer() {} - ~MeshIndexBuffer() + MeshIndexBufferD3D12() {} + ~MeshIndexBufferD3D12() { COMRelease(m_buffer); COMRelease(m_upload); } }; -struct MeshVertexBuffer +inline MeshIndexBufferD3D12* cast_to_MeshIndexBufferD3D12(MeshIndexBuffer* buf) +{ + return (MeshIndexBufferD3D12*)(buf); +} + +inline MeshIndexBuffer* cast_from_MeshIndexBufferD3D12(MeshIndexBufferD3D12* buf) +{ + return (MeshIndexBuffer*)(buf); +} + +struct MeshVertexBufferD3D12 { ID3D12Resource* m_buffer = nullptr; MeshUint m_numElements = 0u; @@ -154,16 +174,28 @@ struct MeshVertexBuffer ID3D12Resource* m_upload = nullptr; - MeshVertexBuffer() {} - ~MeshVertexBuffer() + MeshVertexBufferD3D12() {} + ~MeshVertexBufferD3D12() { COMRelease(m_buffer); COMRelease(m_upload); } }; -MeshConstantHeap::MeshConstantHeap(MeshContext* meshContext, int size) : m_meshContext(meshContext) +inline MeshVertexBufferD3D12* cast_to_MeshVertexBufferD3D12(MeshVertexBuffer* buf) { + return (MeshVertexBufferD3D12*)(buf); +} + +inline MeshVertexBuffer* cast_from_MeshVertexBufferD3D12(MeshVertexBufferD3D12* buf) +{ + return (MeshVertexBuffer*)(buf); +} + +MeshConstantHeap::MeshConstantHeap(MeshContext* meshContextIn, int size) : m_meshContext(meshContextIn) +{ + auto meshContext = cast_to_MeshContextD3D12(meshContextIn); + // create a constant buffer { HRESULT hr = S_OK; @@ -208,9 +240,11 @@ MeshConstantHeap::MeshConstantHeap(MeshContext* meshContext, int size) : m_meshC } } -MeshContext* MeshContextCreate(const MeshContextDesc* desc) +MeshContext* MeshContextCreateD3D12(const MeshContextDesc* descIn) { - MeshContext* context = new MeshContext; + auto desc = cast_to_MeshContextDescD3D12(descIn); + + MeshContextD3D12* context = new MeshContextD3D12; context->m_device = desc->device; context->m_commandList = desc->commandList; @@ -333,30 +367,35 @@ MeshContext* MeshContextCreate(const MeshContextDesc* desc) // create constant heap { - context->m_constantHeap = new MeshConstantHeap(context, 4096u); + context->m_constantHeap = new MeshConstantHeap(cast_from_MeshContextD3D12(context), 4096u); } - return context; + return cast_from_MeshContextD3D12(context); } -void MeshContextUpdate(MeshContext* context, const MeshContextDesc* desc) +void MeshContextUpdateD3D12(MeshContext* contextIn, const MeshContextDesc* descIn) { + auto context = cast_to_MeshContextD3D12(contextIn); + auto desc = cast_to_MeshContextDescD3D12(descIn); + context->m_device = desc->device; context->m_commandList = desc->commandList; context->m_constantHeap->reset(); } -void MeshContextRelease(MeshContext* context) +void MeshContextReleaseD3D12(MeshContext* context) { if (context == nullptr) return; - delete context; + delete cast_to_MeshContextD3D12(context); } -MeshIndexBuffer* MeshIndexBufferCreate(MeshContext* context, MeshUint* indices, MeshUint numIndices) +MeshIndexBuffer* MeshIndexBufferCreateD3D12(MeshContext* contextIn, MeshUint* indices, MeshUint numIndices) { - MeshIndexBuffer* buffer = new MeshIndexBuffer; + auto context = cast_to_MeshContextD3D12(contextIn); + + MeshIndexBufferD3D12* buffer = new MeshIndexBufferD3D12; buffer->m_numElements = numIndices; // create an index buffer @@ -433,19 +472,21 @@ MeshIndexBuffer* MeshIndexBufferCreate(MeshContext* context, MeshUint* indices, buffer->m_view.Format = DXGI_FORMAT_R32_UINT; } - return buffer; + return cast_from_MeshIndexBufferD3D12(buffer); } -void MeshIndexBufferRelease(MeshIndexBuffer* buffer) +void MeshIndexBufferReleaseD3D12(MeshIndexBuffer* buffer) { if (buffer == nullptr) return; - delete buffer; + delete cast_to_MeshIndexBufferD3D12(buffer); } -MeshVertexBuffer* MeshVertexBufferCreate(MeshContext* context, MeshVertex* vertices, MeshUint numVertices) +MeshVertexBuffer* MeshVertexBufferCreateD3D12(MeshContext* contextIn, MeshVertex* vertices, MeshUint numVertices) { - MeshVertexBuffer* buffer = new MeshVertexBuffer; + auto context = cast_to_MeshContextD3D12(contextIn); + + MeshVertexBufferD3D12* buffer = new MeshVertexBufferD3D12; buffer->m_numElements = numVertices; // create a vertex buffer @@ -522,18 +563,20 @@ MeshVertexBuffer* MeshVertexBufferCreate(MeshContext* context, MeshVertex* verti buffer->m_view.SizeInBytes = bufferSize; } - return buffer; + return cast_from_MeshVertexBufferD3D12(buffer); } -void MeshVertexBufferRelease(MeshVertexBuffer* buffer) +void MeshVertexBufferReleaseD3D12(MeshVertexBuffer* buffer) { if (buffer == nullptr) return; - delete buffer; + delete cast_to_MeshVertexBufferD3D12(buffer); } -void MeshContextDraw(MeshContext* context, const MeshContextDrawParams* params) +void MeshContextDrawD3D12(MeshContext* contextIn, const MeshContextDrawParams* params) { + auto context = cast_to_MeshContextD3D12(contextIn); + using namespace DirectX; XMMATRIX matrix = XMMatrixTranspose(XMMatrixMultiply(XMMatrixMultiply( @@ -569,8 +612,8 @@ void MeshContextDraw(MeshContext* context, const MeshContextDrawParams* params) commandList->SetGraphicsRootConstantBufferView(0, cbvHandle); commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - commandList->IASetVertexBuffers(0, 1, ¶ms->vertexBuffer->m_view); - commandList->IASetIndexBuffer(¶ms->indexBuffer->m_view); + commandList->IASetVertexBuffers(0, 1, &cast_to_MeshVertexBufferD3D12(params->vertexBuffer)->m_view); + commandList->IASetIndexBuffer(&cast_to_MeshIndexBufferD3D12(params->indexBuffer)->m_view); - commandList->DrawIndexedInstanced((UINT)params->indexBuffer->m_numElements, 1, 0, 0, 0); + commandList->DrawIndexedInstanced((UINT)cast_to_MeshIndexBufferD3D12(params->indexBuffer)->m_numElements, 1, 0, 0, 0); }
\ No newline at end of file diff --git a/demo/DemoAppD3D12/meshD3D12.h b/demo/DemoAppD3D12/meshD3D12.h index 4bd0fab..94c8994 100644 --- a/demo/DemoAppD3D12/meshD3D12.h +++ b/demo/DemoAppD3D12/meshD3D12.h @@ -12,8 +12,35 @@ #include "../DemoApp/mesh.h" -struct MeshContextDesc +struct MeshContextDescD3D12 { ID3D12Device* device; ID3D12GraphicsCommandList* commandList; -};
\ No newline at end of file +}; + +inline const MeshContextDescD3D12* cast_to_MeshContextDescD3D12(const MeshContextDesc* desc) +{ + return (const MeshContextDescD3D12*)(desc); +} + +inline MeshContextDesc* cast_from_MeshContextDescD3D12(MeshContextDescD3D12* desc) +{ + return (MeshContextDesc*)(desc); +} + +MESH_API MeshContext* MeshContextCreateD3D12(const MeshContextDesc* desc); + +MESH_API void MeshContextUpdateD3D12(MeshContext* context, const MeshContextDesc* desc); + +MESH_API void MeshContextReleaseD3D12(MeshContext* context); + + +MESH_API MeshIndexBuffer* MeshIndexBufferCreateD3D12(MeshContext* context, MeshUint* indices, MeshUint numIndices); + +MESH_API void MeshIndexBufferReleaseD3D12(MeshIndexBuffer* buffer); + +MESH_API MeshVertexBuffer* MeshVertexBufferCreateD3D12(MeshContext* context, MeshVertex* vertices, MeshUint numVertices); + +MESH_API void MeshVertexBufferReleaseD3D12(MeshVertexBuffer* buffer); + +MESH_API void MeshContextDrawD3D12(MeshContext* context, const MeshContextDrawParams* params);
\ No newline at end of file diff --git a/demo/DemoAppD3D12/meshInteropD3D12.cpp b/demo/DemoAppD3D12/meshInteropD3D12.cpp index 19c034d..28649a0 100644 --- a/demo/DemoAppD3D12/meshInteropD3D12.cpp +++ b/demo/DemoAppD3D12/meshInteropD3D12.cpp @@ -19,20 +19,28 @@ #include "appD3D12Ctx.h" #include "meshD3D12.h" -MeshContext* MeshInteropContextCreate(AppGraphCtx* appctx) +MESH_API MeshContext* MeshInteropContextCreateD3D12(AppGraphCtx* appctx); + +MESH_API void MeshInteropContextUpdateD3D12(MeshContext* context, AppGraphCtx* appctx); + +MeshContext* MeshInteropContextCreateD3D12(AppGraphCtx* appctxIn) { - MeshContextDesc desc = {}; + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + + MeshContextDescD3D12 desc = {}; desc.device = appctx->m_device; desc.commandList = appctx->m_commandList; - return MeshContextCreate(&desc); + return MeshContextCreateD3D12(cast_from_MeshContextDescD3D12(&desc)); } -void MeshInteropContextUpdate(MeshContext* context, AppGraphCtx* appctx) +void MeshInteropContextUpdateD3D12(MeshContext* context, AppGraphCtx* appctxIn) { - MeshContextDesc desc = {}; + auto appctx = cast_to_AppGraphCtxD3D12(appctxIn); + + MeshContextDescD3D12 desc = {}; desc.device = appctx->m_device; desc.commandList = appctx->m_commandList; - return MeshContextUpdate(context, &desc); + return MeshContextUpdateD3D12(context, cast_from_MeshContextDescD3D12(&desc)); }
\ No newline at end of file |