How to mask 2D tile sprites

I want to use masking technique to be able to reuse tiles. Basically I have the base texture and an array of masking textures for different corners and positions and want to combine the two to get a new texture.

enter image description here

I encountered this technique while browsing https://github.com/FergusGriggs/Fegaria-Remastered and want to learn more about it. Any help is appreciated. Sorry for my bad english.