Ticket #2513: LOSTexture.patch

File LOSTexture.patch, 3.1 KB (added by dan_trev, 10 years ago)

Patch to fix LOS segfault crash when added during game play.

  • source/graphics/LOSTexture.cpp

     
    6060CLOSTexture::CLOSTexture(CSimulation2& simulation) :
    6161    m_Simulation(simulation), m_Dirty(true), m_Texture(0), m_smoothFbo(0), m_MapSize(0), m_TextureSize(0), whichTex(true)
    6262{
     63    wasShaderInit = false;
    6364    if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS)
    6465    {
    65         m_smoothShader = g_Renderer.GetShaderManager().LoadEffect(str_los_interp);
    66         CShaderProgramPtr shader = m_smoothShader->GetShader();
    67 
    68         if (m_smoothShader && shader)
    69         {
    70             pglGenFramebuffersEXT(1, &m_smoothFbo);
    71         }
    72         else
    73         {
    74             LOGERROR(L"Failed to load SmoothLOS shader, disabling.");
    75             g_Renderer.m_Options.m_SmoothLOS = false;
    76         }
     66        CreateShader();
    7767    }
    7868}
    7969
     
    8373        DeleteTexture();
    8474}
    8575
     76// Create the LOS texture engine.Only run once.
     77void CLOSTexture::CreateShader()
     78{
     79    m_smoothShader = g_Renderer.GetShaderManager().LoadEffect(str_los_interp);
     80    CShaderProgramPtr shader = m_smoothShader->GetShader();
     81
     82    // Check if the shader was created correctly
     83    wasShaderInit = m_smoothShader && shader;
     84    if (wasShaderInit)
     85    {
     86        pglGenFramebuffersEXT(1, &m_smoothFbo);
     87    }
     88    else
     89    {
     90        LOGERROR(L"Failed to load SmoothLOS shader, disabling.");
     91        g_Renderer.m_Options.m_SmoothLOS = false;
     92    }
     93}
     94
    8695void CLOSTexture::DeleteTexture()
    8796{
    8897    glDeleteTextures(1, &m_Texture);
    89     if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS)
    90     {
     98
     99    if (m_TextureSmooth1)
    91100        glDeleteTextures(1, &m_TextureSmooth1);
     101
     102    if (m_TextureSmooth2)
    92103        glDeleteTextures(1, &m_TextureSmooth2);
    93     }
     104
    94105    m_Texture = 0;
     106    m_TextureSmooth1 = 0;
     107    m_TextureSmooth2 = 0;
    95108}
    96109
    97110void CLOSTexture::MakeDirty()
     
    120133
    121134void CLOSTexture::InterpolateLOS()
    122135{
     136    // Is the Renderer on and Smooth LOS active?
    123137    if (CRenderer::IsInitialised() && !g_Renderer.m_Options.m_SmoothLOS)
    124138        return;
    125    
     139
     140    if (!wasShaderInit)
     141    {
     142        CreateShader();
     143
     144        // RecomputeTexture(0) will not cause the ConstructTexture to run.
     145        // Force the textures to be created.
     146        DeleteTexture();
     147        ConstructTexture(0);
     148        m_Dirty = true;
     149    }
     150
    126151    if (m_Dirty)
    127152    {
    128153        RecomputeTexture(0);
    129154        m_Dirty = false;
    130155    }
    131    
     156
     157    // Get the current return format for simple GL states
    132158    GLint originalFBO;
    133159    glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &originalFBO);
    134160   
     161    // Bind the LOS framebuffer to GL_FRAMEBUFFER_EXT
    135162    pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_smoothFbo);
     163
    136164    pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,
    137165                   whichTex ? m_TextureSmooth2 : m_TextureSmooth1, 0);
    138166   
  • source/graphics/LOSTexture.h

     
    7878
    7979private:
    8080    void DeleteTexture();
     81    void CreateShader();
    8182    void ConstructTexture(int unit);
    8283    void RecomputeTexture(int unit);
    8384
     
    8889
    8990    bool m_Dirty;
    9091
     92    bool wasShaderInit;
     93
    9194    GLuint m_Texture;
    9295    GLuint m_TextureSmooth1, m_TextureSmooth2;
    9396