Ryan Sutherland
Ryan Sutherland

Reputation: 1

how to fix problem with swap chain creation

im learning directx im working on initializing direct3d in my application and all of my HRESULTS are returning S_OK which is a success return code except the last one which involved swap chain creation i have the debug layer enabled so im getting a warning error in the Output Log that says 'OurDevice' could be 0... (OurDevice is the pointer name to my ID3D11Device).. after many hours of scanning through Microsoft Docs and checking and rechecking all my code i cant seem to find out the issue does anyone have a clue ?

#include "dx3dmanager.h"
#include "Core.h"
#include <Windows.h>
#include <d3d11.h>
#include <d3d11_1.h>
#include <d3d11_2.h>
#include <DirectXMath.h>
#include <dxgi.h>


dx3dmanager::dx3dmanager()
{
}


dx3dmanager::~dx3dmanager()
{
}

// this method intitializes the Direct3d version 11 API this 
initialization method is subject to change with new versions /*

void dx3dmanager::initialize3d(HWND MainWindow)

{
    // declarations for Direct3D feature levels ours will include 
versions 11-12 unless changed /*
    //declarations for our device(GPU) and device context /*
    UINT creationflags = D3D11_CREATE_DEVICE_DEBUG;

    ID3D11Device* OurDevice;

    ID3D11DeviceContext* IDeviceContext;

    D3D_FEATURE_LEVEL featurelevels[2] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_11_1

    };

    HRESULT CreateDev;


    switch (CreateDev) {
    case S_OK: {

        MessageBeep(0xFFFFFFFF);

    }

    }


     // checks multisampling quality 

  HRESULT checkmxquality;

    UINT m4xMsaaQuality;
    DXGI_FORMAT dxgi_format = DXGI_FORMAT_R8G8B8A8_UNORM;
    checkmxquality = OurDevice->CheckMultisampleQualityLevels(dxgi_format, 4, &m4xMsaaQuality);
    assert(m4xMsaaQuality > 0);

    switch (checkmxquality) {

    case S_OK: {

        MessageBeep(0xFFFFFFFF);

    }
                  break;
    }
    // descriptions for rendering structs IE:  sampling, format, and swap 
chain /*
    DXGI_SAMPLE_DESC sampdesc;
    sampdesc.Count = 1;
    sampdesc.Quality = 0;

    DXGI_MODE_DESC1 dxmode;
    dxmode.Width = 125;
    dxmode.Height = 125;
    dxmode.Format = dxgi_format;
    dxmode.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    dxmode.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
    dxmode.RefreshRate.Numerator = 60;
    dxmode.RefreshRate.Denominator = 1;
    dxmode.Stereo = false;

    DXGI_SWAP_CHAIN_DESC1 chaindesc;
    chaindesc.Width = 0;
    chaindesc.Height = 0;
    chaindesc.Format = dxgi_format;
    chaindesc.Stereo = false;
    chaindesc.SampleDesc = sampdesc;
    chaindesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    chaindesc.BufferCount = 3;
    chaindesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    chaindesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH| 
DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE| DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY;
    chaindesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
    chaindesc.Scaling = DXGI_SCALING_STRETCH;
    //------------------------------------------------------------------- 

-------------------------------/*

    // this is our DXGI interface (DXGI is not a part of direct3d it is a 
seperate API (COM) /* 
    IDXGIDevice* dxgiDevice = nullptr;
    CreateDev = OurDevice->QueryInterface(__uuidof(IDXGIDevice), 
(void**)& dxgiDevice);
    if (SUCCEEDED(CreateDev)) {

    MessageBeep(0xFFFFFFFF);
    }
    IDXGIAdapter* dxgiadapter = nullptr;
    CreateDev = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)& 
dxgiadapter);
    if (SUCCEEDED(CreateDev)) {

        MessageBeep(0xFFFFFFFF);
    }
    IDXGIFactory2* factory;
    CreateDev = dxgiadapter->GetParent(__uuidof(IDXGIFactory), (void**)& 
factory);
    if (SUCCEEDED(CreateDev)) {

        MessageBeep(0xFFFFFFFF);
    }
    IDXGISwapChain1* mSwapChain;
    CreateDev = factory->CreateSwapChainForHwnd(OurDevice, MainWindow, 
&chaindesc, NULL, NULL, &mSwapChain);

    if (FAILED(CreateDev)) {

        MessageBeep(0xFFFFFFFF);
    }


    //------------------------------------------------------------------- 
    -------------------------------/*

Upvotes: 0

Views: 2298

Answers (1)

Chuck Walbourn
Chuck Walbourn

Reputation: 41057

If you enable DXGI debugging as well as using D3D11_CREATE_DEVICE_DEBUG, then you will get output messages in the debug window that tells you problems that result in failed error codes.

Even with just the Direct3D 11 debugging enabled, you get:

D3D11 ERROR: ID3D11Device::CreateTexture2D1: D3D11_RESOURCE_MISC_GDI_COMPATIBLE
requires a B8G8R8A8 format.
[ STATE_CREATION ERROR #103: CREATETEXTURE2D_INVALIDMISCFLAGS]

So your problem is very simple and easy to fix: Change dxgi_format to DXGI_FORMAT_B8G8R8A8_UNORM

See Anatomy of Direct3D 11 Create Device as well as Direct3D Game Visual Studio templates

Upvotes: 0

Related Questions