Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Window from handle and shaders not available.  (Read 2762 times)

0 Members and 1 Guest are viewing this topic.

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Window from handle and shaders not available.
« on: August 18, 2014, 03:35:34 pm »
Hi :) i have two versions of my framework - one for games and one for IDE scene editor, but whole code is the same with only one difference on creating sfml window. Version for games create window in default way, but version for IDE create window from handle (.NET control). Now: when i try to load shader from game code it works, but when i try to load shader from editor code it fails on line that tells me that shaders are not available :<
Here is some code that will tell you more about how i'm doing this:

Creating in-game window:
sf::RenderWindow* window = xnew sf::RenderWindow(
    sf::VideoMode( WINDOW_WIDTH, WINDOW_HEIGHT ),
    APP_NAME,
    sf::Style::Titlebar | sf::Style::Close
);

Creating editor window:
m_renderWindow = xnew sf::RenderWindow( (sf::WindowHandle)windowHandle );

Loading shaders:
sf::Shader* Assets::loadShader( const std::string& id, const std::string& vspath, const std::string& fspath, const std::string* uniforms, unsigned int uniformsCount )
{
        sf::Shader* t = getShader( id );
        if( !t )
        {
                if( !sf::Shader::isAvailable() )
                        return 0;
                t = xnew sf::Shader();
                if( m_fileSystemRoot.empty() )
                {
                        if( !t->loadFromFile( vspath, fspath ) )
                        {
                                DELETE_OBJECT( t );
                                return 0;
                        }
                }
                else
                {
                        std::stringstream ssv;
                        ssv << m_fileSystemRoot.c_str() << vspath;
                        std::stringstream ssf;
                        ssf << m_fileSystemRoot.c_str() << fspath;
                        if( !t->loadFromFile( ssv.str(), ssf.str() ) )
                        {
                                DELETE_OBJECT( t );
                                return 0;
                        }
                }
                m_shaders[ id ] = t;
                m_metaShaders[ id ] = vspath + "|" + fspath;
                m_uniformsShaders[ id ].clear();
                for( unsigned int i = 0; i < uniformsCount; i++ )
                        m_uniformsShaders[ id ].push_back( uniforms[ i ] );
                if( m_assetsChangedListener )
                        m_assetsChangedListener->onShaderChanged( id, t, true );
        }
        return t;
}

Does someone know why shaders are not available with window from handle, but they works with default window? I had the same problem when i used official SFML.NET assembly and tried to use shaders with window from handle.

Cheers! :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Window from handle and shaders not available.
« Reply #1 on: August 18, 2014, 03:43:46 pm »
Can you be more specific with "not available". What error messages do you get?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Window from handle and shaders not available.
« Reply #2 on: August 18, 2014, 03:48:51 pm »
yes :)
Quote
but when i try to load shader from editor code it fails on line that tells me that shaders are not available :<
means
if( !sf::Shader::isAvailable() )
    return 0;

and there is no error messages since .NET does not display errors from C std console.
« Last Edit: August 18, 2014, 03:51:04 pm by PsichiX »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Window from handle and shaders not available.
« Reply #3 on: August 18, 2014, 04:19:08 pm »
Is the window created before the call to Shader::isAvailable()?
Laurent Gomila - SFML developer

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Window from handle and shaders not available.
« Reply #4 on: August 18, 2014, 04:31:35 pm »
yes - window is created within .NET control Load event, then shaders are loaded when scene is opened in editor.

.NET class constructor:
public RendererSurfaceControl()
{
        Load += new EventHandler(RendererSurfaceControl_Load);
}

.NET Load event handle:
private void RendererSurfaceControl_Load(object sender, EventArgs e)
{
        PtakopyskInterface.Instance.Initialize(Handle.ToInt32());
        PtakopyskInterface.Instance.SetVerticalSyncEnabled(false);
}

.NET PtakopyskInterface.Initialize()
public bool Initialize(int windowHandle, bool editMode = true)
{
        try
        {
                bool status = _Initialize(windowHandle, editMode);
                Console.Write(_PopErrors());
                return status;
        }
        catch (Exception ex) { LogException(ex); return false; }
}

.NET native dll import
[DllImport(DLL, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.U1)]
private static extern bool _Initialize(int windowHandle, bool editMode);

Native bridge initialization:
bool DLL_EXPORT _Initialize( int windowHandle, bool editMode )
{
    return PtakopyskInterface::use().initialize( windowHandle, editMode );
}

Native implementation initialization:
bool PtakopyskInterface::initialize( int windowHandle, bool editMode )
{
    if( !windowHandle )
    {
        m_errors << "Cannot initialize PtakopyskInterface: windowHandle cannot be null!\n";
        return false;
    }

    release();

    LOG_SETUP( "PtakopyskInterface.log" );
    GameManager::initialize();
    GameManager::setEditMode( editMode );
    m_renderWindow = xnew sf::RenderWindow( (sf::WindowHandle)windowHandle );
    m_gameManager = xnew GameManager();
    m_gameManager->RenderWindow = m_renderWindow;

    return true;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Window from handle and shaders not available.
« Reply #5 on: August 18, 2014, 04:35:31 pm »
And are you sure that the window creation succeeds when you pass the handle this way? Is Shader::isAvailable() the only thing that fails?

By the way, you should not use int for the window handle, use at least unsigned long to be safe.
Laurent Gomila - SFML developer

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Window from handle and shaders not available.
« Reply #6 on: August 18, 2014, 04:48:58 pm »
I have to ask, what is this 'xnew' keyword that you are using to construct the SFML window?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Window from handle and shaders not available.
« Reply #7 on: August 18, 2014, 04:52:27 pm »
i'm sure about window successful creation because rendering is working, textures are displayed, window has opened status, everything works but only shaders are not loaded because of sf::Shader::isAvailable() returns false. i've even added debug logs to file to get to know on what line exactly function fails and it was on sf::Shader::isAvailable() definitely.
Is there a way to get sfml logs string and put it into my logger? that should help me to get information what behind the scenes is happening.
btw. i will change int to long for window handle anyway, thanks for this info :)

@zsbzsb: it's a macro to enable memory tracing in my engine, but here is disabled so it's replaced by 'new' during compilation process.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Window from handle and shaders not available.
« Reply #8 on: August 18, 2014, 05:02:36 pm »
sf::err() is a standard std::ostream, so you can redirect it like any other stream (see its rdbuf function, or Google "redirect ostream"). I don't know what your logging system uses so I can't tell you exactly how to plug sf::err() to it.
Laurent Gomila - SFML developer

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Window from handle and shaders not available.
« Reply #9 on: August 18, 2014, 05:04:35 pm »
rdbuf was exactly what i needed! :) so i'm back to testing.

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Window from handle and shaders not available.
« Reply #10 on: August 18, 2014, 07:14:41 pm »
ok, i'va changed int to long in code that creates window and somehow shaders are now available :0 but now after loading shaders happens something weird - scene looks like default blend mode is set to none, so there is no more transparency, but i think i can fix that - thanks for solution!

edit: nope, i've added some line of code related to assets loading (just a setter of variable, nothing that touch shaders itself) before window creaton so problem is still not solved :>
« Last Edit: August 18, 2014, 07:42:52 pm by PsichiX »