Changeset 2768


Ignore:
Timestamp:
Jun 22, 2013, 11:23:40 PM (8 years ago)
Author:
sam
Message:

gpu: framebuffers now use the g_render object to restore viewport data.

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gpu/framebuffer.cpp

    r2748 r2768  
    4242    friend class Framebuffer;
    4343
     44    ibox2 m_saved_viewport;
    4445    ivec2 m_size;
     46    bool m_bound;
    4547
    4648#if defined USE_D3D9
     
    289291{
    290292    m_data->m_size = size;
     293    m_data->m_bound = false;
    291294#if defined USE_D3D9
    292295    if (FAILED(g_d3ddevice->CreateTexture(size.x, size.y, 1,
     
    371374#   endif
    372375
    373     Unbind();
     376#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
     377    glBindFramebuffer(GL_FRAMEBUFFER, 0);
     378#   else
     379    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
     380#   endif
    374381#endif
    375382}
     
    413420void Framebuffer::Bind()
    414421{
     422    ASSERT(!m_data->m_bound, "trying to bind an already bound framebuffer");
     423
    415424#if defined USE_D3D9 || defined _XBOX
    416425    if (FAILED(g_d3ddevice->GetRenderTarget(0, &m_data->m_back_surface)))
     
    424433    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_data->m_fbo);
    425434#   endif
    426     //change viewport draw size
    427     Video::SetCustomSize(m_data->m_size);
    428 #endif
     435#endif
     436
     437    /* FIXME: this should be done in the RenderContext object
     438     * instead, maybe by getting rid of Framebuffer::Bind() and
     439     * creating RenderContext::SetFramebuffer() instead. */
     440    m_data->m_saved_viewport = g_renderer->GetViewport();
     441    g_renderer->SetViewport(ibox2(ivec2(0), m_data->m_size));
     442    m_data->m_bound = true;
    429443}
    430444
    431445void Framebuffer::Unbind()
    432446{
    433 #if defined USE_D3D9
    434     if (FAILED(g_d3ddevice->SetRenderTarget(0, m_data->m_back_surface)))
    435         Abort();
    436     m_data->m_back_surface->Release();
    437 #elif defined _XBOX
     447    ASSERT(m_data->m_bound, "trying to unbind an unbound framebuffer");
     448
     449#if defined USE_D3D9 || defined _XBOX
     450#   if defined _XBOX
    438451    if (FAILED(g_d3ddevice->Resolve(D3DRESOLVE_RENDERTARGET0, nullptr,
    439452                                    m_data->m_texture, nullptr, 0, 0, nullptr,
    440453                                    0, 0, nullptr)))
    441454        Abort();
     455#   endif
    442456    if (FAILED(g_d3ddevice->SetRenderTarget(0, m_data->m_back_surface)))
    443457        Abort();
    444458    m_data->m_back_surface->Release();
    445459#else
    446     //Restore viewport draw size
    447     Video::RestoreSize();
    448460#   if GL_VERSION_1_1 || GL_ES_VERSION_2_0
    449461    glBindFramebuffer(GL_FRAMEBUFFER, 0);
     
    452464#   endif
    453465#endif
     466
     467    g_renderer->SetViewport(m_data->m_saved_viewport);
     468    m_data->m_bound = false;
    454469}
    455470
  • trunk/src/gpu/rendercontext.cpp

    r2764 r2768  
    5555    Scene *m_scene;
    5656
     57    TrackedState<ibox2> m_viewport;
    5758    TrackedState<vec4> m_clear_color;
    58 
    5959    TrackedState<float> m_clear_depth;
    60 
    6160    TrackedState<AlphaFunc> m_alpha_func;
    6261    TrackedState<float> m_alpha_value;
    63 
    6462    TrackedState<BlendFunc> m_blend_src, m_blend_dst;
    65 
    6663    TrackedState<DepthFunc> m_depth_func;
    67 
    6864    TrackedState<CullMode> m_face_culling;
    6965};
     
    8177RenderContext::~RenderContext()
    8278{
     79    if (m_data->m_viewport.HasChanged())
     80        g_renderer->SetViewport(m_data->m_viewport.GetValue());
     81
    8382    if (m_data->m_clear_color.HasChanged())
    8483        g_renderer->SetClearColor(m_data->m_clear_color.GetValue());
     
    102101
    103102    delete m_data;
     103}
     104
     105void RenderContext::SetViewport(ibox2 viewport)
     106{
     107    if (!m_data->m_viewport.HasChanged())
     108        m_data->m_viewport.TrackValue(g_renderer->GetViewport());
     109
     110    g_renderer->SetViewport(viewport);
    104111}
    105112
  • trunk/src/gpu/renderer.cpp

    r2765 r2768  
    5454
    5555private:
     56    ibox2 m_viewport;
    5657    vec4 m_clear_color;
    5758    float m_clear_depth;
     
    7374 */
    7475
    75 Renderer::Renderer()
     76Renderer::Renderer(ivec2 size)
    7677  : m_data(new RendererData())
    7778{
     
    9293
    9394    /* Initialise rendering states */
     95    m_data->m_viewport = ibox2(0, 0, 0, 0);
     96    SetViewport(ibox2(vec2(0), size));
     97
    9498    m_data->m_clear_color = vec4(-1.f);
    9599    SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f));
     
    128132
    129133/*
     134 * Viewport dimensions
     135 */
     136
     137void Renderer::SetViewport(ibox2 viewport)
     138{
     139    if (m_data->m_viewport == viewport)
     140        return;
     141
     142#if defined USE_D3D9 || defined _XBOX
     143    D3DVIEWPORT9 vp = { viewport.A.x, viewport.A.y,
     144                        viewport.B.x, viewport.B.y,
     145                        0.0f, 1.0f };
     146    m_data->m_d3d_dev->SetViewport(&vp);
     147#else
     148    glViewport(viewport.A.x, viewport.A.y, viewport.B.x, viewport.B.y);
     149#endif
     150
     151    m_data->m_viewport = viewport;
     152}
     153
     154ibox2 Renderer::GetViewport() const
     155{
     156    return m_data->m_viewport;
     157}
     158
     159/*
    130160 * Clear color
    131161 */
     
    133163void Renderer::SetClearColor(vec4 color)
    134164{
     165    if (m_data->m_clear_color == color)
     166        return;
     167
    135168#if defined USE_D3D9 || defined _XBOX
    136169    /* Nothing to do */
     
    153186void Renderer::SetClearDepth(float depth)
    154187{
     188    if (m_data->m_clear_depth == depth)
     189        return;
     190
    155191#if defined USE_D3D9 || defined _XBOX
    156192    /* Nothing to do */
  • trunk/src/lol/gpu/rendercontext.h

    r2764 r2768  
    2828    ~RenderContext();
    2929
     30    void SetViewport(ibox2 viewport);
    3031    void SetClearColor(vec4 color);
    3132    void SetClearDepth(float depth);
  • trunk/src/lol/gpu/renderer.h

    r2764 r2768  
    116116    friend class Video;
    117117
    118     Renderer();
     118    Renderer(ivec2 size);
    119119    ~Renderer();
    120120
    121121public:
     122    void SetViewport(ibox2 viewport);
     123    ibox2 GetViewport() const;
     124
    122125    void SetClearColor(vec4 color);
    123126    vec4 GetClearColor() const;
  • trunk/src/video.cpp

    r2761 r2768  
    4949
    5050private:
    51     static ivec2 saved_viewport;
    5251    static DebugRenderMode render_mode;
    5352#if defined USE_D3D9 || defined _XBOX
     
    6261};
    6362
    64 ivec2 VideoData::saved_viewport(0, 0);
    6563DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;
    6664
     
    9694                                VideoMode.dwDisplayHeight);
    9795#   endif
    98     VideoData::saved_viewport = size;
    99 
    10096    D3DPRESENT_PARAMETERS d3dpp;
    10197    memset(&d3dpp, 0, sizeof(d3dpp));
     
    124120    g_d3ddevice = VideoData::d3d_dev;
    125121
    126     g_renderer = new Renderer();
     122    g_renderer = new Renderer(size);
    127123#else
    128124    /* Initialise OpenGL */
    129     g_renderer = new Renderer();
    130 
    131     glViewport(0, 0, size.x, size.y);
    132     VideoData::saved_viewport = size;
     125    g_renderer = new Renderer(size);
    133126#endif
    134127
    135128    /* Initialise reasonable scene default properties */
    136129    SetDebugRenderMode(DebugRenderMode::Default);
    137 }
    138 
    139 void Video::SetCustomSize(ivec2 size)
    140 {
    141     ivec4 current_size(0);
    142 #if defined USE_D3D9 || defined _XBOX
    143 #   define STR0(x) #x
    144 #   define STR(x) STR0(x)
    145 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetSize() not implemented")
    146 #elif defined __CELLOS_LV2__
    147     // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
    148 #else
    149     glGetIntegerv(GL_VIEWPORT, (GLint*)&current_size);
    150     if (current_size.zw != size)
    151         glViewport(0, 0, size.x, size.y);
    152 #endif
    153 }
    154 
    155 void Video::RestoreSize()
    156 {
    157     ivec4 current_size(0);
    158 #if defined USE_D3D9 || defined _XBOX
    159 #   define STR0(x) #x
    160 #   define STR(x) STR0(x)
    161 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetSize() not implemented")
    162 #elif defined __CELLOS_LV2__
    163     // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
    164 #else
    165     glGetIntegerv(GL_VIEWPORT, (GLint*)&current_size);
    166     if (current_size.zw != VideoData::saved_viewport)
    167         glViewport(0, 0, VideoData::saved_viewport.x, VideoData::saved_viewport.y);
    168 #endif
    169130}
    170131
     
    244205        Abort();
    245206#else
    246     /* FIXME: is this necessary here? */
    247     ivec2 size = GetSize();
    248     glViewport(0, 0, size.x, size.y);
    249 
    250207    GLbitfield mask = 0;
    251208    if (m & ClearMask::Color)
     
    303260ivec2 Video::GetSize()
    304261{
    305 #if defined USE_D3D9 || defined _XBOX
    306     return VideoData::saved_viewport;
    307 #elif 1
    308     /* GetSize() is called too often on the game thread; we cannot rely on
    309      * the GL context at this point */
    310     return VideoData::saved_viewport;
    311 #elif defined __CELLOS_LV2__
    312     // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
    313 #else
    314     GLint v[4];
    315     glGetIntegerv(GL_VIEWPORT, v);
    316     return ivec2(v[2], v[3]);
    317 #endif
     262    ibox2 viewport = g_renderer->GetViewport();
     263
     264    return viewport.B - viewport.A;
    318265}
    319266
  • trunk/src/video.h

    r2754 r2768  
    6969    static void Destroy();
    7070
    71     static void SetCustomSize(ivec2 size);
    72     static void RestoreSize();
    7371    static void SetDebugRenderMode(DebugRenderMode d);
    7472    static DebugRenderMode GetDebugRenderMode();
Note: See TracChangeset for help on using the changeset viewer.