Forum

Notifications
Clear all

DirectX 12 3D Video Game Engine with Nvidia CUDA

1 Posts
1 Users
0 Reactions
16 Views
 josh
(@josh)
Member Admin
Joined: 2 months ago
Posts: 510
Topic starter  

Let’s architect a modular 3D game engine that showcases the top 20 DirectX 12 and CUDA API calls, with clean separation between rendering, compute, and engine logic. I’ll scaffold the folder structure, key file names, and source code snippets that demonstrate how these APIs integrate into a real-time engine. You’ll be able to plug in assets later with minimal friction.


🗂 Folder Structure

NeuroEngine/
├── Assets/                  # Placeholder for textures, models, shaders
├── Engine/
│   ├── Core/                # Initialization, windowing, input
│   ├── Graphics/            # DirectX 12 rendering pipeline
│   ├── Compute/             # CUDA compute modules
│   └── Utils/               # Logging, profiling, memory
├── Shaders/                 # HLSL and CUDA kernels
├── ThirdParty/              # DX12/CUDA headers, helper libs
├── App/
│   └── Main.cpp             # Entry point
├── CMakeLists.txt           # Build configuration
└── README.md

🔧 Top DirectX 12 API Calls Used

Category API Calls Used
Device & Adapter D3D12CreateDevice, IDXGIFactory4::EnumAdapters1, IDXGIAdapter1::GetDesc1
Command Queue ID3D12Device::CreateCommandQueue, ID3D12CommandQueue::ExecuteCommandLists
Swap Chain IDXGISwapChain3::Present, CreateSwapChain
Descriptor Heaps CreateDescriptorHeap, CreateRenderTargetView, CreateDepthStencilView
Resource Mgmt CreateCommittedResource, Map, Unmap, CopyResource
Pipeline State CreateGraphicsPipelineState, CreateRootSignature, SetGraphicsRootSignature
Command Lists CreateCommandList, Close, Reset, DrawInstanced
Synchronization CreateFence, Signal, WaitForSingleObject

⚙️ Top CUDA API Calls Used

Category API Calls Used
Initialization cudaSetDevice, cudaGetDeviceProperties, cudaDeviceSynchronize
Memory Mgmt cudaMalloc, cudaMemcpy, cudaFree, cudaMemset
Kernel Launch <<<>>>, cudaConfigureCall, cudaLaunchKernel
Streams & Events cudaStreamCreate, cudaStreamSynchronize, cudaEventRecord, cudaEventSynchronize
Error Handling cudaGetLastError, cudaPeekAtLastError

🧠 Sample Code Snippets

📁 Engine/Core/Init.cpp

// DXGI Factory and Device
ComPtr<IDXGIFactory4> dxgiFactory;
CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory));

ComPtr<IDXGIAdapter1> adapter;
dxgiFactory->EnumAdapters1(0, &adapter);

ComPtr<ID3D12Device> device;
D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_1, IID_PPV_ARGS(&device));

📁 Engine/Graphics/Renderer.cpp

// Command Queue
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue));

// Swap Chain
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
dxgiFactory->CreateSwapChain(commandQueue.Get(), &swapChainDesc, &swapChain);

// Render Target View
device->CreateRenderTargetView(renderTarget.Get(), nullptr, rtvHeap->GetCPUDescriptorHandleForHeapStart());

📁 Engine/Compute/CUDAInterop.cu

__global__ void ComputeLighting(float3* positions, float3* normals, float3* output) {
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    output[idx] = positions[idx] + normals[idx]; // Simplified lighting
}

extern "C" void LaunchLightingKernel(float3* d_positions, float3* d_normals, float3* d_output, int count) {
    int threads = 256;
    int blocks = (count + threads - 1) / threads;
    ComputeLighting<<<blocks, threads>>>(d_positions, d_normals, d_output);
    cudaDeviceSynchronize();
}

📁 App/Main.cpp

#include "Engine/Core/Init.h"
#include "Engine/Graphics/Renderer.h"
#include "Engine/Compute/CUDAInterop.h"

int main() {
    InitEngine();         // DX12 + Window
    InitCUDA();           // CUDA device setup
    while (IsRunning()) {
        UpdateScene();    // Game logic
        RunCompute();     // CUDA lighting
        RenderFrame();    // DX12 draw call
    }
    Shutdown();
    return 0;
}

🧱 Modular Extensibility

  • Assets: Plug in FBX/GLTF models, DDS textures, and HLSL shaders.
  • Compute: Expand CUDA kernels for physics, AI, or neural inference.
  • Graphics: Add post-processing, shadow maps, and deferred rendering.
  • Engine: Integrate ECS, input systems, and audio pipelines.

 


   
Quote
Share: