Winapi CreateTexture2D和CreateDepthStencilView失败

Winapi CreateTexture2D和CreateDepthStencilView失败,winapi,directx-11,Winapi,Directx 11,我正在努力学习Directx 11.0。我遇到了一个错误。这是代码 // include the basic windows header files and the Direct3D header files #include <windows.h> #include <windowsx.h> #include <d3d11.h> #include <d3dx11.h> #include <d3dx10.h> // include

我正在努力学习Directx 11.0。我遇到了一个错误。这是代码

// include the basic windows header files and the Direct3D header files
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>

// include the Direct3D Library file
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")

// define the screen resolution
#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600

// global declarations
IDXGISwapChain *swapchain = NULL;             // the pointer to the swap chain interface
ID3D11Device *dev = NULL;                     // the pointer to our Direct3D device interface
ID3D11DeviceContext *devcon = NULL;           // the pointer to our Direct3D device context
ID3D11RenderTargetView *backbuffer = NULL;    // the pointer to our back buffer
ID3D11InputLayout *pLayout = NULL;            // the pointer to the input layout
ID3D11VertexShader *pVS = NULL;               // the pointer to the vertex shader
ID3D11PixelShader *pPS = NULL;                // the pointer to the pixel shader
ID3D11Buffer *pVBuffer = NULL;                // the pointer to the vertex buffer
ID3D11Buffer *pIBuffer = NULL;                // the pointer to the index buffer
ID3D11DepthStencilView *pDView = NULL;        // the pointer to depth stencil view
ID3D11Texture2D *pDStencil = NULL;            // the pointer to depth stencil

// a struct to define a single vertex
struct VERTEX{FLOAT X, Y, Z; D3DXCOLOR Color;};

// function prototypes
void InitD3D(HWND hWnd);    // sets up and initializes Direct3D
void RenderFrame(void);     // renders a single frame
void CleanD3D(void);        // closes Direct3D and releases memory
void InitGraphics(void);    // creates the shape to render
void InitPipeline(void);    // loads and prepares the shaders

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "WindowClass";

    RegisterClassEx(&wc);

    RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);

    hWnd = CreateWindowEx(NULL,
                          "WindowClass",
                          "Our First Direct3D Program",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          wr.right - wr.left,
                          wr.bottom - wr.top,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D
    InitD3D(hWnd);

    // enter the main loop:

    MSG msg;

    while(TRUE)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);

            if(msg.message == WM_QUIT)
                break;
        }

        RenderFrame();
    }

    // clean up DirectX and COM
    CleanD3D();

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}


// this function initializes and prepares Direct3D for use
void InitD3D(HWND hWnd)
{
    // create a struct to hold information about the swap chain
    DXGI_SWAP_CHAIN_DESC scd;

    // clear out the struct for use
    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

    // fill the swap chain description struct
    scd.BufferCount = 1;                                   // one back buffer
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
    scd.BufferDesc.Width = SCREEN_WIDTH;                   // set the back buffer width
    scd.BufferDesc.Height = SCREEN_HEIGHT;                 // set the back buffer height
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;     // how swap chain is to be used
    scd.OutputWindow = hWnd;                               // the window to be used
    scd.SampleDesc.Count = 4;                              // how many multisamples
    scd.Windowed = TRUE;                                   // windowed/full-screen mode
    scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;    // allow full-screen switching

    // create a device, device context and swap chain using the information in the scd struct
    D3D11CreateDeviceAndSwapChain(NULL,
                                  D3D_DRIVER_TYPE_HARDWARE,
                                  NULL,
                                  D3D11_CREATE_DEVICE_DEBUG,
                                  NULL,
                                  NULL,
                                  D3D11_SDK_VERSION,
                                  &scd,
                                  &swapchain,
                                  &dev,
                                  NULL,
                                  &devcon);


    // get the address of the back buffer
    ID3D11Texture2D *pBackBuffer;
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

    // use the back buffer address to create the render target
    dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
    pBackBuffer->Release();

    //Create depth stencil buffer
    D3D11_TEXTURE2D_DESC descDepth;
    descDepth.Width = SCREEN_WIDTH;
    descDepth.Height = SCREEN_HEIGHT;
    descDepth.MipLevels = 1;
    descDepth.ArraySize = 1;
    descDepth.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    descDepth.SampleDesc.Count = 4;
    descDepth.SampleDesc.Quality = 0;
    descDepth.Usage = D3D11_USAGE_DYNAMIC;
    descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags = 0;
    descDepth.MiscFlags = 0;

    dev->CreateTexture2D( &descDepth, NULL, &pDStencil );

    // Create the depth stencil view
    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    descDSV.Format = descDepth.Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
    descDSV.Texture2DMS.UnusedField_NothingToDefine = 0;

    dev->CreateDepthStencilView( pDStencil, &descDSV, &pDView );

    // set the render target as the back buffer
    devcon->OMSetRenderTargets(1, &backbuffer, pDView);

    // Set the viewport
    D3D11_VIEWPORT viewport;
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = SCREEN_WIDTH;
    viewport.Height = SCREEN_HEIGHT;
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;

    devcon->RSSetViewports(1, &viewport);

    InitPipeline();
    InitGraphics();
}


// this is the function used to render a single frame
void RenderFrame(void)
{
    // clear the back buffer to a deep blue
    devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));

    //clear the depth stencil view
    devcon->ClearDepthStencilView(pDView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

        // select which vertex buffer to display
        UINT stride = sizeof(VERTEX);
        UINT offset = 0;
        devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);

        //set index buffer
        devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0);

        // select which primtive type we are using
        devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

        // draw the vertex buffer to the back buffer
        devcon->DrawIndexed(9, 0, 0);

    // switch the back buffer and the front buffer
    swapchain->Present(0, 0);
}


// this is the function that cleans up Direct3D and COM
void CleanD3D(void)
{
    swapchain->SetFullscreenState(FALSE, NULL);    // switch to windowed mode

    // close and release all existing COM objects
    if(pLayout)pLayout->Release();
    if(pVS)pVS->Release();
    if(pPS)pPS->Release();
    if(pVBuffer)pVBuffer->Release();
    if(pIBuffer)pIBuffer->Release();
    if(swapchain)swapchain->Release();
    if(backbuffer)backbuffer->Release();
    if(dev)dev->Release();
    if(devcon)devcon->Release();
    if(pDStencil)pDStencil->Release();
    if(pDView)pDView->Release();
}


// this is the function that creates the shape to render
void InitGraphics()
{
    // create a triangle using the VERTEX struct
    VERTEX Vertices[] =
    {
        {-0.5f, 0.5f, 1.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
        {0.5f, 0.5f, 1.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
        {0.5f, -0.5f, 1.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
        {-0.5f, -0.5f, 1.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
        {0.75f, 0.75f, 0.0f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
        {0.75f, -0.5f, 0.0f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
        {0.0f, -0.5f, 0.0f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
    };

    DWORD Indices[]=
    {
        0,2,3,
        4,5,6,
    };

    // create the vertex buffer
    D3D11_BUFFER_DESC bd,id;
    ZeroMemory(&bd, sizeof(bd));
    ZeroMemory(&id, sizeof(id));

    bd.Usage = D3D11_USAGE_DYNAMIC;                // write access access by CPU and GPU
    bd.ByteWidth = sizeof(VERTEX) * 7;             // size is the VERTEX struct * 4
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;       // use as a vertex buffer
    bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;    // allow CPU to write in buffer

    id.Usage = D3D11_USAGE_DYNAMIC;                // write access access by CPU and GPU
    id.ByteWidth = sizeof(DWORD) * 3*3;             // size is the DWORD struct * 2 * 3
    id.BindFlags = D3D11_BIND_INDEX_BUFFER;       // use as a index buffer
    id.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;    // allow CPU to write in buffer

    dev->CreateBuffer(&bd, NULL, &pVBuffer);       // create the buffer
    dev->CreateBuffer(&id, NULL, &pIBuffer);

    // copy the vertices into the buffer
    D3D11_MAPPED_SUBRESOURCE ms;
    devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);    // map the buffer
    memcpy(ms.pData, Vertices, sizeof(Vertices));                       // copy the data
    devcon->Unmap(pVBuffer, NULL);                                      // unmap the buffer

     // copy the indices into the buffer
    D3D11_MAPPED_SUBRESOURCE ims;
    devcon->Map(pIBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ims);    // map the buffer
    memcpy(ims.pData, Indices, sizeof(Indices));                       // copy the data
    devcon->Unmap(pIBuffer, NULL);                                      // unmap the buffer
}


// this function loads and prepares the shaders
void InitPipeline()
{
    // load and compile the two shaders
    ID3D10Blob *VS, *PS;
    D3DX11CompileFromFile("shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
    D3DX11CompileFromFile("shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);

    // encapsulate both shaders into shader objects
    dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
    dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);

    // set the shader objects
    devcon->VSSetShader(pVS, 0, 0);
    devcon->PSSetShader(pPS, 0, 0);

    // create the input layout object
    D3D11_INPUT_ELEMENT_DESC ied[] =
    {
        {"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},
    };

    dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
    devcon->IASetInputLayout(pLayout);
}
//包括基本windows头文件和Direct3D头文件
#包括
#包括
#包括
#包括
#包括
//包括Direct3D库文件
#pragma注释(lib,“d3d11.lib”)
#pragma注释(lib,“d3dx11.lib”)
#pragma注释(lib,“d3dx10.lib”)
//定义屏幕分辨率
#定义屏幕宽度800
#定义屏幕高度600
//全球宣言
IDXGISwapChain*swapchain=NULL;//指向交换链接口的指针
ID3D11设备*dev=NULL;//指向Direct3D设备界面的指针
ID3D11DeviceContext*devcon=NULL;//指向Direct3D设备上下文的指针
ID3D11RenderTargetView*backbuffer=NULL;//指向后台缓冲区的指针
ID3D11InputLayout*播放=NULL;//指向输入布局的指针
ID3D11VertexShader*pVS=NULL;//指向顶点着色器的指针
ID3D11PixelShader*pPS=NULL;//指向像素着色器的指针
ID3D11Buffer*pVBuffer=NULL;//指向顶点缓冲区的指针
ID3D11Buffer*pIBuffer=NULL;//指向索引缓冲区的指针
ID3D11DepthStencilView*pDView=NULL;//指向深度模具视图的指针
ID3D11Texture2D*pDStencil=NULL;//指向深度模具的指针
//用于定义单个顶点的结构
结构顶点{FLOAT X,Y,Z;D3DXCOLOR;};
//功能原型
void InitD3D(HWND HWND);//设置并初始化Direct3D
无效渲染帧(无效);//渲染单个帧
空隙清理3d(空隙);//关闭Direct3D并释放内存
void InitGraphics(void);//创建要渲染的形状
void InitPipeline(void);//加载并准备着色器
//WindowProc函数原型
LRESULT回调WindowProc(HWND-HWND,UINT消息,WPARAM-WPARAM,LPARAM-LPARAM);
//任何Windows程序的入口点
int WINAPI WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
HWND-HWND;
WNDCLASSEX wc;
零内存(&wc,sizeof(WNDCLASSEX));
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.hInstance=hInstance;
wc.hCursor=LoadCursor(空,IDC_箭头);
wc.lpszClassName=“WindowClass”;
注册类别(&wc);
RECT wr={0,0,屏幕宽度,屏幕高度};
AdjustWindowRect(&wr,WS_重叠窗口,FALSE);
hWnd=CreateWindowEx(空,
“窗口类”,
“我们的第一个Direct3D程序”,
WS_重叠窗口,
CW_使用默认值,
CW_使用默认值,
wr.右-wr.左,
wr.底部-wr.顶部,
无效的
无效的
hInstance,
无效);
显示窗口(hWnd、nCmdShow);
//设置并初始化Direct3D
InitD3D(hWnd);
//进入主循环:
味精;
while(TRUE)
{
if(peek消息(&msg,NULL,0,0,PM_-REMOVE))
{
翻译信息(&msg);
发送消息(&msg);
如果(msg.message==WM\u退出)
打破
}
RenderFrame();
}
//清理DirectX和COM
清洁d3d();
返回msg.wParam;
}
//这是程序的主要消息处理程序
LRESULT回调WindowProc(HWND-HWND,UINT消息,WPARAM-WPARAM,LPARAM-LPARAM)
{
开关(信息)
{
案例WM_销毁:
{
PostQuitMessage(0);
返回0;
}中断;
}
返回DefWindowProc(hWnd、message、wParam、lParam);
}
//此函数用于初始化和准备Direct3D以供使用
无效初始D3D(HWND HWND)
{
//创建一个结构以保存有关交换链的信息
DXGI交换链描述scd;
//清除结构以供使用
零内存(&scd,sizeof(DXGI_-SWAP_-CHAIN_-DESC));
//填写交换链描述结构
scd.BufferCount=1;//一个后台缓冲区
scd.BufferDesc.Format=DXGI_Format_R8G8B8A8_UNORM;//使用32位颜色
scd.BufferDesc.Width=屏幕宽度;//设置后缓冲区宽度
scd.BufferDesc.Height=屏幕高度;//设置后缓冲区高度
scd.BufferUsage=DXGI\u USAGE\u RENDER\u TARGET\u OUTPUT;//如何使用交换链
scd.OutputWindow=hWnd;//要使用的窗口
scd.SampleDesc.Count=4;//有多少个多样本
scd.Windowed=TRUE;//窗口/全屏模式
scd.Flags=DXGI\u SWAP\u CHAIN\u FLAG\u ALLOW\u MODE\u SWITCH;//允许全屏切换
//使用scd结构中的信息创建设备、设备上下文和交换链
D3D11CreateDeviceAndSwapChain(空,
D3D_驱动器_类型_硬件,
无效的
D3D11_创建_设备_调试,
无效的
无效的
D3D11_SDK_版本,
&scd,
&swapchain,
&德夫,
无效的
&devcon);
//获取后台缓冲区的地址
ID3D11Texture2D*pBackBuffer;
swapchain->GetBuffer(0,u_uidof(ID3D11Texture2D),(LPVOID*)和pBackBuffer);
//使用后缓冲区地址创建渲染目标
dev->CreateRenderTargetView(pBackBuffer、NULL和backbuffer);
pBackBuffer->Release();
//创建深度模具缓冲区
D3D11_纹理2d_描述描述
descDSV.Flags = 0;            //0 for it 'not' being read only