Hello everybody. I have the following method:
SDL_Texture* SDLGraphicsImpl::AddColorsToTheTexture(SDL_Renderer* renderer, SDL_Texture* texture, DrawTrianglesCmd* cmd)
{
int w, h;
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
std::vector<Uint32> pixelBuffer(w * h);
Uint32* pixels = nullptr;
int pitch = 0;
if (SDL_LockTexture(texture, nullptr, reinterpret_cast<void**>(&pixels), &pitch) != 0)
{
ENGINE_LOG_WARNING("Failed to lock the texture for pixel modifications: %s", SDL_GetError());
return texture;
}
SDL_PixelFormat* pixelFormat = SDL_AllocFormat(texture->format);
Uint32 formatMask = pixelFormat->Rmask | pixelFormat->Gmask | pixelFormat->Bmask;
const Uint32 rShift = pixelFormat->Rshift;
const Uint32 gShift = pixelFormat->Gshift;
const Uint32 bShift = pixelFormat->Bshift;
#pragma omp parallel for
for (int i = 0; i < w * h; i++)
{
Uint32 pixel = pixels[i];
const Uint8 r = (pixel & pixelFormat->Rmask) >> rShift;
const Uint8 g = (pixel & pixelFormat->Gmask) >> gShift;
const Uint8 b = (pixel & pixelFormat->Bmask) >> bShift;
const Uint8 newR = (r + cmd->addition_color.Red > 255) ? 255 : r + cmd->addition_color.Red;
const Uint8 newG = (g + cmd->addition_color.Green > 255) ? 255 : g + cmd->addition_color.Green;
const Uint8 newB = (b + cmd->addition_color.Blue > 255) ? 255 : b + cmd->addition_color.Blue;
pixelBuffer[i] = (newR << pixelFormat->Rshift) |
(newG << pixelFormat->Gshift) |
(newB << pixelFormat->Bshift) |
(pixel & ~formatMask);
}
SDL_FreeFormat(pixelFormat);
SDL_UnlockTexture(texture);
SDL_UpdateTexture(texture, nullptr, pixelBuffer.data(), w * sizeof(Uint32));
return texture;
}
fps drops very much, how can it be fixed?