3 * Copyright (c) 2007-2010 SlimDX Group
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "Direct3D9Exception.h"
30 #include "BaseTexture.h"
32 #include "VolumeTexture.h"
33 #include "CubeTexture.h"
35 using namespace System;
41 BaseTexture^ BaseTexture::FromUnmanaged( IDirect3DBaseTexture9 *texture )
46 switch( texture->GetType() )
48 case D3DRTYPE_TEXTURE:
49 return Texture::FromPointer( static_cast<IDirect3DTexture9*>( texture ) );
50 case D3DRTYPE_VOLUMETEXTURE:
51 return VolumeTexture::FromPointer( static_cast<IDirect3DVolumeTexture9*>( texture ) );
52 case D3DRTYPE_CUBETEXTURE:
53 return CubeTexture::FromPointer( static_cast<IDirect3DCubeTexture9*>( texture ) );
60 void BaseTexture::GenerateMipSublevels()
62 InternalPointer->GenerateMipSubLevels();
65 DataStream^ BaseTexture::ToStream( BaseTexture^ texture, ImageFileFormat format, array<PaletteEntry>^ palette )
67 ID3DXBuffer *buffer = NULL;
68 pin_ptr<PaletteEntry> pinnedPalette = palette == nullptr ? nullptr : &palette[0];
70 HRESULT hr = D3DXSaveTextureToFileInMemory( &buffer, static_cast<D3DXIMAGE_FILEFORMAT>( format ),
71 texture->InternalPointer, reinterpret_cast<const PALETTEENTRY*>( pinnedPalette ) );
73 if( RECORD_D3D9( hr ).IsFailure )
76 return gcnew DataStream( buffer );
79 DataStream^ BaseTexture::ToStream( BaseTexture^ texture, ImageFileFormat format )
81 ID3DXBuffer *buffer = NULL;
83 HRESULT hr = D3DXSaveTextureToFileInMemory( &buffer, static_cast<D3DXIMAGE_FILEFORMAT>( format ),
84 texture->InternalPointer, NULL );
86 if( RECORD_D3D9( hr ).IsFailure )
89 return gcnew DataStream( buffer );
92 Result BaseTexture::ToFile( BaseTexture^ texture, String^ fileName, ImageFileFormat format, array<PaletteEntry>^ palette )
94 pin_ptr<const wchar_t> pinnedName = PtrToStringChars(fileName);
95 pin_ptr<PaletteEntry> pinnedPalette = palette == nullptr ? nullptr : &palette[0];
97 HRESULT hr = D3DXSaveTextureToFile( pinnedName, static_cast<D3DXIMAGE_FILEFORMAT>( format ),
98 texture->InternalPointer, reinterpret_cast<const PALETTEENTRY*>( pinnedPalette ) );
99 return RECORD_D3D9( hr );
102 Result BaseTexture::ToFile( BaseTexture^ texture, String^ fileName, ImageFileFormat format )
104 pin_ptr<const wchar_t> pinnedName = PtrToStringChars(fileName);
106 HRESULT hr = D3DXSaveTextureToFile( pinnedName, static_cast<D3DXIMAGE_FILEFORMAT>( format ),
107 texture->InternalPointer, NULL );
108 return RECORD_D3D9( hr );
111 Result BaseTexture::FilterTexture( int sourceLevel, Filter filter, array<PaletteEntry>^ palette )
113 pin_ptr<PaletteEntry> pinnedPalette = palette == nullptr ? nullptr : &palette[0];
115 HRESULT hr = D3DXFilterTexture( InternalPointer, reinterpret_cast<const PALETTEENTRY*>( pinnedPalette ),
116 sourceLevel, static_cast<DWORD>( filter ) );
117 return RECORD_D3D9( hr );
120 Result BaseTexture::FilterTexture( int sourceLevel, Filter filter )
122 HRESULT hr = D3DXFilterTexture( InternalPointer, NULL, sourceLevel, static_cast<DWORD>( filter ) );
123 return RECORD_D3D9( hr );