Offscreen drawing to alpha channel only

Hi,

does SDL support offscreen drawing to the alpha channel of a texture
only? For example, let’s suppose I have a SDL_TEXTUREACCESS_TARGET
texture A.

Now I want to draw into the alpha channel of this texture A. The color
channels shall be preserved. Precisely, I’d like to be able to do
the following things:

  1. Draw into the alpha channel of texture A using graphics primitives
    like SDL_RenderFillRect() or SDL_RenderDrawLine().

  2. Copy the alpha channel from another texture to texture A. E.g.
    I have another texture, texture B, and I want to copy the alpha
    channel (or part of the alpha channel) of texture B to texture A.
    Once again, the three color channels in texture A shall not be
    touched by this operation. I only want to draw to texture A’s
    alpha channel. Whatever is in the color channels of texture A should
    be preserved.

Is that possible using hardware acceleration or do I have to go the
distance and use SDL_LockTexture() or SDL_UpdateTexture() to achieve
what I want?–
Best regards,
Andreas Falkenhahn mailto:@Andreas_Falkenhahn

SDL only supports a limited set of blend modes, so I don’t think this is
currently possible with hardware acceleration and only SDL:
https://wiki.libsdl.org/SDL_BlendMode

Using OpenGL directly, you can do this either by setting the blend function
appropriately (glBlendFunc) or by disabling writing to the color channels
(glColorMask).

Alternatively, SDL_gpu gives you a 2D API like SDL’s that has support for
blend functions and blend equations (as of r423 in the SVN repository - see
GPU_SetBlendFunction and GPU_SetShapeBlendFunction) that can easily do it.
I made a pixel art program with extra alpha channel tools that do this.

Jonny DOn Wed, Aug 13, 2014 at 6:12 AM, Andreas Falkenhahn wrote:

Hi,

does SDL support offscreen drawing to the alpha channel of a texture
only? For example, let’s suppose I have a SDL_TEXTUREACCESS_TARGET
texture A.

Now I want to draw into the alpha channel of this texture A. The color
channels shall be preserved. Precisely, I’d like to be able to do
the following things:

  1. Draw into the alpha channel of texture A using graphics primitives
    like SDL_RenderFillRect() or SDL_RenderDrawLine().

  2. Copy the alpha channel from another texture to texture A. E.g.
    I have another texture, texture B, and I want to copy the alpha
    channel (or part of the alpha channel) of texture B to texture A.
    Once again, the three color channels in texture A shall not be
    touched by this operation. I only want to draw to texture A’s
    alpha channel. Whatever is in the color channels of texture A should
    be preserved.

Is that possible using hardware acceleration or do I have to go the
distance and use SDL_LockTexture() or SDL_UpdateTexture() to achieve
what I want?


Best regards,
Andreas Falkenhahn mailto:andreas at falkenhahn.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL only supports a limited set of blend modes, so I don’t think
this is currently possible with hardware acceleration and only SDL:
https://wiki.libsdl.org/SDL_BlendMode

Using OpenGL directly, you can do this either by setting the blend
function appropriately (glBlendFunc) or by disabling writing to the color channels (glColorMask).

I’d like to avoid using OpenGL because the state machine makes my
head dizzy :wink:

Alternatively, SDL_gpu gives you a 2D API like SDL’s that has
support for blend functions and blend equations (as of r423 in the
SVN repository - see GPU_SetBlendFunction and
GPU_SetShapeBlendFunction) that can easily do it. I made a pixel
art program with extra alpha channel tools that do this.

Thanks for the pointer, though I’d prefer if it was possible with SDL
directly. Maybe in a future version.On 13.08.2014 at 14:57 Jonathan Dearborn wrote:


Best regards,
Andreas Falkenhahn mailto:@Andreas_Falkenhahn

Hi,

does SDL support offscreen drawing to the alpha channel of a texture
only? For example, let’s suppose I have a SDL_TEXTUREACCESS_TARGET
texture A.

Now I want to draw into the alpha channel of this texture A. The color
channels shall be preserved. Precisely, I’d like to be able to do
the following things:

  1. Draw into the alpha channel of texture A using graphics primitives
    like SDL_RenderFillRect() or SDL_RenderDrawLine().

  2. Copy the alpha channel from another texture to texture A. E.g.
    I have another texture, texture B, and I want to copy the alpha
    channel (or part of the alpha channel) of texture B to texture A.
    Once again, the three color channels in texture A shall not be
    touched by this operation. I only want to draw to texture A’s
    alpha channel. Whatever is in the color channels of texture A should
    be preserved.

Is that possible using hardware acceleration or do I have to go the
distance and use SDL_LockTexture() or SDL_UpdateTexture() to achieve
what I want?

I made a patch to SDL with my own blend mode that does something very
similar. You can find it in bug 2594:
https://bugzilla.libsdl.org/show_bug.cgi?id=2594 – I think it contains
what you need to be able to change it up to fit your needs as well.

It’s not a lot of code, the biggest part of the job is to maintain your own
copy of the library - and deal with building it with your adaptations.

RegardsOn Wed, Aug 13, 2014 at 12:12 PM, Andreas Falkenhahn <andreas at falkenhahn.com wrote:

Trygve Vea

Hi Trygve,On 14.08.2014 at 02:24 Trygve Vea wrote:

On Wed, Aug 13, 2014 at 12:12 PM, Andreas Falkenhahn <@Andreas_Falkenhahn>wrote:

Hi,

does SDL support offscreen drawing to the alpha channel of a texture
only? For example, let’s suppose I have a SDL_TEXTUREACCESS_TARGET
texture A.

Now I want to draw into the alpha channel of this texture A. The color
channels shall be preserved. Precisely, I’d like to be able to do
the following things:

  1. Draw into the alpha channel of texture A using graphics primitives
    like SDL_RenderFillRect() or SDL_RenderDrawLine().

  2. Copy the alpha channel from another texture to texture A. E.g.
    I have another texture, texture B, and I want to copy the alpha
    channel (or part of the alpha channel) of texture B to texture A.
    Once again, the three color channels in texture A shall not be
    touched by this operation. I only want to draw to texture A’s
    alpha channel. Whatever is in the color channels of texture A should
    be preserved.

Is that possible using hardware acceleration or do I have to go the
distance and use SDL_LockTexture() or SDL_UpdateTexture() to achieve
what I want?

I made a patch to SDL with my own blend mode that does something
very similar. You can find it in bug 2594:
https://bugzilla.libsdl.org/show_bug.cgi?id=2594 – I think it
contains what you need to be able to change it up to fit your needs as well.

It’s not a lot of code, the biggest part of the job is to maintain
your own copy of the library - and deal with building it with your adaptations.

Thanks, this code looks simple and straightforward enough, though I’d need
it the other way round, i.e. color channels should be left untouched and
it should only write to the alpha channel (but without blending anything;
the values in the alpha channel should just be overwritten). But I’m sure
that this could be adapted.


Best regards,
Andreas Falkenhahn mailto:@Andreas_Falkenhahn