In order to cope with high memory consumption, remove the 'rectangles' member from OpenGLRenderer structure (#152)

This commit is contained in:
nikitar020 2019-01-17 12:18:31 +00:00 committed by Liam Galvin
parent 6f11a23e6a
commit 1b843e338d
2 changed files with 15 additions and 67 deletions

View File

@ -11,8 +11,6 @@ import (
) )
type OpenGLRenderer struct { type OpenGLRenderer struct {
font *glfont.Font
boldFont *glfont.Font
areaWidth int areaWidth int
areaHeight int areaHeight int
areaX int areaX int
@ -22,7 +20,6 @@ type OpenGLRenderer struct {
termCols uint termCols uint
termRows uint termRows uint
cellPositions map[[2]uint][2]float32 cellPositions map[[2]uint][2]float32
rectangles map[[2]uint]*rectangle
config *config.Config config *config.Config
colourAttr uint32 colourAttr uint32
program uint32 program uint32
@ -48,34 +45,9 @@ func (r *OpenGLRenderer) CellHeight() float32 {
return r.cellHeight return r.cellHeight
} }
func (r *OpenGLRenderer) Clean() { func (r *OpenGLRenderer) newRectangle(x float32, y float32, colourAttr uint32) *rectangle {
for _, rect := range r.rectangles {
rect.Free()
}
r.rectangles = map[[2]uint]*rectangle{} rect := &rectangle{}
}
func (r *OpenGLRenderer) initRectangle(rect *rectangle, x float32, y float32, colourAttr uint32) {
if rect == nil {
panic("rect pointer is nil")
}
if rect.vao != 0 {
gl.DeleteVertexArrays(1, &rect.vao)
rect.vao = 0
}
if rect.vbo != 0 {
gl.DeleteBuffers(1, &rect.vbo)
rect.vbo = 0
}
if rect.cv != 0 {
gl.DeleteBuffers(1, &rect.cv)
rect.cv = 0
}
halfAreaWidth := float32(r.areaWidth / 2) halfAreaWidth := float32(r.areaWidth / 2)
halfAreaHeight := float32(r.areaHeight / 2) halfAreaHeight := float32(r.areaHeight / 2)
@ -114,13 +86,6 @@ func (r *OpenGLRenderer) initRectangle(rect *rectangle, x float32, y float32, co
gl.GenBuffers(1, &rect.cv) gl.GenBuffers(1, &rect.cv)
rect.setColour([3]float32{0, 1, 0}) rect.setColour([3]float32{0, 1, 0})
}
func (r *OpenGLRenderer) newRectangle(x float32, y float32, colourAttr uint32) *rectangle {
rect := &rectangle{}
r.initRectangle(rect, x, y, colourAttr)
return rect return rect
} }
@ -171,7 +136,6 @@ func NewOpenGLRenderer(config *config.Config, fontMap *FontMap, areaX int, areaY
areaX: areaX, areaX: areaX,
areaY: areaY, areaY: areaY,
cellPositions: map[[2]uint][2]float32{}, cellPositions: map[[2]uint][2]float32{},
rectangles: map[[2]uint]*rectangle{},
config: config, config: config,
colourAttr: colourAttr, colourAttr: colourAttr,
program: program, program: program,
@ -184,11 +148,6 @@ func NewOpenGLRenderer(config *config.Config, fontMap *FontMap, areaX int, areaY
// This method ensures that all OpenGL resources are deleted correctly // This method ensures that all OpenGL resources are deleted correctly
func (r *OpenGLRenderer) Free() { func (r *OpenGLRenderer) Free() {
for _, rect := range r.rectangles {
rect.Free()
}
r.rectangles = map[[2]uint]*rectangle{}
for _, tex := range r.textureMap { for _, tex := range r.textureMap {
gl.DeleteTextures(1, &tex) gl.DeleteTextures(1, &tex)
} }
@ -215,31 +174,21 @@ func (r *OpenGLRenderer) SetArea(areaX int, areaY int, areaWidth int, areaHeight
//= f.LineHeight() // includes vertical padding //= f.LineHeight() // includes vertical padding
r.termCols = uint(math.Floor(float64(float32(r.areaWidth) / r.cellWidth))) r.termCols = uint(math.Floor(float64(float32(r.areaWidth) / r.cellWidth)))
r.termRows = uint(math.Floor(float64(float32(r.areaHeight) / r.cellHeight))) r.termRows = uint(math.Floor(float64(float32(r.areaHeight) / r.cellHeight)))
r.Clean()
} }
func (r *OpenGLRenderer) getRectangle(col uint, row uint) *rectangle { func (r *OpenGLRenderer) getRectangle(col uint, row uint) *rectangle {
x := float32(float32(col) * r.cellWidth) x := float32(float32(col) * r.cellWidth)
y := float32(float32(row) * r.cellHeight) + r.cellHeight y := float32(float32(row) * r.cellHeight) + r.cellHeight
coords := [2]uint{col, row} return r.newRectangle(x, y, r.colourAttr)
rect, ok := r.rectangles[coords]
if ok {
r.initRectangle(rect, x, y, r.colourAttr)
return rect
} else {
rect = r.newRectangle(x, y, r.colourAttr)
r.rectangles[coords] = rect
return rect
}
} }
func (r *OpenGLRenderer) DrawCursor(col uint, row uint, colour config.Colour) { func (r *OpenGLRenderer) DrawCursor(col uint, row uint, colour config.Colour) {
rect := r.getRectangle(col, row) rect := r.getRectangle(col, row)
rect.setColour(colour) rect.setColour(colour)
rect.Draw() rect.Draw()
rect.Free()
} }
func (r *OpenGLRenderer) DrawCellBg(cell buffer.Cell, col uint, row uint, cursor bool, colour *config.Colour, force bool) { func (r *OpenGLRenderer) DrawCellBg(cell buffer.Cell, col uint, row uint, cursor bool, colour *config.Colour, force bool) {
@ -261,6 +210,8 @@ func (r *OpenGLRenderer) DrawCellBg(cell buffer.Cell, col uint, row uint, cursor
rect := r.getRectangle(col, row) rect := r.getRectangle(col, row)
rect.setColour(bg) rect.setColour(bg)
rect.Draw() rect.Draw()
rect.Free()
} }
} }
@ -340,5 +291,4 @@ func (r *OpenGLRenderer) DrawCellImage(cell buffer.Cell, col uint, row uint) {
gl.COLOR_BUFFER_BIT, gl.LINEAR) gl.COLOR_BUFFER_BIT, gl.LINEAR)
gl.BindFramebuffer(gl.READ_FRAMEBUFFER, 0) gl.BindFramebuffer(gl.READ_FRAMEBUFFER, 0)
gl.DeleteFramebuffers(1, &readFboId) gl.DeleteFramebuffers(1, &readFboId)
} }

View File

@ -79,8 +79,6 @@ DONE:
addLine() addLine()
} }
gui.renderer.Clean()
for hx := col; hx < col+uint16(longestLine)+1; hx++ { for hx := col; hx < col+uint16(longestLine)+1; hx++ {
for hy := row - 1; hy < row+uint16(len(lines))+1; hy++ { for hy := row - 1; hy < row+uint16(len(lines))+1; hy++ {
gui.renderer.DrawCellBg(buffer.NewBackgroundCell(bg), uint(hx), uint(hy), false, nil, true) gui.renderer.DrawCellBg(buffer.NewBackgroundCell(bg), uint(hx), uint(hy), false, nil, true)