Skip to main content
GameDev.net gamedev.net
🔒 Locked

can a mipmap texture be bound to render target

Started by chenpan Jul 11, 2011 at 8:50 AM 7 replies 10k views
Original Post
chenpan
chenpan
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!
Adam_42
Adam_42
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?
chenpan
chenpan
I'm using D3D10.
I'm implementing a paper.

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?
pcmaster
pcmaster
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.
mind in a box
mind in a box
I did that before when implementing some ScreenSpaceDisplacementMapping-Paper.

Here's one little helper struct which should gibe you the information you need:
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;
};


It has been a while since I wrote that code, but it should work. Just replace all 11s with 10s.
chenpan
chenpan
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.

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.
mind in a box
mind in a box
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...
chenpan
chenpan
Thank you very much. Your code snippet helps a lot.

I did that before when implementing some ScreenSpaceDisplacementMapping-Paper.

Here's one little helper struct which should gibe you the information you need:
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;
};


It has been a while since I wrote that code, but it should work. Just replace all 11s with 10s.
chenpan
chenpan
What should I do other than creating the rendertargetview? Reset the viewport? Or anything else? Thank you ~

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.


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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.