• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jakovo

Using mipmaps with RT's in DirectX9

14 posts in this topic

Hi,

I've been trying to create a texture as a RenderTarget, and I want to use mipmapping with that texture, so I create it with D3DUSAGE_RENDERTARGET | D3DUSAGE_AUTOGENERATEMIPMAP.

However, doesn't matter if I explicitly tell it to use X number of mipmaps or not, or if I use GenerateMipSubLevels() at some point, it will always have only 1 level.

I haven't found in the Doc's any information that DirectX9 does not support using mipmaps on a RT, however, I have google for any successfull case, and many have the same problem.

The only diference is when people are using XNA, in which case it is possible, but XNA docs state that mipmapping on RT does not work on XB360.. which makes me believe that natively XNA does something to support mipmapping with RT's that it is not supported by default on DX9.

Can anyone confirm this?... why does MSDN docs don't state that this is not possible?..

Thanks! Edited by ???
0

Share this post


Link to post
Share on other sites
[quote name='???' timestamp='1336743613' post='4939291']
However, doesn't matter if I explicitly tell it to use X number of mipmaps or not, or if I use GenerateMipSubLevels() at some point, it will always have only 1 level.
[/quote]IIRC, you're supposed to explicitly specify 0 mip-maps when using auto-generation. Also, you will have a full-mip chain if you inspect the texture in PIX, but if you query via the API, the object will report that it only has 1 mip-level.
1

Share this post


Link to post
Share on other sites
Thanks Hodgman,

Yes, I have specified 0 mip-maps also, with same result.

using tex2dlod() in the pixel shader always returns the same level, and as you mentioned the API always report it has only 1 mip-level.

so, apparently there is no support for mipmaping on RT's using DX9.

but I can't confirm my assumption.

Thanks!
0

Share this post


Link to post
Share on other sites
[quote name='???' timestamp='1336747239' post='4939309']using tex2dlod() in the pixel shader always returns the same level ... so, apparently there is no support for mipmaping on RT's using DX9[/quote]Hmm, that is strange... I have used them before, so it is possible.

Try using PIX to investigate the state of the RT, by capturing a frame from your game.
Also, what kind of GPU are you using? Older ones may not support this feature:[quote][url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172340(v=vs.85).aspx"]D3DUSAGE_AUTOGENMIPMAP[/url] is only a hint ... To check for support for automatic generation of mipmaps, check to see that D3DCAPS2_CANAUTOGENMIPMAP is set. If it is, call CheckDeviceFormat with D3DUSAGE_AUTOGENMIPMAP. If the return value is D3D_OK, the mipmaps are guaranteed to be autogenerated. If the return value is D3DOK_NOAUTOGEN, this means that the create call will succeed, but there won't be any mipmaps generated.[/quote] Edited by Hodgman
1

Share this post


Link to post
Share on other sites
Thanks Hodgman,

I have an ATI Radeon HD 5800 GPU (which supports Dx11), so at least the video card is not the problem.

Also, I have checked the return value I get from CheckDeviceFormat(), as the quote you sent... but it returns D3D_OK. :S
0

Share this post


Link to post
Share on other sites
[b]Hi![/b]

Please, read the forum's thread here:
[url="http://forums.create.msdn.com/forums/t/102169.aspx"]http://forums.create...s/t/102169.aspx[/url]
I hope it would be helpful.
Maybe you should use both VS & PS in SM 3.0 for the correct textures interpretation with the LOD.
(but I am not sure, that this is the reason)
0

Share this post


Link to post
Share on other sites
Thanks [b]kubera[/b],

I have read the thread you sent, but I'm actually using plain DirectX9 in my application (not DX11 with a lower Feature Level), so in this case both Vertex and Pixel shaders are 3.0... but thanks anyways ;)

Has anyone else had any successful story trying to create a Render Target with Mipmaps?... and if so do you remember what else did you did to achieve that?
0

Share this post


Link to post
Share on other sites
You would ask this question on Microsoft's DirectX forum.
[url="http://forums.create.msdn.com/forums/default.aspx?GroupID=7"]http://forums.create....aspx?GroupID=7[/url]

P. S.
It possible to register there without a payment. Edited by kubera
0

Share this post


Link to post
Share on other sites
One other thing you could check is your sampler state when using [font=courier new,courier,monospace]tex2dlod[/font] - maybe the problem is with [font=courier new,courier,monospace]D3DSAMP_MIPMAPLODBIAS[/font]/etc instead of with the creation of the texture?
0

Share this post


Link to post
Share on other sites
Thanks kubera,

Yes, I had already tryied to post a message on MS's forums to get ant response, unfortunateley the page is buggy and when you try to write the post (or send it when you have had luck to write it) errors keep appearing in the page and in the end no message is sent to the forums :(

Good observation Hodgman, I have just checked if the problem was there... however it doesn't seem to be the problem [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

looks like right now the only option is to get some confirmation by someone at Microsoft that this is really not possible (and if it is, what is the approriate way to do it)... the application's code is relativeley small so there should not be many weird things happening inside that could make it to miss behave the way it does.

if I get any news about it I will let you know.

Thanks! Edited by ???
0

Share this post


Link to post
Share on other sites
[quote name='???' timestamp='1337203048' post='4940769']looks like right now the only option is to get some confirmation by someone at Microsoft that this is really not possible[/quote]It is possible, I've done it before...
There's really only 4 places something can go wrong, so there's probably a small bug in your code in one of them:
* Create the resource.
* Bind the resource for writing.
* Bind the resource for sampling.
* Sample the resource.

I can't personally offer any more help/advice unless you post your code up, other then reiterating that PIX might be useful, and that you should enable the debug runtimes in the DirectX control panel to check for obvious errors. Edited by Hodgman
0

Share this post


Link to post
Share on other sites
Thanks [b]Hodgman[/b]

I have taken the SDK's Vertex tutorial sample, and managed to reduce it to the following code to handle the most basic form of a mipmapped render target:

[CODE]
//-----------------------------------------------------------------------------
// File: Vertices.cpp
//
// Desc: In this tutorial, we are rendering some Vertices. This introduces the
// concept of the vertex buffer, a Direct3D object used to store
// Vertices. Vertices can be defined any way we want by defining a
// custom structure and a custom FVF (flexible vertex format). In this
// tutorial, we are using Vertices that are transformed (meaning they
// are already in 2D window coordinates) and lit (meaning we are not
// using Direct3D lighting, but are supplying our own colors).
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <d3d9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
#include <d3dx9effect.h>

#define WWIDTH 640.0f
#define WHEIGHT 480.0f

#define DoSafeRelease( COM ) if( COM ){ COM->Release(); COM = NULL; }

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold Vertices
LPDIRECT3DVERTEXBUFFER9 g_pVB2 = NULL; // Buffer to hold Vertices
LPDIRECT3DTEXTURE9 g_pRTTexture = NULL;
LPDIRECT3DSURFACE9 g_pFrontBuffer = NULL;
ID3DXEffect* g_pEffect = NULL;
// A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

struct CUSTOMTEXVERTEX
{
FLOAT x, y, z, rhw;
DWORD color;
FLOAT u, v;
};
#define D3DFVF_CUSTOMTEXVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
HRESULT R;
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &amp;d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&amp;d3dpp, &amp;g_pd3dDevice ) ) )
{
return E_FAIL;
}
R = g_pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&amp;g_pFrontBuffer);
if( FAILED(R) ) return R;
// Device state would normally be set here
R = g_pd3dDevice->CreateTexture(640,480,0,(D3DUSAGE_RENDERTARGET|D3DUSAGE_AUTOGENMIPMAP), D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT,&amp;g_pRTTexture, NULL);
if( FAILED(R) ) return R;

LPD3DXBUFFER ErrBuffer;
D3DXCreateEffectFromFile( g_pd3dDevice, L".\\ReadLOD.fx", NULL, NULL, 0, NULL, &amp;g_pEffect, &amp;ErrBuffer );
if( !g_pEffect ) return E_FAIL;
return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitVB()
//-----------------------------------------------------------------------------
HRESULT InitVB()
{
// Initialize three Vertices for rendering a triangle
CUSTOMVERTEX Vertices[] =
{ // 640x480
{ (WWIDTH*0.5f ), (WHEIGHT*0.05f), 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ (WWIDTH*0.95f), (WHEIGHT*0.95f), 0.5f, 1.0f, 0xff00ff00, },
{ (WWIDTH*0.05f), (WHEIGHT*0.95f), 0.5f, 1.0f, 0xff00ffff, },
};
// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom Vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &amp;g_pVB, NULL ) ) )
{
return E_FAIL;
}
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the Vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof( Vertices ), ( void** )&amp;pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, Vertices, sizeof( Vertices ) );
g_pVB->Unlock();

CUSTOMTEXVERTEX Grid[] =
{
{0.0f, 0.0f, 0.5f, 1.0f, 0xFFffFFff, 0.0f, 0.0f },
{640.0f, 0.0f, 0.5f, 1.0f, 0xFFffFFff, 1.0f, 0.0f },
{640.0f, 480.0f, 0.5f, 1.0f, 0xFFffFFff, 1.0f, 1.0f },
{0.0f, 0.0f, 0.5f, 1.0f, 0xFFffFFff, 0.0f, 0.0f },
{640.0f, 480.0f, 0.5f, 1.0f, 0xFFffFFff, 1.0f, 1.0f },
{0.0f, 480.0f, 0.5f, 1.0f, 0xFFffFFff, 0.0f, 1.0f }
};
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 6 * sizeof( CUSTOMTEXVERTEX ),
0, D3DFVF_CUSTOMTEXVERTEX,
D3DPOOL_DEFAULT, &amp;g_pVB2, NULL ) ) )
{
return E_FAIL;
}
VOID* pGrid;
if( FAILED( g_pVB2->Lock( 0, sizeof( Grid ), ( void** )&amp;pGrid, 0 ) ) )
return E_FAIL;
memcpy( pGrid, Grid, sizeof( Grid ) );
g_pVB2->Unlock();
return S_OK;
}

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pVB != NULL )
g_pVB->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
if( g_pRTTexture != NULL )
g_pRTTexture->Release();
if( g_pEffect != NULL )
g_pEffect->Release();
}

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
IDirect3DSurface9 *pRT = NULL;

/***************** Render into the g_RTTexture surface *****************/
g_pd3dDevice->SetTexture( 0, NULL );
g_pRTTexture->GetSurfaceLevel( 0, &amp;pRT);
g_pd3dDevice->SetRenderTarget( 0, pRT );
DoSafeRelease( pRT )
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 128, 128, 128 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );

/***************** Render into the Frame Buffer *****************/
g_pd3dDevice->SetRenderTarget(0,g_pFrontBuffer);
g_pRTTexture->GenerateMipSubLevels();
g_pd3dDevice->SetTexture( 0, g_pRTTexture );

g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 225, 0, 225 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
UINT passes;
g_pEffect->SetTechnique( "Render_LOD" );
g_pEffect->Begin(&amp;passes,0);
for( UINT i=0; i < passes; i++ )
{
g_pEffect->BeginPass( i );
g_pd3dDevice->SetStreamSource( 0, g_pVB2, 0, sizeof( CUSTOMTEXVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMTEXVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
g_pEffect->EndPass();
}
g_pEffect->End();
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}

//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
UNREFERENCED_PARAMETER( hInst );
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3DLODRenderTarget", NULL
};
RegisterClassEx( &amp;wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3DLODRenderTarget", L"Test LOD on Render Target",
WS_OVERLAPPEDWINDOW, 100, 100, 658, 524,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &amp;msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
if( PeekMessage( &amp;msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &amp;msg );
DispatchMessage( &amp;msg );
}
else
Render();
}
}
}
UnregisterClass( L"D3DLODRenderTarget", wc.hInstance );
return 0;
}
[/CODE]

and for the PixelShader, this is the code:

[CODE]
sampler s;

float4 PS( float4 tex : TEXCOORD0 ) : COLOR
{
return tex2Dlod( s, float4( tex.xyz, 3 ) );
}
technique Render_LOD
{
pass P0
{
PixelShader = compile ps_3_0 PS();
}
}
[/CODE]

PIX doesn't really give me any useful information (except the fact that when creating the RT texture it doesn't take the D3DUSAGE_AUTOGENMIPMAP part, although it is explicitly created with it in the code).

Or am I missing something?

Thanks! Edited by ???
0

Share this post


Link to post
Share on other sites
Really really thank you [b]unbird[/b]!..

you are totalli right, I was almost convinced it was not possible, but now, after explictly setting the mip filter to point/linear it's doing it correctly.

thanks the three of you for your help!
1

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  
Followers 0