简体   繁体   English

使用C ++的Directx11渲染不起作用

[英]Directx11 Rendering with C++ Not Working

I started to practice rendering with directx11.I had done rendering in directx 9. But DX11 is a pain in the ass.. I couldn't manage to even draw a simple traingle. 我开始练习DirectX11的渲染。我已经在DirectX 9中完成了渲染。但是DX11实在是太麻烦了。我什至无法画出一个简单的小方块。 I dont know what I am doing wrong. 我不知道我在做什么错。

I tried removing depth and stencil buffer but was in vain. 我尝试删除深度和模板缓冲区,但徒劳无功。 So after brooding over this issue for 2 hours i still can't find the solution. 因此,在解决了这个问题2个小时之后,我仍然找不到解决方案。

Can anyone please be kind enough to help me.. Please guys 任何人都可以请足够好心来帮助我..请大家

Thanks 谢谢

Here is the full code 这是完整的代码

#include <Windows.h>
#include <windowsx.h>
#include <D3D11.h>
#include <D3DX10math.h>
#include <dxerr.h>
#include <D3DX11.h>
#include <D3DX10.h>

#include "CoreApp.h"
#include "AppTime.h"


using namespace std;
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")




LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

bool InitWindowClass(HINSTANCE hinst,int shw,HWND * _hwnd,WNDCLASSEX * exClass);

RECT windowWidth = {0,0, 512,512};

void TW_CALL _Callbak(void * clenddata);

void InitPipeline();
void initGraphics();




ID3D11VertexShader * vshader = 0;
ID3D11PixelShader * pixelShader = 0;
ID3D11Device * d3dDevice = 0;   
ID3D11DeviceContext * d3dContext = 0;

#pragma region verticles
struct CVertex
{
    float X,Y,Z;
    D3DXCOLOR col;
};

namespace Color
{
    D3DXCOLOR white (255);
    D3DXCOLOR Red ((float)255,0.0,0.0,1.0);
}
#pragma endregion


int WINAPI WinMain(HINSTANCE inst,HINSTANCE previnst,LPSTR cmdLine,int show)
{


    // Window Params
    HWND hwnd;
    WNDCLASSEX exClass;
    MSG msg;
    GameTime time;

    // Iniitaliztion
    SecureZeroMemory(&hwnd,sizeof(HWND));
    SecureZeroMemory(&exClass,sizeof(exClass));
    SecureZeroMemory(&msg,sizeof(MSG));
    SecureZeroMemory(&time,sizeof(time));

if(InitWindowClass(inst,show,&hwnd,&exClass))
    {
    // Directx  11 Functionalities

        #pragma region Create the device


        D3D_FEATURE_LEVEL feature_LEVEL; // Output feature level



            HRESULT hr = D3D11CreateDevice(0,
                                           D3D_DRIVER_TYPE_HARDWARE,
                                           0,
                                           0,       // No flags 
                                           0,0,
                                           D3D11_SDK_VERSION,
                                           &d3dDevice,
                                           &feature_LEVEL,&d3dContext);
            if(FAILED(hr))
            {
                MessageBox(hwnd,L"Failed TO CREATE DEVICE",L"ERROR",0);
            }

      #pragma endregion

        #pragma region Multisampling
            UINT multisampleQuality = 0;
            d3dDevice->CheckMultisampleQualityLevels(DXGI_FORMAT_B8G8R8A8_UNORM,4,&multisampleQuality);


        #pragma endregion

        #pragma region DescribeSwapChain
            DXGI_SWAP_CHAIN_DESC swapDesc;

            // Allocate Mommory
             SecureZeroMemory(&swapDesc,sizeof(DXGI_SWAP_CHAIN_DESC));

            swapDesc.BufferDesc.Width =  512;
            swapDesc.BufferDesc.Height = 512;
            swapDesc.BufferDesc.RefreshRate.Numerator= 60;
            swapDesc.BufferDesc.RefreshRate.Denominator = 1;
            swapDesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
            swapDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
            swapDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

            //MSAA 
            swapDesc.SampleDesc.Count = 4;
            swapDesc.SampleDesc.Quality = multisampleQuality -1;

            //BackBuffer
            swapDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
            swapDesc.BufferCount = 1;

            swapDesc.OutputWindow = hwnd;
            swapDesc.Windowed = true;

            swapDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
            swapDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;


        #pragma endregion

        #pragma region CreateSwapChain

            #pragma region Obtain DXGIFactory

        // DXGIFactory >> DXGIAdaptor >> DXGIDevice

            // Get the DXGI device
            IDXGIDevice * dxgiDevice = 0;
            d3dDevice->QueryInterface(__uuidof(IDXGIDevice),(void**)&dxgiDevice);

            // Obtain DXGIAdaptor
            IDXGIAdapter * dxgiAdaptor = 0;
            dxgiDevice->GetParent(__uuidof(IDXGIAdapter),(void**)&dxgiAdaptor);

            IDXGIFactory * dxgiFactory = 0;
            dxgiAdaptor->GetParent(__uuidof(IDXGIFactory),(void**)&dxgiFactory);
            #pragma endregion


            IDXGISwapChain * SwapChain = 0;
                if(FAILED(dxgiFactory->CreateSwapChain(d3dDevice,&swapDesc,&SwapChain)))
                {
                    MessageBox(hwnd,L"Failed Creating back buffer",L"Error",0);
                }



                dxgiFactory->MakeWindowAssociation(hwnd,0);


                //Release COMs
            dxgiAdaptor->Release();
            dxgiDevice->Release();
            dxgiFactory->Release();

            /*IDXGISwapChain * SwapChain = 0;
            if(FAILED(D3D11CreateDeviceAndSwapChain(0,D3D_DRIVER_TYPE_HARDWARE,0,0,0,0,D3D11_SDK_VERSION,&swapDesc,&SwapChain,&d3dDevice,&feature_LEVEL,&d3dContext)))
            {
                MessageBox(hwnd,L"Failed Creating back buffer and device",L"Error",0);
            }*/


        #pragma endregion

        #pragma region Create Render Target View

            ID3D11RenderTargetView * RenderTarget ;
            ID3D11Texture2D * backBuffer ;

            SecureZeroMemory(&backBuffer,sizeof(ID3D11Texture2D));
            SecureZeroMemory(&RenderTarget,sizeof(ID3D11RenderTargetView));

            SwapChain->GetBuffer(0,__uuidof(ID3D11Texture2D),(void**)&backBuffer);
            d3dDevice->CreateRenderTargetView(backBuffer,NULL,&RenderTarget);
        //  d3dContext->OM
            //Release COM
            backBuffer->Release();



        #pragma endregion

        #pragma region Create Depth Stencil Texture

            D3D11_TEXTURE2D_DESC depthDesc;

            SecureZeroMemory(&depthDesc,sizeof(depthDesc)); // Allocate Memeory

            depthDesc.ArraySize = 1;

            //Flags
            depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
            depthDesc.CPUAccessFlags = 0;
            depthDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; // Format

            // Area
            depthDesc.Height = 512;
            depthDesc.Width = 512;

            // Mipmap and MXAA
            depthDesc.MipLevels= 1;
            depthDesc.SampleDesc.Count = 4;
            depthDesc.SampleDesc.Quality = multisampleQuality -1;


            depthDesc.Usage = D3D11_USAGE_DEFAULT;
            depthDesc.MiscFlags = 0;

            // Create and initialize pointers
            ID3D11Texture2D * DepthTexture = 0;
            ID3D11DepthStencilView * depthView = 0;

            //d3dDevice->CreateTexture2D(&depthDesc,0,&DepthTexture);
        //  d3dDevice->CreateDepthStencilView(DepthTexture,0,&depthView);


        #pragma endregion

        #pragma region Bind to Output
            d3dContext->OMSetRenderTargets(1,&RenderTarget,NULL); // removed depth buffer for testing but still wont work
        #pragma endregion

        #pragma region Create ViewPort
            D3D11_VIEWPORT viewport;

            //Clear mommory
            SecureZeroMemory(&viewport,sizeof(viewport));

            viewport.Height = 512;
            viewport.Width = 512;
            viewport.MinDepth = 0;
            viewport.MaxDepth = 1;
            viewport.TopLeftX = 0;
            viewport.TopLeftY = 0;

            d3dContext->RSSetViewports(1,&viewport);
        #pragma endregion


            TwInit(ETwGraphAPI::TW_DIRECT3D11,d3dDevice);
            TwWindowSize(swapDesc.BufferDesc.Width,swapDesc.BufferDesc.Height);


            time.Reset();

            InitPipeline();
            #pragma region Vertex Buffer Creation
            CVertex vertex[3] = {
                {0,0,0,Color::white},
                {0.5,0,0,Color::white},
                {0.25,0.5,0,Color::Red},
            };

            D3D11_BUFFER_DESC VbufferDesc;
            D3D11_SUBRESOURCE_DATA vBufferInit;

            SecureZeroMemory(&vertex,sizeof(vertex));
                SecureZeroMemory(&VbufferDesc,sizeof(VbufferDesc));
                SecureZeroMemory(&vBufferInit,sizeof(vBufferInit));

                VbufferDesc.Usage = D3D11_USAGE_DEFAULT;
                VbufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
                VbufferDesc.ByteWidth = 3* sizeof(CVertex);
                VbufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_FLAG::D3D11_CPU_ACCESS_READ;
                VbufferDesc.MiscFlags = 0;
                VbufferDesc.StructureByteStride = 0;


                vBufferInit.pSysMem = vertex;

                ID3D11Buffer * Vbuffer = {0};

                    d3dDevice->CreateBuffer(&VbufferDesc,&vBufferInit,&Vbuffer);



            #pragma endregion

            while(msg.message != WM_QUIT)
        {
            if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
          {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
            }else{


             // Update Sequences

                // Rendering
                d3dContext->ClearRenderTargetView(RenderTarget,D3DXCOLOR(0,0,300,0));


                UINT stride = sizeof(CVertex);
                UINT offset = 0;    
                d3dContext->IASetVertexBuffers(0,1,&Vbuffer,&stride,&offset);
                d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
                d3dContext->Draw(3,0);


                SwapChain->Present(0,0);
            }
        }
    }

    return msg.wParam;

}

void InitPipeline()
{
    ID3D10Blob * vBuffBlob = 0;
ID3D10Blob * pBuffBlob = 0;

    //SecureZeroMemory(&vBuffBlob,sizeof(ID3D10Blob));
    //SecureZeroMemory(&pBuffBlob,sizeof(ID3D10Blob));
    SecureZeroMemory(&vshader,sizeof(ID3D11VertexShader));
    SecureZeroMemory(&pixelShader,sizeof(ID3D11PixelShader));

    HRESULT hr = D3DX11CompileFromFile(L"source.shader",0,0,"VShader","vs_4_0",0,0,0,&vBuffBlob,0,0);
    D3DX11CompileFromFile(L"source.shader",0,0,"PShader","ps_4_0",0,0,0,&pBuffBlob,0,0);
    if(FAILED(hr))
        MessageBeep(1);

    d3dDevice->CreateVertexShader(vBuffBlob->GetBufferPointer(),vBuffBlob->GetBufferSize(),NULL,&vshader);
    d3dDevice->CreatePixelShader(pBuffBlob->GetBufferPointer(),pBuffBlob->GetBufferSize(),NULL,&pixelShader);


D3D11_INPUT_ELEMENT_DESC inp[] = {
    {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
    {"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0}
};
    SecureZeroMemory(inp,sizeof(inp));

    ID3D11InputLayout * inputL = 0;
    d3dDevice->CreateInputLayout(inp,2,vBuffBlob->GetBufferPointer(),vBuffBlob->GetBufferSize(),&inputL);


    d3dContext->IASetInputLayout(inputL);

                d3dContext->VSSetShader(vshader,NULL,0);
                d3dContext->PSSetShader(pixelShader,0,0);

    pBuffBlob->Release();
    vBuffBlob->Release();

}


// Initialize and show the Window
bool InitWindowClass(HINSTANCE hinst,int shw,HWND * _hwnd,WNDCLASSEX * exClass)
{

     exClass->cbSize = sizeof(WNDCLASSEX);
     exClass->hCursor = LoadCursor(0,IDC_ARROW);
     exClass->hInstance = hinst;
     exClass->lpfnWndProc = WndProc;
     exClass->lpszClassName = L"DX_Test";
     exClass->lpszMenuName = L"Test";
     exClass->hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
     RegisterClassEx(exClass);


     AdjustWindowRect(&windowWidth,WS_OVERLAPPEDWINDOW,false);

     (*_hwnd) = CreateWindowEx(0,L"DX_Test",L"Test",WS_OVERLAPPEDWINDOW,500,200,windowWidth.right -  windowWidth.left,windowWidth.bottom-windowWidth.top,NULL,NULL,hinst,0);

     ShowWindow((*_hwnd),shw);


     UpdateWindow(*_hwnd);

     return true;

}



// Message Loop
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
   .....

First off, you're not properly initializing the D3D11_INPUT_ELEMENT_DESC structure. 首先,您没有正确初始化D3D11_INPUT_ELEMENT_DESC结构。 You have: 你有:

D3D11_INPUT_ELEMENT_DESC inp[] = {
    {"POSITION",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,0},
    {"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,12}
};
SecureZeroMemory(inp,sizeof(inp));

which partially initializes the structure then completely zeros it. 它会部分初始化结构,然后将其完全清零。 That code snippet should be: 该代码段应为:

D3D11_INPUT_ELEMENT_DESC inp[] = {
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

which gives the full description of the input and how it's laid out in memory. 它提供了输入的完整说明以及它在内存中的布局方式。

Second, I'd suggest putting this entire project aside and spending about a week reading through both of the tutorials Chuck has linked, reading as much info on MSDN as possible, and give a good read through the book by Frank D. Luna. 其次,我建议您将整个项目放在一边,并花大约一周的时间阅读Chuck链接的两个教程,阅读尽可能多的MSDN信息,并充分阅读Frank D. Luna的书。 A lot of this stuff is addressed in his book, and all of the code is available online, so the book isn't required to learn the API (though it certainly helps). 他的书中提到了很多这些内容,并且所有代码都可以在线获得,因此不需要学习API就可以了(尽管它肯定会有所帮助)。

DirectX 9 was easier to get used to, as the fixed function pipeline helped to organize a lot of this extra work. DirectX 9更易于习惯,因为固定功能管道帮助组织了许多此类额外工作。 However, if you used DirectX 9.0c and Shader Model 2.x/3, a lot of this code is very similar, and is now required as there is no longer a fixed function pipeline in D3D. 但是,如果您使用DirectX 9.0c和Shader Model 2.x / 3,则其中的许多代码非常相似,并且由于D3D中不再存在固定功能管道,因此现在需要使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM