Skip to content

Instantly share code, notes, and snippets.

@XProger
Created May 4, 2015 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XProger/155262f898ceb2368323 to your computer and use it in GitHub Desktop.
Save XProger/155262f898ceb2368323 to your computer and use it in GitHub Desktop.
MMX: fast integer alpha blending
macro CANVAS_SET_ALPHA col { ; prepare mmx registers (alpha) by col (0xAAxxxxxx)
push col
shr col, 24
pxor mm5, mm5
movd mm7, col
pshufw mm7, mm7, 0
neg col
add col, 256
movd mm6, col
pshufw mm6, mm6, 0
pop col
}
; Result := (Frgb*Fa + Brgb*(256-Fa))/256
macro CANVAS_SET_PIXEL dst_ptr, color {
movd mm0, [dst_ptr] ; mm0(A) = 0 0 0 0 | 0 Ra Ga Ba
movd mm1, color ; mm1(B) = 0 0 0 0 | 0 Rb Gb Bb
punpcklbw mm0, mm5 ; mm0 = 0 0 0 Ra | 0 Ga 0 Ba
punpcklbw mm1, mm5 ; mm1 = 0 0 0 Rb | 0 Gb 0 Bb
pmullw mm0, mm6 ; mm0 = 0 Ra*X | Ga*X Ba*X
pmullw mm1, mm7 ; mm1 = 0 Rb*Y | Gb*Y Bb*Y
paddusw mm0, mm1 ; mm0 = 0 Ra*X+Rb*y | Ga*X+Gb*y Ba*X+Bb*Y
psrlw mm0, 8 ; mm0 = 0 0 0 Rc | 0 Gc 0 Bc
packuswb mm0, mm0 ; mm0 = 0 0 0 0 | 0 Rc Gc Bc
movd [dst_ptr], mm0
}
...
mov eax, 0x800000ff ; red color with 50% alpha
CANVAS_SET_ALPHA eax ; prepare MMX alpha-registers
CANVAS_SET_PIXELA esi,eax ; blend pixel from [esi] to eax by alpha, and write it to [esi]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment