DirectX Render to Texture
Asked Answered
P

1

6

i am trying to render my Scene to a texture to use it in shaders for some post processing effect. I am not very experienced with this kind of stuff so i have some questions here.. First off all:

I am having this code in my OnCreateDevice() method:

    D3D11_TEXTURE2D_DESC textureDesc;
    D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

    ZeroMemory(&textureDesc, sizeof(textureDesc));

    textureDesc.Width = 800/2;
    textureDesc.Height = 600/2;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
    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;


    pd3dDevice->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);


    renderTargetViewDesc.Format = textureDesc.Format;
    renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
    renderTargetViewDesc.Texture2D.MipSlice = 0;


    pd3dDevice->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetViewMap);

    shaderResourceViewDesc.Format = textureDesc.Format;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;


    pd3dDevice->CreateShaderResourceView(renderTargetTextureMap, &shaderResourceViewDesc, &shaderResourceViewMap);

I am creating a texture, a renderView and a resourceView. But where do i go from here? Do i have to define another buffer or does this work after all?

Specifically,

So i think i know i have to set the OMRenderTargets with: renderTargetViewMap and depthStencilView. But where do i have to set this? In every frame render call? Before or after i rendered the other objects in my scene? and my guess would be that i have to make a new shader to which i give this texture created to work with it.

Thanks for any insight :/

Psychopathy answered 29/6, 2012 at 20:5 Comment(7)
What have you tried? I'm sure there are MANY render to texture tutorials out there (google.com/…). What part, specifically are you looking for help with?Dupaix
So i think i know i have to set the OMRenderTargets with: renderTargetViewMap and depthStencilView. But where do i have to set this? In every frame render call? Before or after i rendered the other objects in my scene? and my guess would be that i have to make a new shader to which i give this texture created to work with it.Psychopathy
You will have to set it each time you want to render to the texture, yes (and as long as you haven't switched to another render target or the back buffer). Once you've rendered the data to the texture, set up the shader that processes this data and draw a screen aligned quad with this texture.Dupaix
Thanks. I will try around a little bit and see how this works. Thanks so far. I guess the screen aligned quad could be accomplished with a geometryshader? Or would this be a bad use of it?Psychopathy
That'd work, although I wouldn't use a geometry shader for simply rendering a quad :)Dupaix
Added my comment as an answer, please be sure to accept it so people who come by later will know it as such.Dupaix
You are right. I guess im just using a VertexBuffer then.Psychopathy
D
1

You will have to set it each time you want to render to the texture, yes (and as long as you haven't switched to another render target or the back buffer). Once you've rendered the data to the texture, set up the shader that processes this data and draw a screen aligned quad with this texture.

Dupaix answered 29/6, 2012 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.