Esempio n. 1
0
	ETOOLS_API HRESULT WINAPI
		D3DX_CreateCubeTextureFromFileInMemoryEx(
		LPDIRECT3DDEVICE9       pDevice,
		LPCVOID                 pSrcData,
		UINT                    SrcDataSize,
		UINT                    Size,
		UINT                    MipLevels,
		DWORD                   Usage,
		D3DFORMAT               Format,
		D3DPOOL                 Pool,
		DWORD                   Filter,
		DWORD                   MipFilter,
		D3DCOLOR                ColorKey,
		D3DXIMAGE_INFO*         pSrcInfo,
		PALETTEENTRY*           pPalette,
		LPDIRECT3DCUBETEXTURE9* ppCubeTexture)
	{
		return D3DXCreateCubeTextureFromFileInMemoryEx(
			pDevice, pSrcData, SrcDataSize, Size, MipLevels, Usage,
			Format, Pool, Filter, MipFilter, ColorKey, pSrcInfo, pPalette,
			ppCubeTexture);
	}
Esempio n. 2
0
int JRenderServer::GetTextureID( const char* texName )
{
    if (!texName || !m_pDevice) return -1;
    for (int i = 0; i < m_Textures.size(); i++)
    {
        if (!stricmp( m_Textures[i].m_Name.c_str(), texName )) return i;
    }

    FInStream is;
    g_pFileServer->OpenMedia( texName, "", is );
    if (!is) return -1;

    int nBytes = is.GetTotalSize();
    BYTE* buf = new BYTE[nBytes];
    is.Read( buf, nBytes );
    
    D3DXIMAGE_INFO info;
    HRESULT hRes = D3DXGetImageInfoFromFileInMemory( buf, nBytes, &info );
    DXBaseTexture* pTex = NULL;


    TextureFile tex;
    tex.m_pDepthStencil = NULL;
    tex.m_Name          = texName;

    TextureProperties& tp = tex.m_Prop;

    if (info.ResourceType == D3DRTYPE_CUBETEXTURE)
    {
        IDirect3DCubeTexture8* pCubeTexture = NULL;
        hRes = D3DXCreateCubeTextureFromFileInMemoryEx( m_pDevice,
            buf, nBytes,
            D3DX_DEFAULT, 0,
            0, D3DFMT_UNKNOWN,
            D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
            0x00000000, NULL, NULL, &pCubeTexture );
        pTex = pCubeTexture;

        D3DSURFACE_DESC desc;
        pCubeTexture->GetLevelDesc( 0, &desc );
        tp.m_Format         = ConvertColorFormat( desc.Format );
        tp.m_Height         = desc.Height;
        tp.m_Width          = desc.Width;
        tp.m_PoolType       = ConvertPoolType( desc.Pool );
    }
    else
    {
        DXTexture* pTexture2D = NULL;
        hRes = D3DXCreateTextureFromFileInMemoryEx( m_pDevice,
            buf, nBytes,
            D3DX_DEFAULT, D3DX_DEFAULT, 0,
            0, D3DFMT_UNKNOWN,
            D3DPOOL_MANAGED , D3DX_DEFAULT, D3DX_DEFAULT,
            0x00000000, NULL, NULL, &pTexture2D );
        pTex = pTexture2D;

        D3DSURFACE_DESC desc;
        pTexture2D->GetLevelDesc( 0, &desc );
        tp.m_Format         = ConvertColorFormat( desc.Format );
        tp.m_Height         = desc.Height;
        tp.m_Width          = desc.Width;
        tp.m_PoolType       = ConvertPoolType( desc.Pool );
    }
    
    delete []buf;
    if (!pTex || hRes != S_OK) return -1;

    tp.m_bAutoGenMips   = false;
    tp.m_NMips          = pTex->GetLevelCount();
    strcpy( tp.m_Name, texName );

    tex.m_pTexture      = pTex;
    m_Textures.push_back( tex );
    return m_Textures.size() - 1;
} // JRenderServer::GetTextureID
Esempio n. 3
0
ID3DBaseTexture*	CRender::texture_load(LPCSTR fRName, u32& ret_msize)
{
    ID3DTexture2D*		pTexture2D		= NULL;
    IDirect3DCubeTexture9*	pTextureCUBE	= NULL;
    string_path				fn;
    u32						dwWidth,dwHeight;
    u32						img_size		= 0;
    int						img_loaded_lod	= 0;
    D3DFORMAT				fmt;
    u32						mip_cnt=u32(-1);
    // validation
    R_ASSERT				(fRName);
    R_ASSERT				(fRName[0]);

    // make file name
    string_path				fname;
    xr_strcpy(fname,fRName); //. andy if (strext(fname)) *strext(fname)=0;
    fix_texture_name		(fname);
    IReader* S				= NULL;
    //if (FS.exist(fn,"$game_textures$",fname,	".dds")	&& strstr(fname,"_bump"))	goto _BUMP;
    if (!FS.exist(fn,"$game_textures$",	fname,	".dds")	&& strstr(fname,"_bump"))	goto _BUMP_from_base;
    if (FS.exist(fn,"$level$",			fname,	".dds"))							goto _DDS;
    if (FS.exist(fn,"$game_saves$",		fname,	".dds"))							goto _DDS;
    if (FS.exist(fn,"$game_textures$",	fname,	".dds"))							goto _DDS;


#ifdef _EDITOR
    ELog.Msg(mtError,"Can't find texture '%s'",fname);
    return 0;
#else

    Msg("! Can't find texture '%s'",fname);
    R_ASSERT(FS.exist(fn,"$game_textures$",	"ed\\ed_not_existing_texture",".dds"));
    goto _DDS;

//	Debug.fatal(DEBUG_INFO,"Can't find texture '%s'",fname);

#endif

_DDS:
    {
        // Load and get header
        D3DXIMAGE_INFO			IMG;
        S						= FS.r_open	(fn);
#ifdef DEBUG
        Msg						("* Loaded: %s[%d]b",fn,S->length());
#endif // DEBUG
        img_size				= S->length	();
        R_ASSERT				(S);
        HRESULT const result	= D3DXGetImageInfoFromFileInMemory	(S->pointer(),S->length(),&IMG);
        if ( FAILED(result) ) {
            Msg					("! Can't get image info for texture '%s'",fn);
            FS.r_close			(S);
            string_path			temp;
            R_ASSERT			( FS.exist( temp, "$game_textures$", "ed\\ed_not_existing_texture", ".dds" ) );
            R_ASSERT			( xr_strcmp(temp,fn) );
            xr_strcpy			( fn, temp );
            goto _DDS;
        }

        if (IMG.ResourceType	== D3DRTYPE_CUBETEXTURE)			goto _DDS_CUBE;
        else														goto _DDS_2D;

_DDS_CUBE:
        {
            HRESULT const result	=
                D3DXCreateCubeTextureFromFileInMemoryEx(
                    HW.pDevice,
                    S->pointer(),S->length(),
                    D3DX_DEFAULT,
                    IMG.MipLevels,0,
                    IMG.Format,
                    D3DPOOL_MANAGED,
                    D3DX_DEFAULT,
                    D3DX_DEFAULT,
                    0,&IMG,0,
                    &pTextureCUBE
                );
            FS.r_close				(S);

            if ( FAILED(result) ) {
                Msg					("! Can't load texture '%s'",fn);
                string_path			temp;
                R_ASSERT			( FS.exist( temp, "$game_textures$", "ed\\ed_not_existing_texture", ".dds" ) );
                R_ASSERT			( xr_strcmp(temp,fn) );
                xr_strcpy			( fn, temp );
                goto _DDS;
            }

            // OK
            dwWidth					= IMG.Width;
            dwHeight				= IMG.Height;
            fmt						= IMG.Format;
            ret_msize				= calc_texture_size(img_loaded_lod, mip_cnt, img_size);
            mip_cnt					= pTextureCUBE->GetLevelCount();
            return					pTextureCUBE;
        }
_DDS_2D:
        {
            strlwr					(fn);
            // Load   SYS-MEM-surface, bound to device restrictions
            ID3DTexture2D*		T_sysmem;
            HRESULT const result	=
                D3DXCreateTextureFromFileInMemoryEx(
                    HW.pDevice,S->pointer(),S->length(),
                    D3DX_DEFAULT,D3DX_DEFAULT,
                    IMG.MipLevels,0,
                    IMG.Format,
                    D3DPOOL_SYSTEMMEM,
                    D3DX_DEFAULT,
                    D3DX_DEFAULT,
                    0,&IMG,0,
                    &T_sysmem
                );
            FS.r_close				(S);

            if ( FAILED(result) ) {
                Msg					("! Can't load texture '%s'",fn);
                string_path			temp;
                R_ASSERT			( FS.exist( temp, "$game_textures$", "ed\\ed_not_existing_texture", ".dds" ) );
                strlwr				(temp);
                R_ASSERT			( xr_strcmp(temp,fn) );
                xr_strcpy			( fn, temp );
                goto _DDS;
            }

            img_loaded_lod			= get_texture_load_lod(fn);
            pTexture2D				= TW_LoadTextureFromTexture(T_sysmem,IMG.Format, img_loaded_lod, dwWidth, dwHeight);
            mip_cnt					= pTexture2D->GetLevelCount();
            _RELEASE				(T_sysmem);

            // OK
            fmt						= IMG.Format;
            ret_msize				= calc_texture_size(img_loaded_lod, mip_cnt, img_size);
            return					pTexture2D;
        }
    }
    /*
    _BUMP:
    {
    	// Load   SYS-MEM-surface, bound to device restrictions
    	D3DXIMAGE_INFO			IMG;
    	IReader* S				= FS.r_open	(fn);
    	msize					= S->length	();
    	ID3DTexture2D*		T_height_gloss;
    	R_CHK(D3DXCreateTextureFromFileInMemoryEx(
    		HW.pDevice,	S->pointer(),S->length(),
    		D3DX_DEFAULT,D3DX_DEFAULT,	D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,
    		D3DPOOL_SYSTEMMEM,			D3DX_DEFAULT,D3DX_DEFAULT,
    		0,&IMG,0,&T_height_gloss	));
    	FS.r_close				(S);
    	//TW_Save						(T_height_gloss,fname,"debug-0","original");

    	// Create HW-surface, compute normal map
    	ID3DTexture2D*	T_normal_1	= 0;
    	R_CHK(D3DXCreateTexture		(HW.pDevice,IMG.Width,IMG.Height,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&T_normal_1));
    	R_CHK(D3DXComputeNormalMap	(T_normal_1,T_height_gloss,0,0,D3DX_CHANNEL_RED,_BUMPHEIGH));
    	//TW_Save						(T_normal_1,fname,"debug-1","normal");

    	// Transfer gloss-map
    	TW_Iterate_1OP				(T_normal_1,T_height_gloss,it_gloss_rev);
    	//TW_Save						(T_normal_1,fname,"debug-2","normal-G");

    	// Compress
    	fmt								= D3DFMT_DXT5;
    	ID3DTexture2D*	T_normal_1C	= TW_LoadTextureFromTexture(T_normal_1,fmt,psTextureLOD,dwWidth,dwHeight);
    	//TW_Save						(T_normal_1C,fname,"debug-3","normal-G-C");

    #if RENDER==R_R2
    	// Decompress (back)
    	fmt								= D3DFMT_A8R8G8B8;
    	ID3DTexture2D*	T_normal_1U	= TW_LoadTextureFromTexture(T_normal_1C,fmt,0,dwWidth,dwHeight);
    	// TW_Save						(T_normal_1U,fname,"debug-4","normal-G-CU");

    	// Calculate difference
    	ID3DTexture2D*	T_normal_1D = 0;
    	R_CHK(D3DXCreateTexture(HW.pDevice,dwWidth,dwHeight,T_normal_1U->GetLevelCount(),0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&T_normal_1D));
    	TW_Iterate_2OP				(T_normal_1D,T_normal_1,T_normal_1U,it_difference);
    	// TW_Save						(T_normal_1D,fname,"debug-5","normal-G-diff");

    	// Reverse channels back + transfer heightmap
    	TW_Iterate_1OP				(T_normal_1D,T_height_gloss,it_height_rev);
    	// TW_Save						(T_normal_1D,fname,"debug-6","normal-G-diff-H");

    	// Compress
    	fmt								= D3DFMT_DXT5;
    	ID3DTexture2D*	T_normal_2C	= TW_LoadTextureFromTexture(T_normal_1D,fmt,0,dwWidth,dwHeight);
    	// TW_Save						(T_normal_2C,fname,"debug-7","normal-G-diff-H-C");
    	_RELEASE					(T_normal_1U	);
    	_RELEASE					(T_normal_1D	);

    	//
    	string256			fnameB;
    	strconcat			(fnameB,"$user$",fname,"X");
    	ref_texture			t_temp		= dxRenderDeviceRender::Instance().Resources->_CreateTexture	(fnameB);
    	t_temp->surface_set	(T_normal_2C	);
    	_RELEASE			(T_normal_2C	);	// texture should keep reference to it by itself
    #endif

    	// release and return
    	// T_normal_1C	- normal.gloss,		reversed
    	// T_normal_2C	- 2*error.height,	non-reversed
    	_RELEASE			(T_height_gloss	);
    	_RELEASE			(T_normal_1		);
    	return				T_normal_1C;
    }
    */
_BUMP_from_base:
    {
        Msg			("! auto-generated bump map: %s",fname);
//////////////////
#ifndef _EDITOR
        if (strstr(fname,"_bump#"))
        {
            R_ASSERT2	(FS.exist(fn,"$game_textures$",	"ed\\ed_dummy_bump#",	".dds"), "ed_dummy_bump#");
            S						= FS.r_open	(fn);
            R_ASSERT2				(S, fn);
            img_size				= S->length	();
            goto		_DDS_2D;
        }
        if (strstr(fname,"_bump"))
        {
            R_ASSERT2	(FS.exist(fn,"$game_textures$",	"ed\\ed_dummy_bump",	".dds"),"ed_dummy_bump");
            S						= FS.r_open	(fn);

            R_ASSERT2	(S, fn);

            img_size				= S->length	();
            goto		_DDS_2D;
        }
#endif
//////////////////

        *strstr		(fname,"_bump")	= 0;
        R_ASSERT2	(FS.exist(fn,"$game_textures$",	fname,	".dds"),fname);

        // Load   SYS-MEM-surface, bound to device restrictions
        D3DXIMAGE_INFO			IMG;
        S						= FS.r_open	(fn);
        img_size				= S->length	();
        ID3DTexture2D*		T_base;
        R_CHK2(D3DXCreateTextureFromFileInMemoryEx(
                   HW.pDevice,	S->pointer(),S->length(),
                   D3DX_DEFAULT,D3DX_DEFAULT,	D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,
                   D3DPOOL_SYSTEMMEM,			D3DX_DEFAULT,D3DX_DEFAULT,
                   0,&IMG,0,&T_base	), fn);
        FS.r_close				(S);

        // Create HW-surface
        ID3DTexture2D*	T_normal_1	= 0;
        R_CHK(D3DXCreateTexture		(HW.pDevice,IMG.Width,IMG.Height,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM, &T_normal_1));
        R_CHK(D3DXComputeNormalMap	(T_normal_1,T_base,0,D3DX_NORMALMAP_COMPUTE_OCCLUSION,D3DX_CHANNEL_LUMINANCE,_BUMPHEIGH));

        // Transfer gloss-map
        TW_Iterate_1OP				(T_normal_1,T_base,it_gloss_rev_base);

        // Compress
        fmt								= D3DFMT_DXT5;
        img_loaded_lod					= get_texture_load_lod(fn);
        ID3DTexture2D*	T_normal_1C	= TW_LoadTextureFromTexture(T_normal_1, fmt, img_loaded_lod, dwWidth, dwHeight);
        mip_cnt							= T_normal_1C->GetLevelCount();

#if RENDER==R_R2
        // Decompress (back)
        fmt								= D3DFMT_A8R8G8B8;
        ID3DTexture2D*	T_normal_1U	= TW_LoadTextureFromTexture(T_normal_1C,fmt,0,dwWidth,dwHeight);

        // Calculate difference
        ID3DTexture2D*	T_normal_1D = 0;
        R_CHK(D3DXCreateTexture(HW.pDevice,dwWidth,dwHeight,T_normal_1U->GetLevelCount(),0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&T_normal_1D));
        TW_Iterate_2OP		(T_normal_1D,T_normal_1,T_normal_1U,it_difference);

        // Reverse channels back + transfer heightmap
        TW_Iterate_1OP		(T_normal_1D,T_base,it_height_rev_base);

        // Compress
        fmt								= D3DFMT_DXT5;
        ID3DTexture2D*	T_normal_2C	= TW_LoadTextureFromTexture(T_normal_1D,fmt,0,dwWidth,dwHeight);
        _RELEASE						(T_normal_1U	);
        _RELEASE						(T_normal_1D	);

        //
        string256			fnameB;
        strconcat			(sizeof(fnameB),fnameB,"$user$",fname,"_bumpX");
        ref_texture			t_temp			= dxRenderDeviceRender::Instance().Resources->_CreateTexture	(fnameB);
        t_temp->surface_set	(T_normal_2C	);
        _RELEASE			(T_normal_2C	);	// texture should keep reference to it by itself
#endif
        // T_normal_1C	- normal.gloss,		reversed
        // T_normal_2C	- 2*error.height,	non-reversed
        _RELEASE			(T_base);
        _RELEASE			(T_normal_1);
        ret_msize			= calc_texture_size(img_loaded_lod, mip_cnt, img_size);
        return				T_normal_1C;
    }
}