PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Blit with mask + Blending = trouble

Blit with Full Alpha Support (J Elmore) + Blending = trouble
-----
When i use this Blit with display->SetBlending()
surfaces displays with bugs like on attached screenshot.
And when i set grayscale shader surfaces becomes invisible.

how can i fix this trouble?
---------------------------------------------------
Когда я юзаю Blit с display->SetBlending(), то все нарисованное отображается как на прикрепленном скрине. А когда я включаю серый шейдер, то вообще все рисуемое под маской пропадает.

Как это побороть?

-------------------
Thanks - спасибо =)
Attachments:
screen.jpg
If you look into alphablending code, you probably find fast_rx's message that custom shaders are not supported by this routine :).
So, the answer is - you cant.

Or, in russian (хрен тебе :)) .

Anyway, blitting with alpha and custom shader should affect FPS dramatically.
I'll investigate the problem with setBlending. It's been a long time since I've looked at the code though.

BTW, You can blit a new alpha mask using the blending value and get the correct effect - but it's another blit, so speed may be an issue unless you can do it beforehand.
Ok, so here's the fix:

In raster2d.cpp
Code:

void AlphaRaster2D::BlitAlpha( PHAL::Surface& buffer, const Rect& destRect, const PHAL::Surface& source, const PHAL::Surface& alphasource, const Rect& srcRect )
{

//todo: optimize
const Pixel* pSrc = source.GetPixels( srcRect.left, srcRect.top );
Pixel* pDest = buffer.GetPixels( destRect.left, destRect.top );
const Pixel* pAlphaSrc = alphasource.GetPixels( srcRect.left, srcRect.top );

const int bufferpitch = buffer.GetPitch()>>1;
const int sourcepitch = source.GetPitch()>>1;

for(int h = srcRect.GetHeight(); h ; --h)
{
Pixel *dest = (pDest);
const Pixel *src = (pSrc);
const Pixel *AlphaPixel = pAlphaSrc;

for(int w = srcRect.GetWidth(); w ; --w)
{
int m_alpha2 = ((*AlphaPixel & GREEN_MASK)>>(GREEN_SHIFT-2 +3 )) ;
m_alpha2 = (m_alpha * m_alpha2) >>8; //error here - was >> 6
if(m_alpha2)
{
const unsigned long mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);

unsigned long d = *src;
unsigned long s = *dest;

//split into xGxRxB
d = (d | (d<<16)) & mask;
s = (s | (s<<16)) & mask;

//do alpha op
d = ((((d-s)*(m_alpha2))>>5) + s)&mask; //error here - was m_alpha

//recombine into RGB
d |= d >> 16;
*dest = (Pixel)d;
}

dest++;
AlphaPixel++;
src++;
}

pDest += bufferpitch;
pSrc += sourcepitch;
pAlphaSrc += sourcepitch;
}
}


Note: there were actually 2 errors, the first was that I was shifting by 6 instead of 8 (for some reason, I thought that the alpha values were 0-63) and then I wasn't using the 'modified' alpha in the 'alpha op' section. I really thought I had tested this, but you must be the first to have used it.

A note about the custom shader not working... It is defined as { }, so that's why it doesn't draw anything. The reason it's not implemented is that the way pixel shaders are set up, they cannot get access to both surfaces (img and alpha), so if you really wanted it to work - you would have to make some changes to the whole shader framework...