SDL2 texture update speed

SergeyK

I'm trying to setup SDL2 environment for future running software rendering examples, so i need direct access to pixels to draw. Here is some code, that draws 1 red pixel to texture, then displaying a it said https://wiki.libsdl.org/MigrationGuide#If_your_game_just_wants_to_get_fully-rendered_frames_to_the_screen

#include <SDL.h>
#include <stdio.h>

const int SCREEN_WIDTH = 1920;
const int SCREEN_HEIGHT = 1080;

SDL_Window* gWindow;
SDL_Renderer* gRenderer;
SDL_Texture* gTexture;
SDL_Event e;

void* gPixels = NULL;
int gPitch = SCREEN_WIDTH * 4;

bool gExitFlag = false;

Uint64 start;
Uint64 end;
Uint64 freq;
double seconds;

int main(int argc, char* args[])
{
    SDL_Init(SDL_INIT_VIDEO);
    gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
    gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); // | SDL_RENDERER_PRESENTVSYNC); vsync is turned off
    gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);

    while (!gExitFlag)
    {
        while (SDL_PollEvent(&e) != 0)
        {
            if (e.type == SDL_QUIT)
            {
                gExitFlag = true;
            }
        }

        start = SDL_GetPerformanceCounter();

        SDL_LockTexture(gTexture, NULL, &gPixels, &gPitch);
        *((uint32_t*)gPixels) = 0xff000ff;
        SDL_UnlockTexture(gTexture); //20-100ms on different hardware

        end = SDL_GetPerformanceCounter();
        freq = SDL_GetPerformanceFrequency();

        SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
        SDL_RenderPresent(gRenderer);

        gPixels = NULL;
        gPitch = 0;

        seconds = (end - start) / static_cast<double>(freq);
        printf("Frame time: %fms\n", seconds * 1000.0);
    }

    SDL_DestroyWindow(gWindow);
    SDL_DestroyRenderer(gRenderer);
    SDL_DestroyTexture(gTexture);

    SDL_Quit();
    return 0;
}

As i mention in the code comment SDL_UnlockTexture gets up to 100ms with fullhd texture. (Switching to SDL_UpdateTexture cause no significant difference) It is too much for realtime rendering i think. Am i doing something wrong or i should not use at all texture API(or any other GPU-accelerated api, where texture must be uploaded to gpu memory every frame) for realtime rendering whole frame?

Peter K

As you want to work with raw pixeldata, you should use SDL's SDL_Surfaces and not textures. It's different SDL API optimized for your case, see this example and dont forget to update.

The reason for this is that textures are stored in VRAM and reads from VRAM are very slow. Surfaces are stored in RAM, processed there and are only written to VRAM, which is very fast.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Texture not displaying SDL2

What is Renderer and Texture in SDL2, conceptually?

How to rotate a texture in SDL2?

SDL2 - render on texture alpha channels

SDL2 with OpenGL texture displaying incorrectly

Rectangle to Texture in SDL2 C

SDL2 2D texture allocation/pooling

SDL2 Create Texture from different thread on OSX

iOS SDL2 OpenGL ES Cannot Draw Texture

How to use SDL2 Texture as sort of updating canvas?

How do i display an OpenGL texture in SDL2

SDL2 texture's pixel format and surface color mask

How to render opengl 3.3 into an SDL2 Texture?

Fastest way to load a texture in C++ with SDL2

C++ SDL2 Error when trying to render SDL_Texture: Invalid texture

Can't create SDL2 texture using SDL_PIXELFORMAT_NV12 pixel format

Why doesn't my SDL2 code display images with SDL_Texture*

Texture sampling coordinates seemingly scaled by factor of 2 in OpenGL/SDL2 tutorial

Two texture scrolling for simple water effect using SDL2 won't work as expected

Creating OpenGL texture from SDL2 surface - strange pixel values

SDL2 Texture sometimes empty after loading multiple 8 bit surfaces

How to render a rectangle SDL2 texture from a buffer of hex values?

What's the best way to read texture info from an image file and get to the pixels using SDL2?

SDL2 IMG_LoadTexture can't find the texture from asset - Android

Modifying pixel RGBA values of texture using glTexSubImage2D does not update the texture

I'm trying to update a texture with glTexSubImage2D

Update Texture2D pixels from C++

CUDA: Is texture memory still useful to speed up access times for compute capability 2.x and newer?

Is it possible to speed up matrix multiplication with texture memory?