Jump to content

Texture::SetPixels() Memory Leak


Go to solution Solved by Josh,

Recommended Posts

Posted

The following code will increase memory usage to about 12 GB before it goes back down to a normal amount.  My game gets up to 40GB with no signs of slowing.

#include "Engine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto framebuffer = CreateFramebuffer(window);
    auto world = CreateWorld();
  
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 2, -6);
    camera->SetDebugPhysicsMode(true);

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 35, 0);
    light->SetColor(3);

    auto size = 512;
    auto buffer = CreateBuffer(size * size * 4);
    auto texture = CreateTexture(TEXTURE_2D, size, size);

    for (int i = 0; i < 10000; i++) {
        texture->SetPixels(buffer);//The more this is called the worse the leak!
    }
    
    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

  • Solution
Posted

This algorithm creates an intermediate buffer that gets passed to the rendering thread. It's working as I would expect.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted

Ah okay.  Well that's okay.  I just need to change how I'm updating the texture then.  Is it okay to do this in a render hook?  Are render hooks before render or after?

RenderHook() {
texture->SetPixels(buffer);
}

 

Posted
9 minutes ago, SpiderPig said:

Ah okay.  Well that's okay.  I just need to change how I'm updating the texture then.  Is it okay to do this in a render hook?  Are render hooks before render or after?

RenderHook() {
texture->SetPixels(buffer);
}

Why? :blink: Transfer buffers are a very common technique.

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted

I have a texture for holding a lot of variables for shaders.  Sometimes those variables change, like 10'000 times at startup but only one or twice during game.  So I do need to get the updating variable to the shaders.  :)

Posted

You might want to change your design so some global variable gets set when something is changed, and then just submit the changes once if they occur. This is how I handle changes to mesh data, instead of submitting the mesh each time it just adds it to a list of meshes that need to be sent, and all updated meshes are submitted right before the render.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted

Yeah this is what I will do I think.  That's why I was wondering if it's okay to update the texture with the new buffer in the render hook.  It will look more like this.

RenderHook() {
	if(changes == true){
		changes = false;
		texture->SetPixels(buffer);
	}
}

 

Posted

As long as "changes" is not a variable both threads can access! But I would just use SetPixels from the main thread, it's safer and less likely to break down.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...