RenderTargetView <-> Texture

2.1k views Asked by At

I found this tutorial, i follow it and i create the class DirectX

class DirectX
{
private:
D3D_DRIVER_TYPE DriverType;
D3D_FEATURE_LEVEL FeatureLevel;
public:
int InitDirectX(HWND &hwnd, WinDesc &wd)
{
    HRESULT result;
    RECT dimensions;
    GetClientRect( hwnd, &dimensions );
    int width=dimensions.right-dimensions.left;
    int height=dimensions.bottom-dimensions.top;
    D3D_DRIVER_TYPE driverTypes[]=
    {
        D3D_DRIVER_TYPE_HARDWARE, 
        D3D_DRIVER_TYPE_WARP, 
        D3D_DRIVER_TYPE_SOFTWARE
    };
    unsigned int totalDriverTypes=ARRAYSIZE(driverTypes);
    D3D_FEATURE_LEVEL featureLevels[]=
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0
    };
    unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels );
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    ZeroMemory(&swapChainDesc,sizeof(swapChainDesc));
    swapChainDesc.BufferCount=1;
    swapChainDesc.BufferDesc.Width=width;
    swapChainDesc.BufferDesc.Height=height;
    swapChainDesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChainDesc.BufferDesc.RefreshRate.Numerator=60;
    swapChainDesc.BufferDesc.RefreshRate.Denominator=1;
    swapChainDesc.BufferUsage=DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.OutputWindow=hwnd;
    swapChainDesc.Windowed=1;
    swapChainDesc.SampleDesc.Count=1;
    swapChainDesc.SampleDesc.Quality=0;
    for(int driver=0;driver<totalDriverTypes;++driver)
    {
        result = D3D11CreateDeviceAndSwapChain( 0, driverTypes[driver], 0,
                0, featureLevels, totalFeatureLevels,
                D3D11_SDK_VERSION, &swapChainDesc, &SwapChain,
                &Device, &FeatureLevel, &Context );
        if( SUCCEEDED( result ) )
        {
        D3D_DRIVER_TYPE driverType_ = driverTypes[driver];
        break;
        }
    }

    }
    D3D11_TEXTURE2D_DESC textureDesc;
    D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
    ZeroMemory(&textureDesc, sizeof(textureDesc));
    textureDesc.Width = width;
    textureDesc.Height = height;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.Usage = D3D11_USAGE_DEFAULT;
    textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = 0;
    textureDesc.MiscFlags = 0;
    result=Device->CreateTexture2D(&textureDesc, NULL, &backBuffer);

    renderTargetViewDesc.Format = textureDesc.Format;
    renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
    renderTargetViewDesc.Texture2D.MipSlice = 0;
    result=Device->CreateRenderTargetView(backBuffer, &renderTargetViewDesc, &RenderTargetView);

    shaderResourceViewDesc.Format = textureDesc.Format;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;
    result=Device->CreateShaderResourceView(backBuffer, &shaderResourceViewDesc, &shaderResourceView);

    float color[4]={1,1,0,0};
    Context->ClearRenderTargetView(RenderTargetView,color);
    Context->OMSetRenderTargets(1,&RenderTargetView,0);
}
};

When i run it it get no error put when i call SwapChain->Present(0,0) nothing is displayed althought the renderTargetView should to be yellow. Why?? I cannot found the error.

PS if i create a renderTargetView with this code:

ID3D11Texture2D* backBufferTexture;
result = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ),
                    ( LPVOID* )&backBufferTexture );
result = Device->CreateRenderTargetView( backBufferTexture, 0,
            &RenderTargetView );

all works; and in my code i check all the "result" values;

2° this is code of the sprite i want render: Init()//init vertex buffer, index buffer, texture, matrix.... the render functioon is this:

C->OMSetRenderTargets(1,&RT1,0);
C->Update(...)// matrix to sent vertex shader, set texture, vertex, buffer....
C->Draw(6,0)//draw the sprite;

It should render a sprite(ex. a ball) on RT1 then :

C->OMSetRenderTargets(1,&RT,0);
RT1->Update() //like the texture;
RT1->Draw(6,0)
...
Swapchain->present(1,0);

The RT1 is displayed(ex. if i have clear it with blue there is a blue shape on the screen i can translate, scale and rotate) but not the ball(i have render on RT1); but if i render the ball without set RT1 as renderTargetView the ball is correct displayed

0

There are 0 answers