Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VRAM leak on window resize #413

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Libraries/3D/Source/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void g3_interpret_object(ubyte *object_ptr, ...) {
}

// get addr of stack parms
parm_ptr = (&object_ptr) + sizeof(object_ptr); //va_start(parm_ptr, object_ptr);
parm_ptr = (ubyte*)(&object_ptr) + sizeof(object_ptr); //va_start(parm_ptr, object_ptr);

// mark res points as free
LG_memset(resbuf, 0, N_RES_POINTS * 4);
Expand Down
41 changes: 26 additions & 15 deletions src/MacSrc/OpenGL.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifdef USE_OPENGL

#include <cstdio>
#include <memory>

#include "OpenGL.h"

#ifdef _WIN32
Expand Down Expand Up @@ -75,7 +77,16 @@ static Shader textureShaderProgram;
static Shader colorShaderProgram;
static Shader starShaderProgram;

static FrameBuffer backupBuffer;
struct FrameBufferDeleter {
void operator()(FrameBuffer* fb) {
glDeleteFramebuffers(1, &fb->frameBuffer);
glDeleteRenderbuffers(1, &fb->stencilBuffer);
glDeleteTextures(1, &fb->texture);
}
};

using SmartFrameBuffer = std::unique_ptr<FrameBuffer, FrameBufferDeleter>;
static SmartFrameBuffer backupBuffer;

static SDL_GLContext context;
static GLuint dynTexture;
Expand Down Expand Up @@ -242,24 +253,24 @@ static int CreateShader(const char *vertexShaderFile, const char *fragmentShader
return 0;
}

static FrameBuffer CreateFrameBuffer(int width, int height) {
FrameBuffer newBuffer{};
newBuffer.width = width;
newBuffer.height = height;
static SmartFrameBuffer CreateFrameBuffer(int width, int height) {
SmartFrameBuffer newBuffer{new FrameBuffer{}};
newBuffer->width = width;
newBuffer->height = height;

// Make a frame buffer, texture for color, and render buffer for stencil
glGenFramebuffers(1, &newBuffer.frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, newBuffer.frameBuffer);
glGenFramebuffers(1, &newBuffer->frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, newBuffer->frameBuffer);

glGenRenderbuffers(1, &newBuffer.stencilBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, newBuffer.stencilBuffer);
glGenRenderbuffers(1, &newBuffer->stencilBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, newBuffer->stencilBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, newBuffer.stencilBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, newBuffer->stencilBuffer);

glGenTextures(1, &newBuffer.texture);
glBindTexture(GL_TEXTURE_2D, newBuffer.texture);
glGenTextures(1, &newBuffer->texture);
glBindTexture(GL_TEXTURE_2D, newBuffer->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, newBuffer.texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, newBuffer->texture, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
Expand Down Expand Up @@ -418,7 +429,7 @@ void opengl_start_frame() {
SDL_GL_MakeCurrent(window, context);

// Start rendering to our frame buffer canvas
BindFrameBuffer(&backupBuffer);
BindFrameBuffer(backupBuffer.get());

// Setup the render width
int logical_width, logical_height;
Expand Down Expand Up @@ -468,7 +479,7 @@ void opengl_swap_and_restore() {
glUniformMatrix4fv(textureShaderProgram.uniView, 1, false, IdentityMatrix);
glUniformMatrix4fv(textureShaderProgram.uniProj, 1, false, IdentityMatrix);

bind_texture(backupBuffer.texture);
bind_texture(backupBuffer->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Expand Down