DirectX10 (D3D10) & DevIL Image Library

After extensively searching online for some documentation on how to use the DevIL Image Library with DirectX10 to no avail, I have brought it upon myself to write a brief post on how to get them two to coexist in love.

Although DirectX10 (Direct3D10) already has functionality that allows users to load images directly, it might not be enough. For example, DirectX is unable to load some image types such as TARGA (.tga) files. And if you are writing a OpenGL & DirectX10 dual-renderer, you might want unite both renderers under a common image library API. Plus, DevIL has a lot of great functionality, and you want to take full advantage of it. Below, I provided the code to load an image given the filename.

#include <IL/il.h>
#include <string>
#include <assert.h>
 
// Use this macro to verify if D3D10 function succeeded
#define HV(x) if(!SUCCEEDED(x)) MessageBox(NULL, #x, "HRESULT fail", MB_OK)
 
// Creates a shader resource view given a filepath & D3D10 device
//	@filePath - String of the file path of the image file
//	@pDevice - Pointer to the D3D10 device
ID3D10ShaderResourceView* MakeSRV( const std::string& filePath, ID3D10Device* pDevice )
{
	// Will be filled and returned
	ID3D10ShaderResourceView* pSRV = NULL;
 
	// Load image from DevIL
	ILuint idImage;
	ilGenImages( 1, &idImage );
	ilBindImage( idImage );
	ilLoadImage( filePath.c_str() );
	assert ( IL_NO_ERROR == ilGetError() );
 
	// Fetch dimensions of image
	int width = ilGetInteger( IL_IMAGE_WIDTH );
	int height = ilGetInteger( IL_IMAGE_HEIGHT );
 
	// Load the data
	ilConvertImage( IL_RGBA,IL_UNSIGNED_BYTE );
	unsigned char * pData = ilGetData();
 
	// Build the texture header descriptor
	D3D10_TEXTURE2D_DESC descTex;
	descTex.Width = width;
	descTex.Height = height;
	descTex.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	descTex.Usage = D3D10_USAGE_DEFAULT;
	descTex.BindFlags = D3D10_BIND_SHADER_RESOURCE | D3D10_BIND_RENDER_TARGET;
	descTex.CPUAccessFlags = 0;
	descTex.MipLevels = 1;
	descTex.ArraySize = 1;
	descTex.SampleDesc.Count = 1;
	descTex.SampleDesc.Quality = 0;
	descTex.MiscFlags = D3D10_RESOURCE_MISC_GENERATE_MIPS;
 
	// Resource data descriptor
	D3D10_SUBRESOURCE_DATA data ;
	memset( &data, 0, sizeof(D3D10_SUBRESOURCE_DATA));
	data.pSysMem = pData;
	data.SysMemPitch = 4 * width;
 
	// Create the 2d texture from data
	ID3D10Texture2D * pTexture = NULL;
	HV( pDevice->CreateTexture2D( &descTex, &data, &pTexture ));
 
	// Create resource view descriptor
	D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
	srvDesc.Format = descTex.Format;
	srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
	srvDesc.Texture2D.MostDetailedMip = 0;
	srvDesc.Texture2D.MipLevels = D3D10_RESOURCE_MISC_GENERATE_MIPS ;
 
	// Create the shader resource view
	HV( pDevice->CreateShaderResourceView( pTexture, &srvDesc, &pSRV ));
 
	// Delete from IL buffer after image loaded correctly
	ilDeleteImages( 1, &idImage );
	idImage = 0;
 
	return pSRV;
}

There you have it. Let me know if you got any woes getting it to work. Thanks to my bud Manny for helping with the code.


Subscribe to comments Posted on 02.14.10 to Game Development, How To by Eddie
Post Tags:

Browse Timeline


Comments ( 2 )

Thanks buddy for that mip-mappy deliciousness

Eddie added these pithy words on Feb 17, 2010 @ 11:51 am

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2012 Illogic Tree | Powered by WordPress | Theme by Zidalgo | Log in