Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DirectX 11
#1
@Unseen Machine wrote:

AND TO ME we need an Extensions forum or something where these MAJOR extensions can be stored and easily got to...heck even an option in the GUI for packages would be AWESOME!

Heres your new challenge....as for the life off me i cant make it work! 

Code: (Select All)
 
#include <windows.h>
#include <d3d11.h>
#include <dxgi.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")       
// Global Internal State
HWND                    g_hwnd = NULL;
ID3D11Device*          g_pd3dDevice = NULL;
ID3D11DeviceContext*    g_pd3dDeviceContext = NULL;
IDXGISwapChain*        g_pSwapChain = NULL;
ID3D11RenderTargetView* g_pRenderTargetView = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
// Input Tracking
bool g_keys[256] = { false };
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_DESTROY: PostQuitMessage(0); return 0;
        case WM_KEYDOWN: if (wParam < 256) g_keys[wParam] = true; return 0;
        case WM_KEYUP:  if (wParam < 256) g_keys[wParam] = false; return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
extern "C" {
    // 1. Create the Window and store the handle globally
    __declspec(dllexport) void DX_Screen_New(int w, int h, char* title) {
        HINSTANCE hInstance = GetModuleHandle(NULL);
        WNDCLASS wc = { 0 };
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.lpszClassName = "DXWindowPro";
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        RegisterClass(&wc);
        g_hwnd = CreateWindow("DXWindowPro", title, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                              CW_USEDEFAULT, CW_USEDEFAULT, w, h, NULL, NULL, hInstance, NULL);
    }
    // 2. Initialize DX using the global g_hwnd
    __declspec(dllexport) int DX_Init(int width, int height) {
        if (!g_hwnd) return 0;
        DXGI_SWAP_CHAIN_DESC sd = { 0 };
        sd.BufferCount = 1;
        sd.BufferDesc.Width = width;
        sd.BufferDesc.Height = height;
        sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        sd.OutputWindow = g_hwnd; // Use global handle
        sd.SampleDesc.Count = 1;
        sd.Windowed = TRUE;
        D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3 };
        if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, levels, 3,
            D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, NULL, &g_pd3dDeviceContext))) return 0;
        ID3D11Texture2D* pBackBuffer;
        g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
        g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);
        pBackBuffer->Release();
        D3D11_TEXTURE2D_DESC dsd = { 0 };
        dsd.Width = width; dsd.Height = height; dsd.MipLevels = 1; dsd.ArraySize = 1;
        dsd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; dsd.SampleDesc.Count = 1;
        dsd.Usage = D3D11_USAGE_DEFAULT; dsd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
        ID3D11Texture2D* pDepthStencil;
        g_pd3dDevice->CreateTexture2D(&dsd, NULL, &pDepthStencil);
        g_pd3dDevice->CreateDepthStencilView(pDepthStencil, NULL, &g_pDepthStencilView);
        pDepthStencil->Release();
        g_pd3dDeviceContext->OMSetRenderTargets(1, &g_pRenderTargetView, g_pDepthStencilView);
        return 1;
    }
    __declspec(dllexport) void DX_ProcessEvents() {
        MSG msg;
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); }
    }
    __declspec(dllexport) void DX_Clear(float r, float g, float b, float a) {
        float color[4] = { r, g, b, a };
        if (g_pd3dDeviceContext) {
            g_pd3dDeviceContext->ClearRenderTargetView(g_pRenderTargetView, color);
            g_pd3dDeviceContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
        }
    }
    __declspec(dllexport) void DX_Present() { if (g_pSwapChain) g_pSwapChain->Present(1, 0); }
   
    __declspec(dllexport) int DX_IsKeyPressed(int vk) { return (vk >= 0 && vk < 256) ? (int)g_keys[vk] : 0; }
    __declspec(dllexport) void DX_Cleanup() {
        if (g_pDepthStencilView) g_pDepthStencilView->Release();
        if (g_pRenderTargetView) g_pRenderTargetView->Release();
        if (g_pSwapChain) g_pSwapChain->Release();
        if (g_pd3dDeviceContext) g_pd3dDeviceContext->Release();
        if (g_pd3dDevice) g_pd3dDevice->Release();
        g_hwnd = NULL;
    }
}

I see you've posted some C++ snippets as a challenge. Here is my take: writing raw D3D11 code is easy, but making it actually work with QB64PE requires more than just copying a boilerplate.

I openly admit that I use AI as a highly efficient 'thought partner' for the low-level C++ plumbing. However, the key is the integration. My code (see the attached sample) handles the specific ABI requirements of QB64PE, such as: * C-style wrappers for COM interfaces (which QB64 cannot call directly). * Proper string handling (NUL-termination for Win32 API). * Message loop synchronization and proper resource cleanup to prevent memory leaks or crashes.


Using AI to speed up the C++ 'bridge' development allows me to focus on the actual functionality within QB64. If your snippets 'don't work,' it's because AI can generate code, but it won't debug the memory mapping and calling conventions for you. I prefer results over snippets. Here is a fully functional, 64-bit DX11 triangle AND MORE demos to prove the point.

This feels like quite a leap in a completely different direction. Up until now, you've been focused on OpenGL wrappers, and suddenly there's this non-functional piece of C++ code for DirectX 11. OpenGL and D3D11 are two entirely different rendering APIs—while you can have both in a single project, mixing them is usually far from "free."

If your goal is "better performance" and you want to stick with D3D11, the cleanest approach is to render and present directly into its own DX window and swapchain. The moment you start copying frames from D3D11 into OpenGL (or vice versa), you often run into extra copies and synchronization overhead (GPU to GPU or GPU to CPU), which usually kills performance rather than saving it.

Regarding platforms: D3D11 is natively Windows-only. While it can run on Linux via Wine/Proton and DXVK (translating D3D11 to Vulkan), that is definitely not the same as a native port.

ZIP file contains DLL (64bit), CPP for DLL, BAS (DirectX11 workings demos)


Attached Files
.zip   D3D11.ZIP (Size: 341.84 KB / Downloads: 10)


Reply
#2
WTF Dude! Nice and thanks.

I want eventually to add DX to GDK and it was one of those things I worked on in downtime but couldn't ever get to work quite right. NOW I CAN ADD IT SO CHEERS!

+1 FROM ME

Unseen
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)