edgar-mtz-e / slimdx

Automatically exported from code.google.com/p/slimdx
0 stars 0 forks source link

Texture FromStream (possible 15% performance gain) #397

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi

I'm pushing slimdx to the limits of my cpu(s).  I'm rendering 4 live 
streams to textures all around 3d thingies.  I'm noticing a huge cpu 
bottleneck.  This is prefectly normal since I receive these streams as RGB 
array of bytes and I need to get them inside video memory before I can use 
them as skins.

Anyway, every saved tick is usefull for me.  Therefor I would propose the 
following optimization.  In a tight creation / dispose loop is brings down 
the creation/disposing of a thousand textures down from 7 to 5.8 - 6 
seconds.  1/7 is a lot.  Or You could even leave your original code 
unharmed and just add FromDataStream method.

ID3D10Resource* Texture::ConstructFromDataStream( 
SlimDX::Direct3D10::Device^ device, DataStream^ stream, int sizeInBytes, 
D3DX10_IMAGE_LOAD_INFO* info ){
    long pos = stream->Position;
    if ((pos + sizeInBytes) > stream->Length) {
        throw gcnew InvalidOperationException( "End of the stream 
reached, cannot create the texture." );
    }
    ID3D10Resource* resource = 0;
    HRESULT hr = D3DX10CreateTextureFromMemory( device-
>InternalPointer, stream->RawPointer + pos, sizeInBytes, info, 0, 
&resource, 0 ); 
    RECORD_D3D10( hr );
    return resource;
}

ID3D10Resource* Texture::ConstructFromStream( SlimDX::Direct3D10::Device^ 
device, Stream^ stream, int sizeInBytes, D3DX10_IMAGE_LOAD_INFO* info )
{
    if( stream->GetType() == DataStream::typeid ){
        return ConstructFromDataStream(device, 
safe_cast<DataStream^>( stream ), sizeInBytes, info);
    } 
    array<Byte>^ memory = SlimDX::Utilities::ReadStream( stream, 
sizeInBytes );
    return ConstructFromMemory( device, memory, info );
}

kind regards

Alexander

Original issue reported on code.google.com by amuylaer...@gmail.com on 11 Jan 2009 at 2:06

GoogleCodeExporter commented 9 years ago
It also happens a lot that you (or maybe just me) are passing a memorystream 
containing a single resource.  

ID3D10Resource* Texture::ConstructFromStream( SlimDX::Direct3D10::Device^ 
device, 
Stream^ stream, int sizeInBytes, D3DX10_IMAGE_LOAD_INFO* info )
{
    system::Type^ t = stream->GetType();
    if( t == DataStream::typeid ){
        return ConstructFromDataStream(device, safe_cast<DataStream^>( 
stream ), sizeInBytes, info);
    } else if ((t == System::IO::MemoryStream::typeid) && (stream->Position == 
0)) {
        return ConstructFromMemory(device, 
safe_cast<System::IO::MemoryStream^>(stream)->GetBuffer(), info);
    }
    array<Byte>^ memory = SlimDX::Utilities::ReadStream( stream, sizeInBytes );
    return ConstructFromMemory( device, memory, info );
}

Kind regards

Alexander

Original comment by amuylaer...@gmail.com on 11 Jan 2009 at 2:54

GoogleCodeExporter commented 9 years ago
Similar optimizations have been done in places, but it's not something that has 
been 
properly added across SlimDX. I'd like to make it a priority to do so. Accepted 
and 
priority boosted.

Original comment by promit....@gmail.com on 11 Jan 2009 at 10:30

GoogleCodeExporter commented 9 years ago
Fixed as of r867.

Original comment by promit....@gmail.com on 18 Jan 2009 at 2:48