aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/renderer/GPUMemoryManager.java
blob: 7693b4a9f1e87ad733215a584fd746728e7caacf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package makamys.lodmod.renderer;

import static org.lwjgl.opengl.GL15.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import makamys.lodmod.renderer.Mesh.GPUStatus;
import makamys.lodmod.util.GuiHelper;

public class GPUMemoryManager {
    
    private static int BUFFER_SIZE = 1024 * 1024 * 1024;
    
    public int VBO;
    
    private int nextMesh;
    
    private List<Mesh> sentMeshes = new ArrayList<>();
    
    public GPUMemoryManager() {
        VBO = glGenBuffers();
        
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        
        glBufferData(GL_ARRAY_BUFFER, BUFFER_SIZE, GL_DYNAMIC_DRAW);
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    
    public void runGC() {
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
            
        int deletedNum = 0;
        int deletedRAM = 0;
        
        long t0 = System.nanoTime();
        
        int moved = 0;
        
        Mesh startMesh = null;
        
        while(moved < 5 && !sentMeshes.isEmpty()) {
            if(nextMesh >= sentMeshes.size()) {
                nextMesh = 0;
            }
            Mesh mesh = sentMeshes.get(nextMesh);
            
            if(mesh == startMesh) {
                break; // we have wrapped around
            } else if(startMesh == null) {
                startMesh = mesh;
            }
            
            if(mesh.gpuStatus == GPUStatus.SENT) {
                int offset = nextMesh == 0 ? 0 : sentMeshes.get(nextMesh - 1).getEnd();
                if(mesh.offset != offset) {
                    glBufferSubData(GL_ARRAY_BUFFER, offset, mesh.buffer);
                    moved++;
                }
                mesh.iFirst = offset / mesh.getStride();
                mesh.offset = offset;
                nextMesh++;
            } else if(mesh.gpuStatus == GPUStatus.PENDING_DELETE) {
                mesh.iFirst = mesh.offset = -1;
                mesh.visible = false;
                mesh.gpuStatus = GPUStatus.UNSENT;
                mesh.destroyBuffer();
                
                sentMeshes.remove(nextMesh);
                
                deletedNum++;
                deletedRAM += mesh.bufferSize();
            }
        }
        
        long t1 = System.nanoTime();
        
        //System.out.println("Deleted " + deletedNum + " meshes in " + ((t1 - t0) / 1_000_000.0) + " ms, freeing up " + (deletedRAM / 1024 / 1024) + "MB of VRAM");
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    
    private int malloc(int size) {
        int nextBase = 0;
        if(!sentMeshes.isEmpty()) {
            nextBase = sentMeshes.get(sentMeshes.size() - 1).getEnd();
        }
        
        if(nextBase + size >= BUFFER_SIZE) {
            return -1;
        } else {
            return nextBase;
        }
    }
    
    public void sendMeshToGPU(Mesh mesh) {
        if(mesh == null) {
            return;
        }
        
        int nextMeshOffset = malloc(mesh.buffer.limit());
        
        if(nextMeshOffset == -1) {
            return;
        }
        
        if(mesh.gpuStatus == GPUStatus.UNSENT) {
            mesh.prepareBuffer();
            
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            
            glBufferSubData(GL_ARRAY_BUFFER, nextMeshOffset, mesh.buffer);
            mesh.iFirst = nextMeshOffset / mesh.getStride();
            mesh.iCount = mesh.quadCount * 6;
            mesh.offset = nextMeshOffset;
            
            sentMeshes.add(mesh);
            
            glBindBuffer(GL_ARRAY_BUFFER, 0);
        }
        
        mesh.gpuStatus = GPUStatus.SENT;
    }
    
    public void deleteMeshFromGPU(Mesh mesh) {
        if(mesh == null || mesh.gpuStatus == GPUStatus.UNSENT) {
            return;
        }
        mesh.gpuStatus = GPUStatus.PENDING_DELETE;
    }

    public void destroy() {
        glDeleteBuffers(VBO);
    }

    public List<String> getDebugText() {
        return Arrays.asList("VRAM: " + (malloc(0) / 1024 / 1024) + "MB / " + (BUFFER_SIZE / 1024 / 1024) + "MB");
    }

    public void drawInfo() {
        int scale = 10000;
        int rowLength = 512;
        int yOff = 20;
        
        int meshI = 0;
        for(Mesh mesh : sentMeshes) {
            
            int o = mesh.offset / 10000;
            int o2 = (mesh.offset + mesh.buffer.limit()) / 10000;
            if(o / rowLength == o2 / rowLength) {
                if(mesh.gpuStatus != Mesh.GPUStatus.PENDING_DELETE) {
                    GuiHelper.drawRectangle(o % rowLength, o / rowLength + yOff, mesh.buffer.limit() / scale + 1, 1, meshI == nextMesh ? 0x00FF00 : 0xFFFFFF);
                }
            } else {
                for(int i = o; i < o2; i++) {
                    int x = i % rowLength;
                    int y = i / rowLength;
                    if(mesh.gpuStatus != Mesh.GPUStatus.PENDING_DELETE) {
                        GuiHelper.drawRectangle(x, y + yOff, 1, 1, 0xFFFFFF);
                    }
                }
            }
            meshI++;
        }
        GuiHelper.drawRectangle(0 % rowLength, 0 + yOff, 4, 4, 0x00FF00);
        GuiHelper.drawRectangle((BUFFER_SIZE / scale) % rowLength, (BUFFER_SIZE / scale) / rowLength + yOff, 4, 4, 0xFF0000);
    }
    
}