2

I am trying to write a C++ lambda that is registered and to be used in Lua using the Sol2 binding. The callback below should create an SDL_Texture, and clear it to a color. A Lua_Texture is just a wrapper for an SDL_Texture, and l_txt.texture is of type SDL_Texture*.

lua.set_function("init_texture",
    [render](Lua_Texture &l_txt, int w, int h)
    {
        // free any previous texture
        l_txt.deleteTexture();

        l_txt.texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);

        SDL_SetRenderTarget(render, l_txt.texture);
        SDL_Texture *target = SDL_GetRenderTarget(render);

        assert(l_txt.texture == target);
        assert(target == nullptr);

        SDL_SetRenderDrawColor(render, 0xFF, 0x22, 0x22, 0xFF);
        SDL_RenderClear(render);
    });

My problem is that SDL_SetRenderTarget isn't functioning as I'd expect it. I try to set the texture as the target so I can clear it's color, but when I try to draw the texture to the screen it is still blank. The asserts in the above code both fail, and show that the current target texture is not set to the texture I am trying to clear and later use, nor is it Null (which is the expected value if there is no current target texture).

I have used this snippet of code before in just c++ (not as a Lua callback) and it works as intended. Somehow, embedding it in Lua causes the behavior to change. Any help is very much appreciated as I've been pulling my hair out over this for a while, thanks!

4
  • Is it possible you're hitting a threading issue (ie. another thread is trampling that state)?
    – Varrak
    May 18, 2018 at 16:45
  • I only use a single thread at the moment, and this function is only called at the creation of a single object since I only just introduced this code to my project.
    – James
    May 18, 2018 at 16:53
  • It's possible the SDL_CreateTexture call is failing. SDL_SetRenderTarget(null) will set the render target to the default, which would explain why you're hitting these two asserts. Have you verified that l_txt.texture is not null after the create call?
    – Varrak
    May 18, 2018 at 17:00
  • Yes I have, that was kind of the point of the second assert, as if l_txt.texture was null then SDL_SetRenderTarget would have set the target texture to null and SDL_GetRenderTarget would have returned null as well, so assert(target == nullptr) would have passed. I had also previously verified with a print debug (lol)
    – James
    May 18, 2018 at 17:19

1 Answer 1

1

I may have an answer for you, but you're not going to like it.

It looks like SDL_GetRenderTarget doesn't work as expected.

I got the exact same problem you have (that's how I found your question), and I could reproduce it reliably using that simple program :

int rendererIndex;

[snipped code : rendererIndex is set to the index of the DX11 renderer]

SDL_Renderer * renderer = SDL_CreateRenderer(pWindow->pWindow, rendererIndex, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

SDL_Texture* rtTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 200, 200);

SDL_SetRenderTarget(renderer, rtTexture);

if(SDL_GetRenderTarget(renderer) != rtTexture)
  printf("ERROR.");

This always produces :

ERROR.

The workaround I used it that I'm saving the pointer to the render target texture I'm setting for the renderer and I don't use SDL_GetRenderTarget.

EDIT :

I was curious why I didn't get the correct render target when getting it, and I look through SDL2's source code. I found out why (code snipped for clarity) :

int
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
{

// CODE SNIPPED

    /* texture == NULL is valid and means reset the target to the window */
    if (texture) {
        CHECK_TEXTURE_MAGIC(texture, -1);
        if (renderer != texture->renderer) {
            return SDL_SetError("Texture was not created with this renderer");
        }
        if (texture->access != SDL_TEXTUREACCESS_TARGET) {
            return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
        }
// *** EMPHASIS MINE : This is the problem.
        if (texture->native) {
            /* Always render to the native texture */
            texture = texture->native;
        }
    }

// CODE SNIPPED

    renderer->target = texture;

// CODE SNIPPED
}

SDL_Texture *
SDL_GetRenderTarget(SDL_Renderer *renderer)
{
    return renderer->target;
}

In short, the renderer saves the current render target in renderer->target, but not before converting the current texture to it's native form. When we use SDL_GetRenderTarget, we're getting that native texture, which may or may not be different.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.