c++ - 无法从 NULL 资源创建 RenderTargetView

标签 c++ c visual-c++ directx

我正在尝试创建渲染目标 View ,但我从 direct X 收到此错误

 A RenderTargetView cannot be created from a NULL Resource

据我所知,我似乎必须在传递 rendertarget 指针之前用数据填充它。但我很难弄清楚如何做。下面是我的声明和实现

声明

#pragma once
#include "stdafx.h"
#include "resource.h"
#include "d3d10.h"
#include "d3dx10.h"
#include "dinput.h"


#define MAX_LOADSTRING 100

class RenderEngine {
protected:

    RECT m_screenRect;

    //direct3d Members
    ID3D10Device *m_pDevice; // The IDirect3DDevice10
    // interface
    ID3D10Texture2D *m_pBackBuffer; // Pointer to the back buffer
    ID3D10RenderTargetView *m_pRenderTargetView; // Pointer to render target view
    IDXGISwapChain *m_pSwapChain; // Pointer to the swap chain
    RECT m_rcScreenRect; // The dimensions of the screen

    ID3DX10Font *m_pFont; // The font used for rendering text
    // Sprites used to hold font characters
    ID3DX10Sprite *m_pFontSprite;

    ATOM RegisterEngineClass();
    void Present();

public:
    static HINSTANCE m_hInst;
    HWND m_hWnd;
    int m_nCmdShow;
    TCHAR m_szTitle[MAX_LOADSTRING];                    // The title bar text
    TCHAR m_szWindowClass[MAX_LOADSTRING];          // the main window class name

    void DrawTextString(int x, int y, D3DXCOLOR color, const TCHAR *strOutput);

    //static functions
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

    bool InitWindow();
    bool InitDirectX();
    bool InitInstance();
    int Run();

    RenderEngine()
    {
        m_screenRect.right = 800;
        m_screenRect.bottom = 600;
    }

};

我的实现

bool RenderEngine::InitDirectX()
{   
    //potential error. You did not set to zero memory and you did not set the scaling property
    DXGI_MODE_DESC bd;
    bd.Width                    = m_screenRect.right;
    bd.Height                   = m_screenRect.bottom;
    bd.Format                   = DXGI_FORMAT_R8G8B8A8_UNORM;
    bd.RefreshRate.Numerator    = 60;
    bd.RefreshRate.Denominator  = 1;

    DXGI_SAMPLE_DESC sd;
    sd.Count = 1;
    sd.Quality = 0;

    DXGI_SWAP_CHAIN_DESC swapDesc;
    ZeroMemory(&swapDesc, sizeof(swapDesc));

    swapDesc.BufferDesc     = bd;
    swapDesc.SampleDesc     = sd;
    swapDesc.BufferUsage    = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapDesc.OutputWindow   = m_hWnd;
    swapDesc.BufferCount    = 1;
    swapDesc.SwapEffect     = DXGI_SWAP_EFFECT_DISCARD,
    swapDesc.Windowed       = true;
    swapDesc.Flags          = 0;

    HRESULT hr;

    hr = D3D10CreateDeviceAndSwapChain(NULL,
        D3D10_DRIVER_TYPE_HARDWARE, 
        NULL, 
        D3D10_CREATE_DEVICE_DEBUG,
        D3D10_SDK_VERSION ,
        &swapDesc, &m_pSwapChain, &m_pDevice);

    if(FAILED(hr))
        return false;

    // Create a render target view
    hr = m_pDevice->CreateRenderTargetView(
                    m_pBackBuffer, NULL, &m_pRenderTargetView); // FAILS RIGHT HERE
    //

    if(FAILED(hr))
        return false;



    return true;
}

最佳答案

想通了。我忘记先检索缓冲区。通过调用

m_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &m_pBackBuffer);

然后我将缓冲区传递到渲染目标 View 方法中。

关于c++ - 无法从 NULL 资源创建 RenderTargetView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2892953/

相关文章:

c++ - 为什么Visual Studio生成的C++ "Hello World"项目看起来有点奇怪?

c++ - 在 C++/CLR 项目中引用 NuGet 包时编译器警告 C4691

c++ - 关于 boost::unique_future 的文档

c++ - vc++运行时错误

python - 无法避免 Cython 在 Python2.7 而不是 Python3.x 中编译外部 C 模块

c - 使用 malloc 对 double 动态数组进行合并排序,内存问题

c++ - 使用 C 中的 .lib 文件

C++双向链表 - 使用pop_back()从尾部删除元素

c++ - 模板非类型参数常量限制过滤库

使用 gcc 编译多个 C 文件