SDL3 - SDL_CreateTextureFromSurface

For some reason, I cant get the textures returned by this function to render. It either renders nothing or a white rectangle. If I manually create a texture and use SDL_UpdateTexture this renders fine. SDL_CreateTextureFromSurface does not return an error and the white rectangle that sometimes does get rendered is at least the right size. The video driver is DirectX 11 and Windows 10.

Please provide a Minimal Reproducible Example (MRE) so we can try to reproduce your problem.
Thanks!

Ok, here is some code that currently fails. image will render fine but not texture.

SDL_Texture *image = LoadTexture("assets/c64.png");
SDL_RenderClear(renderer);
SDL_RenderTexture(renderer, image, NULL, NULL);

SDL_Surface *surface = SDL_RenderReadPixels(renderer, NULL);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);


while (1) {
    HandleEvents();
    SDL_RenderClear(renderer);
    SDL_RenderTexture(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);
}

Next time, please provide full source code. Code which can be passed to the compiler unchanged.

I adapted your code slightly (loading a bmp instead of a png), and it appears to work fine?

#define SDL_ASSERT_LEVEL 2
#include <SDL3/SDL.h>

int main(int argc, char *argv[]) {
    SDL_assert(SDL_Init(SDL_INIT_VIDEO));
    SDL_Window *window;
    SDL_Renderer *renderer;
    if (argc < 2) {
        SDL_Log("Need an argument");
        return 1;
    }
    SDL_Surface *image_surface = SDL_LoadBMP(argv[1]);
    if (!image_surface) {
        SDL_Log("Failed to load %s (%s)", argv[1]);
        SDL_Quit();
        return 1;
    }
    SDL_assert(SDL_CreateWindowAndRenderer("title", 640, 480, 0, &window, &renderer));
    SDL_Texture *image_texture = SDL_CreateTextureFromSurface(renderer, image_surface);
    SDL_assert(image_texture);
    SDL_DestroySurface(image_surface);
    SDL_assert(SDL_RenderClear(renderer));
    SDL_assert(SDL_RenderTexture(renderer, image_texture, NULL, NULL));
    SDL_DestroyTexture(image_texture);

    SDL_Surface *surface_read = SDL_RenderReadPixels(renderer, NULL);
    SDL_assert(surface_read);
    SDL_Texture *texture_read = SDL_CreateTextureFromSurface(renderer, surface_read);
    SDL_assert(texture_read);
    SDL_DestroySurface(surface_read);

    while (1) {
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_EVENT_QUIT) {
                goto finish;
            }
        }
        SDL_assert(SDL_RenderClear(renderer));
        SDL_assert(SDL_RenderTexture(renderer, texture_read, NULL, NULL));
        SDL_assert(SDL_RenderPresent(renderer));
    }
finish:
    SDL_DestroyTexture(texture_read);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

I still just get a black screen. It was working before in an older version so I just replaced the function with the older version recompiled it and it worked. Then I used WinMerge to compare the differences it showed that the only difference with the older version that works is a single line:

This is from the version that shows the image okay:
texture_colorspace = SDL_GetSurfaceColorspace(surface);

And this is from the lastest code that iis only showing a black screen for me:
surface_colorspace = SDL_GetSurfaceColorspace(surface);

Can you please report this as a bug on GitHub?

Please include the code you have here as well as the asset that’s failing and your LoadTexture() implementation.

Thanks!