0

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

Liuka
  • 289
  • 2
  • 10
  • `Present()` won't show the new render target, but the swap chain's next backbuffer. You should not try to change this. – Nico Schertler Jan 06 '14 at 21:30
  • And how to set this as the next swap chain's back buffer? Actually i'm trying to render some sprites on this rendertargetview-texture and then display it on a first render target. I can clear it with a color(ex. blue), and render it like a texture of a square on my first rendertarget; but i can't render others texture on the rendertarget-texture: it remains blue – Liuka Jan 07 '14 at 07:37
  • The idea is the following. `RT1` is your new render target. `RTSwapChain` is the swap chain's original render target. Set `RT1` as the OM's render target. (Clear the target.) Draw your sprites. Set `RTSwapChain` as the OM's render target. Draw `RT1's` texture. Present. If that does not work, try to debug the program with a graphics debugger (i.e. the VS2012 graphics debugger, PIX ...). There you can see the results of the draw calls. – Nico Schertler Jan 07 '14 at 08:57
  • I have tried to do this but it doesnt work, i can post the code, and i cant use pix( i tried but when i run the program it soon crash). Anyway i can render RT1 like a texture, scaling and rotation it but no render on it another texture, althought i use OMSetRenderTargets(1,&RT1,0); – Liuka Jan 07 '14 at 13:04

0 Answers0