23.9 C
New York
Monday, April 28, 2025

Directx12 – My dice in my DX12 window by no means exhibits


My dice isn’t proven in my DX12 window. As an alternative, I see that Home windows’s buffer icon is proven till clicking on the window through which it disappears. I see the blue background in my window however and not using a dice. I’ve tried for days to make this work, however I’m completely misplaced in why it doesn’t work.

That is the primary time I work with DX12. Please let me see in case you see one thing I lack.

I’m executing my challenge in vs 2022. My file packages.config exhibits the next:


  
  

Right here is my fundamental.CPP:

#embrace "Graphics.h"
#embrace 

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    change (uMsg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
    const wchar_t CLASS_NAME() = L"DirectX12Window";
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0, CLASS_NAME, L"DirectX 12 Dice", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, nullptr, nullptr, hInstance, nullptr);

    if (!hwnd) return -1;

    ShowWindow(hwnd, nCmdShow);

    Graphics graphics;
    if (!graphics.Initialize(hwnd)) return -1;

    MSG msg = {};
    whereas (msg.message != WM_QUIT) {
        if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        graphics.Replace();
        graphics.Render();
    }

    return 0;
}

Right here is my graphics. H:

#pragma as soon as
#embrace 
#embrace 
#embrace 
#embrace 
#embrace 
#embrace 

utilizing namespace Microsoft::WRL;
utilizing namespace DirectX;

class Graphics {
public:
    bool Initialize(HWND hwnd);
    void Replace();
    void Render();

non-public:
    void CreatePipeline();
    void CreateCube();

    static const UINT FrameCount = 2;
    HWND hwnd;
    UINT frameIndex;
    UINT rtvDescriptorSize;

    ComPtr machine;
    ComPtr commandQueue;
    ComPtr swapChain;
    ComPtr rtvHeap;
    ComPtr renderTargets(FrameCount);
    ComPtr commandAllocator;
    ComPtr commandList;
    ComPtr pipelineState;
    ComPtr rootSignature;
    ComPtr vertexBuffer;
    D3D12_VERTEX_BUFFER_VIEW vertexBufferView;
    ComPtr fence;
    UINT64 fenceValue;
    HANDLE fenceEvent;
};

Right here is my graphic.CPP:

#embrace "Graphics.h"
#embrace 
#embrace "d3dx12.h"

bool Graphics::Initialize(HWND hwnd) {
    this->hwnd = hwnd;

    UINT dxgiFactoryFlags = 0;
#ifdef _DEBUG
    ComPtr debugController;
    if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) = DXGI_CREATE_FACTORY_DEBUG;
    
#endif

    ComPtr manufacturing unit;
    CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&manufacturing unit));

    ComPtr hardwareAdapter;
    factory->EnumAdapters1(0, &hardwareAdapter);
    D3D12CreateDevice(hardwareAdapter.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&machine));

    D3D12_COMMAND_QUEUE_DESC queueDesc = {};
    queueDesc.Kind = D3D12_COMMAND_LIST_TYPE_DIRECT;
    queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
    device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue));

    DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
    swapChainDesc.BufferCount = FrameCount;
    swapChainDesc.Width = 1280;
    swapChainDesc.Peak = 720;
    swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
    swapChainDesc.SampleDesc.Depend = 1;

    ComPtr tempSwapChain;
    factory->CreateSwapChainForHwnd(commandQueue.Get(), hwnd, &swapChainDesc, nullptr, nullptr, &tempSwapChain);
    tempSwapChain.As(&swapChain);

    frameIndex = swapChain->GetCurrentBackBufferIndex();

    D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
    rtvHeapDesc.NumDescriptors = FrameCount;
    rtvHeapDesc.Kind = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
    device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap));

    rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);

    for (UINT i = 0; i GetBuffer(i, IID_PPV_ARGS(&renderTargets(i)));
        D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = rtvHeap->GetCPUDescriptorHandleForHeapStart();
        rtvHandle.ptr += i * rtvDescriptorSize;
        device->CreateRenderTargetView(renderTargets(i).Get(), nullptr, rtvHandle);
    }

    device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&commandAllocator));
    device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, commandAllocator.Get(), nullptr, IID_PPV_ARGS(&commandList));

    CreatePipeline();
    CreateCube();

    device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
    fenceValue = 1;
    fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);

    return true;
}

void Graphics::CreateCube() {
    // Outline the dice vertices
    struct Vertex {
        XMFLOAT3 place;
        XMFLOAT4 coloration;
    };

    Vertex vertices() = {
        {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f, 1.0f}}, // Purple
        {{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f, 1.0f}},  // Inexperienced
        {{0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f, 1.0f}},   // Blue
        {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 0.0f, 1.0f}},  // Yellow
        {{-0.5f, -0.5f, 0.5f}, {1.0f, 0.0f, 1.0f, 1.0f}},  // Magenta
        {{0.5f, -0.5f, 0.5f}, {0.0f, 1.0f, 1.0f, 1.0f}},   // Cyan
        {{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f, 1.0f}},    // White
        {{-0.5f, 0.5f, 0.5f}, {0.0f, 0.0f, 0.0f, 1.0f}}    // Black
    };

    // Outline the dice indices
    UINT16 indices() = {
        0, 1, 2, 0, 2, 3,  // Entrance face
        4, 6, 5, 4, 7, 6,  // Again face
        4, 5, 1, 4, 1, 0,  // Backside face
        3, 2, 6, 3, 6, 7,  // Prime face
        1, 5, 6, 1, 6, 2,  // Proper face
        4, 0, 3, 4, 3, 7   // Left face
    };

    const UINT vertexBufferSize = sizeof(vertices);
    const UINT indexBufferSize = sizeof(indices);

    // Create vertex buffer
    CD3DX12_HEAP_PROPERTIES heapProperties(D3D12_HEAP_TYPE_UPLOAD);
    CD3DX12_RESOURCE_DESC resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize);

    device->CreateCommittedResource(
        &heapProperties,
        D3D12_HEAP_FLAG_NONE,
        &resourceDesc,
        D3D12_RESOURCE_STATE_GENERIC_READ,
        nullptr,
        IID_PPV_ARGS(&vertexBuffer)
    );

    // Copy vertex information to buffer
    void* pVertexData;
    vertexBuffer->Map(0, nullptr, &pVertexData);
    memcpy(pVertexData, vertices, vertexBufferSize);
    vertexBuffer->Unmap(0, nullptr);

    vertexBufferView.BufferLocation = vertexBuffer->GetGPUVirtualAddress();
    vertexBufferView.StrideInBytes = sizeof(Vertex);
    vertexBufferView.SizeInBytes = vertexBufferSize;
}

void Graphics::CreatePipeline() {
    // Load compiled shader bytecode
    ComPtr vertexShader;
    ComPtr pixelShader;

    D3DReadFileToBlob(L"VertexShader.cso", &vertexShader);
    D3DReadFileToBlob(L"PixelShader.cso", &pixelShader);

    // Outline enter structure
    D3D12_INPUT_ELEMENT_DESC inputLayout() = {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
    };

    // Create Root Signature
    D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc = {};
    rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
    ComPtr signature;
    ComPtr error;
    D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error);
    device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&rootSignature));

    // Outline the PSO descriptor
    D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
    psoDesc.InputLayout = { inputLayout, _countof(inputLayout) };
    psoDesc.pRootSignature = rootSignature.Get();
    psoDesc.VS = { vertexShader->GetBufferPointer(), vertexShader->GetBufferSize() };
    psoDesc.PS = { pixelShader->GetBufferPointer(), pixelShader->GetBufferSize() };
    psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
    psoDesc.RTVFormats(0) = DXGI_FORMAT_R8G8B8A8_UNORM;
    psoDesc.NumRenderTargets = 1;
    psoDesc.SampleDesc.Depend = 1;

    // Create the pipeline state
    device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pipelineState));
}

void Graphics::Replace() {}

void Graphics::Render() {
    D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = rtvHeap->GetCPUDescriptorHandleForHeapStart();
    rtvHandle.ptr += frameIndex * rtvDescriptorSize;

    commandAllocator->Reset();
    commandList->Reset(commandAllocator.Get(), pipelineState.Get());
    const FLOAT clearColor(4) = { 0.098f, 0.098f, 0.439f, 1.0f }; // Midnight Blue
    commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
    commandList->Shut();

    ID3D12CommandList* commandLists() = { commandList.Get() };
    commandQueue->ExecuteCommandLists(_countof(commandLists), commandLists);
    swapChain->Current(1, 0);
    frameIndex = swapChain->GetCurrentBackBufferIndex();
}

Right here is my vertexShader.hlsl:

struct VSInput {
    float3 place : POSITION;
    float4 coloration : COLOR;
};

struct PSInput {
    float4 place : SV_POSITION;
    float4 coloration : COLOR;
};

cbuffer ConstantBuffer : register(b0) {
    float4x4 modelViewProjection;
};

PSInput fundamental(VSInput enter) {
    PSInput output;
    output.place = mul(float4(enter.place, 1.0f), modelViewProjection);
    output.coloration = enter.coloration;
    return output;
}

Right here is my pixelshader.hlsl:

struct PSInput {
    float4 place : SV_POSITION;
    float4 coloration : COLOR;
};

float4 fundamental(PSInput enter) : SV_TARGET{
    return enter.coloration;
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles