在Clearrendertargetview崩溃

时间:2010-08-02 00:59:53

标签: directx

我在调用函数Clearrendertargetview()时遇到了崩溃。

以下是代码:

HWND hwnd;

    ID3D10Device* device;
    IDXGISwapChain* swapChain;
    ID3D10RenderTargetView* renderTarget;
    D3D10_VIEWPORT  viewPort;

DXGI_SWAP_CHAIN_DESC swapchaind;

    SecureZeroMemory(&swapchaind, sizeof(DXGI_SWAP_CHAIN_DESC));

    swapchaind.BufferCount = 2;
    swapchaind.OutputWindow = hwnd;
    swapchaind.BufferDesc.Height = height;
    swapchaind.BufferDesc.Width = width;
    swapchaind.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapchaind.BufferDesc.RefreshRate.Numerator = 60;
    swapchaind.BufferDesc.RefreshRate.Denominator = 1;
    swapchaind.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapchaind.Windowed = true;
    swapchaind.SampleDesc.Count = 1;
    swapchaind.SampleDesc.Quality = 0;
    device = NULL;
    swapChain = NULL;
    if(FAILED(D3D10CreateDeviceAndSwapChain(NULL,
                                    D3D10_DRIVER_TYPE_HARDWARE,
                                    NULL,
                                    NULL,
                                    D3D10_SDK_VERSION,
                                    &swapchaind,
                                    &swapChain,
                                    &device))) exit(11);

    ID3D10Texture2D*  backbuffer;
    if(FAILED(swapChain->GetBuffer(0, _uuidof(ID3D10Texture2D), (void**)&backbuffer))) exit(12);
    if(FAILED(device->CreateRenderTargetView(backbuffer, NULL, &renderTarget))) exit(13);
    backbuffer->Release();
    device->OMGetRenderTargets(1, &renderTarget, NULL);

    viewPort.Height = height;
    viewPort.Width = width;
    viewPort.MaxDepth = 1.0f;
    viewPort.MinDepth = 0.0f;
    viewPort.TopLeftX = 0;
    viewPort.TopLeftY = 0;

    device->RSSetViewports(1, &viewPort);
float clearColor[4] = {0.0f,0.0f,1.0f,1.0f};
device->ClearRenderTargetView(renderTarget, clearColor); //da crash iz here
renderFunc();//rendering
swapChain->Present(0,0);

我哪里错了?

1 个答案:

答案 0 :(得分:0)

device->OMGetRenderTargets(1, &renderTarget, NULL);

你不想要

device->OMSetRenderTargets(1, &renderTarget, NULL);