Sign in to follow this  
chenpan

can a mipmap texture be bound to render target

Recommended Posts

chenpan    100
Hey,
I want to create a mipmap texture manually.
Such that texel (i, j) of level n stores the minimum of texels (2i, 2j), (2i+1, 2j), (2i, 2j+1), (2i+1, 2j+1) in level n-1.
Now I have level 0 of the mipmap texture.
I want to fill texels data to the other levels using shader.
Just like render to texture. I want to render to a level of a mipmap texture.
Can I bind a level of a mipmap texture to the render target? How to do it?
Thank you in advance!

Share this post


Link to post
Share on other sites
Adam_42    3629
Which API are you using?

Under D3D9 I think you'd need to create one render target per mip level which you can then render to with an appropriate shader using the higher mip level as a source texture, and then you'd copy from those into the mip levels of a separate texture using StretchRect().

Why do you want do do this?

Share this post


Link to post
Share on other sites
chenpan    100
I'm using D3D10.
I'm implementing a paper.
[quote name='Adam_42' timestamp='1310378440' post='4833654']
Which API are you using?

Under D3D9 I think you'd need to create one render target per mip level which you can then render to with an appropriate shader using the higher mip level as a source texture, and then you'd copy from those into the mip levels of a separate texture using StretchRect().

Why do you want do do this?
[/quote]

Share this post


Link to post
Share on other sites
pcmaster    982
You can generate each mip-map manually using a shader or whatever. But you'll probably have to create a temporary render-target of the selected mip-map's resolution, render the mip-map into it (using a shader) and then copy this tmp render-target texture into the sub-resource of your final texture (ID3D11DeviceContext::CopySubresourceRegion() + D3D11CalcSubresource(mipLevel, 0, numMipLevels), the same for DX10). The final texture has to be created first, with all the mip-map levels and they will be "replaced" this way.

Share this post


Link to post
Share on other sites
mind in a box    887
I did that before when implementing some ScreenSpaceDisplacementMapping-Paper.

Here's one little helper struct which should gibe you the information you need:
[code]struct PostFX_SSDM_Mip_RTV_SRV
{
~PostFX_SSDM_Mip_RTV_SRV()
{
if(SRV)SRV->Release();
if(RTV)RTV->Release();
}

PostFX_SSDM_Mip_RTV_SRV()
{
SRV=NULL;
RTV=NULL;
}

HRESULT Create(ID3D11Resource* Res, DXGI_FORMAT fmt, int Level)
{
HRESULT hr=S_OK;

//Create a render target view
D3D11_RENDER_TARGET_VIEW_DESC DescRT;
DescRT.Format = fmt;
DescRT.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
DescRT.Texture2D.MipSlice = Level;
LE(DXUTGetD3D11Device()->CreateRenderTargetView(Res,&DescRT,&RTV));

// Get the mip level and create the SRV
ID3D11Resource* MipRes;
RTV->GetResource(&MipRes);

// Create the resource view
D3D11_SHADER_RESOURCE_VIEW_DESC DescRV;
DescRV.Format = fmt;
DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
DescRV.Texture2D.MipLevels = 1;
DescRV.Texture2D.MostDetailedMip = Level;

LE(DXUTGetD3D11Device()->CreateShaderResourceView(MipRes , &DescRV, &SRV ));

MipRes->Release();

return hr;
}

ID3D11ShaderResourceView* SRV;
ID3D11RenderTargetView* RTV;
};[/code]

It has been a while since I wrote that code, but it should work. Just replace all 11s with 10s. :)

Share this post


Link to post
Share on other sites
chenpan    100
uh, it works. I just think it's inconvenient. So I'm wondering if D3D10 support setting a level of a mipmap texture as render target. That will be very convenient.
[quote name='pcmaster' timestamp='1310388162' post='4833701']
You can generate each mip-map manually using a shader or whatever. But you'll probably have to create a temporary render-target of the selected mip-map's resolution, render the mip-map into it (using a shader) and then copy this tmp render-target texture into the sub-resource of your final texture (ID3D11DeviceContext::CopySubresourceRegion() + D3D11CalcSubresource(mipLevel, 0, numMipLevels), the same for DX10). The final texture has to be created first, with all the mip-map levels and they will be "replaced" this way.
[/quote]

Share this post


Link to post
Share on other sites
mind in a box    887
[quote]uh, it works. I just think it's inconvenient. So I'm wondering if D3D10 support setting a level of a mipmap texture as render target. That will be very convenient.[/quote]

Well, I just told you how to create that rendertargetview...

Share this post


Link to post
Share on other sites
chenpan    100
Thank you very much. Your code snippet helps a lot.
[quote name='mind in a box' timestamp='1310388994' post='4833706']
I did that before when implementing some ScreenSpaceDisplacementMapping-Paper.

Here's one little helper struct which should gibe you the information you need:
[code]struct PostFX_SSDM_Mip_RTV_SRV
{
~PostFX_SSDM_Mip_RTV_SRV()
{
if(SRV)SRV->Release();
if(RTV)RTV->Release();
}

PostFX_SSDM_Mip_RTV_SRV()
{
SRV=NULL;
RTV=NULL;
}

HRESULT Create(ID3D11Resource* Res, DXGI_FORMAT fmt, int Level)
{
HRESULT hr=S_OK;

//Create a render target view
D3D11_RENDER_TARGET_VIEW_DESC DescRT;
DescRT.Format = fmt;
DescRT.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
DescRT.Texture2D.MipSlice = Level;
LE(DXUTGetD3D11Device()->CreateRenderTargetView(Res,&DescRT,&RTV));

// Get the mip level and create the SRV
ID3D11Resource* MipRes;
RTV->GetResource(&MipRes);

// Create the resource view
D3D11_SHADER_RESOURCE_VIEW_DESC DescRV;
DescRV.Format = fmt;
DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
DescRV.Texture2D.MipLevels = 1;
DescRV.Texture2D.MostDetailedMip = Level;

LE(DXUTGetD3D11Device()->CreateShaderResourceView(MipRes , &DescRV, &SRV ));

MipRes->Release();

return hr;
}

ID3D11ShaderResourceView* SRV;
ID3D11RenderTargetView* RTV;
};[/code]

It has been a while since I wrote that code, but it should work. Just replace all 11s with 10s. :)
[/quote]

Share this post


Link to post
Share on other sites
chenpan    100
What should I do other than creating the rendertargetview? Reset the viewport? Or anything else? Thank you ~
[quote name='mind in a box' timestamp='1310391226' post='4833721']
[quote]uh, it works. I just think it's inconvenient. So I'm wondering if D3D10 support setting a level of a mipmap texture as render target. That will be very convenient.[/quote]

Well, I just told you how to create that rendertargetview...
[/quote]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this