While volzhs answer is valid for static brushes i needed a way to draw like one is able to in the canvasItem draw method (drawcircle, drawrect, draw_polygon...)
So here is what i ended up with:
Disclaimer: While this is working i am sure there are more efficient solutions
Create the mask renderer
1. Create a Viewport and set
Render Target to enabled
2. Add a Node2D (or any CanvasItem) as child and use the _draw method to draw your mask. Paint everything which should be visible with white.
Use the mask
In GDScript use my_viewport.
get_render_target_texture() to get your texture.
If you want to only show masked areas just plug that texture into a LightMask2D (See link posted by volzhs). In this case you are done and can stop reading.
If you want the reverse case (hide everything masked) you need to write a shader (at least that was the easiest way i could think of). Don't fret, its easy.
Create a material and set the shader to
In the fragment shader wire it up like seen below:
In the shader we defined two parameter (Uniforms): One is the Texture we want to mask and the other our mask texture.
In GDScript you can provide them to the shader like:
This approach is using a 4 channel (r,g,b,a) image for our mask and only uses 1, tho.
If it is somehow possible to use only one chanel images that should improve efficiency.