Changeset 2760


Ignore:
Timestamp:
Jun 22, 2013, 4:10:06 PM (8 years ago)
Author:
sam
Message:

gpu: implement blend function and alpha test in the renderer.

Location:
trunk
Files:
7 edited

Legend:

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

    r2753 r2760  
    5858    TrackedState<float> m_clear_depth;
    5959    TrackedState<bool> m_alpha_blend;
     60    TrackedState<BlendFactor> m_blend_src;
     61    TrackedState<BlendFactor> m_blend_dst;
     62    TrackedState<bool> m_alpha_test;
    6063    TrackedState<bool> m_depth_test;
    6164    TrackedState<bool> m_face_culling;
     
    7477RenderContext::~RenderContext()
    7578{
     79    if (m_data->m_clear_color.HasChanged())
     80        g_renderer->SetClearColor(m_data->m_clear_color.GetValue());
     81
     82    if (m_data->m_clear_depth.HasChanged())
     83        g_renderer->SetClearDepth(m_data->m_clear_depth.GetValue());
     84
    7685    if (m_data->m_alpha_blend.HasChanged())
    7786        g_renderer->SetAlphaBlend(m_data->m_alpha_blend.GetValue());
     87
     88    if (m_data->m_blend_src.HasChanged())
     89        g_renderer->SetBlendFunc(m_data->m_blend_src.GetValue(),
     90                                 m_data->m_blend_dst.GetValue());
     91
     92    if (m_data->m_alpha_test.HasChanged())
     93        g_renderer->SetAlphaTest(m_data->m_alpha_test.GetValue());
    7894
    7995    if (m_data->m_depth_test.HasChanged())
     
    110126}
    111127
     128void RenderContext::SetBlendFunc(BlendFactor src, BlendFactor dst)
     129{
     130    if (!m_data->m_blend_src.HasChanged())
     131        m_data->m_blend_src.TrackValue(g_renderer->GetBlendFuncSrc());
     132    if (!m_data->m_blend_dst.HasChanged())
     133        m_data->m_blend_dst.TrackValue(g_renderer->GetBlendFuncDst());
     134
     135    g_renderer->SetBlendFunc(src, dst);
     136}
     137
     138void RenderContext::SetAlphaTest(bool set)
     139{
     140    if (!m_data->m_alpha_test.HasChanged())
     141        m_data->m_alpha_test.TrackValue(g_renderer->GetAlphaTest());
     142
     143    g_renderer->SetAlphaTest(set);
     144}
     145
    112146void RenderContext::SetDepthTest(bool set)
    113147{
  • trunk/src/gpu/renderer.cpp

    r2754 r2760  
    5050    vec4 m_clear_color;
    5151    float m_clear_depth;
    52     bool m_blend, m_depth_test, m_face_culling;
     52    BlendFactor m_blend_src, m_blend_dst;
     53    bool m_alpha_blend, m_alpha_test, m_depth_test, m_face_culling;
    5354};
    5455
     
    8182    SetClearDepth(1.f);
    8283
    83     m_data->m_blend = false;
     84    m_data->m_alpha_blend = false;
    8485    SetAlphaBlend(true);
     86
     87    m_data->m_alpha_test = true;
     88    SetAlphaTest(false);
     89
     90    m_data->m_blend_src = BlendFactor::Zero;
     91    m_data->m_blend_dst = BlendFactor::Zero;
     92    SetBlendFunc(BlendFactor::SrcAlpha, BlendFactor::OneMinusSrcAlpha);
    8593
    8694    m_data->m_depth_test = false;
     
    153161void Renderer::SetAlphaBlend(bool set)
    154162{
    155     if (m_data->m_blend == set)
    156         return;
    157 
    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: Renderer::SetAlphaBlend() not implemented")
     163    if (m_data->m_alpha_blend == set)
     164        return;
     165
     166#if defined USE_D3D9 || defined _XBOX
     167    m_data->m_d3d_dev->SetRenderState(D3DRS_ALPHABLENDENABLE, set ? 1 : 0);
    162168#else
    163169    if (set)
     
    167173#endif
    168174
    169     m_data->m_blend = set;
     175    m_data->m_alpha_blend = set;
    170176}
    171177
    172178bool Renderer::GetAlphaBlend() const
    173179{
    174     return m_data->m_blend;
     180    return m_data->m_alpha_blend;
     181}
     182
     183/*
     184 * Blend function
     185 */
     186
     187void Renderer::SetBlendFunc(BlendFactor src, BlendFactor dst)
     188{
     189    if (m_data->m_blend_src == src && m_data->m_blend_dst == dst)
     190        return;
     191
     192#if defined USE_D3D9 || defined _XBOX
     193    enum D3DBLEND s1[2] = { D3DBLEND_ONE, D3DBLEND_ZERO };
     194    BlendFactor s2[2] = { src, dst };
     195
     196    for (int i = 0; i < 2; ++i)
     197    {
     198        switch (s2[i])
     199        {
     200            case BlendFactor::Zero:
     201                s1[i] = D3DBLEND_ZERO; break;
     202            case BlendFactor::One:
     203                s1[i] = D3DBLEND_ONE; break;
     204            case BlendFactor::SrcColor:
     205                s1[i] = D3DBLEND_SRCCOLOR; break;
     206            case BlendFactor::OneMinusSrcColor:
     207                s1[i] = D3DBLEND_INVSRCCOLOR; break;
     208            case BlendFactor::DstColor:
     209                s1[i] = D3DBLEND_DESTCOLOR; break;
     210            case BlendFactor::OneMinusDstColor:
     211                s1[i] = D3DBLEND_INVDESTCOLOR; break;
     212            case BlendFactor::SrcAlpha:
     213                s1[i] = D3DBLEND_SRCALPHA; break;
     214            case BlendFactor::OneMinusSrcAlpha:
     215                s1[i] = D3DBLEND_INVSRCALPHA; break;
     216            case BlendFactor::DstAlpha:
     217                s1[i] = D3DBLEND_DESTALPHA; break;
     218            case BlendFactor::OneMinusDstAlpha:
     219                s1[i] = D3DBLEND_INVDESTALPHA; break;
     220            /* FiXME: these can be supported using D3DPBLENDCAPS_BLENDFACTOR */
     221            case BlendFactor::ConstantColor:
     222                assert(0, "BlendFactor::ConstantColor not supported");
     223                break;
     224            case BlendFactor::OneMinusConstantColor:
     225                assert(0, "BlendFactor::OneMinusConstantColor not supported");
     226                break;
     227            case BlendFactor::ConstantAlpha:
     228                assert(0, "BlendFactor::ConstantAlpha not supported");
     229                break;
     230            case BlendFactor::OneMinusConstantAlpha:
     231                assert(0, "BlendFactor::OneMinusConstantAlpha not supported");
     232                break;
     233        }
     234    }
     235
     236    m_data->m_d3d_dev->SetRenderState(D3DRS_SRCBLEND, s1[0]);
     237    m_data->m_d3d_dev->SetRenderState(D3DRS_DESTBLEND, s1[1]);
     238#else
     239    GLenum s1[2] = { GL_ONE, GL_ZERO };
     240    BlendFactor s2[2] = { src, dst };
     241
     242    for (int i = 0; i < 2; ++i)
     243    {
     244        switch (s2[i])
     245        {
     246            case BlendFactor::Zero:
     247                s1[i] = GL_ZERO; break;
     248            case BlendFactor::One:
     249                s1[i] = GL_ONE; break;
     250            case BlendFactor::SrcColor:
     251                s1[i] = GL_SRC_COLOR; break;
     252            case BlendFactor::OneMinusSrcColor:
     253                s1[i] = GL_ONE_MINUS_SRC_COLOR; break;
     254            case BlendFactor::DstColor:
     255                s1[i] = GL_DST_COLOR; break;
     256            case BlendFactor::OneMinusDstColor:
     257                s1[i] = GL_ONE_MINUS_DST_COLOR; break;
     258            case BlendFactor::SrcAlpha:
     259                s1[i] = GL_SRC_ALPHA; break;
     260            case BlendFactor::OneMinusSrcAlpha:
     261                s1[i] = GL_ONE_MINUS_SRC_ALPHA; break;
     262            case BlendFactor::DstAlpha:
     263                s1[i] = GL_DST_ALPHA; break;
     264            case BlendFactor::OneMinusDstAlpha:
     265                s1[i] = GL_ONE_MINUS_DST_ALPHA; break;
     266            case BlendFactor::ConstantColor:
     267                s1[i] = GL_CONSTANT_COLOR; break;
     268            case BlendFactor::OneMinusConstantColor:
     269                s1[i] = GL_ONE_MINUS_CONSTANT_COLOR; break;
     270            case BlendFactor::ConstantAlpha:
     271                s1[i] = GL_CONSTANT_ALPHA; break;
     272            case BlendFactor::OneMinusConstantAlpha:
     273                s1[i] = GL_ONE_MINUS_CONSTANT_ALPHA; break;
     274        }
     275    }
     276
     277    glBlendFunc(s1[0], s1[1]);
     278#endif
     279
     280    m_data->m_blend_src = src;
     281    m_data->m_blend_dst = dst;
     282}
     283
     284BlendFactor Renderer::GetBlendFuncSrc() const
     285{
     286    return m_data->m_blend_src;
     287}
     288
     289BlendFactor Renderer::GetBlendFuncDst() const
     290{
     291    return m_data->m_blend_dst;
     292}
     293
     294/*
     295 * Alpha testing
     296 */
     297
     298void Renderer::SetAlphaTest(bool set)
     299{
     300    if (m_data->m_alpha_test == set)
     301        return;
     302
     303#if defined USE_D3D9 || defined _XBOX
     304    m_data->m_d3d_dev->SetRenderState(D3DRS_ALPHATESTENABLE, set ? 1 : 0);
     305#else
     306    if (set)
     307        glEnable(GL_ALPHA_TEST);
     308    else
     309        glDisable(GL_ALPHA_TEST);
     310#endif
     311
     312    m_data->m_alpha_test = set;
     313}
     314
     315bool Renderer::GetAlphaTest() const
     316{
     317    return m_data->m_alpha_test;
    175318}
    176319
  • trunk/src/gpu/vertexbuffer.cpp

    r2731 r2760  
    128128
    129129#if defined _XBOX || defined USE_D3D9
    130     g_d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
    131     g_d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    132     g_d3ddevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    133130    if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
    134131        Abort();
     
    164161    /* FIXME: this has nothing to do here! */
    165162    glFrontFace(GL_CCW);
    166     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    167163
    168164    switch (type)
     
    195191
    196192#if defined _XBOX || defined USE_D3D9
    197     g_d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
    198     g_d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    199     g_d3ddevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    200193    if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
    201194        Abort();
     
    234227    /* FIXME: this has nothing to do here! */
    235228    glFrontFace(GL_CCW);
    236     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    237229
    238230    switch (type)
  • trunk/src/lol/gpu/rendercontext.h

    r2753 r2760  
    3131    void SetClearDepth(float depth);
    3232    void SetAlphaBlend(bool set);
     33    void SetBlendFunc(BlendFactor src, BlendFactor dst);
     34    void SetAlphaTest(bool set);
    3335    void SetDepthTest(bool set);
    3436    void SetFaceCulling(bool set);
  • trunk/src/lol/gpu/renderer.h

    r2757 r2760  
    2222class RendererData;
    2323
     24/* A safe enum to indicate the blending factors. */
     25struct BlendFactor
     26{
     27    enum Value
     28    {
     29        Zero,
     30        One,
     31        SrcColor,
     32        OneMinusSrcColor,
     33        DstColor,
     34        OneMinusDstColor,
     35        SrcAlpha,
     36        OneMinusSrcAlpha,
     37        DstAlpha,
     38        OneMinusDstAlpha,
     39        ConstantColor,
     40        OneMinusConstantColor,
     41        ConstantAlpha,
     42        OneMinusConstantAlpha,
     43    }
     44    m_value;
     45
     46    inline BlendFactor() : m_value(Zero) {}
     47    inline BlendFactor(Value v) : m_value(v) {}
     48    inline operator Value() { return m_value; }
     49};
     50
    2451class Renderer
    2552{
     
    4168    bool GetAlphaBlend() const;
    4269
     70    void SetBlendFunc(BlendFactor src, BlendFactor dst);
     71    BlendFactor GetBlendFuncSrc() const;
     72    BlendFactor GetBlendFuncDst() const;
     73
     74    void SetAlphaTest(bool set);
     75    bool GetAlphaTest() const;
     76
    4377    void SetDepthTest(bool set);
    4478    bool GetDepthTest() const;
  • trunk/src/scene.cpp

    r2752 r2760  
    195195    rc.SetDepthTest(true);
    196196    rc.SetAlphaBlend(true);
     197    rc.SetBlendFunc(BlendFactor::SrcAlpha, BlendFactor::OneMinusSrcAlpha);
    197198
    198199#if defined USE_D3D9 || defined _XBOX
     
    203204    glAlphaFunc(GL_GEQUAL, 0.01f);
    204205#endif
    205     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    206206#endif
    207207
  • trunk/tools/neercs/video/render.cpp

    r2753 r2760  
    14541454    RenderContext rc;
    14551455    rc.SetAlphaBlend(true);
     1456    rc.SetBlendFunc(BlendFactor::SrcColor, BlendFactor::DstAlpha);
    14561457
    14571458    glEnable(GL_TEXTURE_2D);
    1458     glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA);
    14591459
    14601460    rc.SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
Note: See TracChangeset for help on using the changeset viewer.