From a26e06929e862aa7ffe38776cf6f2453ec3a30ff Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sat, 30 Mar 2013 09:44:13 -0700 Subject: [PATCH] Fixed issue#26 blend factor logic. --- src/renderer_d3d9.cpp | 46 +++++++++++++++++++++++++------------------ src/renderer_gl.cpp | 41 ++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index aa376221..1c1020e3 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -35,22 +35,29 @@ namespace bgfx { D3DMULTISAMPLE_16_SAMPLES, 0 }, }; - static const D3DBLEND s_blendFactor[][2] = + struct Blend { - { (D3DBLEND)0, (D3DBLEND)0 }, // ignored - { D3DBLEND_ZERO, D3DBLEND_ZERO }, - { D3DBLEND_ONE, D3DBLEND_ONE }, - { D3DBLEND_SRCCOLOR, D3DBLEND_SRCCOLOR }, - { D3DBLEND_INVSRCCOLOR, D3DBLEND_INVSRCCOLOR }, - { D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA }, - { D3DBLEND_INVSRCALPHA, D3DBLEND_INVSRCALPHA }, - { D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA }, - { D3DBLEND_INVDESTALPHA, D3DBLEND_INVDESTALPHA }, - { D3DBLEND_DESTCOLOR, D3DBLEND_DESTCOLOR }, - { D3DBLEND_INVDESTCOLOR, D3DBLEND_INVDESTCOLOR }, - { D3DBLEND_SRCALPHASAT, D3DBLEND_ONE }, - { D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR }, - { D3DBLEND_INVBLENDFACTOR, D3DBLEND_INVBLENDFACTOR }, + D3DBLEND m_src; + D3DBLEND m_dst; + bool m_factor; + }; + + static const Blend s_blendFactor[] = + { + { (D3DBLEND)0, (D3DBLEND)0, false }, // ignored + { D3DBLEND_ZERO, D3DBLEND_ZERO, false }, + { D3DBLEND_ONE, D3DBLEND_ONE, false }, + { D3DBLEND_SRCCOLOR, D3DBLEND_SRCCOLOR, false }, + { D3DBLEND_INVSRCCOLOR, D3DBLEND_INVSRCCOLOR, false }, + { D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA, false }, + { D3DBLEND_INVSRCALPHA, D3DBLEND_INVSRCALPHA, false }, + { D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA, false }, + { D3DBLEND_INVDESTALPHA, D3DBLEND_INVDESTALPHA, false }, + { D3DBLEND_DESTCOLOR, D3DBLEND_DESTCOLOR, false }, + { D3DBLEND_INVDESTCOLOR, D3DBLEND_INVDESTCOLOR, false }, + { D3DBLEND_SRCALPHASAT, D3DBLEND_ONE, false }, + { D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR, true }, + { D3DBLEND_INVBLENDFACTOR, D3DBLEND_INVBLENDFACTOR, true }, }; static const D3DCMPFUNC s_depthFunc[] = @@ -2394,16 +2401,17 @@ namespace bgfx uint32_t src = blend&0xf; uint32_t dst = (blend>>4)&0xf; - DX_CHECK(device->SetRenderState(D3DRS_SRCBLEND, s_blendFactor[src][0]) ); - DX_CHECK(device->SetRenderState(D3DRS_DESTBLEND, s_blendFactor[dst][1]) ); + DX_CHECK(device->SetRenderState(D3DRS_SRCBLEND, s_blendFactor[src].m_src) ); + DX_CHECK(device->SetRenderState(D3DRS_DESTBLEND, s_blendFactor[dst].m_dst) ); // DX_CHECK(device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_SRCALPHA) ); // DX_CHECK(device->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA) ); - if (0 != (blend&(BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_FACTOR, BGFX_STATE_BLEND_FACTOR) ) ) + if ( (s_blendFactor[src].m_factor || s_blendFactor[dst].m_factor) && blendFactor != state.m_rgba) { blendFactor = state.m_rgba; - DX_CHECK(device->SetRenderState(D3DRS_BLENDFACTOR, blendFactor) ); + D3DCOLOR color = D3DCOLOR_RGBA(blendFactor>>24, (blendFactor>>16)&0xff, (blendFactor>>8)&0xff, blendFactor&0xff); + DX_CHECK(device->SetRenderState(D3DRS_BLENDFACTOR, color) ); } } } diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index cb1426be..6ea878f1 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -556,22 +556,29 @@ namespace bgfx GL_FLOAT, }; - static const GLenum s_blendFactor[][2] = + struct Blend { - { 0, 0 }, // ignored - { GL_ZERO, GL_ZERO }, - { GL_ONE, GL_ONE }, - { GL_SRC_COLOR, GL_SRC_COLOR }, - { GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR }, - { GL_SRC_ALPHA, GL_SRC_ALPHA }, - { GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, - { GL_DST_ALPHA, GL_DST_ALPHA }, - { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA }, - { GL_DST_COLOR, GL_DST_COLOR }, - { GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR }, - { GL_SRC_ALPHA_SATURATE, GL_ONE }, - { GL_CONSTANT_COLOR, GL_CONSTANT_COLOR }, - { GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR }, + GLenum m_src; + GLenum m_dst; + bool m_factor; + }; + + static const Blend s_blendFactor[] = + { + { 0, 0, false }, // ignored + { GL_ZERO, GL_ZERO, false }, + { GL_ONE, GL_ONE, false }, + { GL_SRC_COLOR, GL_SRC_COLOR, false }, + { GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, false }, + { GL_SRC_ALPHA, GL_SRC_ALPHA, false }, + { GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false }, + { GL_DST_ALPHA, GL_DST_ALPHA, false }, + { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, false }, + { GL_DST_COLOR, GL_DST_COLOR, false }, + { GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR, false }, + { GL_SRC_ALPHA_SATURATE, GL_ONE, false }, + { GL_CONSTANT_COLOR, GL_CONSTANT_COLOR, true }, + { GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, true }, }; static const GLenum s_depthFunc[] = @@ -2651,9 +2658,9 @@ namespace bgfx uint32_t src = blend&0xf; uint32_t dst = (blend>>4)&0xf; GL_CHECK(glEnable(GL_BLEND) ); - GL_CHECK(glBlendFunc(s_blendFactor[src][0], s_blendFactor[dst][1]) ); + GL_CHECK(glBlendFunc(s_blendFactor[src].m_src, s_blendFactor[dst].m_dst) ); - if (0 != (blend&(BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_FACTOR, BGFX_STATE_BLEND_FACTOR) ) ) + if ( (s_blendFactor[src].m_factor || s_blendFactor[dst].m_factor) && blendFactor != state.m_rgba) { blendFactor = state.m_rgba;