winapidirectx-12

How can I create a swapchain compatible with direct3d12?


I tried to create a swapchain for d3d12 and learned that you can't use the same function for d3d11 but even the right ones aren't working for me.

    COM<IDXGISwapChain4> swapchain(NULL); {
        COM<IDXGISwapChain1> temp(NULL);
        LOG(factory->CreateSwapChainForHwnd(queue.ptr, hwnd, (DXGI_SWAP_CHAIN_DESC1[]){{
            .Width = 1000, .Height = 600,
            .Format = DXGI_FORMAT_R8G8B8A8_UNORM,
            .SampleDesc = (DXGI_SAMPLE_DESC){
                .Count = 1,
                .Quality = 0,
            },
            .BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
            .BufferCount = 2,
            .SwapEffect = DXGI_SWAP_EFFECT_DISCARD,
            .Flags = 0
        }}, NULL, NULL, &temp.ptr)); d3dmsg();
        if(temp.ptr) LOG(temp->QueryInterface(IID_PPV_ARGS(&swapchain.ptr)));
    }

I did use the _com_error to write the error to the console, even used the d3d12debug to enable the debug layers.

The error message for the hresult says The application made a call that is invalid. Either the parameters of the call or the state of some object was incorrect. Enable the D3D debug layer in order to see details via debug messages., but I don't understand what exactly I am doing wrong, I also tried it with a proper structure but that did not work either.

I am handling the d3d debug messages myself but it hasn't printing anything to the console yet, I don't if i somehow failed at that too.

auto d3dmsg = [&info_queue](){
        UINT64 messageCount = info_queue->GetNumStoredMessages();
        for (UINT64 i = 0; i < messageCount; ++i) {
            SIZE_T messageLength = 0;
            info_queue->GetMessage(i, nullptr, &messageLength);

            // Allocate space for the message
            D3D12_MESSAGE* message = (D3D12_MESSAGE*)malloc(messageLength);
            if (message != nullptr) {
                // Get the message
                info_queue->GetMessage(i, message, &messageLength);

                if (message->Severity == D3D12_MESSAGE_SEVERITY_ERROR|D3D12_MESSAGE_SEVERITY_WARNING) {
                    printf("D3D12 Error: %s\n", message->pDescription);
                }

                // Free the allocated memory
                free(message);
            }
        }
        
    }; 

I would like to understand what I am doing wrong. I am just trying to write a hello_triangle.


Solution

  • According to your code, you are using DXGI_SWAP_EFFECT_DISCARD.

    According to the Doc:DXGI_SWAP_EFFECT enumeration

    This enumeration value is never supported. D3D12 apps must use DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL or DXGI_SWAP_EFFECT_FLIP_DISCARD.

    I suggest you could try to use the "flip" model instead of the "blt" present model.