Esempio n. 1
0
RenderShader* RBShaderLoader::ParseFile( const tchar* fname, ShaderManager* db )
{
    Asset::ShaderAssetPtr shaderClass = Asset::AssetClass::LoadAssetClass< Asset::ShaderAsset >( fname );
    if ( !shaderClass.ReferencesObject() )
    {
        Log::Warning( TXT( "Could not load shader '%s'.\n" ), fname );
        return NULL;
    }

    // this seems like a valid shader, allocate a shader
    RenderShader* sh = new RenderShader(db,fname);

    tstring texturePath;
    bool hadError = false;

    SetShaderClassAlpha( sh, shaderClass->m_AlphaMode );

    if (shaderClass->m_DoubleSided)
        sh->m_flags|=SHDR_FLAG_TWO_SIDED;

    TextureSettings settings;
    settings.Clear();
    settings.m_WrapU = TextureAddressModes( shaderClass->m_WrapModeU );
    settings.m_WrapV = TextureAddressModes( shaderClass->m_WrapModeV );

    Asset::TexturePtr textureClass = Asset::AssetClass::LoadAssetClass< Asset::Texture >( shaderClass->m_ColorMapPath );
    if( textureClass.ReferencesObject() )
    {
        settings.m_Path = textureClass->GetPath().Get();
        settings.m_Anisotropy = 0;
        settings.m_Filter = TextureFilterMode( textureClass->GetFilter() );
        settings.m_Format = GetD3DColorFormat( textureClass->GetFormat() );
        settings.m_MipBias = 0.0f;
    }
    else
    {
        settings.m_Format = D3DFMT_UNKNOWN;
        settings.m_Path = TXT( "@@base" );
    }

    UpdateShader(sh, shaderClass);

    if( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_BASE_MAP ) )
    {
        if ( settings.m_Path != TXT( "@@base" ) )
        {
            Log::Warning( TXT( "Could not load base map '%s', loading default.\n" ), settings.m_Path.c_str() );
            settings.Clear();
            settings.m_Format = D3DFMT_UNKNOWN;
            settings.m_Path = TXT( "@@base" );

            if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_BASE_MAP ) )
            {
                Log::Error( TXT( "Could not load default base map.\n" ) );
                hadError = true;
            }
        }
    }

    settings.Clear();
    settings.m_WrapU = TextureAddressModes( shaderClass->m_WrapModeU );
    settings.m_WrapV = TextureAddressModes( shaderClass->m_WrapModeV );

    if ( shaderClass->m_NormalMapScaling > 0.0f )
    {
        Asset::TexturePtr normalMap = Asset::AssetClass::LoadAssetClass< Asset::Texture >( shaderClass->m_NormalMapPath );

        if ( normalMap.ReferencesObject() )
        {
            settings.m_Path = normalMap->GetPath().Get();
            settings.m_Anisotropy = 0;
            settings.m_Filter = TextureFilterMode( normalMap->GetFilter() );
            settings.m_Format = GetD3DColorFormat( normalMap->GetFormat() );
            settings.m_MipBias = 0.0f;
        }
        else
        {
            settings.m_Format = D3DFMT_UNKNOWN;
            settings.m_Path = TXT( "@@normal" );
        }
    }

    UpdateShader(sh, shaderClass);

    if( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_NORMAL_MAP ) )
    {
        if ( settings.m_Path != TXT( "@@normal" ) )
        {
            Log::Warning( TXT( "Could not load normal map '%s', loading default.\n" ), settings.m_Path.c_str() );
            settings.Clear();
            settings.m_Format = D3DFMT_UNKNOWN;
            settings.m_Path = TXT( "@@normal" );

            if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_NORMAL_MAP ) )
            {
                Log::Error( TXT( "Could not load default normal map.\n" ) );
                hadError = true;
            }
        }
    }

    settings.Clear();
    settings.m_WrapU = TextureAddressModes( shaderClass->m_WrapModeU );
    settings.m_WrapV = TextureAddressModes( shaderClass->m_WrapModeV );

    Asset::TexturePtr gpiMap = Asset::AssetClass::LoadAssetClass< Asset::Texture >( shaderClass->m_GPIMapPath );
    if( gpiMap.ReferencesObject() )
    {
        settings.m_Path = gpiMap->GetPath().Get();
        settings.m_Anisotropy = 0;
        settings.m_Filter = TextureFilterMode( gpiMap->GetFilter() );
        settings.m_Format = GetD3DColorFormat( gpiMap->GetFormat() );
        settings.m_MipBias = 0.0f;

        if( db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_GPI_MAP ) )
        {
            sh->m_textures[ Texture::SAMPLER_INCAN_MAP ] = 0xffffffff;
            sh->m_textures[ Texture::SAMPLER_PARALLAX_MAP ] = 0xffffffff;

            sh->m_flags |= SHDR_FLAG_GPI_MAP;

            UpdateShader( sh, shaderClass );
        }
        else
        {
            UpdateShader( sh, 0 );
            Log::Warning( TXT( "Could not load expensive map '%s', loading defaults.\n" ), settings.m_Path.c_str() );
            settings.Clear();
            settings.m_Format = D3DFMT_UNKNOWN;

            settings.m_Path = TXT( "@@gloss" );
            if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_GLOSS_MAP ) )
            {
                Log::Error( TXT( "Could not load default gloss map.\n" ) );
                hadError = true;
            }
            settings.m_Path = TXT( "@@parallax" );
            if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_PARALLAX_MAP ) )
            {
                Log::Error( TXT( "Could not load default parallax map.\n" ) );
                hadError = true;
            }
            settings.m_Path = TXT( "@@incan" );
            if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_INCAN_MAP ) )
            {
                Log::Error( TXT( "Could not load default incan map.\n" ) );
                hadError = true;
            }
        }
    }
    else
    {
        UpdateShader( sh, 0 );

        settings.Clear();
        settings.m_Format = D3DFMT_UNKNOWN;

        settings.m_Path = TXT( "@@gloss" );
        if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_GLOSS_MAP ) )
        {
            Log::Error( TXT( "Could not load default gloss map.\n" ) );
            hadError = true;
        }
        settings.m_Path = TXT( "@@parallax" );
        if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_PARALLAX_MAP ) )
        {
            Log::Error( TXT( "Could not load default parallax map.\n" ) );
            hadError = true;
        }
        settings.m_Path = TXT( "@@incan" );
        if ( !db->LoadTextureWithSettings( settings, sh, Texture::SAMPLER_INCAN_MAP ) )
        {
            Log::Error( TXT( "Could not load default incan map.\n" ) );
            hadError = true;
        }
    }

    if ( hadError )
    {
        delete sh;
        return NULL;
    }

    return sh;
}
Esempio n. 2
0
void* ThumbnailLoader::LoadThread::Entry()
{
#ifdef HELIUM_ASSERT_ENABLED
    bool emptyQueuePollingCheck = false;
#endif

    while ( true )
    {
        m_Loader.m_Signal.Decrement();

        if ( m_Loader.m_Quit )
        {
            break;
        }

        // the device is gone, our glorious benefactor is probably cleaning up
        IDirect3DDevice9* device = m_Loader.m_DeviceManager->GetD3DDevice();
        if ( !device )
        {
            // You should stop this thread before letting go of the window that
            // owns the device.
            HELIUM_BREAK();
            break;
        }

        // while the device is lost, just wait for it to come back
        while ( device->TestCooperativeLevel() != D3D_OK )
        {
            if ( m_Loader.m_Quit )
            {
                break;
            }

            wxThread::Sleep( 100 );
            continue;
        }

        if ( m_Loader.m_Quit )
        {
            break;
        }

        Helium::Path path;

        {
            Helium::Locker< Helium::OrderedSet< Helium::Path > >::Handle queue( m_Loader.m_FileQueue );
            if ( !queue->Empty() )
            {
#ifdef HELIUM_ASSERT_ENABLED
                emptyQueuePollingCheck = false;
#endif
                path = queue->Front();
            }
            else
            {
                // if you hit this then the bookkeeping of the counting semaphore is broken
                HELIUM_ASSERT( !emptyQueuePollingCheck );

#ifdef HELIUM_ASSERT_ENABLED
                emptyQueuePollingCheck = true;
#endif
                continue;
            }

            queue->Remove( path );
        }

        ResultArgs args;
        args.m_Path = path;
        args.m_Cancelled = false;

        if ( SceneGraph::IsSupportedTexture( path.Get() ) )
        {
            IDirect3DTexture9* texture = NULL;
            if ( texture = LoadTexture( device, path.Get() ) )
            {
                ThumbnailPtr thumbnail = new Thumbnail( m_Loader.m_DeviceManager, texture );
                args.m_Textures.push_back( thumbnail );
            }
        }
        else
        {
#pragma TODO( "When we store the thumbnail in the asset file, fix this." )

            if ( path.Extension() == TXT( "HeliumEntity" ) )
            {
                Path thumbnailPath( path.Directory() + path.Basename() + TXT( "_thumbnail.png" ) );

                if ( thumbnailPath.Exists() )
                {
                    IDirect3DTexture9* texture = NULL;
                    if ( texture = LoadTexture( device, thumbnailPath.Get() ) )
                    {
                        ThumbnailPtr thumbnail = new Thumbnail( m_Loader.m_DeviceManager, texture );
                        args.m_Textures.push_back( thumbnail );
                    }
                }
            }
            // Include the color map of a shader as a possible thumbnail image
            else if ( path.Extension() == TXT( "HeliumShader" ) )
            {
                Asset::ShaderAssetPtr shader = NULL;
                try
                {
                    shader = Asset::AssetClass::LoadAssetClass< Asset::ShaderAsset >( path );
                }
                catch( const Exception& )
                {
                    shader = NULL;
                }

                if ( shader )
                {
                    Asset::TexturePtr colorMap = Asset::AssetClass::LoadAssetClass< Asset::Texture >( shader->m_ColorMapPath );
                    if ( colorMap.ReferencesObject() )
                    {
                        if ( colorMap->GetContentPath().Exists() && SceneGraph::IsSupportedTexture( colorMap->GetContentPath().Get() ) )
                        {
                            IDirect3DTexture9* texture = NULL;
                            if ( texture = LoadTexture( device, colorMap->GetContentPath().Get() ) )
                            {
                                ThumbnailPtr thumbnail = new Thumbnail( m_Loader.m_DeviceManager, texture );
                                args.m_Textures.push_back( thumbnail );
                            }
                        }
                    }
                }
            }
            else if ( path.Extension() == TXT( "HeliumTexture" ) )
            {
                Asset::TexturePtr textureAsset = NULL;
                
                try
                {
                    textureAsset = Asset::AssetClass::LoadAssetClass< Asset::Texture >( path );
                }
                catch( const Exception& )
                {
                    textureAsset = NULL;
                }

                if ( textureAsset.ReferencesObject() )
                {
                    if ( textureAsset->GetContentPath().Exists() && SceneGraph::IsSupportedTexture( textureAsset->GetContentPath().Get() ) )
                    {
                        IDirect3DTexture9* texture = NULL;
                        if ( texture = LoadTexture( device, textureAsset->GetContentPath().Get() ) )
                        {
                            ThumbnailPtr thumbnail = new Thumbnail( m_Loader.m_DeviceManager, texture );
                            args.m_Textures.push_back( thumbnail );
                        }
                    }
                }
            }
        }

        m_Loader.m_Result.Raise( args );
    }

    return NULL;
}